From 82916439290d452a13a5ac5129d3b6eac8687970 Mon Sep 17 00:00:00 2001 From: zhongyehong Date: Thu, 21 Jul 2016 19:19:53 +0800 Subject: [PATCH] default container cpu, memory and disk can be modify in admin page --- src/httprest.py | 18 +++++++++++ src/userManager.py | 25 +++++++++++++-- web/templates/admin.html | 36 +++++++++++++++++++++ web/web.py | 5 +++ web/webViews/admin.py | 9 +++++- web/webViews/cluster.py | 68 ++++++++++++++++++++++------------------ 6 files changed, 127 insertions(+), 34 deletions(-) diff --git a/src/httprest.py b/src/httprest.py index 602a3bb..c3ea6b4 100755 --- a/src/httprest.py +++ b/src/httprest.py @@ -608,6 +608,22 @@ def usageQuery_user(cur_user, user, form): result = G_usermgr.usageQuery(cur_user = cur_user) return json.dumps(result) +@app.route("/user/lxcsettingList/", methods=['POST']) +@login_required +def lxcsettingList_user(cur_user, user, form): + global G_usermgr + logger.info("handle request: user/lxcsettingList/") + result = G_usermgr.lxcsettingList(cur_user = cur_user, form = form) + return json.dumps(result) + +@app.route("/user/chlxcsetting/", methods=['POST']) +@login_required +def chlxcsetting_user(cur_user, user, form): + global G_usermgr + logger.info("handle request: user/chlxcsetting/") + result = G_usermgr.chlxcsetting(cur_user = cur_user, form = form) + return json.dumps(result) + @app.route("/notification/list/", methods=['POST']) @login_required def list_notifications(cur_user, user, form): @@ -861,6 +877,8 @@ if __name__ == '__main__': etcdclient.deldir("_lock") G_usermgr = userManager.userManager('root') + if mode == "new": + G_usermgr.initUsage() G_notificationmgr = notificationmgr.NotificationMgr() clusternet = env.getenv("CLUSTER_NET") diff --git a/src/userManager.py b/src/userManager.py index 8086d32..d58a68b 100755 --- a/src/userManager.py +++ b/src/userManager.py @@ -628,9 +628,9 @@ class userManager: return True def initUsage(*args, **kwargs): - """ - init the usage info when start docklet with init mode - """ + """ + init the usage info when start docklet with init mode + """ usages = UserUsage.query.all() for usage in usages: usage.cpu = "0" @@ -920,6 +920,25 @@ class userManager: groupfile.write(json.dumps(groups)) groupfile.close() return {"success":'true'} + + @administration_required + def lxcsettingList(*args, **kwargs): + lxcsettingfile = open(fspath+"/global/sys/lxc.default", 'r') + lxcsetting = json.loads(lxcsettingfile.read()) + lxcsettingfile.close() + return {"success": 'true', 'data':lxcsetting} + + @administration_required + def chlxcsetting(*args, **kwargs): + form = kwargs['form'] + lxcsetting = {} + lxcsetting['cpu'] = form['lxcCpu'] + lxcsetting['memory'] = form['lxcMemory'] + lxcsetting['disk'] = form['lxcDisk'] + lxcsettingfile = open(fspath+"/global/sys/lxc.default", 'w') + lxcsettingfile.write(json.dumps(lxcsetting)) + lxcsettingfile.close() + return {"success": 'true'} def queryForDisplay(*args, **kwargs): ''' diff --git a/web/templates/admin.html b/web/templates/admin.html index 9384ed4..c5a351d 100644 --- a/web/templates/admin.html +++ b/web/templates/admin.html @@ -202,6 +202,42 @@ +
+
+
+
+

Container Default Setting

+ +
+ + +
+
+
+
+
+ +
CORE
+
+
+ +
MB
+
+
+ +
MB
+
+
+
+
+
+
+
+
+
+ {% for field in ["docklet"] %}
diff --git a/web/web.py b/web/web.py index 41fb397..fcbf124 100755 --- a/web/web.py +++ b/web/web.py @@ -331,6 +331,11 @@ def quotaadd(): def chdefault(): return chdefaultView.as_view() +@app.route("/quota/chlxcsetting/", methods=['POST']) +@administration_required +def chlxcsetting(): + return chlxcsettingView.as_view() + @app.route("/group/add/", methods=['POST']) @administration_required def groupadd(): diff --git a/web/webViews/admin.py b/web/webViews/admin.py index fcb20cb..8671ab2 100644 --- a/web/webViews/admin.py +++ b/web/webViews/admin.py @@ -14,7 +14,8 @@ class adminView(normalView): quotas = result["quotas"] defaultgroup = result["default"] parms = dockletRequest.post('/system/parmList/') - return self.render(self.template_path, groups = groups, quotas = quotas, defaultgroup = defaultgroup, parms = parms) + lxcsetting = dockletRequest.post('/user/lxcsettingList/')['data'] + return self.render(self.template_path, groups = groups, quotas = quotas, defaultgroup = defaultgroup, parms = parms, lxcsetting = lxcsetting) class groupaddView(normalView): @classmethod @@ -64,6 +65,12 @@ class chdefaultView(normalView): dockletRequest.post('/user/chdefault/', request.form) return redirect('/admin/') +class chlxcsettingView(normalView): + @classmethod + def post(self): + dockletRequest.post('/user/chlxcsetting/', request.form) + return redirect('/admin/') + class groupdelView(normalView): @classmethod def post(self): diff --git a/web/webViews/cluster.py b/web/webViews/cluster.py index ea0d7c4..f63d913 100755 --- a/web/webViews/cluster.py +++ b/web/webViews/cluster.py @@ -15,27 +15,31 @@ class addClusterView(normalView): result = dockletRequest.post("/user/usageQuery/") quota = result.get("quota") usage = result.get("usage") - defaultcpu = int(quota['cpu']) - int(usage['cpu']) - defaultmemory = int(quota['memory']) - int(usage['memory']) - defaultdisk = int(quota['disk']) - int(usage['disk']) - if defaultcpu >= 4: - defaultcpu = "4" - elif defaultcpu <= 0: + default = result.get("default") + restcpu = int(quota['cpu']) - int(usage['cpu']) + restmemory = int(quota['memory']) - int(usage['memory']) + restdisk = int(quota['disk']) - int(usage['disk']) + if restcpu >= int(default['cpu']): + defaultcpu = default['cpu'] + elif restcpu <= 0: defaultcpu = "0" else: - defaultcpu = str(defaultcpu) - if defaultmemory >= 2000: - defaultmemory = "2000" - elif defaultmemory <= 0: + defaultcpu = str(restcpu) + + if restmemory >= int(default['memory']): + defaultmemory = default['memory'] + elif restmemory <= 0: defaultmemory = "0" else: - defaultmemory = str(defaultmemory) - if defaultdisk >= 2000: - defaultdisk = "2000" - elif defaultdisk <= 0: + defaultmemory = str(restmemory) + + if restdisk >= int(default['disk']): + defaultdisk = default['disk'] + elif restdisk <= 0: defaultdisk = "0" else: - defaultdisk = str(defaultdisk) + defaultdisk = str(restdisk) + defaultsetting = { 'cpu': defaultcpu, 'memory': defaultmemory, @@ -328,27 +332,31 @@ class configView(normalView): result = dockletRequest.post("/user/usageQuery/") quota = result.get("quota") usage = result.get("usage") - defaultcpu = int(quota['cpu']) - int(usage['cpu']) - defaultmemory = int(quota['memory']) - int(usage['memory']) - defaultdisk = int(quota['disk']) - int(usage['disk']) - if defaultcpu >= 4: - defaultcpu = "4" - elif defaultcpu <= 0: + default = result.get("default") + restcpu = int(quota['cpu']) - int(usage['cpu']) + restmemory = int(quota['memory']) - int(usage['memory']) + restdisk = int(quota['disk']) - int(usage['disk']) + if restcpu >= int(default['cpu']): + defaultcpu = default['cpu'] + elif restcpu <= 0: defaultcpu = "0" else: - defaultcpu = str(defaultcpu) - if defaultmemory >= 2000: - defaultmemory = "2000" - elif defaultmemory <= 0: + defaultcpu = str(restcpu) + + if restmemory >= int(default['memory']): + defaultmemory = default['memory'] + elif restmemory <= 0: defaultmemory = "0" else: - defaultmemory = str(defaultmemory) - if defaultdisk >= 2000: - defaultdisk = "2000" - elif defaultdisk <= 0: + defaultmemory = str(restmemory) + + if restdisk >= int(default['disk']): + defaultdisk = default['disk'] + elif restdisk <= 0: defaultdisk = "0" else: - defaultdisk = str(defaultdisk) + defaultdisk = str(restdisk) + defaultsetting = { 'cpu': defaultcpu, 'memory': defaultmemory,