Esempio n. 1
0
def _change_migrate_host(host_id_d, ins_id_s):
    '''
        当迁移失败后将instance_dsthost这条记录删除
        或者迁移成功后将instance_srchost记录删除
    :param host_id_d: 热迁移目标host的id
    :param ins_id_s:
    :return:
    '''
    where_data = {'host_id': host_id_d, 'instance_id': ins_id_s}
    return ins_h_s.InstanceHostService().delete_instance_host(where_data)
Esempio n. 2
0
def _change_migrate_host(host_id_s, host_name_s, ins_id_s):
    '''
        当迁移失败后将host修改回源host
    :param host_id_s:
    :param host_name_s:
    :param ins_id_s:
    :return:
    '''
    update_data = {
        'host_id': host_id_s,
        'host_name': host_name_s,
        'updated_at': get_datetime_str()
    }
    where_data = {'isdeleted': '0', 'instance_id': ins_id_s}
    ret = ins_h_s.InstanceHostService().update_instance_host_info(
        update_data, where_data)
    if ret != 1:
        logging.error(
            'update instance host info error when cold migrate, update_data:%s, where_data:%s',
            update_data, where_data)
def instance_hot_migrate(instance_id, host_id):
    '''
        虚拟机迁移
    :param instance_id:
    :param host_id:
    :return:
    '''
    speed_limit = request.values.get('speed_limit')
    if not instance_id or not host_id or not speed_limit or int(
            speed_limit) < 0:
        logging.info('the params is invalid when migrate instance')
        return json_helper.format_api_resp(code=ErrorCode.PARAM_ERR,
                                           msg="参数错误")

    host_data_s = ins_s.get_host_of_instance(instance_id)
    if not host_data_s:
        logging.error(
            'instance %s of host is not exist in db when migrate instance',
            str(instance_id))
        return json_helper.format_api_resp(code=ErrorCode.SYS_ERR)

    host_data_d = host_s.HostService().get_host_info(host_id)
    if not host_data_d:
        logging.error(
            'target host %s is not exist in db when migrate instance',
            str(instance_id))
        return json_helper.format_api_resp(code=ErrorCode.SYS_ERR)

    if host_data_d['typestatus'] == HostTypeStatus.LOCK or host_data_d[
            'typestatus'] == HostTypeStatus.MAINTAIN:
        logging.error(
            'target host %s is in invalid status %s when migrate instance',
            host_id, host_data_d['typestatus'])
        return json_helper.format_api_resp(code=ErrorCode.SYS_ERR,
                                           msg='不能迁移到锁定或维护的主机上')

    ins_flavor_data = ins_s.get_flavor_of_instance(instance_id)
    if not ins_flavor_data:
        logging.error('hot migrate can not get instance %s flavor info' %
                      str(instance_id))
        return json_helper.format_api_resp(code=ErrorCode.SYS_ERR,
                                           msg='无法获取被迁移虚拟机的基础配置信息')

    ins_group = ins_g_s.InstanceGroupService().get_instance_group_info(
        instance_id)
    if not ins_group:
        return json_helper.format_api_resp(code=ErrorCode.SYS_ERR,
                                           msg="无法获取虚拟机所在应用组信息")

    data_disk_status, data_disk_size = ins_s.get_data_disk_size_of_instance(
        instance_id)
    if not data_disk_status:
        return json_helper.format_api_resp(code=ErrorCode.SYS_ERR,
                                           msg="无法获取被迁移虚拟机磁盘配置信息")
    instance_disk_size = int(
        ins_flavor_data['root_disk_gb']) + int(data_disk_size)

    # 获取物理机所在资源池可用物理机数量
    all_hosts_nums, all_hosts_data = host_s.HostService(
    ).get_available_hosts_of_hostpool(host_data_d["hostpool_id"])
    if all_hosts_nums < 1:
        return False, '集群物理机资源不足,无法满足虚拟机迁移'

    host_data_d_before_match = []
    host_data_d_before_match.append(host_data_d)

    # 过滤host
    # 这里不核对host的cpu型号
    hosts_after_filter = host_s_s.migrate_filter_hosts(
        host_data_d_before_match, int(all_hosts_nums))
    if len(hosts_after_filter) == 0:
        logging.info('no available host when get migrate host')
        return json_helper.format_api_resp(code=ErrorCode.SYS_ERR,
                                           msg="集群内其他物理机cpu、内存使用率超过阀值,暂时不能迁移")

    # VM分配给HOST看是否满足迁移
    vm = {
        "vcpu": ins_flavor_data['vcpu'],
        "mem_MB": ins_flavor_data['memory_mb'],
        "disk_GB": instance_disk_size,
    }
    host_after_match = host_s_s.migrate_match_hosts(hosts_after_filter,
                                                    vm,
                                                    ins_group['group_id'],
                                                    least_host_num=1,
                                                    max_disk=2000)
    if len(host_after_match) == 0:
        logging.info('no available host when get migrate host')
        return json_helper.format_api_resp(
            code=ErrorCode.SYS_ERR, msg="集群内其他物理机cpu、内存资源不足或者应用互斥,暂时不能迁移")

    # 不能本身
    if host_data_s['id'] == host_id:
        logging.error('no allow migrate to the same host %s', host_id)
        return json_helper.format_api_resp(code=ErrorCode.SYS_ERR,
                                           msg="不能迁移到原主机上")

    ins_data_s = ins_s.InstanceService().get_instance_info(instance_id)
    if not ins_data_s:
        logging.error('instance %s is not exist in db when migrate instance')
        return json_helper.format_api_resp(code=ErrorCode.SYS_ERR)

    # 排除非法状态,vm只能在开机状态下才能热迁移
    if ins_data_s['status'] != VMStatus.STARTUP or ins_data_s[
            'typestatus'] != VMTypeStatus.NORMAL:
        logging.error(
            'instance status %s, typestatus %s is invalid when migrate instance',
            ins_data_s['status'], ins_data_s['typestatus'])
        return json_helper.format_api_resp(code=ErrorCode.SYS_ERR,
                                           msg='只能在关机状态下执行热迁移')

    # 检测目标主机是否有迁入VM
    if not _check_has_migrate(host_id):
        logging.error(
            'dest host %s has other migrating instance when migrate instance',
            host_id)
        return json_helper.format_api_resp(code=ErrorCode.SYS_ERR,
                                           msg='目标主机现有正在迁入的虚机,不能迁移到该主机')

    # 将VM状态改为热迁移中
    update_data = {
        'status': VMStatus.MIGRATE,  # 热迁移中
        'updated_at': get_datetime_str()
    }
    where_data = {'id': instance_id}
    ret = ins_s.InstanceService().update_instance_info(update_data, where_data)
    if ret != 1:
        logging.error(
            'update instance status error when cold migrate, update_data:%s, where_data:%s',
            update_data, where_data)
        return json_helper.format_api_resp(code=ErrorCode.SYS_ERR)
    ins_data = ins_s.InstanceService().get_instance_info(instance_id)

    # 新增一个instance_dsthost记录,dsthost为目标host的ip
    # 迁移过程中失败则删除这条新增的Instance_dsthost记录,迁移成功之后则删除Instance_srchost记录
    # 迁移过程中前端vm页面不会显示这条新增的记录
    instance_host_data = {
        'instance_id': instance_id,
        'instance_name': ins_data['name'],
        'host_id': host_id,
        'host_name': host_data_d['name'],
        'isdeleted': '0',
        'created_at': get_datetime_str()
    }
    ret_h = ins_h_s.InstanceHostService().add_instance_host_info(
        instance_host_data)
    if ret_h.get('row_num') <= 0:
        logging.error(
            'add instance host info error when hot migrate, %instance_id,%host_id'
        )
        return json_helper.format_api_resp(code=ErrorCode.SYS_ERR)

    # 在instance_migrate表中增加一条数据
    insert_data = {
        'instance_id': instance_id,
        'src_host_id': host_data_s['id'],
        'dst_host_id': host_id,
        'migrate_status': MigrateStatus.DOING,
        'created_at': get_datetime_str()
    }
    ret_m = ins_m_s.InstanceMigrateService().add_instance_migrate_info(
        insert_data)
    if ret_m.get('row_num') <= 0:
        logging.error(
            'add instance migrate info error when cold migrate, insert_data:%s',
            insert_data)
        return json_helper.format_api_resp(code=ErrorCode.SYS_ERR)

    # 发送异步消息到队列
    data = {
        "routing_key": "INSTANCE.HOTMIGRATE",
        "send_time": get_datetime_str(),
        "data": {
            "request_id": ins_s.generate_req_id(),
            "user_id": get_user()['user_id'],
            "migrate_tab_id": ret_m.get('last_id'),
            "task_id": ins_s.generate_task_id(),
            "speed_limit": speed_limit,
            "ins_data_s": {
                "id": ins_data_s['id'],
                "uuid": ins_data_s['uuid'],
                "name": ins_data_s['name']
            },
            "host_data_d": {
                "id": host_data_d['id'],
                "name": host_data_d['name'],
                "ipaddress": host_data_d['ipaddress']
            },
            "host_data_s": {
                "id": host_data_s['id'],
                "name": host_data_s['name'],
                "ipaddress": host_data_s['ipaddress'],
                "sn": host_data_s['sn']
            }
        }
    }

    # todo:这里有可能发送不成功
    ret_kafka = send_async_msg(KAFKA_TOPIC_NAME, data)

    return json_helper.format_api_resp(code=ErrorCode.SUCCESS)
Esempio n. 4
0
def _create_instance_info(task_id, instance_name, app_info, owner, password,
                          flavor_id, group_id, vm_host, flavor_info,
                          image_data, ip_data, vm_disk_gb, mount_point,
                          instance_system, net_area_id, segment_data, vm_env,
                          user_id):
    uuid = randomUUID()
    request_id = ins_s.generate_req_id()
    # 往instance表添加记录
    logging.info(
        '创建VM 步骤10-1:插入instance表 task %s : insert instance table start when create instance',
        task_id)
    instance_data = {
        'uuid': uuid,
        'name': instance_name,
        'displayname': instance_name,
        'description': '',
        'status': VMStatus.CREATING,
        'typestatus': VMTypeStatus.NORMAL,
        'create_source': VMCreateSource.CLOUD_SOURCE,
        'isdeleted': '0',
        'app_info': app_info,
        'owner': owner,
        'created_at': get_datetime_str(),
        'password': encrypt_helper.encrypt(str(password)),  # 密码加密
        'request_id': request_id,
        'task_id': task_id
    }
    ret = ins_s.InstanceService().add_instance_info(instance_data)
    if ret.get('row_num') <= 0:
        logging.error(
            'task %s : add instance info error when create instance, insert_data: %s',
            task_id, instance_data)
        return json_helper.format_api_resp(code=ErrorCode.SYS_ERR)
    logging.info(
        '创建VM 步骤10-1:插入instance表成功 '
        'task %s : insert instance table successful when create instance',
        task_id)

    instance_id = ret.get('last_id')

    # 往instance_flavor表添加记录
    logging.info(
        '创建VM 步骤10-2:插入instance_flavor表 '
        'task %s : insert instance_flavor table start when create instance',
        task_id)
    instance_flavor_data = {
        'instance_id': instance_id,
        'flavor_id': flavor_id,
        'created_at': get_datetime_str()
    }
    ret1 = ins_f_s.InstanceFlavorService().add_instance_flavor_info(
        instance_flavor_data)
    if ret1.get('row_num') <= 0:
        logging.error(
            'task %s : add instance_flavor info error when create instance, insert_data: %s',
            task_id, instance_flavor_data)
        return json_helper.format_api_resp(code=ErrorCode.SYS_ERR)
    logging.info(
        '创建VM 步骤10-2:插入instance_flavor表成功 '
        'task %s : insert instance_flavor table successful when create instance',
        task_id)

    # 往instance_group表添加记录
    logging.info(
        '创建VM 步骤10-3:插入instance_group表 task %s : insert instance_group table start when create instance',
        task_id)
    instance_group_data = {
        'instance_id': instance_id,
        'group_id': group_id,
        'created_at': get_datetime_str()
    }
    ret2 = ins_g_s.InstanceGroupService().add_instance_group_info(
        instance_group_data)
    if ret2.get('row_num') <= 0:
        logging.error(
            'task %s : add instance_group info error when create instance, insert_data: %s',
            task_id, instance_group_data)
        return json_helper.format_api_resp(code=ErrorCode.SYS_ERR)
    logging.info(
        '创建VM 步骤10-3:插入instance_group表成功 task %s : insert instance_group table successful when create instance',
        task_id)

    # 往instance_host表添加记录
    logging.info(
        '创建VM 步骤10-4:插入instance_host表 task %s : insert instance_host table start when create instance',
        task_id)
    instance_host_data = {
        'instance_id': instance_id,
        'instance_name': instance_name,
        'host_id': vm_host['host_id'],
        'host_name': vm_host['name'],
        'isdeleted': '0',
        'created_at': get_datetime_str()
    }
    ret3 = ins_h_s.InstanceHostService().add_instance_host_info(
        instance_host_data)
    if ret3.get('row_num') <= 0:
        logging.error(
            'task %s : add instance_host info error when create instance, insert_data: %s',
            task_id, instance_host_data)
        return json_helper.format_api_resp(code=ErrorCode.SYS_ERR)
    logging.info(
        '创建VM 步骤10-4:插入instance_host表成功 '
        'task %s : insert instance_host table successful when create instance',
        task_id)

    # host预分配资源
    logging.info(
        '创建VM 步骤10-5:host预分配资源 task %s : pre allocate host resource start when create instance',
        task_id)
    ret4 = host_s.pre_allocate_host_resource(vm_host['host_id'],
                                             flavor_info['vcpu'],
                                             flavor_info['memory_mb'],
                                             flavor_info['root_disk_gb'])
    if ret4 != 1:
        logging.error(
            'task %s : pre allocate host resource to db error when create instance',
            task_id)
        return json_helper.format_api_resp(code=ErrorCode.SYS_ERR)
    logging.info(
        '创建VM 步骤10-5:host预分配资源成功 '
        'task %s : pre allocate host resource successful when create instance',
        task_id)

    # 往instance_image表添加记录
    logging.info(
        '创建VM 步骤10-6:插入instance_image表 task %s : insert instance_image table start when create instance',
        task_id)
    for _image in image_data:
        instance_image_data = {
            'instance_id': instance_id,
            'image_id': _image['id'],
            'created_at': get_datetime_str()
        }
        ret5 = ins_img_s.InstanceImageService().add_instance_image_info(
            instance_image_data)
        if ret5.get('row_num') <= 0:
            logging.error(
                'task %s : add instance_image info error when create instance, insert_data: %s',
                task_id, instance_image_data)
            return json_helper.format_api_resp(code=ErrorCode.SYS_ERR)
    logging.info(
        '创建VM 步骤10-6:插入instance_image表成功 '
        'task %s : insert instance_image table successful when create instance',
        task_id)

    # 往instance_ip表添加记录
    logging.info(
        '创建VM 步骤10-7:插入instance_ip表 '
        'task %s : insert instance_ip table start when create instance',
        task_id)
    mac = randomMAC()
    data_ip_address = ip_data['ip_address']
    instance_ip_data = {
        'instance_id': instance_id,
        'ip_id': ip_data['id'],
        'mac': mac,
        'type': InstanceNicType.MAIN_NETWORK_NIC,
        'isdeleted': '0',
        'created_at': get_datetime_str()
    }
    ret6 = ins_ip_s.InstanceIPService().add_instance_ip_info(instance_ip_data)
    if ret6.get('row_num') <= 0:
        logging.error(
            'task %s : add instance_ip info error when create instance, insert_data: %s',
            task_id, instance_ip_data)
        return json_helper.format_api_resp(code=ErrorCode.SYS_ERR)
    logging.info(
        '创建VM 步骤10-7:插入instance_ip表成功 '
        'task %s : insert instance_ip table successful when create instance',
        task_id)

    # 标识该IP为已使用
    logging.info(
        '创建VM 步骤10-8:设置IP为已使用 task %s : set ip used start when create instance',
        task_id)
    update_data = {'status': IPStatus.USED}
    where_data = {'id': ip_data['id']}
    ret7 = ip_service.IPService().update_ip_info(update_data, where_data)
    if ret7 <= 0:
        logging.info(
            'task %s : update ip info error when create instance, update_data: %s, where_data: %s',
            task_id, update_data, where_data)
        return json_helper.format_api_resp(code=ErrorCode.SYS_ERR)
    logging.info(
        '创建VM 步骤10-8:设置IP为已使用成功 task %s : set ip used successful when create instance',
        task_id)

    # 拼装消息需要的镜像信息
    logging.info(
        '创建VM 步骤10-9:拼装所需的镜像信息 '
        'task %s : piece together need image info start when create instance',
        task_id)
    image_list = []
    # 数据盘数量
    data_image_num = 0
    for _image in image_data:
        _image_type = _image['type']
        _info = {
            "disk_format": _image['format'],
            "url": _image['url'],
            # "md5sum": _image['md5'],
            "image_size_gb": _image['size_gb']  # 镜像预分配大小
        }
        # 系统盘
        if _image_type == ImageType.SYSTEMDISK:
            _disk_name = instance_name + '.img'
            _info['image_dir_path'] = '/app/image/' + uuid + '/' + _disk_name
            _info['disk_name'] = _disk_name
            _info['disk_size_gb'] = None
            _info['dev_name'] = 'vda'

            # 如果只有一块盘且为系统盘,则预先分配一块数据盘的数据存入instance_disk表
            if len(image_data) == 1:
                logging.info(
                    'task %s : pre insert instance_disk table that has only one system disk start when create '
                    'instance', task_id)
                instance_disk_data = {
                    'instance_id': instance_id,
                    'size_gb': vm_disk_gb,
                    'mount_point': mount_point,
                    'dev_name': 'vdb',
                    'isdeleted': '0',
                    'created_at': get_datetime_str()
                }
                ret9 = ins_d_s.InstanceDiskService().add_instance_disk_info(
                    instance_disk_data)
                if ret9.get('row_num') <= 0:
                    logging.info(
                        'task %s : pre add instance_disk info that has only one system disk error when create '
                        'instance, insert_data: %s', task_id,
                        instance_disk_data)
                    return json_helper.format_api_resp(code=ErrorCode.SYS_ERR)
                logging.info(
                    'task %s : pre insert instance_disk table that has only one system disk successful when '
                    'create instance', task_id)
        else:
            # 数据盘
            _disk_name = instance_name + '.disk' + str(data_image_num + 1)
            _disk_dev_name = _get_vd_map(data_image_num)
            _info['image_dir_path'] = '/app/image/' + uuid + '/' + _disk_name
            _info['disk_name'] = _disk_name
            _info['disk_size_gb'] = vm_disk_gb
            _info['dev_name'] = _disk_dev_name
            data_image_num += 1

            # 往instance_disk表添加记录
            logging.info(
                'task %s : insert instance_disk table start when create instance',
                task_id)
            instance_disk_data = {
                'instance_id': instance_id,
                'size_gb': vm_disk_gb,
                'mount_point': mount_point,
                'dev_name': _disk_dev_name,
                'isdeleted': '0',
                'created_at': get_datetime_str()
            }
            ret8 = ins_d_s.InstanceDiskService().add_instance_disk_info(
                instance_disk_data)
            if ret8.get('row_num') <= 0:
                logging.info(
                    'task %s : add instance_disk info error when create instance, insert_data: %s',
                    task_id, instance_disk_data)
                return json_helper.format_api_resp(code=ErrorCode.SYS_ERR)
            logging.info(
                'task %s : insert instance_disk table successful when create instance',
                task_id)

        image_list.append(_info)
    logging.info(
        '创建VM 步骤10-9:拼装所需的镜像信息成功 '
        'task %s : piece together need image info successful when create instance',
        task_id)

    # 发送异步消息到队列
    logging.info(
        '创建VM 步骤10-10:发送异步消息给队列 '
        'task %s : send kafka message start when create instance', task_id)
    data = {
        "routing_key": "INSTANCE.CREATE",
        "send_time": get_datetime_str(),
        "data": {
            "task_id":
            task_id,
            "request_id":
            request_id,
            "host_ip":
            vm_host['ipaddress'],
            "uuid":
            uuid,
            "hostname":
            instance_name,  # 实例名
            "memory_mb":
            flavor_info['memory_mb'],
            "vcpu":
            flavor_info['vcpu'],
            "ostype":
            instance_system,
            "user_id":
            user_id,
            "disks":
            image_list,
            "disk_size":
            vm_disk_gb,
            "image_name":
            _image['url'].split('/')[-1],
            "net_area_id":
            net_area_id,
            "networks": [{
                "net_card_name": "br_bond0." + segment_data['vlan'],
                "ip": data_ip_address,
                "netmask": segment_data['netmask'],
                "dns1": segment_data['dns1'],
                "dns2": segment_data['dns2'],
                "mac": mac,
                "gateway": segment_data['gateway_ip'],
                "env": vm_env  # SIT STG PRD DR
            }]
        }
    }
    ret_kafka = send_async_msg(KAFKA_TOPIC_NAME, data)
Esempio n. 5
0
def _instance_db_info(uuid, vmname, vm_app_info, owner, flavor_id, group_id,
                      host, mac, vmdisk, ip_id, vmostype, requestid, ver_data):
    vm_ostype_todb = ''
    vmhost = ho_s.HostService().get_host_info_by_hostip(host)
    # 往instance表添加记录
    instance_data = {
        'uuid': uuid,
        'name': vmname,
        'displayname': vmname,
        'description': '',
        'status': VMStatus.CONVERTING,
        'typestatus': VMTypeStatus.NORMAL,
        'isdeleted': '0',
        'app_info': vm_app_info,
        'owner': owner,
        'created_at': get_datetime_str(),
        'create_source': VMCreateSource.ESX
    }
    ret = ins_s.InstanceService().add_instance_info(instance_data)
    if ret.get('row_num') <= 0:
        message = 'add instance info error when create instance'
        logging.info(
            'add instance info error when create instance, insert_data: %s',
            instance_data)
        return False, message

    instance_id = ret.get('last_id')

    if vmostype == 'Windows':
        vm_ostype_todb = 'windows'
    elif vmostype == 'Linux':
        vm_ostype_todb = 'linux'

    # 往v2v_instance_info表添加记录
    v2v_instance_data = {
        'instance_id': instance_id,
        'os_type': vm_ostype_todb,
        'isdeleted': '0',
        'created_at': get_datetime_str(),
        'request_id': requestid,
        'os_version': ver_data
    }
    ret_v2v_instance = v2v_in_i.v2vInstanceinfo().add_v2v_instance_info(
        v2v_instance_data)
    if ret_v2v_instance.get('row_num') <= 0:
        logging.info(
            'add v2v_instance info error when create instance, v2v_instance_data: %s',
            v2v_instance_data)
        message = 'add v2v_instance info error when create instance'
        return False, message

    # 往instance_flavor表添加记录
    instance_flavor_data = {
        'instance_id': instance_id,
        'flavor_id': flavor_id,
        'created_at': get_datetime_str()
    }
    ret1 = ins_f_s.InstanceFlavorService().add_instance_flavor_info(
        instance_flavor_data)
    if ret1.get('row_num') <= 0:
        logging.info(
            'add instance_flavor info error when create instance, insert_data: %s',
            instance_flavor_data)
        message = 'add instance_flavor info error when create instance'
        return False, message

    # 往instance_group表添加记录
    instance_group_data = {
        'instance_id': instance_id,
        'group_id': group_id,
        'created_at': get_datetime_str()
    }
    ret2 = ins_g_s.InstanceGroupService().add_instance_group_info(
        instance_group_data)
    if ret2.get('row_num') <= 0:
        logging.info(
            'add instance_group info error when create instance, insert_data: %s',
            instance_group_data)
        message = 'add instance_group info error when create instance'
        return False, message

    # 往instance_host表添加记录
    instance_host_data = {
        'instance_id': instance_id,
        'instance_name': vmname,
        'host_id': vmhost['id'],
        'host_name': vmhost['name'],
        'isdeleted': '0',
        'created_at': get_datetime_str()
    }
    ret3 = ins_h_s.InstanceHostService().add_instance_host_info(
        instance_host_data)
    if ret3.get('row_num') <= 0:
        logging.info(
            'add instance_host info error when create instance, insert_data: %s',
            instance_host_data)
        message = 'add instance_host info error when create instance'
        return False, message

    # 往instance_ip表添加记录
    instance_ip_data = {
        'instance_id': instance_id,
        'ip_id': ip_id,
        'mac': mac,
        'type': InstanceNicType.MAIN_NETWORK_NIC,
        'isdeleted': '0',
        'created_at': get_datetime_str()
    }
    ret4 = ins_ip_s.InstanceIPService().add_instance_ip_info(instance_ip_data)
    if ret4.get('row_num') <= 0:
        logging.info(
            'add instance_ip info error when create instance, insert_data: %s',
            instance_ip_data)
        message = 'add instance_ip info error when create instance'
        return False, message

    # 往instance_disk表添加记录
    instance_disk_data = {
        'instance_id': instance_id,
        'size_gb': vmdisk,
        'mount_point': '',
        'dev_name': '',
        'isdeleted': '0',
        'created_at': get_datetime_str()
    }
    ret5 = ins_d_s.InstanceDiskService().add_instance_disk_info(
        instance_disk_data)
    if ret5.get('row_num') <= 0:
        logging.info(
            'add instance_disk info error when create instance, insert_data: %s',
            instance_disk_data)
        message = 'add instance_disk info error when create instance'
        return False, message
    message = "信息入库完成"
    return True, message
Esempio n. 6
0
def v2v_openstack_del():

    #获取入参信息
    delete = request.values.get('delete')
    request_his = request.values.get('request_id')
    source = v2v_op.v2vTaskService().get_v2v_task_by_requestid(
        request_his)['source']

    if not source:
        return json_helper.format_api_resp(code=ErrorCode.PARAM_ERR,
                                           msg='v2v来源缺失')

    if delete != '1':
        return json_helper.format_api_resp(code=ErrorCode.PARAM_ERR,
                                           msg='参数错误')

    #判断当前任务是否完成
    data, message = v2v_op.get_v2v_deleteable(request_his)
    if data != '2':
        return_msg = message
        return json_helper.format_api_resp(code=ErrorCode.PARAM_ERR,
                                           msg=return_msg)
    else:
        if source == VMCreateSource.OPENSTACK:
            del_res, del_msg = del_action(request_his)
            if del_res == False:
                return json_helper.format_api_resp(code=ErrorCode.SYS_ERR,
                                                   msg=del_msg)
        else:
            tag, errmsg = esx_del_action(request_his)
            if not tag:
                return json_helper.format_api_resp(code=ErrorCode.SYS_ERR,
                                                   msg=errmsg)

        v2v_task = v2v_op.v2vTaskService().get_v2v_task_by_requestid(
            request_his)
        v2v_vm_uuid = v2v_task['vm_uuid']
        #更新instance表
        instance_info = ins_s.InstanceService().get_instance_info_by_uuid(
            v2v_vm_uuid)
        instance_id = instance_info['id']
        update_data = {'isdeleted': '1', 'deleted_at': get_datetime_str()}
        where_data = {'id': instance_id}
        ret = ins_s.InstanceService().update_instance_info(
            update_data, where_data)
        if ret != 1:
            logging.error('删除instance %s 错误', instance_id)
            return json_helper.format_api_resp(code=ErrorCode.SYS_ERR,
                                               msg='删除instance错误')

        # instance_flavor
        ret_f = ins_f_s.InstanceFlavorService().delete_instance_flavor(
            instance_id)
        if ret_f != 1:
            logging.error('delete instance %s flavor error', instance_id)
            return json_helper.format_api_resp(code=ErrorCode.SYS_ERR,
                                               msg='删除instance flavor错误')

        # instance_group
        ret_g = ins_g_s.InstanceGroupService().delete_instance_group_info(
            instance_id)
        if ret_g != 1:
            logging.error('delete instance %s group error', instance_id)
            return json_helper.format_api_resp(code=ErrorCode.SYS_ERR,
                                               msg='删除instance group错误')

        # instance_host
        update_data_h = {'isdeleted': '1', 'deleted_at': get_datetime_str()}
        where_data_h = {'instance_id': instance_id}
        ret_h = ins_h_s.InstanceHostService().update_instance_host_info(
            update_data_h, where_data_h)
        if ret_h != 1:
            logging.error('delete instance %s host error', instance_id)
            return json_helper.format_api_resp(code=ErrorCode.SYS_ERR,
                                               msg='删除instance host错误')

        # instance_disk
        update_data_d = {'isdeleted': '1', 'deleted_at': get_datetime_str()}
        where_data_d = {'instance_id': instance_id}
        ret_d = ins_d_s.InstanceDiskService().update_instance_disk_info(
            update_data_d, where_data_d)
        if ret_d != 1:
            logging.error('delete instance %s disk error', instance_id)
            return json_helper.format_api_resp(code=ErrorCode.SYS_ERR,
                                               msg='删除instance disk错误')

        # instance_ip
        update_data_ip = {'isdeleted': '1', 'deleted_at': get_datetime_str()}
        where_data_ip = {'instance_id': instance_id}
        ret_i_ip = ins_ip_s.InstanceIPService().update_instance_ip_info(
            update_data_ip, where_data_ip)
        if ret_i_ip != 1:
            logging.error('delete instance %s ip error', instance_id)
            return json_helper.format_api_resp(code=ErrorCode.SYS_ERR,
                                               msg='删除instance ip错误')

        #更新v2v_instance_info
        update_data = {'isdeleted': '1', 'deleted_at': get_datetime_str()}
        where_data = {'instance_id': instance_id}
        ret_v2v_in_i = v2v_in_i.v2vInstanceinfo().update_v2v_status(
            update_data, where_data)
        if ret_v2v_in_i != 1:
            logging.error('delete v2v instance info error')
            return json_helper.format_api_resp(code=ErrorCode.SYS_ERR,
                                               msg='删除v2v instance info错误')

        # 删除vm的ip
        ip_data = ins_s.get_ip_of_instance(instance_id)
        ip_id = ip_data['id']
        if ip_data:
            ip_s.del_ip_info(ip_id)

        #更新v2v_task表
        where_v2v = {'request_id': request_his}
        update_v2v = {'destory': '1'}
        v2v_op.v2vTaskService().update_v2v_status(update_v2v, where_v2v)

        return json_helper.format_api_resp(code=ErrorCode.SUCCESS,
                                           msg='删除v2v任务完成')