def create_snapshot(self, name, description="", memory=False, quiesce=True): task = self.vmobj.CreateSnapshot_Task(name=name, description=description, memory=memory, quiesce=quiesce) result = util.wait_for_tasks(self.server.service_instance, [task]) return VirtualMachineSnapshot(self.server, self.vmobj, name)
def create_dummy_vm(self, vmname, datastore, datacentername, hostname, memory, cpus): content = self.service_instance.RetrieveContent() datacenters = content.rootFolder.childEntity datacenter = None for d in datacenters: if d.name == datacentername: datacenter = d if datacenter == None: raise Exception('Datacenter {} not found.'.format(datacentername)) vmfolder = datacenter.vmFolder resource_pool = None hosts = datacenter.hostFolder.childEntity for host in hosts: if host.name == hostname: resource_pool = host.resourcePool if resource_pool == None: raise Exception('Host {} not found.'.format(hostname)) datastore_path = '[' + datastore + ']' + vmname vmx_file = vim.vm.FileInfo(logDirectory=None, snapshotDirectory=None, suspendDirectory=None, vmPathName=datastore_path) config = vim.vm.ConfigSpec(name=vmname, memoryMB=int(memory), numCPUs=int(cpus), files=vmx_file, guestId='dosGuest', version='vmx-07') task = vmfolder.CreateVM_Task(config=config, pool=resource_pool) result = util.wait_for_tasks(self.service_instance, [task]) vmresult = result[task.info.key].info.result uuid = result[task.info.key].info.result.config.uuid util.add_scsi_controller(self.service_instance, vmresult) vm_obj = VirtualMachine(self, vmobj=vmresult) vm_obj.add_disk(size_gb=2) return vm_obj
def change_name(self, newname): spec = vim.vm.ConfigSpec() spec.name = newname task = self.vmobj.ReconfigVM_Task(spec=spec) util.wait_for_tasks(self.server.service_instance, [task])
def poweroff(self): task = self.vmobj.PowerOffVM_Task() util.wait_for_tasks(self.server.service_instance, [task])
def register(self, vmxpath, name=None): task = self.parent_folder.RegisterVM_Task(path=vmxpath, name=name, asTemplate=False, pool=self.vmobj.resourcePool) util.wait_for_tasks(self.server.service_instance, [task])
def disable_cbt(self): config_spec = vim.vm.ConfigSpec() config_spec.changeTrackingEnabled = False util.wait_for_tasks(self.server.service_instance, [self.vmobj.Reconfigure(config_spec)])
def delete_all_snapshots(self): task = self.vmobj.RemoveAllSnapshots_Task() result = util.wait_for_tasks(self.server.service_instance, [task])
def delete(self, remove_children=False): task = self.snapobj.RemoveSnapshot_Task(removeChildren=remove_children) result = util.wait_for_tasks(self.server.service_instance, [task])
def create_vm_from_template(self, vmname, template_name, datastore_name, datacenter_name, hostname): content = self.service_instance.RetrieveContent() template = None datastore = None template = self.get_obj(content, [vim.VirtualMachine], template_name) if template: datacenters = content.rootFolder.childEntity datacenter = None datastore = None for d in datacenters: if d.name == datacenter_name: datacenter = d datastores_object_view = content.viewManager.CreateContainerView( datacenter, [vim.Datastore], True) for ds in datastores_object_view.view: if ds.info.name == datastore_name: datastore = ds datastores_object_view.Destroy() print(datacenter.name) print(datastore.name) # if None, get the first one resource_pool = None hosts = datacenter.hostFolder.childEntity for host in hosts: if host.name == hostname: resource_pool = host.resourcePool if resource_pool == None: raise Exception('Host {} not found.') vmconf = vim.vm.ConfigSpec() # set relospec relospec = vim.vm.RelocateSpec() relospec.datastore = datastore relospec.pool = resource_pool clonespec = vim.vm.CloneSpec() clonespec.location = relospec clonespec.powerOn = True vmfolder = datacenter.vmFolder print("cloning VM...") task = template.Clone(folder=vmfolder, name=vmname, spec=clonespec) result = util.wait_for_tasks(self.service_instance, [task]) vmresult = result[task.info.key].info.result uuid = result[task.info.key].info.result.config.uuid print(uuid) # wait for OS to come up and get guestOS IP. count = 0 ip = None while (not ip): try: data = VirtualMachine(self, identity={'uuid': uuid}).info() ip = data['ip'] time.sleep(30) count += 1 if count > 10: break except: pass else: print("Template not found!") print(ip) return ip, uuid
def delete_vm(self, identity): vm = util.find_vmobj(service_instance=self.service_instance, identity=identity) task = vm.Destroy_Task() util.wait_for_tasks(self.service_instance, [task])