From 8323cb62d484ca4f736a4cf0ed1d7dfd615a2ae1 Mon Sep 17 00:00:00 2001 From: zhongyehong Date: Wed, 24 Apr 2019 21:25:38 +0800 Subject: [PATCH 1/2] add page for migrate host --- src/master/httprest.py | 21 +++++++++++++ src/master/vclustermgr.py | 33 ++++++++++++++++++-- web/templates/monitor/hosts.html | 53 ++++++++++++++++++++++++++++++-- web/web.py | 9 ++++++ web/webViews/admin.py | 10 ++++++ 5 files changed, 122 insertions(+), 4 deletions(-) diff --git a/src/master/httprest.py b/src/master/httprest.py index 620652d..ffbf567 100755 --- a/src/master/httprest.py +++ b/src/master/httprest.py @@ -432,7 +432,28 @@ def migrate_cluster(): finally: G_ulockmgr.release(user) +@app.route("/host/migrate/", methods=['POST']) +@login_required +def migrate_host(user, beans, form): + global G_vclustermgr + global G_ulockmgr + src_host = request.form.get('src_host', None) + dst_host_list = request.form.getlist('dst_host_list', 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'}) + logger.info(str(src_host)) + logger.info(type(dst_host_list)) + logger.info(str(dst_host_list)) + #[status, msg] = G_vclustermgr.migrate_host(src_host, dst_host_list) + status = True + if status: + return json.dumps({'success': 'true', 'action': 'migrate_host'}) + else: + return json.dumps({'success': 'false', 'message': msg}) + + + @app.route("/image/list/", methods=['POST']) @login_required def list_image(user, beans, form): diff --git a/src/master/vclustermgr.py b/src/master/vclustermgr.py index fb3cff8..ce2adca 100755 --- a/src/master/vclustermgr.py +++ b/src/master/vclustermgr.py @@ -789,14 +789,14 @@ class VclusterMgr(object): self.imgmgr.removeImage(username,imagename) return [True,""] - def migrate_cluster(self, clustername, username, new_host_list, user_info): + def migrate_cluster(self, clustername, username, src_host, new_host_list, user_info): [status, info] = self.get_clusterinfo(clustername, username) if not status: return [False, "cluster not found"] prestatus = info['status'] self.stop_cluster(clustername, username) for container in info['containers']: - if container['host'] in new_host_list: + if container['host'] == src_host: continue random.shuffle(new_host_list) for new_host in new_host_list: @@ -816,6 +816,27 @@ class VclusterMgr(object): return [False, msg] return [True, ""] + def migrate_host(self, src_host, new_host_list): + vcluster_list = self.get_all_clusterinfo() + auth_key = env.getenv('AUTH_KEY') + res = post_to_user("/master/user/groupinfo/", {'auth_key':auth_key}) + groups = json.loads(res['groups']) + quotas = {} + for group in groups: + quotas[group['name']] = group['quotas'] + + for vcluster in vcluster_list: + try: + clustername = vcluster['clustername'] + username = vcluster['ownername'] + rc_info = post_to_user("/master/user/recoverinfo/", {'username':username,'auth_key':auth_key}) + groupname = rc_info['groupname'] + user_info = {"data":{"id":rc_info['uid'],"groupinfo":quotas[groupname]}} + self.migrate_cluster(clustername, username, src_host, new_host_list, user_info) + except Exception as ex: + return [False, str(ex)] + return [True, ""] + def is_cluster(self, clustername, username): [status, clusters] = self.list_clusters(username) if clustername in clusters: @@ -862,6 +883,14 @@ class VclusterMgr(object): else: return [True, vcluster] + def get_all_clusterinfo(self): + vcluster_list = VCluster.query.all() + logger.info(str(vcluster_list)) + if vcluster_list is None: + return [False, None] + else: + return [True, json.loads(str(vcluster_list))] + # acquire cluster id from etcd def _acquire_id(self): self.clusterid_locks.acquire() diff --git a/web/templates/monitor/hosts.html b/web/templates/monitor/hosts.html index 3df9d13..46e0812 100644 --- a/web/templates/monitor/hosts.html +++ b/web/templates/monitor/hosts.html @@ -15,6 +15,12 @@ {% endblock %} +{% block css_src %} + + + +{% endblock %} + {% block content %}