def hosts_monitor(user, beans, form, com_id, issue): global G_clustername logger.info("handle request: monitor/hosts") res = {} fetcher = monitor.Fetcher(com_id) if issue == 'meminfo': res['meminfo'] = fetcher.get_meminfo() elif issue == 'cpuinfo': res['cpuinfo'] = fetcher.get_cpuinfo() elif issue == 'cpuconfig': res['cpuconfig'] = fetcher.get_cpuconfig() elif issue == 'diskinfo': res['diskinfo'] = fetcher.get_diskinfo() elif issue == 'osinfo': res['osinfo'] = fetcher.get_osinfo() #elif issue == 'concpuinfo': # res['concpuinfo'] = fetcher.get_concpuinfo() elif issue == 'containers': res['containers'] = fetcher.get_containers() elif issue == 'status': res['status'] = fetcher.get_status() elif issue == 'containerslist': res['containerslist'] = fetcher.get_containerslist() elif issue == 'containersinfo': res = [] conlist = fetcher.get_containerslist() for container in conlist: ans = {} confetcher = monitor.Container_Fetcher(etcdaddr, G_clustername) ans = confetcher.get_basic_info(container) ans['cpu_use'] = confetcher.get_cpu_use(container) ans['mem_use'] = confetcher.get_mem_use(container) res.append(ans) else: return json.dumps({ 'success': 'false', 'message': 'not supported request' }) return json.dumps({'success': 'true', 'monitor': res})
def do_POST(self): global G_vclustermgr global G_usermgr #logger.info ("get request, header content:\n%s" % self.headers) #logger.info ("read request content:\n%s" % self.rfile.read(int(self.headers["Content-Length"]))) logger.info ("get request, path: %s" % self.path) # for test if self.path == '/test': logger.info ("return welcome for test") self.response(200, {'success':'true', 'message':'welcome to docklet'}) return [True, 'test ok'] # check for not null content if 'Content-Length' not in self.headers: logger.info ("request content is null") self.response(401, {'success':'false', 'message':'request content is null'}) return [False, 'content is null'] # auth the user # cgi.FieldStorage need fp/headers/environ. (see /usr/lib/python3.4/cgi.py) form = cgi.FieldStorage(fp=self.rfile, headers=self.headers,environ={'REQUEST_METHOD':'POST'}) cmds = self.path.strip('/').split('/') if cmds[0] == 'register' and form.getvalue('activate', None) == None: logger.info ("handle request : user register") username = form.getvalue('username', '') password = form.getvalue('password', '') email = form.getvalue('email', '') description = form.getvalue('description','') if (username == '' or password == '' or email == ''): self.response(500, {'success':'false'}) newuser = G_usermgr.newuser() newuser.username = form.getvalue('username') newuser.password = form.getvalue('password') newuser.e_mail = form.getvalue('email') newuser.student_number = form.getvalue('studentnumber') newuser.department = form.getvalue('department') newuser.nickname = form.getvalue('truename') newuser.truename = form.getvalue('truename') newuser.description = form.getvalue('description') newuser.status = "init" newuser.auth_method = "local" result = G_usermgr.register(user = newuser) self.response(200, result) return [True, "register succeed"] if cmds[0] == 'login': logger.info ("handle request : user login") user = form.getvalue("user") key = form.getvalue("key") if user == None or key == None: self.response(401, {'success':'false', 'message':'user or key is null'}) return [False, "auth failed"] auth_result = G_usermgr.auth(user, key) if auth_result['success'] == 'false': self.response(401, {'success':'false', 'message':'auth failed'}) return [False, "auth failed"] self.response(200, {'success':'true', 'action':'login', 'data': auth_result['data']}) return [True, "auth succeeded"] if cmds[0] == 'external_login': logger.info ("handle request : external user login") try: result = G_usermgr.auth_external(form) self.response(200, result) return result except: result = {'success': 'false', 'reason': 'Something wrong happened when auth an external account'} self.response(200, result) return result token = form.getvalue("token") if token == None: self.response(401, {'success':'false', 'message':'user or key is null'}) return [False, "auth failed"] cur_user = G_usermgr.auth_token(token) if cur_user == None: self.response(401, {'success':'false', 'message':'token failed or expired', 'Unauthorized': 'True'}) return [False, "auth failed"] user = cur_user.username # parse the url and get to do actions # /cluster/list # /cluster/create & clustername # /cluster/start & clustername # /cluster/stop & clustername # /cluster/delete & clustername # /cluster/info & clustername if cmds[0] == 'cluster': clustername = form.getvalue('clustername') # check for 'clustername' : all actions except 'list' need 'clustername' if (cmds[1] != 'list') and clustername == None: self.response(401, {'success':'false', 'message':'clustername is null'}) return [False, "clustername is null"] if cmds[1] == 'create': image = {} image['name'] = form.getvalue("imagename") image['type'] = form.getvalue("imagetype") image['owner'] = form.getvalue("imageowner") user_info = G_usermgr.selfQuery(cur_user = cur_user) user_info = json.dumps(user_info) logger.info ("handle request : create cluster %s with image %s " % (clustername, image['name'])) [status, result] = G_vclustermgr.create_cluster(clustername, user, image, user_info) if status: self.response(200, {'success':'true', 'action':'create cluster', 'message':result}) else: self.response(200, {'success':'false', 'action':'create cluster', 'message':result}) elif cmds[1] == 'scaleout': logger.info("handle request : scale out %s" % clustername) image = {} image['name'] = form.getvalue("imagename") image['type'] = form.getvalue("imagetype") image['owner'] = form.getvalue("imageowner") logger.debug("imagename:" + image['name']) logger.debug("imagetype:" + image['type']) logger.debug("imageowner:" + image['owner']) user_info = G_usermgr.selfQuery(cur_user = cur_user) user_info = json.dumps(user_info) [status, result] = G_vclustermgr.scale_out_cluster(clustername, user, image, user_info) if status: self.response(200, {'success':'true', 'action':'scale out', 'message':result}) else: self.response(200, {'success':'false', 'action':'scale out', 'message':result}) elif cmds[1] == 'scalein': logger.info("handle request : scale in %s" % clustername) containername = form.getvalue("containername") [status, result] = G_vclustermgr.scale_in_cluster(clustername, user, containername) if status: self.response(200, {'success':'true', 'action':'scale in', 'message':result}) else: self.response(200, {'success':'false', 'action':'scale in', 'message':result}) elif cmds[1] == 'start': logger.info ("handle request : start cluster %s" % clustername) [status, result] = G_vclustermgr.start_cluster(clustername, user) if status: self.response(200, {'success':'true', 'action':'start cluster', 'message':result}) else: self.response(200, {'success':'false', 'action':'start cluster', 'message':result}) elif cmds[1] == 'stop': logger.info ("handle request : stop cluster %s" % clustername) [status, result] = G_vclustermgr.stop_cluster(clustername, user) if status: self.response(200, {'success':'true', 'action':'stop cluster', 'message':result}) else: self.response(200, {'success':'false', 'action':'stop cluster', 'message':result}) elif cmds[1] == 'delete': logger.info ("handle request : delete cluster %s" % clustername) [status, result] = G_vclustermgr.delete_cluster(clustername, user) if status: self.response(200, {'success':'true', 'action':'delete cluster', 'message':result}) else: self.response(200, {'success':'false', 'action':'delete cluster', 'message':result}) elif cmds[1] == 'info': logger.info ("handle request : info cluster %s" % clustername) [status, result] = G_vclustermgr.get_clusterinfo(clustername, user) if status: self.response(200, {'success':'true', 'action':'info cluster', 'message':result}) else: self.response(200, {'success':'false', 'action':'info cluster', 'message':result}) elif cmds[1] == 'list': logger.info ("handle request : list clusters for %s" % user) [status, clusterlist] = G_vclustermgr.list_clusters(user) if status: self.response(200, {'success':'true', 'action':'list cluster', 'clusters':clusterlist}) else: self.response(400, {'success':'false', 'action':'list cluster', 'message':clusterlist}) elif cmds[1] == 'flush': from_lxc = form.getvalue('from_lxc') G_vclustermgr.flush_cluster(user,clustername,from_lxc) self.response(200, {'success':'true', 'action':'flush'}) elif cmds[1] == 'save': imagename = form.getvalue("image") description = form.getvalue("description") containername = form.getvalue("containername") isforce = form.getvalue("isforce") if not isforce == "true": [status,message] = G_vclustermgr.image_check(user,imagename) if not status: self.response(200, {'success':'false','reason':'exists', 'message':message}) return [False, "image already exists"] user_info = G_usermgr.selfQuery(cur_user = cur_user) [status,message] = G_vclustermgr.create_image(user,clustername,containername,imagename,description,user_info["data"]["groupinfo"]["image"]) if status: logger.info("image has been saved") self.response(200, {'success':'true', 'action':'save'}) else: logger.debug(message) self.response(200, {'success':'false', 'reason':'exceed', 'message':message}) else: logger.warning ("request not supported ") self.response(400, {'success':'false', 'message':'not supported request'}) # Request for Image elif cmds[0] == 'image': if cmds[1] == 'list': images = G_imagemgr.list_images(user) self.response(200, {'success':'true', 'images': images}) elif cmds[1] == 'description': image = {} image['name'] = form.getvalue("imagename") image['type'] = form.getvalue("imagetype") image['owner'] = form.getvalue("imageowner") description = G_imagemgr.get_image_description(user,image) self.response(200, {'success':'true', 'message':description}) elif cmds[1] == 'share': image = form.getvalue('image') G_imagemgr.shareImage(user,image) self.response(200, {'success':'true', 'action':'share'}) elif cmds[1] == 'unshare': image = form.getvalue('image') G_imagemgr.unshareImage(user,image) self.response(200, {'success':'true', 'action':'unshare'}) elif cmds[1] == 'delete': image = form.getvalue('image') G_imagemgr.removeImage(user,image) self.response(200, {'success':'true', 'action':'delete'}) else: logger.warning("request not supported ") self.response(400, {'success':'false', 'message':'not supported request'}) # Add Proxy elif cmds[0] == 'addproxy': logger.info ("handle request : add proxy") proxy_ip = form.getvalue("ip") proxy_port = form.getvalue("port") clustername = form.getvalue("clustername") [status, message] = G_vclustermgr.addproxy(user,clustername,proxy_ip,proxy_port) if status is True: self.response(200, {'success':'true', 'action':'addproxy'}) else: self.response(400, {'success':'false', 'message': message}) # Delete Proxy elif cmds[0] == 'deleteproxy': logger.info ("handle request : delete proxy") clustername = form.getvalue("clustername") G_vclustermgr.deleteproxy(user,clustername) self.response(200, {'success':'true', 'action':'deleteproxy'}) # Request for Monitor elif cmds[0] == 'monitor': logger.info("handle request: monitor") res = {} if cmds[1] == 'hosts': com_id = cmds[2] fetcher = monitor.Fetcher(etcdaddr,G_clustername,com_id) if cmds[3] == 'meminfo': res['meminfo'] = fetcher.get_meminfo() elif cmds[3] == 'cpuinfo': res['cpuinfo'] = fetcher.get_cpuinfo() elif cmds[3] == 'cpuconfig': res['cpuconfig'] = fetcher.get_cpuconfig() elif cmds[3] == 'diskinfo': res['diskinfo'] = fetcher.get_diskinfo() elif cmds[3] == 'osinfo': res['osinfo'] = fetcher.get_osinfo() elif cmds[3] == 'containers': res['containers'] = fetcher.get_containers() elif cmds[3] == 'status': res['status'] = fetcher.get_status() elif cmds[3] == 'containerslist': res['containerslist'] = fetcher.get_containerslist() elif cmds[3] == 'containersinfo': res = [] conlist = fetcher.get_containerslist() for container in conlist: ans = {} confetcher = monitor.Container_Fetcher(etcdaddr,G_clustername) ans = confetcher.get_basic_info(container) ans['cpu_use'] = confetcher.get_cpu_use(container) ans['mem_use'] = confetcher.get_mem_use(container) res.append(ans) else: self.response(400, {'success':'false', 'message':'not supported request'}) return self.response(200, {'success':'true', 'monitor':res}) elif cmds[1] == 'vnodes': fetcher = monitor.Container_Fetcher(etcdaddr,G_clustername) if cmds[3] == 'cpu_use': res['cpu_use'] = fetcher.get_cpu_use(cmds[2]) elif cmds[3] == 'mem_use': res['mem_use'] = fetcher.get_mem_use(cmds[2]) elif cmds[3] == 'basic_info': res['basic_info'] = fetcher.get_basic_info(cmds[2]) self.response(200, {'success':'true', 'monitor':res}) elif cmds[1] == 'user': if not user == 'root': self.response(400, {'success':'false', 'message':'Root Required'}) if cmds[3] == 'clustercnt': flag = True clutotal = 0 clurun = 0 contotal = 0 conrun = 0 [status, clusterlist] = G_vclustermgr.list_clusters(cmds[2]) if status: for clustername in clusterlist: clutotal += 1 [status2, result] = G_vclustermgr.get_clusterinfo(clustername, cmds[2]) if status2: contotal += result['size'] if result['status'] == 'running': clurun += 1 conrun += result['size'] else: flag = False if flag: res = {} res['clutotal'] = clutotal res['clurun'] = clurun res['contotal'] = contotal res['conrun'] = conrun self.response(200, {'success':'true', 'monitor':{'clustercnt':res}}) else: self.response(200, {'success':'false','message':clusterlist}) elif cmds[3] == 'cluster': if cmds[4] == 'list': [status, clusterlist] = G_vclustermgr.list_clusters(cmds[2]) if status: self.response(200, {'success':'true', 'monitor':{'clusters':clusterlist}}) else: self.response(400, {'success':'false', 'message':clusterlist}) elif cmds[4] == 'info': clustername = form.getvalue('clustername') logger.info ("handle request : info cluster %s" % clustername) [status, result] = G_vclustermgr.get_clusterinfo(clustername, user) if status: self.response(200, {'success':'true', 'monitor':{'info':result}}) else: self.response(200, {'success':'false','message':result}) else: self.response(400, {'success':'false', 'message':'not supported request'}) elif cmds[1] == 'listphynodes': res['allnodes'] = G_nodemgr.get_allnodes() self.response(200, {'success':'true', 'monitor':res}) # Request for User elif cmds[0] == 'user': logger.info("handle request: user") if cmds[1] == 'modify': #user = G_usermgr.query(username = form.getvalue("username"), cur_user = cur_user).get('token', None) result = G_usermgr.modify(newValue = form, cur_user = cur_user) self.response(200, result) if cmds[1] == 'groupModify': result = G_usermgr.groupModify(newValue = form, cur_user = cur_user) self.response(200, result) if cmds[1] == 'query': result = G_usermgr.query(ID = form.getvalue("ID"), cur_user = cur_user) if (result.get('success', None) == None or result.get('success', None) == "false"): self.response(301,result) else: result = G_usermgr.queryForDisplay(user = result['token']) self.response(200,result) elif cmds[1] == 'add': user = G_usermgr.newuser(cur_user = cur_user) user.username = form.getvalue('username') user.password = form.getvalue('password') user.e_mail = form.getvalue('e_mail', '') user.status = "normal" result = G_usermgr.register(user = user, cur_user = cur_user) self.response(200, result) elif cmds[1] == 'groupadd': result = G_usermgr.groupadd(form = form, cur_user = cur_user) self.response(200, result) elif cmds[1] == 'quotaadd': result = G_usermgr.quotaadd(form = form, cur_user = cur_user) self.response(200, result) elif cmds[1] == 'groupdel': result = G_usermgr.groupdel(name = form.getvalue('name', None), cur_user = cur_user) self.response(200, result) elif cmds[1] == 'data': logger.info("handle request: user/data") result = G_usermgr.userList(cur_user = cur_user) self.response(200, result) elif cmds[1] == 'groupNameList': result = G_usermgr.groupListName(cur_user = cur_user) self.response(200, result) elif cmds[1] == 'groupList': result = G_usermgr.groupList(cur_user = cur_user) self.response(200, result) elif cmds[1] == 'groupQuery': result = G_usermgr.groupQuery(name = form.getvalue("name"), cur_user = cur_user) if (result.get('success', None) == None or result.get('success', None) == "false"): self.response(301,result) else: self.response(200,result) elif cmds[1] == 'selfQuery': result = G_usermgr.selfQuery(cur_user = cur_user) self.response(200,result) elif cmds[1] == 'selfModify': result = G_usermgr.selfModify(cur_user = cur_user, newValue = form) self.response(200,result) elif cmds[0] == 'register' : #activate logger.info("handle request: user/activate") newuser = G_usermgr.newuser() newuser.username = cur_user.username newuser.nickname = cur_user.truename newuser.status = 'applying' newuser.user_group = cur_user.user_group newuser.auth_method = cur_user.auth_method newuser.e_mail = form.getvalue('email','') newuser.student_number = form.getvalue('studentnumber', '') newuser.department = form.getvalue('department', '') newuser.truename = form.getvalue('truename', '') newuser.tel = form.getvalue('tel', '') newuser.description = form.getvalue('description', '') result = G_usermgr.register(user = newuser) userManager.send_remind_activating_email(newuser.username) self.response(200,result) else: logger.warning ("request not supported ") self.response(400, {'success':'false', 'message':'not supported request'})