Ejemplo n.º 1
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.º 2
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.º 3
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()
Ejemplo n.º 4
0
def cold_migrate(msg_data):
    '''
        虚机冷迁移
    :param msg_data:
    :return:
    '''
    msg = json_helper.read(msg_data)
    data = msg.get('data')

    request_id = data.get('request_id')
    # migarte表ID
    migrate_tab_id = data.get('migrate_tab_id')
    speed_limit = data.get('speed_limit')
    ins_data_s = data.get('ins_data_s')
    host_data_d = data.get('host_data_d')
    host_data_s = data.get('host_data_s')

    host_ip_d = host_data_d['ipaddress']
    host_id_d = host_data_d['id']
    host_name_d = host_data_d['name']

    host_ip_s = host_data_s['ipaddress']
    host_id_s = host_data_s['id']
    host_name_s = host_data_s['name']

    ins_id_s = ins_data_s['id']
    ins_uuid_s = ins_data_s['uuid']
    ins_name_s = ins_data_s['name']

    user_id = data.get('user_id')

    # todo:先判断该VM是否存在
    data = {
        'action': InstaceActions.INSTANCE_COLD_MIGRATE_CONFIRM_SPEED,
        'instance_uuid': ins_uuid_s,
        'request_id': request_id,
        'user_id': user_id,
        'message': 'start'
    }
    add_instance_actions(data)

    m_flag, m_speed = _confirm_migrate_speed(speed_limit, host_data_s)
    if not m_flag:
        message = 'source host %s confirm migrate speed %s error' % (
            host_id_s, speed_limit)
        logging.error(message)
        _change_migrate_host(host_id_s, host_name_s, ins_id_s)
        update_instance_actions(
            request_id, InstaceActions.INSTANCE_COLD_MIGRATE_CONFIRM_SPEED,
            ActionStatus.FAILD, message)
        ins_m_s.InstanceMigrateService().change_migrate_status(
            migrate_tab_id, MigrateStatus.FAILED)
        return
    else:
        message = 'source host %s confirm migrate speed %s successful' % (
            host_id_s, speed_limit)
        update_instance_actions(
            request_id, InstaceActions.INSTANCE_COLD_MIGRATE_CONFIRM_SPEED,
            ActionStatus.SUCCSESS, message)

    ret_s = ansible_migrate_qos_speed(host_ip_s, host_ip_d, m_speed)
    if not ret_s:
        message = 'host %s ansible migrate qos speed %s Mbit/s error' % (
            host_ip_d, m_speed)
        logging.error(message)
        _change_migrate_host(host_id_s, host_name_s, ins_id_s)
        update_instance_actions(
            request_id, InstaceActions.INSTANCE_COLD_MIGRATE_CONFIRM_SPEED,
            ActionStatus.FAILD, message)
        ins_m_s.InstanceMigrateService().change_migrate_status(
            migrate_tab_id, MigrateStatus.FAILED)
        return
    else:
        message = 'host %s ansible migrate qos speed %s Mbit/s successful' % (
            host_ip_d, m_speed)
        update_instance_actions(
            request_id, InstaceActions.INSTANCE_COLD_MIGRATE_CONFIRM_SPEED,
            ActionStatus.SUCCSESS, message)

    # 开始迁移前获取源端vol、xml文件md5
    data = {
        'action': InstaceActions.INSTANCE_COLD_MIGRATE_VOL_XML_MD5_S,
        'instance_uuid': ins_uuid_s,
        'request_id': request_id,
        'user_id': user_id,
        'message': 'start'
    }
    add_instance_actions(data)

    ins_vol_md5_cmd = 'cd /app/image/' + ins_uuid_s + ';/bin/md5sum *'
    ins_xml_md5_cmd = 'cd /etc/libvirt/qemu;/bin/md5sum ' + ins_data_s[
        'name'] + '.xml'
    ret_v_m, ins_vol_md5_s = ansible_migrate_md5_get(host_ip_s,
                                                     ins_vol_md5_cmd)
    if not ret_v_m:
        message = 'host %s get migrate vol md5 error' % host_id_s
        logging.error(message)
        _change_migrate_host(host_id_s, host_name_s, ins_id_s)
        update_instance_actions(
            request_id, InstaceActions.INSTANCE_COLD_MIGRATE_VOL_XML_MD5_S,
            ActionStatus.FAILD, message)
        ins_m_s.InstanceMigrateService().change_migrate_status(
            migrate_tab_id, MigrateStatus.FAILED)
        return
    else:
        message = 'host %s get migrate vol md5 successful' % host_id_s
        update_instance_actions(
            request_id, InstaceActions.INSTANCE_COLD_MIGRATE_VOL_XML_MD5_S,
            ActionStatus.SUCCSESS, message)

    ret_x_m, ins_xml_md5_s = ansible_migrate_md5_get(host_ip_s,
                                                     ins_xml_md5_cmd)
    if not ret_x_m:
        message = 'host %s get migrate xml md5 error' % host_id_s
        logging.error(message)
        _change_migrate_host(host_id_s, host_name_s, ins_id_s)
        update_instance_actions(
            request_id, InstaceActions.INSTANCE_COLD_MIGRATE_VOL_XML_MD5_S,
            ActionStatus.FAILD, message)
        ins_m_s.InstanceMigrateService().change_migrate_status(
            migrate_tab_id, MigrateStatus.FAILED)
        return
    else:
        message = 'host %s get migrate xml md5 successful' % host_id_s
        update_instance_actions(
            request_id, InstaceActions.INSTANCE_COLD_MIGRATE_VOL_XML_MD5_S,
            ActionStatus.SUCCSESS, message)

    # 在目标主机上新建虚拟机镜像存储目录
    data = {
        'action': InstaceActions.INSTANCE_COLD_MIGRATE_MKDIR_DIR,
        'instance_uuid': ins_uuid_s,
        'request_id': request_id,
        'user_id': user_id,
        'message': 'start'
    }
    add_instance_actions(data)

    ret_status, ret_middle_status, ret_msg = __ansible_remote_mkdir_instance_dir(
        host_ip_d, ins_uuid_s)
    if not ret_status:
        message = 'host %s remote mkdir instance %s dir error' % (host_ip_d,
                                                                  ins_uuid_s)
        logging.error(message)
        _change_migrate_host(host_id_s, host_name_s, ins_id_s)
        update_instance_actions(request_id,
                                InstaceActions.INSTANCE_COLD_MIGRATE_MKDIR_DIR,
                                ActionStatus.FAILD, message)
        ins_m_s.InstanceMigrateService().change_migrate_status(
            migrate_tab_id, MigrateStatus.FAILED)
        return
    else:
        message = 'host %s remote mkdir instance %s dir successful' % (
            host_ip_d, ins_uuid_s)
        update_instance_actions(request_id,
                                InstaceActions.INSTANCE_COLD_MIGRATE_MKDIR_DIR,
                                ActionStatus.SUCCSESS, message)

    # 新建虚拟机池
    data = {
        'action': InstaceActions.INSTANCE_COLD_MIGRATE_STORAGE_POOL,
        'instance_uuid': ins_uuid_s,
        'request_id': request_id,
        'user_id': user_id,
        'message': 'start'
    }
    add_instance_actions(data)

    logging.info('start create storage pool %s', ins_uuid_s)
    ret_p = _create_storage_pool(host_ip_d, ins_uuid_s)
    if not ret_p:
        message = 'host %s create storage pool error' % host_ip_d
        logging.error(message)
        _change_migrate_host(host_id_s, host_name_s, ins_id_s)
        update_instance_actions(
            request_id, InstaceActions.INSTANCE_COLD_MIGRATE_STORAGE_POOL,
            ActionStatus.FAILD, message)
        ins_m_s.InstanceMigrateService().change_migrate_status(
            migrate_tab_id, MigrateStatus.FAILED)
        return
    else:
        message = 'host %s create storage pool successful' % host_ip_d
        update_instance_actions(
            request_id, InstaceActions.INSTANCE_COLD_MIGRATE_STORAGE_POOL,
            ActionStatus.SUCCSESS, message)

    # 拷贝vol和xml文件
    data = {
        'action': InstaceActions.INSTANCE_COLD_MIGRATE_VOL_XML_COPY,
        'instance_uuid': ins_uuid_s,
        'request_id': request_id,
        'user_id': user_id,
        'message': 'start'
    }
    add_instance_actions(data)

    ret_c = _copy_vol_and_xml(host_id_s, host_ip_s, host_id_d, host_ip_d,
                              ins_uuid_s, ins_data_s['name'])
    if not ret_c:
        message = 'copy vol and xml from %s to %s error' % (host_ip_s,
                                                            host_ip_d)
        logging.error(message)
        _change_migrate_host(host_id_s, host_name_s, ins_id_s)
        update_instance_actions(
            request_id, InstaceActions.INSTANCE_COLD_MIGRATE_VOL_XML_COPY,
            ActionStatus.FAILD, message)
        ins_m_s.InstanceMigrateService().change_migrate_status(
            migrate_tab_id, MigrateStatus.FAILED)
        return
    else:
        message = 'copy vol and xml from %s to %s successful' % (host_ip_s,
                                                                 host_ip_d)
        update_instance_actions(
            request_id, InstaceActions.INSTANCE_COLD_MIGRATE_VOL_XML_COPY,
            ActionStatus.SUCCSESS, message)

    # todo:先判断该VM是否存在
    # 查看拷贝后目的端主机文件md5是否与源端一致
    data = {
        'action': InstaceActions.INSTANCE_COLD_MIGRATE_VOL_XML_MD5_D,
        'instance_uuid': ins_uuid_s,
        'request_id': request_id,
        'user_id': user_id,
        'message': 'start'
    }
    add_instance_actions(data)

    ret_v_m, ins_vol_md5_d = ansible_migrate_md5_get(host_ip_d,
                                                     ins_vol_md5_cmd)
    if not ret_v_m:
        message = 'get migrate destination %s vol md5 error' % host_ip_d
        logging.error(message)
        _change_migrate_host(host_id_s, host_name_s, ins_id_s)
        update_instance_actions(
            request_id, InstaceActions.INSTANCE_COLD_MIGRATE_VOL_XML_MD5_D,
            ActionStatus.FAILD, message)
        ins_m_s.InstanceMigrateService().change_migrate_status(
            migrate_tab_id, MigrateStatus.FAILED)
        return
    else:
        message = 'get migrate destination %s vol md5 successful' % host_ip_d
        update_instance_actions(
            request_id, InstaceActions.INSTANCE_COLD_MIGRATE_VOL_XML_MD5_D,
            ActionStatus.SUCCSESS, message)

    ret_x_m, ins_xml_md5_d = ansible_migrate_md5_get(host_ip_d,
                                                     ins_xml_md5_cmd)
    if not ret_x_m:
        message = 'get migrate destination %s xml md5 error' % host_ip_d
        logging.error(message)
        _change_migrate_host(host_id_s, host_name_s, ins_id_s)
        update_instance_actions(
            request_id, InstaceActions.INSTANCE_COLD_MIGRATE_VOL_XML_MD5_D,
            ActionStatus.FAILD, message)
        ins_m_s.InstanceMigrateService().change_migrate_status(
            migrate_tab_id, MigrateStatus.FAILED)
        return
    else:
        message = 'get migrate destination %s xml md5 successful' % host_ip_d
        update_instance_actions(
            request_id, InstaceActions.INSTANCE_COLD_MIGRATE_VOL_XML_MD5_D,
            ActionStatus.SUCCSESS, message)

    # 比对
    data = {
        'action': InstaceActions.INSTANCE_COLD_MIGRATE_MD5_MATCH,
        'instance_uuid': ins_uuid_s,
        'request_id': request_id,
        'user_id': user_id,
        'message': 'start'
    }
    add_instance_actions(data)

    if ins_vol_md5_d != ins_vol_md5_s or ins_xml_md5_d != ins_xml_md5_s:
        message = 'vol or xml md5 not match, vol_s:%s, vol_d:%s, xml_s:%s, xml_d:%s' % \
                  (ins_vol_md5_s, ins_vol_md5_d, ins_xml_md5_s, ins_xml_md5_d)
        logging.error(message)
        _change_migrate_host(host_id_s, host_name_s, ins_id_s)
        update_instance_actions(request_id,
                                InstaceActions.INSTANCE_COLD_MIGRATE_MD5_MATCH,
                                ActionStatus.FAILD, message)
        ins_m_s.InstanceMigrateService().change_migrate_status(
            migrate_tab_id, MigrateStatus.FAILED)
        return
    else:
        message = 'vol or xml md5 match successful, vol_s:%s, vol_d:%s, xml_s:%s, xml_d:%s' % \
                  (ins_vol_md5_s, ins_vol_md5_d, ins_xml_md5_s, ins_xml_md5_d)
        update_instance_actions(request_id,
                                InstaceActions.INSTANCE_COLD_MIGRATE_MD5_MATCH,
                                ActionStatus.SUCCSESS, message)

    # 在目标主机上定义迁移后虚拟机
    data = {
        'action': InstaceActions.INSTANCE_COLD_MIGRATE_DEFINE_D,
        'instance_uuid': ins_uuid_s,
        'request_id': request_id,
        'user_id': user_id,
        'message': 'start'
    }
    add_instance_actions(data)

    ret_status, ret_middle_status, ret_msg = __ansible_migrate_instance_define(
        host_ip_d, ins_name_s)
    if not ret_status:
        message = 'dest host %s define migrate instance error' % host_ip_d
        logging.error(message)
        _change_migrate_host(host_id_s, host_name_s, ins_id_s)
        update_instance_actions(request_id,
                                InstaceActions.INSTANCE_COLD_MIGRATE_DEFINE_D,
                                ActionStatus.FAILD, message)
        ins_m_s.InstanceMigrateService().change_migrate_status(
            migrate_tab_id, MigrateStatus.FAILED)
        return
    else:
        message = 'dest host %s define migrate instance successful' % host_ip_d
        update_instance_actions(request_id,
                                InstaceActions.INSTANCE_COLD_MIGRATE_DEFINE_D,
                                ActionStatus.SUCCSESS, message)

    # 这里之后的步骤都是在成功将VM迁移到目标host上后对源host的操作,所以接下来每一步如果出错都不将VM的host ip改回源host
    # 将虚拟机和存储池在源宿主机上undefined
    data = {
        'action': InstaceActions.INSTANCE_COLD_MIGRATE_UNDEFINE_S,
        'instance_uuid': ins_uuid_s,
        'request_id': request_id,
        'user_id': user_id,
        'message': 'start'
    }
    add_instance_actions(data)

    ret_u = libvirt_instance_undefined(host_ip_s, ins_data_s)
    if not ret_u:
        message = 'source host %s undefined instance error' % host_id_s
        logging.error(message)
        update_instance_actions(
            request_id, InstaceActions.INSTANCE_COLD_MIGRATE_UNDEFINE_S,
            ActionStatus.FAILD, message)
        ins_m_s.InstanceMigrateService().change_migrate_status(
            migrate_tab_id, MigrateStatus.FAILED)
        return
    else:
        message = 'source host %s undefined instance successful' % host_id_s
        update_instance_actions(
            request_id, InstaceActions.INSTANCE_COLD_MIGRATE_UNDEFINE_S,
            ActionStatus.SUCCSESS, message)

    # 修改原虚拟机存储目录名字
    data = {
        'action': InstaceActions.INSTANCE_COLD_MIGRATE_BACKUP_NAME,
        'instance_uuid': ins_uuid_s,
        'request_id': request_id,
        'user_id': user_id,
        'message': 'start'
    }
    add_instance_actions(data)

    ret_d = ansible_change_migrate_dir(host_ip_s, host_ip_d, ins_uuid_s)
    if not ret_d:
        message = 'source host %s backup dir after migrate error' % host_id_s
        logging.error(message)
        update_instance_actions(
            request_id, InstaceActions.INSTANCE_COLD_MIGRATE_BACKUP_NAME,
            ActionStatus.FAILD, message)
        ins_m_s.InstanceMigrateService().change_migrate_status(
            migrate_tab_id, MigrateStatus.FAILED)
        return
    else:
        message = 'source host %s backup dir after migrate successful' % host_id_s
        update_instance_actions(
            request_id, InstaceActions.INSTANCE_COLD_MIGRATE_BACKUP_NAME,
            ActionStatus.SUCCSESS, message)

    # 迁移后取消限速
    data = {
        'action': InstaceActions.INSTANCE_COLD_MIGRATE_CANCEL_SPEED,
        'instance_uuid': ins_uuid_s,
        'request_id': request_id,
        'user_id': user_id,
        'message': 'start'
    }
    add_instance_actions(data)

    ret_c = ansible_migrate_cancel_qos_speed(host_ip_s)
    if not ret_c:
        message = 'source host %s cancel set migrate speed error' % host_id_s
        logging.error(message)
        update_instance_actions(
            request_id, InstaceActions.INSTANCE_COLD_MIGRATE_CANCEL_SPEED,
            ActionStatus.FAILD, message)
        ins_m_s.InstanceMigrateService().change_migrate_status(
            migrate_tab_id, MigrateStatus.FAILED)
        return
    else:
        message = 'source host %s cancel set migrate speed successful' % host_id_s
        update_instance_actions(
            request_id, InstaceActions.INSTANCE_COLD_MIGRATE_CANCEL_SPEED,
            ActionStatus.SUCCSESS, message)

    ret_u = ins_m_s.InstanceMigrateService().change_migrate_status(
        migrate_tab_id, MigrateStatus.SUCCESS)
    if ret_u != 1:
        logging.error(
            'update instance migrate info error when after cold migrate instance'
        )

    # 迁移后修改虚拟机状态为关机中
    vm_cold_migrate_status = VMStatus.SHUTDOWN
    _update_instance_status(ins_uuid_s, vm_cold_migrate_status)
Ejemplo n.º 5
0
def update_instance_actions(uuid, request_id, user_id, action, status,
                            message):
    return ins_a_s.update_instance_actions(request_id, action, status, message)
Ejemplo n.º 6
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.º 7
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.º 8
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.º 9
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.º 10
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()