Esempio n. 1
0
    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_resource = self.get_resource(vm_id)
        datastores = self._ds_manager.get_datastores()
        ds_type = [
            ds.type for ds in datastores if ds.id == vm_resource.datastore
        ][0]

        vm_dir = self.vim_client.delete_vm(vm_id, force)

        # Path of vm directory is different in vsanDatastore
        # In VSAN every entity(image,vm,disk) is associated with id
        # and all the files for that entity are under the id folder.
        # eg. vm dir in vsan :- /vmfs/volumes/vsanDatastore/8989/
        # vm dir in vmfs datastore :- /vmfs/volumes/datastore1/vm_1234/1234/
        if ds_type == DatastoreType.VSAN:
            vm_dir = datastore_to_os_path(vm_dir)
        else:
            # Getting parent directory for vm :- /vmfs/volumes/datastore1/vm_1234/
            vm_dir = os.path.dirname(datastore_to_os_path(vm_dir))
        # Upon successful destroy of VM, log any stray files still left in the
        # VM directory and delete the directory.
        if os.path.isdir(vm_dir):
            # log any stray files still left in the VM directory
            try:
                target_dir = vm_dir
                if os.path.islink(vm_dir):
                    target_dir = os.readlink(vm_dir)
                if os.path.isdir(
                        target_dir):  # check link-target exists and is dir
                    files = os.listdir(target_dir)
                    for f in files:
                        if f.endswith(".vmdk"):
                            self._logger.info(
                                "Stray disk (possible data leak): %s" % f)
                        else:
                            self._logger.info("Stray file: %s" % f)
            except:
                pass

            # delete the directory
            self._logger.warning("Force delete vm directory %s" % vm_dir)
            self.vim_client.delete_file(vm_dir)
    def create_image_with_vm_disk(self, datastore_id, tmp_dir, image_id,
                                  vm_disk_os_path):
        """ Fills a temp image directory with a disk from a VM,
            then installs directory in the shared image folder.
        """
        # Create parent directory as required by CopyVirtualDisk_Task
        dst_vmdk_path = os.path.join(datastore_to_os_path(tmp_dir), "%s.vmdk" % image_id)
        if os.path.exists(dst_vmdk_path):
            self._logger.warning("Unexpected disk %s present, overwriting" % dst_vmdk_path)

        self._host_client.copy_disk(vm_disk_os_path, dst_vmdk_path)

        try:
            self.finalize_image(datastore_id, tmp_dir, image_id)
        except:
            self._logger.warning("Delete copied disk %s" % dst_vmdk_path)
            self._host_client.delete_disk(dst_vmdk_path)
            raise
Esempio n. 3
0
    def create_image_with_vm_disk(self, datastore_id, tmp_dir, image_id,
                                  vm_disk_os_path):
        """ Fills a temp image directory with a disk from a VM,
            then installs directory in the shared image folder.
        """
        # Create parent directory as required by CopyVirtualDisk_Task
        dst_vmdk_path = os.path.join(datastore_to_os_path(tmp_dir), "%s.vmdk" % image_id)
        if os.path.exists(dst_vmdk_path):
            self._logger.warning("Unexpected disk %s present, overwriting" % dst_vmdk_path)

        self._host_client.copy_disk(vm_disk_os_path, dst_vmdk_path)

        try:
            self.finalize_image(datastore_id, tmp_dir, image_id)
        except:
            self._logger.warning("Delete copied disk %s" % dst_vmdk_path)
            self._host_client.delete_disk(dst_vmdk_path)
            raise
Esempio n. 4
0
        """ Disconnect cdrom device from VM

        :param vm_id: The id of VM to detach iso from
        :param delete_file: a boolean that indicates whether to delete the iso file
        :type vm_id: str
        """
        try:
            iso_path = self.vim_client.detach_iso(vm_id)
        except DeviceNotFoundException, e:
            raise IsoNotAttachedException(e)
        except TypeError, e:
            raise IsoNotAttachedException(e)

        if delete_file:
            try:
                os.remove(datastore_to_os_path(iso_path))
            except:
                # The iso may not exist, so just catch and move on.
                pass

    def attach_virtual_network(self, vm_id, network_id):
        """Attach virtual network to VM.

        :param vm_id: vm id
        :type vm_id: str
        :param network_id: virtual switch id
        :type network_id: str
        """
        return self.vim_client.attach_virtual_network(vm_id, network_id)

    @log_duration
Esempio n. 5
0
 def finalize_image(self, datastore_id, tmp_dir, image_id):
     """ Installs an image using image data staged at a temp directory.
     """
     self._move_image(image_id, datastore_id, datastore_to_os_path(tmp_dir))
     self._create_image_timestamp_file(self._image_directory(datastore_id, image_id))
 def finalize_image(self, datastore_id, tmp_dir, image_id):
     """ Installs an image using image data staged at a temp directory.
     """
     self._move_image(image_id, datastore_id, datastore_to_os_path(tmp_dir))
     self._create_image_timestamp_file(self._image_directory(datastore_id, image_id))
Esempio n. 7
0
 def unregister_vm(self, vm_id):
     vm = self.get_vm(vm_id)
     vm_dir = os.path.dirname(datastore_to_os_path(vm.config.files.vmPathName))
     vm.Unregister()
     return vm_dir
Esempio n. 8
0
 def unregister_vm(self, vm_id):
     vm = self.get_vm(vm_id)
     vm_dir = os.path.dirname(datastore_to_os_path(vm.config.files.vmPathName))
     vm.Unregister()
     return vm_dir
 def test_path_conversion(self, ds_path, expected_os_path):
     path = datastore_to_os_path(ds_path)
     assert_that(path, equal_to(expected_os_path))
Esempio n. 10
0
        """ Disconnect cdrom device from VM

        :param vm_id: The id of VM to detach iso from
        :param delete_file: a boolean that indicates whether to delete the iso file
        :type vm_id: str
        """
        try:
            iso_path = self.vim_client.detach_iso(vm_id)
        except DeviceNotFoundException, e:
            raise IsoNotAttachedException(e)
        except TypeError, e:
            raise IsoNotAttachedException(e)

        if delete_file:
            try:
                os.remove(datastore_to_os_path(iso_path))
            except:
                # The iso may not exist, so just catch and move on.
                pass

    def attach_virtual_network(self, vm_id, network_id):
        """Attach virtual network to VM.

        :param vm_id: vm id
        :type vm_id: str
        :param network_id: virtual switch id
        :type network_id: str
        """
        return self.vim_client.attach_virtual_network(vm_id, network_id)

    @log_duration
 def test_path_conversion(self, ds_path, expected_os_path):
     path = datastore_to_os_path(ds_path)
     assert_that(path, equal_to(expected_os_path))