Fix some bugs about database committing

This commit is contained in:
Firmlyzhu 2019-04-26 23:30:52 +08:00
parent e37c9eb6de
commit ed908a4d9e
2 changed files with 35 additions and 10 deletions

View File

@ -442,14 +442,14 @@ def migrate_host(user, beans, form):
if src_host is None or dst_host_list is None: if src_host is None or dst_host_list is None:
return json.dumps({'success':'false', 'message': 'src host or dst host list is null'}) return json.dumps({'success':'false', 'message': 'src host or dst host list is null'})
[status, msg] = G_vclustermgr.migrate_host(src_host, dst_host_list) [status, msg] = G_vclustermgr.migrate_host(src_host, dst_host_list, G_ulockmgr)
if status: if status:
return json.dumps({'success': 'true', 'action': 'migrate_host'}) return json.dumps({'success': 'true', 'action': 'migrate_host'})
else: else:
return json.dumps({'success': 'false', 'message': msg}) return json.dumps({'success': 'false', 'message': msg})
@app.route("/image/list/", methods=['POST']) @app.route("/image/list/", methods=['POST'])
@login_required @login_required
def list_image(user, beans, form): def list_image(user, beans, form):

View File

@ -19,6 +19,15 @@ def post_to_user(url = '/', data={}):
# #
################################################## ##################################################
def db_commit():
try:
db.session.commit()
except Exception as err:
db.session.rollback()
logger.error(traceback.format_exc())
return False
return True
class VclusterMgr(object): class VclusterMgr(object):
def __init__(self, nodemgr, networkmgr, etcdclient, addr, mode, distributedgw='False'): def __init__(self, nodemgr, networkmgr, etcdclient, addr, mode, distributedgw='False'):
self.mode = mode self.mode = mode
@ -615,7 +624,8 @@ class VclusterMgr(object):
[status,vcluster] = self.get_vcluster(clustername,username) [status,vcluster] = self.get_vcluster(clustername,username)
vcluster.status ='running' vcluster.status ='running'
vcluster.start_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") vcluster.start_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
db.session.commit() if not db_commit():
return [False, "Commit Errror"]
return [True, "start cluster"] return [True, "start cluster"]
def mount_cluster(self, clustername, username): def mount_cluster(self, clustername, username):
@ -697,7 +707,8 @@ class VclusterMgr(object):
[status, vcluster] = self.get_vcluster(clustername, username) [status, vcluster] = self.get_vcluster(clustername, username)
vcluster.status = 'stopped' vcluster.status = 'stopped'
vcluster.start_time ="------" vcluster.start_time ="------"
db.session.commit() if not db_commit():
return [False, "Commit Errror"]
return [True, "stop cluster"] return [True, "stop cluster"]
def detach_cluster(self, clustername, username): def detach_cluster(self, clustername, username):
@ -784,7 +795,14 @@ class VclusterMgr(object):
self.imgmgr.removeImage(username,imagename) self.imgmgr.removeImage(username,imagename)
return [False, msg] return [False, msg]
con_db.host = new_host con_db.host = new_host
db.session.commit() try:
db.session.commit()
except Exception as err:
logger.error(traceback.format_exc())
db.session.rollback()
worker.delete_container(containername)
self.imgmgr.removeImage(username,imagename)
return [False, "Database commit error!"]
oldworker.delete_container(containername) oldworker.delete_container(containername)
self.imgmgr.removeImage(username,imagename) self.imgmgr.removeImage(username,imagename)
return [True,""] return [True,""]
@ -816,7 +834,7 @@ class VclusterMgr(object):
return [False, msg] return [False, msg]
return [True, ""] return [True, ""]
def migrate_host(self, src_host, new_host_list): def migrate_host(self, src_host, new_host_list, ulockmgr):
[status, vcluster_list] = self.get_all_clusterinfo() [status, vcluster_list] = self.get_all_clusterinfo()
if not status: if not status:
return [False, vcluster_list] return [False, vcluster_list]
@ -828,15 +846,21 @@ class VclusterMgr(object):
quotas[group['name']] = group['quotas'] quotas[group['name']] = group['quotas']
for vcluster in vcluster_list: for vcluster in vcluster_list:
if 'ownername' not in vcluster.keys():
return [Flase, 'Ownername not in vcluster(%s).keys' % str(vcluster) ]
try: try:
clustername = vcluster['clustername']
username = vcluster['ownername'] username = vcluster['ownername']
ulockmgr.acquire(username)
clustername = vcluster['clustername']
rc_info = post_to_user("/master/user/recoverinfo/", {'username':username,'auth_key':auth_key}) rc_info = post_to_user("/master/user/recoverinfo/", {'username':username,'auth_key':auth_key})
groupname = rc_info['groupname'] groupname = rc_info['groupname']
user_info = {"data":{"id":rc_info['uid'],"groupinfo":quotas[groupname]}} user_info = {"data":{"id":rc_info['uid'],"groupinfo":quotas[groupname]}}
self.migrate_cluster(clustername, username, src_host, new_host_list, user_info) self.migrate_cluster(clustername, username, src_host, new_host_list, user_info)
except Exception as ex: except Exception as ex:
ulockmgr.release(username)
logger.error(traceback.format_exc())
return [False, str(ex)] return [False, str(ex)]
ulockmgr.release(username)
return [True, ""] return [True, ""]
def is_cluster(self, clustername, username): def is_cluster(self, clustername, username):
@ -868,7 +892,8 @@ class VclusterMgr(object):
vcluster.proxy_public_ip = proxy_public_ip vcluster.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
db.session.commit() if not db_commit():
return [False, "Commit Errror"]
return proxy_public_ip return proxy_public_ip
def get_clusterinfo(self, clustername, username): def get_clusterinfo(self, clustername, username):
@ -887,7 +912,7 @@ class VclusterMgr(object):
def get_all_clusterinfo(self): def get_all_clusterinfo(self):
vcluster_list = VCluster.query.all() vcluster_list = VCluster.query.all()
logger.info(str(vcluster_list)) #logger.info(str(vcluster_list))
if vcluster_list is None: if vcluster_list is None:
return [False, None] return [False, None]
else: else: