Search in sources :

Example 1 with ResourceType

use of com.cloud.legacymodel.communication.command.DownloadCommand.ResourceType in project cosmic by MissionCriticalCloud.

the class DownloadManagerImpl method postLocalDownload.

/**
 * Post local download activity (install and cleanup). Executed in context of
 * downloader thread
 *
 * @throws IOException
 */
private String postLocalDownload(final String jobId) {
    final DownloadJob dnld = this.jobs.get(jobId);
    final TemplateDownloader td = dnld.getTemplateDownloader();
    // path with mount
    final String resourcePath = dnld.getInstallPathPrefix();
    // directory
    // template download
    final String finalResourcePath = dnld.getTmpltPath();
    // path on secondary
    // storage
    final ResourceType resourceType = dnld.getResourceType();
    final File originalTemplate = new File(td.getDownloadLocalPath());
    final 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(this._storage.getSize(td.getDownloadLocalPath()) * 1.0d / (1024 * 1024 * 1024));
    // add one just in case
    imgSizeGigs++;
    final long timeout = (long) imgSizeGigs * this.installTimeoutPerGig;
    Script scr = null;
    final String script = resourceType == ResourceType.TEMPLATE ? this.createTmpltScr : this.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());
    }
    // add options common to ISO and template
    final String extension = dnld.getFormat().toString().toLowerCase();
    String templateName = "";
    if (extension.equals("iso")) {
        templateName = this.jobs.get(jobId).getTmpltName().trim().replace(" ", "_");
    } else {
        templateName = java.util.UUID.nameUUIDFromBytes((this.jobs.get(jobId).getTmpltName() + System.currentTimeMillis()).getBytes(StringUtils.getPreferredCharset())).toString();
    }
    // run script to mv the temporary template file to the final template
    // file
    final 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");
    final String result;
    result = scr.execute();
    if (result != null) {
        return result;
    }
    // Set permissions for the downloaded template
    final File downloadedTemplate = new File(resourcePath + "/" + templateFilename);
    this._storage.setWorldReadableAndWriteable(downloadedTemplate);
    // Set permissions for template/volume.properties
    String propertiesFile = resourcePath;
    if (resourceType == ResourceType.TEMPLATE) {
        propertiesFile += "/template.properties";
    } else {
        propertiesFile += "/volume.properties";
    }
    final File templateProperties = new File(propertiesFile);
    this._storage.setWorldReadableAndWriteable(templateProperties);
    final TemplateLocation loc = new TemplateLocation(this._storage, resourcePath);
    try {
        loc.create(dnld.getId(), true, dnld.getTmpltName());
    } catch (final IOException e) {
        s_logger.warn("Something is wrong with template location " + resourcePath, e);
        loc.purge();
        return "Unable to download due to " + e.getMessage();
    }
    final Iterator<Processor> en = this._processors.values().iterator();
    while (en.hasNext()) {
        final Processor processor = en.next();
        TemplateFormatInfo info = null;
        try {
            info = processor.process(resourcePath, null, templateName);
        } catch (final 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) IsoProcessor(com.cloud.common.storageprocessor.IsoProcessor) QCOW2Processor(com.cloud.common.storageprocessor.QCOW2Processor) RawImageProcessor(com.cloud.common.storageprocessor.RawImageProcessor) TARProcessor(com.cloud.common.storageprocessor.TARProcessor) VhdProcessor(com.cloud.common.storageprocessor.VhdProcessor) Processor(com.cloud.common.storageprocessor.Processor) ResourceType(com.cloud.legacymodel.communication.command.DownloadCommand.ResourceType) IOException(java.io.IOException) InternalErrorException(com.cloud.legacymodel.exceptions.InternalErrorException) TemplateLocation(com.cloud.common.storageprocessor.TemplateLocation) TemplateFormatInfo(com.cloud.legacymodel.storage.TemplateFormatInfo) File(java.io.File)

Example 2 with ResourceType

use of com.cloud.legacymodel.communication.command.DownloadCommand.ResourceType in project cosmic by MissionCriticalCloud.

the class DownloadManagerImpl method handleDownloadCommand.

@Override
public DownloadAnswer handleDownloadCommand(final SecondaryStorageResource resource, final DownloadCommand cmd) {
    final 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", VMTemplateStatus.DOWNLOAD_ERROR);
    }
    if (cmd.getName() == null) {
        return new DownloadAnswer("Invalid Name", VMTemplateStatus.DOWNLOAD_ERROR);
    }
    final 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()) + 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
    final long maxDownloadSizeInBytes = cmd.getMaxDownloadSizeInBytes() == null ? TemplateDownloader.DEFAULT_MAX_TEMPLATE_SIZE_IN_BYTES : cmd.getMaxDownloadSizeInBytes();
    String jobId = null;
    jobId = downloadPublicTemplate(cmd.getId(), cmd.getUrl(), cmd.getName(), cmd.getFormat(), cmd.getAccountId(), cmd.getDescription(), cmd.getChecksum(), installPathPrefix, cmd.getInstallPath(), user, password, maxDownloadSizeInBytes, cmd.getProxy(), resourceType);
    sleep();
    if (jobId == null) {
        return new DownloadAnswer("Internal Error", VMTemplateStatus.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.legacymodel.to.DataStoreTO) ResourceType(com.cloud.legacymodel.communication.command.DownloadCommand.ResourceType) DownloadAnswer(com.cloud.legacymodel.communication.answer.DownloadAnswer) NfsTO(com.cloud.legacymodel.to.NfsTO) DownloadProgressCommand(com.cloud.legacymodel.communication.command.DownloadProgressCommand)

Aggregations

ResourceType (com.cloud.legacymodel.communication.command.DownloadCommand.ResourceType)2 IsoProcessor (com.cloud.common.storageprocessor.IsoProcessor)1 Processor (com.cloud.common.storageprocessor.Processor)1 QCOW2Processor (com.cloud.common.storageprocessor.QCOW2Processor)1 RawImageProcessor (com.cloud.common.storageprocessor.RawImageProcessor)1 TARProcessor (com.cloud.common.storageprocessor.TARProcessor)1 TemplateLocation (com.cloud.common.storageprocessor.TemplateLocation)1 VhdProcessor (com.cloud.common.storageprocessor.VhdProcessor)1 DownloadAnswer (com.cloud.legacymodel.communication.answer.DownloadAnswer)1 DownloadProgressCommand (com.cloud.legacymodel.communication.command.DownloadProgressCommand)1 InternalErrorException (com.cloud.legacymodel.exceptions.InternalErrorException)1 TemplateFormatInfo (com.cloud.legacymodel.storage.TemplateFormatInfo)1 DataStoreTO (com.cloud.legacymodel.to.DataStoreTO)1 NfsTO (com.cloud.legacymodel.to.NfsTO)1 Script (com.cloud.utils.script.Script)1 File (java.io.File)1 IOException (java.io.IOException)1