From fd120f4232e401ccceac1a50b7530e105d663025 Mon Sep 17 00:00:00 2001 From: zhongyehong Date: Tue, 12 Apr 2016 15:03:00 +0800 Subject: [PATCH] add disk quota, add button on adminpage to add quota #7 move system data to global/sys #41 --- bin/docklet-master | 2 + src/container.py | 3 +- src/httprest.py | 3 + src/model.py | 2 +- src/userManager.py | 111 ++++++++++++---------- web/templates/admin.html | 168 +++++++++++++++++----------------- web/templates/config.html | 2 +- web/web.py | 11 ++- web/webViews/admin.py | 34 ++++++- web/webViews/user/userlist.py | 17 ---- 10 files changed, 194 insertions(+), 159 deletions(-) diff --git a/bin/docklet-master b/bin/docklet-master index 0d7c489..020913c 100755 --- a/bin/docklet-master +++ b/bin/docklet-master @@ -75,6 +75,8 @@ pre_start_master () { [ ! -d $FS_PREFIX/global ] && mkdir -p $FS_PREFIX/global [ ! -d $FS_PREFIX/local ] && mkdir -p $FS_PREFIX/local [ ! -d $FS_PREFIX/global/users ] && mkdir -p $FS_PREFIX/global/users + [ ! -d $FS_PREFIX/global/sys ] && mkdir -p $FS_PREFIX/global/sys + [ ! -d $FS_PREFIX/global/images ] && mkdir -p $FS_PREFIX/global/images [ ! -d $FS_PREFIX/local/volume ] && mkdir -p $FS_PREFIX/local/volume [ ! -d $FS_PREFIX/local/temp ] && mkdir -p $FS_PREFIX/local/temp [ ! -d $FS_PREFIX/local/run ] && mkdir -p $FS_PREFIX/local/run diff --git a/src/container.py b/src/container.py index 356f915..1e572f5 100755 --- a/src/container.py +++ b/src/container.py @@ -27,8 +27,9 @@ class Container(object): user_info = json.loads(user_info) cpu = user_info["data"]["groupinfo"]["cpu"] memory = user_info["data"]["groupinfo"]["memory"] + disk = user_info["data"]["groupinfo"]["disk"] image = json.loads(image) - status = self.imgmgr.prepareFS(username,image,lxc_name) + status = self.imgmgr.prepareFS(username,image,lxc_name,disk) if not status: return [False, "Create container failed when preparing filesystem, possibly insufficient space"] diff --git a/src/httprest.py b/src/httprest.py index efded1b..2b48b0d 100755 --- a/src/httprest.py +++ b/src/httprest.py @@ -423,6 +423,9 @@ class DockletHttpHandler(http.server.BaseHTTPRequestHandler): elif cmds[1] == 'groupadd': result = G_usermgr.groupadd(form = form, cur_user = cur_user) self.response(200, result) + elif cmds[1] == 'quotaadd': + result = G_usermgr.quotaadd(form = form, cur_user = cur_user) + self.response(200, result) elif cmds[1] == 'groupdel': result = G_usermgr.groupdel(name = form.getvalue('name', None), cur_user = cur_user) self.response(200, result) diff --git a/src/model.py b/src/model.py index be2e65d..157c476 100755 --- a/src/model.py +++ b/src/model.py @@ -40,7 +40,7 @@ import env fsdir = env.getenv('FS_PREFIX') app = Flask(__name__) -app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///'+fsdir+'/local/UserTable.db' +app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///'+fsdir+'/global/sys/UserTable.db' try: secret_key_file = open(env.getenv('FS_PREFIX') + '/local/token_secret_key.txt') app.secret_key = secret_key_file.read() diff --git a/src/userManager.py b/src/userManager.py index 727772e..e45eb20 100755 --- a/src/userManager.py +++ b/src/userManager.py @@ -20,6 +20,7 @@ from email.mime.multipart import MIMEMultipart from email.header import Header from datetime import datetime import json +from log import logger email_from_address = env.getenv('EMAIL_FROM_ADDRESS') admin_email_address = env.getenv('ADMIN_EMAIL_ADDRESS') @@ -145,12 +146,13 @@ class userManager: path = env.getenv('DOCKLET_LIB') subprocess.call([path+"/userinit.sh", username]) db.session.commit() - if not os.path.exists(fspath+"/global/group"): - groupfile = open(fspath+"/global/group",'w') + if not os.path.exists(fspath+"/global/sys/quota"): + groupfile = open(fspath+"/global/sys/quota",'w') groups = [] - groups.append({'name':'root', 'cpu':'100000', 'memory':'2000', 'imageQuantity':'10', 'lifeCycle':'24'}) - groups.append({'name':'admin', 'cpu':'100000', 'memory':'2000', 'imageQuantity':'10', 'lifeCycle':'24'}) - groups.append({'name':'primary', 'cpu':'100000', 'memory':'2000', 'imageQuantity':'10', 'lifeCycle':'24'}) + groups.append({'name':'root', 'quotas':{ 'cpu':'100000', 'disk':'2000', 'memory':'2000', 'imageQuantity':'10', 'lifeCycle':'24'}}) + groups.append({'name':'admin', 'quotas':{'cpu':'100000', 'disk':'2000', 'memory':'2000', 'imageQuantity':'10', 'lifeCycle':'24'}}) + groups.append({'name':'primary', 'quotas':{'cpu':'100000', 'disk':'2000', 'memory':'2000', 'imageQuantity':'10', 'lifeCycle':'24'}}) + groups.append({'name':'fundamental', 'quotas':{'cpu':'100000', 'disk':'2000', 'memory':'2000', 'imageQuantity':'10', 'lifeCycle':'24'}}) groupfile.write(json.dumps(groups)) groupfile.close() @@ -356,18 +358,18 @@ class userManager: List informantion for oneself ''' user = kwargs['cur_user'] - groupfile = open(fspath+"/global/group",'r') + groupfile = open(fspath+"/global/sys/quota",'r') groups = json.loads(groupfile.read()) groupfile.close() group = None for one_group in groups: if one_group['name'] == user.user_group: - group = one_group + group = one_group['quotas'] break else: for one_group in groups: if one_group['name'] == "primary": - group = one_group + group = one_group['quotas'] break result = { "success": 'true', @@ -385,12 +387,7 @@ class userManager: "tel" : user.tel, "register_date" : "%s"%(user.register_date), "group" : user.user_group, - "groupinfo": { - "cpu": group['cpu'], - "memory": group['memory'], - "imageQuantity": group['imageQuantity'], - "lifeCycle":group['lifeCycle'], - }, + "groupinfo": group, }, } return result @@ -458,24 +455,14 @@ class userManager: Usage: list(cur_user = token_from_auth) List all groups for an administrator ''' - groupfile = open(fspath+"/global/group",'r') + groupfile = open(fspath+"/global/sys/quota",'r') groups = json.loads(groupfile.read()) groupfile.close() result = { "success": 'true', - "data":[] + "groups": groups, + "quotas": list(groups[0]['quotas'].keys()), } - for group in groups: - groupinfo = [ - group['name'], - group['cpu'], - group['memory'], - group['imageQuantity'], - group['lifeCycle'], - '', - ] - result["data"].append(groupinfo) - return result @administration_required @@ -484,20 +471,14 @@ class userManager: Usage: groupQuery(name = XXX, cur_user = token_from_auth) List a group for an administrator ''' - groupfile = open(fspath+"/global/group",'r') + groupfile = open(fspath+"/global/sys/quota",'r') groups = json.loads(groupfile.read()) groupfile.close() for group in groups: if group['name'] == kwargs['name']: result = { "success":'true', - "data":{ - "name" : group['name'] , - "cpu" : group['cpu'] , - "memory" : group['memory'], - "imageQuantity" : group['imageQuantity'], - "lifeCycle" : group['lifeCycle'], - } + "data": group, } return result else: @@ -509,7 +490,7 @@ class userManager: Usage: grouplist(cur_user = token_from_auth) List all group names for an administrator ''' - groupfile = open(fspath+"/global/group",'r') + groupfile = open(fspath+"/global/sys/quota",'r') groups = json.loads(groupfile.read()) groupfile.close() result = { @@ -524,17 +505,18 @@ class userManager: ''' Usage: groupModify(newValue = dict_from_form, cur_user = token_from_auth) ''' - groupfile = open(fspath+"/global/group",'r') + groupfile = open(fspath+"/global/sys/quota",'r') groups = json.loads(groupfile.read()) groupfile.close() for group in groups: if group['name'] == kwargs['newValue'].getvalue('groupname',None): form = kwargs['newValue'] - group['cpu'] = form.getvalue('cpu', '') - group['memory'] = form.getvalue('memory', '') - group['imageQuantity'] = form.getvalue('image', '') - group['lifeCycle'] = form.getvalue('lifecycle', '') - groupfile = open(fspath+"/global/group",'w') + for key in form.keys(): + if key == "groupname" or key == "token": + pass + else: + group['quotas'][key] = form.getvalue(key) + groupfile = open(fspath+"/global/sys/quota",'w') groupfile.write(json.dumps(groups)) groupfile.close() return {"success":'true'} @@ -622,15 +604,44 @@ class userManager: return {"success":'true'} @administration_required - def groupadd(*args, **kwargs): + def quotaadd(*args, **kwargs): form = kwargs.get('form') - if (form.getvalue("name") == None): - return {"success":'false', "reason": "Empty group name"} - groupfile = open(fspath+"/global/group",'r') + quotaname = form.getvalue("quotaname") + default_value = form.getvalue("default_value") + if (quotaname == None): + return { "success":'false', "reason": "Empty quota name"} + if (default_value == None): + default_value = "--" + groupfile = open(fspath+"/global/sys/quota",'r') groups = json.loads(groupfile.read()) groupfile.close() - groups.append({'name':form.getvalue("name"), 'cpu':form.getvalue("cpu"), 'memory':form.getvalue("memory"), 'imageQuantity':form.getvalue("image"), 'lifeCycle':form.getvalue("lifecycle")}) - groupfile = open(fspath+"/global/group",'w') + for group in groups: + group['quotas'][quotaname] = default_value + groupfile = open(fspath+"/global/sys/quota",'w') + groupfile.write(json.dumps(groups)) + groupfile.close() + return {"success":'true'} + + @administration_required + def groupadd(*args, **kwargs): + form = kwargs.get('form') + groupname = form.getvalue("groupname") + if (groupname == None): + return {"success":'false', "reason": "Empty group name"} + groupfile = open(fspath+"/global/sys/quota",'r') + groups = json.loads(groupfile.read()) + groupfile.close() + group = { + 'name': groupname, + 'quotas': {} + } + for key in form.keys(): + if key == "groupname" or key == "token": + pass + else: + group['quotas'][key] = form.getvalue(key) + groups.append(group) + groupfile = open(fspath+"/global/sys/quota",'w') groupfile.write(json.dumps(groups)) groupfile.close() return {"success":'true'} @@ -640,14 +651,14 @@ class userManager: name = kwargs.get('name', None) if (name == None): return {"success":'false', "reason": "Empty group name"} - groupfile = open(fspath+"/global/group",'r') + groupfile = open(fspath+"/global/sys/quota",'r') groups = json.loads(groupfile.read()) groupfile.close() for group in groups: if group['name'] == name: groups.remove(group) break - groupfile = open(fspath+"/global/group",'w') + groupfile = open(fspath+"/global/sys/quota",'w') groupfile.write(json.dumps(groups)) groupfile.close() return {"success":'true'} diff --git a/web/templates/admin.html b/web/templates/admin.html index 33433a1..f384db4 100644 --- a/web/templates/admin.html +++ b/web/templates/admin.html @@ -51,27 +51,16 @@ @@ -82,59 +71,93 @@ - - - - - - - - - - - - - - +
NameCPUMemoryImageQuantityLifeCycleCommand
+ + + + {% for quota in quotas %} + + {% endfor %} + + + + + {% for group in groups %} + + + {% for quota in quotas %} + + {% endfor %} + + {% else %} + Delete + {% endif %} + + + {% endfor %} +
Name {{ quota }} Command
{{ group['name'] }} {{ group['quotas'][quota] }} Edit  + {% if group['name'] in [ "root", "primary", "admin", "fundamental" ] %} + Delete
@@ -149,30 +172,11 @@