def delete_voi_desktop(self, desktop_uuid): """ 删除桌面组 """ desktop = db_api.get_item_with_first(models.YzyVoiDesktop, {"uuid": desktop_uuid}) if not desktop: logger.error("desktop %s not exist", desktop_uuid) return get_error_result("DesktopNotExist", name="") try: # 清除桌面组与终端的绑定关系 terminal_desktop_binds = db_api.get_item_with_all(models.YzyVoiTerminalToDesktops, {"desktop_group_uuid": desktop_uuid}) for bind in terminal_desktop_binds: # type: 0-upload, 1-download bt_task = db_api.get_item_with_first(models.YzyVoiTorrentTask, {"terminal_mac": bind.terminal_mac, "type": 1}) # status == 1, task running if bt_task and bt_task.status == 1: return get_error_result("TerminalTorrentDownloading") bind.soft_delete() logger.info("delete voi desktop, clear terminal desktop bind success!!") desktop.soft_delete() logger.info("delete voi desktop, uuid:%s, name:%s", desktop.uuid, desktop.name) if desktop.default: default_one = db_api.get_item_with_first(models.YzyVoiDesktop, {}) if default_one: default_one.default = True default_one.soft_update() logger.info("the delete desktop is default, set %s as default", default_one.name) return get_error_result("Success") except Exception as e: logger.error("delete voi desktop failed:%s", e, exc_info=True) return get_error_result("DesktopDeleteFail")
def update_desktop_sent_flag(self, data): logger.info("input data: {}".format(data)) try: terminal_mac = data.get("mac") sys_uuids = data.get("sys_disk_uuids").split(',') group_uuid = data.get("group_uuid", "") for uuid in sys_uuids: disk = db_api.get_item_with_first(models.YzyVoiDeviceInfo, { "uuid": uuid, "type": "system" }) if disk and disk.instance_uuid: desktop_group = db_api.get_item_with_first( models.YzyVoiDesktop, { "template_uuid": disk.instance_uuid, "group_uuid": group_uuid }) if desktop_group and desktop_group.uuid: db_api.update_voi_terminal_desktop_bind( desktop_group.uuid, terminal_mac, {"desktop_is_sent": 1}) logger.info( "update yzy_voi_terminal_to_desktops, mac: {}, dsk_grp_uuid: {}" .format(terminal_mac, desktop_group.uuid)) return build_result("Success") except Exception as e: logger.error("", exc_info=True) return build_result("OtherError")
def set_default_voi_desktop(self, desktop_uuid): desktop = db_api.get_item_with_first(models.YzyVoiDesktop, {'uuid': desktop_uuid}) if not desktop: logger.error("desktop %s not exist", desktop_uuid) return get_error_result("DesktopNotExist", name="") if desktop.default: return get_error_result("Success") default_one = db_api.get_item_with_first(models.YzyVoiDesktop, {'default': True}) desktop.default = True desktop.soft_update() default_one.default = False default_one.soft_update() logger.info("set default voi desktop %s success", desktop.name) return get_error_result("Success")
def update_voi_desktop(self, data): """ :param data: { "uuid": "", "value": { "name": "", ... } } :return: """ desktop_uuid = data.get('uuid', '') desktop = db_api.get_item_with_first(models.YzyVoiDesktop, {"uuid": desktop_uuid}) if not desktop: logger.error("desktop %s not exist", desktop_uuid) return get_error_result("DesktopNotExist", name="") try: if data['value'].get('ip_detail'): data['value']['ip_detail'] = json.dumps(data['value']['ip_detail']) else: data['value']['ip_detail'] = '' desktop.update(data['value']) desktop.soft_update() terminal_desktop_binds = db_api.get_item_with_all(models.YzyVoiTerminalToDesktops, {"desktop_group_uuid": desktop_uuid}) for bind in terminal_desktop_binds: bind.soft_delete() logger.info("update voi desktop, clear terminal desktop bind success!!") except Exception as e: logger.error("update voi desktop %s failed:%s", desktop_uuid, e, exc_info=True) return get_error_result("DesktopUpdateFail", name=desktop.name) logger.info("update voi desktop %s success", desktop_uuid) return get_error_result("Success")
def notify_terminal(self, desktop_group_uuid=None, group_uuid=None): # get group_uuid logger.info("desktop_group_uuid={}, group_uuid={}".format( desktop_group_uuid, group_uuid)) if not group_uuid and desktop_group_uuid: qry = db_api.get_item_with_first(models.YzyVoiDesktop, {'uuid': desktop_group_uuid}) group_uuid = qry.group_uuid if not group_uuid: logger.error('group_uuid is null, do not notify terminals') return requst_data = { "handler": "WebTerminalHandler", "command": "update_desktop_group_notify", "data": { "group_uuid": group_uuid } } ret = voi_terminal_post("/api/v1/voi/terminal/command/", requst_data) if ret.get("code", -1) != 0: logger.error("voi_terminal_post request: {}, ret: {}".format( requst_data, ret)) return ret logger.info("voi_terminal_post request: {}, ret: {}".format( requst_data, ret))
def inactive_voi_desktop(self, desktop_uuid): desktop = db_api.get_item_with_first(models.YzyVoiDesktop, {'uuid': desktop_uuid}) if not desktop: logger.error("desktop %s not exist", desktop_uuid) return get_error_result("DesktopNotExist", name="") desktop.active = False desktop.soft_update() logger.info("inactive voi desktop %s success", desktop.name) return get_error_result("Success")
def update_terminal_desktop_bind(self, data): """ 'new_group_uuid','terminal_mac' """ logger.info("update desktop_status data: {}".format(data)) try: desktop_group_uuid = data.get('desktop_group_uuid', None) terminal_mac = data.get('mac', None) cmd = data.get("cmd", None) desktop_ip_info = { "desktop_is_dhcp": data.get("is_dhcp", 0), "desktop_ip": data.get("ip", ""), "desktop_mask": data.get("netmask", ""), "desktop_gateway": data.get("gateway", ""), "desktop_dns1": data.get("dns1", ""), "desktop_dns2": data.get("dns2", "") } if cmd == "login" and desktop_group_uuid: update_data = { "desktop_group_uuid": desktop_group_uuid, "terminal_mac": terminal_mac, "desktop_status": 1 } # get desktop_group ip_detail, if is dhcp, then update ip info desktop_group = db_api.get_item_with_first( models.YzyVoiDesktop, {'uuid': desktop_group_uuid}) if desktop_group and not desktop_group.use_bottom_ip and desktop_group.ip_detail: ip_detail = json.loads(desktop_group.ip_detail) is_dhcp = ip_detail.get('auto', False) if is_dhcp: update_data.update(desktop_ip_info) logger.info('update table data: {}'.format(update_data)) db_api.update_voi_terminal_desktop_info(update_data) elif cmd == "logout" and not desktop_group_uuid: update_data = { "terminal_mac": terminal_mac, "desktop_status": 0 } logger.info('update table data: {}'.format(update_data)) db_api.update_voi_terminal_desktop_info(update_data) else: logger.error("request message error: {}".format(data)) return build_result("MessageError") return build_result("Success") except Exception as e: logger.error("", exc_info=True) return build_result("OtherError")
def terminal_desktop_bind(self, data): logger.info("terminal desktop bind data: {}".format(data)) try: terminal_uuid = data.get("terminal_uuid", "") desktop_uuid = data.get("desktop_uuid", "") desktop = db_api.get_item_with_first(models.YzyVoiDesktop, {"uuid": desktop_uuid}) if not desktop: logger.error("terminal desktop bind desktop not exist: %s", desktop_uuid) return build_result("VOIDesktopGroupNotExist") bind_info = { "uuid": create_uuid(), "terminal_uuid": terminal_uuid, "desktop_uuid": desktop_uuid } db_api.create_voi_terminal_desktop_bind(bind_info) logger.info( "terminal desktop bind data: {} success".format(bind_info)) return build_result("Success") except Exception as e: logger.error("", exc_info=True) return build_result("OtherError")
def create_voi_desktop(self, data): """ 创建教学桌面组 :param data: { "name": "desktop2", "owner_id": "", "group_uuid": "d02cd368-5396-11ea-ad80-000c295dd728", "template_uuid": "6f1006c0-56d1-11ea-aec0-000c295dd728", "sys_restore": 1, "data_restore": 1, "prefix": "pc", "postfix": 3, "postfix_start": 5, "show_info": true, "auto_update": true } :return: """ if not self._check_params(data): return get_error_result("ParamError") group = db_api.get_item_with_first(models.YzyVoiGroup, {"uuid": data['group_uuid']}) if not group: logger.error("voi group: %s not exist", data['group_uuid']) return get_error_result("GroupNotExists", name="") template = db_api.get_item_with_first(models.YzyVoiTemplate, {"uuid": data['template_uuid']}) if not template: logger.error("voi template: %s not exist", data['template_uuid']) return get_error_result("TemplateNotExist") has_group = db_api.get_item_with_first(models.YzyVoiDesktop, {}) # if constants.PERSONAL_DEKSTOP == template.classify: # return get_error_result("TemplatePersonalError", name=template.name) # add desktop desktop_uuid = create_uuid() desktop_value = { "uuid": desktop_uuid, "owner_id": data['owner_id'], "name": data['name'], "group_uuid": data['group_uuid'], "template_uuid": data['template_uuid'], "os_type": template.os_type, "sys_restore": data['sys_restore'], "data_restore": data['data_restore'], "sys_reserve_size": data['sys_reserve_size'], "data_reserve_size": data['data_reserve_size'], "prefix": data['prefix'], "diff_mode": data.get("diff_mode", 0), "use_bottom_ip": data.get('use_bottom_ip', True), "ip_detail": json.dumps(data['ip_detail']) if data.get('ip_detail') else '', # "postfix": data.get('postfix', 1), # "postfix_start": data.get('postfix_start', 1), "active": True, # 默认激活 "default": False if has_group else True, "show_info": data.get('show_info', False), "auto_update": data.get('auto_update', False) } try: db_api.create_voi_desktop(desktop_value) logger.info("create voi desktop %s success", data['name']) except Exception as e: logging.info("insert voi desktop info to db failed:%s", e) return get_error_result("DesktopCreateFail", name=data['name']) return get_error_result("Success")
def update_share_disk(self, data): """ 更新共享数据盘 { "uuid": "xxxxxxxxxx", "enable": 0, "disk_size": 8, "restore": 1, "share_desktop": [ {"uuid": "xxxxxxx", "name": "xxxxx", "choice": 0}, {"uuid": "xxxxxxx", "name": "xxxxx", "choice": 0} ] } :param data: :return: """ """ { "command": "create_share", "handler": "VoiHandler", "data": { "disk_info": { 'uuid': '2f110de8-78d8-11ea-ad5d-000c29e84b9c', 'base_path': '/opt/slow/instances' } "version": 0 } } """ logger.info("terminal share disk update data: {}".format(data)) try: # disk_uuid = create_uuid() version = 0 disk_uuid = data["uuid"] sys_base, data_base = self._get_template_storage() share_disk = db_api.get_item_with_first( models.YzyVoiTerminalShareDisk, {"uuid": disk_uuid}) disk_info = dict() disk_info["uuid"] = share_disk.uuid disk_info["base_path"] = sys_base['path'] disk_info["size"] = data["disk_size"] # 判断是否大小有更新 if data["disk_size"] != share_disk.disk_size: # 需要重新删除创建 # pass node = db_api.get_controller_node() delete_command = { "command": "delete_share", "handler": "VoiHandler", "data": { "disk_info": { "uuid": share_disk.uuid, "base_path": sys_base['path'], }, "version": version } } ret = compute_post(node.ip, delete_command) if ret.get("code", -1) != 0: logger.error( "terminal share disk update fail, delete old fail") return build_result("ShareDiskUpdateFail") # 创建新的容量盘 command_data = { "command": "create_share", "handler": "VoiHandler", "data": { "disk_info": disk_info, "version": version } } ret_json = compute_post(node.ip, command_data) if ret_json.get("code", -1) != 0: logger.error( "terminal share disk update fail, create new fail") return build_result("ShareDiskUpdateFail") share_disk.disk_size = data["disk_size"] share_disk.version += 1 # todo 维护桌面组的绑定关系 # import pdb; pdb.set_trace() desktops = db_api.get_item_with_all( models.YzyVoiDesktop, {"group_uuid": share_disk.group_uuid}) desktop_binds = db_api.get_item_with_all( models.YzyVoiShareToDesktops, {"disk_uuid": disk_uuid}) share_desktops = data["share_desktop"] copy_share_desktops = share_desktops[:] for desktop in share_desktops: # is_exist = False for bind in desktop_binds: if desktop["uuid"] == bind.desktop_uuid: # is_exist = True if not int(desktop.get("choice", 0)): bind.soft_delete() copy_share_desktops.remove(desktop) insert_binds = list() if copy_share_desktops: for desktop in copy_share_desktops: if desktop["choice"]: for _d in desktops: if desktop["uuid"] == _d.uuid: insert_binds.append({ "uuid": create_uuid(), "group_uuid": share_disk.group_uuid, "disk_uuid": disk_uuid, "desktop_uuid": desktop["uuid"], "desktop_name": desktop["name"] }) # 更新数据库绑定记录 if insert_binds: db_api.insert_with_many(models.YzyVoiShareToDesktops, insert_binds) # 更新数据库记录 share_disk.restore = data["restore"] share_disk.enable = data["enable"] share_disk.soft_update() # todo 生成bt种子 # 生成种子文件 task = Thread(target=self.create_share_disk_torrent, args=(disk_info, version)) task.start() logger.info( "update terminal voi share disk data: {} success".format( share_disk)) return build_result("Success") except Exception as e: logger.error("", exc_info=True) return build_result("OtherError")
def create_terminal_desktop_bind_bak(self, data): """ 'group_uuid','terminal_uuid','terminal_id','mac', 'ip','mask','gateway','dns1','dns2','is_dhcp', """ logger.info("create terminal desktop bind data: {}".format(data)) try: terminal_uuid = data.get("terminal_uuid", "") terminal_id = data.get("terminal_id", "") group_uuid = data.get("group_uuid", "") terminal_mac = data.get("mac", "") bind_info = {} # group_uuid != input group_uuid, then delete all old data db_api.delete_voi_terminal_desktops(group_uuid, terminal_uuid) # get all desktop groups qry_desktop_groups = db_api.get_item_with_all( models.YzyVoiDesktop, {"group_uuid": group_uuid}) # 查找所有该终端、桌面组对应关系 for qry in qry_desktop_groups: desktop_group_uuid = qry.uuid # one (desktop_group_uuid, terminal_id) only one row qry_exists = db_api.get_item_with_first( models.YzyVoiTerminalToDesktops, { "desktop_group_uuid": desktop_group_uuid, "terminal_uuid": terminal_uuid }) if qry_exists: logger.info('continue: {}'.format(terminal_mac)) continue logger.info('desktop_group_uuid: {}, qry.ip_detail: {}'.format( desktop_group_uuid, qry.ip_detail)) desktop_is_dhcp = 0 if qry.ip_detail: ip_detail = json.loads(qry.ip_detail) desktop_is_dhcp = int(ip_detail.get("auto", True)) desktop_start_ip = ip_detail.get("start_ip", "") desktop_mask = ip_detail.get("netmask", "") desktop_gateway = ip_detail.get("gateway", "") desktop_dns1 = ip_detail.get("dns_master", "") desktop_dns2 = ip_detail.get("dns_slave", "") desktop_ip_info = {} if bool(qry.use_bottom_ip): logger.info( 'desktop_group_uuid: {}, qry.use_bottom_ip: {}'.format( desktop_group_uuid, qry.use_bottom_ip)) desktop_ip_info = { "desktop_is_dhcp": data.get("is_dhcp", ""), "desktop_ip": data.get("ip", ""), "desktop_mask": data.get("mask", ""), "desktop_gateway": data.get("gateway", ""), "desktop_dns1": data.get("dns1", ""), "desktop_dns2": data.get("dns2", ""), } elif desktop_is_dhcp: logger.info( 'desktop_group_uuid: {}, desktop_is_dhcp: {}'.format( desktop_group_uuid, desktop_is_dhcp)) desktop_ip_info = { "desktop_is_dhcp": 1, "desktop_ip": "", "desktop_mask": "", "desktop_gateway": "", "desktop_dns1": "", "desktop_dns2": "", } else: logger.info('desktop_group_uuid: {}, static ip'.format( desktop_group_uuid)) # get ip pool use start_ip and netmask, filter yzy_voi_terminal_desktops ips, get least ip netmask_bits = netaddr.IPAddress( desktop_mask).netmask_bits() network = ipaddress.ip_interface(desktop_start_ip + '/' + str(netmask_bits)).network original_ip_pool = [ x for x in network.hosts() if x >= ipaddress.IPv4Address(desktop_start_ip) ] # get used ip in this desktop_group_uuid qry_ips = db_api.get_item_with_all( models.YzyVoiTerminalToDesktops, {"desktop_group_uuid": desktop_group_uuid}) used_ip_pool = [ ipaddress.IPv4Address(qry.desktop_ip) for qry in qry_ips ] available_ip_pool = [ ip for ip in original_ip_pool if ip not in used_ip_pool ] if available_ip_pool: desktop_ip = min(available_ip_pool).compressed desktop_ip_info = { "desktop_is_dhcp": 0, "desktop_ip": desktop_ip, "desktop_mask": desktop_mask, "desktop_gateway": desktop_gateway, "desktop_dns1": desktop_dns1, "desktop_dns2": desktop_dns2, } else: # use use_bottom_ip logger.info( 'desktop_group_uuid: {}, no availabel_ip'.format( desktop_group_uuid)) desktop_ip_info = { "desktop_is_dhcp": data.get("is_dhcp", ""), "desktop_ip": data.get("ip", ""), "desktop_mask": data.get("mask", ""), "desktop_gateway": data.get("gateway", ""), "desktop_dns1": data.get("dns1", ""), "desktop_dns2": data.get("dns2", ""), } bind_info = { "uuid": create_uuid(), "group_uuid": group_uuid, "terminal_uuid": terminal_uuid, "desktop_group_uuid": desktop_group_uuid, "terminal_mac": terminal_mac, } bind_info.update(desktop_ip_info) db_api.create_voi_terminal_desktop_bind(bind_info) logger.info( "terminal desktop bind data: {} success".format(bind_info)) return build_result("Success") except Exception as e: logger.error("", exc_info=True) return build_result("OtherError")