Search in sources :

Example 11 with TemplateProp

use of com.cloud.storage.template.TemplateProp in project cloudstack by apache.

the class NfsSecondaryStorageResource method s3ListVolume.

Map<Long, TemplateProp> s3ListVolume(S3TO s3) {
    String bucket = s3.getBucketName();
    // List the objects in the source directory on S3
    final List<S3ObjectSummary> objectSummaries = S3Utils.listDirectory(s3, bucket, VOLUME_ROOT_DIR);
    if (objectSummaries == null) {
        return null;
    }
    Map<Long, TemplateProp> tmpltInfos = new HashMap<Long, TemplateProp>();
    for (S3ObjectSummary objectSummary : objectSummaries) {
        String key = objectSummary.getKey();
        // String installPath = StringUtils.substringBeforeLast(key,
        // S3Utils.SEPARATOR);
        Long id = determineS3VolumeIdFromKey(key);
        // TODO: how to get volume template name
        TemplateProp tInfo = new TemplateProp(id.toString(), key, objectSummary.getSize(), objectSummary.getSize(), true, false);
        tmpltInfos.put(id, tInfo);
    }
    return tmpltInfos;
}
Also used : TemplateProp(com.cloud.storage.template.TemplateProp) HashMap(java.util.HashMap) S3ObjectSummary(com.amazonaws.services.s3.model.S3ObjectSummary)

Example 12 with TemplateProp

use of com.cloud.storage.template.TemplateProp 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 13 with TemplateProp

use of com.cloud.storage.template.TemplateProp in project cloudstack by apache.

the class DownloadManagerImpl method gatherTemplateInfo.

@Override
public Map<String, TemplateProp> gatherTemplateInfo(String rootDir) {
    Map<String, TemplateProp> result = new HashMap<String, TemplateProp>();
    String templateDir = rootDir + File.separator + _templateDir;
    if (!_storage.exists(templateDir)) {
        _storage.mkdirs(templateDir);
    }
    List<String> publicTmplts = listTemplates(templateDir);
    for (String tmplt : publicTmplts) {
        String path = tmplt.substring(0, tmplt.lastIndexOf(File.separator));
        TemplateLocation loc = new TemplateLocation(_storage, path);
        try {
            if (!loc.load()) {
                s_logger.warn("Post download installation was not completed for " + path);
                // loc.purge();
                _storage.cleanup(path, templateDir);
                continue;
            }
        } catch (IOException e) {
            s_logger.warn("Unable to load template location " + path, e);
            continue;
        }
        TemplateProp tInfo = loc.getTemplateInfo();
        if ((tInfo.getSize() == tInfo.getPhysicalSize()) && (tInfo.getInstallPath().endsWith(ImageFormat.OVA.getFileExtension()))) {
            try {
                Processor processor = _processors.get("OVA Processor");
                OVAProcessor vmdkProcessor = (OVAProcessor) processor;
                long vSize = vmdkProcessor.getTemplateVirtualSize(path, tInfo.getInstallPath().substring(tInfo.getInstallPath().lastIndexOf(File.separator) + 1));
                tInfo.setSize(vSize);
                loc.updateVirtualSize(vSize);
                loc.save();
            } catch (Exception e) {
                s_logger.error("Unable to get the virtual size of the template: " + tInfo.getInstallPath() + " due to " + e.getMessage());
            }
        }
        result.put(tInfo.getTemplateName(), tInfo);
        s_logger.debug("Added template name: " + tInfo.getTemplateName() + ", path: " + tmplt);
    }
    /*
        for (String tmplt : isoTmplts) {
            String tmp[];
            tmp = tmplt.split("/");
            String tmpltName = tmp[tmp.length - 2];
            tmplt = tmplt.substring(tmplt.lastIndexOf("iso/"));
            TemplateInfo tInfo = new TemplateInfo(tmpltName, tmplt, false);
            s_logger.debug("Added iso template name: " + tmpltName + ", path: " + tmplt);
            result.put(tmpltName, tInfo);
        }
         */
    return result;
}
Also used : TemplateProp(com.cloud.storage.template.TemplateProp) VhdProcessor(com.cloud.storage.template.VhdProcessor) OVAProcessor(com.cloud.storage.template.OVAProcessor) QCOW2Processor(com.cloud.storage.template.QCOW2Processor) TARProcessor(com.cloud.storage.template.TARProcessor) IsoProcessor(com.cloud.storage.template.IsoProcessor) Processor(com.cloud.storage.template.Processor) VmdkProcessor(com.cloud.storage.template.VmdkProcessor) RawImageProcessor(com.cloud.storage.template.RawImageProcessor) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) OVAProcessor(com.cloud.storage.template.OVAProcessor) TemplateLocation(com.cloud.storage.template.TemplateLocation) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InternalErrorException(com.cloud.exception.InternalErrorException) ConfigurationException(javax.naming.ConfigurationException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) IOException(java.io.IOException)

Example 14 with TemplateProp

use of com.cloud.storage.template.TemplateProp in project cloudstack by apache.

the class NfsSecondaryStorageResource method execute.

private Answer execute(ListTemplateCommand cmd) {
    if (!_inSystemVM) {
        return new ListTemplateAnswer(null, null);
    }
    DataStoreTO store = cmd.getDataStore();
    if (store instanceof NfsTO) {
        NfsTO nfs = (NfsTO) store;
        String secUrl = nfs.getUrl();
        String root = getRootDir(secUrl, cmd.getNfsVersion());
        Map<String, TemplateProp> templateInfos = _dlMgr.gatherTemplateInfo(root);
        return new ListTemplateAnswer(secUrl, templateInfos);
    } else if (store instanceof SwiftTO) {
        SwiftTO swift = (SwiftTO) store;
        Map<String, TemplateProp> templateInfos = swiftListTemplate(swift);
        return new ListTemplateAnswer(swift.toString(), templateInfos);
    } else if (store instanceof S3TO) {
        S3TO s3 = (S3TO) store;
        Map<String, TemplateProp> templateInfos = s3ListTemplate(s3);
        return new ListTemplateAnswer(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) SwiftTO(com.cloud.agent.api.to.SwiftTO) ListTemplateAnswer(com.cloud.agent.api.storage.ListTemplateAnswer) NfsTO(com.cloud.agent.api.to.NfsTO) Map(java.util.Map) HashMap(java.util.HashMap) S3TO(com.cloud.agent.api.to.S3TO)

Example 15 with TemplateProp

use of com.cloud.storage.template.TemplateProp in project cloudstack by apache.

the class NfsSecondaryStorageResource method swiftListTemplate.

Map<String, TemplateProp> swiftListTemplate(SwiftTO swift) {
    String[] containers = SwiftUtil.list(swift, "", null);
    if (containers == null) {
        return null;
    }
    Map<String, TemplateProp> tmpltInfos = new HashMap<String, TemplateProp>();
    for (String container : containers) {
        if (container.startsWith("T-")) {
            String[] files = SwiftUtil.list(swift, container, _tmpltpp);
            if (files.length != 1) {
                continue;
            }
            try {
                File tempFile = File.createTempFile("template", ".tmp");
                File tmpFile = SwiftUtil.getObject(swift, tempFile, container + File.separator + _tmpltpp);
                if (tmpFile == null) {
                    continue;
                }
                try (FileReader fr = new FileReader(tmpFile);
                    BufferedReader brf = new BufferedReader(fr)) {
                    String line = null;
                    String uniqName = null;
                    Long size = null;
                    Long physicalSize = null;
                    String name = null;
                    while ((line = brf.readLine()) != null) {
                        if (line.startsWith("uniquename=")) {
                            uniqName = line.split("=")[1];
                        } else if (line.startsWith("size=")) {
                            physicalSize = Long.parseLong(line.split("=")[1]);
                        } else if (line.startsWith("virtualsize=")) {
                            size = Long.parseLong(line.split("=")[1]);
                        } else if (line.startsWith("filename=")) {
                            name = line.split("=")[1];
                        }
                    }
                    //fallback
                    if (size == null) {
                        size = physicalSize;
                    }
                    tempFile.delete();
                    if (uniqName != null) {
                        TemplateProp prop = new TemplateProp(uniqName, container + File.separator + name, size, physicalSize, true, false);
                        tmpltInfos.put(uniqName, prop);
                    }
                } catch (IOException ex) {
                    s_logger.debug("swiftListTemplate:Exception:" + ex.getMessage());
                    continue;
                }
            } catch (IOException e) {
                s_logger.debug("Failed to create templ file:" + e.toString());
                continue;
            } catch (Exception e) {
                s_logger.debug("Failed to get properties: " + e.toString());
                continue;
            }
        }
    }
    return tmpltInfos;
}
Also used : TemplateProp(com.cloud.storage.template.TemplateProp) HashMap(java.util.HashMap) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) IOException(java.io.IOException) File(java.io.File) S3Utils.putFile(com.cloud.utils.storage.S3.S3Utils.putFile) 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)

Aggregations

TemplateProp (com.cloud.storage.template.TemplateProp)22 HashMap (java.util.HashMap)18 Answer (com.cloud.agent.api.Answer)11 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)11 ConfigurationException (javax.naming.ConfigurationException)11 ModifyStoragePoolAnswer (com.cloud.agent.api.ModifyStoragePoolAnswer)9 GetStorageStatsAnswer (com.cloud.agent.api.GetStorageStatsAnswer)7 URISyntaxException (java.net.URISyntaxException)7 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)7 StorageFilerTO (com.cloud.agent.api.to.StorageFilerTO)6 IOException (java.io.IOException)6 ListTemplateAnswer (com.cloud.agent.api.storage.ListTemplateAnswer)5 ListVolumeAnswer (com.cloud.agent.api.storage.ListVolumeAnswer)5 PrimaryStorageDownloadAnswer (com.cloud.agent.api.storage.PrimaryStorageDownloadAnswer)5 InternalErrorException (com.cloud.exception.InternalErrorException)5 ReadyAnswer (com.cloud.agent.api.ReadyAnswer)4 SecStorageSetupAnswer (com.cloud.agent.api.SecStorageSetupAnswer)4 CopyVolumeAnswer (com.cloud.agent.api.storage.CopyVolumeAnswer)4 CreatePrivateTemplateAnswer (com.cloud.agent.api.storage.CreatePrivateTemplateAnswer)4 DownloadAnswer (com.cloud.agent.api.storage.DownloadAnswer)4