def main(): args = get_args() try: if is_xenial_or_above(): ssl = __import__("ssl") context = ssl._create_unverified_context() si = connect.SmartConnect(host=args.host, user=args.user, pwd=args.password, port=args.port, sslContext=context) else: si = connect.SmartConnect(host=args.host, user=args.user, pwd=args.password, port=args.port) si_content = si.RetrieveContent() except: print "Unable to connect to %s" % args.host exit(1) # get VM object vm_obj = get_obj(si_content, [vim.VirtualMachine], args.vm_name) if not vm_obj: print "VM %s not pressent" %(args.vm_name) exit(1) if args.pci_nics: task = add_pci_nics(args, vm_obj) wait_for_task(task) if args.sriov_nics: task = add_sriov_nics(args, vm_obj, si_content) wait_for_task(task) connect.Disconnect(si)
def main(): args = get_args() try: if is_xenial_or_above(): ssl = __import__("ssl") context = ssl._create_unverified_context() si = connect.SmartConnect(host=args.host, user=args.user, pwd=args.password, port=args.port, sslContext=context) else: si = connect.SmartConnect(host=args.host, user=args.user, pwd=args.password, port=args.port) si_content = si.RetrieveContent() except: print "Unable to connect to %s" % args.host exit(1) # get VM object vm_obj = get_obj(si_content, [vim.VirtualMachine], args.vm_name) if not vm_obj: print "VM %s not pressent" %(args.vm_name) exit(1) #net_obj = get_obj(si_content, [vim.Network], args.network_name) nics = args.nics.split(',') for nic in nics: if not nic: continue task = update_mac(nic, vm_obj, si_content) wait_for_task(task) connect.Disconnect(si)
def main(): args = get_args() try: if is_xenial_or_above(): ssl = __import__("ssl") context = ssl._create_unverified_context() si = connect.SmartConnect(host=args.host, user=args.user, pwd=args.password, port=args.port, sslContext=context) else: si = connect.SmartConnect(host=args.host, user=args.user, pwd=args.password, port=args.port) si_content = si.RetrieveContent() except: print "Unable to connect to %s" % args.host exit(1) # get VM object vm_obj = get_obj(si_content, [vim.VirtualMachine], args.vm_name) if not vm_obj: print "VM %s not pressent" % (args.vm_name) exit(1) if args.pci_nics: task = add_pci_nics(args, vm_obj) wait_for_task(task) if args.sriov_nics: task = add_sriov_nics(args, vm_obj, si_content) wait_for_task(task) connect.Disconnect(si)
def auto_start_vm(si, args, vm_obj): si_content = si.RetrieveContent() objs = get_objects(si, args) host = objs['host_obj'] vm_obj = get_obj(si_content, [vim.VirtualMachine], args.vm_name) host_settings = vim.host.AutoStartManager.SystemDefaults() host_settings.enabled = True config = host.configManager.autoStartManager.config config.defaults = host_settings auto_power_info = vim.host.AutoStartManager.AutoPowerInfo() auto_power_info.key = vm_obj auto_power_info.startOrder = 1 auto_power_info.startAction = "powerOn" auto_power_info.startDelay = -1 auto_power_info.stopAction = "powerOff" auto_power_info.stopDelay = -1 auto_power_info.waitForHeartbeat = 'no' config.powerInfo = [auto_power_info] host.configManager.autoStartManager.ReconfigureAutostart(config)
def add_sriov_nics(args, vm, si_content): dvs = get_obj(si_content, [vim.DistributedVirtualSwitch], args.sriov_dvs) fix_sriov_pg(si_content, dvs, args.sriov_dvs_pg) sr_iov_nic_list = args.sriov_nics if vm.runtime.powerState == vim.VirtualMachinePowerState.poweredOn: print "VM:%s is powered ON. Cannot do hot pci add now. Shutting it down" %(args.vm_name) poweroff(vm) # get pci id of the sriov nic ssh_handle = paramiko.SSHClient() ssh_handle.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh_handle.connect(args.esxi_host, username = args.esxi_user, password = args.esxi_password) cmd = "vmware -v" stdin, stdout, stderr = ssh_handle.exec_command(cmd) err = stderr.read() op = stdout.read() if err: self.log_and_raise_exception(err) esxi_version = op.split()[2][:3] for sr_iov_nic in sr_iov_nic_list: cmd = "vmkchdev -l | grep %s" %sr_iov_nic stdin, stdout, stderr = ssh_handle.exec_command(cmd) err = stderr.read() op = stdout.read() if err: self.log_and_raise_exception(err) nic_info = str(op) if len(nic_info) == 0: raise Exception("Unable to add sriov interface for physical nic %s \ on esxi host %s" %(sr_iov_nic, args.esxi_host)) pci_id = nic_info.split()[0] if (esxi_version == '5.5'): pci_id = pci_id[5:] mac_address = None devices = [] nicspec = vim.vm.device.VirtualDeviceSpec() nicspec.device = vim.vm.device.VirtualSriovEthernetCard() nicspec.operation = vim.vm.device.VirtualDeviceSpec.Operation.add nicspec.device.wakeOnLanEnabled = True nicspec.device.allowGuestOSMtuChange = True nicspec.device.deviceInfo = vim.Description() pg_obj = get_obj([vim.dvs.DistributedVirtualPortgroup], args.sriov_dvs_pg) dvs_port_connection = vim.dvs.PortConnection() dvs_port_connection.portgroupKey = pg_obj.key dvs_port_connection.switchUuid = pg_obj.config.distributedVirtualSwitch.uuid nicspec.device.backing = vim.vm.device.VirtualEthernetCard.DistributedVirtualPortBackingInfo() nicspec.device.backing.port = dvs_port_connection nicspec.device.sriovBacking = vim.vm.device.VirtualSriovEthernetCard.SriovBackingInfo() nicspec.device.sriovBacking.physicalFunctionBacking = vim.vm.device.VirtualPCIPassthrough.DeviceBackingInfo() nicspec.device.sriovBacking.physicalFunctionBacking.id = pci_id if (mac_address): nicspec.device.addressType = "Manual" nicspec.device.macAddress = mac_address devices.append(nicspec) vmconf = vim.vm.ConfigSpec(deviceChange=devices) task = vm.ReconfigVM_Task(vmconf) wait_for_task(task) if not mac_address: for device in vm.config.hardware.device: if isinstance(device, vim.vm.device.VirtualSriovEthernetCard): devices = [] mac_address = device.macAddress nicspec = vim.vm.device.VirtualDeviceSpec() nicspec.operation = vim.vm.device.VirtualDeviceSpec.Operation.edit nicspec.device = device nicspec.device.addressType = "Manual" nicspec.device.macAddress = mac_address devices.append(nicspec) vmconf = vim.vm.ConfigSpec(deviceChange=devices) task = vm.ReconfigVM_Task(vmconf) wait_for_task(task)
def main(): args = get_args() t = tarfile.open(args.ova_path) ovffilename = list(filter(lambda x: x.endswith(".ovf"), t.getnames()))[0] ovffile = t.extractfile(ovffilename) ovfd = get_ovf_descriptor(ovffile) ovffile.close() try: ssl = __import__("ssl") context = ssl._create_unverified_context() si = connect.SmartConnect(host=args.host, user=args.user, pwd=args.password, port=args.port, sslContext=context) except Exception as e: si = connect.SmartConnect(host=args.host, user=args.user, pwd=args.password, port=args.port) except: print "Unable to connect to %s" % args.host exit(1) objs = get_objects(si, args) # if VM already exists exit right away si_content = si.RetrieveContent() vm_obj = get_obj(si_content, [vim.VirtualMachine], args.vm_name) if vm_obj: print "vm %s already exists" % args.vm_name exit(0) manager = si.content.ovfManager spec_params = vim.OvfManager.CreateImportSpecParams() # update spec_params to include host and name of the VM and possibly nics spec_params.hostSystem = objs['host_obj'] spec_params.diskProvisioning = "thick" spec_params.entityName = args.vm_name import_spec = manager.CreateImportSpec(ovfd, objs["resource pool"], objs["datastore"], spec_params) lease = objs["resource pool"].ImportVApp(import_spec.importSpec, objs["datacenter"].vmFolder, objs['host_obj']) while (True): if lease.state == vim.HttpNfcLease.State.ready: # Spawn a thread to keep the lease active while POSTing # VMDK. keepalive_thread = Thread(target=keep_lease_alive, args=(lease, )) keepalive_thread.daemon = True keepalive_thread.start() try: for deviceUrl in lease.info.deviceUrl: url = deviceUrl.url.replace('*', args.host) fileItem = list( filter(lambda x: x.deviceId == deviceUrl.importKey, import_spec.fileItem))[0] ovffilename = list( filter(lambda x: x == fileItem.path, t.getnames()))[0] ovffile = t.extractfile(ovffilename) headers = {'Content-length': ovffile.size} req = urllib2.Request(url, ovffile, headers) try: response = urllib2.urlopen(req, context=context) except: response = urllib2.urlopen(req) lease.HttpNfcLeaseComplete() except: raise keepalive_thread.join() auto_start_vm(si, args, vm_obj) connect.Disconnect(si) return 0 elif lease.state == vim.HttpNfcLease.State.error: print "Lease error: %s" % lease.error connect.Disconnect(si) exit(1)
def add_sriov_nics(args, vm, si_content): dvs = get_obj(si_content, [vim.DistributedVirtualSwitch], args.sriov_dvs) fix_sriov_pg(si_content, dvs, args.sriov_dvs_pg) sr_iov_nic_list = args.sriov_nics if vm.runtime.powerState == vim.VirtualMachinePowerState.poweredOn: print "VM:%s is powered ON. Cannot do hot pci add now. Shutting it down" % ( args.vm_name) poweroff(vm) # get pci id of the sriov nic ssh_handle = paramiko.SSHClient() ssh_handle.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh_handle.connect(args.esxi_host, username=args.esxi_user, password=args.esxi_password) cmd = "vmware -v" stdin, stdout, stderr = ssh_handle.exec_command(cmd) err = stderr.read() op = stdout.read() if err: self.log_and_raise_exception(err) esxi_version = op.split()[2][:3] for sr_iov_nic in sr_iov_nic_list: cmd = "vmkchdev -l | grep %s" % sr_iov_nic stdin, stdout, stderr = ssh_handle.exec_command(cmd) err = stderr.read() op = stdout.read() if err: self.log_and_raise_exception(err) nic_info = str(op) if len(nic_info) == 0: raise Exception( "Unable to add sriov interface for physical nic %s \ on esxi host %s" % (sr_iov_nic, args.esxi_host)) pci_id = nic_info.split()[0] if (esxi_version == '5.5'): pci_id = pci_id[5:] mac_address = None devices = [] nicspec = vim.vm.device.VirtualDeviceSpec() nicspec.device = vim.vm.device.VirtualSriovEthernetCard() nicspec.operation = vim.vm.device.VirtualDeviceSpec.Operation.add nicspec.device.wakeOnLanEnabled = True nicspec.device.allowGuestOSMtuChange = True nicspec.device.deviceInfo = vim.Description() pg_obj = get_obj([vim.dvs.DistributedVirtualPortgroup], args.sriov_dvs_pg) dvs_port_connection = vim.dvs.PortConnection() dvs_port_connection.portgroupKey = pg_obj.key dvs_port_connection.switchUuid = pg_obj.config.distributedVirtualSwitch.uuid nicspec.device.backing = vim.vm.device.VirtualEthernetCard.DistributedVirtualPortBackingInfo( ) nicspec.device.backing.port = dvs_port_connection nicspec.device.sriovBacking = vim.vm.device.VirtualSriovEthernetCard.SriovBackingInfo( ) nicspec.device.sriovBacking.physicalFunctionBacking = vim.vm.device.VirtualPCIPassthrough.DeviceBackingInfo( ) nicspec.device.sriovBacking.physicalFunctionBacking.id = pci_id if (mac_address): nicspec.device.addressType = "Manual" nicspec.device.macAddress = mac_address devices.append(nicspec) vmconf = vim.vm.ConfigSpec(deviceChange=devices) task = vm.ReconfigVM_Task(vmconf) wait_for_task(task) if not mac_address: for device in vm.config.hardware.device: if isinstance(device, vim.vm.device.VirtualSriovEthernetCard): devices = [] mac_address = device.macAddress nicspec = vim.vm.device.VirtualDeviceSpec() nicspec.operation = vim.vm.device.VirtualDeviceSpec.Operation.edit nicspec.device = device nicspec.device.addressType = "Manual" nicspec.device.macAddress = mac_address devices.append(nicspec) vmconf = vim.vm.ConfigSpec(deviceChange=devices) task = vm.ReconfigVM_Task(vmconf) wait_for_task(task)