def _get_vm_and_vmdk_attribs(): # Get the vmdk file name that the VM is pointing to hardware_devices = self._session._call_method( vim_util, "get_dynamic_property", vm_ref, "VirtualMachine", "config.hardware.device" ) _vmdk_info = vm_util.get_vmdk_file_path_and_adapter_type(client_factory, hardware_devices) vmdk_file_path_before_snapshot, adapter_type = _vmdk_info datastore_name = vm_util.split_datastore_path(vmdk_file_path_before_snapshot)[0] os_type = self._session._call_method( vim_util, "get_dynamic_property", vm_ref, "VirtualMachine", "summary.config.guestId" ) return (vmdk_file_path_before_snapshot, adapter_type, datastore_name, os_type)
def _get_vm_and_vmdk_attribs(): # Get the vmdk file name that the VM is pointing to hardware_devices = self._session._call_method(vim_util, "get_dynamic_property", vm_ref, "VirtualMachine", "config.hardware.device") _vmdk_info = vm_util.get_vmdk_file_path_and_adapter_type( client_factory, hardware_devices) vmdk_file_path_before_snapshot, adapter_type = _vmdk_info datastore_name = vm_util.split_datastore_path( vmdk_file_path_before_snapshot)[0] os_type = self._session._call_method(vim_util, "get_dynamic_property", vm_ref, "VirtualMachine", "summary.config.guestId") return (vmdk_file_path_before_snapshot, adapter_type, datastore_name, os_type)
def destroy(self, instance, network_info): """ Destroy a VM instance. Steps followed are: 1. Power off the VM, if it is in poweredOn state. 2. Un-register a VM. 3. Delete the contents of the folder holding the VM related data. """ try: vm_ref = self._get_vm_ref_from_the_name(instance.name) if vm_ref is None: LOG.debug(_("instance - %s not present") % instance.name) return lst_properties = ["config.files.vmPathName", "runtime.powerState"] props = self._session._call_method(vim_util, "get_object_properties", None, vm_ref, "VirtualMachine", lst_properties) pwr_state = None for elem in props: vm_config_pathname = None for prop in elem.propSet: if prop.name == "runtime.powerState": pwr_state = prop.val elif prop.name == "config.files.vmPathName": vm_config_pathname = prop.val if vm_config_pathname: datastore_name, vmx_file_path = \ vm_util.split_datastore_path(vm_config_pathname) # Power off the VM if it is in PoweredOn state. if pwr_state == "poweredOn": LOG.debug(_("Powering off the VM %s") % instance.name) poweroff_task = self._session._call_method( self._session._get_vim(), "PowerOffVM_Task", vm_ref) self._session._wait_for_task(instance.id, poweroff_task) LOG.debug(_("Powered off the VM %s") % instance.name) # Un-register the VM try: LOG.debug(_("Unregistering the VM %s") % instance.name) self._session._call_method(self._session._get_vim(), "UnregisterVM", vm_ref) LOG.debug(_("Unregistered the VM %s") % instance.name) except Exception, excep: LOG.warn(_("In vmwareapi:vmops:destroy, got this exception" " while un-registering the VM: %s") % str(excep)) self._unplug_vifs(instance, network_info) # Delete the folder holding the VM related content on # the datastore. try: dir_ds_compliant_path = vm_util.build_datastore_path( datastore_name, os.path.dirname(vmx_file_path)) LOG.debug(_("Deleting contents of the VM %(name)s from " "datastore %(datastore_name)s") % ({'name': instance.name, 'datastore_name': datastore_name})) delete_task = self._session._call_method( self._session._get_vim(), "DeleteDatastoreFile_Task", self._session._get_vim().get_service_content().fileManager, name=dir_ds_compliant_path) self._session._wait_for_task(instance.id, delete_task) LOG.debug(_("Deleted contents of the VM %(name)s from " "datastore %(datastore_name)s") % ({'name': instance.name, 'datastore_name': datastore_name})) except Exception, excep: LOG.warn(_("In vmwareapi:vmops:destroy, " "got this exception while deleting" " the VM contents from the disk: %s") % str(excep))
def destroy(self, instance, network_info): """ Destroy a VM instance. Steps followed are: 1. Power off the VM, if it is in poweredOn state. 2. Un-register a VM. 3. Delete the contents of the folder holding the VM related data. """ try: vm_ref = self._get_vm_ref_from_the_name(instance.name) if vm_ref is None: LOG.debug(_("instance - %s not present") % instance.name) return lst_properties = ["config.files.vmPathName", "runtime.powerState"] props = self._session._call_method(vim_util, "get_object_properties", None, vm_ref, "VirtualMachine", lst_properties) pwr_state = None for elem in props: vm_config_pathname = None for prop in elem.propSet: if prop.name == "runtime.powerState": pwr_state = prop.val elif prop.name == "config.files.vmPathName": vm_config_pathname = prop.val if vm_config_pathname: datastore_name, vmx_file_path = \ vm_util.split_datastore_path(vm_config_pathname) # Power off the VM if it is in PoweredOn state. if pwr_state == "poweredOn": LOG.debug(_("Powering off the VM %s") % instance.name) poweroff_task = self._session._call_method( self._session._get_vim(), "PowerOffVM_Task", vm_ref) self._session._wait_for_task(instance.id, poweroff_task) LOG.debug(_("Powered off the VM %s") % instance.name) # Un-register the VM try: LOG.debug(_("Unregistering the VM %s") % instance.name) self._session._call_method(self._session._get_vim(), "UnregisterVM", vm_ref) LOG.debug(_("Unregistered the VM %s") % instance.name) except Exception, excep: LOG.warn( _("In vmwareapi:vmops:destroy, got this exception" " while un-registering the VM: %s") % str(excep)) self._unplug_vifs(instance, network_info) # Delete the folder holding the VM related content on # the datastore. try: dir_ds_compliant_path = vm_util.build_datastore_path( datastore_name, os.path.dirname(vmx_file_path)) LOG.debug( _("Deleting contents of the VM %(name)s from " "datastore %(datastore_name)s") % ({ 'name': instance.name, 'datastore_name': datastore_name })) delete_task = self._session._call_method( self._session._get_vim(), "DeleteDatastoreFile_Task", self._session._get_vim().get_service_content().fileManager, name=dir_ds_compliant_path) self._session._wait_for_task(instance.id, delete_task) LOG.debug( _("Deleted contents of the VM %(name)s from " "datastore %(datastore_name)s") % ({ 'name': instance.name, 'datastore_name': datastore_name })) except Exception, excep: LOG.warn( _("In vmwareapi:vmops:destroy, " "got this exception while deleting" " the VM contents from the disk: %s") % str(excep))