2016-04-29 15:40:53 +08:00
|
|
|
#!/usr/bin/python3
|
|
|
|
|
2016-05-06 16:41:11 +08:00
|
|
|
import os, json, sys
|
2016-05-09 11:56:12 +08:00
|
|
|
sys.path.append("../src/")
|
|
|
|
from model import db, User
|
2016-09-23 17:11:37 +08:00
|
|
|
from lvmtool import sys_run
|
2016-04-29 15:40:53 +08:00
|
|
|
fspath="/opt/docklet"
|
|
|
|
|
|
|
|
def update_quotainfo():
|
|
|
|
if not os.path.exists(fspath+"/global/sys/quotainfo"):
|
|
|
|
print("quotainfo file not exists, please run docklet to init it")
|
|
|
|
return False
|
|
|
|
quotafile = open(fspath+"/global/sys/quotainfo", 'r')
|
|
|
|
quotas = json.loads(quotafile.read())
|
|
|
|
quotafile.close()
|
|
|
|
if type(quotas) is list:
|
|
|
|
new_quotas = {}
|
2016-05-09 11:56:12 +08:00
|
|
|
new_quotas['default'] = 'foundation'
|
2016-04-29 15:40:53 +08:00
|
|
|
new_quotas['quotainfo'] = quotas
|
|
|
|
quotas = new_quotas
|
|
|
|
print("change the type of quotafile from list to dict")
|
|
|
|
keys = []
|
|
|
|
for quota in quotas['quotainfo']:
|
|
|
|
keys.append(quota['name'])
|
|
|
|
if 'cpu' not in keys:
|
|
|
|
quotas['quotainfo'].append({'name':'cpu', 'hint':'the cpu quota, number of cores, e.g. 4'})
|
|
|
|
if 'memory' not in keys:
|
|
|
|
quotas['quotainfo'].append({'name':'memory', 'hint':'the memory quota, number of MB, e.g. 4000'})
|
|
|
|
if 'disk' not in keys:
|
|
|
|
quotas['quotainfo'].append({'name':'disk', 'hint':'the disk quota, number of MB, e.g. 4000'})
|
|
|
|
if 'data' not in keys:
|
|
|
|
quotas['quotainfo'].append({'name':'data', 'hint':'the quota of data space, number of GB, e.g. 100'})
|
|
|
|
if 'image' not in keys:
|
|
|
|
quotas['quotainfo'].append({'name':'image', 'hint':'how many images the user can have, e.g. 8'})
|
|
|
|
if 'idletime' not in keys:
|
|
|
|
quotas['quotainfo'].append({'name':'idletime', 'hint':'will stop cluster after idletime, number of hours, e.g. 24'})
|
|
|
|
if 'vnode' not in keys:
|
|
|
|
quotas['quotainfo'].append({'name':'vnode', 'hint':'how many containers the user can have, e.g. 8'})
|
|
|
|
print("quotainfo updated")
|
|
|
|
quotafile = open(fspath+"/global/sys/quotainfo", 'w')
|
|
|
|
quotafile.write(json.dumps(quotas))
|
|
|
|
quotafile.close()
|
2016-05-09 10:57:49 +08:00
|
|
|
if not os.path.exists(fspath+"/global/sys/quota"):
|
|
|
|
print("quota file not exists, please run docklet to init it")
|
|
|
|
return False
|
|
|
|
groupfile = open(fspath+"/global/sys/quota",'r')
|
|
|
|
groups = json.loads(groupfile.read())
|
|
|
|
groupfile.close()
|
|
|
|
for group in groups:
|
|
|
|
if 'cpu' not in group['quotas'].keys():
|
|
|
|
group['quotas']['cpu'] = "4"
|
|
|
|
if 'memory' not in group['quotas'].keys():
|
|
|
|
group['quotas']['memory'] = "2000"
|
|
|
|
if 'disk' not in group['quotas'].keys():
|
|
|
|
group['quotas']['disk'] = "2000"
|
|
|
|
if 'data' not in group['quotas'].keys():
|
|
|
|
group['quotas']['data'] = "100"
|
|
|
|
if 'image' not in group['quotas'].keys():
|
|
|
|
group['quotas']['image'] = "10"
|
|
|
|
if 'idletime' not in group['quotas'].keys():
|
|
|
|
group['quotas']['idletime'] = "24"
|
|
|
|
if 'vnode' not in group['quotas'].keys():
|
|
|
|
group['quotas']['vnode'] = "8"
|
|
|
|
print("quota updated")
|
|
|
|
groupfile = open(fspath+"/global/sys/quota",'w')
|
|
|
|
groupfile.write(json.dumps(groups))
|
|
|
|
groupfile.close()
|
|
|
|
|
|
|
|
|
2016-05-09 11:56:12 +08:00
|
|
|
def name_error():
|
|
|
|
quotafile = open(fspath+"/global/sys/quotainfo", 'r')
|
|
|
|
quotas = json.loads(quotafile.read())
|
|
|
|
quotafile.close()
|
|
|
|
if quotas['default'] == 'fundation':
|
|
|
|
quotas['default'] = 'foundation'
|
|
|
|
quotafile = open(fspath+"/global/sys/quotainfo",'w')
|
|
|
|
quotafile.write(json.dumps(quotas))
|
|
|
|
quotafile.close()
|
|
|
|
|
|
|
|
groupfile = open(fspath+"/global/sys/quota", 'r')
|
|
|
|
groups = json.loads(groupfile.read())
|
|
|
|
groupfile.close()
|
|
|
|
for group in groups:
|
|
|
|
if group['name'] == 'fundation':
|
|
|
|
group['name'] = 'foundation'
|
|
|
|
groupfile = open(fspath+"/global/sys/quota",'w')
|
|
|
|
groupfile.write(json.dumps(groups))
|
|
|
|
groupfile.close()
|
|
|
|
|
|
|
|
users = User.query.filter_by(user_group = 'fundation').all()
|
|
|
|
for user in users:
|
|
|
|
user.user_group = 'foundation'
|
|
|
|
db.session.commit()
|
|
|
|
|
2016-04-29 15:40:53 +08:00
|
|
|
|
2016-05-06 16:41:11 +08:00
|
|
|
def allquota():
|
|
|
|
try:
|
|
|
|
quotafile = open(fspath+"/global/sys/quota", 'r')
|
|
|
|
quotas = json.loads(quotafile.read())
|
|
|
|
quotafile.close()
|
|
|
|
return quotas
|
|
|
|
except Exception as e:
|
|
|
|
print(e)
|
|
|
|
return None
|
|
|
|
|
|
|
|
def quotaquery(quotaname,quotas):
|
|
|
|
for quota in quotas:
|
|
|
|
if quota['name'] == quotaname:
|
|
|
|
return quota['quotas']
|
|
|
|
return None
|
|
|
|
|
|
|
|
def enable_gluster_quota():
|
|
|
|
conffile=open("../conf/docklet.conf",'r')
|
|
|
|
conf=conffile.readlines()
|
|
|
|
conffile.close()
|
|
|
|
enable = False
|
|
|
|
volume_name = ""
|
|
|
|
for line in conf:
|
2016-05-06 18:00:49 +08:00
|
|
|
if line.startswith("DATA_QUOTA"):
|
2016-05-06 16:41:11 +08:00
|
|
|
keyvalue = line.split("=")
|
|
|
|
if len(keyvalue) < 2:
|
|
|
|
continue
|
|
|
|
key = keyvalue[0].strip()
|
|
|
|
value = keyvalue[1].strip()
|
|
|
|
if value == "YES":
|
|
|
|
enable = True
|
|
|
|
break
|
|
|
|
for line in conf:
|
2016-05-06 18:00:49 +08:00
|
|
|
if line.startswith("DATA_QUOTA_CMD"):
|
2016-05-06 16:41:11 +08:00
|
|
|
keyvalue = line.split("=")
|
|
|
|
if len(keyvalue) < 2:
|
|
|
|
continue
|
|
|
|
volume_name = keyvalue[1].strip()
|
|
|
|
if not enable:
|
|
|
|
print("don't need to enable the quota")
|
|
|
|
return
|
|
|
|
|
|
|
|
users = User.query.all()
|
|
|
|
quotas = allquota()
|
|
|
|
if quotaquery == None:
|
|
|
|
print("quota info not found")
|
|
|
|
return
|
|
|
|
sys_run("gluster volume quota %s enable" % volume_name)
|
|
|
|
for user in users:
|
|
|
|
quota = quotaquery(user.user_group, quotas)
|
|
|
|
nfs_quota = quota['data']
|
|
|
|
if nfs_quota == None:
|
|
|
|
print("data quota should be set")
|
|
|
|
return
|
|
|
|
nfspath = "/users/%s/data" % user.username
|
|
|
|
sys_run("gluster volume quota %s limit-usage %s %sGB" % (volume_name,nfspath,nfs_quota))
|
|
|
|
|
2016-09-23 17:11:37 +08:00
|
|
|
def update_image():
|
|
|
|
private_imagepath = fspath + "/global/images/private/"
|
|
|
|
public_imagepath = fspath + "/global/images/public/"
|
|
|
|
userdirs = os.listdir(private_imagepath)
|
|
|
|
for userdir in userdirs:
|
|
|
|
if os.path.isdir(private_imagepath+userdir+"/"):
|
|
|
|
currentdir = private_imagepath+userdir+"/"
|
|
|
|
images = os.listdir(currentdir)
|
|
|
|
for image in images:
|
|
|
|
if os.path.isdir(currentdir+image+"/"):
|
|
|
|
try:
|
2016-12-17 17:55:39 +08:00
|
|
|
sys_run("tar -cvf %s -C %s ." % (currentdir+image+".tz",currentdir+image))
|
2016-12-04 17:35:10 +08:00
|
|
|
#sys_run("rm -rf %s" % currentdir+image)
|
2016-09-23 17:11:37 +08:00
|
|
|
except Exception as e:
|
|
|
|
print(e)
|
|
|
|
userdirs = os.listdir(public_imagepath)
|
|
|
|
for userdir in userdirs:
|
2016-12-04 17:35:10 +08:00
|
|
|
if os.path.isdir(public_imagepath+userdir+"/"):
|
2016-09-23 17:11:37 +08:00
|
|
|
currentdir = public_imagepath+userdir+"/"
|
|
|
|
images = os.listdir(currentdir)
|
|
|
|
for image in images:
|
|
|
|
if os.path.isdir(currentdir+image+"/"):
|
|
|
|
try:
|
2016-12-17 17:55:39 +08:00
|
|
|
sys_run("tar -cvf %s -C %s ." % (currentdir+image+".tz",currentdir+image))
|
2016-12-04 17:35:10 +08:00
|
|
|
#sys_run("rm -rf %s" % currentdir+image)
|
2016-09-23 17:11:37 +08:00
|
|
|
except Exception as e:
|
|
|
|
print(e)
|
|
|
|
|
2016-04-29 15:40:53 +08:00
|
|
|
if __name__ == '__main__':
|
2016-09-23 17:11:37 +08:00
|
|
|
# update_quotainfo()
|
2016-05-09 11:56:12 +08:00
|
|
|
if "fix-name-error" in sys.argv:
|
|
|
|
name_error()
|
2016-05-06 18:00:49 +08:00
|
|
|
# enable_gluster_quota()
|
2016-09-23 17:11:37 +08:00
|
|
|
if "update-image" in sys.argv:
|
|
|
|
update_image()
|