From 1a8eef98678a72a13cb7fba67bc252bc0aa873e5 Mon Sep 17 00:00:00 2001 From: zhongyehong Date: Thu, 17 Nov 2016 14:22:40 +0800 Subject: [PATCH] change the user module to an independent http server. --- bin/docklet-master | 28 +- src/httprest.py | 456 ++++++++------------------------ src/model.py | 1 + src/monitor.py | 74 ++++++ src/userManager.py | 18 +- user/user.py | 462 +++++++++++++++++++++++++++++++++ web/webViews/dockletrequest.py | 17 +- 7 files changed, 695 insertions(+), 361 deletions(-) create mode 100755 user/user.py diff --git a/bin/docklet-master b/bin/docklet-master index 749723f..c7d493c 100755 --- a/bin/docklet-master +++ b/bin/docklet-master @@ -13,6 +13,7 @@ LXC_SCRIPT=$DOCKLET_CONF/lxc-script DOCKLET_SRC=$DOCKLET_HOME/src DOCKLET_LIB=$DOCKLET_SRC DOCKLET_WEB=$DOCKLET_HOME/web +DOCKLET_USER=$DOCKLET_HOME/user # default working directory, default to /opt/docklet FS_PREFIX=/opt/docklet @@ -32,6 +33,7 @@ ETCD=localhost:2379 CLUSTER_NAME=docklet-vc #web port, default is 8888 WEB_PORT=8888 +USER_PORT=9100 #cluster net, default is 172.16.0.1/16 CLUSTER_NET="172.16.0.1/16" @@ -61,6 +63,12 @@ DAEMON_NAME_WEB=docklet-web PIDFILE_WEB=$RUN_DIR/docklet-web.pid DAEMON_OPTS_WEB= +# settings for docklet user +DAEMON_USER_MODULE=$DOCKLET_USER/user.py +DAEMON_NAME_USER=docklet-user +PIDFILE_USER=$RUN_DIR/docklet-user.pid +DAEMON_OPTS_USER= + RUNNING_CONFIG=$FS_PREFIX/local/docklet-running.conf export CONFIG=$RUNNING_CONFIG @@ -121,7 +129,7 @@ do_start_proxy () { } pre_start_web () { - log_daemon_msg "Starting $DAEMON_NAME_WEB in $FS_PREFIX" + log_daemon_msg "Starting $DAEMON_NAME_WEB in $FS_PREFIX" webip=$(ip addr show $NETWORK_DEVICE | grep -oE "[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/[0-9]+") @@ -144,6 +152,14 @@ do_start_web () { log_end_msg $? } +do_start_user () { + log_daemon_msg "Starting $DAEMON_NAME_USER in $FS_PREFIX" + + DAEMON_OPTS_USER="-p $USER_PORT" + + start-stop-daemon --start --background --pidfile $PIDFILE_USER --make-pidfile --user $DAEMON_USER --chuid $DAEMON_USER --startas $DAEMON_USER_MODULE -- $DAEMON_OPTS_USER + log_end_msg $? +} do_stop_master () { log_daemon_msg "Stopping $DAEMON_NAME_MASTER daemon" @@ -164,22 +180,31 @@ do_stop_web () { log_end_msg $? } +do_stop_user () { + log_daemon_msg "Stopping $DAEMON_NAME_USER daemon" + start-stop-daemon --stop --quiet --oknodo --remove-pidfile --pidfile $PIDFILE_USER --retry 10 + log_end_msg $? +} + case "$1" in init) do_start_master "new" do_start_proxy do_start_web + do_start_user ;; start) do_start_master "recovery" do_start_proxy do_start_web + do_start_user ;; stop) do_stop_web do_stop_proxy do_stop_master + do_stop_user ;; restart) @@ -221,6 +246,7 @@ case "$1" in status_of_proc -p $PIDFILE_MASTER "$DAEMON_MASTER" "$DAEMON_NAME_MASTER" || status=$? status_of_proc -p $PIDFILE_PROXY "$DAEMON_PROXY" "$DAEMON_NAME_PROXY" || status=$? status_of_proc -p $PIDFILE_WEB "$DAEMON_WEB" "$DAEMON_NAME_WEB" || status=$? + status_of_proc -p $PIDFILE_USER "$DAEMON_USER" "$DAEMON_NAME_USER" || status=$? exit $status ;; diff --git a/src/httprest.py b/src/httprest.py index c3ea6b4..ba7d38c 100755 --- a/src/httprest.py +++ b/src/httprest.py @@ -27,12 +27,19 @@ import userManager import monitor,traceback import threading import sysmgr +import requests #default EXTERNAL_LOGIN=False external_login = env.getenv('EXTERNAL_LOGIN') if (external_login == 'TRUE'): from userDependence import external_auth +#userpoint = env.getenv('USER_IP') + ":" + env.getenv('USER_PORT') +userpoint = "http://0.0.0.0:9100" + +def post_to_user(url = '/', data={}): + return requests.post(userpoint+url,data=data).json() + app = Flask(__name__) from functools import wraps @@ -41,95 +48,38 @@ from functools import wraps def login_required(func): @wraps(func) def wrapper(*args, **kwargs): - global G_usermgr logger.info ("get request, path: %s" % request.path) 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'}) - return func(cur_user, cur_user.username, request.form, *args, **kwargs) + result = post_to_user("/authtoken/", {'token':token}) + if result.get('success') == 'true': + username = result.get('username') + beans = result.get('beans') + else: + return result + #if (cur_user == None): + # return json.dumps({'success':'false', 'message':'token failed or expired', 'Unauthorized': 'True'}) + return func(username, beans, 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 = request.form.get('email','') - newuser.student_number = request.form.get('studentnumber', '') - newuser.department = request.form.get('department', '') - newuser.truename = request.form.get('truename', '') - newuser.tel = request.form.get('tel', '') - newuser.description = request.form.get('description', '') - result = G_usermgr.register(user = newuser) - userManager.send_remind_activating_email(newuser.username) - return json.dumps(result) +def beans_check(func): + @wraps(func) + def wrapper(*args, **kwargs): + beans = args[1] + if beans <= 0: + return json.dumps({'success':'false','message':'user\'s beans are less than or equal to zero!'}) + else: + return func(*args, **kwargs) + return wrapper @app.route("/cluster/create/", methods=['POST']) @login_required -def create_cluster(cur_user, user, form): - global G_usermgr +@beans_check +def create_cluster(user, beans, form): global G_vclustermgr clustername = form.get('clustername', None) if (clustername == None): @@ -138,7 +88,7 @@ def create_cluster(cur_user, user, form): 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 = post_to_user("/user/selfQuery/", {'token':form.get("token")}) user_info = json.dumps(user_info) logger.info ("handle request : create cluster %s with image %s " % (clustername, image['name'])) setting = { @@ -146,20 +96,22 @@ def create_cluster(cur_user, user, form): 'memory': form.get('memorySetting'), 'disk': form.get('diskSetting') } - [status, result] = G_usermgr.usageInc(cur_user = cur_user, modification = setting) + res = post_to_user("/user/usageInc/", {'token':form.get('token'), 'setting':json.dumps(setting)}) + status = res.get('success') + result = res.get('result') if not status: return json.dumps({'success':'false', 'action':'create cluster', 'message':result}) [status, result] = G_vclustermgr.create_cluster(clustername, user, image, user_info, setting) if status: return json.dumps({'success':'true', 'action':'create cluster', 'message':result}) else: - G_usermgr.usageRecover(cur_user = cur_user, modification = setting) + post_to_user("/user/usageRecover/", {'token':form.get('token'), 'setting':json.dumps(setting)}) 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 +@beans_check +def scaleout_cluster(user, beans, form): global G_vclustermgr clustername = form.get('clustername', None) if (clustername == None): @@ -169,33 +121,35 @@ def scaleout_cluster(cur_user, user, form): 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 = post_to_user("/user/selfQuery/", {'token':form.get("token")}) user_info = json.dumps(user_info) setting = { 'cpu': form.get('cpuSetting'), 'memory': form.get('memorySetting'), 'disk': form.get('diskSetting') } - [status, result] = G_usermgr.usageInc(cur_user = cur_user, modification = setting) + res = post_to_user("/user/usageInc/", {'token':form.get('token'), 'setting':json.dumps(setting)}) + status = res.get('success') + result = res.get('result') if not status: return json.dumps({'success':'false', 'action':'scale out', 'message': result}) [status, result] = G_vclustermgr.scale_out_cluster(clustername, user, image, user_info, setting) if status: return json.dumps({'success':'true', 'action':'scale out', 'message':result}) else: - G_usermgr.usageRecover(cur_user = cur_user, modification = setting) + post_to_user("/user/usageRecover/", {'token':form.get('token'), 'setting':json.dumps(setting)}) 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): +def scalein_cluster(user, beans, 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) - G_usermgr.usageRelease(cur_user = cur_user, clustername = clustername, containername = containername, allcontainer = False) + post_to_user("/user/usageRelease/", {'token':form.get('token'), 'clustername':clustername, 'containername':containername, 'allcontainer':False}) [status, result] = G_vclustermgr.scale_in_cluster(clustername, user, containername) if status: return json.dumps({'success':'true', 'action':'scale in', 'message':result}) @@ -204,7 +158,8 @@ def scalein_cluster(cur_user, user, form): @app.route("/cluster/start/", methods=['POST']) @login_required -def start_cluster(cur_user, user, form): +@beans_check +def start_cluster(user, beans, form): global G_vclustermgr clustername = form.get('clustername', None) if (clustername == None): @@ -218,7 +173,7 @@ def start_cluster(cur_user, user, form): @app.route("/cluster/stop/", methods=['POST']) @login_required -def stop_cluster(cur_user, user, form): +def stop_cluster(user, beans, form): global G_vclustermgr clustername = form.get('clustername', None) if (clustername == None): @@ -232,16 +187,15 @@ def stop_cluster(cur_user, user, form): @app.route("/cluster/delete/", methods=['POST']) @login_required -def delete_cluster(cur_user, user, form): +def delete_cluster(user, beans, form): global G_vclustermgr - global G_usermgr 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) - user_info = G_usermgr.selfQuery(cur_user=cur_user) + user_info = post_to_user("/user/selfQuery/" , {'token':form.get("token")}) user_info = json.dumps(user_info) - G_usermgr.usageRelease(cur_user = cur_user, clustername = clustername, containername = "all", allcontainer = True) + post_to_user("/user/usageRelease/", {'token':form.get('token'), 'clustername':clustername, 'containername':'all', 'allcontainer':True}) [status, result] = G_vclustermgr.delete_cluster(clustername, user, user_info) if status: return json.dumps({'success':'true', 'action':'delete cluster', 'message':result}) @@ -250,7 +204,7 @@ def delete_cluster(cur_user, user, form): @app.route("/cluster/info/", methods=['POST']) @login_required -def info_cluster(cur_user, user, form): +def info_cluster(user, beans, form): global G_vclustermgr clustername = form.get('clustername', None) if (clustername == None): @@ -264,7 +218,7 @@ def info_cluster(cur_user, user, form): @app.route("/cluster/list/", methods=['POST']) @login_required -def list_cluster(cur_user, user, form): +def list_cluster(user, beans, form): global G_vclustermgr logger.info ("handle request : list clusters for %s" % user) [status, clusterlist] = G_vclustermgr.list_clusters(user) @@ -273,9 +227,22 @@ def list_cluster(cur_user, user, form): else: return json.dumps({'success':'false', 'action':'list cluster', 'message':clusterlist}) +@app.route("/cluster/stopall/",methods=['POST']) +@login_required +def stopall_cluster(user, beans, form): + global G_vclustermgr + logger.info ("handle request : stop all clusters for %s" % user) + [status, clusterlist] = G_vclustermgr.list_clusters(user) + if status: + for cluster in clusterlist: + G_vclustermgr.stop_cluster(cluster,user) + return json.dumps({'success':'true', 'action':'stop all cluster'}) + else: + return json.dumps({'success':'false', 'action':'stop all cluster', 'message':clusterlist}) + @app.route("/cluster/flush/", methods=['POST']) @login_required -def flush_cluster(cur_user, user, form): +def flush_cluster(user, beans, form): global G_vclustermgr clustername = form.get('clustername', None) if (clustername == None): @@ -286,9 +253,8 @@ def flush_cluster(cur_user, user, form): @app.route("/cluster/save/", methods=['POST']) @login_required -def save_cluster(cur_user, user, form): +def save_cluster(user, beans, form): global G_vclustermgr - global G_usermgr clustername = form.get('clustername', None) if (clustername == None): return json.dumps({'success':'false', 'message':'clustername is null'}) @@ -302,7 +268,7 @@ def save_cluster(cur_user, user, form): if not status: return json.dumps({'success':'false','reason':'exists', 'message':message}) - user_info = G_usermgr.selfQuery(cur_user = cur_user) + user_info = post_to_user("/user/selfQuery/", {'token':form.get("token")}) [status,message] = G_vclustermgr.create_image(user,clustername,containername,imagename,description,user_info["data"]["groupinfo"]["image"]) if status: logger.info("image has been saved") @@ -314,14 +280,22 @@ def save_cluster(cur_user, user, form): @app.route("/image/list/", methods=['POST']) @login_required -def list_image(cur_user, user, form): +def list_image(user, beans, form): global G_imagemgr images = G_imagemgr.list_images(user) return json.dumps({'success':'true', 'images': images}) +@app.route("/image/updatebase/", methods=['POST']) +@login_required +def update_base(user, beans, form): + global G_imagemgr + global G_vclustermgr + [success, status] = G_imagemgr.update_base_image(user, G_vclustermgr, form.get('image')) + return json.dumps({'success':'true', 'message':status}) + @app.route("/image/description/", methods=['POST']) @login_required -def description_image(cur_user, user, form): +def description_image(user, beans, form): global G_imagemgr image = {} image['name'] = form.get("imagename", None) @@ -332,7 +306,7 @@ def description_image(cur_user, user, form): @app.route("/image/share/", methods=['POST']) @login_required -def share_image(cur_user, user, form): +def share_image(user, beans, form): global G_imagemgr image = form.get('image') G_imagemgr.shareImage(user,image) @@ -340,7 +314,7 @@ def share_image(cur_user, user, form): @app.route("/image/unshare/", methods=['POST']) @login_required -def unshare_image(cur_user, user, form): +def unshare_image(user, beans, form): global G_imagemgr image = form.get('image', None) G_imagemgr.unshareImage(user,image) @@ -348,7 +322,7 @@ def unshare_image(cur_user, user, form): @app.route("/image/delete/", methods=['POST']) @login_required -def delete_image(cur_user, user, form): +def delete_image(user, beans, form): global G_imagemgr image = form.get('image', None) G_imagemgr.removeImage(user,image) @@ -356,7 +330,7 @@ def delete_image(cur_user, user, form): @app.route("/addproxy/", methods=['POST']) @login_required -def addproxy(cur_user, user, form): +def addproxy(user, beans, form): global G_vclustermgr logger.info ("handle request : add proxy") proxy_ip = form.get("ip", None) @@ -370,7 +344,7 @@ def addproxy(cur_user, user, form): @app.route("/deleteproxy/", methods=['POST']) @login_required -def deleteproxy(cur_user, user, form): +def deleteproxy(user, beans, form): global G_vclustermgr logger.info ("handle request : delete proxy") clustername = form.get("clustername", None) @@ -379,7 +353,7 @@ def deleteproxy(cur_user, user, form): @app.route("/monitor/hosts///", methods=['POST']) @login_required -def hosts_monitor(cur_user, user, form, com_id, issue): +def hosts_monitor(user, beans, form, com_id, issue): global G_clustername logger.info("handle request: monitor/hosts") @@ -421,7 +395,7 @@ def hosts_monitor(cur_user, user, form, com_id, issue): @app.route("/monitor/vnodes///", methods=['POST']) @login_required -def vnodes_monitor(cur_user, user, form, con_id, issue): +def vnodes_monitor(user, beans, form, con_id, issue): global G_clustername logger.info("handle request: monitor/vnodes") res = {} @@ -436,7 +410,7 @@ def vnodes_monitor(cur_user, user, form, con_id, issue): res['basic_info'] = fetcher.get_basic_info() elif issue == 'owner': names = con_id.split('-') - result = G_usermgr.query(username = names[0], cur_user = cur_user) + result = post_to_user("/user/query/", data = {"token": form.get(token)}) if result['success'] == 'false': res['username'] = "" res['truename'] = "" @@ -450,246 +424,32 @@ def vnodes_monitor(cur_user, user, form, con_id, issue): @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}) +def user_quotainfo_monitor(user, beans, form, issue): + global G_historymgr + if issue == 'quotainfo': + logger.info("handle request: monitor/user/quotainfo/") + user_info = post_to_user("/user/selfQuery/", {'token':form.get("token")}) + quotainfo = user_info['data']['groupinfo'] + return json.dumps({'success':'true', 'quotainfo':quotainfo}) + elif issue == 'createdvnodes': + logger.info("handle request: monitor/user/createdvnodes/") + res = G_historymgr.getCreatedVNodes(user) + return json.dumps({'success':'true', 'createdvnodes':res}) + else: + return json.dumps({'success':'false', 'message':"Unspported Method!"}) @app.route("/monitor/listphynodes/", methods=['POST']) @login_required -def listphynodes_monitor(cur_user, user, form): +def listphynodes_monitor(user, beans, 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/chdefault/", methods=['POST']) -@login_required -def chdefault(cur_user, user, form): - global G_usermgr - logger.info("handle request: user/chdefault/") - result = G_usermgr.change_default_group(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) - - -@app.route("/user/groupNameList/", methods=['POST']) -@login_required -def groupNameList_user(cur_user, user, form): - global G_usermgr - logger.info("handle request: user/groupNameList/") - result = G_usermgr.groupListName(cur_user = cur_user) - return json.dumps(result) - - -@app.route("/user/groupList/", methods=['POST']) -@login_required -def groupList_user(cur_user, user, form): - global G_usermgr - logger.info("handle request: user/groupList/") - result = G_usermgr.groupList(cur_user = cur_user) - return json.dumps(result) - - -@app.route("/user/groupQuery/", methods=['POST']) -@login_required -def groupQuery_user(cur_user, user, form): - global G_usermgr - logger.info("handle request: user/groupQuery/") - result = G_usermgr.groupQuery(name = form.get("name"), cur_user = cur_user) - return json.dumps(result) - - -@app.route("/user/selfQuery/", methods=['POST']) -@login_required -def selfQuery_user(cur_user, user, form): - global G_usermgr - logger.info("handle request: user/selfQuery/") - result = G_usermgr.selfQuery(cur_user = cur_user) - return json.dumps(result) - - -@app.route("/user/selfModify/", methods=['POST']) -@login_required -def selfModify_user(cur_user, user, form): - global G_usermgr - logger.info("handle request: user/selfModify/") - result = G_usermgr.selfModify(cur_user = cur_user, newValue = form) - return json.dumps(result) - -@app.route("/user/usageQuery/" , methods=['POST']) -@login_required -def usageQuery_user(cur_user, user, form): - global G_usermgr - logger.info("handle request: user/usageQuery/") - result = G_usermgr.usageQuery(cur_user = cur_user) - return json.dumps(result) - -@app.route("/user/lxcsettingList/", methods=['POST']) -@login_required -def lxcsettingList_user(cur_user, user, form): - global G_usermgr - logger.info("handle request: user/lxcsettingList/") - result = G_usermgr.lxcsettingList(cur_user = cur_user, form = form) - return json.dumps(result) - -@app.route("/user/chlxcsetting/", methods=['POST']) -@login_required -def chlxcsetting_user(cur_user, user, form): - global G_usermgr - logger.info("handle request: user/chlxcsetting/") - result = G_usermgr.chlxcsetting(cur_user = cur_user, form = form) - return json.dumps(result) - -@app.route("/notification/list/", methods=['POST']) -@login_required -def list_notifications(cur_user, user, form): - global G_notificationmgr - logger.info("handle request: notification/list/") - result = G_notificationmgr.list_notifications(cur_user=cur_user, form=form) - return json.dumps(result) - - -@app.route("/notification/create/", methods=['POST']) -@login_required -def create_notification(cur_user, user, form): - global G_notificationmgr - logger.info("handle request: notification/create/") - result = G_notificationmgr.create_notification(cur_user=cur_user, form=form) - return json.dumps(result) - - -@app.route("/notification/modify/", methods=['POST']) -@login_required -def modify_notification(cur_user, user, form): - global G_notificationmgr - logger.info("handle request: notification/modify/") - result = G_notificationmgr.modify_notification(cur_user=cur_user, form=form) - return json.dumps(result) - - -@app.route("/notification/delete/", methods=['POST']) -@login_required -def delete_notification(cur_user, user, form): - global G_notificationmgr - logger.info("handle request: notification/delete/") - result = G_notificationmgr.delete_notification(cur_user=cur_user, form=form) - return json.dumps(result) - - -@app.route("/notification/query_self/", methods=['POST']) -@login_required -def query_self_notification_simple_infos(cur_user, user, form): - global G_notificationmgr - logger.info("handle request: notification/query_self/") - result = G_notificationmgr.query_self_notification_simple_infos(cur_user=cur_user, form=form) - return json.dumps(result) - - -@app.route("/notification/query/", methods=['POST']) -@login_required -def query_notification(cur_user, user, form): - global G_notificationmgr - logger.info("handle request: notification/query/") - result = G_notificationmgr.query_notification(cur_user=cur_user, form=form) - return json.dumps(result) - - -@app.route("/notification/query/all/", methods=['POST']) -@login_required -def query_self_notifications_infos(cur_user, user, form): - global G_notificationmgr - logger.info("handle request: notification/query/all/") - result = G_notificationmgr.query_self_notifications_infos(cur_user=cur_user, form=form) - return json.dumps(result) - - @app.route("/system/parmList/", methods=['POST']) @login_required -def parmList_system(cur_user, user, form): +def parmList_system(user, beans, form): global G_sysmgr logger.info("handle request: system/parmList/") result = G_sysmgr.getParmList() @@ -697,7 +457,7 @@ def parmList_system(cur_user, user, form): @app.route("/system/modify/", methods=['POST']) @login_required -def modify_system(cur_user, user, form): +def modify_system(user, beans, form): global G_sysmgr logger.info("handle request: system/modify/") field = form.get("field", None) @@ -712,7 +472,7 @@ def modify_system(cur_user, user, form): @app.route("/system/clear_history/", methods=['POST']) @login_required -def clear_system(cur_user, user, form): +def clear_system(user, beans, form): global G_sysmgr logger.info("handle request: system/clear_history/") field = form.get("field", None) @@ -726,7 +486,7 @@ def clear_system(cur_user, user, form): @app.route("/system/add/", methods=['POST']) @login_required -def add_system(cur_user, user, form): +def add_system(user, beans, form): global G_sysmgr logger.info("handle request: system/add/") field = form.get("field", None) @@ -741,7 +501,7 @@ def add_system(cur_user, user, form): @app.route("/system/delete/", methods=['POST']) @login_required -def delete_system(cur_user, user, form): +def delete_system(user, beans, form): global G_sysmgr logger.info("handle request: system/delete/") field = form.get("field", None) @@ -755,7 +515,7 @@ def delete_system(cur_user, user, form): @app.route("/system/reset_all/", methods=['POST']) @login_required -def resetall_system(cur_user, user, form): +def resetall_system(user, beans, form): global G_sysmgr logger.info("handle request: system/reset_all/") field = form.get("field", None) @@ -795,7 +555,6 @@ if __name__ == '__main__': global G_nodemgr global G_vclustermgr - global G_usermgr global G_notificationmgr global etcdclient global G_networkmgr @@ -876,10 +635,6 @@ if __name__ == '__main__': if etcdclient.isdir("_lock")[0]: etcdclient.deldir("_lock") - G_usermgr = userManager.userManager('root') - if mode == "new": - G_usermgr.initUsage() - G_notificationmgr = notificationmgr.NotificationMgr() clusternet = env.getenv("CLUSTER_NET") logger.info("using CLUSTER_NET %s" % clusternet) @@ -906,6 +661,11 @@ if __name__ == '__main__': masterport = env.getenv('MASTER_PORT') logger.info("using MASTER_PORT %d", int(masterport)) + G_historymgr = monitor.History_Manager() + master_collector = monitor.Master_Collector(G_nodemgr,ipaddr+":"+str(masterport)) + master_collector.start() + logger.info("master_collector started") + # server = http.server.HTTPServer((masterip, masterport), DockletHttpHandler) logger.info("starting master server") diff --git a/src/model.py b/src/model.py index 3cf0c0f..e986e77 100755 --- a/src/model.py +++ b/src/model.py @@ -91,6 +91,7 @@ class User(db.Model): self.department = department self.truename = truename self.tel = tel + self.beans = 10000 if (date != None): self.register_date = date else: diff --git a/src/monitor.py b/src/monitor.py index f08a828..72a97f1 100755 --- a/src/monitor.py +++ b/src/monitor.py @@ -4,6 +4,9 @@ import subprocess,re,os,etcdlib,psutil,math,sys import time,threading,json,traceback,platform from log import logger +from httplib2 import Http +from urllib.parse import urlencode +from httprest import post_to_user monitor_hosts = {} monitor_vnodes = {} @@ -49,6 +52,77 @@ class Container_Collector(threading.Thread): minutes = int(parts[0]) seconds = int(parts[1]) return ((days * 24 + hours) * 60 + minutes) * 60 + seconds + + @classmethod + def billing_increment(cls,vnode_name): + global increment + global workercinfo + global G_masterip + cpu_val = '0' + if vnode_name not in workercinfo.keys(): + return + if 'cpu_use' in workercinfo[vnode_name].keys(): + cpu_val = workercinfo[vnode_name]['cpu_use']['val'] + if vnode_name not in increment.keys(): + increment[vnode_name] = {} + increment[vnode_name]['lastcputime'] = cpu_val + increment[vnode_name]['memincrement'] = 0 + cpu_increment = float(cpu_val) - float(increment[vnode_name]['lastcputime']) + #logger.info("billing:"+str(cpu_increment)+" "+str(increment[container_name]['lastcputime'])) + if cpu_increment == 0.0: + avemem = 0 + else: + avemem = cpu_increment*float(increment[vnode_name]['memincrement'])/1800.0 + increment[vnode_name]['lastcputime'] = cpu_val + increment[vnode_name]['memincrement'] = 0 + if 'disk_use' in workercinfo[vnode_name].keys(): + disk_quota = workercinfo[vnode_name]['disk_use']['total'] + else: + disk_quota = 0 + #logger.info("cpu_increment:"+str(cpu_increment)+" avemem:"+str(avemem)+" disk:"+str(disk_quota)+"\n") + billingval = cpu_increment/1000.0 + avemem/500000.0 + float(disk_quota)/1024.0/1024.0/2000 + if 'basic_info' not in workercinfo[vnode_name].keys(): + workercinfo[vnode_name]['basic_info'] = {} + workercinfo[vnode_name]['basic_info']['billing'] = 0 + workercinfo[vnode_name]['basic_info']['RunningTime'] = 0 + nowbillingval = workercinfo[vnode_name]['basic_info']['billing'] + nowbillingval += math.ceil(billingval) + try: + vnode = VNode.query.get(vnode_name) + vnode.billing = nowbillingval + db.session.commit() + except Exception as err: + vnode = VNode(vnode_name) + vnode.billing = nowbillingval + db.session.add(vnode) + db.session.commit() + logger.warning(err) + workercinfo[vnode_name]['basic_info']['billing'] = nowbillingval + owner_name = get_owner(vnode_name) + owner = User.query.filter_by(username=owner_name).first() + #result = post_to_user("/user/billing",{'username':owner_name, 'beans': math.ceil(billingval)}) + #if not result.get('success'): + # logger.error(result.get('message')) + #else: + # beans = result.get('restbeans') + # if beans <= 0: + # logger.info("The beans of User(" + str(owner_name) + ") are less than or equal to zero, the container("+vnode_name+") will be stopped.") + # post_to_user() + if owner is None: + logger.warning("Error!!! Billing User %s doesn't exist!" % (owner_name)) + else: + #logger.info("Billing User:"+str(owner)) + owner.beans -= math.ceil(billingval) + db.session.commit() + #logger.info("Billing User:"+str(owner)) + if owner.beans <= 0: + logger.info("The beans of User(" + str(owner) + ") are less than or equal to zero, the container("+vnode_name+") will be stopped.") + token = owner.generate_auth_token() + form = {'token':token} + header = {'Content-Type':'application/x-www-form-urlencoded'} + http = Http() + [resp,content] = http.request("http://"+G_masterip+"/cluster/stopall/","POST",urlencode(form),headers = header) + logger.info("response from master:"+content.decode('utf-8')) def collect_containerinfo(self,container_name): global workerinfo diff --git a/src/userManager.py b/src/userManager.py index d58a68b..2f1d742 100755 --- a/src/userManager.py +++ b/src/userManager.py @@ -522,25 +522,25 @@ class userManager: db.session.commit() usage = UserUsage.query.filter_by(username = cur_user.username).first() if int(modification['cpu']) <= 0 or int(modification['memory']) <= 0 or int(modification['disk']) <= 0: - return [False, "cpu,memory and disk setting cannot less than zero"] + return {'success':False, 'result':"cpu,memory and disk setting cannot less than zero"} cpu = int(usage.cpu) + int(modification['cpu']) memory = int(usage.memory) + int(modification['memory']) disk = int(usage.disk) + int(modification['disk']) if cpu > int(groupinfo['cpu']): logger.error("cpu quota exceed, user:%s" % cur_user.username) - return [False, "cpu quota exceed"] + return {'success':False, 'result':"cpu quota exceed"} if memory > int(groupinfo['memory']): logger.error("memory quota exceed, user:%s" % cur_user.username) - return [False, "memory quota exceed"] + return {'success':False, 'result':"memory quota exceed"} if disk > int(groupinfo['disk']): logger.error("disk quota exceed, user:%s" % cur_user.username) - return [False, "disk quota exceed"] + return {'success':False, 'result':"disk quota exceed"} usage.cpu = str(cpu) usage.memory = str(memory) usage.disk = str(disk) db.session.commit() - return [True, "distribute the resource"] - + return {'success':True, 'result':"distribute the resource"} + @token_required def usageRecover(self, *args, **kwargs): ''' @@ -570,7 +570,7 @@ class userManager: usage.memory = str(memory) usage.disk = str(disk) db.session.commit() - return True + return {'success':True} @token_required def usageRelease(self, *args, **kwargs): @@ -625,8 +625,8 @@ class userManager: usage.memory = str(nowmemory) usage.disk = str(nowdisk) db.session.commit() - return True - + return {'success':True} + def initUsage(*args, **kwargs): """ init the usage info when start docklet with init mode diff --git a/user/user.py b/user/user.py new file mode 100755 index 0000000..6b43f41 --- /dev/null +++ b/user/user.py @@ -0,0 +1,462 @@ +#!/usr/bin/python3 +import json +import os +import getopt + +import sys, inspect + + + + +this_folder = os.path.realpath(os.path.abspath(os.path.split(inspect.getfile(inspect.currentframe()))[0])) +src_folder = os.path.realpath(os.path.abspath(os.path.join(this_folder,"..", "src"))) +if src_folder not in sys.path: + sys.path.insert(0, src_folder) + +# must first init loadenv +from log import initlogging +initlogging("docklet-user") +from log import logger + +import tools, env +config = env.getenv("CONFIG") +tools.loadenv(config) + + +from flask import Flask, request, session, render_template, redirect, send_from_directory, make_response, url_for, abort +from functools import wraps +import userManager,beansapplicationmgr, notificationmgr +import threading + + +app = Flask(__name__) + + +def login_required(func): + @wraps(func) + def wrapper(*args, **kwargs): + global G_usermgr + logger.info ("get request, path: %s" % request.path) + 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'}) + 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': + logger.info("%s login failed" % user) + return json.dumps({'success':'false', 'message':'auth failed'}) + logger.info("%s login success" % user) + return json.dumps({'success':'true', 'action':'login', 'data': auth_result['data']}) + +@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 = request.form.get('email','') + newuser.student_number = request.form.get('studentnumber', '') + newuser.department = request.form.get('department', '') + newuser.truename = request.form.get('truename', '') + newuser.tel = request.form.get('tel', '') + newuser.description = request.form.get('description', '') + result = G_usermgr.register(user = newuser) + userManager.send_remind_activating_email(newuser.username) + return json.dumps(result) + +@app.route("/authtoken/", methods=['POST']) +@login_required +def auth_token(cur_user, user, form): + logger.info("authing") + req = json.dumps({'success':'true','username':cur_user.username,'beans':cur_user.beans}) + logger.info("auth success") + return req + +@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) + result = G_usermgr.query(username = user, 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/chdefault/", methods=['POST']) +@login_required +def chdefault(cur_user, user, form): + global G_usermgr + logger.info("handle request: user/chdefault/") + result = G_usermgr.change_default_group(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) + + +@app.route("/user/groupNameList/", methods=['POST']) +@login_required +def groupNameList_user(cur_user, user, form): + global G_usermgr + logger.info("handle request: user/groupNameList/") + result = G_usermgr.groupListName(cur_user = cur_user) + return json.dumps(result) + + +@app.route("/user/groupList/", methods=['POST']) +@login_required +def groupList_user(cur_user, user, form): + global G_usermgr + logger.info("handle request: user/groupList/") + result = G_usermgr.groupList(cur_user = cur_user) + return json.dumps(result) + + +@app.route("/user/groupQuery/", methods=['POST']) +@login_required +def groupQuery_user(cur_user, user, form): + global G_usermgr + logger.info("handle request: user/groupQuery/") + result = G_usermgr.groupQuery(name = form.get("name"), cur_user = cur_user) + return json.dumps(result) + + +@app.route("/user/selfQuery/", methods=['POST']) +@login_required +def selfQuery_user(cur_user, user, form): + global G_usermgr + logger.info("handle request: user/selfQuery/") + result = G_usermgr.selfQuery(cur_user = cur_user) + return json.dumps(result) + + +@app.route("/user/selfModify/", methods=['POST']) +@login_required +def selfModify_user(cur_user, user, form): + global G_usermgr + logger.info("handle request: user/selfModify/") + result = G_usermgr.selfModify(cur_user = cur_user, newValue = form) + return json.dumps(result) + +@app.route("/user/usageQuery/" , methods=['POST']) +@login_required +def usageQuery_user(cur_user, user, form): + global G_usermgr + logger.info("handle request: user/usageQuery/") + result = G_usermgr.usageQuery(cur_user = cur_user) + return json.dumps(result) + +@app.route("/user/usageInc/", methods=['POST']) +@login_required +def usageInc_user(cur_user, user, form): + global G_usermgr + logger.info("handle request: user/usageInc/") + setting = form.get('setting') + result = G_usermgr.usageInc(cur_user = cur_user, modification = json.loads(setting)) + return json.dumps(result) + +@app.route("/user/usageRelease/", methods=['POST']) +@login_required +def usageRelease_user(cur_user, user, form): + global G_usermgr + logger.info("handle request: user/usageInc/") + result = G_usermgr.usageRelease(cur_user = cur_user, clustername = form.get('clustername'), containername = form.get('containername'), allcontainer = form.get('allcontainer')) + return json.dumps(result) + +@app.route("/user/usageRecover/", methods=['POST']) +@login_required +def usageRecover_user(cur_user, user, form): + global G_usermgr + logger.info("handle request: user/usageInc/") + result = G_usermgr.usageRecover(cur_user = cur_user, modification = json.loads(form.get('setting'))) + return json.dumps(result) + +@app.route("/user/lxcsettingList/", methods=['POST']) +@login_required +def lxcsettingList_user(cur_user, user, form): + global G_usermgr + logger.info("handle request: user/lxcsettingList/") + result = G_usermgr.lxcsettingList(cur_user = cur_user, form = form) + return json.dumps(result) + +@app.route("/user/chlxcsetting/", methods=['POST']) +@login_required +def chlxcsetting_user(cur_user, user, form): + global G_usermgr + logger.info("handle request: user/chlxcsetting/") + result = G_usermgr.chlxcsetting(cur_user = cur_user, form = form) + return json.dumps(result) + +@app.route("/notification/list/", methods=['POST']) +@login_required +def list_notifications(cur_user, user, form): + global G_notificationmgr + logger.info("handle request: notification/list/") + result = G_notificationmgr.list_notifications(cur_user=cur_user, form=form) + return json.dumps(result) + + +@app.route("/notification/create/", methods=['POST']) +@login_required +def create_notification(cur_user, user, form): + global G_notificationmgr + logger.info("handle request: notification/create/") + result = G_notificationmgr.create_notification(cur_user=cur_user, form=form) + return json.dumps(result) + + +@app.route("/notification/modify/", methods=['POST']) +@login_required +def modify_notification(cur_user, user, form): + global G_notificationmgr + logger.info("handle request: notification/modify/") + result = G_notificationmgr.modify_notification(cur_user=cur_user, form=form) + return json.dumps(result) + + +@app.route("/notification/delete/", methods=['POST']) +@login_required +def delete_notification(cur_user, user, form): + global G_notificationmgr + logger.info("handle request: notification/delete/") + result = G_notificationmgr.delete_notification(cur_user=cur_user, form=form) + return json.dumps(result) + + +@app.route("/notification/query_self/", methods=['POST']) +@login_required +def query_self_notification_simple_infos(cur_user, user, form): + global G_notificationmgr + logger.info("handle request: notification/query_self/") + result = G_notificationmgr.query_self_notification_simple_infos(cur_user=cur_user, form=form) + return json.dumps(result) + + +@app.route("/notification/query/", methods=['POST']) +@login_required +def query_notification(cur_user, user, form): + global G_notificationmgr + logger.info("handle request: notification/query/") + result = G_notificationmgr.query_notification(cur_user=cur_user, form=form) + return json.dumps(result) + + +@app.route("/notification/query/all/", methods=['POST']) +@login_required +def query_self_notifications_infos(cur_user, user, form): + global G_notificationmgr + logger.info("handle request: notification/query/all/") + result = G_notificationmgr.query_self_notifications_infos(cur_user=cur_user, form=form) + return json.dumps(result) + +@app.route("/beans//", methods=['POST']) +@login_required +def beans_apply(cur_user,user,form,issue): + global G_applicationmgr + if issue == 'apply': + number = form.get("number",None) + reason = form.get("reason",None) + if number is None or reason is None: + return json.dumps({'success':'false', 'message':'Number and reason can\'t be null.'}) + [success,message] = G_applicationmgr.apply(user,number,reason) + if not success: + return json.dumps({'success':'false', 'message':message}) + else: + return json.dumps({'success':'true'}) + elif issue == 'applymsgs': + applymsgs = G_applicationmgr.query(user) + return json.dumps({'success':'true','applymsgs':applymsgs}) + else: + return json.dumps({'success':'false','message':'Unsupported URL!'}) + +@app.route("/beans/admin//", methods=['POST']) +@login_required +def beans_admin(cur_user,user,form,issue): + global G_applicationmgr + if issue == 'applymsgs': + result = G_applicationmgr.queryUnRead(cur_user = cur_user) + return json.dumps(result) + elif issue == 'agree': + msgid = form.get("msgid",None) + if msgid is None: + return json.dumps({'success':'false', 'message':'msgid can\'t be null.'}) + result = G_applicationmgr.agree(msgid, cur_user = cur_user) + return json.dumps(result) + elif issue == 'reject': + msgid = form.get("msgid",None) + if msgid is None: + return json.dumps({'success':'false', 'message':'msgid can\'t be null.'}) + result = G_applicationmgr.reject(msgid, cur_user = cur_user) + return json.dumps(result) + else: + return json.dumps({'success':'false', 'message':'Unsupported URL!'}) + +if __name__ == '__main__': + logger.info('Start Flask...:') + try: + secret_key_file = open(env.getenv('FS_PREFIX') + '/local/user_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/user_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_usermgr + global G_notificationmgr + global G_sysmgr + global G_historymgr + global G_applicationmgr + + fs_path = env.getenv("FS_PREFIX") + logger.info("using FS_PREFIX %s" % fs_path) + + mode = 'recovery' + if len(sys.argv) > 1 and sys.argv[1] == "new": + mode = 'new' + + + G_usermgr = userManager.userManager('root',"unias") + #if mode == "new": + # G_usermgr.initUsage() + G_notificationmgr = notificationmgr.NotificationMgr() + + #userip = env.getenv('USER_IP') + userip = "0.0.0.0" + logger.info("using USER_IP %s", userip) + + #userport = env.getenv('USER_PORT') + userport = 9100 + logger.info("using USER_PORT %d", int(userport)) + + G_applicationmgr = beansapplicationmgr.ApplicationMgr() + + # server = http.server.HTTPServer((masterip, masterport), DockletHttpHandler) + logger.info("starting user server") + + app.run(host = userip, port = userport, threaded=True,) diff --git a/web/webViews/dockletrequest.py b/web/webViews/dockletrequest.py index 8d1639d..cdd79ed 100644 --- a/web/webViews/dockletrequest.py +++ b/web/webViews/dockletrequest.py @@ -4,6 +4,7 @@ from webViews.log import logger endpoint = "http://0.0.0.0:9000" +user_endpoint = "http://0.0.0.0:9100" class dockletRequest(): @@ -13,8 +14,18 @@ class dockletRequest(): data = dict(data) data['token'] = session['token'] logger.info ("Docklet Request: user = %s data = %s, url = %s"%(session['username'], data, url)) - - result = requests.post(endpoint + url, data=data).json() + reqtype = url.split("/")[1] + userreq = { + 'login', + 'register', + 'user', + 'beans', + 'notification' + } + if reqtype in userreq: + result = requests.post(user_endpoint + url, data=data).json() + else: + result = requests.post(endpoint + url, data=data).json() # logger.info('response content: %s'%response.content) # result = response.json() if (result.get('success', None) == "false" and result.get('reason', None) == "Unauthorized Action"): @@ -32,6 +43,6 @@ class dockletRequest(): data = dict(data) data_log = {'user': data.get('user', 'external')} logger.info("Docklet Unauthorized Request: data = %s, url = %s" % (data_log, url)) - result = requests.post(endpoint + url, data = data).json() + result = requests.post(user_endpoint + url, data = data).json() logger.info("Docklet Unauthorized Response: result = %s, url = %s"%(result, url)) return result