def destroy(self): """ Destroy the virtual machine and set the vm object to None """ self.power_off() if self._vm_object: wait_for_vcenter_task( self._vm_object.Destroy_Task(), 'Destroy virtual machine "{}"'.format(self.name), self.timeout) self._vm_object = None
def revert_snapshot(self, name): """ Revert to a snapshot of the virtual machine :param name: The name of the snapshot to revert to """ if self._vm_object: wait_for_vcenter_task( self.find_snapshot(name).RevertToSnapshot_Task(), 'Restoring snapshot "{}" on "{}"'.format(name, self.name), self.timeout)
def reset(self): """ Reset the virtual machine """ if self._vm_object: try: wait_for_vcenter_task( self._vm_object.ResetVM_Task(), 'Reset virtual machine "{}"'.format(self.name), self.timeout) except vim.fault.InvalidPowerState: pass
def power_off(self): """ Power off the virtual machine """ if self._vm_object: try: wait_for_vcenter_task( self._vm_object.PowerOffVM_Task(), 'Power off virtual machine "{}"'.format(self.name), self.timeout) except vim.fault.InvalidPowerState: pass
def remove_snapshot(self, name, remove_children=False): """ Delete a snapshot from the virtual machine :param name: The name of the snapshot to delete :param remove_children: Whether to remove the children snapshots or not """ if self._vm_object: wait_for_vcenter_task( self.find_snapshot(name).RemoveSnapshot_Task(remove_children), 'Delete snapshot "{}" from "{}"'.format(name, self.name), self.timeout)
def create(self, **kwargs): """ Create the virtual machine and update the vm object """ conn = connection() if not self._vm_object: data_store_name = kwargs['vcdriver_data_store'] data_store = get_vcenter_object_by_name(conn, vim.Datastore, data_store_name) capacity = float(data_store.summary.capacity) free_space = float(data_store.summary.freeSpace) free_percentage = 100 * free_space / capacity threshold = kwargs['vcdriver_data_store_threshold'] if free_percentage < float(threshold): raise NotEnoughDiskSpace(data_store_name, threshold, free_percentage) self._vm_object = wait_for_vcenter_task( get_vcenter_object_by_name( conn, vim.VirtualMachine, self.template).CloneVM_Task( folder=get_vcenter_object_by_name( conn, vim.Folder, kwargs['vcdriver_folder']), name=self.name, spec=vim.vm.CloneSpec(location=vim.vm.RelocateSpec( datastore=data_store, pool=get_vcenter_object_by_name( conn, vim.ResourcePool, kwargs['vcdriver_resource_pool'])), powerOn=True, template=False)), 'Create virtual machine "{}" from template "{}"'.format( self.name, self.template), self.timeout)
def test_wait_for_vcenter_task_wait_for_success(): task = mock.Mock(vim.Task) class TaskInfoTimeline: def __init__(self, states, result): self.result = result self._state_iter = iter(states) @property def state(self): return next(self._state_iter) task.info = TaskInfoTimeline( result='hello', states=( vim.TaskInfo.State.queued, vim.TaskInfo.State.running, # Need success twice (ATM) since it is looked up again after poll # loop vim.TaskInfo.State.success, vim.TaskInfo.State.success)) assert wait_for_vcenter_task(task, 'description', timeout=2, _poll_interval=0) == 'hello' with pytest.raises(StopIteration): task.info.state
def create_snapshot(self, name, dump_memory, description=''): """ Create a snapshot of the virtual machine :param name: The name of the snapshot to create :param dump_memory: Whether to dump the memory of the vm :param description: A description of the snapshot """ if self._vm_object: try: self.find_snapshot(name) except NoObjectFound: wait_for_vcenter_task( self._vm_object.CreateSnapshot(name, description, dump_memory, False), 'Creating snapshot "{}" on "{}"'.format(name, self.name), self.timeout) else: raise TooManyObjectsFound(vim.vm.Snapshot, name)
def power_off(self, delay_by=None): """ Power off the virtual machine :param delay_by: if specified, it has to be a timedelta that indicates when in the future this task will be executed. """ if self._vm_object: if delay_by is None: try: wait_for_vcenter_task( self._vm_object.PowerOffVM_Task(), 'Power off virtual machine "{}"'.format(self.name), self.timeout) except vim.fault.InvalidPowerState: pass else: self._schedule_vcenter_task_on_vm( vim.VirtualMachine.PowerOff, # task name can't be longer than 80 chars and as to be unique 'Power off {}'.format(self.vm_id()), delay_by)
def test_wait_for_vcenter_task_timeout(): task = mock.MagicMock() task.info.state = vim.TaskInfo.State.running with pytest.raises(TimeoutError): wait_for_vcenter_task(task, 'description', timeout=1)
def test_wait_for_vcenter_task_fail_no_exception(): task = mock.MagicMock() task.info.state = vim.TaskInfo.State.error task.info.error = None wait_for_vcenter_task(task, 'description', timeout=1)
def test_wait_for_vcenter_task_fail(): task = mock.MagicMock() task.info.state = vim.TaskInfo.State.error task.info.error = Exception with pytest.raises(Exception): wait_for_vcenter_task(task, 'description', timeout=1)