def stop_desktops(self, desktops, hard=False): success_num = 0 failed_num = 0 for desktop in desktops: logger.info("stop desktop group, name:%s, uuid:%s, hard:%s", desktop['name'], desktop['uuid'], hard) if not education_model.YzyDesktop.objects.filter( uuid=desktop['uuid'], deleted=False): logger.info("stop desktop group, it is not exists") return get_error_result("DesktopNotExist", name=desktop['name']) if hard: ret = server_post("/api/v1/desktop/education/hard_stop", {"uuid": desktop['uuid']}) else: ret = server_post("/api/v1/desktop/education/stop", {"uuid": desktop['uuid']}) if ret.get('code') != 0: logger.info("stop desktop group failed:%s", ret['msg']) return ret else: success_num += ret['data']['success_num'] failed_num += ret['data']['failed_num'] logger.info("stop desktop group success, name:%s", desktop['name']) return get_error_result("Success", data={ "failed_num": failed_num, "success_num": success_num })
def create_remote_storage(self, data): # 检查nfs服务器的有效性,远端存储名称有效性 等等 ip_addr = data.get('server_ip') mount_point = data.get('mount_point') if not is_ip_addr(ip_addr): return get_error_result("IpAddressError") out = server_post("/remote_storage/show_mount", {'ip_addr': ip_addr}) if mount_point not in out['data']['mount_list']: return get_error_result("InvalidNfsServerError") ret = server_post("/remote_storage/create", data) if ret.get("code", -1) != 0: return get_error_result("RemoteStorageAddFail", reason=ret.get("msg")) else: return get_error_result("Success")
def create_check(self, param, log_user=None): logger.info("create voi desktop group") group_uuid = param.get('group_uuid') if log_user: param['owner_id'] = log_user['id'] else: param['owner_id'] = 1 if not voi_education_model.YzyVoiGroup.objects.filter(uuid=group_uuid, deleted=False): logger.info( "create voi desktop group error, education group not exists") return get_error_result("GroupNotExists", name='') # 同一个教学模板,在同一教学分组下只能创建一个教学桌面 if voi_education_model.YzyVoiDesktop.objects.filter( group=group_uuid, template=param['template_uuid'], deleted=False): logger.error("the template already created desktop") return get_error_result("TemplateAlreadyUsed") # 名字冲突检测只在分组范围内 if voi_education_model.YzyVoiDesktop.objects.filter(name=param['name'], deleted=False): logger.error( "create voi desktop group error, desktop group already exists") return get_error_result("DesktopAlreadyExist", name=param['name']) ret = server_post("/api/v1/voi/desktop/education/create", param) logger.info("create voi desktop group end:%s", ret) return ret
def create_check(self, param, log_user=None): logger.info("create desktop group") pool_uuid = param.get('pool_uuid') group_uuid = param.get('group_uuid') if log_user: param['owner_id'] = log_user['id'] else: param['owner_id'] = 1 if not resource_model.YzyResourcePools.objects.filter(uuid=pool_uuid, deleted=False): logger.info("create desktop group error, resource pool not exists") return get_error_result("ResourcePoolNotExist") if not education_model.YzyGroup.objects.filter(uuid=group_uuid, deleted=False): logger.info( "create desktop group error, education group not exists") return get_error_result("GroupNotExists", name='') # 名字冲突检测只在分组范围内 if education_model.YzyDesktop.objects.filter(name=param['name'], group=group_uuid, deleted=False): logger.error( "create desktop group error, desktop group already exists") return get_error_result("DesktopAlreadyExist", name=param['name']) ret = server_post("/api/v1/desktop/education/create", param) logger.info("create desktop group end:%s", ret) return ret
def copy_check(self, param, log_user=None): logger.info("copy template") uuid = param.get('template_uuid') if log_user: param['owner_id'] = log_user['id'] else: param['owner_id'] = 1 if not education_model.YzyInstanceTemplate.objects.filter(uuid=uuid, deleted=False): logger.error("copy template error, it is not exists") return get_error_result("TemplateNotExist", name='') if education_model.YzyInstanceTemplate.objects.filter(name=param['name'], deleted=False): logger.error("copy template error, the template name already exists") return get_error_result("TemplateAlreadyExist", name=param['name']) # 已选择IP类型为固定IP,校验bind_ip if param.get('IPtype', None) == "2": bind_ip = param.get('bind_ip', None) if not bind_ip: return get_error_result("StaticIPTypeWithoutIPAddress") if not is_ip_addr(bind_ip): return get_error_result("IpAddressError") ret = server_post("/api/v1/template/copy", param) logger.info("copy template end:%s", ret) return ret
def shutdown_node(self, data): """ data : [ {"uuid": "xxxxxxx", "hostname": "xxxxxx"}, {"uuid": "xxxxxxx", "hostname": "xxxxxx"}, {"uuid": "xxxxxxx", "hostname": "xxxxxx"} ] :param data: :return: """ names = [] ret = get_error_result("Success") for node in data: uuid = node.get("uuid") hostname = node.get("hostname") names.append(hostname) obj = self.get_object_by_uuid(YzyNodes, uuid) if obj: ret = server_post("/node/shutdown", node) if ret.get("code", -1) != 0: logger.error("shutdown node[%s] error: %s" % (hostname, ret)) break else: logger.error("shutdown node[%s] info not exist!" % hostname) ret = get_error_result("NodeNotExistMsg", hostname=hostname) msg = "关机节点 %s" % ('/'.join(names)) insert_operation_log(msg, ret["msg"]) return ret
def publish(self, data, request): tmp_path = data['path'] # base_path = self._get_template_storage_path() # # try: # os.makedirs(base_path) # except: # pass # data['path'] = os.path.join(base_path, data['uuid']) ser = YzyBaseImagesSerializer(data=data, context={'request': request}) if ser.is_valid() and os.path.exists(tmp_path): logger.info("save image info to db") ser.save() post_data = { "pool_uuid": data['resource_pool'], "image_id": data['uuid'], "image_path": tmp_path, "md5_sum": data['md5_sum'] } # move(tmp_path, data['path']) # os.remove(tmp_path) logger.info("begin upload images to other node in resource pool") ret = server_post('/resource_pool/images/upload', post_data) if ret.get("code", -1) != 0: logger.error("upload base image error, server return: %s", ret) return JSONResponse(ret) else: if os.path.exists(tmp_path): os.remove(tmp_path) return JSONResponse(get_error_result("OtherError")) return JSONResponse(ser.data)
def delete_user(self, user): logger.info("delete group name:%s, user:%s", user['user_name'], user['uuid']) qry = personal_model.YzyGroupUser.objects.filter( uuid=user['uuid'], deleted=False).first() if not qry: logger.info("delete group user failed, it is not exists") return get_error_result("GroupUserNotExists", user_name=user['user_name']) ret = server_post("/api/v1/group/user/delete", {"uuid": user['uuid']}) if ret.get('code') != 0: logger.info("delete group user failed:%s", ret['msg']) return ret else: if qry.mac: # 提交终端服务接口 req_data = { "handler": "WebTerminalHandler", "command": "user_logout", "data": { "mac_list": qry.mac, } } ret = terminal_post("/api/v1/terminal/task", req_data) if ret.get("code", -1) != 0: logger.error("terminal send user logout fail: %s" % qry.mac) logger.info("delete group user success, name:%s, uuid:%s", user['user_name'], user['uuid']) return get_error_result("Success", {"success_num": 1, "failed_num": 0})
def update_subnet(self, data, data_network_uuid, sub_network_uuid): subnets = YzySubnets.objects.filter( network=data_network_uuid, deleted=False).exclude(uuid=sub_network_uuid) ret, status = network_mgr.check_subnet_params(data, subnets) if not status: logger.error("update subnet error: check subnet params fail") return ret # try: # subnets = YzySubnets.objects.filter(network=data_network_uuid, deleted=False) # self.check_subnet_params(data, subnets) # for subnet in subnets: # if subnet.uuid != sub_network_uuid: # flag_a = ipaddress.ip_network(data['start_ip']).compare_networks(ipaddress.ip_network(subnet.start_ip)) # flag_b = ipaddress.ip_network(subnet.end_ip).compare_networks(ipaddress.ip_network(data['start_ip'])) # flag_c = ipaddress.ip_network(data['end_ip']).compare_networks(ipaddress.ip_network(subnet.start_ip)) # flag_d = ipaddress.ip_network(subnet.end_ip).compare_networks(ipaddress.ip_network(data['end_ip'])) # flag_e = ipaddress.ip_network(subnet.start_ip).compare_networks(ipaddress.ip_network(data['start_ip'])) # flag_f = ipaddress.ip_network(data['end_ip']).compare_networks(ipaddress.ip_network(subnet.end_ip)) # if (flag_a >= 0 and flag_b >= 0) or (flag_c >= 0 and flag_d >= 0) or (flag_e >= 0 and flag_f >= 0): # return get_error_result("IpAddressConflictError") # except Exception as e: # logger.error("update subnet error: subnet parameters[%s] error:%s", data, e, exc_info=True) # return get_error_result("SubnetInfoError", name=data['name']) data["uuid"] = sub_network_uuid ret = server_post('/subnet/update', data) logger.info("update subnet: server api return %s", ret) return ret
def delete(self, resource_pool_uuid, uuids): # pass resource_pool = self.get_object_by_uuid(YzyResourcePools, resource_pool_uuid) if not resource_pool: logger.error( "delete resource pool base image error: not resource pool[%s]" % resource_pool_uuid) return get_error_result("ResourcePoolNotExist") # ret = get_error_result("Success") # for uuid in uuids: # try: # base_image = self.get_object_by_uuid(YzyBaseImages, uuid) # except Exception as e: # ret = get_error_result("ResourceImageNotExist") # logger.error("delete base image: %s error!" % (uuid)) # break # # path = "" # try: # path = base_image.path # if os.path.exists(path): # os.remove(path) # base_image.delete() # except: # ret = get_error_result("ResourceImageDelFail") # logger.error("delete base image file: %s fail"%(uuid)) # break data = {"pool_uuid": resource_pool_uuid, "uuids": uuids} ret = server_post("/resource_pool/images/delete", data) msg = "删除基础镜像 %s" % ('/'.join(uuids)) insert_operation_log(msg, ret["msg"]) return ret
def update_group(self, data): if not voi_education_model.YzyVoiGroup.objects.filter(uuid=data['uuid'], deleted=False): logger.info("update voi education group error, it is not exists") return get_error_result("GroupNotExists", name=data['name']) if data['name'] != data['value']['name']: if voi_education_model.YzyVoiGroup.objects.filter(name=data['value']['name'], deleted=False): return get_error_result("GroupAlreadyExists", name=data['value']['name']) groups = voi_education_model.YzyVoiGroup.objects.filter(group_type=1, deleted=False) for group in groups: if group.uuid != data['uuid']: flag_a = ipaddress.ip_network(data['value']['start_ip']).\ compare_networks(ipaddress.ip_network(group.start_ip)) flag_b = ipaddress.ip_network(group.end_ip)\ .compare_networks(ipaddress.ip_network(data['value']['start_ip'])) flag_c = ipaddress.ip_network(data['value']['end_ip']).\ compare_networks(ipaddress.ip_network(group.start_ip)) flag_d = ipaddress.ip_network(group.end_ip).\ compare_networks(ipaddress.ip_network(data['value']['end_ip'])) flag_e = ipaddress.ip_network(group.start_ip).\ compare_networks(ipaddress.ip_network(data['value']['start_ip'])) flag_f = ipaddress.ip_network(data['value']['end_ip']).\ compare_networks(ipaddress.ip_network(group.end_ip)) if (flag_a >= 0 and flag_b >= 0) or (flag_c >= 0 and flag_d >= 0) or (flag_e >= 0 and flag_f >= 0): logger.info("update voi group failed, ipaddress conflict") return get_error_result("IpAddressConflictError") ret = server_post("/api/v1/voi/group/update", data) if ret.get('code') != 0: logger.info("update voi group failed:%s", ret['msg']) return ret else: logger.info("update voi group success, uuid:%s", data['uuid']) return get_error_result("Success")
def hard_stop_instances(self, param): success_num = 0 failed_num = 0 desktop_uuid = param.get('desktop_uuid') logger.info("hard stop instance:%s", param['instances']) if not personal_model.YzyPersonalDesktop.objects.filter( uuid=desktop_uuid, deleted=False): logger.info( "hard stop instance failed,the desktop group not exists") return get_error_result("DesktopNotExist", name=param['desktop_name']) ret = server_post("/api/v1/instance/hard_stop", param) if ret.get('code') != 0: logger.info("hard stop instance failed:%s", ret['msg']) return ret else: success_num += ret['data']['success_num'] failed_num += ret['data']['failed_num'] logger.info( "hard stop instances success, success_num:%s, failed_num:%s", success_num, failed_num) return get_error_result("Success", data={ "failed_num": failed_num, "success_num": success_num })
def post(self, request): try: logger.info("post request: {}".format(request)) data = request.data statis_hours = data.get('statis_hours', 0) step_minutes = data.get('step_minutes', 0) node_uuid = data.get('node_uuid', "") if not (statis_hours and step_minutes and node_uuid): pass request_data = { "statis_hours": statis_hours, "step_minutes": step_minutes, "node_uuid": node_uuid } ret = server_post('/api/v1/monitor/node/get_history_perf', request_data) _data = ret.get("data", {}).copy() if _data: ret['data'].clear() ret['data'] = self.update_nic_order(_data) return JSONResponse(ret) except Exception as e: logger.error("get node history performance data error:%s", e, exc_info=True) ret = get_error_result("OtherError") return JSONResponse(ret)
def delete_resource_pool(self, uuids): logging.info("start delete resource pools: %s" % (uuids)) ret = get_error_result("Success") try: for uuid in uuids: resource_pool = YzyResourcePools.objects.filter( deleted=False).get(uuid=uuid) node_count = YzyNodes.objects.filter( deleted=False, resource_pool=resource_pool).count() image_count = YzyBaseImages.objects.filter( deleted=False, resource_pool=resource_pool).count() template_count = YzyInstanceTemplate.objects.filter( deleted=False, pool=resource_pool).count() if node_count > 0 or image_count > 0: logger.error("delete resource pool fail") return get_error_result("ResourcePoolHaveNodeDeleteFail") if template_count > 0: logger.error("delete resource pool fail") return get_error_result( "ResourcePoolHaveTemplateDeleteFail") else: ret = server_post("/api/v1/resource_pool/delete", {"uuid": uuid}) if ret.get("code", -1) != 0: return get_error_result("ResourcePoolDeleteFail") else: return get_error_result("Success") logging.info("end delete resource pools operate!") msg = "删除资源池 %s" % ('/'.join(uuids)) insert_operation_log(msg, ret['msg']) return get_error_result("Success") except Exception as e: return get_error_result("OtherError")
def check_node(self, data): """ data: { "ip": "172.16.1.49", "root_pwd" : "xxxxxx" } :param data: :return: """ logger.info("check node KVM: %s" % data) ip = data.get("ip") root_pwd = data.get("root_pwd") if not ip or not is_ip_addr(ip): logging.error("check node parameter ip error: %s" % ip) return get_error_result("IPAddrError", ipaddr=ip) # ret['msg'] = ret['msg'].format({"ipaddr": ip}) # return ret if not root_pwd: logging.error("check node parameter root_pwd not exist!") return get_error_result("ParamError") ret = server_post("/api/v1/node/check", data) if ret.get('code') != 0: logger.info("check node KVM failed:%s", ret['msg']) return ret logger.info("check node KVM success, ip: %s", ip) return ret
def delete_remote_storage(self, uuid): ret = server_post("/api/v1/remote_storage/delete", {"uuid": uuid}) if ret.get("code", -1) != 0: return get_error_result("RemoteStorageDeleteFail", reason=ret.get("msg")) else: return get_error_result("Success")
def delete_subnet(self, uuids): """ data [ {"uuid": "1a870202-3732-11ea-8a2d-000c295dd728","name": "xxxxx"}, {"uuid": "1a870202-3732-11ea-8a2d-000c295dd728","name": "xxxxx"}, {"uuid": "1a870202-3732-11ea-8a2d-000c295dd728","name": "xxxxx"} ] :param data: :return: """ ret = get_error_result("Success") # for uuid in uuids: # obj = self.get_object_by_uuid(YzySubnets, uuid) # if obj: # # 判断是否被占用 # templates = YzyInstanceTemplate.objects.filter(subnet_uuid=uuid, deleted=False).all() # if templates: # logger.error("delete subnet[%s] error, is be used"% uuid) ret = server_post("/subnet/delete", {'uuids': uuids}) # if ret.get("code", -1) != 0: # logger.error("delete subnet[%s] error: %s" % (uuid, ret)) # break # else: # logger.error("delete subnet[%s] info not exist!" % uuid) # ret = get_error_result("SubnetNotExist") msg = "删除子网 %s" % ('/'.join(uuids)) insert_operation_log(msg, ret["msg"]) return ret
def single_create_check(self, param, log_user=None): """ :param param: { "group_uuid": "d02cd368-5396-11ea-ad80-000c295dd728", "user_name": "user2", "passwd": "password", "name": "john", "phone": "13144556677", "email": "*****@*****.**", "enabled": 1 } :return: """ group_uuid = param.get('group_uuid') logger.info("create group user:%s", param['user_name']) if not education_model.YzyGroup.objects.filter(uuid=group_uuid, deleted=False): logger.error("create user failed,the group not exists") return get_error_result("GroupNotExists", name='') if personal_model.YzyGroupUser.objects.filter( user_name=param['user_name'], deleted=False): logger.error("create user failed,the user_name already exists") return get_error_result("GroupUserExists", user_name=param['user_name']) ret = server_post("/api/v1/group/user/create", param) return ret
def create_warn_setup(self, data, log_user=None): logger.info("create warn log setup record") if not data.get('status') or data['status'] == 0: return get_error_result("ParameterError") ret = server_post("/api/v1/system/warn/setup/create", data) logger.info("create warn log setup record success") return ret
def create_network(self, data): """ { "name": "test_network2", "switch_uuid" : "9c7050ba-5213-11ea-9d93-000c295dd728", "vlan_id" : 12, "subnet_info": { "name": "subnet1", "start_ip": "192.168.1.10", "end_ip": "192.168.1.20", "netmask": "255.255.255.0", "gateway": "192.168.1.0", "dns1": "8.8.8.8" } } :param data: :return: """ virtual_switch_uuid = data.get("switch_uuid") virtual_switch = self.get_object_by_uuid(YzyVirtualSwitchs, virtual_switch_uuid) if not virtual_switch: logger.error( "create data-network error: virtual switch [%s] not exist!" % virtual_switch_uuid) ret = get_error_result("VSwitchNotExist") return ret if data.get('subnet_info'): ret, status = self.check_subnet_params(data.get('subnet_info')) if not status: logger.error( "create data-network error: check subnet params fail") return ret # virtual_start_ip = data.get('subnet_info').get('start_ip') # virtual_end_ip = data.get('subnet_info').get('end_ip') # gateway = data.get('subnet_info').get('gateway') # netmask = data.get('subnet_info').get('netmask') # dns1 = data.get('subnet_info').get('dns1') # if not virtual_end_ip or not virtual_start_ip or not gateway or not netmask or not dns1: # logger.error("ParameterError") # return get_error_result("ParameterError") # _is_netmask, netmask_bits = is_netmask(netmask) # if _is_netmask: # network_num = ipaddress.ip_interface(virtual_start_ip + '/' + str(netmask_bits)).network # if ipaddress.ip_address(virtual_end_ip) not in network_num or ipaddress.ip_address(gateway) not in network_num: # logger.error("create data-network error: incorrect gateway") # ret = get_error_result("GatewayAndIpError") # return ret # if ipaddress.ip_network(virtual_start_ip).compare_networks(ipaddress.ip_network(virtual_end_ip)) >= 0: # logger.error("create data-network error: virtual start_ip:%s less than virtual! end_ip:%s or input ip in different network segments"% (virtual_end_ip, virtual_start_ip)) # ret = get_error_result("EndIPLessThanStartIP") # return ret # else: # logger.error("subnet mask error: %s"% netmask) # ret = get_error_result("SubnetMaskError") # return ret ret = server_post("/network/create", data) logger.info("create data-network server api return: %s" % ret) return ret
def default_check(self, param, log_user=None): logger.info("set the desktop %s as default", param['name']) if not voi_education_model.YzyVoiDesktop.objects.filter( uuid=param['uuid'], deleted=False): return get_error_result("DesktopNotExist", name=param['name']) ret = server_post("/api/v1/voi/desktop/education/default", param) logger.info("set voi desktop group as default end:%s", ret) return ret
def complete_install_check(self, param, log_user=None): logger.info("complete template install, uuid:%s", param['uuid']) if not education_model.YzyInstanceTemplate.objects.filter(uuid=param['uuid'], deleted=False): logger.error("complete template install error, the template not exists") return get_error_result("TemplateNotExist", name='') ret = server_post("/api/v1/template/complete_install", param) logger.info("complete template install:%s", ret) return ret
def export_users(self, param): ret = server_post("/api/v1/group/user/export", param) if ret.get('code') != 0: logger.info("export group user failed:%s", ret['msg']) return ret else: logger.info("export group user success") return ret
def reclaim_remote_storage(self, remote_storage_uuid): ret = server_post("/api/v1/remote_storage/reclaim", {"remote_storage_uuid": remote_storage_uuid}) if ret.get("code", -1) != 0: return get_error_result("RemoteStorageReclaimError", reason=ret.get("msg")) else: return get_error_result("Success")
def download_check(self, param, log_user=None): logger.info("download template") uuid = param.get('uuid') if not education_model.YzyInstanceTemplate.objects.filter(uuid=uuid, deleted=False): logger.info("download template error, it is not exists") return get_error_result("TemplateNotExist", name=param['name']) ret = server_post("/api/v1/template/download", param) logger.info("download template end:%s", ret) return ret
def get(self, request, *args, **kwargs): try: logger.info("system monitor post request") req_data = {} ret = server_post("/api/v1/index/get_voi_data", req_data) return JSONResponse(ret) except Exception as e: logger.error("Get system monitor top data error: %s", e) logger.error(''.join(traceback.format_exc())) ret = get_error_result("OtherError") return JSONResponse(ret)
def update_template(self, data, log_user=None): logger.info("update education template, name:%s, uuid:%s", data['name'], data['uuid']) if not education_model.YzyInstanceTemplate.objects.filter(uuid=data['uuid'], deleted=False): logger.info("update education template, it is not exists") return get_error_result("TemplateNotExist", name=data['name']) ret = server_post("/api/v1/template/update", data) if ret.get('code') != 0: logger.info("update education template failed:%s", ret['msg']) else: logger.info("update education template success, name:%s", data['name']) return ret
def allocate_remote_storage(self, remote_storage_uuid, resource_pool_uuid): ret = server_post( "/api/v1/remote_storage/allocate", { "remote_storage_uuid": remote_storage_uuid, 'resource_pool_uuid': resource_pool_uuid }) if ret.get("code", -1) != 0: return get_error_result("RemoteStorageAllocateError", reason=ret.get("msg")) else: return get_error_result("Success")
def update_desktop(self, data, log_user=None): logger.info("update personal desktop group, name:%s, uuid:%s", data['name'], data['uuid']) if not personal_model.YzyPersonalDesktop.objects.filter(uuid=data['uuid'], deleted=False): logger.info("update personal desktop group error, it is not exists") return get_error_result("DesktopNotExist", name=data['name']) ret = server_post("/api/v1/desktop/personal/update", data) if ret.get('code') != 0: logger.info("update personal desktop group failed:%s", ret['msg']) else: logger.info("update personal desktop group success, uuid:%s", data['name']) return ret
def delete_template(self, template): logger.info("delete template, name:%s, uuid:%s", template['name'], template['uuid']) if not education_model.YzyInstanceTemplate.objects.filter(uuid=template['uuid'], deleted=False): logger.info("delete template error, it is not exists") return get_error_result("TemplateNotExist", name=template['name']) ret = server_post("/api/v1/template/delete", {"uuid": template['uuid']}) if ret.get('code') != 0: logger.info("delete template failed:%s", ret['msg']) return ret else: logger.info("delete template success, name:%s, uuid:%s", template['name'], template['uuid']) return get_error_result("Success", {"success_num": 1, "failed_num": 0})