Exemplo n.º 1
0
    def virtual_switch_info(self, vswitch_uuid):
        """
        虚拟交换机的信息
        :param vswitch_uuid:
        :return:
        """
        virtual_switch = db_api.get_virtual_switch(vswitch_uuid)
        if not virtual_switch:
            logger.error("virtual switch: %s not exist", vswitch_uuid)
            return build_result("VSwitchNotExist")

        info = {
            "name": virtual_switch.name,
            "type": virtual_switch.type,
            "default": virtual_switch.default,
            "desc": virtual_switch.desc or '',
            "uplinks": list()
        }
        for uplink in virtual_switch.uplinks:
            if not uplink.deleted:
                node = db_api.get_node_by_uuid(uplink.node_uuid)
                if node:
                    uplink_info = {
                        "hostname": node.hostname,
                        "interface": uplink.interface
                    }
                    info['uplinks'].append(uplink_info)
        return build_result("Success", info)
Exemplo n.º 2
0
    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")
Exemplo n.º 3
0
 def get_vswitch_list(self):
     vswitch_list = list()
     vswitchs = db_api.get_virtual_switch_list({})
     for virtual_switch in vswitchs:
         info = {
             "name": virtual_switch.name,
             "type": virtual_switch.type,
             "default": virtual_switch.default,
             "desc": virtual_switch.desc or '',
             "uplinks": list()
         }
         for uplink in virtual_switch.uplinks:
             if not uplink.deleted:
                 node = db_api.get_node_by_uuid(uplink.node_uuid)
                 uplink_info = {
                     "hostname": node.hostname,
                     "interface": uplink.interface
                 }
                 info['uplinks'].append(uplink_info)
         vswitch_list.append(info)
     ret = {"vswitch_list": vswitch_list}
     return build_result("Success", ret)
Exemplo n.º 4
0
    def delete_network(self, network_uuid):
        network = db_api.get_network_by_uuid(network_uuid)
        if not network:
            logger.error("network [%s] info not exist", network_uuid)
            return build_result("NetworkInfoNotExist")

        virtual_switch = network.virtual_switch_of_network
        if not virtual_switch or virtual_switch.deleted:
            logger.error("network [%s] not virtual switch" % network_uuid)
            return build_result("NetworkNotVSError")

        template = db_api.get_template_with_all({'network_uuid': network_uuid})
        if template:
            logger.error("network is in use, can not deleted")
            return build_result("NetworkInUse", name=network.name)

        network_type = virtual_switch.type
        if constants.VLAN_NETWORK_TYPE == network_type:
            vlan_id = network.vlan_id
        else:
            vlan_id = None

        for uplink in virtual_switch.uplinks:
            if not uplink.deleted:
                node = db_api.get_node_by_uuid(uplink.node_uuid)
                try:
                    self._delete_network(node.ip, network_uuid, vlan_id)
                except Exception as e:
                    logger.error("delete network in node %s failed:%s",
                                 node.ip,
                                 e,
                                 exc_info=True)
                    # return build_result("NetworkCreateFail")

        subnets = db_api.get_subnet_by_network(network.uuid)
        for subnet in subnets:
            subnet.soft_delete()
        network.soft_delete()
        return build_result("Success")
Exemplo n.º 5
0
def update_ha_master():
    is_vip, is_master, is_backup = False, False, False
    current_ip = None
    ha_info_obj = db_api.get_ha_info_first()
    if ha_info_obj:
        # logger.info("ha_info before monitor update: %s" % ha_info_obj.dict())

        # 获取本机所有启用网口的ip,查看本节点的ip在ha_info表中是master_ip还是backup_ip
        code, out = cmdutils.run_cmd(
            """ip -br a |grep ' UP ' |grep -o '[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*'""",
            ignore_log=True)
        if code != 0:
            logger.error(out)
        else:
            ip_list = [ip for ip in out.strip('\n').split('\n')]
            for ip in ip_list:
                if ip == ha_info_obj.vip:
                    is_vip = True
                elif ip == ha_info_obj.master_ip:
                    is_master = True
                elif ip == ha_info_obj.backup_ip:
                    is_backup = True

            if not is_vip:
                logger.error("server running without vip[%s]" %
                             ha_info_obj.vip)
            else:
                if not is_master and not is_backup:
                    logger.error(
                        "server running without master_ip[%s], backup_ip[%s]" %
                        (ha_info_obj.master_ip, ha_info_obj.backup_ip))
                elif is_master and is_backup:
                    logger.error(
                        "server running with both master_ip[%s], backup_ip[%s]"
                        % (ha_info_obj.master_ip, ha_info_obj.backup_ip))
                elif is_master and not is_backup:
                    current_ip = ha_info_obj.master_ip
                elif not is_master and is_backup:
                    # 如果发现本节点的ip在ha_info表中是backup_ip,说明notify.sh脚本中调用server服务/node/master接口去更新数据库的操作失败了
                    # 检查并修正ha_info表中的ip
                    current_ip = ha_info_obj.backup_ip
                    ha_info_obj.master_ip, ha_info_obj.backup_ip = ha_info_obj.backup_ip, ha_info_obj.master_ip
                    logger.info("update ha_info[%s] master_ip from %s to %s",
                                (ha_info_obj.uuid, ha_info_obj.backup_ip,
                                 ha_info_obj.master_ip))

                if current_ip:
                    # current_ip所在节点应该为master,检查并修正ha_info表中node_uuid、nic、nic_uuid
                    current_node_obj = db_api.get_node_by_ip(current_ip)
                    if current_node_obj.uuid == ha_info_obj.backup_uuid:
                        ha_info_obj.master_uuid, ha_info_obj.backup_uuid = ha_info_obj.backup_uuid, ha_info_obj.master_uuid
                        logger.info(
                            "update ha_info[%s] master_uuid from %s to %s",
                            (ha_info_obj.uuid, ha_info_obj.backup_uuid,
                             ha_info_obj.master_uuid))
                    current_ip_obj = db_api.get_nic_ip_by_ip(current_ip)
                    if current_ip_obj.nic_uuid == ha_info_obj.backup_nic_uuid:
                        ha_info_obj.master_nic_uuid, ha_info_obj.backup_nic_uuid = ha_info_obj.backup_nic_uuid, ha_info_obj.master_nic_uuid
                        logger.info(
                            "update ha_info[%s] master_nic_uuid from %s to %s",
                            (ha_info_obj.uuid, ha_info_obj.backup_nic_uuid,
                             ha_info_obj.master_nic_uuid))
                    if ha_info_obj.master_nic != ha_info_obj.backup_nic and current_ip_obj.name == ha_info_obj.backup_nic:
                        ha_info_obj.master_nic, ha_info_obj.backup_nic = ha_info_obj.backup_nic, ha_info_obj.master_nic
                        logger.info(
                            "update ha_info[%s] master_nic from %s to %s",
                            (ha_info_obj.uuid, ha_info_obj.backup_nic,
                             ha_info_obj.master_nic))
                    ha_info_obj.soft_update()

                    # 检查并修正backup_uuid节点的type
                    real_backup_node_obj = db_api.get_node_by_uuid(
                        ha_info_obj.backup_uuid)
                    if real_backup_node_obj.type not in [
                            constants.ROLE_SLAVE_AND_COMPUTE,
                            constants.ROLE_SLAVE
                    ]:
                        wrong_type = real_backup_node_obj.type
                        if real_backup_node_obj.type in [
                                constants.ROLE_MASTER_AND_COMPUTE,
                                constants.ROLE_COMPUTE
                        ]:
                            real_backup_node_obj.type = constants.ROLE_SLAVE_AND_COMPUTE
                        else:
                            real_backup_node_obj.type = constants.ROLE_SLAVE
                        real_backup_node_obj.soft_update()
                        logger.info(
                            "update real_backup_node[%s] role from %s to %s",
                            real_backup_node_obj.ip, wrong_type,
                            real_backup_node_obj.type)

                    # 检查并修正master_uuid节点的type
                    if current_node_obj.type not in [
                            constants.ROLE_MASTER,
                            constants.ROLE_MASTER_AND_COMPUTE
                    ]:
                        wrong_type = current_node_obj.type
                        if wrong_type in [
                                constants.ROLE_SLAVE_AND_COMPUTE,
                                constants.ROLE_COMPUTE
                        ]:
                            current_node_obj.type = constants.ROLE_MASTER_AND_COMPUTE
                        else:
                            current_node_obj.type = constants.ROLE_MASTER
                        current_node_obj.soft_update()
                        logger.info(
                            "update current_node[%s] role from %s to %s",
                            current_node_obj.ip, wrong_type,
                            current_node_obj.type)

                    # 检查并修正yzy_template、yzy_voi_template表的模板宿主机uuid
                    templates = db_api.get_template_with_all({})
                    for template in templates:
                        if constants.SYSTEM_DESKTOP == template.classify:
                            continue
                        if template.host_uuid != current_node_obj.uuid:
                            template.host_uuid = current_node_obj.uuid
                            template.soft_update()
                            logger.info("update template %s host_uuid to %s",
                                        template.name, current_node_obj.uuid)

                    voi_templates = db_api.get_voi_template_with_all({})
                    for template in voi_templates:
                        if constants.SYSTEM_DESKTOP == template.classify:
                            continue
                        if template.host_uuid != current_node_obj.uuid:
                            template.host_uuid = current_node_obj.uuid
                            template.soft_update()
                            logger.info(
                                "update voi template %s host_uuid to %s",
                                template.name, current_node_obj.uuid)
Exemplo n.º 6
0
    def create_network(self, data):
        """
        创建数据网络
        :param data:
            {
                "name": "network1",
                "switch_uuid": "570ddad8-27b5-11ea-a53d-562668d3ccea",
                "vlan_id": 10,
                "subnet_info": {
                    "subnet_name": "default",
                    "start_ip": "172.16.1.10",
                    "end_ip": "172.16.1.20",
                    "netmask": "255.255.0.0",
                    "gateway": "172.16.1.254",
                    "dns1": "8.8.8.8",
                    "dns2": ""
                }
            }
        :return:
        """
        if not data:
            return build_result("ParamError")

        network_info = db_api.get_network_by_name(data['name'])
        if network_info:
            logger.error("network name : %s repeat", data['name'])
            return build_result("NetworkNameRepeatError", name=data['name'])

        virtual_switch = db_api.get_virtual_switch(data['switch_uuid'])
        if not virtual_switch:
            logger.error("not virtual switch : %s", data['switch_uuid'])
            return build_result("VSwitchNotExist")
        network_type = virtual_switch.type
        if constants.VLAN_NETWORK_TYPE == network_type:
            vlan_id = data.get('vlan_id', 1)
            if not check_vlan_id(str(vlan_id)):
                return build_result("VlanIDError", vid=vlan_id)
        else:
            vlan_id = None
            net = db_api.get_network_all({'switch_uuid': virtual_switch.uuid})
            if net:
                return build_result("VSwitchFlatInUse")

        network_uuid = create_uuid()
        for uplink in virtual_switch.uplinks:
            if not uplink.deleted:
                node = db_api.get_node_by_uuid(uplink.node_uuid)
                nic = db_api.get_nics_first({"uuid": uplink.nic_uuid})
                try:
                    self._create_network(node.ip, network_uuid,
                                         virtual_switch.type, nic.nic, vlan_id)
                except Exception as e:
                    logger.error("NetworkCreateFail: %s" % e, exc_info=True)
                    # 虚拟机启动会检测网桥并且进行添加,所以这里创建失败无所谓
                    # return build_result("NetworkCreateFail", name=data['name'])

        network_value = {
            "uuid": network_uuid,
            "name": data['name'],
            "switch_name": virtual_switch.name,
            "switch_uuid": virtual_switch.uuid,
            "switch_type": virtual_switch.type,
            "vlan_id": vlan_id
        }
        logger.info("add network info in db")
        db_api.add_network(network_value)
        if data.get('subnet_info'):
            data['subnet_info']['network_uuid'] = network_uuid
            subnet_info = self._generate_subnet_info(data['subnet_info'])
            db_api.add_subnet(subnet_info)
        return build_result("Success")