Search in sources :

Example 1 with DirectTemplateDownloader

use of com.cloud.agent.direct.download.DirectTemplateDownloader in project cloudstack by apache.

the class KVMStorageProcessor method handleDownloadTemplateToPrimaryStorage.

@Override
public Answer handleDownloadTemplateToPrimaryStorage(DirectDownloadCommand cmd) {
    final PrimaryDataStoreTO pool = cmd.getDestPool();
    DirectTemplateDownloader downloader;
    KVMPhysicalDisk template;
    KVMStoragePool destPool = null;
    try {
        s_logger.debug("Verifying temporary location for downloading the template exists on the host");
        String temporaryDownloadPath = resource.getDirectDownloadTemporaryDownloadPath();
        if (!isLocationAccessible(temporaryDownloadPath)) {
            String msg = "The temporary location path for downloading templates does not exist: " + temporaryDownloadPath + " on this host";
            s_logger.error(msg);
            return new DirectDownloadAnswer(false, msg, true);
        }
        Long templateSize = null;
        if (StringUtils.isNotBlank(cmd.getUrl())) {
            String url = cmd.getUrl();
            templateSize = UriUtils.getRemoteSize(url);
        }
        s_logger.debug("Checking for free space on the host for downloading the template with physical size: " + templateSize + " and virtual size: " + cmd.getTemplateSize());
        if (!isEnoughSpaceForDownloadTemplateOnTemporaryLocation(templateSize)) {
            String msg = "Not enough space on the defined temporary location to download the template " + cmd.getTemplateId();
            s_logger.error(msg);
            return new DirectDownloadAnswer(false, msg, true);
        }
        destPool = storagePoolMgr.getStoragePool(pool.getPoolType(), pool.getUuid());
        downloader = getDirectTemplateDownloaderFromCommand(cmd, destPool, temporaryDownloadPath);
        s_logger.debug("Trying to download template");
        Pair<Boolean, String> result = downloader.downloadTemplate();
        if (!result.first()) {
            s_logger.warn("Couldn't download template");
            return new DirectDownloadAnswer(false, "Unable to download template", true);
        }
        String tempFilePath = result.second();
        if (!downloader.validateChecksum()) {
            s_logger.warn("Couldn't validate template checksum");
            return new DirectDownloadAnswer(false, "Checksum validation failed", false);
        }
        final TemplateObjectTO destTemplate = cmd.getDestData();
        String destTemplatePath = (destTemplate != null) ? destTemplate.getPath() : null;
        if (!storagePoolMgr.connectPhysicalDisk(pool.getPoolType(), pool.getUuid(), destTemplatePath, null)) {
            s_logger.warn("Unable to connect physical disk at path: " + destTemplatePath + ", in storage pool id: " + pool.getUuid());
        }
        template = storagePoolMgr.createPhysicalDiskFromDirectDownloadTemplate(tempFilePath, destTemplatePath, destPool, cmd.getFormat(), cmd.getWaitInMillSeconds());
        if (!storagePoolMgr.disconnectPhysicalDisk(pool.getPoolType(), pool.getUuid(), destTemplatePath)) {
            s_logger.warn("Unable to disconnect physical disk at path: " + destTemplatePath + ", in storage pool id: " + pool.getUuid());
        }
    } catch (CloudRuntimeException e) {
        s_logger.warn("Error downloading template " + cmd.getTemplateId() + " due to: " + e.getMessage());
        return new DirectDownloadAnswer(false, "Unable to download template: " + e.getMessage(), true);
    } catch (IllegalArgumentException e) {
        return new DirectDownloadAnswer(false, "Unable to create direct downloader: " + e.getMessage(), true);
    }
    return new DirectDownloadAnswer(true, template.getSize(), template.getName());
}
Also used : HttpsDirectTemplateDownloader(com.cloud.agent.direct.download.HttpsDirectTemplateDownloader) DirectTemplateDownloader(com.cloud.agent.direct.download.DirectTemplateDownloader) NfsDirectTemplateDownloader(com.cloud.agent.direct.download.NfsDirectTemplateDownloader) MetalinkDirectTemplateDownloader(com.cloud.agent.direct.download.MetalinkDirectTemplateDownloader) HttpDirectTemplateDownloader(com.cloud.agent.direct.download.HttpDirectTemplateDownloader) PrimaryDataStoreTO(org.apache.cloudstack.storage.to.PrimaryDataStoreTO) DirectDownloadAnswer(org.apache.cloudstack.agent.directdownload.DirectDownloadAnswer) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) TemplateObjectTO(org.apache.cloudstack.storage.to.TemplateObjectTO)

Aggregations

DirectTemplateDownloader (com.cloud.agent.direct.download.DirectTemplateDownloader)1 HttpDirectTemplateDownloader (com.cloud.agent.direct.download.HttpDirectTemplateDownloader)1 HttpsDirectTemplateDownloader (com.cloud.agent.direct.download.HttpsDirectTemplateDownloader)1 MetalinkDirectTemplateDownloader (com.cloud.agent.direct.download.MetalinkDirectTemplateDownloader)1 NfsDirectTemplateDownloader (com.cloud.agent.direct.download.NfsDirectTemplateDownloader)1 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)1 DirectDownloadAnswer (org.apache.cloudstack.agent.directdownload.DirectDownloadAnswer)1 PrimaryDataStoreTO (org.apache.cloudstack.storage.to.PrimaryDataStoreTO)1 TemplateObjectTO (org.apache.cloudstack.storage.to.TemplateObjectTO)1