Merge remote-tracking branch 'upstream/multimaster'

This commit is contained in:
zhongyehong 2017-03-19 20:47:55 +08:00
commit e714049d54
6 changed files with 679 additions and 461 deletions

View File

@ -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
@ -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,31 +180,42 @@ 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)
do_stop_web
do_stop_proxy
do_stop_master
do_stop_user
do_start_master "recovery"
do_start_proxy
do_start_web
do_start_user
;;
start_proxy)
@ -221,6 +248,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
;;

View File

@ -27,12 +27,20 @@ import userManager,beansapplicationmgr
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"
G_userip = "0.0.0.0" #env.getenv("USER_IP")
def post_to_user(url = '/', data={}):
return requests.post(userpoint+url,data=data).json()
app = Flask(__name__)
from functools import wraps
@ -41,52 +49,23 @@ 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):
logger.info ("get request without token, path: %s" % request.path)
return json.dumps({'success':'false', 'message':'user or key is null'})
cur_user = G_usermgr.auth_token(token)
if (cur_user == None):
logger.info ("get request with an invalid token, path: %s" % request.path)
return json.dumps({'success':'false', 'message':'token failed or expired', 'Unauthorized': 'True'})
logger.info ("get request, user: %s, path: %s" % (cur_user.username, request.path))
return func(cur_user, cur_user.username, request.form, *args, **kwargs)
return wrapper
def beans_check(func):
@wraps(func)
def wrapper(*args, **kwargs):
user = args[0]
if user.beans <= 0:
return json.dumps({'success':'false','message':'user\'s beans are less than or equal to 0! Please apply for more beans by clicking the \'apply\' button or the beans icon on the left side.'})
result = post_to_user("/authtoken/", {'token':token})
if result.get('success') == 'true':
username = result.get('username')
beans = result.get('beans')
else:
return func(*args, **kwargs)
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
# def inside_ip_required(func):
# @wraps(func)
# def wrapper(*args, **kwargs):
# global G_usermgr
# global G_vclustermgr
# #cur_user = G_usermgr.find_by_ip(request.remote_addr)
# cur_user = G_usermgr.auth_token(G_usermgr.auth('username', 'password')['data']['token'])
# if (cur_user == None):
# logger.info ("get request with an invalid ip, path: %s" % request.path)
# return json.dumps({'success':'false', 'message':'invalid ip address', 'Unauthorized': 'True'})
# #cluster_info = G_vclustermgr.find_by_ip(cur_user.username, request.remote_addr)
# cluster_filename = '/opt/docklet/global/users/liupd/clusters/asdf'
# cluster_file = open(cluster_filename,'r')
# cluster_info = json.loads(cluster_file.read())
# cluster_file.close()
# cluster_info['name'] = 'asdf'
# logger.info ("get request with ip, user: %s, ip: %s ,path: %s" % (cur_user.username, request.remote_addr, request.path))
# return func(cur_user, cluster_info, request.form, *args, **kwargs)
#
# return wrapper
def worker_ip_required(func):
@wraps(func)
def wrapper(*args, **kwargs):
@ -107,84 +86,34 @@ def worker_ip_required(func):
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)
def user_ip_required(func):
@wraps(func)
def wrapper(*args,**kwargs):
global G_userip
ip = request.remote_addr
#logger.info(str(ip) + " " + str(G_userip))
if ip == '127.0.0.1' or ip == '0.0.0.0' or ip == G_userip:
return func(*args, **kwargs)
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)
return json.dumps({'success':'false','message':'User node\'s ip is required!'})
return wrapper
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
@beans_check
def create_cluster(cur_user, user, form):
global G_usermgr
def create_cluster(user, beans, form):
global G_vclustermgr
clustername = form.get('clustername', None)
if (clustername == None):
@ -193,7 +122,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 = {
@ -201,21 +130,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
@beans_check
def scaleout_cluster(cur_user, user, form):
global G_usermgr
def scaleout_cluster(user, beans, form):
global G_vclustermgr
clustername = form.get('clustername', None)
logger.info ("scaleout: %s" % form)
@ -226,33 +156,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})
@ -262,7 +194,7 @@ def scalein_cluster(cur_user, user, form):
@app.route("/cluster/start/", methods=['POST'])
@login_required
@beans_check
def start_cluster(cur_user, user, form):
def start_cluster(user, beans, form):
global G_vclustermgr
clustername = form.get('clustername', None)
if (clustername == None):
@ -276,7 +208,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):
@ -290,16 +222,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})
@ -308,7 +239,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):
@ -322,7 +253,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)
@ -332,7 +263,7 @@ def list_cluster(cur_user, user, form):
return json.dumps({'success':'false', 'action':'list cluster', 'message':clusterlist})
@app.route("/cluster/stopall/",methods=['POST'])
@worker_ip_required
@user_ip_required
def stopall_cluster():
global G_vclustermgr
user = request.form.get('username',None)
@ -349,7 +280,7 @@ def stopall_cluster():
@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):
@ -360,9 +291,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'})
@ -376,7 +306,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")
@ -388,14 +318,14 @@ 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(cur_user, user, form):
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'))
@ -403,7 +333,7 @@ def update_base(cur_user, user, form):
@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)
@ -414,7 +344,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)
@ -422,7 +352,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)
@ -430,7 +360,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)
@ -438,7 +368,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)
@ -452,7 +382,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)
@ -461,7 +391,7 @@ def deleteproxy(cur_user, user, form):
@app.route("/monitor/hosts/<com_id>/<issue>/", 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")
@ -503,7 +433,7 @@ def hosts_monitor(cur_user, user, form, com_id, issue):
@app.route("/monitor/vnodes/<con_id>/<issue>/", 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
global G_historymgr
logger.info("handle request: monitor/vnodes")
@ -521,7 +451,7 @@ def vnodes_monitor(cur_user, user, form, con_id, issue):
res['history'] = G_historymgr.getHistory(con_id)
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'] = ""
@ -535,12 +465,11 @@ def vnodes_monitor(cur_user, user, form, con_id, issue):
@app.route("/monitor/user/<issue>/", methods=['POST'])
@login_required
def user_quotainfo_monitor(cur_user, user, form,issue):
global G_usermgr
def user_quotainfo_monitor(user, beans, form, issue):
global G_historymgr
if issue == 'quotainfo':
logger.info("handle request: monitor/user/quotainfo/")
user_info = G_usermgr.selfQuery(cur_user = cur_user)
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':
@ -552,15 +481,23 @@ def user_quotainfo_monitor(cur_user, user, form,issue):
@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("/beans/mail/", methods=['POST'])
@app.route("/billing/beans/", methods=['POST'])
@worker_ip_required
def billing_beans():
form = request.form
res = post_to_user("/billing/beans/",data=form)
logger.info(res)
return json.dumps(res)
@app.route("/beans/mail/", methods=['POST'])
@user_ip_required
def beans_mail():
logger.info("handle request: beans/mail/")
addr = request.form.get("to_address",None)
@ -573,271 +510,10 @@ def beans_mail():
beansapplicationmgr.send_beans_email(addr,username,int(beans))
return json.dumps({'success':'true'})
@app.route("/beans/<issue>/", 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/<issue>/", 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!'})
@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()
@ -845,7 +521,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)
@ -860,7 +536,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)
@ -874,7 +550,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)
@ -889,7 +565,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)
@ -903,7 +579,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)
@ -972,7 +648,6 @@ if __name__ == '__main__':
global G_nodemgr
global G_vclustermgr
global G_usermgr
global G_notificationmgr
global etcdclient
global G_networkmgr
@ -1055,10 +730,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)
@ -1087,10 +758,6 @@ if __name__ == '__main__':
master_collector = monitor.Master_Collector(G_nodemgr,ipaddr+":"+str(masterport))
master_collector.start()
logger.info("master_collector started")
G_applicationmgr = beansapplicationmgr.ApplicationMgr()
approvalrbt = beansapplicationmgr.ApprovalRobot()
approvalrbt.start()
# server = http.server.HTTPServer((masterip, masterport), DockletHttpHandler)
logger.info("starting master server")

View File

@ -22,10 +22,11 @@ import time,threading,json,traceback,platform
import env
from datetime import datetime
from model import db,VNode,History,User
from model import db,VNode,History
from log import logger
from httplib2 import Http
from urllib.parse import urlencode
from httprest import post_to_user
# billing parameters
a_cpu = 500 # seconds
@ -213,30 +214,8 @@ class Container_Collector(threading.Thread):
raise
# update users' tables in database
owner_name = get_owner(vnode_name)
owner = User.query.filter_by(username=owner_name).first()
if owner is None:
logger.warning("Error!!! Billing User %s doesn't exist!" % (owner_name))
else:
#logger.info("Billing User:"+str(owner))
oldbeans = owner.beans
owner.beans -= billingval
#logger.info(str(oldbeans) + " " + str(owner.beans))
if oldbeans > 0 and owner.beans <= 0 or oldbeans >= 100 and owner.beans < 100 or oldbeans >= 500 and owner.beans < 500 or oldbeans >= 1000 and owner.beans < 1000:
# send mail to remind users of their beans if their beans decrease to 0,100,500 and 1000
data = {"to_address":owner.e_mail,"username":owner.username,"beans":owner.beans}
request_master("/beans/mail/",data)
try:
db.session.commit()
except Exception as err:
db.session.rollback()
logger.warning(traceback.format_exc())
logger.warning(err)
#logger.info("Billing User:"+str(owner))
if owner.beans <= 0:
# stop all vcluster of the user if his beans are equal to or lower than 0.
logger.info("The beans of User(" + str(owner) + ") are less than or equal to zero, the container("+vnode_name+") will be stopped.")
form = {'username':owner.username}
request_master("/cluster/stopall/",form)
data = {"owner_name":owner_name,"billing":billingval}
request_master("/billing/beans/",data)
return billingval
# the main function to collect monitoring data of a container

View File

@ -544,24 +544,24 @@ 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):
@ -592,7 +592,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):
@ -647,7 +647,7 @@ class userManager:
usage.memory = str(nowmemory)
usage.disk = str(nowdisk)
db.session.commit()
return True
return {'success':True}
def initUsage(*args, **kwargs):
"""

533
user/user.py Executable file
View File

@ -0,0 +1,533 @@
#!/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)
G_masterip = env.getenv("MASTER_IP") + ":" + str(env.getenv("MASTER_PORT"))
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,traceback
from model import User,db
from httplib2 import Http
from urllib.parse import urlencode
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
def master_ip_required(func):
@wraps(func)
def wrapper(*args, **kwargs):
global G_masterip
ip = request.remote_addr
#logger.info(str(ip) + " " + str(G_masterip))
if ip == '127.0.0.1' or ip == '0.0.0.0' or ip == G_masterip:
return func(*args, **kwargs)
else:
return json.dumps({'success':'false','message':'Master\'s ip is required!'})
return wrapper
# send http request to master
def request_master(url,data):
global G_masterip
#logger.info("master_ip:"+str(G_masterip))
header = {'Content-Type':'application/x-www-form-urlencoded'}
http = Http()
[resp,content] = http.request("http://"+G_masterip+url,"POST",urlencode(data),headers = header)
logger.info("response from master:"+content.decode('utf-8'))
@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("/billing/beans/", methods=['POST'])
@master_ip_required
def billing_beans():
logger.info("handle request: /billing/beans/")
form = request.form
owner_name = form.get("owner_name",None)
billing = int(form.get("billing",None))
if owner_name is None or billing is None:
return json.dumps({'success':'false', 'message':'owner_name and beans fields are required.'})
# update users' tables in database
owner = User.query.filter_by(username=owner_name).first()
if owner is None:
logger.warning("Error!!! Billing User %s doesn't exist!" % (owner_name))
else:
#logger.info("Billing User:"+str(owner))
oldbeans = owner.beans
owner.beans -= billing
#logger.info(str(oldbeans) + " " + str(owner.beans))
if oldbeans > 0 and owner.beans <= 0 or oldbeans >= 100 and owner.beans < 100 or oldbeans >= 500 and owner.beans < 500 or oldbeans >= 1000 and owner.beans < 1000:
# send mail to remind users of their beans if their beans decrease to 0,100,500 and 1000
data = {"to_address":owner.e_mail,"username":owner.username,"beans":owner.beans}
request_master("/beans/mail/",data)
try:
db.session.commit()
except Exception as err:
db.session.rollback()
logger.warning(traceback.format_exc())
logger.warning(err)
return json.dumps({'success':'false', 'message':'Fail to wirte to databases.'})
#logger.info("Billing User:"+str(owner))
if owner.beans <= 0:
# stop all vcluster of the user if his beans are equal to or lower than 0.
logger.info("The beans of User(" + str(owner) + ") are less than or equal to zero, all his or her vclusters will be stopped.")
form = {'username':owner.username}
request_master("/cluster/stopall/",form)
return json.dumps({'success':'true'})
@app.route("/beans/<issue>/", 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/<issue>/", 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)
logger.debug("applymsg success")
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!'})
@app.errorhandler(500)
def internal_server_error(error):
logger.debug("An internel server error occured")
logger.error(traceback.format_exc())
return json.dumps({'success':'false', 'message':'500 Internal Server Error', 'Unauthorized': 'True'})
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()
approvalrbt = beansapplicationmgr.ApprovalRobot()
approvalrbt.start()
# server = http.server.HTTPServer((masterip, masterport), DockletHttpHandler)
logger.info("starting user server")
app.run(host = userip, port = userport, threaded=True,)

View File

@ -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,7 +14,17 @@ class dockletRequest():
data = dict(data)
data['token'] = session['token']
logger.info ("Docklet Request: user = %s data = %s, url = %s"%(session['username'], data, url))
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()
@ -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