Add 3 new models: Container, PortMapping and VCluster

This commit is contained in:
zhuyj17 2018-03-11 16:29:54 +08:00
parent dc97b71550
commit 6b3c83cebb
2 changed files with 100 additions and 11 deletions

View File

@ -42,7 +42,8 @@ app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///'+fsdir+'/global/sys/UserTable.db' app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///'+fsdir+'/global/sys/UserTable.db'
app.config['SQLALCHEMY_BINDS'] = { app.config['SQLALCHEMY_BINDS'] = {
'history': 'sqlite:///'+fsdir+'/global/sys/HistoryTable.db', 'history': 'sqlite:///'+fsdir+'/global/sys/HistoryTable.db',
'beansapplication': 'sqlite:///'+fsdir+'/global/sys/BeansApplication.db' 'beansapplication': 'sqlite:///'+fsdir+'/global/sys/BeansApplication.db',
'system': 'sqlite:///'+fsdir+'/global/sys/System.db'
} }
try: try:
secret_key_file = open(env.getenv('FS_PREFIX') + '/local/token_secret_key.txt') secret_key_file = open(env.getenv('FS_PREFIX') + '/local/token_secret_key.txt')
@ -268,3 +269,80 @@ class ApplyMsg(db.Model):
def __repr__(self): def __repr__(self):
return "{\"id\":\"%d\", \"username\":\"%s\", \"number\": \"%d\", \"reason\":\"%s\", \"status\":\"%s\", \"time\":\"%s\"}" % (self.id, self.username, self.number, self.reason, self.status, self.time.strftime("%Y-%m-%d %H:%M:%S")) return "{\"id\":\"%d\", \"username\":\"%s\", \"number\": \"%d\", \"reason\":\"%s\", \"status\":\"%s\", \"time\":\"%s\"}" % (self.id, self.username, self.number, self.reason, self.status, self.time.strftime("%Y-%m-%d %H:%M:%S"))
class Container(db.Model):
__bind_key__ = 'system'
containername = db.Column(db.String(100), primary_key=True)
hostname = db.Column(db.String(30))
ip = db.Column(db.String(20))
host = db.Column(db.String(20))
image = db.Column(db.String(50))
lastsave = db.Column(db.DateTime)
setting_cpu = db.Column(db.Integer)
setting_mem = db.Column(db.Integer)
setting_disk = db.Column(db.Integer)
vclusterid = db.Column(db.Integer, db.ForeignKey('v_cluster.clusterid'))
def __init__(self, containername, hostname, ip, host, image, lastsave, setting):
self.containername = containername
self.hostname = hostname
self.ip = ip
self.host = host
self.image = image
self.lastsave = lastsave
self.setting_cpu = int(setting['cpu'])
self.setting_mem = int(setting['memory'])
self.setting_disk = int(setting['disk'])
def __repr__(self):
return "{\"containername\":\"%s\", \"hostname\":\"%s\", \"ip\": \"%s\", \"host\":\"%s\", \"image\":\"%s\", \"lastsave\":\"%s\"}" % (self.containername, self.hostname, self.ip, self.host, self.image, self.lastsave.strftime("%Y-%m-%d %H:%M:%S"))
class PortMapping(db.Model):
__bind_key__ = 'system'
id = db.Column(db.BigInteger, primary_key=True)
vnode_name = db.Column(db.String(100))
vnode_ip = db.Column(db.String(20))
vnode_port = db.Column(db.Integer)
host_port= db.Column(db.Integer)
vclusterid = db.Column(db.Integer, db.ForeignKey('v_cluster.clusterid'))
def __init__(self, vnode_name, vnode_ip, vnode_port, host_port):
self.vnode_name = vnode_name
self.vnode_ip = vnode_ip
self.vnode_port = vnode_port
self.host_port = host_port
def __repr__(self):
return "{\"id\":\"%d\", \"vnode_name\":\"%s\", \"vnode_ip\": \"%s\", \"vnode_port\":\"%s\", \"host_port\":\"%s\"}" % (self.id, self.vnode_name, self.vnode_ip, self.vnode_port, self.host_port)
class VCluster(db.Model):
__bind_key__ = 'system'
clusterid = db.Column(db.BigInteger, primary_key=True, autoincrement=False)
clustername = db.Column(db.String(50))
ownername = db.Column(db.String(20))
status = db.Column(db.String(10))
size = db.Column(db.Integer)
containers = db.relationship('Container', backref='v_cluster', lazy='dynamic')
nextcid = db.Column(db.Integer)
create_time = db.Column(db.DateTime)
start_time = db.Column(db.String(20))
proxy_server_ip = db.Column(db.String(20))
proxy_public_ip = db.Column(db.String(20))
portmappings = db.relationship('PortMapping', backref='v_cluster', lazy='dynamic')
def __init__(self, clusterid, clustername, ownername, status, size, nextcid, proxy_server_ip, proxy_public_ip):
self.clusterid = clusterid
self.clustername = clustername
self.ownername = ownername
self.status = status
self.size = size
self.nextcid = nextcid
self.proxy_server_ip = proxy_server_ip
self.proxy_public_ip = proxy_public_ip
self.containers = []
self.portmappings = []
self.create_time = datetime.now()
self.start_time = "------"
def __repr__(self):
return "{\"clusterid\":\"%d\", \"clustername\":\"%s\", \"ownername\": \"%s\", \"status\":\"%s\", \"size\":\"%d\", \"proxy_server_ip\":\"%s\", \"create_time\":\"%s\"}" % (self.clusterid, self.clustername, self.ownername, self.status, self.size, self.proxy_server_ip, self.create_time.strftime("%Y-%m-%d %H:%M:%S"))

View File

@ -10,6 +10,7 @@ import proxytool
import requests, threading import requests, threading
import traceback import traceback
from nettools import portcontrol from nettools import portcontrol
from model import db, Container, PortMapping, VCluster
userpoint = "http://" + env.getenv('USER_IP') + ":" + str(env.getenv('USER_PORT')) userpoint = "http://" + env.getenv('USER_IP') + ":" + str(env.getenv('USER_PORT'))
def post_to_user(url = '/', data={}): def post_to_user(url = '/', data={}):
@ -34,14 +35,26 @@ class VclusterMgr(object):
self.fspath = env.getenv("FS_PREFIX") self.fspath = env.getenv("FS_PREFIX")
self.clusterid_locks = threading.Lock() self.clusterid_locks = threading.Lock()
# check database
try:
Container.query.all()
PortMapping.query.all()
VCluster.query.all()
except:
# create database
db.create_all(bind='__all__')
logger.info ("vcluster start on %s" % (self.addr)) logger.info ("vcluster start on %s" % (self.addr))
if self.mode == 'new': if self.mode == 'new':
logger.info ("starting in new mode on %s" % (self.addr)) logger.info ("starting in new mode on %s" % (self.addr))
# check if all clusters data are deleted in httprest.py # check if all clusters data are deleted in httprest.py
clean = True clean = True
usersdir = self.fspath+"/global/users/" usersdir = self.fspath+"/global/users/"
vclusters = VCluster.query.all()
if len(vclusters) != 0:
clean = False
for user in os.listdir(usersdir): for user in os.listdir(usersdir):
if len(os.listdir(usersdir+user+"/clusters")) > 0 or len(os.listdir(usersdir+user+"/hosts")) > 0: if len(os.listdir(usersdir+user+"/hosts")) > 0:
clean = False clean = False
if not clean: if not clean:
logger.error ("clusters files not clean, start failed") logger.error ("clusters files not clean, start failed")
@ -173,9 +186,9 @@ class VclusterMgr(object):
hostfile.write(hosts) hostfile.write(hosts)
hostfile.close() hostfile.close()
clusterfile = open(clusterpath, 'w') clusterfile = open(clusterpath, 'w')
proxy_url = env.getenv("PORTAL_URL") +"/"+ proxy_public_ip +"/_web/" + username + "/" + clustername #proxy_url = env.getenv("PORTAL_URL") +"/"+ proxy_public_ip +"/_web/" + username + "/" + clustername
info = {'clusterid':clusterid, 'status':'stopped', 'size':clustersize, 'containers':containers, 'nextcid': clustersize, 'create_time':datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), 'start_time':"------"} info = {'clusterid':clusterid, 'status':'stopped', 'size':clustersize, 'containers':containers, 'nextcid': clustersize, 'create_time':datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), 'start_time':"------"}
info['proxy_url'] = proxy_url #info['proxy_url'] = proxy_url
info['proxy_server_ip'] = proxy_server_ip info['proxy_server_ip'] = proxy_server_ip
info['proxy_public_ip'] = proxy_public_ip info['proxy_public_ip'] = proxy_public_ip
info['port_mapping'] = [] info['port_mapping'] = []
@ -713,10 +726,8 @@ class VclusterMgr(object):
return [True, "detach cluster"] return [True, "detach cluster"]
def list_clusters(self, user): def list_clusters(self, user):
if not os.path.exists(self.fspath+"/global/users/"+user+"/clusters"): clusters = VCluster.query.filter_by(ownername = user).all()
return [True, []] '''full_clusters = []
clusters = os.listdir(self.fspath+"/global/users/"+user+"/clusters")
full_clusters = []
for cluster in clusters: for cluster in clusters:
single_cluster = {} single_cluster = {}
single_cluster['name'] = cluster single_cluster['name'] = cluster
@ -725,7 +736,7 @@ class VclusterMgr(object):
single_cluster['status'] = 'running' single_cluster['status'] = 'running'
else: else:
single_cluster['status'] = 'stopping' single_cluster['status'] = 'stopping'
full_clusters.append(single_cluster) full_clusters.append(single_cluster)'''
return [True, clusters] return [True, clusters]
def is_cluster(self, clustername, username): def is_cluster(self, clustername, username):
@ -755,8 +766,8 @@ class VclusterMgr(object):
logger.error("Fail to get proxy_public_ip %s."%(proxy_server_ip)) logger.error("Fail to get proxy_public_ip %s."%(proxy_server_ip))
proxy_public_ip = proxy_server_ip proxy_public_ip = proxy_server_ip
info['proxy_public_ip'] = proxy_public_ip info['proxy_public_ip'] = proxy_public_ip
proxy_url = env.getenv("PORTAL_URL") +"/"+ proxy_public_ip +"/_web/" + username + "/" + clustername #proxy_url = env.getenv("PORTAL_URL") +"/"+ proxy_public_ip +"/_web/" + username + "/" + clustername
info['proxy_url'] = proxy_url #info['proxy_url'] = proxy_url
self.write_clusterinfo(info,clustername,username) self.write_clusterinfo(info,clustername,username)
return proxy_public_ip return proxy_public_ip