Ejemplo n.º 1
0
def _create_storage_pool(host_ip, uuid, request_id):
    '''
        创建存储池
    :param host_ip:
    :param uuid:
    :return:
    '''
    connect_storages = instanceManager.libvirt_get_connect(
        host_ip, conn_type='storages')
    pool_status, pool_name = instanceManager.libvirt_create_storage_pool(
        connect_storages, uuid)
    if pool_status:
        logging.info('create storage pool successful')
        message = '创建存储池成功'
        threadlock = threading.Lock()
        threadlock.acquire()
        v2v_op.updata_v2v_message(request_id, message)
        v2v_op.updata_v2v_ontask(request_id, '0')
        in_a_s.update_instance_actions(request_id,
                                       esx_v2vActions.CREATE_STOR_POOL,
                                       ActionStatus.SUCCSESS, message)
        v2v_op.update_v2v_step(request_id, esx_v2vActions.CREATE_STOR_POOL)
        threadlock.release()
    else:
        msg = "创建存储池失败"
        threadlock = threading.Lock()
        threadlock.acquire()
        v2v_op.updata_v2v_message(request_id, msg)
        v2v_op.updata_v2v_ontask(request_id, '0')
        v2v_op.update_v2v_actions(request_id, ActionStatus.FAILD)
        in_a_s.update_instance_actions(request_id,
                                       esx_v2vActions.CREATE_STOR_POOL,
                                       ActionStatus.FAILD, msg)
        threadlock.release()
Ejemplo n.º 2
0
def vm_start2(kvmhost, vmname, request_id, modulename, vm_uuid):
    command = 'virsh start ' + vmname
    remote_user = ANSIABLE_REMOTE_USER
    remote_pass = decrypt(KVMHOST_LOGIN_PASS)
    become_user = OPENSTACK_DEV_USER
    become_pass = decrypt(KVMHOST_SU_PASS)
    vmstart = ansible_run_shell(kvmhost, command)
    if 'contacted' not in vmstart:
        message = 'vm启动失败'
        threadlock = threading.Lock()
        threadlock.acquire()
        v2v_op.updata_v2v_message(request_id, message)
        v2v_op.update_v2v_actions(request_id, ActionStatus.FAILD)
        v2v_op.updata_v2v_ontask(request_id, '0')
        in_a_s.update_instance_actions(request_id, esx_v2vActions.VM_START2,
                                       ActionStatus.FAILD, message)
        threadlock.release()
    elif vmstart['contacted'] == {}:
        message = 'vm启动失败'
        threadlock = threading.Lock()
        threadlock.acquire()
        v2v_op.updata_v2v_message(request_id, message)
        v2v_op.update_v2v_actions(request_id, ActionStatus.FAILD)
        v2v_op.updata_v2v_ontask(request_id, '0')
        in_a_s.update_instance_actions(request_id, esx_v2vActions.VM_START2,
                                       ActionStatus.FAILD, message)
        threadlock.release()
    elif 'failed' in vmstart['contacted'][kvmhost]:
        message = 'vm启动失败'
        threadlock = threading.Lock()
        threadlock.acquire()
        v2v_op.updata_v2v_message(request_id, message)
        v2v_op.update_v2v_actions(request_id, ActionStatus.FAILD)
        v2v_op.updata_v2v_ontask(request_id, '0')
        in_a_s.update_instance_actions(request_id, esx_v2vActions.VM_START2,
                                       ActionStatus.FAILD, message)
        threadlock.release()
    elif vmstart['contacted'][kvmhost]['stdout'] == '':
        message = 'vm启动失败'
        threadlock = threading.Lock()
        threadlock.acquire()
        v2v_op.updata_v2v_message(request_id, message)
        v2v_op.update_v2v_actions(request_id, ActionStatus.FAILD)
        v2v_op.updata_v2v_ontask(request_id, '0')
        in_a_s.update_instance_actions(request_id, esx_v2vActions.VM_START2,
                                       ActionStatus.FAILD, message)
        threadlock.release()
    else:
        message = 'vm启动成功'
        threadlock = threading.Lock()
        threadlock.acquire()
        v2v_op.updata_v2v_message(request_id, message)
        in_a_s.update_instance_actions(request_id, esx_v2vActions.VM_START2,
                                       ActionStatus.SUCCSESS, message)
        v2v_op.updata_v2v_ontask(request_id, '0')
        v2v_op.update_v2v_step(request_id, esx_v2vActions.VM_START2)
        threadlock.release()
Ejemplo n.º 3
0
def createdir(kvmhost, request_id, modulename, dir):

    command = 'mkdir -p ' + dir
    remote_user = ANSIABLE_REMOTE_USER
    remote_pass = decrypt(KVMHOST_LOGIN_PASS)
    become_user = OPENSTACK_DEV_USER
    become_pass = decrypt(KVMHOST_SU_PASS)
    dir_result = ansible_run_shell(kvmhost, command)
    if 'contacted' not in dir_result:
        message = '创建vm目录失败'
        threadlock = threading.Lock()
        threadlock.acquire()
        v2v_op.updata_v2v_message(request_id, message)
        v2v_op.update_v2v_actions(request_id, ActionStatus.FAILD)
        v2v_op.updata_v2v_ontask(request_id, '0')
        in_a_s.update_instance_actions(request_id,
                                       esx_v2vActions.CREATE_DEST_DIR,
                                       ActionStatus.FAILD, message)
        threadlock.release()
    elif dir_result['contacted'] == {}:
        message = '创建vm目录失败'
        threadlock = threading.Lock()
        threadlock.acquire()
        v2v_op.updata_v2v_message(request_id, message)
        v2v_op.update_v2v_actions(request_id, ActionStatus.FAILD)
        v2v_op.updata_v2v_ontask(request_id, '0')
        in_a_s.update_instance_actions(request_id,
                                       esx_v2vActions.CREATE_DEST_DIR,
                                       ActionStatus.FAILD, message)
        threadlock.release()
    elif 'failed' in dir_result['contacted'][kvmhost]:
        message = '创建vm目录失败'
        threadlock = threading.Lock()
        threadlock.acquire()
        v2v_op.updata_v2v_message(request_id, message)
        v2v_op.update_v2v_actions(request_id, ActionStatus.FAILD)
        v2v_op.updata_v2v_ontask(request_id, '0')
        in_a_s.update_instance_actions(request_id,
                                       esx_v2vActions.CREATE_DEST_DIR,
                                       ActionStatus.FAILD, message)
        threadlock.release()
    else:
        data = dir
        message = '创建vm目录成功'
        threadlock = threading.Lock()
        threadlock.acquire()
        v2v_op.updata_v2v_message(request_id, message)
        in_a_s.update_instance_actions(request_id,
                                       esx_v2vActions.CREATE_DEST_DIR,
                                       ActionStatus.SUCCSESS, message)
        v2v_op.update_v2v_step(request_id, esx_v2vActions.CREATE_DEST_DIR)

        v2v_op.updata_v2v_ontask(request_id, '0')
        v2v_op.update_v2v_destdir(request_id, data)
        threadlock.release()
Ejemplo n.º 4
0
def virt_v2v(dest_dir, kvmhost, request_id, vmware_vm):
    virtv2v_command = 'cd '+ dest_dir +';virt-v2v -i libvirtxml /app/tmp/'+\
                      vmware_vm + '/'+vmware_vm+'.xml -o local -os ' + dest_dir +' -of qcow2 --network bond0'
    remote_user = ANSIABLE_REMOTE_USER
    remote_pass = decrypt(KVMHOST_LOGIN_PASS)
    become_user = OPENSTACK_DEV_USER
    become_pass = decrypt(KVMHOST_SU_PASS)
    virtv2v = ansible_run_shell(kvmhost, virtv2v_command)
    if 'contacted' not in virtv2v:
        message = '无法连接kvmhost'
        threadlock = threading.Lock()
        threadlock.acquire()
        v2v_op.updata_v2v_message(request_id, message)
        v2v_op.update_v2v_actions(request_id, ActionStatus.FAILD)
        v2v_op.updata_v2v_ontask(request_id, '0')
        in_a_s.update_instance_actions(request_id,
                                       esx_v2vActions.VIRT_V2V_FILES,
                                       ActionStatus.FAILD, message)
        threadlock.release()
    elif virtv2v['contacted'] == {}:
        message = '无法连接kvmhost'
        threadlock = threading.Lock()
        threadlock.acquire()
        v2v_op.updata_v2v_message(request_id, message)
        v2v_op.update_v2v_actions(request_id, ActionStatus.FAILD)
        v2v_op.updata_v2v_ontask(request_id, '0')
        in_a_s.update_instance_actions(request_id,
                                       esx_v2vActions.VIRT_V2V_FILES,
                                       ActionStatus.FAILD, message)
        threadlock.release()
    elif 'error' in virtv2v['contacted'][kvmhost]['stderr']:
        message = '转化vm文件失败'
        threadlock = threading.Lock()
        threadlock.acquire()
        v2v_op.updata_v2v_message(request_id, message)
        v2v_op.update_v2v_actions(request_id, ActionStatus.FAILD)
        v2v_op.updata_v2v_ontask(request_id, '0')
        in_a_s.update_instance_actions(request_id,
                                       esx_v2vActions.VIRT_V2V_FILES,
                                       ActionStatus.FAILD, message)
        threadlock.release()
    else:
        message = '转化vm文件成功'
        threadlock = threading.Lock()
        threadlock.acquire()
        v2v_op.updata_v2v_message(request_id, message)
        in_a_s.update_instance_actions(request_id,
                                       esx_v2vActions.VIRT_V2V_FILES,
                                       ActionStatus.SUCCSESS, message)
        v2v_op.update_v2v_step(request_id, esx_v2vActions.VIRT_V2V_FILES)
        v2v_op.updata_v2v_ontask(request_id, '0')
        threadlock.release()
Ejemplo n.º 5
0
def ch_sys_disk_name(dest_dir, kvmhost, vmware_vm, vm_name, request_id):
    remote_user = ANSIABLE_REMOTE_USER
    remote_pass = decrypt(KVMHOST_LOGIN_PASS)
    become_user = OPENSTACK_DEV_USER
    become_pass = decrypt(KVMHOST_SU_PASS)
    change_sysdisk_name = 'cd ' + dest_dir + ';mv ' + vmware_vm + '-sda ' + vm_name + '.img'
    chsysdiskname = ansible_run_shell(kvmhost, change_sysdisk_name)
    if 'contacted' not in chsysdiskname:
        message = '无法连接kvmhost'
        threadlock = threading.Lock()
        threadlock.acquire()
        v2v_op.updata_v2v_message(request_id, message)
        v2v_op.update_v2v_actions(request_id, ActionStatus.FAILD)
        v2v_op.updata_v2v_ontask(request_id, '0')
        in_a_s.update_instance_actions(request_id,
                                       esx_v2vActions.VM_SYS_DISK_STD,
                                       ActionStatus.FAILD, message)
        threadlock.release()
    elif chsysdiskname['contacted'] == {}:
        message = '无法连接kvmhost'
        threadlock = threading.Lock()
        threadlock.acquire()
        v2v_op.updata_v2v_message(request_id, message)
        v2v_op.update_v2v_actions(request_id, ActionStatus.FAILD)
        v2v_op.updata_v2v_ontask(request_id, '0')
        in_a_s.update_instance_actions(request_id,
                                       esx_v2vActions.VM_SYS_DISK_STD,
                                       ActionStatus.FAILD, message)
        threadlock.release()
    elif 'error' in chsysdiskname['contacted'][kvmhost]['stderr']:
        message = '标准化vm系统盘文件失败'
        threadlock = threading.Lock()
        threadlock.acquire()
        v2v_op.updata_v2v_message(request_id, message)
        v2v_op.update_v2v_actions(request_id, ActionStatus.FAILD)
        v2v_op.updata_v2v_ontask(request_id, '0')
        in_a_s.update_instance_actions(request_id,
                                       esx_v2vActions.VM_SYS_DISK_STD,
                                       ActionStatus.FAILD, message)
        threadlock.release()
    else:
        message = '标准化vm系统盘文件成功'
        threadlock = threading.Lock()
        threadlock.acquire()
        v2v_op.updata_v2v_message(request_id, message)
        in_a_s.update_instance_actions(request_id,
                                       esx_v2vActions.VM_SYS_DISK_STD,
                                       ActionStatus.SUCCSESS, message)
        v2v_op.update_v2v_step(request_id, esx_v2vActions.VM_SYS_DISK_STD)
        v2v_op.updata_v2v_ontask(request_id, '0')
        threadlock.release()
Ejemplo n.º 6
0
def rmrawfile(kvmhost, vmware_vm, request_id):
    remote_user = ANSIABLE_REMOTE_USER
    remote_pass = decrypt(KVMHOST_LOGIN_PASS)
    become_user = OPENSTACK_DEV_USER
    become_pass = decrypt(KVMHOST_SU_PASS)
    rm_raw_file = 'cd /app/tmp/' + vmware_vm + '/;rm -f ' + vmware_vm + '*;rmdir /app/tmp/' + vmware_vm
    rmrawfile = ansible_run_shell(kvmhost, rm_raw_file)
    if 'contacted' not in rmrawfile:
        message = '无法连接kvmhost'
        threadlock = threading.Lock()
        threadlock.acquire()
        v2v_op.updata_v2v_message(request_id, message)
        v2v_op.update_v2v_actions(request_id, ActionStatus.FAILD)
        v2v_op.updata_v2v_ontask(request_id, '0')
        in_a_s.update_instance_actions(request_id,
                                       esx_v2vActions.DELETE_TMP_FILE,
                                       ActionStatus.FAILD, message)
        threadlock.release()
    elif rmrawfile['contacted'] == {}:
        message = '无法连接kvmhost'
        threadlock = threading.Lock()
        threadlock.acquire()
        v2v_op.updata_v2v_message(request_id, message)
        v2v_op.update_v2v_actions(request_id, ActionStatus.FAILD)
        v2v_op.updata_v2v_ontask(request_id, '0')
        in_a_s.update_instance_actions(request_id,
                                       esx_v2vActions.DELETE_TMP_FILE,
                                       ActionStatus.FAILD, message)
        threadlock.release()
    elif 'error' in rmrawfile['contacted'][kvmhost]['stderr']:
        message = '删除转化临时文件失败'
        threadlock = threading.Lock()
        threadlock.acquire()
        v2v_op.updata_v2v_message(request_id, message)
        v2v_op.update_v2v_actions(request_id, ActionStatus.FAILD)
        v2v_op.updata_v2v_ontask(request_id, '0')
        in_a_s.update_instance_actions(request_id,
                                       esx_v2vActions.DELETE_TMP_FILE,
                                       ActionStatus.FAILD, message)
        threadlock.release()
    else:
        message = '删除转化临时文件完成'
        threadlock = threading.Lock()
        threadlock.acquire()
        v2v_op.updata_v2v_message(request_id, message)
        in_a_s.update_instance_actions(request_id,
                                       esx_v2vActions.DELETE_TMP_FILE,
                                       ActionStatus.SUCCSESS, message)
        v2v_op.update_v2v_step(request_id, esx_v2vActions.DELETE_TMP_FILE)
        v2v_op.updata_v2v_ontask(request_id, '0')
        threadlock.release()
Ejemplo n.º 7
0
def win_vm_std(request_id):
    v2v_task = v2v_op.v2vTaskService().get_v2v_task_by_requestid(request_id)
    vmip = v2v_task['vm_ip']
    ostype = 'Windows'
    ip_data = ip_s.IPService().get_ip_by_ip_address(vmip)
    vmmask_int = int(ip_data['netmask'])
    vmmask = exchange_maskint(vmmask_int)
    vmgateway = ip_data['gateway_ip']
    vmname = v2v_task['vm_name']
    dns1 = ip_data['dns1']
    dns2 = ip_data['dns2']
    host_ip = v2v_task['dest_host']
    cloudarea = v2v_task['cloud_area']
    connect_instance = instanceManager.libvirt_get_connect(
        host_ip, conn_type='instance', vmname=vmname)
    inject_stauts, mesg = instanceManager.v2v_esx_win_inject(
        connect_instance, vmname, vmip, vmgateway, dns1, dns2, vmmask, ostype,
        cloudarea)
    if inject_stauts:
        message = "信息注入成功"
        threadlock = threading.Lock()
        threadlock.acquire()
        v2v_op.updata_v2v_message(request_id, message)
        in_a_s.update_instance_actions(request_id, esx_v2vActions.WINDOWS_STD,
                                       ActionStatus.SUCCSESS, message)
        v2v_op.update_v2v_step(request_id, esx_v2vActions.WINDOWS_STD)
        v2v_op.updata_v2v_ontask(request_id, '0')
        vm_uuid = v2v_op.v2vTaskService().get_v2v_task_by_requestid(
            request_id)['vm_uuid']
        v2v_op.update_v2v_actions(request_id, 1)
        v2v_op.update_v2v_step(request_id, esx_v2vActions.WINDOWS_STD)
        where_data = {'uuid': vm_uuid}
        update_data = {'status': '3'}
        ins_s.InstanceService().update_instance_info(update_data, where_data)
        threadlock.release()
    else:
        message = "信息注入失败"
        threadlock = threading.Lock()
        threadlock.acquire()
        v2v_op.updata_v2v_message(request_id, message)
        v2v_op.update_v2v_actions(request_id, ActionStatus.FAILD)
        v2v_op.updata_v2v_ontask(request_id, '0')
        in_a_s.update_instance_actions(request_id, esx_v2vActions.WINDOWS_STD,
                                       ActionStatus.FAILD, message)
        threadlock.release()
Ejemplo n.º 8
0
def win_disk_ch(vm_name, kvmhost, request_id):
    vmsd, msg = vm_shutdown(kvmhost, vm_name)
    if not vmsd:
        message = msg
        threadlock = threading.Lock()
        threadlock.acquire()
        v2v_op.updata_v2v_message(request_id, message)
        v2v_op.update_v2v_actions(request_id, ActionStatus.FAILD)
        v2v_op.updata_v2v_ontask(request_id, '0')
        in_a_s.update_instance_actions(request_id,
                                       esx_v2vActions.WINDOWS_DISK_CH,
                                       ActionStatus.FAILD, message)
        threadlock.release()
    else:
        disktype = 'virtio'
        v2v_win_disk_ch = instanceManager.v2v_win_disk_ch(
            kvmhost, vm_name, disktype)
        if not v2v_win_disk_ch:
            message = '修改磁盘格式失败'
            threadlock = threading.Lock()
            threadlock.acquire()
            v2v_op.updata_v2v_message(request_id, message)
            v2v_op.update_v2v_actions(request_id, ActionStatus.FAILD)
            v2v_op.updata_v2v_ontask(request_id, '0')
            in_a_s.update_instance_actions(request_id,
                                           esx_v2vActions.WINDOWS_DISK_CH,
                                           ActionStatus.FAILD, message)
            threadlock.release()
        else:
            message = '修改磁盘格式成功'
            threadlock = threading.Lock()
            threadlock.acquire()
            v2v_op.updata_v2v_message(request_id, message)
            in_a_s.update_instance_actions(request_id,
                                           esx_v2vActions.WINDOWS_DISK_CH,
                                           ActionStatus.SUCCSESS, message)
            v2v_op.updata_v2v_ontask(request_id, '0')
            v2v_op.update_v2v_step(request_id, esx_v2vActions.WINDOWS_DISK_CH)
            threadlock.release()
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
Ejemplo n.º 10
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)
Ejemplo n.º 11
0
def v2v_esx_disk_attach_static(vm_name, kvmhost, request_id):
    create_disk = 'cd /tmp;rm -f diskimg;qemu-img create -f qcow2 diskimg 20G'
    remote_user = ANSIABLE_REMOTE_USER
    remote_pass = decrypt(KVMHOST_LOGIN_PASS)
    become_user = OPENSTACK_DEV_USER
    become_pass = decrypt(KVMHOST_SU_PASS)
    createdisk = ansible_run_shell(kvmhost, create_disk)
    if 'contacted' not in createdisk:
        message = '连接目标kvmhost失败'
        threadlock = threading.Lock()
        threadlock.acquire()
        v2v_op.updata_v2v_message(request_id, message)
        v2v_op.update_v2v_actions(request_id, ActionStatus.FAILD)
        v2v_op.updata_v2v_ontask(request_id, '0')
        in_a_s.update_instance_actions(request_id, esx_v2vActions.ATTACH_DISK,
                                       ActionStatus.FAILD, message)
        threadlock.release()
    elif createdisk['contacted'] == {}:
        message = '连接目标kvmhost失败'
        threadlock = threading.Lock()
        threadlock.acquire()
        v2v_op.updata_v2v_message(request_id, message)
        v2v_op.update_v2v_actions(request_id, ActionStatus.FAILD)
        v2v_op.updata_v2v_ontask(request_id, '0')
        in_a_s.update_instance_actions(request_id, esx_v2vActions.ATTACH_DISK,
                                       ActionStatus.FAILD, message)
        threadlock.release()
    elif 'failed' in createdisk['contacted'][kvmhost]:
        message = '创建临时磁盘失败'
        threadlock = threading.Lock()
        threadlock.acquire()
        v2v_op.updata_v2v_message(request_id, message)
        v2v_op.update_v2v_actions(request_id, ActionStatus.FAILD)
        v2v_op.updata_v2v_ontask(request_id, '0')
        in_a_s.update_instance_actions(request_id, esx_v2vActions.ATTACH_DISK,
                                       ActionStatus.FAILD, message)
        threadlock.release()
    else:
        time.sleep(200)
        xml = """
                    <disk type='file' device='disk'>
                        <driver name='qemu' type='qcow2'/>
                        <source file='/tmp/diskimg'/>
                        <target dev='vdi' bus='virtio'/>
                    </disk>"""
        att_device = instanceManager.v2v_esx_attach_device(
            kvmhost, vm_name, xml)
        if not att_device:
            message = '添加临时磁盘失败'
            threadlock = threading.Lock()
            threadlock.acquire()
            v2v_op.updata_v2v_message(request_id, message)
            v2v_op.update_v2v_actions(request_id, ActionStatus.FAILD)
            v2v_op.updata_v2v_ontask(request_id, '0')
            in_a_s.update_instance_actions(request_id,
                                           esx_v2vActions.ATTACH_DISK,
                                           ActionStatus.FAILD, message)
            threadlock.release()
        else:
            message = '添加临时磁盘成功'
            time.sleep(15)
            threadlock = threading.Lock()
            threadlock.acquire()
            v2v_op.updata_v2v_message(request_id, message)
            in_a_s.update_instance_actions(request_id,
                                           esx_v2vActions.ATTACH_DISK,
                                           ActionStatus.SUCCSESS, message)
            v2v_op.updata_v2v_ontask(request_id, '0')
            v2v_op.update_v2v_step(request_id, esx_v2vActions.ATTACH_DISK)
            threadlock.release()
Ejemplo n.º 12
0
def vm_start1(kvmhost, vmname, request_id, modulename, vm_ostype):
    command = 'virsh start ' + vmname
    remote_user = ANSIABLE_REMOTE_USER
    remote_pass = decrypt(KVMHOST_LOGIN_PASS)
    become_user = OPENSTACK_DEV_USER
    become_pass = decrypt(KVMHOST_SU_PASS)
    vmstart = ansible_run_shell(kvmhost, command)
    if 'contacted' not in vmstart:
        message = 'vm启动失败'
        threadlock = threading.Lock()
        threadlock.acquire()
        v2v_op.updata_v2v_message(request_id, message)
        v2v_op.update_v2v_actions(request_id, ActionStatus.FAILD)
        v2v_op.updata_v2v_ontask(request_id, '0')
        in_a_s.update_instance_actions(request_id, esx_v2vActions.VM_START1,
                                       ActionStatus.FAILD, message)
        threadlock.release()
    elif vmstart['contacted'] == {}:
        message = 'vm启动失败'
        threadlock = threading.Lock()
        threadlock.acquire()
        v2v_op.updata_v2v_message(request_id, message)
        v2v_op.update_v2v_actions(request_id, ActionStatus.FAILD)
        v2v_op.updata_v2v_ontask(request_id, '0')
        in_a_s.update_instance_actions(request_id, esx_v2vActions.VM_START1,
                                       ActionStatus.FAILD, message)
        threadlock.release()
    elif 'failed' in vmstart['contacted'][kvmhost]:
        message = 'vm启动失败'
        threadlock = threading.Lock()
        threadlock.acquire()
        v2v_op.updata_v2v_message(request_id, message)
        v2v_op.update_v2v_actions(request_id, ActionStatus.FAILD)
        v2v_op.updata_v2v_ontask(request_id, '0')
        in_a_s.update_instance_actions(request_id, esx_v2vActions.VM_START1,
                                       ActionStatus.FAILD, message)
        threadlock.release()
    elif vmstart['contacted'][kvmhost]['stdout'] == '':
        message = 'vm启动失败'
        threadlock = threading.Lock()
        threadlock.acquire()
        v2v_op.updata_v2v_message(request_id, message)
        v2v_op.update_v2v_actions(request_id, ActionStatus.FAILD)
        v2v_op.updata_v2v_ontask(request_id, '0')
        in_a_s.update_instance_actions(request_id, esx_v2vActions.VM_START1,
                                       ActionStatus.FAILD, message)
        threadlock.release()
    else:
        message = 'vm启动成功'
        threadlock = threading.Lock()
        threadlock.acquire()
        v2v_op.updata_v2v_message(request_id, message)
        in_a_s.update_instance_actions(request_id, esx_v2vActions.VM_START1,
                                       ActionStatus.SUCCSESS, message)
        v2v_op.updata_v2v_ontask(request_id, '0')
        v2v_op.update_v2v_step(request_id, esx_v2vActions.VM_START1)
        if vm_ostype == "Linux":
            vm_uuid = v2v_op.v2vTaskService().get_v2v_task_by_requestid(
                request_id)['vm_uuid']
            where_data = {'uuid': vm_uuid}
            update_data = {'status': '3'}
            ins_s.InstanceService().update_instance_info(
                update_data, where_data)
            v2v_op.update_v2v_actions(request_id, 1)
        threadlock.release()
Ejemplo n.º 13
0
def vm_define(request_id, vm_ostype, kvmhost, hostname, memory_mb, vcpu, uuid,
              volumes_d, vlan, mac):
    '''

    :param libvirt_connect_create:
    :param hostname: 主机名
    :param memory_mb: 内存大小
    :param vcpu: cpu个数
    :param uuid: instance uuid
    :param volumes_d: 磁盘的字典
    :param net_card:
    :param mac: mac地址
    :param disk_xml: 磁盘的xml文件
    :return:
    '''
    connect_create = instanceManager.libvirt_get_connect(kvmhost)
    if not connect_create:
        message = '连接kvmhost libvirt失败'
        threadlock = threading.Lock()
        threadlock.acquire()
        v2v_op.updata_v2v_message(request_id, message)
        v2v_op.update_v2v_actions(request_id, ActionStatus.FAILD)
        v2v_op.updata_v2v_ontask(request_id, '0')
        in_a_s.update_instance_actions(request_id, esx_v2vActions.VM_DEFINE1,
                                       ActionStatus.FAILD, message)
        threadlock.release()
    else:
        vm_vlan = 'br_bond0.' + vlan
        connect_create.refresh_storage_pool_by_name(uuid)
        succeed_create_xml = False
        retry_create_xml = 0
        while retry_create_xml < 3 and not succeed_create_xml:
            try:
                if vm_ostype == "Linux":
                    instance_xml = connect_create.v2v_esx_xml(
                        hostname, memory_mb, vcpu, False, uuid, volumes_d,
                        'default', vm_vlan, True, mac)
                else:
                    instance_xml = connect_create.v2v_esx_xml(
                        hostname, memory_mb, vcpu, False, uuid, volumes_d,
                        'default', vm_vlan, False, mac)

                succeed_create_xml = True
            except libvirtError as err:
                logging.error(
                    "create host connect failed ,name: %s ;because %s" %
                    (hostname, err))
                retry_create_xml += 1
                time.sleep(5)

        if retry_create_xml == 3:
            message = "vm注册失败"
            threadlock = threading.Lock()
            threadlock.acquire()
            v2v_op.updata_v2v_message(request_id, message)
            v2v_op.update_v2v_actions(request_id, ActionStatus.FAILD)
            v2v_op.updata_v2v_ontask(request_id, '0')
            in_a_s.update_instance_actions(request_id,
                                           esx_v2vActions.VM_DEFINE1,
                                           ActionStatus.FAILD, message)
            threadlock.release()
            return False, err
        else:
            message = 'vm注册成功'
            threadlock = threading.Lock()
            threadlock.acquire()
            v2v_op.updata_v2v_message(request_id, message)
            in_a_s.update_instance_actions(request_id,
                                           esx_v2vActions.VM_DEFINE1,
                                           ActionStatus.SUCCSESS, message)
            v2v_op.update_v2v_step(request_id, esx_v2vActions.VM_DEFINE1)
            v2v_op.updata_v2v_ontask(request_id, '0')
            threadlock.release()
            return True, instance_xml
Ejemplo n.º 14
0
def vm_data_disk_std(dest_dir, kvmhost, vm_name, request_id, vmware_vm):
    datadisk_tag, datadisk_res = get_vm_data_disk(dest_dir, kvmhost)
    if not datadisk_tag:
        fail_msg = datadisk_res
        threadlock = threading.Lock()
        threadlock.acquire()
        v2v_op.updata_v2v_message(request_id, fail_msg)
        v2v_op.update_v2v_actions(request_id, ActionStatus.FAILD)
        v2v_op.updata_v2v_ontask(request_id, '0')
        in_a_s.update_instance_actions(request_id,
                                       esx_v2vActions.VM_DATA_DISK_STD,
                                       ActionStatus.FAILD, fail_msg)
        threadlock.release()
    else:
        if int(datadisk_res) == 0:
            message = "该vm无数据盘"
            vmlistdata = '0'
            v2v_op.up_esxv2v_vlist(request_id, vmlistdata)
            rm_vmfolder_res, rm_vmfolder_msg = del_vm_folder_file(
                dest_dir, kvmhost, vmware_vm)
            if not rm_vmfolder_res:
                message = "重命名vm数据盘成功,临时文件清理失败"
            else:
                message = "重命名vm数据盘成功"
            threadlock = threading.Lock()
            threadlock.acquire()
            v2v_op.updata_v2v_message(request_id, message)
            in_a_s.update_instance_actions(request_id,
                                           esx_v2vActions.VM_DATA_DISK_STD,
                                           ActionStatus.SUCCSESS, message)
            v2v_op.update_v2v_step(request_id, esx_v2vActions.VM_DATA_DISK_STD)
            v2v_op.updata_v2v_ontask(request_id, '0')
            threadlock.release()

        else:
            total = 0
            datanum = int(datadisk_res)
            tag = 1
            volumetag = [
                'tag', '-sdb', '-sdc', '-sdd', '-sde', '-sdf', '-sdg', '-sdh',
                '-sdi', '-sdj'
            ]
            while tag <= datanum:
                datadisk = vmware_vm + volumetag[tag]
                res, res_msg = vm_data_disk_rename(datadisk, dest_dir, tag,
                                                   vm_name, kvmhost)
                tag = tag + 1
                total = total + res
            if total > 0:
                message = "重命名vm数据盘失败"
                threadlock = threading.Lock()
                threadlock.acquire()
                v2v_op.updata_v2v_message(request_id, message)
                v2v_op.update_v2v_actions(request_id, ActionStatus.FAILD)
                v2v_op.updata_v2v_ontask(request_id, '0')
                in_a_s.update_instance_actions(request_id,
                                               esx_v2vActions.VM_DATA_DISK_STD,
                                               ActionStatus.FAILD, message)
                threadlock.release()
            else:
                v2v_op.up_esxv2v_vlist(request_id, str(datanum))
                rm_vmfolder_res, rm_vmfolder_msg = del_vm_folder_file(
                    dest_dir, kvmhost, vmware_vm)
                if not rm_vmfolder_res:
                    message = "重命名vm数据盘成功,临时文件清理失败"
                else:
                    message = "重命名vm数据盘成功"
                threadlock = threading.Lock()
                threadlock.acquire()
                v2v_op.updata_v2v_message(request_id, message)
                in_a_s.update_instance_actions(request_id,
                                               esx_v2vActions.VM_DATA_DISK_STD,
                                               ActionStatus.SUCCSESS, message)
                v2v_op.update_v2v_step(request_id,
                                       esx_v2vActions.VM_DATA_DISK_STD)
                v2v_op.updata_v2v_ontask(request_id, '0')
                threadlock.release()
Ejemplo n.º 15
0
def virt_v2v_copy_to_local(dest_dir, kvmhost, esx_ip, esx_passwd, vmware_vm,
                           request_id):
    echopass_command = 'mkdir -p /tmp/' + esx_ip + ';echo ' + esx_passwd + ' >> /tmp/' + esx_ip + '/passwd'
    remote_user = ANSIABLE_REMOTE_USER
    remote_pass = decrypt(KVMHOST_LOGIN_PASS)
    become_user = OPENSTACK_DEV_USER
    become_pass = decrypt(KVMHOST_SU_PASS)
    echopass = ansible_run_shell(kvmhost, echopass_command)
    if 'contacted' not in echopass:
        message = '无法连接kvmhost'
        threadlock = threading.Lock()
        threadlock.acquire()
        v2v_op.updata_v2v_message(request_id, message)
        v2v_op.update_v2v_actions(request_id, ActionStatus.FAILD)
        v2v_op.updata_v2v_ontask(request_id, '0')
        in_a_s.update_instance_actions(request_id,
                                       esx_v2vActions.COPY_FILE_TO_LOCAL,
                                       ActionStatus.FAILD, message)
        threadlock.release()
    elif echopass['contacted'] == {}:
        message = '无法连接kvmhost'
        threadlock = threading.Lock()
        threadlock.acquire()
        v2v_op.updata_v2v_message(request_id, message)
        v2v_op.update_v2v_actions(request_id, ActionStatus.FAILD)
        v2v_op.updata_v2v_ontask(request_id, '0')
        in_a_s.update_instance_actions(request_id,
                                       esx_v2vActions.COPY_FILE_TO_LOCAL,
                                       ActionStatus.FAILD, message)
        threadlock.release()
    elif "error" in echopass['contacted'][kvmhost]['stderr']:
        message = '记录esxi密码失败'
        threadlock = threading.Lock()
        threadlock.acquire()
        v2v_op.updata_v2v_message(request_id, message)
        v2v_op.update_v2v_actions(request_id, ActionStatus.FAILD)
        v2v_op.updata_v2v_ontask(request_id, '0')
        in_a_s.update_instance_actions(request_id,
                                       esx_v2vActions.COPY_FILE_TO_LOCAL,
                                       ActionStatus.FAILD, message)
        threadlock.release()
    else:
        rmfilefirst = 'rm -f /app/tmp/' + vmware_vm + '/' + vmware_vm + '*'
        rmfile_first = ansible_run_shell(kvmhost, rmfilefirst)
        if rmfile_first['contacted'] == {} or "error" in rmfile_first[
                'contacted'][kvmhost]['stderr']:
            message = "清除临时文件失败"
            threadlock = threading.Lock()
            threadlock.acquire()
            v2v_op.updata_v2v_message(request_id, message)
            v2v_op.update_v2v_actions(request_id, ActionStatus.FAILD)
            v2v_op.updata_v2v_ontask(request_id, '0')
            in_a_s.update_instance_actions(request_id,
                                           esx_v2vActions.COPY_FILE_TO_LOCAL,
                                           ActionStatus.FAILD, message)
            threadlock.release()
        else:
            copy_to_local_command = 'mkdir -p /app/tmp/'+ vmware_vm +'/;export TMPDIR=/app/tmp;cd /app/tmp/'+ vmware_vm +'/;virt-v2v-copy-to-local -ic esx://root@' \
                                    + esx_ip + '?no_verify=1 ' + vmware_vm  + ' --password-file ' + '/tmp/' + esx_ip + '/passwd'
            copy_local = ansible_run_shell(kvmhost, copy_to_local_command)
            if copy_local['contacted'] == {}:
                message = '无法连接kvmhost'
                threadlock = threading.Lock()
                threadlock.acquire()
                v2v_op.updata_v2v_message(request_id, message)
                v2v_op.update_v2v_actions(request_id, ActionStatus.FAILD)
                v2v_op.updata_v2v_ontask(request_id, '0')
                in_a_s.update_instance_actions(
                    request_id, esx_v2vActions.COPY_FILE_TO_LOCAL,
                    ActionStatus.FAILD, message)
                threadlock.release()
            elif 'error' in copy_local['contacted'][kvmhost]['stderr']:
                message = '拷贝vm文件到目标host失败'
                threadlock = threading.Lock()
                threadlock.acquire()
                v2v_op.updata_v2v_message(request_id, message)
                v2v_op.update_v2v_actions(request_id, ActionStatus.FAILD)
                v2v_op.updata_v2v_ontask(request_id, '0')
                in_a_s.update_instance_actions(
                    request_id, esx_v2vActions.COPY_FILE_TO_LOCAL,
                    ActionStatus.FAILD, message)
                threadlock.release()
            else:
                rm_esxpass_com = "rm -f /tmp/" + esx_ip + '/passwd;rmdir /tmp/' + esx_ip
                rmesxpass_com = ansible_run_shell(kvmhost, rm_esxpass_com)
                if rmesxpass_com['contacted'] == {} or 'error' in rmesxpass_com[
                        'contacted'][kvmhost]['stderr']:
                    message = 'vm文件拷贝成功,删除esxi密码文件失败'
                else:
                    message = 'vm文件拷贝成功,删除esxi密码文件成功'
                threadlock = threading.Lock()
                threadlock.acquire()
                v2v_op.updata_v2v_message(request_id, message)
                in_a_s.update_instance_actions(
                    request_id, esx_v2vActions.COPY_FILE_TO_LOCAL,
                    ActionStatus.SUCCSESS, message)
                v2v_op.update_v2v_step(request_id,
                                       esx_v2vActions.COPY_FILE_TO_LOCAL)
                v2v_op.updata_v2v_ontask(request_id, '0')
                threadlock.release()