예제 #1
0
def del_vm(dest_host, vmname):
    command1 = 'virsh destroy ' + vmname
    command = 'virsh  undefine ' + vmname
    r_user = ANSIABLE_REMOTE_USER
    r_pass = decrypt(KVMHOST_LOGIN_PASS)
    b_user = OPENSTACK_DEV_USER
    b_pass = decrypt(KVMHOST_SU_PASS)
    res1 = ansible_run_shell(dest_host, command1)
    if res1['contacted'] == {}:
        msg = "连接目标kvmhost失败"
        return False, msg
    else:
        time.sleep(3)
        delvm_res = ansible_run_shell(dest_host, command)
        if 'contacted' not in delvm_res:
            msg = "连接目标kvmhost失败"
            return False, msg
        elif delvm_res['contacted'] == {}:
            msg = "连接目标kvmhost失败"
            return False, msg
        elif 'failed' in delvm_res['contacted'][dest_host]:
            msg = "删除目标vm失败"
            return False, msg
        else:
            msg = "目标VM已删除"
            return True, msg
예제 #2
0
def esx_del_tmp_folder(dest_host, vmware_vm):
    r_user = ANSIABLE_REMOTE_USER
    r_pass = decrypt(KVMHOST_LOGIN_PASS)
    b_user = OPENSTACK_DEV_USER
    b_pass = decrypt(KVMHOST_SU_PASS)
    dest_dir = '/app/tmp/' + vmware_vm
    comand_rm = 'cd ' + dest_dir + ';rm -f ' + vmware_vm + '*;rm -f *.xml'
    folder_rm = ansible_run_shell(dest_host, comand_rm)
    if 'contacted' not in folder_rm:
        msg = "连接目标kvmhost失败"
        return False, msg
    elif folder_rm['contacted'] == {}:
        msg = "连接目标kvmhost失败"
        return False, msg
    elif 'failed' in folder_rm['contacted'][dest_host]:
        msg = "删除vm文件失败"
        return False, msg
    else:
        command_rmdir = 'rmdir ' + dest_dir
        dir_rm = ansible_run_shell(dest_host, command_rmdir)
        if dir_rm['contacted'] == '{}':
            msg = "连接目标kvmhost失败"
            return False, msg
        else:
            msg = "目标路径已删除"
            return True, msg
예제 #3
0
def esx_retry_del_vm_folder(dest_host, dest_dir, vm_name, vmware_vm):
    if dest_dir == '/app/image/' or dest_dir == '/app/image':
        msg = "目标VM路径无效"
        return False, msg
    else:
        command_check = 'ls ' + dest_dir
        r_user = ANSIABLE_REMOTE_USER
        r_pass = decrypt(KVMHOST_LOGIN_PASS)
        b_user = OPENSTACK_DEV_USER
        b_pass = decrypt(KVMHOST_SU_PASS)
        folder_check = ansible_run_shell(dest_host, command_check)
        if 'contacted' not in folder_check:
            msg = "连接目标kvmhost失败"
            return False, msg
        elif folder_check['contacted'] == {}:
            msg = "连接目标kvmhost失败"
            return False, msg
        elif 'failed' in folder_check['contacted'][dest_host]:
            msg = "vm存储路径获取失败"
            return False, msg
        else:
            comand_rm = 'cd ' + dest_dir + ';rm -f ' + vm_name + '*;rm -f *.xml;rm -f ' + vmware_vm + '*'
            folder_rm = ansible_run_shell(dest_host, comand_rm)
            if folder_rm['contacted'] == {}:
                msg = "连接目标kvmhost失败"
                return False, msg
            elif 'failed' in folder_rm['contacted'][dest_host]:
                msg = "删除vm文件失败"
                return False, msg
            else:
                msg = "目标路径已删除"
                return True, msg
예제 #4
0
def check_vlan_connection(host_ip, test_vlan_info_list):
    test_vlan_info = '"' + str(test_vlan_info_list) + '"'
    command = '/usr/bin/python /root/check_vlan_connection.py %s' % test_vlan_info
    ret = ansibleCmdV2.ansible_run_shell(host_ip, command)
    if 'contacted' not in ret:
        message = '连接目标HOST %s 失败' % host_ip
        logging.error(message)
        return False, message
    elif ret['contacted'] == {}:
        message = '连接目标kvm host %s失败' % host_ip
        logging.error(message)
        return False, message
    elif 'failed' in ret['contacted'][host_ip]:
        message = '连接目标kvm host %s失败' % host_ip
        logging.error(message)
        return False, message
    elif ret['contacted'][host_ip]['stderr']:
        message = 'ansible执行命令失败,物理机ip:%s' % host_ip
        logging.error(message)
        return False, message
    # elif ret['contacted'][host_ip]['rc'] != 0:
    #     vlan_check_error = ret['contacted'][host_ip]['stdout'].encode('raw_unicode_escape').decode('utf8')
    #     message = '目标kvm host %s到以下vlan %s 不通,请检查' % (host_ip, vlan_check_error)
    #     logging.info(message)
    #     return False, message
    else:
        message = '目标KVM HOST %s vlan检查通过' % host_ip
        return True, message


# # 日志格式化
# def _init_log(service_name):
#     log_basic_path = os.path.dirname(os.path.abspath(__file__))[0:-14]
#     log_name = log_basic_path + 'log/' + str(service_name) + '.log'
#     add_timed_rotating_file_handler(log_name, logLevel='INFO')
예제 #5
0
def check_host_os_ver(host_ip):
    os_ver = HOST_OS_VER
    command1 = 'cat /etc/redhat-release |grep %s' % os_ver
    r_user = ANSIABLE_REMOTE_USER
    r_pass = decrypt(ANSIABLE_REMOTE_PWD)
    b_user = ANSIABLE_REMOTE_SU_USER
    b_pass = decrypt(ANSIABLE_REMOTE_SU_PWD)
    host = host_ip
    c1 = ansible_run_shell(host, command1)
    if 'contacted' not in c1:
        message = '连接目标HOST %s 失败' % host_ip
        logging.error(message)
        return False, message
    elif c1['contacted'] == {}:
        message = '连接目标kvm host失败'
        logging.error(message)
        return False, message
    elif 'failed' in c1['contacted'][host_ip]:
        message = '获取HOST %s OS版本失败' % host_ip
        logging.error(message)
        return False, message
    elif c1['contacted'][host_ip]['stderr']:
        message = '获取HOST %s OS版本失败' % host_ip
        logging.error(message)
        return False, message
    elif c1['contacted'][host_ip]['stdout'] == '':
        message = 'HOST %s OS版本异常,非指定的 %s 版本,请检查' % (host_ip, os_ver)
        logging.info(message)
        return False, message
    else:
        message = 'HOST %s OS版本检查通过' % host_ip
        logging.info(message)
        return True, message
예제 #6
0
def check_bond_connection(host_ip, vlan_id):
    command1 = '/usr/sbin/ip a|grep br_bond0.' + vlan_id
    r_user = ANSIABLE_REMOTE_USER
    r_pass = decrypt(ANSIABLE_REMOTE_PWD)
    b_user = ANSIABLE_REMOTE_SU_USER
    b_pass = decrypt(ANSIABLE_REMOTE_SU_PWD)
    host = host_ip
    c1 = ansible_run_shell(host, command1)
    if 'contacted' not in c1:
        message = '连接目标HOST %s 失败' % (host_ip)
        logging.error(message)
        return False, False, message
    elif c1['contacted'] == {}:
        message = '连接目标kvm host失败'
        logging.error(message)
        return False, False, message
    elif 'failed' in c1['contacted'][host_ip]:
        message = '获取HOST %s 网桥信息出错' % (host_ip)
        logging.error(message)
        return False, False, message
    elif c1['contacted'][host_ip]['stderr']:
        message = '获取HOST %s 网桥信息出错' % (host_ip)
        logging.error(message)
        return False, False, message
    elif c1['contacted'][host_ip]['stdout'] == '':
        message = 'HOST %s 上主网所在网桥未创建' % (host_ip)
        logging.info(message)
        return False, True, message
    else:
        message = '获取HOST上主网网桥成功'
        logging.info(message)
        return True, True, message
예제 #7
0
def ansible_migrate_qos_speed(host_ip_s, host_ip_d, migrate_speed):
    '''
        设置目标主机的迁移速度
    :param host_ip_s:
    :param host_ip_d:
    :param migrate_speed:
    :return:
    '''
    try:
        speed_cmd = 'cd /root;/bin/bash migratespeedQos ' + host_ip_d + ' ' + str(
            migrate_speed)
        hostlist = [host_ip_s]
        ansible_code, ansible_msg = ansible_run_shell(host_ip_s, speed_cmd)
        logging.info(ansible_msg)

        if ansible_code == 3:
            message = '连接目标KVM HOST %s 失败' % (host_ip_s)
            logging.error(message)
            return False, message

        elif ansible_code != 0:
            logging.info("set host migrate speed return: %s" %
                         ansible_msg['np_fact_cache'][host_ip_s])
            logging.info('exec %s   failed ' % speed_cmd)
            return False
        else:
            logging.info('exec %s   success ' % speed_cmd)
            return True
    except:
        logging.error(traceback.format_exc())
        return False
예제 #8
0
def ansible_migrate_md5_get(host_ip, cmd):
    '''
        获取迁移文件MD5
    :param host_ip:
    :param cmd:
    :return:
    '''
    try:
        hostlist = [host_ip]
        ansible_code, ansible_msg = ansible_run_shell(host_ip, cmd)
        logging.info(ansible_msg)

        if ansible_code == 3:
            message = '连接目标KVM HOST %s 失败' % (host_ip)
            logging.error(message)
            return False, message

        elif ansible_code != 0:
            logging.info("get migrate md5 return: %s" %
                         ansible_msg['np_fact_cache'][host_ip])
            logging.info('exec %s   failed ' % cmd)
            return False, None
        else:
            logging.info('exec %s   success ' % cmd)
            return True, ansible_msg['np_fact_cache'][host_ip]['shell_out'][
                'stdout'].split('\n')
    except:
        logging.error(traceback.format_exc())
        return False, None
예제 #9
0
def del_vm_folder_file(dest_dir, kvmhost, vmware_vm):
    remote_user = ANSIABLE_REMOTE_USER
    remote_pass = decrypt(KVMHOST_LOGIN_PASS)
    become_user = OPENSTACK_DEV_USER
    become_pass = decrypt(KVMHOST_SU_PASS)
    del_vm_com = 'cd ' + dest_dir + ';rm -f ' + vmware_vm + '-sd*;rm -f ' + vmware_vm + '.xml'
    delvmfolderfile = ansible_run_shell(kvmhost, del_vm_com)
    if 'contacted' not in delvmfolderfile:
        message = '连接目标host失败'
        logging.info(message)
        return False, message
    elif delvmfolderfile['contacted'] == {}:
        message = '连接目标host失败'
        logging.info(message)
        return False, message
    elif 'failed' in delvmfolderfile['contacted'][kvmhost]:
        message = '删除'
        logging.info(message)
        return False, message
    elif delvmfolderfile['contacted'][kvmhost]['stderr']:
        message = '重命名vm数据盘失败'
        logging.info(message)
        return False, message
    else:
        message = '重命名vm数据盘成功'
        logging.info(message)
        return True, message
예제 #10
0
def ansible_change_migrate_dir(host_ip_s, host_ip_d, ins_uuid):
    '''
        迁移后修改目录名称
    :param host_ip_s:
    :param host_ip_d:
    :param ins_uuid:
    :return:
    '''
    try:
        bak_name = ins_uuid + '-migrate-to-' + host_ip_d + '-' + str(
            get_timestamp())
        dir_cmd = 'cd /app/image;/bin/mv ' + ins_uuid + ' ' + bak_name
        hostlist = [host_ip_s]
        ansible_code, ansible_msg = ansible_run_shell(host_ip_s, dir_cmd)
        logging.info(ansible_msg)

        if ansible_code == 3:
            message = '连接目标KVM HOST %s 失败' % (host_ip_s)
            logging.error(message)
            return False, message

        elif ansible_code != 0:
            logging.info("change migrate dir return: %s" %
                         ansible_msg['np_fact_cache'][host_ip_s])
            logging.info('exec %s   failed ' % dir_cmd)
            return False
        else:
            logging.info('exec %s   success ' % dir_cmd)
            return True
    except:
        logging.error(traceback.format_exc())
        return False
예제 #11
0
def get_vm_data_disk(dest_dir, kvmhost):
    remote_user = ANSIABLE_REMOTE_USER
    remote_pass = decrypt(KVMHOST_LOGIN_PASS)
    become_user = OPENSTACK_DEV_USER
    become_pass = decrypt(KVMHOST_SU_PASS)
    get_vm_data_list = 'cd ' + dest_dir + ";ls |grep -e '-sd'|grep -v .img|grep -v .xml|wc -l"
    getvmdatadisk = ansible_run_shell(kvmhost, get_vm_data_list)
    if 'contacted' not in getvmdatadisk:
        message = '连接目标host失败'
        logging.info(message)
        return False, message
    elif getvmdatadisk['contacted'] == {}:
        message = '连接目标host失败'
        logging.info(message)
        return False, message
    elif 'failed' in getvmdatadisk['contacted'][kvmhost]:
        message = '获取vm数据盘清单失败'
        logging.info(message)
        return False, message
    elif getvmdatadisk['contacted'][kvmhost]['stderr']:
        message = '获取vm数据盘清单失败'
        logging.info(message)
        return False, message
    else:
        message = "获取vm磁盘文件清单成功"
        logging.info(message)
        return True, getvmdatadisk['contacted'][kvmhost]['stdout']
예제 #12
0
def vm_data_disk_rename(datadisk, dest_dir, tag, vm_name, kvmhost):
    remote_user = ANSIABLE_REMOTE_USER
    remote_pass = decrypt(KVMHOST_LOGIN_PASS)
    become_user = OPENSTACK_DEV_USER
    become_pass = decrypt(KVMHOST_SU_PASS)
    mv_command = 'cd ' + dest_dir + ';cp -f ' + datadisk + ' ' + vm_name + '_disk' + str(
        tag)
    mvcommand = ansible_run_shell(kvmhost, mv_command)
    if 'contacted' not in mvcommand:
        message = '连接目标host失败'
        logging.info(message)
        return 1, message
    elif mvcommand['contacted'] == {}:
        message = '连接目标host失败'
        logging.info(message)
        return 1, message
    elif 'failed' in mvcommand['contacted'][kvmhost]:
        message = '重命名vm数据盘失败'
        logging.info(message)
        return 1, message
    elif mvcommand['contacted'][kvmhost]['stderr']:
        message = '重命名vm数据盘失败'
        logging.info(message)
        return 1, message
    else:
        message = '重命名vm数据盘成功'
        logging.info(message)
        datadisk = vm_name + '_disk' + str(tag)
        return 0, datadisk
예제 #13
0
def ansible_migrate_cancel_qos_speed(host_ip):
    '''
        取消host迁移速度限制
    :param host_ip:
    :param migrate_speed:
    :return:
    '''
    try:
        cancel_cmd = 'cd /root;/bin/bash deletemigratespeedQos'
        hostlist = [host_ip]
        ansible_code, ansible_msg = ansible_run_shell(host_ip, cancel_cmd)
        logging.info(ansible_msg)

        if ansible_code == 3:
            message = '连接目标KVM HOST %s 失败' % (host_ip)
            logging.error(message)
            return False, message

        elif ansible_code != 0:
            logging.info("set host migrate speed return: %s" %
                         ansible_msg['np_fact_cache'][host_ip])
            return False
        else:
            logging.info('exec %s   success ' % cancel_cmd)
            return True
    except:
        logging.error(traceback.format_exc())
        return False
예제 #14
0
def ansible_migrate_vol_get(host_ip, ins_uuid):
    '''
        获取迁移目标主机卷名称
    :param host_ip:
    :param ins_uuid:
    :return:
    '''
    try:
        vol_cmd = 'cd /app/image/' + ins_uuid + ';ls'
        hostlist = [host_ip]
        ansible_code, ansible_msg = ansible_run_shell(host_ip, vol_cmd)
        logging.info(ansible_msg)

        if ansible_code == 3:
            message = '连接目标KVM HOST %s 失败' % (host_ip)
            logging.error(message)
            return False, message

        elif ansible_code != 0:
            logging.info("get migrate host vol return: %s" %
                         ansible_msg['np_fact_cache'][host_ip])
            logging.info('exec %s   failed ' % vol_cmd)
            return False, None
        else:
            logging.info('exec %s   success ' % vol_cmd)
            return True, ansible_msg['np_fact_cache'][host_ip]['shell_out'][
                'stdout'].split('\n')
    except:
        logging.error(traceback.format_exc())
        return False, None
예제 #15
0
def _pool_delete(remote_ip):
    command1 = 'virsh pool-destroy image'
    command2 = 'virsh pool-undefine image'
    r_user = ANSIABLE_REMOTE_USER
    r_pass = decrypt(ANSIABLE_REMOTE_PWD)
    b_user = ANSIABLE_REMOTE_SU_USER
    b_pass = decrypt(ANSIABLE_REMOTE_SU_PWD)
    host = remote_ip
    c1 = ansible_run_shell(host, command1)
    if c1['contacted'] == {}:
        return False
    else:
        c2 = ansible_run_shell(host, command2)
        if c2['contacted'] == {}:
            return False
        else:
            return True
예제 #16
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()
예제 #17
0
def esx_del_vm_folder(dest_host, dest_dir, vm_ip, vmuuid, vmware_vm):
    if dest_dir == '/app/image/' or dest_dir == '/app/image':
        msg = "目标VM路径无效"
        return False, msg
    else:
        command_check = 'ls ' + dest_dir
        r_user = ANSIABLE_REMOTE_USER
        r_pass = decrypt(KVMHOST_LOGIN_PASS)
        b_user = OPENSTACK_DEV_USER
        b_pass = decrypt(KVMHOST_SU_PASS)
        folder_check = ansible_run_shell(dest_host, command_check)
        if 'contacted' not in folder_check:
            msg = "连接目标kvmhost失败"
            return False, msg
        elif folder_check['contacted'] == {}:
            msg = "连接目标kvmhost失败"
            return False, msg
        elif 'failed' in folder_check['contacted'][dest_host]:
            msg = "vm存储路径获取失败"
            return False, msg
        else:
            comand_rm = 'cd ' + dest_dir + ';rm -f ' + vm_ip + '*;rm -f *.xml;rm -f ' + vmware_vm + '*'
            folder_rm = ansible_run_shell(dest_host, comand_rm)
            if folder_rm['contacted'] == {}:
                msg = "连接目标kvmhost失败"
                return False, msg
            elif 'failed' in folder_rm['contacted'][dest_host]:
                msg = "删除vm文件失败"
                return False, msg
            else:
                command_rmdir = 'rmdir ' + dest_dir
                dir_rm = ansible_run_shell(dest_host, command_rmdir)
                if dir_rm['contacted'] == {}:
                    msg = "连接目标kvmhost失败"
                    return False, msg
                elif 'failed' in dir_rm['contacted'][dest_host]:
                    msg = "删除vm路径失败"
                    return False, msg
                else:
                    rm_pool = 'virsh pool-destroy ' + vmuuid + ";virsh pool-undefine " + vmuuid
                    ansible_run_shell(dest_host, rm_pool)
                    msg = "目标路径已删除"
                    return True, msg
예제 #18
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()
예제 #19
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()
예제 #20
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()
예제 #21
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()
예제 #22
0
def get_vm_version(ctrhost, ctrpass, vmostype, vmip):
    command = "vmop " + vmip + "|grep image|cut -d '|' -f 3|cut -d ' ' -f 2"
    remote_user = OPENSTACK_DEV_USER
    become_user = OPENSTACK_DEV_USER
    remote_pass = ctrpass
    become_pass = ctrpass
    host = ctrhost
    osversion = ansible_run_shell(host, command)
    print osversion['contacted']
    if 'contacted' not in osversion:
        msg = '获取vmOS版本失败'
        return False, msg
    elif osversion['contacted'] == {}:
        msg = '获取vmOS版本失败'
        return False, msg
    elif 'failed' in osversion['contacted'][host]:
        msg = '获取vmOS版本失败'
        return False, msg
    else:
        osversion_s = osversion['contacted'][host]['stdout']
        if vmostype == "Windows":
            if '2008' in osversion_s:
                version_res = '2008'
            elif '2012' in osversion_s:
                version_res = '2012'
            elif 'win7' in osversion_s:
                version_res = 'win7'
            else:
                version_res = 'windows'
            return True, version_res
        elif vmostype == "Linux":
            if ('centos' or 'RHEL') in osversion_s:
                if '6.6' in osversion_s:
                    version_res = '6.6'
                elif '7.2' in osversion_s:
                    version_res = '7.2'
                else:
                    version_res = 'centos'

            elif 'ubuntu' in osversion_s:
                version_res = 'ubuntu'
            elif 'debian' in osversion_s:
                version_res = 'debian'
            else:
                version_res = 'unknown'
            return True, version_res
예제 #23
0
def ansible_clone_file_get(host, command):
    hostlist = []
    hostlist.append(host)
    ansible_code, ansible_msg = ansibleCmdV2.ansible_run_shell(host, command)
    if ansible_code == 3:
        message = '连接目标KVM HOST %s 失败' % (host)
        logging.error(message)
        return False, message
    elif ansible_code != 0:
        logging.info('在host %s 上下发NC传输失败' % (host))
        return False
    elif 'failed' in ansible_msg['np_fact_cache'][host]:
        logging.info('在host %s 上下发NC传输失败' % (host))
        return False
    else:
        logging.info('host %s 上下发NC传输成功' % (host))
        return True
예제 #24
0
def vm_stat(vmip, host, host_pass):
    command = "/usr/bin/vmop " + vmip + " |grep 'vm_state'|grep 'stopped'"
    remote_user = OPENSTACK_DEV_USER
    become_user = OPENSTACK_DEV_USER
    remote_pass = host_pass
    become_pass = host_pass
    ctrhost = host
    vmstat = ansible_run_shell(ctrhost, command)
    if 'contacted' not in vmstat:
        return False
    elif vmstat['contacted'] == {}:
        return False
        #return json_helper.format_api_resp(code=ErrorCode.SYS_ERR, msg='无法连接目标vm所在控制节点,无法完成v2v操作')
    elif 'failed' in vmstat['contacted'][ctrhost]:
        return False
        #return json_helper.format_api_resp(code=ErrorCode.SYS_ERR, msg='获取待转化vm当前状态失败,无法完成v2v操作')
    elif vmstat['contacted'][ctrhost]['stdout'] == '':
        return False
        #return json_helper.format_api_resp(code=ErrorCode.SYS_ERR, msg='待转化vm未关机,无法完成v2v操作')
    else:
        return True
예제 #25
0
def vm_disk_size(vmip, cloudarea, host, host_pass):
    # if cloudarea == "SIT":
    #     command = "/usr/bin/vmop " + vmip + " data_volume"
    # else:
    command = "/usr/bin/vmop " + vmip + " data_volume"
    remote_user = OPENSTACK_DEV_USER
    become_user = OPENSTACK_DEV_USER
    remote_pass = host_pass
    become_pass = host_pass
    ctrhost = host
    vmdisksize = ansible_run_shell(ctrhost, command)
    if 'contacted' not in vmdisksize:
        return False
    elif vmdisksize['contacted'] == {}:
        return False
        #return json_helper.format_api_resp(code=ErrorCode.SYS_ERR, msg='无法连接目标vm所在控制节点,无法完成v2v操作')
    elif 'failed' in vmdisksize['contacted'][ctrhost]:
        return False
        #return json_helper.format_api_resp(code=ErrorCode.SYS_ERR, msg='获取待转化vm总磁盘大小失败,无法完成v2v操作')
    else:
        disk_size = int(vmdisksize['contacted'][ctrhost]['stdout']) + 80
        return disk_size
예제 #26
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()
예제 #27
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()
예제 #28
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()