def cloneVm(request): logging.info('克隆虚拟机') try: request_data = json.loads(request.body) keys = request_data.keys() if keys is not None: vmParamter = ['template_vm_id', 'new_vm_name', 'vm_datacenter', 'vm_cluster', 'vm_datastore', 'memory', 'cpu_num'] paramCheck = False for param in vmParamter: if param not in keys: paramCheck = True res = { 'code': '60011', 'message': '克隆失败,缺少参数 %s' % param } break if paramCheck : return render_json(res) # 模板ID templateVmId = request_data.get('template_vm_id') # 获取配置信息 newVmName = request_data.get('new_vm_name') vmDatacenter = request_data.get('vm_datacenter') vmCluster = request_data.get('vm_cluster') vmDatastore = request_data.get('vm_datastore') vmMemory = request_data.get('memory') cpuNum = request_data.get('cpu_num') logging.info('获取vcenter') vcenterVirtualMachineModel = VcenterVirtualMachine.objects.get(id=templateVmId) accountModel = vcenterVirtualMachineModel.account if accountModel is None: logging.info('vcenter为空') res = { 'code': '60012', 'message': u"克隆失败,虚拟机账号错误", } return render_json(res) else: logging.info('vcenter不为空') vmManager = VmManage(host=accountModel.vcenter_host,user=accountModel.account_name,password=accountModel.account_password,port=accountModel.vcenter_port,ssl=None) template = vmManager.get_vm_by_name(vcenterVirtualMachineModel.name) # vm 配置信息 vmconf = vim.vm.ConfigSpec(numCPUs=int(cpuNum), memoryMB=int(vmMemory)) result = vmManager.clone(template=template, vm_name=newVmName, datacenter_name=vmDatacenter, vm_folder=None, datastore_name=vmDatastore, cluster_name=vmCluster, resource_pool=None, power_on=False, vmconf=vmconf ) logging.info('克隆结果:%s' % str(result)) # close out connection Disconnect(vmManager) if result == False: res = { 'code': '60013', 'message': '虚拟机克隆失败' } return render_json(res) else: res = { 'code': '0', 'message': u"克隆成功", } return render_json(res) else: res = { 'code': '60014', 'message': u"请求方式错误", } return render_json(res) except Exception as e: logging.info('程序抛出错误:%s' % e.message ) res = { 'code': '50000', 'message': e.message, } return render_json(res)
def cloneVmRequest(request): logger.info("克隆虚拟机") try: if request.method == 'POST': vmId = request.POST['vmId'] vmName = request.POST['vmName'] vmDatacenter = request.POST['vmDatacenter'] vmCluster = request.POST['vmCluster'] vmDatastore = request.POST['vmDatastore'] print 'vmid is %s, vmName is %s , vmDatacenter is %s, vmCluster is %s, vmDatastore is %s '.format(vmId,vmName,vmDatacenter,vmCluster,vmDatastore) vcenterVirtualMachineModel = VcenterVirtualMachine.objects.get(id=vmId) accountModel = vcenterVirtualMachineModel.account if accountModel is None: res = { 'result': True, 'message': u"克隆失败,资源账号错误", } return render_json(res) else: vmManager = VmManage(host=accountModel.vcenter_host,user=accountModel.account_name,password=accountModel.account_password,port=accountModel.vcenter_port,ssl=None) template = vmManager.get_vm_by_name(vcenterVirtualMachineModel.name) # print template result = vmManager.clone(template=template, vm_name=vmName, datacenter_name=vmDatacenter, vm_folder=None, datastore_name=vmDatastore, cluster_name=vmCluster, resource_pool=None, power_on=True) #同步信息 if result == False: res = { 'result': True, 'message': u"克隆失败", } else: # vm = vmManager.find_by_uuid(vcenterVirtualMachineModel.instance_uuid) # vcenterVirtualMachineModel.power_state = vm.summary.runtime.powerState vm = vmManager.get_vm_by_name(vmName) cloneVmModel = convertVmEntityToVcenterVirtualMachine(vm) #数据中心,需要通过名字查询 todo cloneVmModel.datacenter = vcenterVirtualMachineModel.datacenter #集群需要通过名字查询 todo cloneVmModel.cluster = vcenterVirtualMachineModel.cluster cloneVmModel.account = vcenterVirtualMachineModel.account cloneVmModel.save() res = { 'result': True, 'message': u"克隆成功", } except Exception as e: traceback.print_exc() # print str(e) res = { 'result': False, 'message': u"克隆失败", } return render_json(res)