def update_group(self, data): group_uuid = data.get('uuid', '') group = db_api.get_group_with_first({"uuid": group_uuid}) if not group: logger.error("group: %s not exist" % group_uuid) return get_error_result("GroupNotExists", name='') if group.group_type == 1: desktop = db_api.get_desktop_with_all({'group_uuid': group_uuid}) if desktop: if 'subnet_uuid' in data['value'] and data['value'][ 'subnet_uuid'] != group.subnet_uuid: return get_error_result("GroupSubnetError") start_ip = data.get('value').get('start_ip') end_ip = data.get('value').get('end_ip') if group.start_ip != start_ip or group.end_ip != end_ip: old_terminals = db_api.get_terminal_with_all( {'group_uuid': group_uuid}) for terminal in old_terminals: terminal.group_uuid = None terminal.updated_at = datetime.datetime.utcnow() terminal.soft_update() new_terminals = db_api.get_terminal_with_all( {'group_uuid': None}) self.change_group_uuid(new_terminals, start_ip, end_ip, group_uuid) try: group.update(data['value']) group.soft_update() except Exception as e: logger.error("update group:%s failed:%s", group_uuid, e) return get_error_result("GroupUpdateError", name=group.name) logger.info("update group:%s success", group_uuid) return get_error_result("Success")
def edu_desktop_groups(self, data): """ 教学桌面组列表 """ logger.debug( "get education desktop info, terminal_id:%s, terminal_ip:%s", data.get("terminal_id", 0), data.get("terminal_ip", "")) terminal_id = data.get("terminal_id", 0) if not terminal_id: logger.error("terminal id param error: %s" % terminal_id) return build_result("ParamError") terminal_ip = data.get("terminal_ip", "") if not is_ip_addr(terminal_ip): logger.error("terminal edu desktop group info error, %s not ip" % terminal_ip) return build_result("IPAddrError", ipaddr=terminal_ip) groups = db_api.get_group_with_all( {"group_type": constants.EDUCATION_DESKTOP}) terminal_group = None for group in groups: if group.start_ip and group.end_ip: if terminal_ip in find_ips(group.start_ip, group.end_ip): terminal_group = group break _groups = list() if not terminal_group: logger.error("terminal group not exist: %s" % terminal_ip) return build_result("Success", _groups) logger.debug( "get education desktop terminal ip: %s, terminal_group: %s", terminal_ip, terminal_group.uuid) terminal_id = int(terminal_id) # 查找分组 desktops = db_api.get_desktop_with_all({ "group_uuid": terminal_group.uuid, "active": True }) logger.debug("get education desktop terminal ip: %s, desktops_len: %s", terminal_ip, len(desktops)) for desktop in desktops: # 判断是否总数大于序号 if desktop.instance_num < terminal_id: continue _d = { "uuid": desktop.uuid, "name": desktop.name, "desc": desktop.template.desc, "active": desktop.active, "order_num": desktop.order_num, "os_type": desktop.os_type } _groups.append(_d) _groups = sorted(_groups, key=lambda _groups: _groups['order_num']) logger.debug("edu terminal_ip %s desktop group list: %s" % (terminal_ip, _groups)) return build_result("Success", _groups)
def update_virtual_switch(self, data): """ 更新虚拟交换机信息 限制条件比较多 1、有模板或者桌面使用基于该虚拟机交换机的网络时,不能修改节点的接口对应关系 2、修改节点的接口对应关系时,需要删除原先创建的设备,然后重新创建新的网络设备 :param data: { "uuid": "caa5d57e-3731-11ea-801e-000c295dd728", "node_uuid": "", "nic_uuid": "" } :return: """ vswitch_uuid = data.get('uuid', '') logger.info("update vswitch %s", vswitch_uuid) vswitch = db_api.get_virtual_switch(vswitch_uuid) if not vswitch: return build_result("VSwitchNotExist") # 虚拟交换机有被使用,无法修改 networks = db_api.get_network_all({"switch_uuid": vswitch.uuid}) for network in networks: templates = db_api.get_template_with_all( {"network_uuid": network.uuid}) if templates: return build_result("VSwitchUsedError", name=vswitch.name) desktops = db_api.get_desktop_with_all( {"network_uuid": network.uuid}) if desktops: return build_result("VSwitchUsedError", name=vswitch.name) # 如果没有数据网络使用该虚拟交换机,则只需要修改对应关系 try: for uplink in vswitch.uplinks: if not uplink.deleted: if data['node_uuid'] == uplink.node_uuid: if data['nic_uuid'] != uplink.nic_uuid: logger.info("update nic from %s to %s", uplink.nic_uuid, data['nic_uuid']) node = db_api.get_node_by_uuid(data['node_uuid']) nic = db_api.get_nics_first( {'uuid': data['nic_uuid']}) for network in networks: self._delete_network(node.ip, network.uuid, network.vlan_id) self._create_network(node.ip, network.uuid, network.switch_type, nic.nic, network.vlan_id) uplink.nic_uuid = data['nic_uuid'] uplink.soft_update() break except Exception as e: return build_result("OtherError") return build_result("Success")
def delete_group(self, group_uuid): group = db_api.get_group_with_first({"uuid": group_uuid}) if not group: logger.error("group: %s not exist" % group_uuid) return get_error_result("GroupNotExists", name='') # 教学分组如果在使用中,不能删除 if constants.EDUCATION_DESKTOP == group.group_type: desktop = db_api.get_desktop_with_all({"group_uuid": group_uuid}) if desktop: logger.error("group already in use", group_uuid) return get_error_result("GroupInUse", name=group.name) # 教学分组有关联课表时,不能删除 if db_api.get_course_schedule_with_all({"group_uuid": group_uuid}): logger.error("group already in use by course_schedule", group_uuid) return get_error_result("GroupInUseByCourseSchedule", name=group.name) # 个人分组删除时需要删除分组中的用户 if constants.PERSONAL_DEKSTOP == group.group_type: users = db_api.get_group_user_with_all({'group_uuid': group.uuid}) for user in users: binds = db_api.get_instance_with_all({"user_uuid": user.uuid}) for bind in binds: bind.user_uuid = '' bind.soft_update() user.soft_delete() desktops = db_api.get_personal_desktop_with_all( {"group_uuid": group_uuid}) for desktop in desktops: desktop.group_uuid = "" desktop.soft_update() group.soft_delete() logger.info("delete group %s success", group_uuid) ret = terminal_post( "/api/v1/terminal/task", { "handler": "WebTerminalHandler", "command": "delete_group", "data": { "group_uuid": group_uuid } }) return ret
def delete_subnet(self, subnet_uuids): """ 删除子网, 批量操作 :param subnet_uuids: :return: """ prompt_info = [] try: for subnet_uuid in subnet_uuids: ### 判断子网是否已被占用 subnet = db_api.get_subnet_by_uuid(subnet_uuid) if not subnet: logger.error("subnet: %s not exist", subnet_uuid) return build_result("SubnetNotExist") templates = db_api.get_template_with_all({ 'deleted': False, 'subnet_uuid': subnet_uuid }) template_names = list( map(lambda template: template.name, templates)) if len(template_names) > 0: prompt_info.append("子网 %s 被模板 %s 所引用" % (subnet.name, ','.join(template_names))) continue groups = db_api.get_group_with_all({ 'deleted': False, 'subnet_uuid': subnet_uuid }) groups_names = list(map(lambda group: group.name, groups)) if len(groups_names) > 0: prompt_info.append("子网 %s 被分组 %s 所引用" % (subnet.name, ','.join(groups_names))) continue desktops = db_api.get_desktop_with_all({ 'deleted': False, 'subnet_uuid': subnet_uuid }) desktop_names = list( map(lambda desktop: desktop.name, desktops)) if len(desktop_names) > 0: prompt_info.append("子网 %s 被桌面 %s 所引用" % (subnet.name, ','.join(desktop_names))) continue personal_desktops = db_api.get_personal_desktop_with_all({ 'deleted': False, 'subnet_uuid': subnet_uuid }) personal_desktops_names = list( map(lambda personal_desktop: personal_desktop.name, personal_desktops)) if len(personal_desktops_names) > 0: prompt_info.append( "子网 %s 被桌面 %s 所引用" % (subnet.name, ','.join(personal_desktops_names))) continue # 判断子网是否被VOI使用 voi_templates = db_api.get_voi_template_with_all({ 'deleted': False, 'subnet_uuid': subnet_uuid }) voi_template_names = list( map(lambda template: template.name, voi_templates)) if len(voi_template_names) > 0: prompt_info.append( "子网 %s 被VOI模板 %s 所引用" % (subnet.name, ','.join(voi_template_names))) continue subnet.soft_delete() if len(prompt_info) > 0: return build_result("SubnetDeleteInfo") except Exception as e: return build_result("SubnetDeleteFail") return build_result("Success")