Search in sources :

Example 16 with DataStoreTO

use of com.cloud.agent.api.to.DataStoreTO in project cloudstack by apache.

the class NfsSecondaryStorageResource method registerTemplateOnSwift.

protected Answer registerTemplateOnSwift(DownloadCommand cmd) {
    SwiftTO swiftTO = (SwiftTO) cmd.getDataStore();
    String path = cmd.getInstallPath();
    DataStoreTO cacheStore = cmd.getCacheStore();
    if (cacheStore == null || !(cacheStore instanceof NfsTO)) {
        return new DownloadAnswer("cache store can't be null", VMTemplateStorageResourceAssoc.Status.DOWNLOAD_ERROR);
    }
    File file = null;
    try {
        NfsTO nfsCacheStore = (NfsTO) cacheStore;
        String fileName = cmd.getName() + "." + cmd.getFormat().getFileExtension();
        file = downloadFromUrlToNfs(cmd.getUrl(), nfsCacheStore, path, fileName);
        String container = "T-" + cmd.getId();
        String swiftPath = SwiftUtil.putObject(swiftTO, file, container, null);
        long virtualSize = getVirtualSize(file, getTemplateFormat(file.getName()));
        long size = file.length();
        String uniqueName = cmd.getName();
        //put metda file
        File uniqDir = _storage.createUniqDir();
        String metaFileName = uniqDir.getAbsolutePath() + File.separator + _tmpltpp;
        _storage.create(uniqDir.getAbsolutePath(), _tmpltpp);
        File metaFile = swiftWriteMetadataFile(metaFileName, uniqueName, fileName, size, virtualSize);
        SwiftUtil.putObject(swiftTO, metaFile, container, _tmpltpp);
        metaFile.delete();
        uniqDir.delete();
        String md5sum = null;
        try (FileInputStream fs = new FileInputStream(file)) {
            md5sum = DigestUtils.md5Hex(fs);
        } catch (IOException e) {
            s_logger.debug("Failed to get md5sum: " + file.getAbsoluteFile());
        }
        DownloadAnswer answer = new DownloadAnswer(null, 100, null, VMTemplateStorageResourceAssoc.Status.DOWNLOADED, swiftPath, swiftPath, virtualSize, file.length(), md5sum);
        return answer;
    } catch (IOException e) {
        s_logger.debug("Failed to register template into swift", e);
        return new DownloadAnswer(e.toString(), VMTemplateStorageResourceAssoc.Status.DOWNLOAD_ERROR);
    } finally {
        if (file != null) {
            file.delete();
        }
    }
}
Also used : DataStoreTO(com.cloud.agent.api.to.DataStoreTO) SwiftTO(com.cloud.agent.api.to.SwiftTO) IOException(java.io.IOException) DownloadAnswer(com.cloud.agent.api.storage.DownloadAnswer) NfsTO(com.cloud.agent.api.to.NfsTO) File(java.io.File) S3Utils.putFile(com.cloud.utils.storage.S3.S3Utils.putFile) FileInputStream(java.io.FileInputStream)

Example 17 with DataStoreTO

use of com.cloud.agent.api.to.DataStoreTO in project cloudstack by apache.

the class NfsSecondaryStorageResource method deleteTemplate.

protected Answer deleteTemplate(DeleteCommand cmd) {
    DataTO obj = cmd.getData();
    DataStoreTO dstore = obj.getDataStore();
    if (dstore instanceof NfsTO) {
        NfsTO nfs = (NfsTO) dstore;
        String relativeTemplatePath = obj.getPath();
        String parent = getRootDir(nfs.getUrl(), _nfsVersion);
        if (relativeTemplatePath.startsWith(File.separator)) {
            relativeTemplatePath = relativeTemplatePath.substring(1);
        }
        if (!parent.endsWith(File.separator)) {
            parent += File.separator;
        }
        String absoluteTemplatePath = parent + relativeTemplatePath;
        File tmpltPath = new File(absoluteTemplatePath);
        File tmpltParent = null;
        if (tmpltPath.exists() && tmpltPath.isDirectory()) {
            tmpltParent = tmpltPath;
        } else {
            tmpltParent = tmpltPath.getParentFile();
        }
        String details = null;
        if (!tmpltParent.exists()) {
            details = "template parent directory " + tmpltParent.getName() + " doesn't exist";
            s_logger.debug(details);
            return new Answer(cmd, true, details);
        }
        File[] tmpltFiles = tmpltParent.listFiles();
        if (tmpltFiles == null || tmpltFiles.length == 0) {
            details = "No files under template parent directory " + tmpltParent.getName();
            s_logger.debug(details);
        } else {
            boolean found = false;
            for (File f : tmpltFiles) {
                if (!found && f.getName().equals(_tmpltpp)) {
                    found = true;
                }
                // Don't let this stop us from cleaning up the template
                if (f.isDirectory() && f.getName().equals("KVMHA")) {
                    s_logger.debug("Deleting KVMHA directory contents from template location");
                    File[] haFiles = f.listFiles();
                    for (File haFile : haFiles) {
                        haFile.delete();
                    }
                }
                if (!f.delete()) {
                    return new Answer(cmd, false, "Unable to delete file " + f.getName() + " under Template path " + relativeTemplatePath);
                }
            }
            if (!found) {
                details = "Can not find template.properties under " + tmpltParent.getName();
                s_logger.debug(details);
            }
        }
        if (!tmpltParent.delete()) {
            details = "Unable to delete directory " + tmpltParent.getName() + " under Template path " + relativeTemplatePath;
            s_logger.debug(details);
            return new Answer(cmd, false, details);
        }
        return new Answer(cmd, true, null);
    } else if (dstore instanceof S3TO) {
        final S3TO s3 = (S3TO) dstore;
        final String path = obj.getPath();
        final String bucket = s3.getBucketName();
        try {
            S3Utils.deleteDirectory(s3, bucket, path);
            return new Answer(cmd, true, String.format("Deleted template %1$s from bucket %2$s.", path, bucket));
        } catch (Exception e) {
            final String errorMessage = String.format("Failed to delete template %1$s from bucket %2$s due to the following error: %3$s", path, bucket, e.getMessage());
            s_logger.error(errorMessage, e);
            return new Answer(cmd, false, errorMessage);
        }
    } else if (dstore instanceof SwiftTO) {
        SwiftTO swift = (SwiftTO) dstore;
        String container = "T-" + obj.getId();
        String object = "";
        try {
            String result = swiftDelete(swift, container, object);
            if (result != null) {
                String errMsg = "failed to delete object " + container + "/" + object + " , err=" + result;
                s_logger.warn(errMsg);
                return new Answer(cmd, false, errMsg);
            }
            return new Answer(cmd, true, "success");
        } catch (Exception e) {
            String errMsg = cmd + " Command failed due to " + e.toString();
            s_logger.warn(errMsg, e);
            return new Answer(cmd, false, errMsg);
        }
    } else {
        return new Answer(cmd, false, "Unsupported image data store: " + dstore);
    }
}
Also used : ListTemplateAnswer(com.cloud.agent.api.storage.ListTemplateAnswer) UploadStatusAnswer(org.apache.cloudstack.storage.command.UploadStatusAnswer) GetStorageStatsAnswer(com.cloud.agent.api.GetStorageStatsAnswer) CopyCmdAnswer(org.apache.cloudstack.storage.command.CopyCmdAnswer) ListVolumeAnswer(com.cloud.agent.api.storage.ListVolumeAnswer) DownloadAnswer(com.cloud.agent.api.storage.DownloadAnswer) Answer(com.cloud.agent.api.Answer) CheckHealthAnswer(com.cloud.agent.api.CheckHealthAnswer) ReadyAnswer(com.cloud.agent.api.ReadyAnswer) SecStorageSetupAnswer(com.cloud.agent.api.SecStorageSetupAnswer) DataStoreTO(com.cloud.agent.api.to.DataStoreTO) DataTO(com.cloud.agent.api.to.DataTO) SwiftTO(com.cloud.agent.api.to.SwiftTO) NfsTO(com.cloud.agent.api.to.NfsTO) File(java.io.File) S3Utils.putFile(com.cloud.utils.storage.S3.S3Utils.putFile) S3TO(com.cloud.agent.api.to.S3TO) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) InternalErrorException(com.cloud.exception.InternalErrorException) ConfigurationException(javax.naming.ConfigurationException)

Example 18 with DataStoreTO

use of com.cloud.agent.api.to.DataStoreTO in project cloudstack by apache.

the class NfsSecondaryStorageResource method execute.

protected GetStorageStatsAnswer execute(final GetStorageStatsCommand cmd) {
    DataStoreTO store = cmd.getStore();
    if (store instanceof S3TO || store instanceof SwiftTO) {
        long infinity = Integer.MAX_VALUE;
        return new GetStorageStatsAnswer(cmd, infinity, 0L);
    }
    String rootDir = getRootDir(((NfsTO) store).getUrl(), cmd.getNfsVersion());
    final long usedSize = getUsedSize(rootDir);
    final long totalSize = getTotalSize(rootDir);
    if (usedSize == -1 || totalSize == -1) {
        return new GetStorageStatsAnswer(cmd, "Unable to get storage stats");
    } else {
        return new GetStorageStatsAnswer(cmd, totalSize, usedSize);
    }
}
Also used : DataStoreTO(com.cloud.agent.api.to.DataStoreTO) GetStorageStatsAnswer(com.cloud.agent.api.GetStorageStatsAnswer) SwiftTO(com.cloud.agent.api.to.SwiftTO) S3TO(com.cloud.agent.api.to.S3TO)

Example 19 with DataStoreTO

use of com.cloud.agent.api.to.DataStoreTO in project cloudstack by apache.

the class NfsSecondaryStorageResource method execute.

private Answer execute(ListVolumeCommand cmd) {
    if (!_inSystemVM) {
        return new ListVolumeAnswer(cmd.getSecUrl(), null);
    }
    DataStoreTO store = cmd.getDataStore();
    if (store instanceof NfsTO) {
        String root = getRootDir(cmd.getSecUrl(), _nfsVersion);
        Map<Long, TemplateProp> templateInfos = _dlMgr.gatherVolumeInfo(root);
        return new ListVolumeAnswer(cmd.getSecUrl(), templateInfos);
    } else if (store instanceof S3TO) {
        S3TO s3 = (S3TO) store;
        Map<Long, TemplateProp> templateInfos = s3ListVolume(s3);
        return new ListVolumeAnswer(s3.getBucketName(), templateInfos);
    } else {
        return new Answer(cmd, false, "Unsupported image data store: " + store);
    }
}
Also used : TemplateProp(com.cloud.storage.template.TemplateProp) ListTemplateAnswer(com.cloud.agent.api.storage.ListTemplateAnswer) UploadStatusAnswer(org.apache.cloudstack.storage.command.UploadStatusAnswer) GetStorageStatsAnswer(com.cloud.agent.api.GetStorageStatsAnswer) CopyCmdAnswer(org.apache.cloudstack.storage.command.CopyCmdAnswer) ListVolumeAnswer(com.cloud.agent.api.storage.ListVolumeAnswer) DownloadAnswer(com.cloud.agent.api.storage.DownloadAnswer) Answer(com.cloud.agent.api.Answer) CheckHealthAnswer(com.cloud.agent.api.CheckHealthAnswer) ReadyAnswer(com.cloud.agent.api.ReadyAnswer) SecStorageSetupAnswer(com.cloud.agent.api.SecStorageSetupAnswer) DataStoreTO(com.cloud.agent.api.to.DataStoreTO) ListVolumeAnswer(com.cloud.agent.api.storage.ListVolumeAnswer) NfsTO(com.cloud.agent.api.to.NfsTO) S3TO(com.cloud.agent.api.to.S3TO) Map(java.util.Map) HashMap(java.util.HashMap)

Example 20 with DataStoreTO

use of com.cloud.agent.api.to.DataStoreTO in project cloudstack by apache.

the class LibvirtComputingResourceTest method testGetStorageStatsCommand.

@Test
public void testGetStorageStatsCommand() {
    final DataStoreTO store = Mockito.mock(DataStoreTO.class);
    final GetStorageStatsCommand command = new GetStorageStatsCommand(store);
    final KVMStoragePoolManager storagePoolMgr = Mockito.mock(KVMStoragePoolManager.class);
    final KVMStoragePool secondaryPool = Mockito.mock(KVMStoragePool.class);
    when(libvirtComputingResource.getStoragePoolMgr()).thenReturn(storagePoolMgr);
    when(storagePoolMgr.getStoragePool(command.getPooltype(), command.getStorageId(), true)).thenReturn(secondaryPool);
    final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
    assertNotNull(wrapper);
    final Answer answer = wrapper.execute(command, libvirtComputingResource);
    assertTrue(answer.getResult());
    verify(libvirtComputingResource, times(1)).getStoragePoolMgr();
    verify(storagePoolMgr, times(1)).getStoragePool(command.getPooltype(), command.getStorageId(), true);
}
Also used : AttachAnswer(org.apache.cloudstack.storage.command.AttachAnswer) Answer(com.cloud.agent.api.Answer) CheckRouterAnswer(com.cloud.agent.api.CheckRouterAnswer) DataStoreTO(com.cloud.agent.api.to.DataStoreTO) KVMStoragePoolManager(com.cloud.hypervisor.kvm.storage.KVMStoragePoolManager) LibvirtRequestWrapper(com.cloud.hypervisor.kvm.resource.wrapper.LibvirtRequestWrapper) KVMStoragePool(com.cloud.hypervisor.kvm.storage.KVMStoragePool) GetStorageStatsCommand(com.cloud.agent.api.GetStorageStatsCommand) Test(org.junit.Test)

Aggregations

DataStoreTO (com.cloud.agent.api.to.DataStoreTO)91 NfsTO (com.cloud.agent.api.to.NfsTO)66 CopyCmdAnswer (org.apache.cloudstack.storage.command.CopyCmdAnswer)54 PrimaryDataStoreTO (org.apache.cloudstack.storage.to.PrimaryDataStoreTO)50 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)47 DataTO (com.cloud.agent.api.to.DataTO)46 VolumeObjectTO (org.apache.cloudstack.storage.to.VolumeObjectTO)36 InternalErrorException (com.cloud.exception.InternalErrorException)30 TemplateObjectTO (org.apache.cloudstack.storage.to.TemplateObjectTO)28 Answer (com.cloud.agent.api.Answer)26 SnapshotObjectTO (org.apache.cloudstack.storage.to.SnapshotObjectTO)20 UnsupportedEncodingException (java.io.UnsupportedEncodingException)19 IOException (java.io.IOException)17 S3TO (com.cloud.agent.api.to.S3TO)16 ConfigurationException (javax.naming.ConfigurationException)16 XmlRpcException (org.apache.xmlrpc.XmlRpcException)16 XenAPIException (com.xensource.xenapi.Types.XenAPIException)15 RemoteException (java.rmi.RemoteException)15 SwiftTO (com.cloud.agent.api.to.SwiftTO)14 ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)14