Now backend is flask-styled
fix a bug that will cause 404 in /login/
This commit is contained in:
parent
ea929a4dec
commit
c56c3d52db
945
src/httprest.py
945
src/httprest.py
|
@ -4,6 +4,8 @@
|
|||
# because some modules need variables when import
|
||||
# for example, userManager/model.py
|
||||
|
||||
from flask import Flask, request
|
||||
|
||||
# must first init loadenv
|
||||
import tools, env
|
||||
config = env.getenv("CONFIG")
|
||||
|
@ -27,475 +29,520 @@ external_login = env.getenv('EXTERNAL_LOGIN')
|
|||
if (external_login == 'TRUE'):
|
||||
from userDependence import external_auth
|
||||
|
||||
class DockletHttpHandler(http.server.BaseHTTPRequestHandler):
|
||||
def response(self, code, output):
|
||||
self.send_response(code)
|
||||
self.send_header("Content-type", "application/json")
|
||||
self.end_headers()
|
||||
# wfile/rfile are in byte/binary encoded. need to recode
|
||||
self.wfile.write(json.dumps(output).encode('ascii'))
|
||||
self.wfile.write("\n".encode('ascii'))
|
||||
# do not wfile.close()
|
||||
# because self.handle_one_request will call wfile.flush after calling do_*
|
||||
# and self.handle_one_request will close this wfile after timeout automatically
|
||||
# (see /usr/lib/python3.4/http/server.py handle_one_request function)
|
||||
#self.wfile.close()
|
||||
app = Flask(__name__)
|
||||
|
||||
# override log_request to not print default request log
|
||||
# we use the log info by ourselves in our style
|
||||
def log_request(code = '-', size = '-'):
|
||||
pass
|
||||
from functools import wraps
|
||||
|
||||
def do_PUT(self):
|
||||
self.response(400, {'success':'false', 'message':'Not supported methond'})
|
||||
|
||||
def do_GET(self):
|
||||
self.response(400, {'success':'false', 'message':'Not supported methond'})
|
||||
|
||||
def do_DELETE(self):
|
||||
self.response(400, {'success':'false', 'message':'Not supported methond'})
|
||||
|
||||
# handler POST request
|
||||
def do_POST(self):
|
||||
global G_vclustermgr
|
||||
def login_required(func):
|
||||
@wraps(func)
|
||||
def wrapper(*args, **kwargs):
|
||||
global G_usermgr
|
||||
#logger.info ("get request, header content:\n%s" % self.headers)
|
||||
#logger.info ("read request content:\n%s" % self.rfile.read(int(self.headers["Content-Length"])))
|
||||
logger.info ("get request, path: %s" % self.path)
|
||||
# for test
|
||||
if self.path == '/test':
|
||||
logger.info ("return welcome for test")
|
||||
self.response(200, {'success':'true', 'message':'welcome to docklet'})
|
||||
return [True, 'test ok']
|
||||
|
||||
# check for not null content
|
||||
if 'Content-Length' not in self.headers:
|
||||
logger.info ("request content is null")
|
||||
self.response(401, {'success':'false', 'message':'request content is null'})
|
||||
return [False, 'content is null']
|
||||
|
||||
# auth the user
|
||||
# cgi.FieldStorage need fp/headers/environ. (see /usr/lib/python3.4/cgi.py)
|
||||
form = cgi.FieldStorage(fp=self.rfile, headers=self.headers,environ={'REQUEST_METHOD':'POST'})
|
||||
cmds = self.path.strip('/').split('/')
|
||||
if cmds[0] == 'register' and form.getvalue('activate', None) == None:
|
||||
logger.info ("handle request : user register")
|
||||
username = form.getvalue('username', '')
|
||||
password = form.getvalue('password', '')
|
||||
email = form.getvalue('email', '')
|
||||
description = form.getvalue('description','')
|
||||
if (username == '' or password == '' or email == ''):
|
||||
self.response(500, {'success':'false'})
|
||||
newuser = G_usermgr.newuser()
|
||||
newuser.username = form.getvalue('username')
|
||||
newuser.password = form.getvalue('password')
|
||||
newuser.e_mail = form.getvalue('email')
|
||||
newuser.student_number = form.getvalue('studentnumber')
|
||||
newuser.department = form.getvalue('department')
|
||||
newuser.nickname = form.getvalue('truename')
|
||||
newuser.truename = form.getvalue('truename')
|
||||
newuser.description = form.getvalue('description')
|
||||
newuser.status = "init"
|
||||
newuser.auth_method = "local"
|
||||
result = G_usermgr.register(user = newuser)
|
||||
self.response(200, result)
|
||||
return [True, "register succeed"]
|
||||
if cmds[0] == 'login':
|
||||
logger.info ("handle request : user login")
|
||||
user = form.getvalue("user")
|
||||
key = form.getvalue("key")
|
||||
if user == None or key == None:
|
||||
self.response(401, {'success':'false', 'message':'user or key is null'})
|
||||
return [False, "auth failed"]
|
||||
auth_result = G_usermgr.auth(user, key)
|
||||
if auth_result['success'] == 'false':
|
||||
self.response(401, {'success':'false', 'message':'auth failed'})
|
||||
return [False, "auth failed"]
|
||||
self.response(200, {'success':'true', 'action':'login', 'data': auth_result['data']})
|
||||
return [True, "auth succeeded"]
|
||||
if cmds[0] == 'external_login':
|
||||
logger.info ("handle request : external user login")
|
||||
try:
|
||||
result = G_usermgr.auth_external(form)
|
||||
self.response(200, result)
|
||||
return result
|
||||
except:
|
||||
result = {'success': 'false', 'reason': 'Something wrong happened when auth an external account'}
|
||||
self.response(200, result)
|
||||
return result
|
||||
|
||||
token = form.getvalue("token")
|
||||
if token == None:
|
||||
self.response(401, {'success':'false', 'message':'user or key is null'})
|
||||
return [False, "auth failed"]
|
||||
logger.info ("get request, path: %s" % request.path)
|
||||
token = request.form.get("token", None)
|
||||
if (token == None):
|
||||
return {'success':'false', 'message':'user or key is null'}
|
||||
cur_user = G_usermgr.auth_token(token)
|
||||
if cur_user == None:
|
||||
self.response(401, {'success':'false', 'message':'token failed or expired', 'Unauthorized': 'True'})
|
||||
return [False, "auth failed"]
|
||||
|
||||
if (cur_user == None):
|
||||
return {'success':'false', 'message':'token failed or expired', 'Unauthorized': 'True'}
|
||||
return func(cur_user, cur_user.username, request.form, *args, **kwargs)
|
||||
|
||||
return wrapper
|
||||
|
||||
@app.route("/login/", methods=['POST'])
|
||||
def login():
|
||||
global G_usermgr
|
||||
logger.info ("handle request : user login")
|
||||
user = request.form.get("user", None)
|
||||
key = request.form.get("key", None)
|
||||
if user == None or key == None:
|
||||
return json.dumps({'success':'false', 'message':'user or key is null'})
|
||||
auth_result = G_usermgr.auth(user, key)
|
||||
if auth_result['success'] == 'false':
|
||||
return json.dumps({'success':'false', 'message':'auth failed'})
|
||||
return json.dumps({'success':'true', 'action':'login', 'data': auth_result['data']})
|
||||
|
||||
@app.route("/external_login/", methods=['POST'])
|
||||
def external_login():
|
||||
global G_usermgr
|
||||
logger.info ("handle request : external user login")
|
||||
try:
|
||||
result = G_usermgr.auth_external(request.form)
|
||||
return json.dumps(result)
|
||||
except:
|
||||
result = {'success': 'false', 'reason': 'Something wrong happened when auth an external account'}
|
||||
return json.dumps(result)
|
||||
|
||||
@app.route("/register/", methods=['POST'])
|
||||
def register():
|
||||
global G_usermgr
|
||||
if request.form.get('activate', None) == None:
|
||||
logger.info ("handle request : user register")
|
||||
username = request.form.get('username', '')
|
||||
password = request.form.get('password', '')
|
||||
email = request.form.get('email', '')
|
||||
description = request.form.get('description','')
|
||||
if (username == '' or password == '' or email == ''):
|
||||
return json.dumps({'success':'false'})
|
||||
newuser = G_usermgr.newuser()
|
||||
newuser.username = request.form.get('username')
|
||||
newuser.password = request.form.get('password')
|
||||
newuser.e_mail = request.form.get('email')
|
||||
newuser.student_number = request.form.get('studentnumber')
|
||||
newuser.department = request.form.get('department')
|
||||
newuser.nickname = request.form.get('truename')
|
||||
newuser.truename = request.form.get('truename')
|
||||
newuser.description = request.form.get('description')
|
||||
newuser.status = "init"
|
||||
newuser.auth_method = "local"
|
||||
result = G_usermgr.register(user = newuser)
|
||||
return json.dumps(result)
|
||||
else:
|
||||
logger.info ("handle request, user activating")
|
||||
token = request.form.get("token", None)
|
||||
if (token == None):
|
||||
return json.dumps({'success':'false', 'message':'user or key is null'})
|
||||
cur_user = G_usermgr.auth_token(token)
|
||||
if (cur_user == None):
|
||||
return json.dumps({'success':'false', 'message':'token failed or expired', 'Unauthorized': 'True'})
|
||||
newuser = G_usermgr.newuser()
|
||||
newuser.username = cur_user.username
|
||||
newuser.nickname = cur_user.truename
|
||||
newuser.status = 'applying'
|
||||
newuser.user_group = cur_user.user_group
|
||||
newuser.auth_method = cur_user.auth_method
|
||||
newuser.e_mail = form.get('email','')
|
||||
newuser.student_number = form.get('studentnumber', '')
|
||||
newuser.department = form.get('department', '')
|
||||
newuser.truename = form.get('truename', '')
|
||||
newuser.tel = form.get('tel', '')
|
||||
newuser.description = form.get('description', '')
|
||||
result = G_usermgr.register(user = newuser)
|
||||
userManager.send_remind_activating_email(newuser.username)
|
||||
return json.dumps(result)
|
||||
|
||||
|
||||
user = cur_user.username
|
||||
# parse the url and get to do actions
|
||||
# /cluster/list
|
||||
# /cluster/create & clustername
|
||||
# /cluster/start & clustername
|
||||
# /cluster/stop & clustername
|
||||
# /cluster/delete & clustername
|
||||
# /cluster/info & clustername
|
||||
@app.route("/cluster/create/", methods=['POST'])
|
||||
@login_required
|
||||
def create_cluster(cur_user, user, form):
|
||||
global G_usermgr
|
||||
global G_vclustermgr
|
||||
clustername = form.get('clustername', None)
|
||||
if (clustername == None):
|
||||
return json.dumps({'success':'false', 'message':'clustername is null'})
|
||||
image = {}
|
||||
image['name'] = form.get("imagename", None)
|
||||
image['type'] = form.get("imagetype", None)
|
||||
image['owner'] = form.get("imageowner", None)
|
||||
user_info = G_usermgr.selfQuery(cur_user = cur_user)
|
||||
user_info = json.dumps(user_info)
|
||||
logger.info ("handle request : create cluster %s with image %s " % (clustername, image['name']))
|
||||
[status, result] = G_vclustermgr.create_cluster(clustername, user, image, user_info)
|
||||
if status:
|
||||
return json.dumps({'success':'true', 'action':'create cluster', 'message':result})
|
||||
else:
|
||||
return json.dumps({'success':'false', 'action':'create cluster', 'message':result})
|
||||
|
||||
@app.route("/cluster/scaleout/", methods=['POST'])
|
||||
@login_required
|
||||
def scaleout_cluster(cur_user, user, form):
|
||||
global G_usermgr
|
||||
global G_vclustermgr
|
||||
clustername = form.get('clustername', None)
|
||||
if (clustername == None):
|
||||
return json.dumps({'success':'false', 'message':'clustername is null'})
|
||||
logger.info("handle request : scale out %s" % clustername)
|
||||
image = {}
|
||||
image['name'] = form.get("imagename", None)
|
||||
image['type'] = form.get("imagetype", None)
|
||||
image['owner'] = form.get("imageowner", None)
|
||||
logger.debug("imagename:" + image['name'])
|
||||
logger.debug("imagetype:" + image['type'])
|
||||
logger.debug("imageowner:" + image['owner'])
|
||||
user_info = G_usermgr.selfQuery(cur_user = cur_user)
|
||||
user_info = json.dumps(user_info)
|
||||
[status, result] = G_vclustermgr.scale_out_cluster(clustername, user, image, user_info)
|
||||
if status:
|
||||
return json.dumps({'success':'true', 'action':'scale out', 'message':result})
|
||||
else:
|
||||
return json.dumps({'success':'false', 'action':'scale out', 'message':result})
|
||||
|
||||
@app.route("/cluster/scalein/", methods=['POST'])
|
||||
@login_required
|
||||
def scalein_cluster(cur_user, user, form):
|
||||
global G_vclustermgr
|
||||
clustername = form.get('clustername', None)
|
||||
if (clustername == None):
|
||||
return json.dumps({'success':'false', 'message':'clustername is null'})
|
||||
logger.info("handle request : scale in %s" % clustername)
|
||||
containername = form.get("containername", None)
|
||||
[status, result] = G_vclustermgr.scale_in_cluster(clustername, user, containername)
|
||||
if status:
|
||||
return json.dumps({'success':'true', 'action':'scale in', 'message':result})
|
||||
else:
|
||||
return json.dumps({'success':'false', 'action':'scale in', 'message':result})
|
||||
|
||||
@app.route("/cluster/start/", methods=['POST'])
|
||||
@login_required
|
||||
def start_cluster(cur_user, user, form):
|
||||
global G_vclustermgr
|
||||
clustername = form.get('clustername', None)
|
||||
if (clustername == None):
|
||||
return json.dumps({'success':'false', 'message':'clustername is null'})
|
||||
logger.info ("handle request : start cluster %s" % clustername)
|
||||
[status, result] = G_vclustermgr.start_cluster(clustername, user)
|
||||
if status:
|
||||
return json.dumps({'success':'true', 'action':'start cluster', 'message':result})
|
||||
else:
|
||||
return json.dumps({'success':'false', 'action':'start cluster', 'message':result})
|
||||
|
||||
@app.route("/cluster/stop/", methods=['POST'])
|
||||
@login_required
|
||||
def stop_cluster(cur_user, user, form):
|
||||
global G_vclustermgr
|
||||
clustername = form.get('clustername', None)
|
||||
if (clustername == None):
|
||||
return json.dumps({'success':'false', 'message':'clustername is null'})
|
||||
logger.info ("handle request : start cluster %s" % clustername)
|
||||
[status, result] = G_vclustermgr.stop_cluster(clustername, user)
|
||||
if status:
|
||||
return json.dumps({'success':'true', 'action':'stop cluster', 'message':result})
|
||||
else:
|
||||
return json.dumps({'success':'false', 'action':'stop cluster', 'message':result})
|
||||
|
||||
@app.route("/cluster/delete/", methods=['POST'])
|
||||
@login_required
|
||||
def delete_cluster(cur_user, user, form):
|
||||
global G_vclustermgr
|
||||
clustername = form.get('clustername', None)
|
||||
if (clustername == None):
|
||||
return json.dumps({'success':'false', 'message':'clustername is null'})
|
||||
logger.info ("handle request : delete cluster %s" % clustername)
|
||||
[status, result] = G_vclustermgr.delete_cluster(clustername, user)
|
||||
if status:
|
||||
return json.dumps({'success':'true', 'action':'delete cluster', 'message':result})
|
||||
else:
|
||||
return json.dumps({'success':'false', 'action':'delete cluster', 'message':result})
|
||||
|
||||
@app.route("/cluster/info/", methods=['POST'])
|
||||
@login_required
|
||||
def info_cluster(cur_user, user, form):
|
||||
global G_vclustermgr
|
||||
clustername = form.get('clustername', None)
|
||||
if (clustername == None):
|
||||
return json.dumps({'success':'false', 'message':'clustername is null'})
|
||||
logger.info ("handle request : info cluster %s" % clustername)
|
||||
[status, result] = G_vclustermgr.get_clusterinfo(clustername, user)
|
||||
if status:
|
||||
return json.dumps({'success':'true', 'action':'info cluster', 'message':result})
|
||||
else:
|
||||
return json.dumps({'success':'false', 'action':'info cluster', 'message':result})
|
||||
|
||||
@app.route("/cluster/list/", methods=['POST'])
|
||||
@login_required
|
||||
def list_cluster(cur_user, user, form):
|
||||
global G_vclustermgr
|
||||
logger.info ("handle request : list clusters for %s" % user)
|
||||
[status, clusterlist] = G_vclustermgr.list_clusters(user)
|
||||
if status:
|
||||
return json.dumps({'success':'true', 'action':'list cluster', 'clusters':clusterlist})
|
||||
else:
|
||||
return json.dumps({'success':'false', 'action':'list cluster', 'message':clusterlist})
|
||||
|
||||
@app.route("/cluster/flush/", methods=['POST'])
|
||||
@login_required
|
||||
def flush_cluster(cur_user, user, form):
|
||||
global G_vclustermgr
|
||||
clustername = form.get('clustername', None)
|
||||
if (clustername == None):
|
||||
return json.dumps({'success':'false', 'message':'clustername is null'})
|
||||
from_lxc = form.get('from_lxc', None)
|
||||
G_vclustermgr.flush_cluster(user,clustername,from_lxc)
|
||||
return json.dumps({'success':'true', 'action':'flush'})
|
||||
|
||||
@app.route("/cluster/save/", methods=['POST'])
|
||||
@login_required
|
||||
def save_cluster(cur_user, user, form):
|
||||
global G_vclustermgr
|
||||
global G_usermgr
|
||||
clustername = form.get('clustername', None)
|
||||
if (clustername == None):
|
||||
return json.dumps({'success':'false', 'message':'clustername is null'})
|
||||
|
||||
imagename = form.get("image", None)
|
||||
description = form.get("description", None)
|
||||
containername = form.get("containername", None)
|
||||
isforce = form.get("isforce", None)
|
||||
if not isforce == "true":
|
||||
[status,message] = G_vclustermgr.image_check(user,imagename)
|
||||
if not status:
|
||||
return json.dumps({'success':'false','reason':'exists', 'message':message})
|
||||
|
||||
user_info = G_usermgr.selfQuery(cur_user = cur_user)
|
||||
[status,message] = G_vclustermgr.create_image(user,clustername,containername,imagename,description,user_info["data"]["groupinfo"]["image"])
|
||||
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})
|
||||
|
||||
|
||||
if cmds[0] == 'cluster':
|
||||
clustername = form.getvalue('clustername')
|
||||
# check for 'clustername' : all actions except 'list' need 'clustername'
|
||||
if (cmds[1] != 'list') and clustername == None:
|
||||
self.response(401, {'success':'false', 'message':'clustername is null'})
|
||||
return [False, "clustername is null"]
|
||||
if cmds[1] == 'create':
|
||||
image = {}
|
||||
image['name'] = form.getvalue("imagename")
|
||||
image['type'] = form.getvalue("imagetype")
|
||||
image['owner'] = form.getvalue("imageowner")
|
||||
user_info = G_usermgr.selfQuery(cur_user = cur_user)
|
||||
user_info = json.dumps(user_info)
|
||||
logger.info ("handle request : create cluster %s with image %s " % (clustername, image['name']))
|
||||
[status, result] = G_vclustermgr.create_cluster(clustername, user, image, user_info)
|
||||
if status:
|
||||
self.response(200, {'success':'true', 'action':'create cluster', 'message':result})
|
||||
else:
|
||||
self.response(200, {'success':'false', 'action':'create cluster', 'message':result})
|
||||
elif cmds[1] == 'scaleout':
|
||||
logger.info("handle request : scale out %s" % clustername)
|
||||
image = {}
|
||||
image['name'] = form.getvalue("imagename")
|
||||
image['type'] = form.getvalue("imagetype")
|
||||
image['owner'] = form.getvalue("imageowner")
|
||||
logger.debug("imagename:" + image['name'])
|
||||
logger.debug("imagetype:" + image['type'])
|
||||
logger.debug("imageowner:" + image['owner'])
|
||||
user_info = G_usermgr.selfQuery(cur_user = cur_user)
|
||||
user_info = json.dumps(user_info)
|
||||
[status, result] = G_vclustermgr.scale_out_cluster(clustername, user, image, user_info)
|
||||
if status:
|
||||
self.response(200, {'success':'true', 'action':'scale out', 'message':result})
|
||||
else:
|
||||
self.response(200, {'success':'false', 'action':'scale out', 'message':result})
|
||||
elif cmds[1] == 'scalein':
|
||||
logger.info("handle request : scale in %s" % clustername)
|
||||
containername = form.getvalue("containername")
|
||||
[status, result] = G_vclustermgr.scale_in_cluster(clustername, user, containername)
|
||||
if status:
|
||||
self.response(200, {'success':'true', 'action':'scale in', 'message':result})
|
||||
else:
|
||||
self.response(200, {'success':'false', 'action':'scale in', 'message':result})
|
||||
elif cmds[1] == 'start':
|
||||
logger.info ("handle request : start cluster %s" % clustername)
|
||||
[status, result] = G_vclustermgr.start_cluster(clustername, user)
|
||||
if status:
|
||||
self.response(200, {'success':'true', 'action':'start cluster', 'message':result})
|
||||
else:
|
||||
self.response(200, {'success':'false', 'action':'start cluster', 'message':result})
|
||||
elif cmds[1] == 'stop':
|
||||
logger.info ("handle request : stop cluster %s" % clustername)
|
||||
[status, result] = G_vclustermgr.stop_cluster(clustername, user)
|
||||
if status:
|
||||
self.response(200, {'success':'true', 'action':'stop cluster', 'message':result})
|
||||
else:
|
||||
self.response(200, {'success':'false', 'action':'stop cluster', 'message':result})
|
||||
elif cmds[1] == 'delete':
|
||||
logger.info ("handle request : delete cluster %s" % clustername)
|
||||
[status, result] = G_vclustermgr.delete_cluster(clustername, user)
|
||||
if status:
|
||||
self.response(200, {'success':'true', 'action':'delete cluster', 'message':result})
|
||||
else:
|
||||
self.response(200, {'success':'false', 'action':'delete cluster', 'message':result})
|
||||
elif cmds[1] == 'info':
|
||||
logger.info ("handle request : info cluster %s" % clustername)
|
||||
[status, result] = G_vclustermgr.get_clusterinfo(clustername, user)
|
||||
if status:
|
||||
self.response(200, {'success':'true', 'action':'info cluster', 'message':result})
|
||||
else:
|
||||
self.response(200, {'success':'false', 'action':'info cluster', 'message':result})
|
||||
elif cmds[1] == 'list':
|
||||
logger.info ("handle request : list clusters for %s" % user)
|
||||
[status, clusterlist] = G_vclustermgr.list_clusters(user)
|
||||
if status:
|
||||
self.response(200, {'success':'true', 'action':'list cluster', 'clusters':clusterlist})
|
||||
else:
|
||||
self.response(400, {'success':'false', 'action':'list cluster', 'message':clusterlist})
|
||||
@app.route("/image/list/", methods=['POST'])
|
||||
@login_required
|
||||
def list_image(cur_user, user, form):
|
||||
global G_imagemgr
|
||||
images = G_imagemgr.list_images(user)
|
||||
return json.dumps({'success':'true', 'images': images})
|
||||
|
||||
elif cmds[1] == 'flush':
|
||||
from_lxc = form.getvalue('from_lxc')
|
||||
G_vclustermgr.flush_cluster(user,clustername,from_lxc)
|
||||
self.response(200, {'success':'true', 'action':'flush'})
|
||||
@app.route("/image/description/", methods=['POST'])
|
||||
@login_required
|
||||
def description_image(cur_user, user, form):
|
||||
global G_imagemgr
|
||||
image = {}
|
||||
image['name'] = form.get("imagename", None)
|
||||
image['type'] = form.get("imagetype", None)
|
||||
image['owner'] = form.get("imageowner", None)
|
||||
description = G_imagemgr.get_image_description(user,image)
|
||||
return json.dumps({'success':'true', 'message':description})
|
||||
|
||||
elif cmds[1] == 'save':
|
||||
imagename = form.getvalue("image")
|
||||
description = form.getvalue("description")
|
||||
containername = form.getvalue("containername")
|
||||
isforce = form.getvalue("isforce")
|
||||
if not isforce == "true":
|
||||
[status,message] = G_vclustermgr.image_check(user,imagename)
|
||||
if not status:
|
||||
self.response(200, {'success':'false','reason':'exists', 'message':message})
|
||||
return [False, "image already exists"]
|
||||
user_info = G_usermgr.selfQuery(cur_user = cur_user)
|
||||
[status,message] = G_vclustermgr.create_image(user,clustername,containername,imagename,description,user_info["data"]["groupinfo"]["image"])
|
||||
if status:
|
||||
logger.info("image has been saved")
|
||||
self.response(200, {'success':'true', 'action':'save'})
|
||||
else:
|
||||
logger.debug(message)
|
||||
self.response(200, {'success':'false', 'reason':'exceed', 'message':message})
|
||||
@app.route("/image/share/", methods=['POST'])
|
||||
@login_required
|
||||
def share_image(cur_user, user, form):
|
||||
global G_imagemgr
|
||||
image = form.getvalue('image')
|
||||
G_imagemgr.shareImage(user,image)
|
||||
return json.dumps({'success':'true', 'action':'share'})
|
||||
|
||||
else:
|
||||
logger.warning ("request not supported ")
|
||||
self.response(400, {'success':'false', 'message':'not supported request'})
|
||||
@app.route("/image/unshare/", methods=['POST'])
|
||||
@login_required
|
||||
def unshare_image(cur_user, user, form):
|
||||
global G_imagemgr
|
||||
image = form.get('image', None)
|
||||
G_imagemgr.unshareImage(user,image)
|
||||
return json.dumps({'success':'true', 'action':'unshare'})
|
||||
|
||||
# Request for Image
|
||||
elif cmds[0] == 'image':
|
||||
if cmds[1] == 'list':
|
||||
images = G_imagemgr.list_images(user)
|
||||
self.response(200, {'success':'true', 'images': images})
|
||||
elif cmds[1] == 'description':
|
||||
image = {}
|
||||
image['name'] = form.getvalue("imagename")
|
||||
image['type'] = form.getvalue("imagetype")
|
||||
image['owner'] = form.getvalue("imageowner")
|
||||
description = G_imagemgr.get_image_description(user,image)
|
||||
self.response(200, {'success':'true', 'message':description})
|
||||
elif cmds[1] == 'share':
|
||||
image = form.getvalue('image')
|
||||
G_imagemgr.shareImage(user,image)
|
||||
self.response(200, {'success':'true', 'action':'share'})
|
||||
elif cmds[1] == 'unshare':
|
||||
image = form.getvalue('image')
|
||||
G_imagemgr.unshareImage(user,image)
|
||||
self.response(200, {'success':'true', 'action':'unshare'})
|
||||
elif cmds[1] == 'delete':
|
||||
image = form.getvalue('image')
|
||||
G_imagemgr.removeImage(user,image)
|
||||
self.response(200, {'success':'true', 'action':'delete'})
|
||||
else:
|
||||
logger.warning("request not supported ")
|
||||
self.response(400, {'success':'false', 'message':'not supported request'})
|
||||
@app.route("/image/delete/", methods=['POST'])
|
||||
@login_required
|
||||
def delete_image(cur_user, user, form):
|
||||
global G_imagemgr
|
||||
image = form.get('image', None)
|
||||
G_imagemgr.removeImage(user,image)
|
||||
return json.dumps({'success':'true', 'action':'delete'})
|
||||
|
||||
# Add Proxy
|
||||
elif cmds[0] == 'addproxy':
|
||||
logger.info ("handle request : add proxy")
|
||||
proxy_ip = form.getvalue("ip")
|
||||
proxy_port = form.getvalue("port")
|
||||
clustername = form.getvalue("clustername")
|
||||
[status, message] = G_vclustermgr.addproxy(user,clustername,proxy_ip,proxy_port)
|
||||
if status is True:
|
||||
self.response(200, {'success':'true', 'action':'addproxy'})
|
||||
else:
|
||||
self.response(400, {'success':'false', 'message': message})
|
||||
# Delete Proxy
|
||||
elif cmds[0] == 'deleteproxy':
|
||||
logger.info ("handle request : delete proxy")
|
||||
clustername = form.getvalue("clustername")
|
||||
G_vclustermgr.deleteproxy(user,clustername)
|
||||
self.response(200, {'success':'true', 'action':'deleteproxy'})
|
||||
@app.route("/addproxy/", methods=['POST'])
|
||||
@login_required
|
||||
def addproxy(cur_user, user, form):
|
||||
global G_vclustermgr
|
||||
logger.info ("handle request : add proxy")
|
||||
proxy_ip = form.get("ip", None)
|
||||
proxy_port = form.get("port", None)
|
||||
clustername = form.get("clustername", None)
|
||||
[status, message] = G_vclustermgr.addproxy(user,clustername,proxy_ip,proxy_port)
|
||||
if status is True:
|
||||
return json.dumps({'success':'true', 'action':'addproxy'})
|
||||
else:
|
||||
return json.dumps({'success':'false', 'message': message})
|
||||
|
||||
# Request for Monitor
|
||||
elif cmds[0] == 'monitor':
|
||||
logger.info("handle request: monitor")
|
||||
res = {}
|
||||
if cmds[1] == 'hosts':
|
||||
com_id = cmds[2]
|
||||
fetcher = monitor.Fetcher(etcdaddr,G_clustername,com_id)
|
||||
if cmds[3] == 'meminfo':
|
||||
res['meminfo'] = fetcher.get_meminfo()
|
||||
elif cmds[3] == 'cpuinfo':
|
||||
res['cpuinfo'] = fetcher.get_cpuinfo()
|
||||
elif cmds[3] == 'cpuconfig':
|
||||
res['cpuconfig'] = fetcher.get_cpuconfig()
|
||||
elif cmds[3] == 'diskinfo':
|
||||
res['diskinfo'] = fetcher.get_diskinfo()
|
||||
elif cmds[3] == 'osinfo':
|
||||
res['osinfo'] = fetcher.get_osinfo()
|
||||
elif cmds[3] == 'containers':
|
||||
res['containers'] = fetcher.get_containers()
|
||||
elif cmds[3] == 'status':
|
||||
res['status'] = fetcher.get_status()
|
||||
elif cmds[3] == 'containerslist':
|
||||
res['containerslist'] = fetcher.get_containerslist()
|
||||
elif cmds[3] == 'containersinfo':
|
||||
res = []
|
||||
conlist = fetcher.get_containerslist()
|
||||
for container in conlist:
|
||||
ans = {}
|
||||
confetcher = monitor.Container_Fetcher(etcdaddr,G_clustername)
|
||||
ans = confetcher.get_basic_info(container)
|
||||
ans['cpu_use'] = confetcher.get_cpu_use(container)
|
||||
ans['mem_use'] = confetcher.get_mem_use(container)
|
||||
res.append(ans)
|
||||
else:
|
||||
self.response(400, {'success':'false', 'message':'not supported request'})
|
||||
return
|
||||
@app.route("/deleteproxy/", methods=['POST'])
|
||||
@login_required
|
||||
def deleteproxy(cur_user, user, form):
|
||||
global G_vclustermgr
|
||||
logger.info ("handle request : delete proxy")
|
||||
clustername = form.get("clustername", None)
|
||||
G_vclustermgr.deleteproxy(user,clustername)
|
||||
self.response(200, {'success':'true', 'action':'deleteproxy'})
|
||||
|
||||
self.response(200, {'success':'true', 'monitor':res})
|
||||
elif cmds[1] == 'vnodes':
|
||||
fetcher = monitor.Container_Fetcher(etcdaddr,G_clustername)
|
||||
if cmds[3] == 'cpu_use':
|
||||
res['cpu_use'] = fetcher.get_cpu_use(cmds[2])
|
||||
elif cmds[3] == 'mem_use':
|
||||
res['mem_use'] = fetcher.get_mem_use(cmds[2])
|
||||
elif cmds[3] == 'disk_use':
|
||||
res['disk_use'] = fetcher.get_disk_use(cmds[2])
|
||||
elif cmds[3] == 'basic_info':
|
||||
res['basic_info'] = fetcher.get_basic_info(cmds[2])
|
||||
elif cmds[3] == 'owner':
|
||||
names = cmds[2].split('-')
|
||||
result = G_usermgr.query(username = names[0], cur_user = cur_user)
|
||||
if result['success'] == 'false':
|
||||
res['username'] = ""
|
||||
res['truename'] = ""
|
||||
else:
|
||||
res['username'] = result['data']['username']
|
||||
res['truename'] = result['data']['truename']
|
||||
else:
|
||||
res = "Unspported Method!"
|
||||
self.response(200, {'success':'true', 'monitor':res})
|
||||
elif cmds[1] == 'user':
|
||||
if cmds[2] == 'quotainfo':
|
||||
user_info = G_usermgr.selfQuery(cur_user = cur_user)
|
||||
quotainfo = user_info['data']['groupinfo']
|
||||
self.response(200, {'success':'true', 'quotainfo':quotainfo})
|
||||
'''if not user == 'root':
|
||||
self.response(400, {'success':'false', 'message':'Root Required'})
|
||||
if cmds[3] == 'clustercnt':
|
||||
flag = True
|
||||
clutotal = 0
|
||||
clurun = 0
|
||||
contotal = 0
|
||||
conrun = 0
|
||||
[status, clusterlist] = G_vclustermgr.list_clusters(cmds[2])
|
||||
if status:
|
||||
for clustername in clusterlist:
|
||||
clutotal += 1
|
||||
[status2, result] = G_vclustermgr.get_clusterinfo(clustername, cmds[2])
|
||||
if status2:
|
||||
contotal += result['size']
|
||||
if result['status'] == 'running':
|
||||
clurun += 1
|
||||
conrun += result['size']
|
||||
else:
|
||||
flag = False
|
||||
if flag:
|
||||
res = {}
|
||||
res['clutotal'] = clutotal
|
||||
res['clurun'] = clurun
|
||||
res['contotal'] = contotal
|
||||
res['conrun'] = conrun
|
||||
self.response(200, {'success':'true', 'monitor':{'clustercnt':res}})
|
||||
else:
|
||||
self.response(200, {'success':'false','message':clusterlist})
|
||||
elif cmds[3] == 'cluster':
|
||||
if cmds[4] == 'list':
|
||||
[status, clusterlist] = G_vclustermgr.list_clusters(cmds[2])
|
||||
if status:
|
||||
self.response(200, {'success':'true', 'monitor':{'clusters':clusterlist}})
|
||||
else:
|
||||
self.response(400, {'success':'false', 'message':clusterlist})
|
||||
elif cmds[4] == 'info':
|
||||
clustername = form.getvalue('clustername')
|
||||
logger.info ("handle request : info cluster %s" % clustername)
|
||||
[status, result] = G_vclustermgr.get_clusterinfo(clustername, user)
|
||||
if status:
|
||||
self.response(200, {'success':'true', 'monitor':{'info':result}})
|
||||
else:
|
||||
self.response(200, {'success':'false','message':result})
|
||||
else:
|
||||
self.response(400, {'success':'false', 'message':'not supported request'})'''
|
||||
@app.route("/monitor/hosts/<com_id>/<issue>/", methods=['POST'])
|
||||
@login_required
|
||||
def hosts_monitor(cur_user, user, form, com_id, issue):
|
||||
global G_clustername
|
||||
|
||||
elif cmds[1] == 'listphynodes':
|
||||
res['allnodes'] = G_nodemgr.get_allnodes()
|
||||
self.response(200, {'success':'true', 'monitor':res})
|
||||
# Request for User
|
||||
elif cmds[0] == 'user':
|
||||
logger.info("handle request: user")
|
||||
if cmds[1] == 'modify':
|
||||
#user = G_usermgr.query(username = form.getvalue("username"), cur_user = cur_user).get('token', None)
|
||||
result = G_usermgr.modify(newValue = form, cur_user = cur_user)
|
||||
self.response(200, result)
|
||||
if cmds[1] == 'groupModify':
|
||||
result = G_usermgr.groupModify(newValue = form, cur_user = cur_user)
|
||||
self.response(200, result)
|
||||
if cmds[1] == 'query':
|
||||
result = G_usermgr.query(ID = form.getvalue("ID"), cur_user = cur_user)
|
||||
if (result.get('success', None) == None or result.get('success', None) == "false"):
|
||||
self.response(301,result)
|
||||
else:
|
||||
result = G_usermgr.queryForDisplay(user = result['token'])
|
||||
self.response(200,result)
|
||||
logger.info("handle request: monitor/hosts")
|
||||
res = {}
|
||||
fetcher = monitor.Fetcher(etcdaddr,G_clustername,com_id)
|
||||
if issue == 'meminfo':
|
||||
res['meminfo'] = fetcher.get_meminfo()
|
||||
elif issue == 'cpuinfo':
|
||||
res['cpuinfo'] = fetcher.get_cpuinfo()
|
||||
elif issue == 'cpuconfig':
|
||||
res['cpuconfig'] = fetcher.get_cpuconfig()
|
||||
elif issue == 'diskinfo':
|
||||
res['diskinfo'] = fetcher.get_diskinfo()
|
||||
elif issue == 'osinfo':
|
||||
res['osinfo'] = fetcher.get_osinfo()
|
||||
elif issue == 'containers':
|
||||
res['containers'] = fetcher.get_containers()
|
||||
elif issue == 'status':
|
||||
res['status'] = fetcher.get_status()
|
||||
elif issue == 'containerslist':
|
||||
res['containerslist'] = fetcher.get_containerslist()
|
||||
elif issue == 'containersinfo':
|
||||
res = []
|
||||
conlist = fetcher.get_containerslist()
|
||||
for container in conlist:
|
||||
ans = {}
|
||||
confetcher = monitor.Container_Fetcher(etcdaddr,G_clustername)
|
||||
ans = confetcher.get_basic_info(container)
|
||||
ans['cpu_use'] = confetcher.get_cpu_use(container)
|
||||
ans['mem_use'] = confetcher.get_mem_use(container)
|
||||
res.append(ans)
|
||||
else:
|
||||
return json.dumps({'success':'false', 'message':'not supported request'})
|
||||
|
||||
elif cmds[1] == 'add':
|
||||
user = G_usermgr.newuser(cur_user = cur_user)
|
||||
user.username = form.getvalue('username')
|
||||
user.password = form.getvalue('password')
|
||||
user.e_mail = form.getvalue('e_mail', '')
|
||||
user.status = "normal"
|
||||
result = G_usermgr.register(user = user, cur_user = cur_user)
|
||||
self.response(200, result)
|
||||
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)
|
||||
elif cmds[1] == 'data':
|
||||
logger.info("handle request: user/data")
|
||||
result = G_usermgr.userList(cur_user = cur_user)
|
||||
self.response(200, result)
|
||||
elif cmds[1] == 'groupNameList':
|
||||
result = G_usermgr.groupListName(cur_user = cur_user)
|
||||
self.response(200, result)
|
||||
elif cmds[1] == 'groupList':
|
||||
result = G_usermgr.groupList(cur_user = cur_user)
|
||||
self.response(200, result)
|
||||
elif cmds[1] == 'groupQuery':
|
||||
result = G_usermgr.groupQuery(name = form.getvalue("name"), cur_user = cur_user)
|
||||
if (result.get('success', None) == None or result.get('success', None) == "false"):
|
||||
self.response(301,result)
|
||||
else:
|
||||
self.response(200,result)
|
||||
elif cmds[1] == 'selfQuery':
|
||||
result = G_usermgr.selfQuery(cur_user = cur_user)
|
||||
self.response(200,result)
|
||||
elif cmds[1] == 'selfModify':
|
||||
result = G_usermgr.selfModify(cur_user = cur_user, newValue = form)
|
||||
self.response(200,result)
|
||||
elif cmds[0] == 'register' :
|
||||
#activate
|
||||
logger.info("handle request: user/activate")
|
||||
newuser = G_usermgr.newuser()
|
||||
newuser.username = cur_user.username
|
||||
newuser.nickname = cur_user.truename
|
||||
newuser.status = 'applying'
|
||||
newuser.user_group = cur_user.user_group
|
||||
newuser.auth_method = cur_user.auth_method
|
||||
newuser.e_mail = form.getvalue('email','')
|
||||
newuser.student_number = form.getvalue('studentnumber', '')
|
||||
newuser.department = form.getvalue('department', '')
|
||||
newuser.truename = form.getvalue('truename', '')
|
||||
newuser.tel = form.getvalue('tel', '')
|
||||
newuser.description = form.getvalue('description', '')
|
||||
result = G_usermgr.register(user = newuser)
|
||||
userManager.send_remind_activating_email(newuser.username)
|
||||
self.response(200,result)
|
||||
return json.dumps({'success':'true', 'monitor':res})
|
||||
|
||||
|
||||
@app.route("/monitor/vnodes/<com_id>/<issue>/", methods=['POST'])
|
||||
@login_required
|
||||
def vnodes_monitor(cur_user, user, form, com_id, issue):
|
||||
global G_clustername
|
||||
logger.info("handle request: monitor/vnodes")
|
||||
res = {}
|
||||
fetcher = monitor.Container_Fetcher(etcdaddr,G_clustername)
|
||||
if issue == 'cpu_use':
|
||||
res['cpu_use'] = fetcher.get_cpu_use(cmds[2])
|
||||
elif issue == 'mem_use':
|
||||
res['mem_use'] = fetcher.get_mem_use(cmds[2])
|
||||
elif issue == 'disk_use':
|
||||
res['disk_use'] = fetcher.get_disk_use(cmds[2])
|
||||
elif issue == 'basic_info':
|
||||
res['basic_info'] = fetcher.get_basic_info(cmds[2])
|
||||
elif issue == 'owner':
|
||||
names = com_id.split('-')
|
||||
result = G_usermgr.query(username = names[0], cur_user = cur_user)
|
||||
if result['success'] == 'false':
|
||||
res['username'] = ""
|
||||
res['truename'] = ""
|
||||
else:
|
||||
logger.warning ("request not supported ")
|
||||
self.response(400, {'success':'false', 'message':'not supported request'})
|
||||
res['username'] = result['data']['username']
|
||||
res['truename'] = result['data']['truename']
|
||||
else:
|
||||
res = "Unspported Method!"
|
||||
return json.dumps({'success':'true', 'monitor':res})
|
||||
|
||||
|
||||
@app.route("/monitor/user/quotainfo/", methods=['POST'])
|
||||
@login_required
|
||||
def user_quotainfo_monitor(cur_user, user, form):
|
||||
global G_usermgr
|
||||
logger.info("handle request: monitor/user/quotainfo/")
|
||||
user_info = G_usermgr.selfQuery(cur_user = cur_user)
|
||||
quotainfo = user_info['data']['groupinfo']
|
||||
return json.dumps({'success':'true', 'quotainfo':quotainfo})
|
||||
|
||||
@app.route("/monitor/listphynodes/", methods=['POST'])
|
||||
@login_required
|
||||
def listphynodes_monitor(cur_user, user, form):
|
||||
global G_nodemgr
|
||||
logger.info("handle request: monitor/listphynodes/")
|
||||
res = {}
|
||||
res['allnodes'] = G_nodemgr.get_allnodes()
|
||||
return json.dumps({'success':'true', 'monitor':res})
|
||||
|
||||
|
||||
@app.route("/user/modify/", methods=['POST'])
|
||||
@login_required
|
||||
def modify_user(cur_user, user, form):
|
||||
global G_usermgr
|
||||
logger.info("handle request: user/modify/")
|
||||
result = G_usermgr.modify(newValue = form, cur_user = cur_user)
|
||||
return json.dumps(result)
|
||||
|
||||
@app.route("/user/groupModify/", methods=['POST'])
|
||||
@login_required
|
||||
def groupModify_user(cur_user, user, form):
|
||||
global G_usermgr
|
||||
logger.info("handle request: user/groupModify/")
|
||||
result = G_usermgr.groupModify(newValue = form, cur_user = cur_user)
|
||||
return json.dumps(result)
|
||||
|
||||
|
||||
@app.route("/user/query/", methods=['POST'])
|
||||
@login_required
|
||||
def query_user(cur_user, user, form):
|
||||
global G_usermgr
|
||||
logger.info("handle request: user/query/")
|
||||
result = G_usermgr.query(ID = form.get("ID"), cur_user = cur_user)
|
||||
if (result.get('success', None) == None or result.get('success', None) == "false"):
|
||||
return json.dumps(result)
|
||||
else:
|
||||
result = G_usermgr.queryForDisplay(user = result['token'])
|
||||
return json.dumps(result)
|
||||
|
||||
|
||||
@app.route("/user/add/", methods=['POST'])
|
||||
@login_required
|
||||
def add_user(cur_user, user, form):
|
||||
global G_usermgr
|
||||
logger.info("handle request: user/add/")
|
||||
user = G_usermgr.newuser(cur_user = cur_user)
|
||||
user.username = form.get('username', None)
|
||||
user.password = form.get('password', None)
|
||||
user.e_mail = form.get('e_mail', '')
|
||||
user.status = "normal"
|
||||
result = G_usermgr.register(user = user, cur_user = cur_user)
|
||||
return json.dumps(result)
|
||||
|
||||
|
||||
@app.route("/user/groupadd/", methods=['POST'])
|
||||
@login_required
|
||||
def groupadd_user(cur_user, user, form):
|
||||
global G_usermgr
|
||||
logger.info("handle request: user/groupadd/")
|
||||
result = G_usermgr.groupadd(form = form, cur_user = cur_user)
|
||||
return json.dumps(result)
|
||||
|
||||
|
||||
@app.route("/user/quotaadd/", methods=['POST'])
|
||||
@login_required
|
||||
def quotaadd_user(cur_user, user, form):
|
||||
global G_usermgr
|
||||
logger.info("handle request: user/quotaadd/")
|
||||
result = G_usermgr.quotaadd(form = form, cur_user = cur_user)
|
||||
return json.dumps(result)
|
||||
|
||||
|
||||
@app.route("/user/groupdel/", methods=['POST'])
|
||||
@login_required
|
||||
def groupdel_user(cur_user, user, form):
|
||||
global G_usermgr
|
||||
logger.info("handle request: user/groupdel/")
|
||||
result = G_usermgr.groupdel(name = form.get('name', None), cur_user = cur_user)
|
||||
return json.dumps(result)
|
||||
|
||||
|
||||
@app.route("/user/data/", methods=['POST'])
|
||||
@login_required
|
||||
def data_user(cur_user, user, form):
|
||||
global G_usermgr
|
||||
logger.info("handle request: user/data/")
|
||||
result = G_usermgr.userList(cur_user = cur_user)
|
||||
return json.dumps(result)
|
||||
|
||||
|
||||
class ThreadingHttpServer(ThreadingMixIn, http.server.HTTPServer):
|
||||
pass
|
||||
|
||||
if __name__ == '__main__':
|
||||
logger.info('Start Flask...:')
|
||||
try:
|
||||
secret_key_file = open(env.getenv('FS_PREFIX') + '/local/httprest_secret_key.txt')
|
||||
app.secret_key = secret_key_file.read()
|
||||
secret_key_file.close()
|
||||
except:
|
||||
from base64 import b64encode
|
||||
from os import urandom
|
||||
secret_key = urandom(24)
|
||||
secret_key = b64encode(secret_key).decode('utf-8')
|
||||
app.secret_key = secret_key
|
||||
secret_key_file = open(env.getenv('FS_PREFIX') + '/local/httprest_secret_key.txt', 'w')
|
||||
secret_key_file.write(secret_key)
|
||||
secret_key_file.close()
|
||||
|
||||
os.environ['APP_KEY'] = app.secret_key
|
||||
runcmd = sys.argv[0]
|
||||
app.runpath = runcmd.rsplit('/', 1)[0]
|
||||
|
||||
|
||||
global G_nodemgr
|
||||
global G_vclustermgr
|
||||
global G_usermgr
|
||||
|
@ -605,6 +652,6 @@ if __name__ == '__main__':
|
|||
logger.info("using MASTER_PORT %d", int(masterport))
|
||||
|
||||
# server = http.server.HTTPServer((masterip, masterport), DockletHttpHandler)
|
||||
server = ThreadingHttpServer((masterip, int(masterport)), DockletHttpHandler)
|
||||
logger.info("starting master server")
|
||||
server.serve_forever()
|
||||
|
||||
app.run(host = masterip, port = masterport, debug = True, threaded=True)
|
||||
|
|
|
@ -0,0 +1,610 @@
|
|||
#!/usr/bin/python3
|
||||
|
||||
# load environment variables in the beginning
|
||||
# because some modules need variables when import
|
||||
# for example, userManager/model.py
|
||||
|
||||
# must first init loadenv
|
||||
import tools, env
|
||||
config = env.getenv("CONFIG")
|
||||
tools.loadenv(config)
|
||||
|
||||
# second init logging
|
||||
# must import logger after initlogging, ugly
|
||||
from log import initlogging
|
||||
initlogging("docklet-master")
|
||||
from log import logger
|
||||
|
||||
import os
|
||||
import http.server, cgi, json, sys, shutil
|
||||
from socketserver import ThreadingMixIn
|
||||
import nodemgr, vclustermgr, etcdlib, network, imagemgr
|
||||
import userManager
|
||||
import monitor
|
||||
import guest_control, threading
|
||||
|
||||
external_login = env.getenv('EXTERNAL_LOGIN')
|
||||
if (external_login == 'TRUE'):
|
||||
from userDependence import external_auth
|
||||
|
||||
class DockletHttpHandler(http.server.BaseHTTPRequestHandler):
|
||||
def response(self, code, output):
|
||||
self.send_response(code)
|
||||
self.send_header("Content-type", "application/json")
|
||||
self.end_headers()
|
||||
# wfile/rfile are in byte/binary encoded. need to recode
|
||||
self.wfile.write(json.dumps(output).encode('ascii'))
|
||||
self.wfile.write("\n".encode('ascii'))
|
||||
# do not wfile.close()
|
||||
# because self.handle_one_request will call wfile.flush after calling do_*
|
||||
# and self.handle_one_request will close this wfile after timeout automatically
|
||||
# (see /usr/lib/python3.4/http/server.py handle_one_request function)
|
||||
#self.wfile.close()
|
||||
|
||||
# override log_request to not print default request log
|
||||
# we use the log info by ourselves in our style
|
||||
def log_request(code = '-', size = '-'):
|
||||
pass
|
||||
|
||||
def do_PUT(self):
|
||||
self.response(400, {'success':'false', 'message':'Not supported methond'})
|
||||
|
||||
def do_GET(self):
|
||||
self.response(400, {'success':'false', 'message':'Not supported methond'})
|
||||
|
||||
def do_DELETE(self):
|
||||
self.response(400, {'success':'false', 'message':'Not supported methond'})
|
||||
|
||||
# handler POST request
|
||||
def do_POST(self):
|
||||
global G_vclustermgr
|
||||
global G_usermgr
|
||||
#logger.info ("get request, header content:\n%s" % self.headers)
|
||||
#logger.info ("read request content:\n%s" % self.rfile.read(int(self.headers["Content-Length"])))
|
||||
logger.info ("get request, path: %s" % self.path)
|
||||
# for test
|
||||
if self.path == '/test':
|
||||
logger.info ("return welcome for test")
|
||||
self.response(200, {'success':'true', 'message':'welcome to docklet'})
|
||||
return [True, 'test ok']
|
||||
|
||||
# check for not null content
|
||||
if 'Content-Length' not in self.headers:
|
||||
logger.info ("request content is null")
|
||||
self.response(401, {'success':'false', 'message':'request content is null'})
|
||||
return [False, 'content is null']
|
||||
|
||||
# auth the user
|
||||
# cgi.FieldStorage need fp/headers/environ. (see /usr/lib/python3.4/cgi.py)
|
||||
form = cgi.FieldStorage(fp=self.rfile, headers=self.headers,environ={'REQUEST_METHOD':'POST'})
|
||||
cmds = self.path.strip('/').split('/')
|
||||
if cmds[0] == 'register' and form.getvalue('activate', None) == None:
|
||||
logger.info ("handle request : user register")
|
||||
username = form.getvalue('username', '')
|
||||
password = form.getvalue('password', '')
|
||||
email = form.getvalue('email', '')
|
||||
description = form.getvalue('description','')
|
||||
if (username == '' or password == '' or email == ''):
|
||||
self.response(500, {'success':'false'})
|
||||
newuser = G_usermgr.newuser()
|
||||
newuser.username = form.getvalue('username')
|
||||
newuser.password = form.getvalue('password')
|
||||
newuser.e_mail = form.getvalue('email')
|
||||
newuser.student_number = form.getvalue('studentnumber')
|
||||
newuser.department = form.getvalue('department')
|
||||
newuser.nickname = form.getvalue('truename')
|
||||
newuser.truename = form.getvalue('truename')
|
||||
newuser.description = form.getvalue('description')
|
||||
newuser.status = "init"
|
||||
newuser.auth_method = "local"
|
||||
result = G_usermgr.register(user = newuser)
|
||||
self.response(200, result)
|
||||
return [True, "register succeed"]
|
||||
if cmds[0] == 'login':
|
||||
logger.info ("handle request : user login")
|
||||
user = form.getvalue("user")
|
||||
key = form.getvalue("key")
|
||||
if user == None or key == None:
|
||||
self.response(401, {'success':'false', 'message':'user or key is null'})
|
||||
return [False, "auth failed"]
|
||||
auth_result = G_usermgr.auth(user, key)
|
||||
if auth_result['success'] == 'false':
|
||||
self.response(401, {'success':'false', 'message':'auth failed'})
|
||||
return [False, "auth failed"]
|
||||
self.response(200, {'success':'true', 'action':'login', 'data': auth_result['data']})
|
||||
return [True, "auth succeeded"]
|
||||
if cmds[0] == 'external_login':
|
||||
logger.info ("handle request : external user login")
|
||||
try:
|
||||
result = G_usermgr.auth_external(form)
|
||||
self.response(200, result)
|
||||
return result
|
||||
except:
|
||||
result = {'success': 'false', 'reason': 'Something wrong happened when auth an external account'}
|
||||
self.response(200, result)
|
||||
return result
|
||||
|
||||
token = form.getvalue("token")
|
||||
if token == None:
|
||||
self.response(401, {'success':'false', 'message':'user or key is null'})
|
||||
return [False, "auth failed"]
|
||||
cur_user = G_usermgr.auth_token(token)
|
||||
if cur_user == None:
|
||||
self.response(401, {'success':'false', 'message':'token failed or expired', 'Unauthorized': 'True'})
|
||||
return [False, "auth failed"]
|
||||
|
||||
|
||||
|
||||
user = cur_user.username
|
||||
# parse the url and get to do actions
|
||||
# /cluster/list
|
||||
# /cluster/create & clustername
|
||||
# /cluster/start & clustername
|
||||
# /cluster/stop & clustername
|
||||
# /cluster/delete & clustername
|
||||
# /cluster/info & clustername
|
||||
|
||||
|
||||
if cmds[0] == 'cluster':
|
||||
clustername = form.getvalue('clustername')
|
||||
# check for 'clustername' : all actions except 'list' need 'clustername'
|
||||
if (cmds[1] != 'list') and clustername == None:
|
||||
self.response(401, {'success':'false', 'message':'clustername is null'})
|
||||
return [False, "clustername is null"]
|
||||
if cmds[1] == 'create':
|
||||
image = {}
|
||||
image['name'] = form.getvalue("imagename")
|
||||
image['type'] = form.getvalue("imagetype")
|
||||
image['owner'] = form.getvalue("imageowner")
|
||||
user_info = G_usermgr.selfQuery(cur_user = cur_user)
|
||||
user_info = json.dumps(user_info)
|
||||
logger.info ("handle request : create cluster %s with image %s " % (clustername, image['name']))
|
||||
[status, result] = G_vclustermgr.create_cluster(clustername, user, image, user_info)
|
||||
if status:
|
||||
self.response(200, {'success':'true', 'action':'create cluster', 'message':result})
|
||||
else:
|
||||
self.response(200, {'success':'false', 'action':'create cluster', 'message':result})
|
||||
elif cmds[1] == 'scaleout':
|
||||
logger.info("handle request : scale out %s" % clustername)
|
||||
image = {}
|
||||
image['name'] = form.getvalue("imagename")
|
||||
image['type'] = form.getvalue("imagetype")
|
||||
image['owner'] = form.getvalue("imageowner")
|
||||
logger.debug("imagename:" + image['name'])
|
||||
logger.debug("imagetype:" + image['type'])
|
||||
logger.debug("imageowner:" + image['owner'])
|
||||
user_info = G_usermgr.selfQuery(cur_user = cur_user)
|
||||
user_info = json.dumps(user_info)
|
||||
[status, result] = G_vclustermgr.scale_out_cluster(clustername, user, image, user_info)
|
||||
if status:
|
||||
self.response(200, {'success':'true', 'action':'scale out', 'message':result})
|
||||
else:
|
||||
self.response(200, {'success':'false', 'action':'scale out', 'message':result})
|
||||
elif cmds[1] == 'scalein':
|
||||
logger.info("handle request : scale in %s" % clustername)
|
||||
containername = form.getvalue("containername")
|
||||
[status, result] = G_vclustermgr.scale_in_cluster(clustername, user, containername)
|
||||
if status:
|
||||
self.response(200, {'success':'true', 'action':'scale in', 'message':result})
|
||||
else:
|
||||
self.response(200, {'success':'false', 'action':'scale in', 'message':result})
|
||||
elif cmds[1] == 'start':
|
||||
logger.info ("handle request : start cluster %s" % clustername)
|
||||
[status, result] = G_vclustermgr.start_cluster(clustername, user)
|
||||
if status:
|
||||
self.response(200, {'success':'true', 'action':'start cluster', 'message':result})
|
||||
else:
|
||||
self.response(200, {'success':'false', 'action':'start cluster', 'message':result})
|
||||
elif cmds[1] == 'stop':
|
||||
logger.info ("handle request : stop cluster %s" % clustername)
|
||||
[status, result] = G_vclustermgr.stop_cluster(clustername, user)
|
||||
if status:
|
||||
self.response(200, {'success':'true', 'action':'stop cluster', 'message':result})
|
||||
else:
|
||||
self.response(200, {'success':'false', 'action':'stop cluster', 'message':result})
|
||||
elif cmds[1] == 'delete':
|
||||
logger.info ("handle request : delete cluster %s" % clustername)
|
||||
[status, result] = G_vclustermgr.delete_cluster(clustername, user)
|
||||
if status:
|
||||
self.response(200, {'success':'true', 'action':'delete cluster', 'message':result})
|
||||
else:
|
||||
self.response(200, {'success':'false', 'action':'delete cluster', 'message':result})
|
||||
elif cmds[1] == 'info':
|
||||
logger.info ("handle request : info cluster %s" % clustername)
|
||||
[status, result] = G_vclustermgr.get_clusterinfo(clustername, user)
|
||||
if status:
|
||||
self.response(200, {'success':'true', 'action':'info cluster', 'message':result})
|
||||
else:
|
||||
self.response(200, {'success':'false', 'action':'info cluster', 'message':result})
|
||||
elif cmds[1] == 'list':
|
||||
logger.info ("handle request : list clusters for %s" % user)
|
||||
[status, clusterlist] = G_vclustermgr.list_clusters(user)
|
||||
if status:
|
||||
self.response(200, {'success':'true', 'action':'list cluster', 'clusters':clusterlist})
|
||||
else:
|
||||
self.response(400, {'success':'false', 'action':'list cluster', 'message':clusterlist})
|
||||
|
||||
elif cmds[1] == 'flush':
|
||||
from_lxc = form.getvalue('from_lxc')
|
||||
G_vclustermgr.flush_cluster(user,clustername,from_lxc)
|
||||
self.response(200, {'success':'true', 'action':'flush'})
|
||||
|
||||
elif cmds[1] == 'save':
|
||||
imagename = form.getvalue("image")
|
||||
description = form.getvalue("description")
|
||||
containername = form.getvalue("containername")
|
||||
isforce = form.getvalue("isforce")
|
||||
if not isforce == "true":
|
||||
[status,message] = G_vclustermgr.image_check(user,imagename)
|
||||
if not status:
|
||||
self.response(200, {'success':'false','reason':'exists', 'message':message})
|
||||
return [False, "image already exists"]
|
||||
user_info = G_usermgr.selfQuery(cur_user = cur_user)
|
||||
[status,message] = G_vclustermgr.create_image(user,clustername,containername,imagename,description,user_info["data"]["groupinfo"]["image"])
|
||||
if status:
|
||||
logger.info("image has been saved")
|
||||
self.response(200, {'success':'true', 'action':'save'})
|
||||
else:
|
||||
logger.debug(message)
|
||||
self.response(200, {'success':'false', 'reason':'exceed', 'message':message})
|
||||
|
||||
else:
|
||||
logger.warning ("request not supported ")
|
||||
self.response(400, {'success':'false', 'message':'not supported request'})
|
||||
|
||||
# Request for Image
|
||||
elif cmds[0] == 'image':
|
||||
if cmds[1] == 'list':
|
||||
images = G_imagemgr.list_images(user)
|
||||
self.response(200, {'success':'true', 'images': images})
|
||||
elif cmds[1] == 'description':
|
||||
image = {}
|
||||
image['name'] = form.getvalue("imagename")
|
||||
image['type'] = form.getvalue("imagetype")
|
||||
image['owner'] = form.getvalue("imageowner")
|
||||
description = G_imagemgr.get_image_description(user,image)
|
||||
self.response(200, {'success':'true', 'message':description})
|
||||
elif cmds[1] == 'share':
|
||||
image = form.getvalue('image')
|
||||
G_imagemgr.shareImage(user,image)
|
||||
self.response(200, {'success':'true', 'action':'share'})
|
||||
elif cmds[1] == 'unshare':
|
||||
image = form.getvalue('image')
|
||||
G_imagemgr.unshareImage(user,image)
|
||||
self.response(200, {'success':'true', 'action':'unshare'})
|
||||
elif cmds[1] == 'delete':
|
||||
image = form.getvalue('image')
|
||||
G_imagemgr.removeImage(user,image)
|
||||
self.response(200, {'success':'true', 'action':'delete'})
|
||||
else:
|
||||
logger.warning("request not supported ")
|
||||
self.response(400, {'success':'false', 'message':'not supported request'})
|
||||
|
||||
# Add Proxy
|
||||
elif cmds[0] == 'addproxy':
|
||||
logger.info ("handle request : add proxy")
|
||||
proxy_ip = form.getvalue("ip")
|
||||
proxy_port = form.getvalue("port")
|
||||
clustername = form.getvalue("clustername")
|
||||
[status, message] = G_vclustermgr.addproxy(user,clustername,proxy_ip,proxy_port)
|
||||
if status is True:
|
||||
self.response(200, {'success':'true', 'action':'addproxy'})
|
||||
else:
|
||||
self.response(400, {'success':'false', 'message': message})
|
||||
# Delete Proxy
|
||||
elif cmds[0] == 'deleteproxy':
|
||||
logger.info ("handle request : delete proxy")
|
||||
clustername = form.getvalue("clustername")
|
||||
G_vclustermgr.deleteproxy(user,clustername)
|
||||
self.response(200, {'success':'true', 'action':'deleteproxy'})
|
||||
|
||||
# Request for Monitor
|
||||
elif cmds[0] == 'monitor':
|
||||
logger.info("handle request: monitor")
|
||||
res = {}
|
||||
if cmds[1] == 'hosts':
|
||||
com_id = cmds[2]
|
||||
fetcher = monitor.Fetcher(etcdaddr,G_clustername,com_id)
|
||||
if cmds[3] == 'meminfo':
|
||||
res['meminfo'] = fetcher.get_meminfo()
|
||||
elif cmds[3] == 'cpuinfo':
|
||||
res['cpuinfo'] = fetcher.get_cpuinfo()
|
||||
elif cmds[3] == 'cpuconfig':
|
||||
res['cpuconfig'] = fetcher.get_cpuconfig()
|
||||
elif cmds[3] == 'diskinfo':
|
||||
res['diskinfo'] = fetcher.get_diskinfo()
|
||||
elif cmds[3] == 'osinfo':
|
||||
res['osinfo'] = fetcher.get_osinfo()
|
||||
elif cmds[3] == 'containers':
|
||||
res['containers'] = fetcher.get_containers()
|
||||
elif cmds[3] == 'status':
|
||||
res['status'] = fetcher.get_status()
|
||||
elif cmds[3] == 'containerslist':
|
||||
res['containerslist'] = fetcher.get_containerslist()
|
||||
elif cmds[3] == 'containersinfo':
|
||||
res = []
|
||||
conlist = fetcher.get_containerslist()
|
||||
for container in conlist:
|
||||
ans = {}
|
||||
confetcher = monitor.Container_Fetcher(etcdaddr,G_clustername)
|
||||
ans = confetcher.get_basic_info(container)
|
||||
ans['cpu_use'] = confetcher.get_cpu_use(container)
|
||||
ans['mem_use'] = confetcher.get_mem_use(container)
|
||||
res.append(ans)
|
||||
else:
|
||||
self.response(400, {'success':'false', 'message':'not supported request'})
|
||||
return
|
||||
|
||||
self.response(200, {'success':'true', 'monitor':res})
|
||||
elif cmds[1] == 'vnodes':
|
||||
fetcher = monitor.Container_Fetcher(etcdaddr,G_clustername)
|
||||
if cmds[3] == 'cpu_use':
|
||||
res['cpu_use'] = fetcher.get_cpu_use(cmds[2])
|
||||
elif cmds[3] == 'mem_use':
|
||||
res['mem_use'] = fetcher.get_mem_use(cmds[2])
|
||||
elif cmds[3] == 'disk_use':
|
||||
res['disk_use'] = fetcher.get_disk_use(cmds[2])
|
||||
elif cmds[3] == 'basic_info':
|
||||
res['basic_info'] = fetcher.get_basic_info(cmds[2])
|
||||
elif cmds[3] == 'owner':
|
||||
names = cmds[2].split('-')
|
||||
result = G_usermgr.query(username = names[0], cur_user = cur_user)
|
||||
if result['success'] == 'false':
|
||||
res['username'] = ""
|
||||
res['truename'] = ""
|
||||
else:
|
||||
res['username'] = result['data']['username']
|
||||
res['truename'] = result['data']['truename']
|
||||
else:
|
||||
res = "Unspported Method!"
|
||||
self.response(200, {'success':'true', 'monitor':res})
|
||||
elif cmds[1] == 'user':
|
||||
if cmds[2] == 'quotainfo':
|
||||
user_info = G_usermgr.selfQuery(cur_user = cur_user)
|
||||
quotainfo = user_info['data']['groupinfo']
|
||||
self.response(200, {'success':'true', 'quotainfo':quotainfo})
|
||||
'''if not user == 'root':
|
||||
self.response(400, {'success':'false', 'message':'Root Required'})
|
||||
if cmds[3] == 'clustercnt':
|
||||
flag = True
|
||||
clutotal = 0
|
||||
clurun = 0
|
||||
contotal = 0
|
||||
conrun = 0
|
||||
[status, clusterlist] = G_vclustermgr.list_clusters(cmds[2])
|
||||
if status:
|
||||
for clustername in clusterlist:
|
||||
clutotal += 1
|
||||
[status2, result] = G_vclustermgr.get_clusterinfo(clustername, cmds[2])
|
||||
if status2:
|
||||
contotal += result['size']
|
||||
if result['status'] == 'running':
|
||||
clurun += 1
|
||||
conrun += result['size']
|
||||
else:
|
||||
flag = False
|
||||
if flag:
|
||||
res = {}
|
||||
res['clutotal'] = clutotal
|
||||
res['clurun'] = clurun
|
||||
res['contotal'] = contotal
|
||||
res['conrun'] = conrun
|
||||
self.response(200, {'success':'true', 'monitor':{'clustercnt':res}})
|
||||
else:
|
||||
self.response(200, {'success':'false','message':clusterlist})
|
||||
elif cmds[3] == 'cluster':
|
||||
if cmds[4] == 'list':
|
||||
[status, clusterlist] = G_vclustermgr.list_clusters(cmds[2])
|
||||
if status:
|
||||
self.response(200, {'success':'true', 'monitor':{'clusters':clusterlist}})
|
||||
else:
|
||||
self.response(400, {'success':'false', 'message':clusterlist})
|
||||
elif cmds[4] == 'info':
|
||||
clustername = form.getvalue('clustername')
|
||||
logger.info ("handle request : info cluster %s" % clustername)
|
||||
[status, result] = G_vclustermgr.get_clusterinfo(clustername, user)
|
||||
if status:
|
||||
self.response(200, {'success':'true', 'monitor':{'info':result}})
|
||||
else:
|
||||
self.response(200, {'success':'false','message':result})
|
||||
else:
|
||||
self.response(400, {'success':'false', 'message':'not supported request'})'''
|
||||
|
||||
elif cmds[1] == 'listphynodes':
|
||||
res['allnodes'] = G_nodemgr.get_allnodes()
|
||||
self.response(200, {'success':'true', 'monitor':res})
|
||||
# Request for User
|
||||
elif cmds[0] == 'user':
|
||||
logger.info("handle request: user")
|
||||
if cmds[1] == 'modify':
|
||||
#user = G_usermgr.query(username = form.getvalue("username"), cur_user = cur_user).get('token', None)
|
||||
result = G_usermgr.modify(newValue = form, cur_user = cur_user)
|
||||
self.response(200, result)
|
||||
if cmds[1] == 'groupModify':
|
||||
result = G_usermgr.groupModify(newValue = form, cur_user = cur_user)
|
||||
self.response(200, result)
|
||||
if cmds[1] == 'query':
|
||||
result = G_usermgr.query(ID = form.getvalue("ID"), cur_user = cur_user)
|
||||
if (result.get('success', None) == None or result.get('success', None) == "false"):
|
||||
self.response(301,result)
|
||||
else:
|
||||
result = G_usermgr.queryForDisplay(user = result['token'])
|
||||
self.response(200,result)
|
||||
|
||||
elif cmds[1] == 'add':
|
||||
user = G_usermgr.newuser(cur_user = cur_user)
|
||||
user.username = form.getvalue('username')
|
||||
user.password = form.getvalue('password')
|
||||
user.e_mail = form.getvalue('e_mail', '')
|
||||
user.status = "normal"
|
||||
result = G_usermgr.register(user = user, cur_user = cur_user)
|
||||
self.response(200, result)
|
||||
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)
|
||||
elif cmds[1] == 'data':
|
||||
logger.info("handle request: user/data")
|
||||
result = G_usermgr.userList(cur_user = cur_user)
|
||||
self.response(200, result)
|
||||
elif cmds[1] == 'groupNameList':
|
||||
result = G_usermgr.groupListName(cur_user = cur_user)
|
||||
self.response(200, result)
|
||||
elif cmds[1] == 'groupList':
|
||||
result = G_usermgr.groupList(cur_user = cur_user)
|
||||
self.response(200, result)
|
||||
elif cmds[1] == 'groupQuery':
|
||||
result = G_usermgr.groupQuery(name = form.getvalue("name"), cur_user = cur_user)
|
||||
if (result.get('success', None) == None or result.get('success', None) == "false"):
|
||||
self.response(301,result)
|
||||
else:
|
||||
self.response(200,result)
|
||||
elif cmds[1] == 'selfQuery':
|
||||
result = G_usermgr.selfQuery(cur_user = cur_user)
|
||||
self.response(200,result)
|
||||
elif cmds[1] == 'selfModify':
|
||||
result = G_usermgr.selfModify(cur_user = cur_user, newValue = form)
|
||||
self.response(200,result)
|
||||
elif cmds[0] == 'register' :
|
||||
#activate
|
||||
logger.info("handle request: user/activate")
|
||||
newuser = G_usermgr.newuser()
|
||||
newuser.username = cur_user.username
|
||||
newuser.nickname = cur_user.truename
|
||||
newuser.status = 'applying'
|
||||
newuser.user_group = cur_user.user_group
|
||||
newuser.auth_method = cur_user.auth_method
|
||||
newuser.e_mail = form.getvalue('email','')
|
||||
newuser.student_number = form.getvalue('studentnumber', '')
|
||||
newuser.department = form.getvalue('department', '')
|
||||
newuser.truename = form.getvalue('truename', '')
|
||||
newuser.tel = form.getvalue('tel', '')
|
||||
newuser.description = form.getvalue('description', '')
|
||||
result = G_usermgr.register(user = newuser)
|
||||
userManager.send_remind_activating_email(newuser.username)
|
||||
self.response(200,result)
|
||||
else:
|
||||
logger.warning ("request not supported ")
|
||||
self.response(400, {'success':'false', 'message':'not supported request'})
|
||||
|
||||
class ThreadingHttpServer(ThreadingMixIn, http.server.HTTPServer):
|
||||
pass
|
||||
|
||||
if __name__ == '__main__':
|
||||
global G_nodemgr
|
||||
global G_vclustermgr
|
||||
global G_usermgr
|
||||
global etcdclient
|
||||
global G_networkmgr
|
||||
global G_clustername
|
||||
# move 'tools.loadenv' to the beginning of this file
|
||||
|
||||
fs_path = env.getenv("FS_PREFIX")
|
||||
logger.info("using FS_PREFIX %s" % fs_path)
|
||||
|
||||
etcdaddr = env.getenv("ETCD")
|
||||
logger.info("using ETCD %s" % etcdaddr)
|
||||
|
||||
G_clustername = env.getenv("CLUSTER_NAME")
|
||||
logger.info("using CLUSTER_NAME %s" % G_clustername)
|
||||
|
||||
# get network interface
|
||||
net_dev = env.getenv("NETWORK_DEVICE")
|
||||
logger.info("using NETWORK_DEVICE %s" % net_dev)
|
||||
|
||||
ipaddr = network.getip(net_dev)
|
||||
if ipaddr==False:
|
||||
logger.error("network device is not correct")
|
||||
sys.exit(1)
|
||||
else:
|
||||
logger.info("using ipaddr %s" % ipaddr)
|
||||
|
||||
# init etcdlib client
|
||||
try:
|
||||
etcdclient = etcdlib.Client(etcdaddr, prefix = G_clustername)
|
||||
except Exception:
|
||||
logger.error ("connect etcd failed, maybe etcd address not correct...")
|
||||
sys.exit(1)
|
||||
mode = 'recovery'
|
||||
if len(sys.argv) > 1 and sys.argv[1] == "new":
|
||||
mode = 'new'
|
||||
|
||||
# do some initialization for mode: new/recovery
|
||||
if mode == 'new':
|
||||
# clean and initialize the etcd table
|
||||
if etcdclient.isdir(""):
|
||||
etcdclient.clean()
|
||||
else:
|
||||
etcdclient.createdir("")
|
||||
token = tools.gen_token()
|
||||
tokenfile = open(fs_path+"/global/token", 'w')
|
||||
tokenfile.write(token)
|
||||
tokenfile.write("\n")
|
||||
tokenfile.close()
|
||||
etcdclient.setkey("token", token)
|
||||
etcdclient.setkey("service/master", ipaddr)
|
||||
etcdclient.setkey("service/mode", mode)
|
||||
etcdclient.createdir("machines/allnodes")
|
||||
etcdclient.createdir("machines/runnodes")
|
||||
etcdclient.setkey("vcluster/nextid", "1")
|
||||
# clean all users vclusters files : FS_PREFIX/global/users/<username>/clusters/<clusterid>
|
||||
usersdir = fs_path+"/global/users/"
|
||||
for user in os.listdir(usersdir):
|
||||
shutil.rmtree(usersdir+user+"/clusters")
|
||||
shutil.rmtree(usersdir+user+"/hosts")
|
||||
os.mkdir(usersdir+user+"/clusters")
|
||||
os.mkdir(usersdir+user+"/hosts")
|
||||
else:
|
||||
# check whether cluster exists
|
||||
if not etcdclient.isdir("")[0]:
|
||||
logger.error ("cluster not exists, you should use mode:new ")
|
||||
sys.exit(1)
|
||||
# initialize the etcd table for recovery
|
||||
token = tools.gen_token()
|
||||
tokenfile = open(fs_path+"/global/token", 'w')
|
||||
tokenfile.write(token)
|
||||
tokenfile.write("\n")
|
||||
tokenfile.close()
|
||||
etcdclient.setkey("token", token)
|
||||
etcdclient.setkey("service/master", ipaddr)
|
||||
etcdclient.setkey("service/mode", mode)
|
||||
if etcdclient.isdir("_lock")[0]:
|
||||
etcdclient.deldir("_lock")
|
||||
if etcdclient.isdir("machines/runnodes")[0]:
|
||||
etcdclient.deldir("machines/runnodes")
|
||||
etcdclient.createdir("machines/runnodes")
|
||||
|
||||
G_usermgr = userManager.userManager('root')
|
||||
clusternet = env.getenv("CLUSTER_NET")
|
||||
logger.info("using CLUSTER_NET %s" % clusternet)
|
||||
|
||||
G_networkmgr = network.NetworkMgr(clusternet, etcdclient, mode)
|
||||
G_networkmgr.printpools()
|
||||
|
||||
# start NodeMgr and NodeMgr will wait for all nodes to start ...
|
||||
G_nodemgr = nodemgr.NodeMgr(G_networkmgr, etcdclient, addr = ipaddr, mode=mode)
|
||||
logger.info("nodemgr started")
|
||||
G_vclustermgr = vclustermgr.VclusterMgr(G_nodemgr, G_networkmgr, etcdclient, ipaddr, mode)
|
||||
logger.info("vclustermgr started")
|
||||
G_imagemgr = imagemgr.ImageMgr()
|
||||
logger.info("imagemgr started")
|
||||
Guest_control = guest_control.Guest(G_vclustermgr,G_nodemgr)
|
||||
logger.info("guest control started")
|
||||
threading.Thread(target=Guest_control.work, args=()).start()
|
||||
|
||||
logger.info("startting to listen on: ")
|
||||
masterip = env.getenv('MASTER_IP')
|
||||
logger.info("using MASTER_IP %s", masterip)
|
||||
|
||||
masterport = env.getenv('MASTER_PORT')
|
||||
logger.info("using MASTER_PORT %d", int(masterport))
|
||||
|
||||
# server = http.server.HTTPServer((masterip, masterport), DockletHttpHandler)
|
||||
server = ThreadingHttpServer((masterip, int(masterport)), DockletHttpHandler)
|
||||
logger.info("starting master server")
|
||||
server.serve_forever()
|
|
@ -13,7 +13,7 @@ import env
|
|||
# 2. update node list when new node joins
|
||||
# ETCD table :
|
||||
# machines/allnodes -- all nodes in docklet, for recovery
|
||||
# machines/runnodes -- run nodes of this start up
|
||||
# machines/runnodes -- run nodes of this start up
|
||||
##############################################
|
||||
class NodeMgr(object):
|
||||
def __init__(self, networkmgr, etcdclient, addr, mode):
|
||||
|
@ -53,13 +53,13 @@ class NodeMgr(object):
|
|||
if len(self.runnodes)>0:
|
||||
logger.error ("init runnodes is not null, need to be clean")
|
||||
sys.exit(1)
|
||||
# init rpc list
|
||||
# init rpc list
|
||||
self.rpcs = []
|
||||
# start new thread to watch whether a new node joins
|
||||
logger.info ("start thread to watch new nodes ...")
|
||||
self.thread_watchnewnode = threading.Thread(target=self._watchnewnode)
|
||||
self.thread_watchnewnode.start()
|
||||
# wait for all nodes joins
|
||||
# wait for all nodes joins
|
||||
while(True):
|
||||
allin = True
|
||||
for node in self.allnodes:
|
||||
|
@ -73,7 +73,7 @@ class NodeMgr(object):
|
|||
logger.info ("run nodes are: %s" % self.runnodes)
|
||||
|
||||
|
||||
# get nodes list from etcd table
|
||||
# get nodes list from etcd table
|
||||
def _nodelist_etcd(self, which):
|
||||
if which == "allnodes" or which == "runnodes":
|
||||
[status, nodeinfo]=self.etcd.listdir("machines/"+which)
|
||||
|
@ -91,7 +91,7 @@ class NodeMgr(object):
|
|||
time.sleep(0.1)
|
||||
[status, runlist] = self.etcd.listdir("machines/runnodes")
|
||||
if not status:
|
||||
logger.warning ("get runnodes list failed from etcd ")
|
||||
logger.warning ("get runnodes list failed from etcd ")
|
||||
continue
|
||||
for node in runlist:
|
||||
nodeip = node['key'].rsplit('/',1)[1]
|
||||
|
@ -109,8 +109,8 @@ class NodeMgr(object):
|
|||
if nodeip in self.allnodes:
|
||||
######## HERE MAYBE NEED TO FIX ###############
|
||||
# here we must use "machines/runnodes/nodeip"
|
||||
# we cannot use node['key'], node['key'] is absolute
|
||||
# path, etcd client will append the path to prefix,
|
||||
# we cannot use node['key'], node['key'] is absolute
|
||||
# path, etcd client will append the path to prefix,
|
||||
# which is wrong
|
||||
###############################################
|
||||
self.etcd.setkey("machines/runnodes/"+nodeip, "init-"+self.mode)
|
||||
|
@ -138,7 +138,7 @@ class NodeMgr(object):
|
|||
% (nodeip, workerport)))
|
||||
logger.info ("add %s:%s in rpc client list" %
|
||||
(nodeip, workerport))
|
||||
|
||||
|
||||
# get all run nodes' IP addr
|
||||
def get_nodeips(self):
|
||||
return self.allnodes
|
||||
|
|
|
@ -166,7 +166,7 @@ class userManager:
|
|||
quotas.append({'name':'vnode', 'hint':'how many containers the user can have, e.g. 8'})
|
||||
quotafile.write(json.dumps(quotas))
|
||||
quotafile.close()
|
||||
|
||||
|
||||
|
||||
def auth_local(self, username, password):
|
||||
password = hashlib.sha512(password.encode('utf-8')).hexdigest()
|
||||
|
@ -399,7 +399,7 @@ class userManager:
|
|||
"tel" : user.tel,
|
||||
"register_date" : "%s"%(user.register_date),
|
||||
"group" : user.user_group,
|
||||
"groupinfo": group,
|
||||
"groupinfo": group,
|
||||
},
|
||||
}
|
||||
return result
|
||||
|
@ -411,8 +411,8 @@ class userManager:
|
|||
Modify informantion for oneself
|
||||
'''
|
||||
form = kwargs['newValue']
|
||||
name = form.getvalue('name', None)
|
||||
value = form.getvalue('value', None)
|
||||
name = form.get('name', None)
|
||||
value = form.get('value', None)
|
||||
if (name == None or value == None):
|
||||
result = {'success': 'false'}
|
||||
return result
|
||||
|
@ -524,13 +524,13 @@ class userManager:
|
|||
groups = json.loads(groupfile.read())
|
||||
groupfile.close()
|
||||
for group in groups:
|
||||
if group['name'] == kwargs['newValue'].getvalue('groupname',None):
|
||||
if group['name'] == kwargs['newValue'].get('groupname',None):
|
||||
form = kwargs['newValue']
|
||||
for key in form.keys():
|
||||
if key == "groupname" or key == "token":
|
||||
pass
|
||||
else:
|
||||
group['quotas'][key] = form.getvalue(key)
|
||||
group['quotas'][key] = form.get(key)
|
||||
groupfile = open(fspath+"/global/sys/quota",'w')
|
||||
groupfile.write(json.dumps(groups))
|
||||
groupfile.close()
|
||||
|
@ -545,28 +545,28 @@ class userManager:
|
|||
will send an e-mail when status is changed from 'applying' to 'normal'
|
||||
Usage: modify(newValue = dict_from_form, cur_user = token_from_auth)
|
||||
'''
|
||||
user_modify = User.query.filter_by(username = kwargs['newValue'].getvalue('username', None)).first()
|
||||
user_modify = User.query.filter_by(username = kwargs['newValue'].get('username', None)).first()
|
||||
if (user_modify == None):
|
||||
|
||||
return {"success":'false', "reason":"User does not exist"}
|
||||
|
||||
#try:
|
||||
form = kwargs['newValue']
|
||||
user_modify.truename = form.getvalue('truename', '')
|
||||
user_modify.e_mail = form.getvalue('e_mail', '')
|
||||
user_modify.department = form.getvalue('department', '')
|
||||
user_modify.student_number = form.getvalue('student_number', '')
|
||||
user_modify.tel = form.getvalue('tel', '')
|
||||
user_modify.user_group = form.getvalue('group', '')
|
||||
user_modify.auth_method = form.getvalue('auth_method', '')
|
||||
if (user_modify.status == 'applying' and form.getvalue('status', '') == 'normal'):
|
||||
user_modify.truename = form.get('truename', '')
|
||||
user_modify.e_mail = form.get('e_mail', '')
|
||||
user_modify.department = form.get('department', '')
|
||||
user_modify.student_number = form.get('student_number', '')
|
||||
user_modify.tel = form.get('tel', '')
|
||||
user_modify.user_group = form.get('group', '')
|
||||
user_modify.auth_method = form.get('auth_method', '')
|
||||
if (user_modify.status == 'applying' and form.get('status', '') == 'normal'):
|
||||
send_activated_email(user_modify.e_mail, user_modify.username)
|
||||
user_modify.status = form.getvalue('status', '')
|
||||
if (form.getvalue('Chpassword', '') == 'Yes'):
|
||||
new_password = form.getvalue('password','no_password')
|
||||
user_modify.status = form.get('status', '')
|
||||
if (form.get('Chpassword', '') == 'Yes'):
|
||||
new_password = form.get('password','no_password')
|
||||
new_password = hashlib.sha512(new_password.encode('utf-8')).hexdigest()
|
||||
user_modify.password = new_password
|
||||
#self.chpassword(cur_user = user_modify, password = form.getvalue('password','no_password'))
|
||||
#self.chpassword(cur_user = user_modify, password = form.get('password','no_password'))
|
||||
|
||||
db.session.commit()
|
||||
return {"success":'true'}
|
||||
|
@ -621,9 +621,9 @@ class userManager:
|
|||
@administration_required
|
||||
def quotaadd(*args, **kwargs):
|
||||
form = kwargs.get('form')
|
||||
quotaname = form.getvalue("quotaname")
|
||||
default_value = form.getvalue("default_value")
|
||||
hint = form.getvalue("hint")
|
||||
quotaname = form.get("quotaname")
|
||||
default_value = form.get("default_value")
|
||||
hint = form.get("hint")
|
||||
if (quotaname == None):
|
||||
return { "success":'false', "reason": "Empty quota name"}
|
||||
if (default_value == None):
|
||||
|
@ -648,7 +648,7 @@ class userManager:
|
|||
@administration_required
|
||||
def groupadd(*args, **kwargs):
|
||||
form = kwargs.get('form')
|
||||
groupname = form.getvalue("groupname")
|
||||
groupname = form.get("groupname")
|
||||
if (groupname == None):
|
||||
return {"success":'false', "reason": "Empty group name"}
|
||||
groupfile = open(fspath+"/global/sys/quota",'r')
|
||||
|
@ -662,13 +662,13 @@ class userManager:
|
|||
if key == "groupname" or key == "token":
|
||||
pass
|
||||
else:
|
||||
group['quotas'][key] = form.getvalue(key)
|
||||
group['quotas'][key] = form.get(key)
|
||||
groups.append(group)
|
||||
groupfile = open(fspath+"/global/sys/quota",'w')
|
||||
groupfile.write(json.dumps(groups))
|
||||
groupfile.close()
|
||||
return {"success":'true'}
|
||||
|
||||
|
||||
@administration_required
|
||||
def groupdel(*args, **kwargs):
|
||||
name = kwargs.get('name', None)
|
||||
|
|
|
@ -17,13 +17,13 @@ class adminView(normalView):
|
|||
class groupaddView(normalView):
|
||||
@classmethod
|
||||
def post(self):
|
||||
dockletRequest.post('/user/groupadd', request.form)
|
||||
dockletRequest.post('/user/groupadd/', request.form)
|
||||
return redirect('/admin/')
|
||||
|
||||
class quotaaddView(normalView):
|
||||
@classmethod
|
||||
def post(self):
|
||||
dockletRequest.post('/user/quotaadd', request.form)
|
||||
dockletRequest.post('/user/quotaadd/', request.form)
|
||||
return redirect('/admin/')
|
||||
|
||||
class groupdelView(normalView):
|
||||
|
@ -32,9 +32,9 @@ class groupdelView(normalView):
|
|||
data = {
|
||||
"name" : self.groupname,
|
||||
}
|
||||
dockletRequest.post('/user/groupdel', data)
|
||||
dockletRequest.post('/user/groupdel/', data)
|
||||
return redirect('/admin/')
|
||||
|
||||
|
||||
@classmethod
|
||||
def get(self):
|
||||
return self.post()
|
||||
|
|
|
@ -72,7 +72,7 @@ class loginView(normalView):
|
|||
else:
|
||||
return redirect('/login/')
|
||||
else:
|
||||
self.error()
|
||||
return redirect('/login/')
|
||||
|
||||
class logoutView(normalView):
|
||||
|
||||
|
|
|
@ -40,7 +40,7 @@ class statusRealtimeView(normalView):
|
|||
data = {
|
||||
"user": session['username'],
|
||||
}
|
||||
result = dockletRequest.post('/monitor/vnodes/%s/basic_info'%(self.node_name), data)
|
||||
result = dockletRequest.post('/monitor/vnodes/%s/basic_info/'%(self.node_name), data)
|
||||
basic_info = result.get('monitor').get('basic_info')
|
||||
return self.render(self.template_path, node_name = self.node_name, user = session['username'], container = basic_info)
|
||||
|
||||
|
@ -53,11 +53,11 @@ class hostsRealtimeView(normalView):
|
|||
data = {
|
||||
"user": session['username'],
|
||||
}
|
||||
result = dockletRequest.post('/monitor/hosts/%s/cpuconfig'%(self.com_ip), data)
|
||||
result = dockletRequest.post('/monitor/hosts/%s/cpuconfig/'%(self.com_ip), data)
|
||||
proc = result.get('monitor').get('cpuconfig')
|
||||
result = dockletRequest.post('/monitor/hosts/%s/osinfo'%(self.com_ip), data)
|
||||
result = dockletRequest.post('/monitor/hosts/%s/osinfo/'%(self.com_ip), data)
|
||||
osinfo = result.get('monitor').get('osinfo')
|
||||
result = dockletRequest.post('/monitor/hosts/%s/diskinfo'%(self.com_ip), data)
|
||||
result = dockletRequest.post('/monitor/hosts/%s/diskinfo/'%(self.com_ip), data)
|
||||
diskinfos = result.get('monitor').get('diskinfo')
|
||||
|
||||
return self.render(self.template_path, com_ip = self.com_ip, user = session['username'],processors = proc, OSinfo = osinfo, diskinfos = diskinfos)
|
||||
|
@ -71,13 +71,13 @@ class hostsConAllView(normalView):
|
|||
data = {
|
||||
"user": session['username'],
|
||||
}
|
||||
result = dockletRequest.post('/monitor/hosts/%s/containerslist'%(self.com_ip), data)
|
||||
result = dockletRequest.post('/monitor/hosts/%s/containerslist/'%(self.com_ip), data)
|
||||
containers = result.get('monitor').get('containerslist')
|
||||
containerslist = []
|
||||
for container in containers:
|
||||
result = dockletRequest.post('/monitor/vnodes/%s/basic_info'%(container), data)
|
||||
result = dockletRequest.post('/monitor/vnodes/%s/basic_info/'%(container), data)
|
||||
basic_info = result.get('monitor').get('basic_info')
|
||||
result = dockletRequest.post('/monitor/vnodes/%s/owner'%(container), data)
|
||||
result = dockletRequest.post('/monitor/vnodes/%s/owner/'%(container), data)
|
||||
owner = result.get('monitor')
|
||||
basic_info['owner'] = owner
|
||||
containerslist.append(basic_info)
|
||||
|
@ -91,14 +91,14 @@ class hostsView(normalView):
|
|||
data = {
|
||||
"user": session['username'],
|
||||
}
|
||||
result = dockletRequest.post('/monitor/listphynodes', data)
|
||||
result = dockletRequest.post('/monitor/listphynodes/', data)
|
||||
iplist = result.get('monitor').get('allnodes')
|
||||
machines = []
|
||||
for ip in iplist:
|
||||
containers = {}
|
||||
result = dockletRequest.post('/monitor/hosts/%s/containers'%(ip), data)
|
||||
result = dockletRequest.post('/monitor/hosts/%s/containers/'%(ip), data)
|
||||
containers = result.get('monitor').get('containers')
|
||||
result = dockletRequest.post('/monitor/hosts/%s/status'%(ip), data)
|
||||
result = dockletRequest.post('/monitor/hosts/%s/status/'%(ip), data)
|
||||
status = result.get('monitor').get('status')
|
||||
machines.append({'ip':ip,'containers':containers, 'status':status})
|
||||
#print(machines)
|
||||
|
@ -112,9 +112,9 @@ class monitorUserAllView(normalView):
|
|||
data = {
|
||||
"user": session['username'],
|
||||
}
|
||||
result = dockletRequest.post('/monitor/listphynodes', data)
|
||||
result = dockletRequest.post('/monitor/listphynodes/', data)
|
||||
userslist = [{'name':'root'},{'name':'libao'}]
|
||||
for user in userslist:
|
||||
result = dockletRequest.post('/monitor/user/%s/clustercnt'%(user['name']), data)
|
||||
result = dockletRequest.post('/monitor/user/%s/clustercnt/'%(user['name']), data)
|
||||
user['clustercnt'] = result.get('monitor').get('clustercnt')
|
||||
return self.render(self.template_path, userslist = userslist, user = session['username'])
|
||||
|
|
|
@ -16,5 +16,5 @@ class userActivateView(normalView):
|
|||
|
||||
@classmethod
|
||||
def post(self):
|
||||
dockletRequest.post('/register', request.form)
|
||||
dockletRequest.post('/register/', request.form)
|
||||
return redirect('/logout/')
|
||||
|
|
|
@ -19,34 +19,32 @@ class userlistView(normalView):
|
|||
class useraddView(normalView):
|
||||
@classmethod
|
||||
def post(self):
|
||||
dockletRequest.post('/user/add', request.form)
|
||||
dockletRequest.post('/user/add/', request.form)
|
||||
return redirect('/user/list/')
|
||||
|
||||
class userdataView(normalView):
|
||||
@classmethod
|
||||
def get(self):
|
||||
return json.dumps(dockletRequest.post('/user/data', request.form))
|
||||
return json.dumps(dockletRequest.post('/user/data/', request.form))
|
||||
|
||||
@classmethod
|
||||
def post(self):
|
||||
return json.dumps(dockletRequest.post('/user/data', request.form))
|
||||
return json.dumps(dockletRequest.post('/user/data/', request.form))
|
||||
|
||||
class userqueryView(normalView):
|
||||
@classmethod
|
||||
def get(self):
|
||||
return json.dumps(dockletRequest.post('/user/query', request.form))
|
||||
return json.dumps(dockletRequest.post('/user/query/', request.form))
|
||||
|
||||
@classmethod
|
||||
def post(self):
|
||||
return json.dumps(dockletRequest.post('/user/query', request.form))
|
||||
return json.dumps(dockletRequest.post('/user/query/', request.form))
|
||||
|
||||
class usermodifyView(normalView):
|
||||
@classmethod
|
||||
def post(self):
|
||||
try:
|
||||
dockletRequest.post('/user/modify', request.form)
|
||||
dockletRequest.post('/user/modify/', request.form)
|
||||
except:
|
||||
return self.render('user/mailservererror.html')
|
||||
return redirect('/user/list/')
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue