def syncCloudAccount(request): try: if request.method =='POST': templateVmId = request.POST['templateVmId'] vmName = request.POST['vmName'] if templateVmId is None: res = { 'code': '60021', 'message': '同步失败,缺少参数templateVmId' } return render_json(res) if vmName is None: res = { 'code': '60022', 'message': '同步失败,缺少参数 vmName' } return render_json(res) vcenterVirtualMachineModel = VcenterVirtualMachine.objects.get(id=templateVmId) accountModel = vcenterVirtualMachineModel.account vmManager = VmManage(host=accountModel.vcenter_host, user=accountModel.account_name, password=accountModel.account_password, port=accountModel.vcenter_port, ssl=None) 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 = { 'code':'0', 'message':'' } else: res = { 'code':'60023', 'message':'' } except Exception as e: res = { 'code':'50000' , 'message':"同步失败 ,%s" %e.message } return render_json(res)
def updateVMConfiguration(request): logging.info(u'更新虚拟机的配置信息') from home_application.celery_tasks import sync_virtualmachine try: request_data = json.loads(request.body) logger.info(u'request_data数据 %s' % request_data) keys = request_data.keys() logger.info(u'request_data的keys值: %s' % str(keys)) if keys is not None: template_vm_id = request_data.get('template_vm_id') vm_name = request_data.get('vm_name') vm_address = request_data.get('vm_address') vm_subnetmask = request_data.get('vm_subnetmask') vm_gateway = request_data.get('vm_gateway') dns = request_data.get('dns') if vm_name is None : # 虚拟机的信息不全无法进行操作 logger.info(u'vm_name的值: %s' % vm_name) res = { 'code': '60031', 'message': u"调整配置失败,获取虚拟机名称失败", } return render_json(res) if vm_address is None or vm_subnetmask is None or vm_gateway is None or dns is None: logger.info(u'配置参数不足') res = { 'code': '60032', 'message': u"调整配置失败,虚拟机配置信息有误", } return render_json(res) # 根据vmid查询vmid在vmware中的具体信息 vcenterVirtualMachineModel = VcenterVirtualMachine.objects.get(id=int(template_vm_id)) accountModel = vcenterVirtualMachineModel.account if accountModel is None: logger.info(u'通过虚拟机模板获取vcenter失败') res = { 'code': '60033', 'message': u"调整配置失败,虚拟机模板账号错误", } return render_json(res) vmManager = VmManage(host=accountModel.vcenter_host, user=accountModel.account_name,password=accountModel.account_password, port=accountModel.vcenter_port, ssl=None) template_vm = vmManager.get_vm_by_name(vm_name) vmstatus = template_vm.summary.runtime.powerState if template_vm is not None: if vmstatus == 'poweredOn': logger.info(u'虚拟机没有关闭,无法操作') # 关闭虚拟机 res = { 'code':'60034', 'message':u'虚拟机没有关机,请先关机' } return render_json(res) # 获取dns dsn_list = str(dns).split('-') dsn_list_value = [] for lst in dsn_list: if lst is not None and lst != '': dsn_list_value.append(lst) logging.info(u'获取dsn_list:%s' % str(len(dsn_list_value))) if len(dsn_list_value) == 0: logging.info(u'dsn为空') res = { 'code': '60035', 'message': u'dns至少填一个' } return render_json(res) # 调整虚拟机的配置 adaptermap = vim.vm.customization.AdapterMapping() # Network adapter settings adaptermap.adapter = vim.vm.customization.IPSettings( ip=vim.vm.customization.FixedIp(ipAddress=vm_address), subnetMask=vm_subnetmask, gateway=vm_gateway) # IP globalip = vim.vm.customization.GlobalIPSettings(dnsServerList=dsn_list_value) ident = vim.vm.customization.LinuxPrep(domain='domain.local', hostName=vim.vm.customization.FixedName( name=vm_name)) # Putting all these pieces together in a custom spec customspec = vim.vm.customization.Specification(nicSettingMap=[adaptermap], globalIPSettings=globalip, identity=ident) task = template_vm.Customize(spec=customspec) result = vmManager.wait_for_task(task) Disconnect(vmManager) # 同步数据 sync_virtualmachine.delay(template_vm_id, vm_name) if result : logger.info(u'更新虚拟机配置成功 ') res = { 'code':'0', 'message':u'更新虚拟机配置成功' } return render_json(res) else: logger.info(u'更新虚拟机配置失败') res = { 'code':'60037', 'message':u'更新虚拟机配置失败' } return render_json(res) else: logger.info(u'传入参数不足 ') res = { 'code': '60036', 'message': u"调整配置失败,虚拟机信息不存在", } return render_json(res) except Exception as e: logger.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)
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 sync_virtualmachine(templateid, name): print u'start updata' template_vm_list = VcenterVirtualMachine.objects.filter(id=templateid) if len(template_vm_list) == 0: print 'tempalte_vm_list' pass else: vcenterVirtualMachineModel = VcenterVirtualMachine.objects.get( id=int(templateid)) accountModel = vcenterVirtualMachineModel.account if accountModel is None: print 'accountModel' pass else: vmManager = VmManage(host=accountModel.vcenter_host, user=accountModel.account_name, password=accountModel.account_password, port=accountModel.vcenter_port, ssl=None) vm = vmManager.get_vm_by_name(name) # 获取virtualMachine信息 print vm summary = vm.summary vcenterVirtualMachineModelList = VcenterVirtualMachine.objects.filter( instance_uuid=summary.config.instanceUuid) if len(vcenterVirtualMachineModelList) == 0: vcenterVirtualMachineModel = VcenterVirtualMachine() else: vcenterVirtualMachineModel = vcenterVirtualMachineModelList[0] vcenterVirtualMachineModel.name = summary.config.name vcenterVirtualMachineModel.vm_pathname = summary.config.vmPathName vcenterVirtualMachineModel.guest_fullname = summary.config.guestFullName vcenterVirtualMachineModel.power_state = summary.runtime.powerState vcenterVirtualMachineModel.instance_uuid = summary.config.instanceUuid vcenterVirtualMachineModel.memorySizeMB = summary.config.memorySizeMB print summary.config.name print summary.runtime.powerState print summary.config.vmPathName print summary.config.instanceUuid print summary.config.memorySizeMB if summary.runtime.maxCpuUsage is not None: vcenterVirtualMachineModel.maxCpuUsage = summary.runtime.maxCpuUsage else: vcenterVirtualMachineModel.maxCpuUsage = 0 if summary.runtime.maxMemoryUsage is not None: vcenterVirtualMachineModel.maxMemoryUsage = summary.runtime.maxMemoryUsage else: vcenterVirtualMachineModel.maxMemoryUsage = 0 vcenterVirtualMachineModel.guestId = summary.config.guestId vcenterVirtualMachineModel.numCpu = summary.config.numCpu vcenterVirtualMachineModel.numEthernetCards = summary.config.numEthernetCards vcenterVirtualMachineModel.numVirtualDisks = summary.config.numVirtualDisks vcenterVirtualMachineModel.overallStatus = summary.overallStatus vcenterVirtualMachineModel.storage_committed = summary.storage.committed vcenterVirtualMachineModel.storage_unshared = summary.storage.unshared vcenterVirtualMachineModel.storage_uncommitted = summary.storage.uncommitted if summary.config.template == True: vcenterVirtualMachineModel.template = True else: vcenterVirtualMachineModel.template = False if summary.guest != None: ip = summary.guest.ipAddress print ip if ip != None and ip != "": vcenterVirtualMachineModel.ipaddress = ip else: vcenterVirtualMachineModel.ipaddress = "" vcenterVirtualMachineModel.save() Disconnect(vmManager)