def delete_vm(self, vm_id, force=False): """Delete a Virtual Machine :param vm_id: Name of the VM :type vm_id: str :param force: Not to check persistent disk, forcefully delete vm. :type force: boolean :raise VmPowerStateException when vm is not powered off """ vm = self.vim_client.get_vm(vm_id) if vm.runtime.powerState != 'poweredOff': raise VmPowerStateException("Can only delete vm in state %s" % vm.runtime.powerState) vm_ds_path = self.get_vm_path(vm.config) if not force: self._verify_disks(vm) self._logger.info("Destroy VM at %s" % vm_ds_path) self._invoke_vm(vm, "Destroy") self._ensure_directory_cleanup(vm_ds_path) self.vim_client.wait_for_vm_delete(vm_id)
def delete_vm(self, vm_id, force=False): """Delete a Virtual Machine :param vm_id: Name of the VM :type vm_id: str :param force: Not to check persistent disk, forcefully delete vm. :type force: boolean :raise VmPowerStateException when vm is not powered off """ vm = self.vim_client.get_vm(vm_id) if vm.runtime.powerState != 'poweredOff': raise VmPowerStateException("Can only delete vm in state %s" % vm.runtime.powerState) # Getting the path for the new dir structure if we have upgraded from older structure datastore_name = self.get_vm_datastore(vm.config) vm_path = os_datastore_path(datastore_name, compond_path_join(VM_FOLDER_NAME_PREFIX, vm_id)) if not force: self._verify_disks(vm) self._logger.info("Destroy VM at %s" % vm_path) self._invoke_vm(vm, "Destroy") self._ensure_directory_cleanup(vm_path) self.vim_client.wait_for_vm_delete(vm_id)
def _invoke_vm(self, vm, op, *args): try: self._logger.debug("Invoking '%s' for VM '%s'" % (op, vm.name)) task = getattr(vm, op)(*args) self.vim_client.wait_for_task(task) except vim.fault.InvalidPowerState, e: if e.existingState == self._vm_op_to_requested_state(op): self._logger.info("VM %s already in %s state, %s successful." % (vm.name, e.existingState, op)) pass else: raise VmPowerStateException(e.msg)
def delete_vm(self, vm_id, force=False): self._assert_exists(vm_id) vm = self._resources[vm_id] if vm.state != "off": raise VmPowerStateException() # fake_vm_spec.disks are all persistent disks. # TODO(agui): This looks hacky. FakeVm should have its own device # list, as opposed to play spec around. if not force and len(self._resources[vm_id].fake_vm_spec.disks) != 0: raise OperationNotAllowedException("persistent disks attached") fake_vm_spec = self._resources[vm_id].fake_vm_spec for disk_id in fake_vm_spec.vm_disks: self._disk_manager().delete_disk(fake_vm_spec.datastore, disk_id) del self._resources[vm_id] self._trigger_vm_listeners()
def _power_vm(self, vm_id, expected, state): vm = self._resources[vm_id] if vm.state != expected and vm.state != state: raise VmPowerStateException() vm.state = state