release lock when exception occur

This commit is contained in:
zhongyehong 2018-10-20 21:33:55 +08:00
parent d190b90766
commit 6d29858905
1 changed files with 203 additions and 137 deletions

View File

@ -125,31 +125,35 @@ def create_cluster(user, beans, form):
if (clustername == None): if (clustername == None):
return json.dumps({'success':'false', 'message':'clustername is null'}) return json.dumps({'success':'false', 'message':'clustername is null'})
G_ulockmgr.acquire(user) G_ulockmgr.acquire(user)
image = {} try:
image['name'] = form.get("imagename", None) image = {}
image['type'] = form.get("imagetype", None) image['name'] = form.get("imagename", None)
image['owner'] = form.get("imageowner", None) image['type'] = form.get("imagetype", None)
user_info = post_to_user("/user/selfQuery/", {'token':form.get("token")}) image['owner'] = form.get("imageowner", None)
user_info = json.dumps(user_info) user_info = post_to_user("/user/selfQuery/", {'token':form.get("token")})
logger.info ("handle request : create cluster %s with image %s " % (clustername, image['name'])) user_info = json.dumps(user_info)
setting = { logger.info ("handle request : create cluster %s with image %s " % (clustername, image['name']))
'cpu': form.get('cpuSetting'), setting = {
'memory': form.get('memorySetting'), 'cpu': form.get('cpuSetting'),
'disk': form.get('diskSetting') 'memory': form.get('memorySetting'),
} 'disk': form.get('diskSetting')
res = post_to_user("/user/usageInc/", {'token':form.get('token'), 'setting':json.dumps(setting)}) }
status = res.get('success') res = post_to_user("/user/usageInc/", {'token':form.get('token'), 'setting':json.dumps(setting)})
result = res.get('result') status = res.get('success')
if not status: result = res.get('result')
if not status:
return json.dumps({'success':'false', 'action':'create cluster', 'message':result})
[status, result] = G_vclustermgr.create_cluster(clustername, user, image, user_info, setting)
if status:
return json.dumps({'success':'true', 'action':'create cluster', 'message':result})
else:
post_to_user("/user/usageRecover/", {'token':form.get('token'), 'setting':json.dumps(setting)})
return json.dumps({'success':'false', 'action':'create cluster', 'message':result})
except Exception as ex:
logger.error(str(ex))
return json.dumps({'success':'false', 'message': str(ex)})
finally:
G_ulockmgr.release(user) G_ulockmgr.release(user)
return json.dumps({'success':'false', 'action':'create cluster', 'message':result})
[status, result] = G_vclustermgr.create_cluster(clustername, user, image, user_info, setting)
G_ulockmgr.release(user)
if status:
return json.dumps({'success':'true', 'action':'create cluster', 'message':result})
else:
post_to_user("/user/usageRecover/", {'token':form.get('token'), 'setting':json.dumps(setting)})
return json.dumps({'success':'false', 'action':'create cluster', 'message':result})
@app.route("/cluster/scaleout/", methods=['POST']) @app.route("/cluster/scaleout/", methods=['POST'])
@login_required @login_required
@ -162,31 +166,35 @@ def scaleout_cluster(user, beans, form):
if (clustername == None): if (clustername == None):
return json.dumps({'success':'false', 'message':'clustername is null'}) return json.dumps({'success':'false', 'message':'clustername is null'})
G_ulockmgr.acquire(user) G_ulockmgr.acquire(user)
logger.info("handle request : scale out %s" % clustername) try:
image = {} logger.info("handle request : scale out %s" % clustername)
image['name'] = form.get("imagename", None) image = {}
image['type'] = form.get("imagetype", None) image['name'] = form.get("imagename", None)
image['owner'] = form.get("imageowner", None) image['type'] = form.get("imagetype", None)
user_info = post_to_user("/user/selfQuery/", {'token':form.get("token")}) image['owner'] = form.get("imageowner", None)
user_info = json.dumps(user_info) user_info = post_to_user("/user/selfQuery/", {'token':form.get("token")})
setting = { user_info = json.dumps(user_info)
'cpu': form.get('cpuSetting'), setting = {
'memory': form.get('memorySetting'), 'cpu': form.get('cpuSetting'),
'disk': form.get('diskSetting') 'memory': form.get('memorySetting'),
} 'disk': form.get('diskSetting')
res = post_to_user("/user/usageInc/", {'token':form.get('token'), 'setting':json.dumps(setting)}) }
status = res.get('success') res = post_to_user("/user/usageInc/", {'token':form.get('token'), 'setting':json.dumps(setting)})
result = res.get('result') status = res.get('success')
if not status: result = res.get('result')
if not status:
return json.dumps({'success':'false', 'action':'scale out', 'message': result})
[status, result] = G_vclustermgr.scale_out_cluster(clustername, user, image, user_info, setting)
if status:
return json.dumps({'success':'true', 'action':'scale out', 'message':result})
else:
post_to_user("/user/usageRecover/", {'token':form.get('token'), 'setting':json.dumps(setting)})
return json.dumps({'success':'false', 'action':'scale out', 'message':result})
except Exception as ex:
logger.error(str(ex))
return json.dumps({'success':'false', 'message': str(ex)})
finally:
G_ulockmgr.release(user) G_ulockmgr.release(user)
return json.dumps({'success':'false', 'action':'scale out', 'message': result})
[status, result] = G_vclustermgr.scale_out_cluster(clustername, user, image, user_info, setting)
G_ulockmgr.release(user)
if status:
return json.dumps({'success':'true', 'action':'scale out', 'message':result})
else:
post_to_user("/user/usageRecover/", {'token':form.get('token'), 'setting':json.dumps(setting)})
return json.dumps({'success':'false', 'action':'scale out', 'message':result})
@app.route("/cluster/scalein/", methods=['POST']) @app.route("/cluster/scalein/", methods=['POST'])
@login_required @login_required
@ -197,17 +205,22 @@ def scalein_cluster(user, beans, form):
if (clustername == None): if (clustername == None):
return json.dumps({'success':'false', 'message':'clustername is null'}) return json.dumps({'success':'false', 'message':'clustername is null'})
G_ulockmgr.acquire(user) G_ulockmgr.acquire(user)
logger.info("handle request : scale in %s" % clustername) try:
containername = form.get("containername", None) logger.info("handle request : scale in %s" % clustername)
[status, usage_info] = G_vclustermgr.get_clustersetting(clustername, user, containername, False) containername = form.get("containername", None)
if status: [status, usage_info] = G_vclustermgr.get_clustersetting(clustername, user, containername, False)
post_to_user("/user/usageRelease/", {'token':form.get('token'), 'cpu':usage_info['cpu'], 'memory':usage_info['memory'],'disk':usage_info['disk']}) if status:
[status, result] = G_vclustermgr.scale_in_cluster(clustername, user, containername) post_to_user("/user/usageRelease/", {'token':form.get('token'), 'cpu':usage_info['cpu'], 'memory':usage_info['memory'],'disk':usage_info['disk']})
G_ulockmgr.release(user) [status, result] = G_vclustermgr.scale_in_cluster(clustername, user, containername)
if status: if status:
return json.dumps({'success':'true', 'action':'scale in', 'message':result}) return json.dumps({'success':'true', 'action':'scale in', 'message':result})
else: else:
return json.dumps({'success':'false', 'action':'scale in', 'message':result}) return json.dumps({'success':'false', 'action':'scale in', 'message':result})
except Exception as ex:
logger.error(str(ex))
return json.dumps({'success':'false', 'message': str(ex)})
finally:
G_ulockmgr.release(user)
@app.route("/cluster/start/", methods=['POST']) @app.route("/cluster/start/", methods=['POST'])
@login_required @login_required
@ -219,14 +232,19 @@ def start_cluster(user, beans, form):
if (clustername == None): if (clustername == None):
return json.dumps({'success':'false', 'message':'clustername is null'}) return json.dumps({'success':'false', 'message':'clustername is null'})
G_ulockmgr.acquire(user) G_ulockmgr.acquire(user)
user_info = post_to_user("/user/selfQuery/", {'token':form.get("token")}) try:
logger.info ("handle request : start cluster %s" % clustername) user_info = post_to_user("/user/selfQuery/", {'token':form.get("token")})
[status, result] = G_vclustermgr.start_cluster(clustername, user, user_info) logger.info ("handle request : start cluster %s" % clustername)
G_ulockmgr.release(user) [status, result] = G_vclustermgr.start_cluster(clustername, user, user_info)
if status: if status:
return json.dumps({'success':'true', 'action':'start cluster', 'message':result}) return json.dumps({'success':'true', 'action':'start cluster', 'message':result})
else: else:
return json.dumps({'success':'false', 'action':'start cluster', 'message':result}) return json.dumps({'success':'false', 'action':'start cluster', 'message':result})
except Exception as ex:
logger.error(str(ex))
return json.dumps({'success':'false', 'message': str(ex)})
finally:
G_ulockmgr.release(user)
@app.route("/cluster/stop/", methods=['POST']) @app.route("/cluster/stop/", methods=['POST'])
@login_required @login_required
@ -237,13 +255,18 @@ def stop_cluster(user, beans, form):
if (clustername == None): if (clustername == None):
return json.dumps({'success':'false', 'message':'clustername is null'}) return json.dumps({'success':'false', 'message':'clustername is null'})
G_ulockmgr.acquire(user) G_ulockmgr.acquire(user)
logger.info ("handle request : stop cluster %s" % clustername) try:
[status, result] = G_vclustermgr.stop_cluster(clustername, user) logger.info ("handle request : stop cluster %s" % clustername)
G_ulockmgr.release(user) [status, result] = G_vclustermgr.stop_cluster(clustername, user)
if status: if status:
return json.dumps({'success':'true', 'action':'stop cluster', 'message':result}) return json.dumps({'success':'true', 'action':'stop cluster', 'message':result})
else: else:
return json.dumps({'success':'false', 'action':'stop cluster', 'message':result}) return json.dumps({'success':'false', 'action':'stop cluster', 'message':result})
except Exception as ex:
logger.error(str(ex))
return json.dumps({'success':'false', 'message': str(ex)})
finally:
G_ulockmgr.release(user)
@app.route("/cluster/delete/", methods=['POST']) @app.route("/cluster/delete/", methods=['POST'])
@login_required @login_required
@ -254,18 +277,23 @@ def delete_cluster(user, beans, form):
if (clustername == None): if (clustername == None):
return json.dumps({'success':'false', 'message':'clustername is null'}) return json.dumps({'success':'false', 'message':'clustername is null'})
G_ulockmgr.acquire(user) G_ulockmgr.acquire(user)
logger.info ("handle request : delete cluster %s" % clustername) try:
user_info = post_to_user("/user/selfQuery/" , {'token':form.get("token")}) logger.info ("handle request : delete cluster %s" % clustername)
user_info = json.dumps(user_info) user_info = post_to_user("/user/selfQuery/" , {'token':form.get("token")})
[status, usage_info] = G_vclustermgr.get_clustersetting(clustername, user, "all", True) user_info = json.dumps(user_info)
if status: [status, usage_info] = G_vclustermgr.get_clustersetting(clustername, user, "all", True)
post_to_user("/user/usageRelease/", {'token':form.get('token'), 'cpu':usage_info['cpu'], 'memory':usage_info['memory'],'disk':usage_info['disk']}) if status:
[status, result] = G_vclustermgr.delete_cluster(clustername, user, user_info) post_to_user("/user/usageRelease/", {'token':form.get('token'), 'cpu':usage_info['cpu'], 'memory':usage_info['memory'],'disk':usage_info['disk']})
G_ulockmgr.release(user) [status, result] = G_vclustermgr.delete_cluster(clustername, user, user_info)
if status: if status:
return json.dumps({'success':'true', 'action':'delete cluster', 'message':result}) return json.dumps({'success':'true', 'action':'delete cluster', 'message':result})
else: else:
return json.dumps({'success':'false', 'action':'delete cluster', 'message':result}) return json.dumps({'success':'false', 'action':'delete cluster', 'message':result})
except Exception as ex:
logger.error(str(ex))
return json.dumps({'success':'false', 'message': str(ex)})
finally:
G_ulockmgr.release(user)
@app.route("/cluster/info/", methods=['POST']) @app.route("/cluster/info/", methods=['POST'])
@login_required @login_required
@ -302,16 +330,20 @@ def stopall_cluster():
if user is None: if user is None:
return json.dumps({'success':'false', 'message':'User is required!'}) return json.dumps({'success':'false', 'message':'User is required!'})
G_ulockmgr.acquire(user) G_ulockmgr.acquire(user)
logger.info ("handle request : stop all clusters for %s" % user) try:
[status, clusterlist] = G_vclustermgr.list_clusters(user) logger.info ("handle request : stop all clusters for %s" % user)
if status: [status, clusterlist] = G_vclustermgr.list_clusters(user)
for cluster in clusterlist: if status:
G_vclustermgr.stop_cluster(cluster,user) for cluster in clusterlist:
G_vclustermgr.stop_cluster(cluster,user)
return json.dumps({'success':'true', 'action':'stop all cluster'})
else:
return json.dumps({'success':'false', 'action':'stop all cluster', 'message':clusterlist})
except Exception as ex:
logger.error(str(ex))
return json.dumps({'success':'false', 'message': str(ex)})
finally:
G_ulockmgr.release(user) G_ulockmgr.release(user)
return json.dumps({'success':'true', 'action':'stop all cluster'})
else:
G_ulockmgr.release(user)
return json.dumps({'success':'false', 'action':'stop all cluster', 'message':clusterlist})
@app.route("/cluster/flush/", methods=['POST']) @app.route("/cluster/flush/", methods=['POST'])
@login_required @login_required
@ -337,22 +369,25 @@ def save_cluster(user, beans, form):
containername = form.get("containername", None) containername = form.get("containername", None)
isforce = form.get("isforce", None) isforce = form.get("isforce", None)
G_ulockmgr.acquire(user) G_ulockmgr.acquire(user)
if not isforce == "true": try:
[status,message] = G_vclustermgr.image_check(user,imagename) if not isforce == "true":
if not status: [status,message] = G_vclustermgr.image_check(user,imagename)
G_ulockmgr.release(user) if not status:
return json.dumps({'success':'false','reason':'exists', 'message':message}) return json.dumps({'success':'false','reason':'exists', 'message':message})
user_info = post_to_user("/user/selfQuery/", {'token':form.get("token")}) user_info = post_to_user("/user/selfQuery/", {'token':form.get("token")})
[status,message] = G_vclustermgr.create_image(user,clustername,containername,imagename,description,user_info["data"]["groupinfo"]["image"]) [status,message] = G_vclustermgr.create_image(user,clustername,containername,imagename,description,user_info["data"]["groupinfo"]["image"])
if status: if status:
logger.info("image has been saved")
return json.dumps({'success':'true', 'action':'save'})
else:
logger.debug(message)
return json.dumps({'success':'false', 'reason':'exceed', 'message':message})
except Exception as ex:
logger.error(str(ex))
return json.dumps({'success':'false', 'message': str(ex)})
finally:
G_ulockmgr.release(user) G_ulockmgr.release(user)
logger.info("image has been saved")
return json.dumps({'success':'true', 'action':'save'})
else:
G_ulockmgr.release(user)
logger.debug(message)
return json.dumps({'success':'false', 'reason':'exceed', 'message':message})
@app.route("/image/list/", methods=['POST']) @app.route("/image/list/", methods=['POST'])
@ -387,9 +422,14 @@ def share_image(user, beans, form):
global G_imagemgr global G_imagemgr
image = form.get('image') image = form.get('image')
G_ulockmgr.acquire(user) G_ulockmgr.acquire(user)
G_imagemgr.shareImage(user,image) try:
G_ulockmgr.release(user) G_imagemgr.shareImage(user,image)
return json.dumps({'success':'true', 'action':'share'}) return json.dumps({'success':'true', 'action':'share'})
except Exception as ex:
logger.error(str(ex))
return json.dumps({'success':'false', 'message': str(ex)})
finally:
G_ulockmgr.release(user)
@app.route("/image/unshare/", methods=['POST']) @app.route("/image/unshare/", methods=['POST'])
@login_required @login_required
@ -397,9 +437,14 @@ def unshare_image(user, beans, form):
global G_imagemgr global G_imagemgr
image = form.get('image', None) image = form.get('image', None)
G_ulockmgr.acquire(user) G_ulockmgr.acquire(user)
G_imagemgr.unshareImage(user,image) try:
G_ulockmgr.release(user) G_imagemgr.unshareImage(user,image)
return json.dumps({'success':'true', 'action':'unshare'}) return json.dumps({'success':'true', 'action':'unshare'})
except Exception as ex:
logger.error(str(ex))
return json.dumps({'success':'false', 'message': str(ex)})
finally:
G_ulockmgr.release(user)
@app.route("/image/delete/", methods=['POST']) @app.route("/image/delete/", methods=['POST'])
@login_required @login_required
@ -407,9 +452,14 @@ def delete_image(user, beans, form):
global G_imagemgr global G_imagemgr
image = form.get('image', None) image = form.get('image', None)
G_ulockmgr.acquire(user) G_ulockmgr.acquire(user)
G_imagemgr.removeImage(user,image) try:
G_ulockmgr.release(user) G_imagemgr.removeImage(user,image)
return json.dumps({'success':'true', 'action':'delete'}) return json.dumps({'success':'true', 'action':'delete'})
except Exception as ex:
logger.error(str(ex))
return json.dumps({'success':'false', 'message': str(ex)})
finally:
G_ulockmgr.release(user)
@app.route("/image/copy/", methods=['POST']) @app.route("/image/copy/", methods=['POST'])
@login_required @login_required
@ -420,9 +470,14 @@ def copy_image(user, beans, form):
target = form.get('target',None) target = form.get('target',None)
token = form.get('token',None) token = form.get('token',None)
G_ulockmgr.acquire(user) G_ulockmgr.acquire(user)
res = G_imagemgr.copyImage(user,image,token,target) try:
G_ulockmgr.release(user) res = G_imagemgr.copyImage(user,image,token,target)
return json.dumps(res) return json.dumps(res)
except Exception as ex:
logger.error(str(ex))
return json.dumps({'success':'false', 'message': str(ex)})
finally:
G_ulockmgr.release(user)
@app.route("/image/copytarget/", methods=['POST']) @app.route("/image/copytarget/", methods=['POST'])
@login_required @login_required
@ -435,11 +490,12 @@ def copytarget_image(user, beans, form):
try: try:
G_ulockmgr.acquire(user) G_ulockmgr.acquire(user)
res = G_imagemgr.updateinfo(user,imagename,description) res = G_imagemgr.updateinfo(user,imagename,description)
return json.dumps({'success':'true', 'action':'copy image to target.'})
except Exception as ex:
logger.error(str(ex))
return json.dumps({'success':'false', 'message':str(ex)})
finally:
G_ulockmgr.release(user) G_ulockmgr.release(user)
except Exception as e:
logger.error(e)
return json.dumps({'success':'false', 'message':str(e)})
return json.dumps({'success':'true', 'action':'copy image to target.'})
@app.route("/cloud/setting/get/", methods=['POST']) @app.route("/cloud/setting/get/", methods=['POST'])
@login_required @login_required
@ -503,12 +559,17 @@ def add_port_mapping(user, beans, form):
return json.dumps({'success':'false', 'message': 'Illegal form.'}) return json.dumps({'success':'false', 'message': 'Illegal form.'})
user_info = post_to_user("/user/selfQuery/", data = {"token": form.get("token")}) user_info = post_to_user("/user/selfQuery/", data = {"token": form.get("token")})
G_ulockmgr.acquire(user) G_ulockmgr.acquire(user)
[status, message] = G_vclustermgr.add_port_mapping(user,clustername,node_name,node_ip,node_port,user_info['data']['groupinfo']) try:
G_ulockmgr.release(user) [status, message] = G_vclustermgr.add_port_mapping(user,clustername,node_name,node_ip,node_port,user_info['data']['groupinfo'])
if status is True: if status is True:
return json.dumps({'success':'true', 'action':'addproxy'}) return json.dumps({'success':'true', 'action':'addproxy'})
else: else:
return json.dumps({'success':'false', 'message': message}) return json.dumps({'success':'false', 'message': message})
except Exception as ex:
logger.error(str(ex))
return json.dumps({'success':'false', 'message':str(ex)})
finally:
G_ulockmgr.release(user)
@app.route("/port_mapping/delete/", methods=['POST']) @app.route("/port_mapping/delete/", methods=['POST'])
@login_required @login_required
@ -522,12 +583,17 @@ def delete_port_mapping(user, beans, form):
if node_name is None or clustername is None: if node_name is None or clustername is None:
return json.dumps({'success':'false', 'message': 'Illegal form.'}) return json.dumps({'success':'false', 'message': 'Illegal form.'})
G_ulockmgr.acquire(user) G_ulockmgr.acquire(user)
[status, message] = G_vclustermgr.delete_port_mapping(user,clustername,node_name,node_port) try:
G_ulockmgr.release(user) [status, message] = G_vclustermgr.delete_port_mapping(user,clustername,node_name,node_port)
if status is True: if status is True:
return json.dumps({'success':'true', 'action':'addproxy'}) return json.dumps({'success':'true', 'action':'addproxy'})
else: else:
return json.dumps({'success':'false', 'message': message}) return json.dumps({'success':'false', 'message': message})
except Exception as ex:
logger.error(str(ex))
return json.dumps({'success':'false', 'message':str(ex)})
finally:
G_ulockmgr.release(user)
@app.route("/monitor/hosts/<com_id>/<issue>/", methods=['POST']) @app.route("/monitor/hosts/<com_id>/<issue>/", methods=['POST'])
@login_required @login_required