Search in sources :

Example 1 with VirtualMachineFileLayoutExFileInfo

use of com.vmware.vim25.VirtualMachineFileLayoutExFileInfo in project cloudstack by apache.

the class VmwareResource method deleteUnregisteredVmFiles.

private void deleteUnregisteredVmFiles(VirtualMachineFileLayoutEx vmFileLayout, DatacenterMO dcMo, boolean deleteDisks) throws Exception {
    s_logger.debug("Deleting files associated with an existing VM that was unregistered");
    DatastoreFile vmFolder = null;
    try {
        List<VirtualMachineFileLayoutExFileInfo> fileInfo = vmFileLayout.getFile();
        for (VirtualMachineFileLayoutExFileInfo file : fileInfo) {
            DatastoreFile fileInDatastore = new DatastoreFile(file.getName());
            // In case of linked clones, VM file layout includes the base disk so don't delete all disk files.
            if (file.getType().startsWith("disk") || file.getType().startsWith("digest"))
                continue;
            else if (file.getType().equals("config"))
                vmFolder = new DatastoreFile(fileInDatastore.getDatastoreName(), fileInDatastore.getDir());
            DatastoreMO dsMo = new DatastoreMO(dcMo.getContext(), dcMo.findDatastore(fileInDatastore.getDatastoreName()));
            s_logger.debug("Deleting file: " + file.getName());
            dsMo.deleteFile(file.getName(), dcMo.getMor(), true);
        }
        // Delete files that are present in the VM folder - this will take care of the VM disks as well.
        DatastoreMO vmFolderDsMo = new DatastoreMO(dcMo.getContext(), dcMo.findDatastore(vmFolder.getDatastoreName()));
        String[] files = vmFolderDsMo.listDirContent(vmFolder.getPath());
        if (deleteDisks) {
            for (String file : files) {
                String vmDiskFileFullPath = String.format("%s/%s", vmFolder.getPath(), file);
                s_logger.debug("Deleting file: " + vmDiskFileFullPath);
                vmFolderDsMo.deleteFile(vmDiskFileFullPath, dcMo.getMor(), true);
            }
        }
        // Delete VM folder
        if (deleteDisks || files.length == 0) {
            s_logger.debug("Deleting folder: " + vmFolder.getPath());
            vmFolderDsMo.deleteFolder(vmFolder.getPath(), dcMo.getMor());
        }
    } catch (Exception e) {
        String message = "Failed to delete files associated with an existing VM that was unregistered due to " + VmwareHelper.getExceptionMessage(e);
        s_logger.warn(message, e);
    }
}
Also used : DatastoreFile(com.cloud.hypervisor.vmware.mo.DatastoreFile) VirtualMachineFileLayoutExFileInfo(com.vmware.vim25.VirtualMachineFileLayoutExFileInfo) DatastoreMO(com.cloud.hypervisor.vmware.mo.DatastoreMO) ConnectException(java.net.ConnectException) IOException(java.io.IOException) RemoteException(java.rmi.RemoteException) InternalErrorException(com.cloud.exception.InternalErrorException) CloudException(com.cloud.exception.CloudException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ConfigurationException(javax.naming.ConfigurationException)

Aggregations

CloudException (com.cloud.exception.CloudException)1 InternalErrorException (com.cloud.exception.InternalErrorException)1 DatastoreFile (com.cloud.hypervisor.vmware.mo.DatastoreFile)1 DatastoreMO (com.cloud.hypervisor.vmware.mo.DatastoreMO)1 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)1 VirtualMachineFileLayoutExFileInfo (com.vmware.vim25.VirtualMachineFileLayoutExFileInfo)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 ConnectException (java.net.ConnectException)1 RemoteException (java.rmi.RemoteException)1 ConfigurationException (javax.naming.ConfigurationException)1