def v2v_openstack_cancel(): #获取入参信息 cancel = request.values.get('cancel') request_his = request.values.get('request_id') source = v2v_op.v2vTaskService().get_v2v_task_by_requestid( request_his)['source'] if cancel != '1': return json_helper.format_api_resp(code=ErrorCode.PARAM_ERR, msg='参数错误') #判断当前任务是否完成 res_task = v2v_op.get_v2v_running_by_reqid(request_his) if res_task == False: return json_helper.format_api_resp(code=ErrorCode.SYS_ERR, msg='任务状态无法操作') else: up_cal = v2v_op.updata_v2v_cancel(request_his, 1, 3) if up_cal == True: return json_helper.format_api_resp(code=ErrorCode.SUCCESS, msg='取消下发成功') else: return json_helper.format_api_resp(code=ErrorCode.SUCCESS, msg='取消下发失败')
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()
def esx_retry_action(request_id): v2v_task = v2v_op.v2vTaskService().get_v2v_task_by_requestid(request_id) step_done = v2v_task['step_done'] dest_host = v2v_task['dest_host'] dest_dir = v2v_task['dest_dir'] vm_name = v2v_task['vm_name'] vmware_vm = v2v_task['vmware_vm'] if int(step_done) >= 3 and int(step_done) < 5: tag, message = v2v_op.esx_retry_del_vm_folder(dest_host, dest_dir, vm_name, vmware_vm) if not tag: return False, message return True, message else: message = 'done' return True, message
def openstack_retry_action(request_id): v2v_task = v2v_op.v2vTaskService().get_v2v_task_by_requestid(request_id) step_done = v2v_task['step_done'] dest_host = v2v_task['dest_host'] dest_dir = v2v_task['dest_dir'] vm_name = v2v_task['vm_name'] vm_uuid = v2v_task['vm_uuid'] if step_done == v2vActions.GET_VM_FILE: res, res_msg = v2v_op.op_retry_del_vm_folder(dest_host, dest_dir, vm_name, vm_uuid) if res == False: msg = "删除下载文件失败" return False, msg else: msg = "删除下载文件成功" return True, msg else: msg = "done" return True, msg
def v2v_openstack_retry(): #获取入参信息 retry = request.values.get('retry') request_his = request.values.get('request_id') source = v2v_op.v2vTaskService().get_v2v_task_by_requestid( request_his)['source'] if not source: return json_helper.format_api_resp(code=ErrorCode.PARAM_ERR, msg='v2v来源缺失') if retry != '1': return json_helper.format_api_resp(code=ErrorCode.PARAM_ERR, msg='参数错误') #判断当前任务是否可重试 res_task = v2v_op.get_v2v_retryable(request_his) if not res_task: return json_helper.format_api_resp(code=ErrorCode.SYS_ERR, msg='任务状态无法重试') else: if source == VMCreateSource.OPENSTACK: res_t, res_msg = openstack_retry_action(request_his) if not res_t: return json_helper.format_api_resp(code=ErrorCode.SYS_ERR, msg=res_msg) else: res_e, res_emsg = esx_retry_action(request_his) if not res_e: return json_helper.format_api_resp(code=ErrorCode.SYS_ERR, msg=res_emsg) up_sta = v2v_op.updata_v2v_retry(request_his, 0) if up_sta == True: return json_helper.format_api_resp(code=ErrorCode.SUCCESS, msg='重试下发成功') else: return json_helper.format_api_resp(code=ErrorCode.SYS_ERR, msg='重试下发失败')
def esx_del_action(request_id): step_done = v2v_op.v2vTaskService().get_v2v_task_by_requestid( request_id)["step_done"] dest_host = v2v_op.v2vTaskService().get_v2v_task_by_requestid( request_id)["dest_host"] dest_dir = v2v_op.v2vTaskService().get_v2v_task_by_requestid( request_id)["dest_dir"] vm_name = v2v_op.v2vTaskService().get_v2v_task_by_requestid( request_id)["vm_name"] vmuuid = v2v_op.v2vTaskService().get_v2v_task_by_requestid( request_id)["vm_uuid"] vmware_vm = v2v_op.v2vTaskService().get_v2v_task_by_requestid( request_id)["vmware_vm"] step_done_int = int(step_done) if step_done_int == 0: msg = "已完成" return True, msg elif step_done_int == 1: tag, msg = v2v_op.del_vm_folder(dest_host, dest_dir, vmware_vm, vmuuid) if tag: return True, msg else: return False, msg elif step_done_int >= 2 and step_done_int <= 7: tag, msg = v2v_op.esx_del_vm_folder(dest_host, dest_dir, vm_name, vmuuid, vmware_vm) if not tag: return False, msg else: tag, msg = v2v_op.esx_del_tmp_folder(dest_host, vmware_vm) if tag: return True, msg else: return False, msg elif step_done_int >= 8: tag, msg = v2v_op.del_vm(dest_host, vm_name) if not tag: return False, msg else: tag, msg = v2v_op.esx_del_vm_folder(dest_host, dest_dir, vm_name, vmuuid, vmware_vm) if tag: return True, msg else: return False, msg
def del_action(request_id): request_his = request_id v2v_task = v2v_op.v2vTaskService().get_v2v_task_by_requestid(request_his) dest_host = v2v_task['dest_host'] dest_dir = v2v_task['dest_dir'] vm_name = v2v_task['vm_name'] vm_ip = v2v_task['vm_ip'] vmuuid = v2v_task['vm_uuid'] step_done = v2v_op.get_v2v_step(request_his) if step_done == v2vActions.COPY_VM_DISK or step_done == v2vActions.CREATE_DEST_DIR or step_done == v2vActions.CREATE_STOR_POOL \ or step_done == v2vActions.GET_VM_FILE or step_done == v2vActions.COPY_VM_XML or step_done == v2vActions.VM_STANDARDLIZE: res, res_data = v2v_op.del_vm_folder(dest_host, dest_dir, vm_name, vmuuid) if res == False: msg = '删除vm路径失败' return False, msg else: msg = "删除vm路径成功" return True, msg elif step_done == v2vActions.VM_DEFINE or step_done == v2vActions.VM_START: res1, res1_data = v2v_op.del_vm(dest_host, vm_name) if res1 == False: msg = '删除vm失败' return False, msg else: res2, res2_data = v2v_op.del_vm_folder(dest_host, dest_dir, vm_name, vmuuid) if res2 == False: msg = '删除vm路径失败' return False, msg else: msg = "删除vm路径成功" return True, msg else: msg = '删除操作完成' return True, msg
def _task_esx_intodb(task): # 获取vm入参 vmname = task['vm_name'] vmip = task['vm_ip'] flavor_id = task['flavor_id'] vm_ostype = task['vm_ostype'] vm_app_info = task['vm_app_info'] vm_owner = task['vm_owner'] vm_segment = task['vm_segment'] vm_group_id = task['group_id'] dest_host = task['dest_host'] vmhost = ho_s.HostService().get_host_info_by_hostip(dest_host) ret_4 = ho_s.pre_allocate_host_resource(vmhost['id'], task['vm_cpu'], task['vm_mem_mb'], 50) if ret_4 != 1: logging.error('资源预分配失败') message = '资源预分配失败' return False, message # 获取并录入IP信息 vm_segment = seg_s.SegmentService().get_segment_info_bysegment(vm_segment) if vm_segment is None: message = '网段信息有误,无法进行v2v操作' return False, message else: segment_id = vm_segment['id'] vm_netmask = vm_segment['netmask'] vm_gateway = vm_segment['gateway_ip'] vm_dns1 = vm_segment['dns1'] vm_dns2 = vm_segment['dns2'] vmvlan = vm_segment['vlan'] ip_data = ip_s.IPService().get_ip_info_by_ipaddress(vmip) if ip_data is None: ip_data = { 'ip_address': vmip, 'segment_id': segment_id, 'netmask': vm_netmask, 'vlan': vmvlan, 'gateway_ip': vm_gateway, 'dns1': vm_dns1, 'dns2': vm_dns2, 'created_at': get_datetime_str(), 'status': '1' } ret = ip_s.IPService().add_ip_info(ip_data) if ret.get('row_num') <= 0: logging.info( 'add ip info error when create v2v task, insert_data: %s', ip_data) message = "录入IP信息失败" return False, message else: ip_id = ret.get('last_id') else: ip_data_status = ip_data['status'] vmvlan = ip_data['vlan'] if ip_data_status != '0': message = "IP与现有环境冲突,无法进行v2v操作" return False, message else: ip_id = ip_data['id'] where_data = {'id': ip_id} updata_data = {'status': '1', 'updated_at': get_datetime_str()} ret1 = ip_s.IPService().update_ip_info(updata_data, where_data) if not ret1: logging.info( 'update ip info error when create v2v task, update_data: %s', updata_data) message = "更新IP状态失败" return False, message # 生成request_id request_id = v2v_op.generate_req_id() # 生成vm的uuid和mac vmuuid = randomUUID() vmmac = randomMAC() # 信息入instance相关库表 instance_tag, instance_info = _instance_db_info( vmuuid, vmname, vm_app_info, vm_owner, flavor_id, vm_group_id, dest_host, vmmac, task['vm_disk'], ip_id, vm_ostype, request_id, task['vm_os_version']) if not instance_tag: message = instance_info return False, message # 将步骤信息存入instance_action表 v2v_cd_d1 = { 'action': esx_v2vActions.CREATE_DEST_DIR, 'request_id': request_id, 'message': 'start', 'start_time': get_datetime_str() } in_a_s.InstanceActionsServices().add_instance_action_info(v2v_cd_d1) v2v_cr_pl = { 'action': esx_v2vActions.CREATE_STOR_POOL, 'request_id': request_id, 'message': 'other', 'start_time': get_datetime_str() } in_a_s.InstanceActionsServices().add_instance_action_info(v2v_cr_pl) v2v_cp_fl = { 'action': esx_v2vActions.COPY_FILE_TO_LOCAL, 'request_id': request_id, 'message': 'other', 'start_time': get_datetime_str() } in_a_s.InstanceActionsServices().add_instance_action_info(v2v_cp_fl) v2v_file = { 'action': esx_v2vActions.VIRT_V2V_FILES, 'request_id': request_id, 'message': 'other', 'start_time': get_datetime_str() } in_a_s.InstanceActionsServices().add_instance_action_info(v2v_file) v2v_del_tmp = { 'action': esx_v2vActions.DELETE_TMP_FILE, 'request_id': request_id, 'message': 'other', 'start_time': get_datetime_str() } in_a_s.InstanceActionsServices().add_instance_action_info(v2v_del_tmp) v2v_sysdisk_std = { 'action': esx_v2vActions.VM_SYS_DISK_STD, 'request_id': request_id, 'message': 'other', 'start_time': get_datetime_str() } in_a_s.InstanceActionsServices().add_instance_action_info( v2v_sysdisk_std) v2v_datadisk_std = { 'action': esx_v2vActions.VM_DATA_DISK_STD, 'request_id': request_id, 'message': 'other', 'start_time': get_datetime_str() } in_a_s.InstanceActionsServices().add_instance_action_info( v2v_datadisk_std) v2v_def_1 = { 'action': esx_v2vActions.VM_DEFINE1, 'request_id': request_id, 'message': 'other', 'start_time': get_datetime_str() } in_a_s.InstanceActionsServices().add_instance_action_info(v2v_def_1) v2v_star_1 = { 'action': esx_v2vActions.VM_START1, 'request_id': request_id, 'message': 'other', 'start_time': get_datetime_str() } in_a_s.InstanceActionsServices().add_instance_action_info(v2v_star_1) if vm_ostype == "Windows": v2v_att_disk = { 'action': esx_v2vActions.ATTACH_DISK, 'request_id': request_id, 'message': 'other', 'start_time': get_datetime_str() } in_a_s.InstanceActionsServices().add_instance_action_info( v2v_att_disk) v2v_win_std = { 'action': esx_v2vActions.WINDOWS_STD, 'request_id': request_id, 'message': 'other', 'start_time': get_datetime_str() } in_a_s.InstanceActionsServices().add_instance_action_info( v2v_win_std) v2v_vm_def2 = { 'action': esx_v2vActions.WINDOWS_DISK_CH, 'request_id': request_id, 'message': 'other', 'start_time': get_datetime_str() } in_a_s.InstanceActionsServices().add_instance_action_info( v2v_vm_def2) v2v_vm_star2 = { 'action': esx_v2vActions.VM_START2, 'request_id': request_id, 'message': 'other', 'start_time': get_datetime_str() } in_a_s.InstanceActionsServices().add_instance_action_info( v2v_vm_star2) # 将v2v信息存入v2v_task表 v2v_data = { 'source': VMCreateSource.ESX, 'request_id': request_id, 'destory': '0', 'start_time': get_datetime_str(), 'status': 0, 'vm_ip': vmip, 'vm_name': vmname, 'vmvlan': vmvlan, 'flavor_id': flavor_id, 'cloud_area': task['esx_env'], 'vm_ostype': vm_ostype, 'vm_app_info': vm_app_info, 'vm_owner': vm_owner, 'vm_group_id': vm_group_id, 'user_id': task['user_id'], 'vm_mac': vmmac, 'vm_uuid': vmuuid, 'cancel': '0', 'dest_dir': '/app/image/' + vmuuid, 'on_task': '0', 'dest_host': dest_host, 'esx_ip': task['esx_ip'], 'esx_passwd': task['esx_passwd'], 'vmware_vm': task['vmware_vm'], 'step_done': esx_v2vActions.BEGIN } v2v_insert = v2v_op.v2vTaskService().add_v2v_task_info(v2v_data) if v2v_insert.get('row_num') <= 0: logging.info('insert info to v2v_task failed! %s', v2v_data) message = '信息入库失败' return False, message message = '信息已添加至任务队列' return True, message
def v2v_esx_intodb(hostpool_id): ''' v2v_esx :param hostpool_id: :return: ''' #获取vm入库信息 vmname = request.values.get('vm_name') vmip = request.values.get('vm_ip') flavor_id = request.values.get('flavor_id') vm_ostype = request.values.get('vm_ostype') vm_app_info = request.values.get('vm_app_info') vm_owner = request.values.get('vm_owner') vm_group_id = request.values.get('vm_group_id') user_id = request.values.get('user_id') vm_segment = request.values.get('segment') vm_osver = request.values.get('vm_osver') vm_disk = request.values.get('vm_disk') esx_env = request.values.get('esx_env') esx_ip = request.values.get('esx_ip') esx_passwd1 = request.values.get('esx_passwd') vmware_vm = request.values.get('vmware_vm') # 入参完全性判断 if not vmname or not vmip or not flavor_id or not vm_ostype or not vm_app_info \ or not vm_osver or not vm_disk or not vm_owner or not vm_group_id or not user_id or not vm_segment or not esx_passwd1: logging.info('params are invalid or missing') return json_helper.format_api_resp(code=ErrorCode.PARAM_ERR, msg='入参缺失') else: esx_passwd = base64.b64decode(esx_passwd1) powertag, msg_power = vm_powerState(esx_ip, esx_passwd, vmware_vm) if not powertag: return json_helper.format_api_resp(code=ErrorCode.SYS_ERR, msg=msg_power) # 获取flavor信息 flavor_info = flavor_service.FlavorService().get_flavor_info(flavor_id) if not flavor_info: logging.info('id: %s flavor info not in db when create instance', flavor_id) return json_helper.format_api_resp(code=ErrorCode.SYS_ERR, msg='实例规格数据有误,无法进行v2v') vmcpu = flavor_info['vcpu'] vmmem = flavor_info['memory_mb'] data_disk = int(vm_disk) # 获取可用目标host host_code, host_data, host_msg = cal_host(hostpool_id, vmcpu, vmmem, data_disk, vm_group_id) if host_code < 0: return json_helper.format_api_resp(code=host_code, msg=host_msg) else: host = host_data vmhost = ho_s.HostService().get_host_info_by_hostip(host) ret_4 = ho_s.pre_allocate_host_resource(vmhost['id'], vmcpu, vmmem, 50) if ret_4 != 1: logging.error('资源预分配失败') message = '资源预分频失败' return json_helper.format_api_resp(code=ErrorCode.SYS_ERR, msg=message) # 获取并录入IP信息 vm_segment = seg_s.SegmentService().get_segment_info_bysegment( vm_segment) if vm_segment == None: return json_helper.format_api_resp(code=ErrorCode.SYS_ERR, msg='网段信息有误,无法进行v2v') else: segment_id = vm_segment['id'] vm_netmask = vm_segment['netmask'] vm_gateway = vm_segment['gateway_ip'] vm_dns1 = vm_segment['dns1'] vm_dns2 = vm_segment['dns2'] vmvlan = vm_segment['vlan'] ip_data = ip_s.IPService().get_ip_info_by_ipaddress(vmip) if ip_data == None: ip_data = { 'ip_address': vmip, 'segment_id': segment_id, 'netmask': vm_netmask, 'vlan': vmvlan, 'gateway_ip': vm_gateway, 'dns1': vm_dns1, 'dns2': vm_dns2, 'created_at': get_datetime_str(), 'status': '1' } ret = ip_s.IPService().add_ip_info(ip_data) if ret.get('row_num') <= 0: logging.info( 'add ip info error when create v2v task, insert_data: %s', ip_data) return json_helper.format_api_resp(code=ErrorCode.SYS_ERR, msg="录入IP信息失败") else: ip_id = ret.get('last_id') else: ip_data_status = ip_data['status'] vmvlan = ip_data['vlan'] if ip_data_status != '0': return json_helper.format_api_resp(code=ErrorCode.SYS_ERR, msg="IP与现有环境冲突,无法进行v2v") else: ip_id = ip_data['id'] where_data = {'id': ip_id} updata_data = { 'status': '1', 'updated_at': get_datetime_str() } ret1 = ip_s.IPService().update_ip_info( updata_data, where_data) if not ret1: logging.info( 'update ip info error when create v2v task, update_data: %s', updata_data) return json_helper.format_api_resp( code=ErrorCode.SYS_ERR, msg="更新IP状态失败") # 生成request_id request_Id = v2v_op.generate_req_id() # 生成vm的uuid和mac vmuuid = randomUUID() vmmac = randomMAC() if not vm_osver: vm_osver = "unknown" # 信息入instance相关库表 instance_info = instance_db_info(vmuuid, vmname, vm_app_info, vm_owner, flavor_id, vm_group_id, host, vmmac, data_disk, ip_id, vm_ostype, request_Id, vm_osver) if instance_info < 0: return json_helper.format_api_resp(code=ErrorCode.SYS_ERR, msg='信息入库失败') # 将步骤信息存入instance_action表 v2v_cd_d1 = { 'action': esx_v2vActions.CREATE_DEST_DIR, 'request_id': request_Id, 'message': 'start', 'start_time': get_datetime_str() } in_a_s.InstanceActionsServices().add_instance_action_info(v2v_cd_d1) v2v_cr_pl = { 'action': esx_v2vActions.CREATE_STOR_POOL, 'request_id': request_Id, 'message': 'other', 'start_time': get_datetime_str() } in_a_s.InstanceActionsServices().add_instance_action_info(v2v_cr_pl) v2v_cp_fl = { 'action': esx_v2vActions.COPY_FILE_TO_LOCAL, 'request_id': request_Id, 'message': 'other', 'start_time': get_datetime_str() } in_a_s.InstanceActionsServices().add_instance_action_info(v2v_cp_fl) v2v_file = { 'action': esx_v2vActions.VIRT_V2V_FILES, 'request_id': request_Id, 'message': 'other', 'start_time': get_datetime_str() } in_a_s.InstanceActionsServices().add_instance_action_info(v2v_file) v2v_del_tmp = { 'action': esx_v2vActions.DELETE_TMP_FILE, 'request_id': request_Id, 'message': 'other', 'start_time': get_datetime_str() } in_a_s.InstanceActionsServices().add_instance_action_info(v2v_del_tmp) v2v_sysdisk_std = { 'action': esx_v2vActions.VM_SYS_DISK_STD, 'request_id': request_Id, 'message': 'other', 'start_time': get_datetime_str() } in_a_s.InstanceActionsServices().add_instance_action_info( v2v_sysdisk_std) v2v_datadisk_std = { 'action': esx_v2vActions.VM_DATA_DISK_STD, 'request_id': request_Id, 'message': 'other', 'start_time': get_datetime_str() } in_a_s.InstanceActionsServices().add_instance_action_info( v2v_datadisk_std) v2v_def_1 = { 'action': esx_v2vActions.VM_DEFINE1, 'request_id': request_Id, 'message': 'other', 'start_time': get_datetime_str() } in_a_s.InstanceActionsServices().add_instance_action_info(v2v_def_1) v2v_star_1 = { 'action': esx_v2vActions.VM_START1, 'request_id': request_Id, 'message': 'other', 'start_time': get_datetime_str() } in_a_s.InstanceActionsServices().add_instance_action_info(v2v_star_1) if vm_ostype == "Windows": v2v_att_disk = { 'action': esx_v2vActions.ATTACH_DISK, 'request_id': request_Id, 'message': 'other', 'start_time': get_datetime_str() } in_a_s.InstanceActionsServices().add_instance_action_info( v2v_att_disk) v2v_win_std = { 'action': esx_v2vActions.WINDOWS_STD, 'request_id': request_Id, 'message': 'other', 'start_time': get_datetime_str() } in_a_s.InstanceActionsServices().add_instance_action_info( v2v_win_std) v2v_vm_def2 = { 'action': esx_v2vActions.WINDOWS_DISK_CH, 'request_id': request_Id, 'message': 'other', 'start_time': get_datetime_str() } in_a_s.InstanceActionsServices().add_instance_action_info( v2v_vm_def2) v2v_vm_star2 = { 'action': esx_v2vActions.VM_START2, 'request_id': request_Id, 'message': 'other', 'start_time': get_datetime_str() } in_a_s.InstanceActionsServices().add_instance_action_info( v2v_vm_star2) message = '信息已添加至任务队列,等待执行' # 将v2v信息存入v2v_task表 v2v_data = { 'source': VMCreateSource.ESX, 'request_id': request_Id, 'destory': '0', 'start_time': get_datetime_str(), 'status': 0, 'vm_ip': vmip, 'vm_name': vmname, 'vmvlan': vmvlan, 'flavor_id': flavor_id, 'cloud_area': esx_env, 'vm_ostype': vm_ostype, 'vm_app_info': vm_app_info, 'vm_owner': vm_owner, 'vm_group_id': vm_group_id, 'user_id': user_id, 'vm_mac': vmmac, 'vm_uuid': vmuuid, 'cancel': '0', 'dest_dir': '/app/image/' + vmuuid, 'on_task': '0', 'dest_host': host, 'esx_ip': esx_ip, 'esx_passwd': esx_passwd, 'vmware_vm': vmware_vm, 'step_done': esx_v2vActions.BEGIN } v2v_insert = v2v_op.v2vTaskService().add_v2v_task_info(v2v_data) if v2v_insert.get('row_num') <= 0: logging.info('insert info to v2v_task failed! %s', v2v_data) return json_helper.format_api_resp(code=ErrorCode.SYS_ERR, msg='信息入库失败') return json_helper.format_api_resp(code=ErrorCode.SUCCESS, msg=message)
def _task_into_db(task): # 入参赋值 vmname = task['vm_name'] vmip = task['vm_ip'] flavor_id = task['flavor_id'] cloudarea = task['cloud_area'] vm_ostype = task['vm_ostype'] vm_app_info = task['vm_app_info'] vm_owner = task['vm_owner'] vm_group_id = task['group_id'] user_id = task['user_id'] vm_segment = task['segment'] vm_disk = task['vm_disk'] vm_osver = task['vm_osver'] host = task['dest_host'] # 入参完全性判断 if not vmname or not vmip or not flavor_id or not cloudarea or not vm_ostype \ or not vm_app_info or not vm_owner or not vm_group_id or not user_id or not vm_segment: logging.info('params are invalid or missing') message = '入参缺失' return False, message else: # 获取flavor信息 flavor_info = flavor_s.FlavorService().get_flavor_info(flavor_id) if not flavor_info: logging.info('id: %s flavor info not in db when create instance', flavor_id) message = '实例规格数据有误,无法进行v2v' return False, message vmcpu = flavor_info['vcpu'] vmmem = flavor_info['memory_mb'] vmhost = ho_s.HostService().get_host_info_by_hostip(host) ret_4 = ho_s.pre_allocate_host_resource(vmhost['id'], vmcpu, vmmem, 50) if ret_4 != 1: logging.error('资源预分配失败') message = '资源预分频失败' return False, message # 获取并录入IP信息 vm_segment = segment_s.SegmentService().get_segment_info_bysegment( vm_segment) if vm_segment is None: message = '网段信息有误,无法进行v2v' return False, message else: segment_id = vm_segment['id'] vm_netmask = vm_segment['netmask'] vm_gateway = vm_segment['gateway_ip'] vm_dns1 = vm_segment['dns1'] vm_dns2 = vm_segment['dns2'] vmvlan = vm_segment['vlan'] ip_data = ip_s.IPService().get_ip_info_by_ipaddress(vmip) if ip_data is None: ip_data = { 'ip_address': vmip, 'segment_id': segment_id, 'netmask': vm_netmask, 'vlan': vmvlan, 'gateway_ip': vm_gateway, 'dns1': vm_dns1, 'dns2': vm_dns2, 'created_at': get_datetime_str(), 'status': '1' } ret = ip_s.IPService().add_ip_info(ip_data) if ret.get('row_num') <= 0: logging.info( 'add ip info error when create v2v task, insert_data: %s', ip_data) message = "录入IP信息失败" return False, message else: ip_id = ret.get('last_id') else: ip_data_status = ip_data['status'] vmvlan = ip_data['vlan'] if ip_data_status != '0': message = "IP与现有环境冲突,无法进行v2v" return False, message else: ip_id = ip_data['id'] where_data = {'id': ip_id} updata_data = { 'status': '1', 'updated_at': get_datetime_str() } ret1 = ip_s.IPService().update_ip_info( updata_data, where_data) if not ret1: logging.info( 'update ip info error when create v2v task, update_data: %s', updata_data) message = "更新IP状态失败" return False, message # 生成request_id request_id = v2v_op.generate_req_id() # 生成vm的uuid和mac vmuuid = randomUUID() vmmac = randomMAC() # 信息入instance相关库表 instance_tag, instance_info = _instance_db_info( vmuuid, vmname, vm_app_info, vm_owner, flavor_id, vm_group_id, host, vmmac, vm_disk, ip_id, vm_ostype, request_id, vm_osver) if not instance_tag: message = instance_info return False, message # 将步骤信息存入instance_action表 # 将createdir信息存入instance_action表 v2v_cd_d1 = { 'action': v2vActions.CREATE_DEST_DIR, 'request_id': request_id, 'message': 'start', 'start_time': get_datetime_str() } in_a_s.InstanceActionsServices().add_instance_action_info(v2v_cd_d1) # 将getfile信息存入instance_action表 v2v_gf_d1 = { 'action': v2vActions.GET_VM_FILE, 'request_id': request_id, 'message': 'other', 'start_time': get_datetime_str() } in_a_s.InstanceActionsServices().add_instance_action_info(v2v_gf_d1) # 将copy disk信息存入instance_action表 v2v_cpd_d1 = { 'action': v2vActions.COPY_VM_DISK, 'request_id': request_id, 'message': 'other', 'start_time': get_datetime_str() } in_a_s.InstanceActionsServices().add_instance_action_info(v2v_cpd_d1) # 将copy xml信息存入instance_action表 v2v_cpx_d1 = { 'action': v2vActions.COPY_VM_XML, 'request_id': request_id, 'message': 'other', 'start_time': get_datetime_str() } in_a_s.InstanceActionsServices().add_instance_action_info(v2v_cpx_d1) # 将创建存储池信息存入instance_action表 v2v_csp_d1 = { 'action': v2vActions.CREATE_STOR_POOL, 'request_id': request_id, 'message': 'other', 'start_time': get_datetime_str() } in_a_s.InstanceActionsServices().add_instance_action_info(v2v_csp_d1) # 将vm标准化信息存入instance_action表 v2v_vmd_d1 = { 'action': v2vActions.VM_STANDARDLIZE, 'request_id': request_id, 'message': 'other', 'start_time': get_datetime_str() } in_a_s.InstanceActionsServices().add_instance_action_info(v2v_vmd_d1) # 将vm注册信息存入instance_action表 v2v_vmdef_d1 = { 'action': v2vActions.VM_DEFINE, 'request_id': request_id, 'message': 'other', 'start_time': get_datetime_str() } in_a_s.InstanceActionsServices().add_instance_action_info(v2v_vmdef_d1) # 将IP注入信息存入instance_action表 v2v_vmipj_d1 = { 'action': v2vActions.IP_INJECT, 'request_id': request_id, 'message': 'other', 'start_time': get_datetime_str() } in_a_s.InstanceActionsServices().add_instance_action_info(v2v_vmipj_d1) # 将vm开机信息存入instance_action表 v2v_vmstar_d1 = { 'action': v2vActions.VM_START, 'request_id': request_id, 'message': 'other', 'start_time': get_datetime_str() } in_a_s.InstanceActionsServices().add_instance_action_info( v2v_vmstar_d1) message = '信息已添加至任务队列,等待执行' # 将v2v信息存入v2v_task表 v2v_data = { 'request_id': request_id, 'destory': '0', 'start_time': get_datetime_str(), 'status': 0, 'vm_ip': vmip, 'vm_name': vmname, 'vmvlan': vmvlan, 'flavor_id': flavor_id, 'cloud_area': cloudarea, 'vm_ostype': vm_ostype, 'vm_app_info': vm_app_info, 'vm_owner': vm_owner, 'vm_group_id': vm_group_id, 'user_id': user_id, 'vm_mac': vmmac, 'vm_uuid': vmuuid, 'cancel': '0', 'dest_dir': '/app/image/' + vmuuid, 'on_task': '0', 'port': '10000', 'source': VMCreateSource.OPENSTACK } v2v_insert = v2v_op.v2vTaskService().add_v2v_task_info(v2v_data) if v2v_insert.get('row_num') <= 0: logging.info('insert info to v2v_task failed! %s', v2v_data) message = '信息入库失败' return False, message # 将目标kvmhost存入信息表 v2v_op.update_v2v_desthost(request_id, host) v2v_op.update_v2v_step(request_id, v2vActions.BEGIN) return True, message
def v2v_openstack_intodb(hostpool_id): ''' v2v_openstack :param hostpool_id: :return: ''' # 判断是否为重试操作 retry = request.values.get('retry') # 如果非重试进行以下步骤 if retry != '1': # 入参赋值 vmname = request.values.get('vm_name').strip() vmip = request.values.get('vm_ip').strip() flavor_id = request.values.get('flavor_id').strip() cloudarea = request.values.get('cloud_area').strip() vm_ostype = request.values.get('vm_ostype').strip() vm_app_info = request.values.get('vm_app_info').strip() vm_owner = request.values.get('vm_owner').strip() vm_group_id = request.values.get('group_id').strip() user_id = request.values.get('user_id').strip() vm_segment = request.values.get('segment').strip() # 入参完全性判断 if not vmname or not vmip or not flavor_id or not cloudarea or not vm_ostype \ or not vm_app_info or not vm_owner or not vm_group_id or not user_id or not vm_segment: logging.info('params are invalid or missing') return json_helper.format_api_resp(code=ErrorCode.PARAM_ERR, msg='入参缺失') else: # 获取flavor信息 flavor_info = flavor_service.FlavorService().get_flavor_info( flavor_id) if not flavor_info: logging.info( 'id: %s flavor info not in db when create instance', flavor_id) return json_helper.format_api_resp(code=ErrorCode.SYS_ERR, msg='实例规格数据有误,无法进行v2v') vmcpu = flavor_info['vcpu'] vmmem = flavor_info['memory_mb'] # 获取对应openstack环境的管理节点及ssh账户信息 if cloudarea == "SIT": ctr_host = '10.202.83.12' ctr_pass = decrypt(OPENSTACK_SIT_PASS) elif cloudarea == "DEV": ctr_host = "10.202.123.4" ctr_pass = decrypt(OPENSTACK_DEV_PASS) else: return json_helper.format_api_resp( code=ErrorCode.SYS_ERR, msg='openstack环境参数错误,无法进行v2v操作') #判断vm信息是否输入错误 vmexist = vm_exist(vmip, ctr_host, ctr_pass) if vmexist == False: return json_helper.format_api_resp(code=ErrorCode.SYS_ERR, msg='获取vm信息失败') #获取OS版本失败 osstat, verdata = get_vm_version(ctr_host, ctr_pass, vm_ostype, vmip) if osstat == False: return json_helper.format_api_resp(code=ErrorCode.SYS_ERR, msg='获取vmOS版本失败') else: ver_data = verdata # 获取待迁移vm磁盘大小 vdiskdata = vm_disk_size(vmip, cloudarea, ctr_host, ctr_pass) if vdiskdata == False: return json_helper.format_api_resp(code=ErrorCode.SYS_ERR, msg='获取vm磁盘信息失败') vdisk = vdiskdata data_disk = vdisk - 80 # 判断待转化vm是否关机 vmshutdown = vm_stat(vmip, ctr_host, ctr_pass) if vmshutdown == False: return json_helper.format_api_resp(code=ErrorCode.SYS_ERR, msg='待转化vm未关机') # 获取可用目标host host_code, host_data, host_msg = cal_host(hostpool_id, vmcpu, vmmem, data_disk, vm_group_id) if host_code < 0: return json_helper.format_api_resp(code=host_code, msg=host_msg) else: host = host_data vmhost = ho_s.HostService().get_host_info_by_hostip(host) ret_4 = ho_s.pre_allocate_host_resource(vmhost['id'], vmcpu, vmmem, 50) if ret_4 != 1: logging.error('资源预分配失败') message = '资源预分频失败' return json_helper.format_api_resp(code=ErrorCode.SYS_ERR, msg=message) # 获取并录入IP信息 vm_segment = seg_s.SegmentService().get_segment_info_bysegment( vm_segment) if vm_segment == None: return json_helper.format_api_resp(code=ErrorCode.SYS_ERR, msg='网段信息有误,无法进行v2v') else: segment_id = vm_segment['id'] vm_netmask = vm_segment['netmask'] vm_gateway = vm_segment['gateway_ip'] vm_dns1 = vm_segment['dns1'] vm_dns2 = vm_segment['dns2'] vmvlan = vm_segment['vlan'] ip_data = ip_s.IPService().get_ip_info_by_ipaddress(vmip) if ip_data == None: ip_data = { 'ip_address': vmip, 'segment_id': segment_id, 'netmask': vm_netmask, 'vlan': vmvlan, 'gateway_ip': vm_gateway, 'dns1': vm_dns1, 'dns2': vm_dns2, 'created_at': get_datetime_str(), 'status': '1' } ret = ip_s.IPService().add_ip_info(ip_data) if ret.get('row_num') <= 0: logging.info( 'add ip info error when create v2v task, insert_data: %s', ip_data) return json_helper.format_api_resp( code=ErrorCode.SYS_ERR, msg="录入IP信息失败") else: ip_id = ret.get('last_id') else: ip_data_status = ip_data['status'] vmvlan = ip_data['vlan'] if ip_data_status != '0': return json_helper.format_api_resp( code=ErrorCode.SYS_ERR, msg="IP与现有环境冲突,无法进行v2v") else: ip_id = ip_data['id'] where_data = {'id': ip_id} updata_data = { 'status': '1', 'updated_at': get_datetime_str() } ret1 = ip_s.IPService().update_ip_info( updata_data, where_data) if not ret1: logging.info( 'update ip info error when create v2v task, update_data: %s', updata_data) return json_helper.format_api_resp( code=ErrorCode.SYS_ERR, msg="更新IP状态失败") # 生成request_id request_Id = v2v_op.generate_req_id() # 生成vm的uuid和mac vmuuid = randomUUID() vmmac = randomMAC() # 信息入instance相关库表 instance_info = instance_db_info(vmuuid, vmname, vm_app_info, vm_owner, flavor_id, vm_group_id, host, vmmac, data_disk, ip_id, vm_ostype, request_Id, ver_data) if instance_info < 0: return json_helper.format_api_resp(code=ErrorCode.SYS_ERR, msg='信息入库失败') # 将步骤信息存入instance_action表 # 将createdir信息存入instance_action表 v2v_cd_d1 = { 'action': v2vActions.CREATE_DEST_DIR, 'request_id': request_Id, 'message': 'start', 'start_time': get_datetime_str() } in_a_s.InstanceActionsServices().add_instance_action_info( v2v_cd_d1) # 将getfile信息存入instance_action表 v2v_gf_d1 = { 'action': v2vActions.GET_VM_FILE, 'request_id': request_Id, 'message': 'other', 'start_time': get_datetime_str() } in_a_s.InstanceActionsServices().add_instance_action_info( v2v_gf_d1) # 将copy disk信息存入instance_action表 v2v_cpd_d1 = { 'action': v2vActions.COPY_VM_DISK, 'request_id': request_Id, 'message': 'other', 'start_time': get_datetime_str() } in_a_s.InstanceActionsServices().add_instance_action_info( v2v_cpd_d1) # 将copy xml信息存入instance_action表 v2v_cpx_d1 = { 'action': v2vActions.COPY_VM_XML, 'request_id': request_Id, 'message': 'other', 'start_time': get_datetime_str() } in_a_s.InstanceActionsServices().add_instance_action_info( v2v_cpx_d1) # 将创建存储池信息存入instance_action表 v2v_csp_d1 = { 'action': v2vActions.CREATE_STOR_POOL, 'request_id': request_Id, 'message': 'other', 'start_time': get_datetime_str() } in_a_s.InstanceActionsServices().add_instance_action_info( v2v_csp_d1) # 将vm标准化信息存入instance_action表 v2v_vmd_d1 = { 'action': v2vActions.VM_STANDARDLIZE, 'request_id': request_Id, 'message': 'other', 'start_time': get_datetime_str() } in_a_s.InstanceActionsServices().add_instance_action_info( v2v_vmd_d1) # 将vm注册信息存入instance_action表 v2v_vmdef_d1 = { 'action': v2vActions.VM_DEFINE, 'request_id': request_Id, 'message': 'other', 'start_time': get_datetime_str() } in_a_s.InstanceActionsServices().add_instance_action_info( v2v_vmdef_d1) # 将IP注入信息存入instance_action表 v2v_vmipj_d1 = { 'action': v2vActions.IP_INJECT, 'request_id': request_Id, 'message': 'other', 'start_time': get_datetime_str() } in_a_s.InstanceActionsServices().add_instance_action_info( v2v_vmipj_d1) # 将vm开机信息存入instance_action表 v2v_vmstar_d1 = { 'action': v2vActions.VM_START, 'request_id': request_Id, 'message': 'other', 'start_time': get_datetime_str() } in_a_s.InstanceActionsServices().add_instance_action_info( v2v_vmstar_d1) message = '信息已添加至任务队列,等待执行' # 将v2v信息存入v2v_task表 v2v_data = { 'request_id': request_Id, 'destory': '0', 'start_time': get_datetime_str(), 'status': 0, 'vm_ip': vmip, 'vm_name': vmname, 'vmvlan': vmvlan, 'flavor_id': flavor_id, 'cloud_area': cloudarea, 'vm_ostype': vm_ostype, 'vm_app_info': vm_app_info, 'vm_owner': vm_owner, 'vm_group_id': vm_group_id, 'user_id': user_id, 'vm_mac': vmmac, 'vm_uuid': vmuuid, 'cancel': '0', 'dest_dir': '/app/image/' + vmuuid, 'on_task': '0', 'port': '10000', 'source': VMCreateSource.OPENSTACK } v2v_insert = v2v_op.v2vTaskService().add_v2v_task_info(v2v_data) if v2v_insert.get('row_num') <= 0: logging.info('insert info to v2v_task failed! %s', v2v_data) return json_helper.format_api_resp(code=ErrorCode.SYS_ERR, msg='信息入库失败') # 将目标kvmhost存入信息表 v2v_op.update_v2v_desthost(request_Id, host) v2v_op.update_v2v_step(request_Id, v2vActions.BEGIN) return json_helper.format_api_resp(code=ErrorCode.SUCCESS, msg=message)
def v2v_openstack_del(): #获取入参信息 delete = request.values.get('delete') request_his = request.values.get('request_id') source = v2v_op.v2vTaskService().get_v2v_task_by_requestid( request_his)['source'] if not source: return json_helper.format_api_resp(code=ErrorCode.PARAM_ERR, msg='v2v来源缺失') if delete != '1': return json_helper.format_api_resp(code=ErrorCode.PARAM_ERR, msg='参数错误') #判断当前任务是否完成 data, message = v2v_op.get_v2v_deleteable(request_his) if data != '2': return_msg = message return json_helper.format_api_resp(code=ErrorCode.PARAM_ERR, msg=return_msg) else: if source == VMCreateSource.OPENSTACK: del_res, del_msg = del_action(request_his) if del_res == False: return json_helper.format_api_resp(code=ErrorCode.SYS_ERR, msg=del_msg) else: tag, errmsg = esx_del_action(request_his) if not tag: return json_helper.format_api_resp(code=ErrorCode.SYS_ERR, msg=errmsg) v2v_task = v2v_op.v2vTaskService().get_v2v_task_by_requestid( request_his) v2v_vm_uuid = v2v_task['vm_uuid'] #更新instance表 instance_info = ins_s.InstanceService().get_instance_info_by_uuid( v2v_vm_uuid) instance_id = instance_info['id'] update_data = {'isdeleted': '1', 'deleted_at': get_datetime_str()} where_data = {'id': instance_id} ret = ins_s.InstanceService().update_instance_info( update_data, where_data) if ret != 1: logging.error('删除instance %s 错误', instance_id) return json_helper.format_api_resp(code=ErrorCode.SYS_ERR, msg='删除instance错误') # instance_flavor ret_f = ins_f_s.InstanceFlavorService().delete_instance_flavor( instance_id) if ret_f != 1: logging.error('delete instance %s flavor error', instance_id) return json_helper.format_api_resp(code=ErrorCode.SYS_ERR, msg='删除instance flavor错误') # instance_group ret_g = ins_g_s.InstanceGroupService().delete_instance_group_info( instance_id) if ret_g != 1: logging.error('delete instance %s group error', instance_id) return json_helper.format_api_resp(code=ErrorCode.SYS_ERR, msg='删除instance group错误') # instance_host update_data_h = {'isdeleted': '1', 'deleted_at': get_datetime_str()} where_data_h = {'instance_id': instance_id} ret_h = ins_h_s.InstanceHostService().update_instance_host_info( update_data_h, where_data_h) if ret_h != 1: logging.error('delete instance %s host error', instance_id) return json_helper.format_api_resp(code=ErrorCode.SYS_ERR, msg='删除instance host错误') # instance_disk update_data_d = {'isdeleted': '1', 'deleted_at': get_datetime_str()} where_data_d = {'instance_id': instance_id} ret_d = ins_d_s.InstanceDiskService().update_instance_disk_info( update_data_d, where_data_d) if ret_d != 1: logging.error('delete instance %s disk error', instance_id) return json_helper.format_api_resp(code=ErrorCode.SYS_ERR, msg='删除instance disk错误') # instance_ip update_data_ip = {'isdeleted': '1', 'deleted_at': get_datetime_str()} where_data_ip = {'instance_id': instance_id} ret_i_ip = ins_ip_s.InstanceIPService().update_instance_ip_info( update_data_ip, where_data_ip) if ret_i_ip != 1: logging.error('delete instance %s ip error', instance_id) return json_helper.format_api_resp(code=ErrorCode.SYS_ERR, msg='删除instance ip错误') #更新v2v_instance_info update_data = {'isdeleted': '1', 'deleted_at': get_datetime_str()} where_data = {'instance_id': instance_id} ret_v2v_in_i = v2v_in_i.v2vInstanceinfo().update_v2v_status( update_data, where_data) if ret_v2v_in_i != 1: logging.error('delete v2v instance info error') return json_helper.format_api_resp(code=ErrorCode.SYS_ERR, msg='删除v2v instance info错误') # 删除vm的ip ip_data = ins_s.get_ip_of_instance(instance_id) ip_id = ip_data['id'] if ip_data: ip_s.del_ip_info(ip_id) #更新v2v_task表 where_v2v = {'request_id': request_his} update_v2v = {'destory': '1'} v2v_op.v2vTaskService().update_v2v_status(update_v2v, where_v2v) return json_helper.format_api_resp(code=ErrorCode.SUCCESS, msg='删除v2v任务完成')
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()
def esx_do_task(): # 获取当前在进行的任务清单 list_working = v2v_op.get_v2v_todo() if not list_working: time.sleep(60) return 0 for i in list_working: # 获取当前任务信息 request_id = i['request_id'] vm_ip = i['vm_ip'] vm_name = i['vm_name'] vm_vlan = i['vmvlan'] flavor_id = i['flavor_id'] cloud_area = i['cloud_area'] vm_mac = i['vm_mac'] vm_uuid = i['vm_uuid'] step_done = i['step_done'] dest_host = i['dest_host'] status = i['status'] ostype = i['vm_ostype'] dest_dir = i['dest_dir'] on_task = i['on_task'] source = i['source'] esx_ip = i['esx_ip'] esx_passwd = i['esx_passwd'] vmware_vm = i['vmware_vm'] if status == 0 and on_task == '0' and source == VMCreateSource.ESX: # 获取flavor信息 flavor_info = flavor_service.FlavorService().get_flavor_info( flavor_id) vmcpu = str(flavor_info['vcpu']) vmmem = flavor_info['memory_mb'] # 创建目标文件夹 esx_v2v_sta1 = v2v_op.get_v2v_running_by_reqid(request_id) if esx_v2v_sta1 == True: step_done1 = v2v_op.get_v2v_step(request_id) if step_done1 == esx_v2vActions.BEGIN: threadlock = threading.Lock() threadlock.acquire() v2v_op.updata_v2v_ontask(request_id, '1') threadlock.release() # 在目标服务器上创建文件夹 createdir(dest_host, request_id, "command", dest_dir) # 创建存储池 esx_v2v_sta2 = v2v_op.get_v2v_running_by_reqid(request_id) if esx_v2v_sta2 == True: step_done2 = v2v_op.get_v2v_step(request_id) if step_done2 == esx_v2vActions.CREATE_DEST_DIR: threadlock = threading.Lock() threadlock.acquire() v2v_op.updata_v2v_ontask(request_id, '1') threadlock.release() # 创建存储池 host_ip = dest_host dir = v2v_op.get_v2v_destdir(request_id) dir_uuid = dir.replace('/app/image/', '') _create_storage_pool(host_ip, dir_uuid, request_id) # 拷贝vm文件至目标host esx_v2v_sta3 = v2v_op.get_v2v_running_by_reqid(request_id) if esx_v2v_sta3 == True: # 获取任务 step_done3 = v2v_op.get_v2v_step(request_id) if step_done3 == esx_v2vActions.CREATE_STOR_POOL: threadlock = threading.Lock() threadlock.acquire() v2v_op.updata_v2v_ontask(request_id, '1') threadlock.release() # 将vm磁盘文件拷贝至kvmhost本地 virt_v2v_copy_to_local(dest_dir, dest_host, esx_ip, esx_passwd, vmware_vm, request_id) # v2v拷贝后的vm文件 esx_v2v_sta4 = v2v_op.get_v2v_running_by_reqid(request_id) if esx_v2v_sta4 == True: step_done4 = v2v_op.get_v2v_step(request_id) if step_done4 == esx_v2vActions.COPY_FILE_TO_LOCAL: threadlock = threading.Lock() threadlock.acquire() v2v_op.updata_v2v_ontask(request_id, '1') threadlock.release() # 将获取到的vm文件v2v virt_v2v(dest_dir, dest_host, request_id, vmware_vm) # 删除v2v过程中的临时文件 esx_v2v_sta5 = v2v_op.get_v2v_running_by_reqid(request_id) if esx_v2v_sta5 == True: step_done5 = v2v_op.get_v2v_step(request_id) if step_done5 == esx_v2vActions.VIRT_V2V_FILES: v2v_op.updata_v2v_ontask(request_id, '1') # 删除v2v过程中的临时文件 rmrawfile(dest_host, vmware_vm, request_id) # vm系统盘标准化 esx_v2v_sta6 = v2v_op.get_v2v_running_by_reqid(request_id) if esx_v2v_sta6 == True: step_done6 = v2v_op.get_v2v_step(request_id) if step_done6 == esx_v2vActions.DELETE_TMP_FILE: v2v_op.updata_v2v_ontask(request_id, '1') # vm系统盘标准化 ch_sys_disk_name(dest_dir, dest_host, vmware_vm, vm_name, request_id) # vm数据盘标准化 esx_v2v_sta7 = v2v_op.get_v2v_running_by_reqid(request_id) if esx_v2v_sta7 == True: step_done7 = v2v_op.get_v2v_step(request_id) if step_done7 == esx_v2vActions.VM_SYS_DISK_STD: v2v_op.updata_v2v_ontask(request_id, '1') vm_data_disk_std(dest_dir, dest_host, vm_name, request_id, vmware_vm) # vm注册 esx_v2v_sta8 = v2v_op.get_v2v_running_by_reqid(request_id) if esx_v2v_sta8 == True: step_done8 = v2v_op.get_v2v_step(request_id) if step_done8 == esx_v2vActions.VM_DATA_DISK_STD: v2v_op.updata_v2v_ontask(request_id, '1') vmlistdata = v2v_op.v2vTaskService( ).get_v2v_task_by_requestid(request_id)['volumelist'] volumes_d = [] vmsysdisk = dest_dir + '/' + vm_name + '.img' volumes_d.append(vmsysdisk) if int(vmlistdata) > 0: tag = 1 while tag <= int(vmlistdata): vmdatadisk = dest_dir + '/' + vm_name + '_disk' + str( tag) tag = tag + 1 volumes_d.append(vmdatadisk) print volumes_d vm_define(request_id, ostype, dest_host, vm_name, vmmem, vmcpu, vm_uuid, volumes_d, vm_vlan, vm_mac) # vm开机1 esx_v2v_sta9 = v2v_op.get_v2v_running_by_reqid(request_id) if esx_v2v_sta9 == True: step_done9 = v2v_op.get_v2v_step(request_id) if step_done9 == esx_v2vActions.VM_DEFINE1: v2v_op.updata_v2v_ontask(request_id, '1') vm_start1(dest_host, vm_name, request_id, 'shell', ostype) # 添加临时盘 esx_v2v_sta10 = v2v_op.get_v2v_running_by_reqid(request_id) if esx_v2v_sta10 == True: step_done10 = v2v_op.get_v2v_step(request_id) if step_done10 == esx_v2vActions.VM_START1: v2v_op.updata_v2v_ontask(request_id, '1') v2v_esx_disk_attach_static(vm_name, dest_host, request_id) #win vm磁盘格式修改 esx_v2v_sta12 = v2v_op.get_v2v_running_by_reqid(request_id) if esx_v2v_sta12 == True: step_done12 = v2v_op.get_v2v_step(request_id) if step_done12 == esx_v2vActions.ATTACH_DISK: v2v_op.updata_v2v_ontask(request_id, '1') win_disk_ch(vm_name, dest_host, request_id) #vm 开机2 esx_v2v_sta13 = v2v_op.get_v2v_running_by_reqid(request_id) if esx_v2v_sta13 == True: step_done13 = v2v_op.get_v2v_step(request_id) if step_done13 == esx_v2vActions.WINDOWS_DISK_CH: v2v_op.updata_v2v_ontask(request_id, '1') vm_start2(dest_host, vm_name, request_id, 'shell', vm_uuid) # win vm 标准化 esx_v2v_sta11 = v2v_op.get_v2v_running_by_reqid(request_id) if esx_v2v_sta11 == True: step_done11 = v2v_op.get_v2v_step(request_id) if step_done11 == esx_v2vActions.VM_START2: v2v_op.updata_v2v_ontask(request_id, '1') win_vm_std(request_id)