def __update_config_msg_to_db(task_id, msg, job_status):
    update_data = {
        'message': msg,
        'status': job_status,
        'finish_time': get_datetime_str()
    }
    where_data = {'task_id': task_id}
    return instance_action_s.InstanceActionsServices(
    ).update_instance_action_status(update_data, where_data)
Example #2
0
def update_instance_actions(request_id, action, status, message):

    update_data = {
        'status': status,
        'finish_time': datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
        'message': message
    }
    print message
    where_data = {'request_id': request_id, 'action': action}
    ret = instance_action.InstanceActionsServices(
    ).update_instance_action_status(update_data, where_data)
    if ret > 0:
        return True
    return False
Example #3
0
def add_instance_actions(data):
    action = data['action']
    instance_uuid = data['instance_uuid']
    request_id = data['request_id']
    user_id = data['user_id']
    message = data['message']
    insert_data = {
        'action': action,
        'instance_uuid': instance_uuid,
        'request_id': request_id,
        'user_id': user_id,
        'message': message,
        'start_time': datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    }
    ret = instance_action.InstanceActionsServices().add_instance_action_info(
        insert_data)
    if ret['last_id'] > 0:
        return True

    return False
Example #4
0
def _task_esx_intodb(task):
    # 获取vm入参
    vmname = task['vm_name']
    vmip = task['vm_ip']
    flavor_id = task['flavor_id']
    vm_ostype = task['vm_ostype']
    vm_app_info = task['vm_app_info']
    vm_owner = task['vm_owner']
    vm_segment = task['vm_segment']
    vm_group_id = task['group_id']
    dest_host = task['dest_host']

    vmhost = ho_s.HostService().get_host_info_by_hostip(dest_host)
    ret_4 = ho_s.pre_allocate_host_resource(vmhost['id'], task['vm_cpu'],
                                            task['vm_mem_mb'], 50)
    if ret_4 != 1:
        logging.error('资源预分配失败')
        message = '资源预分配失败'
        return False, message

    # 获取并录入IP信息
    vm_segment = seg_s.SegmentService().get_segment_info_bysegment(vm_segment)
    if vm_segment is None:
        message = '网段信息有误,无法进行v2v操作'
        return False, message
    else:
        segment_id = vm_segment['id']
        vm_netmask = vm_segment['netmask']
        vm_gateway = vm_segment['gateway_ip']
        vm_dns1 = vm_segment['dns1']
        vm_dns2 = vm_segment['dns2']
        vmvlan = vm_segment['vlan']
        ip_data = ip_s.IPService().get_ip_info_by_ipaddress(vmip)
        if ip_data is None:
            ip_data = {
                'ip_address': vmip,
                'segment_id': segment_id,
                'netmask': vm_netmask,
                'vlan': vmvlan,
                'gateway_ip': vm_gateway,
                'dns1': vm_dns1,
                'dns2': vm_dns2,
                'created_at': get_datetime_str(),
                'status': '1'
            }
            ret = ip_s.IPService().add_ip_info(ip_data)
            if ret.get('row_num') <= 0:
                logging.info(
                    'add ip info error when create v2v task, insert_data: %s',
                    ip_data)
                message = "录入IP信息失败"
                return False, message
            else:
                ip_id = ret.get('last_id')
        else:
            ip_data_status = ip_data['status']
            vmvlan = ip_data['vlan']
            if ip_data_status != '0':
                message = "IP与现有环境冲突,无法进行v2v操作"
                return False, message
            else:
                ip_id = ip_data['id']
                where_data = {'id': ip_id}
                updata_data = {'status': '1', 'updated_at': get_datetime_str()}
                ret1 = ip_s.IPService().update_ip_info(updata_data, where_data)
                if not ret1:
                    logging.info(
                        'update ip info error when create v2v task, update_data: %s',
                        updata_data)
                    message = "更新IP状态失败"
                    return False, message

        # 生成request_id
        request_id = v2v_op.generate_req_id()

        # 生成vm的uuid和mac
        vmuuid = randomUUID()
        vmmac = randomMAC()

        # 信息入instance相关库表
        instance_tag, instance_info = _instance_db_info(
            vmuuid, vmname, vm_app_info, vm_owner, flavor_id, vm_group_id,
            dest_host, vmmac, task['vm_disk'], ip_id, vm_ostype, request_id,
            task['vm_os_version'])
        if not instance_tag:
            message = instance_info
            return False, message

        # 将步骤信息存入instance_action表
        v2v_cd_d1 = {
            'action': esx_v2vActions.CREATE_DEST_DIR,
            'request_id': request_id,
            'message': 'start',
            'start_time': get_datetime_str()
        }
        in_a_s.InstanceActionsServices().add_instance_action_info(v2v_cd_d1)

        v2v_cr_pl = {
            'action': esx_v2vActions.CREATE_STOR_POOL,
            'request_id': request_id,
            'message': 'other',
            'start_time': get_datetime_str()
        }
        in_a_s.InstanceActionsServices().add_instance_action_info(v2v_cr_pl)

        v2v_cp_fl = {
            'action': esx_v2vActions.COPY_FILE_TO_LOCAL,
            'request_id': request_id,
            'message': 'other',
            'start_time': get_datetime_str()
        }
        in_a_s.InstanceActionsServices().add_instance_action_info(v2v_cp_fl)

        v2v_file = {
            'action': esx_v2vActions.VIRT_V2V_FILES,
            'request_id': request_id,
            'message': 'other',
            'start_time': get_datetime_str()
        }
        in_a_s.InstanceActionsServices().add_instance_action_info(v2v_file)

        v2v_del_tmp = {
            'action': esx_v2vActions.DELETE_TMP_FILE,
            'request_id': request_id,
            'message': 'other',
            'start_time': get_datetime_str()
        }
        in_a_s.InstanceActionsServices().add_instance_action_info(v2v_del_tmp)

        v2v_sysdisk_std = {
            'action': esx_v2vActions.VM_SYS_DISK_STD,
            'request_id': request_id,
            'message': 'other',
            'start_time': get_datetime_str()
        }
        in_a_s.InstanceActionsServices().add_instance_action_info(
            v2v_sysdisk_std)

        v2v_datadisk_std = {
            'action': esx_v2vActions.VM_DATA_DISK_STD,
            'request_id': request_id,
            'message': 'other',
            'start_time': get_datetime_str()
        }
        in_a_s.InstanceActionsServices().add_instance_action_info(
            v2v_datadisk_std)

        v2v_def_1 = {
            'action': esx_v2vActions.VM_DEFINE1,
            'request_id': request_id,
            'message': 'other',
            'start_time': get_datetime_str()
        }
        in_a_s.InstanceActionsServices().add_instance_action_info(v2v_def_1)

        v2v_star_1 = {
            'action': esx_v2vActions.VM_START1,
            'request_id': request_id,
            'message': 'other',
            'start_time': get_datetime_str()
        }
        in_a_s.InstanceActionsServices().add_instance_action_info(v2v_star_1)

        if vm_ostype == "Windows":
            v2v_att_disk = {
                'action': esx_v2vActions.ATTACH_DISK,
                'request_id': request_id,
                'message': 'other',
                'start_time': get_datetime_str()
            }
            in_a_s.InstanceActionsServices().add_instance_action_info(
                v2v_att_disk)

            v2v_win_std = {
                'action': esx_v2vActions.WINDOWS_STD,
                'request_id': request_id,
                'message': 'other',
                'start_time': get_datetime_str()
            }
            in_a_s.InstanceActionsServices().add_instance_action_info(
                v2v_win_std)

            v2v_vm_def2 = {
                'action': esx_v2vActions.WINDOWS_DISK_CH,
                'request_id': request_id,
                'message': 'other',
                'start_time': get_datetime_str()
            }
            in_a_s.InstanceActionsServices().add_instance_action_info(
                v2v_vm_def2)

            v2v_vm_star2 = {
                'action': esx_v2vActions.VM_START2,
                'request_id': request_id,
                'message': 'other',
                'start_time': get_datetime_str()
            }
            in_a_s.InstanceActionsServices().add_instance_action_info(
                v2v_vm_star2)

        # 将v2v信息存入v2v_task表
        v2v_data = {
            'source': VMCreateSource.ESX,
            'request_id': request_id,
            'destory': '0',
            'start_time': get_datetime_str(),
            'status': 0,
            'vm_ip': vmip,
            'vm_name': vmname,
            'vmvlan': vmvlan,
            'flavor_id': flavor_id,
            'cloud_area': task['esx_env'],
            'vm_ostype': vm_ostype,
            'vm_app_info': vm_app_info,
            'vm_owner': vm_owner,
            'vm_group_id': vm_group_id,
            'user_id': task['user_id'],
            'vm_mac': vmmac,
            'vm_uuid': vmuuid,
            'cancel': '0',
            'dest_dir': '/app/image/' + vmuuid,
            'on_task': '0',
            'dest_host': dest_host,
            'esx_ip': task['esx_ip'],
            'esx_passwd': task['esx_passwd'],
            'vmware_vm': task['vmware_vm'],
            'step_done': esx_v2vActions.BEGIN
        }
        v2v_insert = v2v_op.v2vTaskService().add_v2v_task_info(v2v_data)

        if v2v_insert.get('row_num') <= 0:
            logging.info('insert info to v2v_task failed! %s', v2v_data)
            message = '信息入库失败'
            return False, message

        message = '信息已添加至任务队列'
        return True, message
Example #5
0
def get_collect_request_multithreading(task_idapi, task_idkvm, vm_count,
                                       request_opr_user, request_api_origin):
    # 判断此工单是否有线程在追踪
    try:
        params = {
            'WHERE_AND': {
                '=': {
                    'taskid_kvm': task_idkvm,
                    'taskid_api': task_idapi,
                    'istraceing': '1'
                },
            }
        }
        ret_traceing_nums, ret_traceing_data = request_r_s.RequestRecordService(
        ).request_db_query_data(**params)
        if ret_traceing_nums > 0:
            return

        # 标记工单为跟踪中
        _update_data = {
            'istraceing': '1',
        }
        _where_data = {
            'taskid_kvm': task_idkvm,
        }
        ret = request_r_s.RequestRecordService().update_request_status(
            _update_data, _where_data)
        if ret <= 0:
            logging.error('update request %s status to db failed' % task_idkvm)

        ret_request_re = request_r_s.RequestRecordService(
        ).get_request_record_info_by_taskid_kvm(task_idkvm)
        request_ip = ret_request_re["request_ip"]

        succeed_http_code = ['200', '500']
        instance_actions_succeed_params = {
            'WHERE_AND': {
                '=': {
                    'task_id': task_idkvm,
                    'action': 'instance_inject_data',
                    'status': 1
                },
            },
        }
        # instance_actions_failed_params = {
        #     'WHERE_AND': {
        #         '=': {
        #             'task_id': task_idkvm,
        #             'status': 2
        #         },
        #         '!=': {
        #             'action': 'image_sync_status'
        #         },
        #     },
        # }
        instance_timeout_failed_params = {
            'WHERE_AND': {
                '=': {
                    'task_id': task_idkvm,
                    'status': VMStatus.CREATE_ERROR
                },
            },
        }
        vm_succeed_count_db_ret, vm_succeed_data = instance_a_s.InstanceActionsServices(
        ).query_data(**instance_actions_succeed_params)
        # vm_failed_count_db_ret, vm_failed_data =
        # instance_a_s.InstanceActionsServices().query_data(**instance_actions_failed_params)
        vm_createtimeout_count_db_ret, vm_createtimeout_failed_data = instance_s.InstanceService(
        ).query_data(**instance_timeout_failed_params)
        if not vm_succeed_count_db_ret:
            vm_succeed_count_db_ret = 0

        if not vm_createtimeout_count_db_ret:
            vm_createtimeout_count_db_ret = 0

        instances_uuid = []
        if vm_succeed_count_db_ret > 0:
            for per_request_data in vm_succeed_data:
                instances_uuid.append(per_request_data['instance_uuid'])

            if vm_count == vm_succeed_count_db_ret:
                # 通过虚拟机uuid查询主机名、ip、物理机序列号
                vm_datas = []
                for instance_uuid in instances_uuid:
                    ret_ins = instance_s.InstanceService(
                    ).get_instance_info_by_uuid(instance_uuid)
                    if ret_ins:
                        ret_ins_host = instance_s.get_host_of_instance(
                            ret_ins['id'])
                        ret_ins_ip = instance_s.get_net_segment_info_of_instance(
                            ret_ins['id'])
                        if ret_ins_host and ret_ins_ip:
                            vm_data = {
                                'instance_ids': ret_ins['id'],
                                'host_name': ret_ins['name'],
                                'ip': ret_ins_ip['ip_address'],
                                'ip_type': ret_ins_ip['segment_type'],
                                'sn': ret_ins_host['sn'],
                                'UUID': instance_uuid,
                                'net_name': ret_ins_ip['segment'],
                                'subnet_mask': ret_ins_ip['netmask'],
                                'gateway': ret_ins_ip['gateway_ip'],
                                'vlan_id': ret_ins_ip['vlan'],
                                'passWord': decrypt(ret_ins['password'])
                            }
                            vm_datas.append(vm_data)

                msg_detail = {'opUser': request_opr_user, 'vm': vm_datas}
                # 回调外部接口
                response_to_api_status = '1'
                if request_api_origin == ApiOrigin.VISHNU:
                    ret_code = msg_to_vishnu(task_idapi, VsJobStatus.SUCCEED,
                                             msg_detail, request_ip)
                elif request_api_origin == ApiOrigin.SFSLB:
                    ret_code = msg_to_sfslb(task_idapi, VsJobStatus.SUCCEED,
                                            msg_detail, request_ip)
                elif request_api_origin == ApiOrigin.FWAF:
                    ret_code = msg_to_fwaf(task_idapi, VsJobStatus.SUCCEED,
                                           msg_detail, request_ip)
                else:
                    ret_code = msg_to_vishnu(task_idapi, VsJobStatus.SUCCEED,
                                             msg_detail, request_ip)
                if ret_code not in succeed_http_code:
                    response_to_api_status = '0'
                update_db_time = get_datetime_str()
                _update_data = {
                    'task_status': '1',
                    'response_to_api': response_to_api_status,
                    'finish_time': update_db_time,
                    'request_status_collect_time': update_db_time,
                }
                _where_data = {
                    'taskid_kvm': task_idkvm,
                }
                ret = request_r_s.RequestRecordService().update_request_status(
                    _update_data, _where_data)
                if ret <= 0:
                    logging.error('update request %s status to db failed' %
                                  task_idkvm)
            elif (vm_createtimeout_count_db_ret + vm_succeed_count_db_ret) == vm_count \
                    and vm_createtimeout_count_db_ret > vm_count * 0.2:
                # 回调外部接口
                response_to_api_status = '1'

                if request_api_origin == ApiOrigin.VISHNU:
                    ret_code = msg_to_vishnu(task_idapi, VsJobStatus.FAILED,
                                             'all kvm instance create failed',
                                             request_ip)
                elif request_api_origin == ApiOrigin.SFSLB:
                    ret_code = msg_to_sfslb(task_idapi, VsJobStatus.FAILED,
                                            'all kvm instance create failed',
                                            request_ip)
                elif request_api_origin == ApiOrigin.FWAF:
                    ret_code = msg_to_fwaf(task_idapi, VsJobStatus.FAILED,
                                           'all kvm instance create failed',
                                           request_ip)
                else:
                    ret_code = msg_to_vishnu(task_idapi, VsJobStatus.FAILED,
                                             'all kvm instance create failed',
                                             request_ip)

                if ret_code not in succeed_http_code:
                    response_to_api_status = '0'
                update_db_time = get_datetime_str()
                _update_data = {
                    'task_status': '2',
                    'response_to_api': response_to_api_status,
                    'finish_time': update_db_time,
                    'request_status_collect_time': update_db_time,
                }
                _where_data = {
                    'taskid_kvm': task_idkvm,
                }
                ret = request_r_s.RequestRecordService().update_request_status(
                    _update_data, _where_data)
                if ret <= 0:
                    logging.error('update request %s status to db failed' %
                                  task_idkvm)
            else:
                if (vm_createtimeout_count_db_ret +
                        vm_succeed_count_db_ret) == vm_count:
                    # 把成功创建的虚拟机返回外部接口
                    """
                    vm_datas = []
                    for instance_uuid in instances_uuid:
                        ret_ins = instance_s.InstanceService().get_instance_info_by_uuid(instance_uuid)
                        if ret_ins:
                            ret_ins_host = instance_s.get_host_of_instance(ret_ins['id'])
                            ret_ins_ip = instance_s.get_net_segment_info_of_instance(ret_ins['id'])
                            if ret_ins_host and ret_ins_ip:
                                vm_data = {
                                    'host_name': ret_ins['name'],
                                    'ip': ret_ins_ip['ip_address'],
                                    'sn': ret_ins_host['sn'],
                                    'UUID': instance_uuid,
                                    'net_name': ret_ins_ip['segment'],
                                    'subnet_mask': ret_ins_ip['netmask'],
                                    'gateway': ret_ins_ip['gateway_ip'],
                                    'vlan_id': ret_ins_ip['vlan']
                                }
                                vm_datas.append(vm_data)

                    msg_detail = {'opUser': request_opr_user, 'vm': vm_datas}
                    """
                    # 回调外部接口
                    response_to_api_status = '1'
                    if request_api_origin == ApiOrigin.VISHNU:
                        ret_code = msg_to_vishnu(
                            task_idapi, VsJobStatus.FAILED,
                            'part of kvm instance create failed', request_ip)
                    elif request_api_origin == ApiOrigin.SFSLB:
                        ret_code = msg_to_sfslb(
                            task_idapi, VsJobStatus.FAILED,
                            'part of kvm instance create failed', request_ip)
                    elif request_api_origin == ApiOrigin.FWAF:
                        ret_code = msg_to_fwaf(
                            task_idapi, VsJobStatus.FAILED,
                            'part of kvm instance create failed', request_ip)
                    else:
                        ret_code = msg_to_vishnu(
                            task_idapi, VsJobStatus.FAILED,
                            'part of kvm instance create failed', request_ip)

                    if ret_code not in succeed_http_code:
                        response_to_api_status = '0'

                    update_db_time = get_datetime_str()
                    _update_data = {
                        'task_status': '2',
                        'response_to_api': response_to_api_status,
                        'finish_time': update_db_time,
                        'request_status_collect_time': update_db_time,
                    }
                    _where_data = {
                        'taskid_kvm': task_idkvm,
                    }
                    ret = request_r_s.RequestRecordService(
                    ).update_request_status(_update_data, _where_data)
                    if ret <= 0:
                        logging.error('update request %s status to db failed' %
                                      task_idkvm)
                else:
                    # 成功失败的总数量未等于count
                    pass

        elif vm_createtimeout_count_db_ret > 0:
            # 全部虚拟机创建失败
            if (vm_createtimeout_count_db_ret + vm_succeed_count_db_ret) == vm_count \
                    and vm_createtimeout_count_db_ret > vm_count * 0.2:
                # 回调外部接口
                response_to_api_status = '1'
                if request_api_origin == ApiOrigin.VISHNU:
                    ret_code = msg_to_vishnu(task_idapi, VsJobStatus.FAILED,
                                             'all kvm instance create failed',
                                             request_ip)
                elif request_api_origin == ApiOrigin.SFSLB:
                    ret_code = msg_to_sfslb(task_idapi, VsJobStatus.FAILED,
                                            'all kvm instance create failed',
                                            request_ip)
                elif request_api_origin == ApiOrigin.FWAF:
                    ret_code = msg_to_fwaf(task_idapi, VsJobStatus.FAILED,
                                           'all kvm instance create failed',
                                           request_ip)
                else:
                    ret_code = msg_to_vishnu(task_idapi, VsJobStatus.FAILED,
                                             'all kvm instance create failed',
                                             request_ip)

                if ret_code not in succeed_http_code:
                    response_to_api_status = '0'
                update_db_time = get_datetime_str()
                _update_data = {
                    'task_status': '2',
                    'response_to_api': response_to_api_status,
                    'finish_time': update_db_time,
                    'request_status_collect_time': update_db_time,
                }
                _where_data = {
                    'taskid_kvm': task_idkvm,
                }
                ret = request_r_s.RequestRecordService().update_request_status(
                    _update_data, _where_data)
                if ret <= 0:
                    logging.error('update request %s status to db failed' %
                                  task_idkvm)
        else:
            update_db_time = get_datetime_str()
            _update_data = {'request_status_collect_time': update_db_time}
            _where_data = {
                'taskid_kvm': task_idkvm,
            }
            ret = request_r_s.RequestRecordService().update_request_status(
                _update_data, _where_data)
            if ret <= 0:
                logging.error('update request %s status to db failed' %
                              task_idkvm)

        # 标记工单为完成追踪
        _update_data = {
            'istraceing': '0',
        }
        _where_data = {
            'taskid_kvm': task_idkvm,
        }
        ret = request_r_s.RequestRecordService().update_request_status(
            _update_data, _where_data)
        if ret <= 0:
            logging.error(
                'update request %s status to db failed when mark istraceing 1'
                % task_idkvm)

        return
    except Exception as e:
        logging.error('request {} threading exception error: {}'.format(
            task_idkvm, e))
        return
Example #6
0
def v2v_esx_intodb(hostpool_id):
    '''
                v2v_esx
            :param hostpool_id:
            :return:
    '''

    #获取vm入库信息
    vmname = request.values.get('vm_name')
    vmip = request.values.get('vm_ip')
    flavor_id = request.values.get('flavor_id')
    vm_ostype = request.values.get('vm_ostype')
    vm_app_info = request.values.get('vm_app_info')
    vm_owner = request.values.get('vm_owner')
    vm_group_id = request.values.get('vm_group_id')
    user_id = request.values.get('user_id')
    vm_segment = request.values.get('segment')
    vm_osver = request.values.get('vm_osver')
    vm_disk = request.values.get('vm_disk')
    esx_env = request.values.get('esx_env')
    esx_ip = request.values.get('esx_ip')
    esx_passwd1 = request.values.get('esx_passwd')
    vmware_vm = request.values.get('vmware_vm')

    # 入参完全性判断
    if not vmname or not vmip  or not flavor_id or not  vm_ostype or not vm_app_info \
        or not vm_osver or not vm_disk  or not vm_owner or not vm_group_id or not user_id or not vm_segment or not esx_passwd1:
        logging.info('params are invalid or missing')
        return json_helper.format_api_resp(code=ErrorCode.PARAM_ERR,
                                           msg='入参缺失')
    else:
        esx_passwd = base64.b64decode(esx_passwd1)
        powertag, msg_power = vm_powerState(esx_ip, esx_passwd, vmware_vm)
        if not powertag:
            return json_helper.format_api_resp(code=ErrorCode.SYS_ERR,
                                               msg=msg_power)

        # 获取flavor信息
        flavor_info = flavor_service.FlavorService().get_flavor_info(flavor_id)
        if not flavor_info:
            logging.info('id: %s flavor info not in db when create instance',
                         flavor_id)
            return json_helper.format_api_resp(code=ErrorCode.SYS_ERR,
                                               msg='实例规格数据有误,无法进行v2v')
        vmcpu = flavor_info['vcpu']
        vmmem = flavor_info['memory_mb']

        data_disk = int(vm_disk)

        # 获取可用目标host

        host_code, host_data, host_msg = cal_host(hostpool_id, vmcpu, vmmem,
                                                  data_disk, vm_group_id)
        if host_code < 0:
            return json_helper.format_api_resp(code=host_code, msg=host_msg)
        else:
            host = host_data

        vmhost = ho_s.HostService().get_host_info_by_hostip(host)
        ret_4 = ho_s.pre_allocate_host_resource(vmhost['id'], vmcpu, vmmem, 50)
        if ret_4 != 1:
            logging.error('资源预分配失败')
            message = '资源预分频失败'
            return json_helper.format_api_resp(code=ErrorCode.SYS_ERR,
                                               msg=message)

        # 获取并录入IP信息

        vm_segment = seg_s.SegmentService().get_segment_info_bysegment(
            vm_segment)
        if vm_segment == None:
            return json_helper.format_api_resp(code=ErrorCode.SYS_ERR,
                                               msg='网段信息有误,无法进行v2v')
        else:
            segment_id = vm_segment['id']
            vm_netmask = vm_segment['netmask']
            vm_gateway = vm_segment['gateway_ip']
            vm_dns1 = vm_segment['dns1']
            vm_dns2 = vm_segment['dns2']
            vmvlan = vm_segment['vlan']
            ip_data = ip_s.IPService().get_ip_info_by_ipaddress(vmip)
            if ip_data == None:
                ip_data = {
                    'ip_address': vmip,
                    'segment_id': segment_id,
                    'netmask': vm_netmask,
                    'vlan': vmvlan,
                    'gateway_ip': vm_gateway,
                    'dns1': vm_dns1,
                    'dns2': vm_dns2,
                    'created_at': get_datetime_str(),
                    'status': '1'
                }
                ret = ip_s.IPService().add_ip_info(ip_data)
                if ret.get('row_num') <= 0:
                    logging.info(
                        'add ip info error when create v2v task, insert_data: %s',
                        ip_data)
                    return json_helper.format_api_resp(code=ErrorCode.SYS_ERR,
                                                       msg="录入IP信息失败")
                else:
                    ip_id = ret.get('last_id')
            else:
                ip_data_status = ip_data['status']
                vmvlan = ip_data['vlan']
                if ip_data_status != '0':
                    return json_helper.format_api_resp(code=ErrorCode.SYS_ERR,
                                                       msg="IP与现有环境冲突,无法进行v2v")
                else:
                    ip_id = ip_data['id']
                    where_data = {'id': ip_id}
                    updata_data = {
                        'status': '1',
                        'updated_at': get_datetime_str()
                    }
                    ret1 = ip_s.IPService().update_ip_info(
                        updata_data, where_data)
                    if not ret1:
                        logging.info(
                            'update ip info error when create v2v task, update_data: %s',
                            updata_data)
                        return json_helper.format_api_resp(
                            code=ErrorCode.SYS_ERR, msg="更新IP状态失败")

        # 生成request_id
        request_Id = v2v_op.generate_req_id()

        # 生成vm的uuid和mac
        vmuuid = randomUUID()
        vmmac = randomMAC()

        if not vm_osver:
            vm_osver = "unknown"

        # 信息入instance相关库表
        instance_info = instance_db_info(vmuuid, vmname, vm_app_info, vm_owner,
                                         flavor_id, vm_group_id, host, vmmac,
                                         data_disk, ip_id, vm_ostype,
                                         request_Id, vm_osver)
        if instance_info < 0:
            return json_helper.format_api_resp(code=ErrorCode.SYS_ERR,
                                               msg='信息入库失败')

        #   将步骤信息存入instance_action表
        v2v_cd_d1 = {
            'action': esx_v2vActions.CREATE_DEST_DIR,
            'request_id': request_Id,
            'message': 'start',
            'start_time': get_datetime_str()
        }
        in_a_s.InstanceActionsServices().add_instance_action_info(v2v_cd_d1)

        v2v_cr_pl = {
            'action': esx_v2vActions.CREATE_STOR_POOL,
            'request_id': request_Id,
            'message': 'other',
            'start_time': get_datetime_str()
        }
        in_a_s.InstanceActionsServices().add_instance_action_info(v2v_cr_pl)

        v2v_cp_fl = {
            'action': esx_v2vActions.COPY_FILE_TO_LOCAL,
            'request_id': request_Id,
            'message': 'other',
            'start_time': get_datetime_str()
        }
        in_a_s.InstanceActionsServices().add_instance_action_info(v2v_cp_fl)

        v2v_file = {
            'action': esx_v2vActions.VIRT_V2V_FILES,
            'request_id': request_Id,
            'message': 'other',
            'start_time': get_datetime_str()
        }
        in_a_s.InstanceActionsServices().add_instance_action_info(v2v_file)

        v2v_del_tmp = {
            'action': esx_v2vActions.DELETE_TMP_FILE,
            'request_id': request_Id,
            'message': 'other',
            'start_time': get_datetime_str()
        }
        in_a_s.InstanceActionsServices().add_instance_action_info(v2v_del_tmp)

        v2v_sysdisk_std = {
            'action': esx_v2vActions.VM_SYS_DISK_STD,
            'request_id': request_Id,
            'message': 'other',
            'start_time': get_datetime_str()
        }
        in_a_s.InstanceActionsServices().add_instance_action_info(
            v2v_sysdisk_std)

        v2v_datadisk_std = {
            'action': esx_v2vActions.VM_DATA_DISK_STD,
            'request_id': request_Id,
            'message': 'other',
            'start_time': get_datetime_str()
        }
        in_a_s.InstanceActionsServices().add_instance_action_info(
            v2v_datadisk_std)

        v2v_def_1 = {
            'action': esx_v2vActions.VM_DEFINE1,
            'request_id': request_Id,
            'message': 'other',
            'start_time': get_datetime_str()
        }
        in_a_s.InstanceActionsServices().add_instance_action_info(v2v_def_1)

        v2v_star_1 = {
            'action': esx_v2vActions.VM_START1,
            'request_id': request_Id,
            'message': 'other',
            'start_time': get_datetime_str()
        }
        in_a_s.InstanceActionsServices().add_instance_action_info(v2v_star_1)

        if vm_ostype == "Windows":

            v2v_att_disk = {
                'action': esx_v2vActions.ATTACH_DISK,
                'request_id': request_Id,
                'message': 'other',
                'start_time': get_datetime_str()
            }
            in_a_s.InstanceActionsServices().add_instance_action_info(
                v2v_att_disk)

            v2v_win_std = {
                'action': esx_v2vActions.WINDOWS_STD,
                'request_id': request_Id,
                'message': 'other',
                'start_time': get_datetime_str()
            }
            in_a_s.InstanceActionsServices().add_instance_action_info(
                v2v_win_std)

            v2v_vm_def2 = {
                'action': esx_v2vActions.WINDOWS_DISK_CH,
                'request_id': request_Id,
                'message': 'other',
                'start_time': get_datetime_str()
            }
            in_a_s.InstanceActionsServices().add_instance_action_info(
                v2v_vm_def2)

            v2v_vm_star2 = {
                'action': esx_v2vActions.VM_START2,
                'request_id': request_Id,
                'message': 'other',
                'start_time': get_datetime_str()
            }
            in_a_s.InstanceActionsServices().add_instance_action_info(
                v2v_vm_star2)

        message = '信息已添加至任务队列,等待执行'

        # 将v2v信息存入v2v_task表
        v2v_data = {
            'source': VMCreateSource.ESX,
            'request_id': request_Id,
            'destory': '0',
            'start_time': get_datetime_str(),
            'status': 0,
            'vm_ip': vmip,
            'vm_name': vmname,
            'vmvlan': vmvlan,
            'flavor_id': flavor_id,
            'cloud_area': esx_env,
            'vm_ostype': vm_ostype,
            'vm_app_info': vm_app_info,
            'vm_owner': vm_owner,
            'vm_group_id': vm_group_id,
            'user_id': user_id,
            'vm_mac': vmmac,
            'vm_uuid': vmuuid,
            'cancel': '0',
            'dest_dir': '/app/image/' + vmuuid,
            'on_task': '0',
            'dest_host': host,
            'esx_ip': esx_ip,
            'esx_passwd': esx_passwd,
            'vmware_vm': vmware_vm,
            'step_done': esx_v2vActions.BEGIN
        }
        v2v_insert = v2v_op.v2vTaskService().add_v2v_task_info(v2v_data)

        if v2v_insert.get('row_num') <= 0:
            logging.info('insert info to v2v_task failed! %s', v2v_data)
            return json_helper.format_api_resp(code=ErrorCode.SYS_ERR,
                                               msg='信息入库失败')

        return json_helper.format_api_resp(code=ErrorCode.SUCCESS, msg=message)
def _task_into_db(task):
    # 入参赋值
    vmname = task['vm_name']
    vmip = task['vm_ip']
    flavor_id = task['flavor_id']
    cloudarea = task['cloud_area']
    vm_ostype = task['vm_ostype']
    vm_app_info = task['vm_app_info']
    vm_owner = task['vm_owner']
    vm_group_id = task['group_id']
    user_id = task['user_id']
    vm_segment = task['segment']
    vm_disk = task['vm_disk']
    vm_osver = task['vm_osver']
    host = task['dest_host']

    # 入参完全性判断
    if not vmname or not vmip or not flavor_id or not cloudarea or not vm_ostype \
            or not vm_app_info or not vm_owner or not vm_group_id or not user_id or not vm_segment:
        logging.info('params are invalid or missing')
        message = '入参缺失'
        return False, message
    else:
        # 获取flavor信息
        flavor_info = flavor_s.FlavorService().get_flavor_info(flavor_id)
        if not flavor_info:
            logging.info('id: %s flavor info not in db when create instance',
                         flavor_id)
            message = '实例规格数据有误,无法进行v2v'
            return False, message
        vmcpu = flavor_info['vcpu']
        vmmem = flavor_info['memory_mb']

        vmhost = ho_s.HostService().get_host_info_by_hostip(host)
        ret_4 = ho_s.pre_allocate_host_resource(vmhost['id'], vmcpu, vmmem, 50)
        if ret_4 != 1:
            logging.error('资源预分配失败')
            message = '资源预分频失败'
            return False, message

        # 获取并录入IP信息
        vm_segment = segment_s.SegmentService().get_segment_info_bysegment(
            vm_segment)
        if vm_segment is None:
            message = '网段信息有误,无法进行v2v'
            return False, message
        else:
            segment_id = vm_segment['id']
            vm_netmask = vm_segment['netmask']
            vm_gateway = vm_segment['gateway_ip']
            vm_dns1 = vm_segment['dns1']
            vm_dns2 = vm_segment['dns2']
            vmvlan = vm_segment['vlan']
            ip_data = ip_s.IPService().get_ip_info_by_ipaddress(vmip)
            if ip_data is None:
                ip_data = {
                    'ip_address': vmip,
                    'segment_id': segment_id,
                    'netmask': vm_netmask,
                    'vlan': vmvlan,
                    'gateway_ip': vm_gateway,
                    'dns1': vm_dns1,
                    'dns2': vm_dns2,
                    'created_at': get_datetime_str(),
                    'status': '1'
                }
                ret = ip_s.IPService().add_ip_info(ip_data)
                if ret.get('row_num') <= 0:
                    logging.info(
                        'add ip info error when create v2v task, insert_data: %s',
                        ip_data)
                    message = "录入IP信息失败"
                    return False, message
                else:
                    ip_id = ret.get('last_id')
            else:
                ip_data_status = ip_data['status']
                vmvlan = ip_data['vlan']
                if ip_data_status != '0':
                    message = "IP与现有环境冲突,无法进行v2v"
                    return False, message
                else:
                    ip_id = ip_data['id']
                    where_data = {'id': ip_id}
                    updata_data = {
                        'status': '1',
                        'updated_at': get_datetime_str()
                    }
                    ret1 = ip_s.IPService().update_ip_info(
                        updata_data, where_data)
                    if not ret1:
                        logging.info(
                            'update ip info error when create v2v task, update_data: %s',
                            updata_data)
                        message = "更新IP状态失败"
                        return False, message

        # 生成request_id
        request_id = v2v_op.generate_req_id()

        # 生成vm的uuid和mac
        vmuuid = randomUUID()
        vmmac = randomMAC()

        # 信息入instance相关库表
        instance_tag, instance_info = _instance_db_info(
            vmuuid, vmname, vm_app_info, vm_owner, flavor_id, vm_group_id,
            host, vmmac, vm_disk, ip_id, vm_ostype, request_id, vm_osver)
        if not instance_tag:
            message = instance_info
            return False, message

        # 将步骤信息存入instance_action表
        # 将createdir信息存入instance_action表
        v2v_cd_d1 = {
            'action': v2vActions.CREATE_DEST_DIR,
            'request_id': request_id,
            'message': 'start',
            'start_time': get_datetime_str()
        }
        in_a_s.InstanceActionsServices().add_instance_action_info(v2v_cd_d1)

        # 将getfile信息存入instance_action表
        v2v_gf_d1 = {
            'action': v2vActions.GET_VM_FILE,
            'request_id': request_id,
            'message': 'other',
            'start_time': get_datetime_str()
        }
        in_a_s.InstanceActionsServices().add_instance_action_info(v2v_gf_d1)

        # 将copy disk信息存入instance_action表
        v2v_cpd_d1 = {
            'action': v2vActions.COPY_VM_DISK,
            'request_id': request_id,
            'message': 'other',
            'start_time': get_datetime_str()
        }
        in_a_s.InstanceActionsServices().add_instance_action_info(v2v_cpd_d1)

        # 将copy xml信息存入instance_action表
        v2v_cpx_d1 = {
            'action': v2vActions.COPY_VM_XML,
            'request_id': request_id,
            'message': 'other',
            'start_time': get_datetime_str()
        }
        in_a_s.InstanceActionsServices().add_instance_action_info(v2v_cpx_d1)

        # 将创建存储池信息存入instance_action表
        v2v_csp_d1 = {
            'action': v2vActions.CREATE_STOR_POOL,
            'request_id': request_id,
            'message': 'other',
            'start_time': get_datetime_str()
        }
        in_a_s.InstanceActionsServices().add_instance_action_info(v2v_csp_d1)

        # 将vm标准化信息存入instance_action表
        v2v_vmd_d1 = {
            'action': v2vActions.VM_STANDARDLIZE,
            'request_id': request_id,
            'message': 'other',
            'start_time': get_datetime_str()
        }
        in_a_s.InstanceActionsServices().add_instance_action_info(v2v_vmd_d1)

        # 将vm注册信息存入instance_action表
        v2v_vmdef_d1 = {
            'action': v2vActions.VM_DEFINE,
            'request_id': request_id,
            'message': 'other',
            'start_time': get_datetime_str()
        }
        in_a_s.InstanceActionsServices().add_instance_action_info(v2v_vmdef_d1)

        # 将IP注入信息存入instance_action表
        v2v_vmipj_d1 = {
            'action': v2vActions.IP_INJECT,
            'request_id': request_id,
            'message': 'other',
            'start_time': get_datetime_str()
        }
        in_a_s.InstanceActionsServices().add_instance_action_info(v2v_vmipj_d1)

        # 将vm开机信息存入instance_action表
        v2v_vmstar_d1 = {
            'action': v2vActions.VM_START,
            'request_id': request_id,
            'message': 'other',
            'start_time': get_datetime_str()
        }
        in_a_s.InstanceActionsServices().add_instance_action_info(
            v2v_vmstar_d1)

        message = '信息已添加至任务队列,等待执行'

        # 将v2v信息存入v2v_task表
        v2v_data = {
            'request_id': request_id,
            'destory': '0',
            'start_time': get_datetime_str(),
            'status': 0,
            'vm_ip': vmip,
            'vm_name': vmname,
            'vmvlan': vmvlan,
            'flavor_id': flavor_id,
            'cloud_area': cloudarea,
            'vm_ostype': vm_ostype,
            'vm_app_info': vm_app_info,
            'vm_owner': vm_owner,
            'vm_group_id': vm_group_id,
            'user_id': user_id,
            'vm_mac': vmmac,
            'vm_uuid': vmuuid,
            'cancel': '0',
            'dest_dir': '/app/image/' + vmuuid,
            'on_task': '0',
            'port': '10000',
            'source': VMCreateSource.OPENSTACK
        }
        v2v_insert = v2v_op.v2vTaskService().add_v2v_task_info(v2v_data)

        if v2v_insert.get('row_num') <= 0:
            logging.info('insert info to v2v_task failed! %s', v2v_data)
            message = '信息入库失败'
            return False, message

        # 将目标kvmhost存入信息表
        v2v_op.update_v2v_desthost(request_id, host)

        v2v_op.update_v2v_step(request_id, v2vActions.BEGIN)
        return True, message
Example #8
0
def v2v_openstack_intodb(hostpool_id):
    '''
                v2v_openstack
            :param hostpool_id:
            :return:
    '''

    # 判断是否为重试操作
    retry = request.values.get('retry')
    # 如果非重试进行以下步骤
    if retry != '1':
        # 入参赋值
        vmname = request.values.get('vm_name').strip()
        vmip = request.values.get('vm_ip').strip()
        flavor_id = request.values.get('flavor_id').strip()
        cloudarea = request.values.get('cloud_area').strip()
        vm_ostype = request.values.get('vm_ostype').strip()
        vm_app_info = request.values.get('vm_app_info').strip()
        vm_owner = request.values.get('vm_owner').strip()
        vm_group_id = request.values.get('group_id').strip()
        user_id = request.values.get('user_id').strip()
        vm_segment = request.values.get('segment').strip()

        # 入参完全性判断
        if not vmname or not vmip  or not flavor_id or not cloudarea or not vm_ostype \
                or not vm_app_info or not vm_owner or not vm_group_id or not user_id or not vm_segment:
            logging.info('params are invalid or missing')
            return json_helper.format_api_resp(code=ErrorCode.PARAM_ERR,
                                               msg='入参缺失')
        else:
            # 获取flavor信息
            flavor_info = flavor_service.FlavorService().get_flavor_info(
                flavor_id)
            if not flavor_info:
                logging.info(
                    'id: %s flavor info not in db when create instance',
                    flavor_id)
                return json_helper.format_api_resp(code=ErrorCode.SYS_ERR,
                                                   msg='实例规格数据有误,无法进行v2v')
            vmcpu = flavor_info['vcpu']
            vmmem = flavor_info['memory_mb']

            # 获取对应openstack环境的管理节点及ssh账户信息
            if cloudarea == "SIT":
                ctr_host = '10.202.83.12'
                ctr_pass = decrypt(OPENSTACK_SIT_PASS)
            elif cloudarea == "DEV":
                ctr_host = "10.202.123.4"
                ctr_pass = decrypt(OPENSTACK_DEV_PASS)
            else:
                return json_helper.format_api_resp(
                    code=ErrorCode.SYS_ERR, msg='openstack环境参数错误,无法进行v2v操作')

            #判断vm信息是否输入错误
            vmexist = vm_exist(vmip, ctr_host, ctr_pass)
            if vmexist == False:
                return json_helper.format_api_resp(code=ErrorCode.SYS_ERR,
                                                   msg='获取vm信息失败')

            #获取OS版本失败
            osstat, verdata = get_vm_version(ctr_host, ctr_pass, vm_ostype,
                                             vmip)
            if osstat == False:
                return json_helper.format_api_resp(code=ErrorCode.SYS_ERR,
                                                   msg='获取vmOS版本失败')
            else:
                ver_data = verdata

            # 获取待迁移vm磁盘大小
            vdiskdata = vm_disk_size(vmip, cloudarea, ctr_host, ctr_pass)
            if vdiskdata == False:
                return json_helper.format_api_resp(code=ErrorCode.SYS_ERR,
                                                   msg='获取vm磁盘信息失败')
            vdisk = vdiskdata
            data_disk = vdisk - 80

            # 判断待转化vm是否关机
            vmshutdown = vm_stat(vmip, ctr_host, ctr_pass)
            if vmshutdown == False:
                return json_helper.format_api_resp(code=ErrorCode.SYS_ERR,
                                                   msg='待转化vm未关机')

            # 获取可用目标host
            host_code, host_data, host_msg = cal_host(hostpool_id, vmcpu,
                                                      vmmem, data_disk,
                                                      vm_group_id)
            if host_code < 0:
                return json_helper.format_api_resp(code=host_code,
                                                   msg=host_msg)
            else:
                host = host_data

            vmhost = ho_s.HostService().get_host_info_by_hostip(host)
            ret_4 = ho_s.pre_allocate_host_resource(vmhost['id'], vmcpu, vmmem,
                                                    50)
            if ret_4 != 1:
                logging.error('资源预分配失败')
                message = '资源预分频失败'
                return json_helper.format_api_resp(code=ErrorCode.SYS_ERR,
                                                   msg=message)

            # 获取并录入IP信息

            vm_segment = seg_s.SegmentService().get_segment_info_bysegment(
                vm_segment)
            if vm_segment == None:
                return json_helper.format_api_resp(code=ErrorCode.SYS_ERR,
                                                   msg='网段信息有误,无法进行v2v')
            else:
                segment_id = vm_segment['id']
                vm_netmask = vm_segment['netmask']
                vm_gateway = vm_segment['gateway_ip']
                vm_dns1 = vm_segment['dns1']
                vm_dns2 = vm_segment['dns2']
                vmvlan = vm_segment['vlan']
                ip_data = ip_s.IPService().get_ip_info_by_ipaddress(vmip)
                if ip_data == None:
                    ip_data = {
                        'ip_address': vmip,
                        'segment_id': segment_id,
                        'netmask': vm_netmask,
                        'vlan': vmvlan,
                        'gateway_ip': vm_gateway,
                        'dns1': vm_dns1,
                        'dns2': vm_dns2,
                        'created_at': get_datetime_str(),
                        'status': '1'
                    }
                    ret = ip_s.IPService().add_ip_info(ip_data)
                    if ret.get('row_num') <= 0:
                        logging.info(
                            'add ip info error when create v2v task, insert_data: %s',
                            ip_data)
                        return json_helper.format_api_resp(
                            code=ErrorCode.SYS_ERR, msg="录入IP信息失败")
                    else:
                        ip_id = ret.get('last_id')
                else:
                    ip_data_status = ip_data['status']
                    vmvlan = ip_data['vlan']
                    if ip_data_status != '0':
                        return json_helper.format_api_resp(
                            code=ErrorCode.SYS_ERR, msg="IP与现有环境冲突,无法进行v2v")
                    else:
                        ip_id = ip_data['id']
                        where_data = {'id': ip_id}
                        updata_data = {
                            'status': '1',
                            'updated_at': get_datetime_str()
                        }
                        ret1 = ip_s.IPService().update_ip_info(
                            updata_data, where_data)
                        if not ret1:
                            logging.info(
                                'update ip info error when create v2v task, update_data: %s',
                                updata_data)
                            return json_helper.format_api_resp(
                                code=ErrorCode.SYS_ERR, msg="更新IP状态失败")

            # 生成request_id
            request_Id = v2v_op.generate_req_id()

            # 生成vm的uuid和mac
            vmuuid = randomUUID()
            vmmac = randomMAC()

            # 信息入instance相关库表
            instance_info = instance_db_info(vmuuid, vmname, vm_app_info,
                                             vm_owner, flavor_id, vm_group_id,
                                             host, vmmac, data_disk, ip_id,
                                             vm_ostype, request_Id, ver_data)
            if instance_info < 0:
                return json_helper.format_api_resp(code=ErrorCode.SYS_ERR,
                                                   msg='信息入库失败')

            #   将步骤信息存入instance_action表
            # 将createdir信息存入instance_action表
            v2v_cd_d1 = {
                'action': v2vActions.CREATE_DEST_DIR,
                'request_id': request_Id,
                'message': 'start',
                'start_time': get_datetime_str()
            }
            in_a_s.InstanceActionsServices().add_instance_action_info(
                v2v_cd_d1)

            # 将getfile信息存入instance_action表
            v2v_gf_d1 = {
                'action': v2vActions.GET_VM_FILE,
                'request_id': request_Id,
                'message': 'other',
                'start_time': get_datetime_str()
            }
            in_a_s.InstanceActionsServices().add_instance_action_info(
                v2v_gf_d1)

            # 将copy disk信息存入instance_action表
            v2v_cpd_d1 = {
                'action': v2vActions.COPY_VM_DISK,
                'request_id': request_Id,
                'message': 'other',
                'start_time': get_datetime_str()
            }
            in_a_s.InstanceActionsServices().add_instance_action_info(
                v2v_cpd_d1)

            # 将copy xml信息存入instance_action表
            v2v_cpx_d1 = {
                'action': v2vActions.COPY_VM_XML,
                'request_id': request_Id,
                'message': 'other',
                'start_time': get_datetime_str()
            }
            in_a_s.InstanceActionsServices().add_instance_action_info(
                v2v_cpx_d1)

            # 将创建存储池信息存入instance_action表
            v2v_csp_d1 = {
                'action': v2vActions.CREATE_STOR_POOL,
                'request_id': request_Id,
                'message': 'other',
                'start_time': get_datetime_str()
            }
            in_a_s.InstanceActionsServices().add_instance_action_info(
                v2v_csp_d1)

            # 将vm标准化信息存入instance_action表
            v2v_vmd_d1 = {
                'action': v2vActions.VM_STANDARDLIZE,
                'request_id': request_Id,
                'message': 'other',
                'start_time': get_datetime_str()
            }
            in_a_s.InstanceActionsServices().add_instance_action_info(
                v2v_vmd_d1)

            # 将vm注册信息存入instance_action表
            v2v_vmdef_d1 = {
                'action': v2vActions.VM_DEFINE,
                'request_id': request_Id,
                'message': 'other',
                'start_time': get_datetime_str()
            }
            in_a_s.InstanceActionsServices().add_instance_action_info(
                v2v_vmdef_d1)

            # 将IP注入信息存入instance_action表
            v2v_vmipj_d1 = {
                'action': v2vActions.IP_INJECT,
                'request_id': request_Id,
                'message': 'other',
                'start_time': get_datetime_str()
            }
            in_a_s.InstanceActionsServices().add_instance_action_info(
                v2v_vmipj_d1)

            # 将vm开机信息存入instance_action表
            v2v_vmstar_d1 = {
                'action': v2vActions.VM_START,
                'request_id': request_Id,
                'message': 'other',
                'start_time': get_datetime_str()
            }
            in_a_s.InstanceActionsServices().add_instance_action_info(
                v2v_vmstar_d1)

            message = '信息已添加至任务队列,等待执行'

            # 将v2v信息存入v2v_task表
            v2v_data = {
                'request_id': request_Id,
                'destory': '0',
                'start_time': get_datetime_str(),
                'status': 0,
                'vm_ip': vmip,
                'vm_name': vmname,
                'vmvlan': vmvlan,
                'flavor_id': flavor_id,
                'cloud_area': cloudarea,
                'vm_ostype': vm_ostype,
                'vm_app_info': vm_app_info,
                'vm_owner': vm_owner,
                'vm_group_id': vm_group_id,
                'user_id': user_id,
                'vm_mac': vmmac,
                'vm_uuid': vmuuid,
                'cancel': '0',
                'dest_dir': '/app/image/' + vmuuid,
                'on_task': '0',
                'port': '10000',
                'source': VMCreateSource.OPENSTACK
            }
            v2v_insert = v2v_op.v2vTaskService().add_v2v_task_info(v2v_data)

            if v2v_insert.get('row_num') <= 0:
                logging.info('insert info to v2v_task failed! %s', v2v_data)
                return json_helper.format_api_resp(code=ErrorCode.SYS_ERR,
                                                   msg='信息入库失败')

            # 将目标kvmhost存入信息表
            v2v_op.update_v2v_desthost(request_Id, host)

            v2v_op.update_v2v_step(request_Id, v2vActions.BEGIN)
            return json_helper.format_api_resp(code=ErrorCode.SUCCESS,
                                               msg=message)
def __instance_add_netcard(instance_netcard_datas):
    # 添加任务信息
    task_id = instance_s.generate_task_id()
    insert_data = {
        'action': InstaceActions.INSTANCE_ADD_NETCARD,
        'instance_uuid': instance_netcard_datas['instance_uuid'],
        'request_id': task_id,
        'task_id': task_id,
        'start_time': get_datetime_str()
    }
    ret_action = instance_action_s.InstanceActionsServices(
    ).add_instance_action_info(insert_data)
    if ret_action.get('row_num') < 1:
        logging.error(
            'add instance change configure action info error, insert_data:%s',
            insert_data)
        return False, "添加操作步骤到instance_action表失败"
    # 判断要申请ip是否被分配了,如果是生产或容灾环境,需要判断其对应的容灾或生产ip是否为未使用状态
    net_ip_list = []
    new_ip_status = ip_s.IPService().get_ip_by_ip_address(
        instance_netcard_datas['ip_addr_new'])
    net_ip_list.append(new_ip_status)
    new_ip_segment_data = segment_s.SegmentService().get_segment_info(
        new_ip_status['segment_id'])
    if not new_ip_status or not new_ip_segment_data:
        _msg = '网卡配置ip:无法获取数据库中ip信息或ip对应的网段信息'
        _job_status = ActionStatus.FAILD
        __update_config_msg_to_db(task_id, _msg, _job_status)
        return False, "无法获取数据库中待使用ip信息或ip对应的网段信息"

    if new_ip_status['status'] != IPStatus.USED:
        _msg = '网卡配置ip:新ip:%s不是使用中状态' % instance_netcard_datas['ip_addr_new']
        _job_status = ActionStatus.FAILD
        __update_config_msg_to_db(task_id, _msg, _job_status)
        return False, "新ip:%s不是使用中状态" % instance_netcard_datas['ip_addr_new']
    # 需要修改xml配置,同时做ip、网关修改注入
    instance_netcard_datas['netmask_new'] = __exchange_maskint(
        int(new_ip_status['netmask']))
    instance_netcard_datas['gateway_new'] = new_ip_status['gateway_ip']

    dev_name = new_ip_segment_data['host_bridge_name'] + '.' + str(
        new_ip_segment_data['vlan'])
    net_on_status, _msg = _instance_net_on(
        instance_netcard_datas['instance_name'],
        instance_netcard_datas['mac_addr'], instance_netcard_datas['host_ip'],
        dev_name)
    if not net_on_status:
        _job_status = ActionStatus.FAILD
        __update_config_msg_to_db(task_id, _msg, _job_status)
        _change_db_ip_unused(net_ip_list)
        return False, _msg

    ret_status, ret_msg = __change_instance_network(
        instance_netcard_datas['host_ip'],
        instance_netcard_datas['instance_name'], instance_netcard_datas)
    if ret_status:
        db_ret_status, db_ret_msg = __instance_ip_configure_change_db(
            instance_netcard_datas['instance_id'], instance_netcard_datas,
            instance_netcard_datas['env'])
        if not db_ret_status:
            _job_status = ActionStatus.FAILD
            __update_config_msg_to_db(task_id, db_ret_msg, _job_status)
            _change_db_ip_unused(net_ip_list)
            return False, db_ret_msg

        _job_status = ActionStatus.SUCCSESS
        __update_config_msg_to_db(task_id, db_ret_msg, _job_status)
    else:
        _job_status = ActionStatus.FAILD
        __update_config_msg_to_db(task_id, ret_msg, _job_status)
        _change_db_ip_unused(net_ip_list)
        return False, ret_msg

    return True, "网卡配置成功"