Search in sources :

Example 1 with ResourceType

use of org.apache.cloudstack.storage.command.DownloadCommand.ResourceType in project cloudstack by apache.

the class DownloadManagerImpl method postLocalDownload.

/**
     * Post local download activity (install and cleanup). Executed in context of
     * downloader thread
     *
     * @throws IOException
     */
private String postLocalDownload(String jobId) {
    DownloadJob dnld = jobs.get(jobId);
    TemplateDownloader td = dnld.getTemplateDownloader();
    // path with mount
    String resourcePath = dnld.getInstallPathPrefix();
    // directory
    // template download
    String finalResourcePath = dnld.getTmpltPath();
    // path on secondary
    // storage
    ResourceType resourceType = dnld.getResourceType();
    File originalTemplate = new File(td.getDownloadLocalPath());
    String checkSum = computeCheckSum(originalTemplate);
    if (checkSum == null) {
        s_logger.warn("Something wrong happened when try to calculate the checksum of downloaded template!");
    }
    dnld.setCheckSum(checkSum);
    int imgSizeGigs = (int) Math.ceil(_storage.getSize(td.getDownloadLocalPath()) * 1.0d / (1024 * 1024 * 1024));
    // add one just in case
    imgSizeGigs++;
    long timeout = (long) imgSizeGigs * installTimeoutPerGig;
    Script scr = null;
    String script = resourceType == ResourceType.TEMPLATE ? createTmpltScr : createVolScr;
    scr = new Script(script, timeout, s_logger);
    scr.add("-s", Integer.toString(imgSizeGigs));
    scr.add("-S", Long.toString(td.getMaxTemplateSizeInBytes()));
    if (dnld.getDescription() != null && dnld.getDescription().length() > 1) {
        scr.add("-d", dnld.getDescription());
    }
    if (dnld.isHvm()) {
        scr.add("-h");
    }
    // add options common to ISO and template
    String extension = dnld.getFormat().getFileExtension();
    String templateName = "";
    if (extension.equals("iso")) {
        templateName = jobs.get(jobId).getTmpltName().trim().replace(" ", "_");
    } else {
        templateName = java.util.UUID.nameUUIDFromBytes((jobs.get(jobId).getTmpltName() + System.currentTimeMillis()).getBytes(StringUtils.getPreferredCharset())).toString();
    }
    // run script to mv the temporary template file to the final template
    // file
    String templateFilename = templateName + "." + extension;
    dnld.setTmpltPath(finalResourcePath + "/" + templateFilename);
    scr.add("-n", templateFilename);
    scr.add("-t", resourcePath);
    // this is the temporary
    scr.add("-f", td.getDownloadLocalPath());
    // template file downloaded
    if (dnld.getChecksum() != null && dnld.getChecksum().length() > 1) {
        scr.add("-c", dnld.getChecksum());
    }
    // cleanup
    scr.add("-u");
    String result;
    result = scr.execute();
    if (result != null) {
        return result;
    }
    // Set permissions for the downloaded template
    File downloadedTemplate = new File(resourcePath + "/" + templateFilename);
    _storage.setWorldReadableAndWriteable(downloadedTemplate);
    // Set permissions for template/volume.properties
    String propertiesFile = resourcePath;
    if (resourceType == ResourceType.TEMPLATE) {
        propertiesFile += "/template.properties";
    } else {
        propertiesFile += "/volume.properties";
    }
    File templateProperties = new File(propertiesFile);
    _storage.setWorldReadableAndWriteable(templateProperties);
    TemplateLocation loc = new TemplateLocation(_storage, resourcePath);
    try {
        loc.create(dnld.getId(), true, dnld.getTmpltName());
    } catch (IOException e) {
        s_logger.warn("Something is wrong with template location " + resourcePath, e);
        loc.purge();
        return "Unable to download due to " + e.getMessage();
    }
    Iterator<Processor> en = _processors.values().iterator();
    while (en.hasNext()) {
        Processor processor = en.next();
        FormatInfo info = null;
        try {
            info = processor.process(resourcePath, null, templateName);
        } catch (InternalErrorException e) {
            s_logger.error("Template process exception ", e);
            return e.toString();
        }
        if (info != null) {
            if (!loc.addFormat(info)) {
                loc.purge();
                return "Unable to install due to invalid file format";
            }
            dnld.setTemplatesize(info.virtualSize);
            dnld.setTemplatePhysicalSize(info.size);
            break;
        }
    }
    if (!loc.save()) {
        s_logger.warn("Cleaning up because we're unable to save the formats");
        loc.purge();
    }
    return null;
}
Also used : Script(com.cloud.utils.script.Script) 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) HttpTemplateDownloader(com.cloud.storage.template.HttpTemplateDownloader) ScpTemplateDownloader(com.cloud.storage.template.ScpTemplateDownloader) S3TemplateDownloader(com.cloud.storage.template.S3TemplateDownloader) TemplateDownloader(com.cloud.storage.template.TemplateDownloader) LocalTemplateDownloader(com.cloud.storage.template.LocalTemplateDownloader) ResourceType(org.apache.cloudstack.storage.command.DownloadCommand.ResourceType) IOException(java.io.IOException) InternalErrorException(com.cloud.exception.InternalErrorException) TemplateLocation(com.cloud.storage.template.TemplateLocation) FormatInfo(com.cloud.storage.template.Processor.FormatInfo) File(java.io.File)

Example 2 with ResourceType

use of org.apache.cloudstack.storage.command.DownloadCommand.ResourceType in project cloudstack by apache.

the class DownloadManagerImpl method handleDownloadCommand.

@Override
public DownloadAnswer handleDownloadCommand(SecondaryStorageResource resource, DownloadCommand cmd) {
    ResourceType resourceType = cmd.getResourceType();
    if (cmd instanceof DownloadProgressCommand) {
        return handleDownloadProgressCmd(resource, (DownloadProgressCommand) cmd);
    }
    if (cmd.getUrl() == null) {
        return new DownloadAnswer(resourceType.toString() + " is corrupted on storage due to an invalid url , cannot download", VMTemplateStorageResourceAssoc.Status.DOWNLOAD_ERROR);
    }
    if (cmd.getName() == null) {
        return new DownloadAnswer("Invalid Name", VMTemplateStorageResourceAssoc.Status.DOWNLOAD_ERROR);
    }
    DataStoreTO dstore = cmd.getDataStore();
    String installPathPrefix = cmd.getInstallPath();
    // for NFS, we need to get mounted path
    if (dstore instanceof NfsTO) {
        installPathPrefix = resource.getRootDir(((NfsTO) dstore).getUrl(), _nfsVersion) + File.separator + installPathPrefix;
    }
    String user = null;
    String password = null;
    if (cmd.getAuth() != null) {
        user = cmd.getAuth().getUserName();
        password = cmd.getAuth().getPassword();
    }
    // TO DO - Define Volume max size as well
    long maxDownloadSizeInBytes = (cmd.getMaxDownloadSizeInBytes() == null) ? TemplateDownloader.DEFAULT_MAX_TEMPLATE_SIZE_IN_BYTES : (cmd.getMaxDownloadSizeInBytes());
    String jobId = null;
    if (dstore instanceof S3TO) {
        jobId = downloadS3Template((S3TO) dstore, cmd.getId(), cmd.getUrl(), cmd.getName(), cmd.getFormat(), cmd.isHvm(), cmd.getAccountId(), cmd.getDescription(), cmd.getChecksum(), installPathPrefix, user, password, maxDownloadSizeInBytes, cmd.getProxy(), resourceType);
    } else {
        jobId = downloadPublicTemplate(cmd.getId(), cmd.getUrl(), cmd.getName(), cmd.getFormat(), cmd.isHvm(), cmd.getAccountId(), cmd.getDescription(), cmd.getChecksum(), installPathPrefix, cmd.getInstallPath(), user, password, maxDownloadSizeInBytes, cmd.getProxy(), resourceType);
    }
    sleep();
    if (jobId == null) {
        return new DownloadAnswer("Internal Error", VMTemplateStorageResourceAssoc.Status.DOWNLOAD_ERROR);
    }
    return new DownloadAnswer(jobId, getDownloadPct(jobId), getDownloadError(jobId), getDownloadStatus2(jobId), getDownloadLocalPath(jobId), getInstallPath(jobId), getDownloadTemplateSize(jobId), getDownloadTemplateSize(jobId), getDownloadCheckSum(jobId));
}
Also used : DataStoreTO(com.cloud.agent.api.to.DataStoreTO) ResourceType(org.apache.cloudstack.storage.command.DownloadCommand.ResourceType) DownloadAnswer(com.cloud.agent.api.storage.DownloadAnswer) NfsTO(com.cloud.agent.api.to.NfsTO) S3TO(com.cloud.agent.api.to.S3TO) DownloadProgressCommand(org.apache.cloudstack.storage.command.DownloadProgressCommand)

Aggregations

ResourceType (org.apache.cloudstack.storage.command.DownloadCommand.ResourceType)2 DownloadAnswer (com.cloud.agent.api.storage.DownloadAnswer)1 DataStoreTO (com.cloud.agent.api.to.DataStoreTO)1 NfsTO (com.cloud.agent.api.to.NfsTO)1 S3TO (com.cloud.agent.api.to.S3TO)1 InternalErrorException (com.cloud.exception.InternalErrorException)1 HttpTemplateDownloader (com.cloud.storage.template.HttpTemplateDownloader)1 IsoProcessor (com.cloud.storage.template.IsoProcessor)1 LocalTemplateDownloader (com.cloud.storage.template.LocalTemplateDownloader)1 OVAProcessor (com.cloud.storage.template.OVAProcessor)1 Processor (com.cloud.storage.template.Processor)1 FormatInfo (com.cloud.storage.template.Processor.FormatInfo)1 QCOW2Processor (com.cloud.storage.template.QCOW2Processor)1 RawImageProcessor (com.cloud.storage.template.RawImageProcessor)1 S3TemplateDownloader (com.cloud.storage.template.S3TemplateDownloader)1 ScpTemplateDownloader (com.cloud.storage.template.ScpTemplateDownloader)1 TARProcessor (com.cloud.storage.template.TARProcessor)1 TemplateDownloader (com.cloud.storage.template.TemplateDownloader)1 TemplateLocation (com.cloud.storage.template.TemplateLocation)1 VhdProcessor (com.cloud.storage.template.VhdProcessor)1