Search in sources :

Example 1 with DirectDownloadAnswer

use of org.apache.cloudstack.agent.directdownload.DirectDownloadAnswer 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)

Example 2 with DirectDownloadAnswer

use of org.apache.cloudstack.agent.directdownload.DirectDownloadAnswer in project cloudstack by apache.

the class DirectDownloadManagerImpl method sendDirectDownloadCommand.

/**
 * Send direct download command for downloading template with ID templateId on storage pool with ID poolId.<br/>
 * At first, cmd is sent to host, in case of failure it will retry on other hosts before failing
 * @param cmd direct download command
 * @param template template
 * @param poolId pool id
 * @param host first host to which send the command
 * @return download answer from any host which could handle cmd
 */
private Answer sendDirectDownloadCommand(DirectDownloadCommand cmd, VMTemplateVO template, long poolId, HostVO host) {
    boolean downloaded = false;
    int retry = 3;
    StoragePoolVO storagePoolVO = primaryDataStoreDao.findById(poolId);
    // TODO: Move the host retry attempts to upper layer
    Long[] hostsToRetry = getHostsToRetryOn(host, storagePoolVO);
    int hostIndex = 0;
    Answer answer = null;
    Long hostToSendDownloadCmd = hostsToRetry[hostIndex];
    boolean continueRetrying = true;
    while (!downloaded && retry > 0 && continueRetrying) {
        PrimaryDataStore primaryDataStore = null;
        TemplateInfo templateOnPrimary = null;
        try {
            if (hostToSendDownloadCmd != host.getId() && storagePoolVO.isManaged()) {
                primaryDataStore = (PrimaryDataStore) dataStoreManager.getPrimaryDataStore(poolId);
                templateOnPrimary = primaryDataStore.getTemplate(template.getId(), null);
                if (templateOnPrimary != null) {
                    volService.grantAccess(templateOnPrimary, host, primaryDataStore);
                }
            }
            s_logger.debug("Sending Direct download command to host " + hostToSendDownloadCmd);
            answer = agentManager.easySend(hostToSendDownloadCmd, cmd);
            if (answer != null) {
                DirectDownloadAnswer ans = (DirectDownloadAnswer) answer;
                downloaded = answer.getResult();
                continueRetrying = ans.isRetryOnOtherHosts();
            }
            hostToSendDownloadCmd = hostsToRetry[(hostIndex + 1) % hostsToRetry.length];
        } finally {
            if (templateOnPrimary != null) {
                volService.revokeAccess(templateOnPrimary, host, primaryDataStore);
            }
        }
        retry--;
    }
    if (!downloaded) {
        logUsageEvent(template, poolId);
        throw new CloudRuntimeException("Template " + template.getId() + " could not be downloaded on pool " + poolId + ", failing after trying on several hosts");
    }
    return answer;
}
Also used : Answer(com.cloud.agent.api.Answer) DirectDownloadAnswer(org.apache.cloudstack.agent.directdownload.DirectDownloadAnswer) TemplateInfo(org.apache.cloudstack.engine.subsystem.api.storage.TemplateInfo) DirectDownloadAnswer(org.apache.cloudstack.agent.directdownload.DirectDownloadAnswer) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VMTemplateStoragePoolVO(com.cloud.storage.VMTemplateStoragePoolVO) StoragePoolVO(org.apache.cloudstack.storage.datastore.db.StoragePoolVO) PrimaryDataStore(org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStore)

Example 3 with DirectDownloadAnswer

use of org.apache.cloudstack.agent.directdownload.DirectDownloadAnswer in project cloudstack by apache.

the class DirectDownloadManagerImpl method downloadTemplate.

@Override
public void downloadTemplate(long templateId, long poolId, long hostId) {
    VMTemplateVO template = vmTemplateDao.findById(templateId);
    StoragePoolVO pool = primaryDataStoreDao.findById(poolId);
    HostVO host = hostDao.findById(hostId);
    if (pool == null) {
        throw new CloudRuntimeException("Storage pool " + poolId + " could not be found");
    }
    if (template == null) {
        throw new CloudRuntimeException("Template " + templateId + " could not be found");
    }
    if (host == null) {
        throw new CloudRuntimeException("Host " + hostId + " could not be found");
    }
    if (!template.isDirectDownload()) {
        throw new CloudRuntimeException("Template " + templateId + " is not marked for direct download");
    }
    Map<String, String> details = template.getDetails();
    String url = template.getUrl();
    String checksum = template.getChecksum();
    Map<String, String> headers = getHeadersFromDetails(details);
    DataStore store = dataStoreManager.getDataStore(poolId, DataStoreRole.Primary);
    if (store == null) {
        throw new CloudRuntimeException("Data store " + poolId + " could not be found");
    }
    PrimaryDataStore primaryDataStore = (PrimaryDataStore) store;
    PrimaryDataStoreTO to = (PrimaryDataStoreTO) primaryDataStore.getTO();
    DownloadProtocol protocol = getProtocolFromUrl(url);
    DirectDownloadCommand cmd = getDirectDownloadCommandFromProtocol(protocol, url, templateId, to, checksum, headers);
    cmd.setTemplateSize(template.getSize());
    cmd.setFormat(template.getFormat());
    if (tmplFactory.getTemplate(templateId, store) != null) {
        cmd.setDestData((TemplateObjectTO) tmplFactory.getTemplate(templateId, store).getTO());
    }
    int cmdTimeOut = StorageManager.PRIMARY_STORAGE_DOWNLOAD_WAIT.value();
    cmd.setWait(cmdTimeOut);
    Answer answer = sendDirectDownloadCommand(cmd, template, poolId, host);
    VMTemplateStoragePoolVO sPoolRef = vmTemplatePoolDao.findByPoolTemplate(poolId, templateId, null);
    if (sPoolRef == null) {
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("Not found (templateId:" + templateId + " poolId: " + poolId + ") in template_spool_ref, persisting it");
        }
        DirectDownloadAnswer ans = (DirectDownloadAnswer) answer;
        sPoolRef = new VMTemplateStoragePoolVO(poolId, templateId, null);
        sPoolRef.setDownloadPercent(100);
        sPoolRef.setDownloadState(VMTemplateStorageResourceAssoc.Status.DOWNLOADED);
        sPoolRef.setState(ObjectInDataStoreStateMachine.State.Ready);
        sPoolRef.setTemplateSize(ans.getTemplateSize());
        sPoolRef.setLocalDownloadPath(ans.getInstallPath());
        sPoolRef.setInstallPath(ans.getInstallPath());
        vmTemplatePoolDao.persist(sPoolRef);
    } else {
        // For managed storage, update after template downloaded and copied to the disk
        DirectDownloadAnswer ans = (DirectDownloadAnswer) answer;
        sPoolRef.setDownloadPercent(100);
        sPoolRef.setDownloadState(VMTemplateStorageResourceAssoc.Status.DOWNLOADED);
        sPoolRef.setState(ObjectInDataStoreStateMachine.State.Ready);
        sPoolRef.setTemplateSize(ans.getTemplateSize());
        sPoolRef.setLocalDownloadPath(ans.getInstallPath());
        sPoolRef.setInstallPath(ans.getInstallPath());
        vmTemplatePoolDao.update(sPoolRef.getId(), sPoolRef);
    }
}
Also used : DirectDownloadCommand(org.apache.cloudstack.agent.directdownload.DirectDownloadCommand) HttpDirectDownloadCommand(org.apache.cloudstack.agent.directdownload.HttpDirectDownloadCommand) MetalinkDirectDownloadCommand(org.apache.cloudstack.agent.directdownload.MetalinkDirectDownloadCommand) HttpsDirectDownloadCommand(org.apache.cloudstack.agent.directdownload.HttpsDirectDownloadCommand) NfsDirectDownloadCommand(org.apache.cloudstack.agent.directdownload.NfsDirectDownloadCommand) VMTemplateVO(com.cloud.storage.VMTemplateVO) HostVO(com.cloud.host.HostVO) VMTemplateStoragePoolVO(com.cloud.storage.VMTemplateStoragePoolVO) Answer(com.cloud.agent.api.Answer) DirectDownloadAnswer(org.apache.cloudstack.agent.directdownload.DirectDownloadAnswer) PrimaryDataStoreTO(org.apache.cloudstack.storage.to.PrimaryDataStoreTO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) DownloadProtocol(org.apache.cloudstack.agent.directdownload.DirectDownloadCommand.DownloadProtocol) DirectDownloadAnswer(org.apache.cloudstack.agent.directdownload.DirectDownloadAnswer) DataStore(org.apache.cloudstack.engine.subsystem.api.storage.DataStore) PrimaryDataStore(org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStore) VMTemplateStoragePoolVO(com.cloud.storage.VMTemplateStoragePoolVO) StoragePoolVO(org.apache.cloudstack.storage.datastore.db.StoragePoolVO) PrimaryDataStore(org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStore)

Aggregations

CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)3 DirectDownloadAnswer (org.apache.cloudstack.agent.directdownload.DirectDownloadAnswer)3 Answer (com.cloud.agent.api.Answer)2 VMTemplateStoragePoolVO (com.cloud.storage.VMTemplateStoragePoolVO)2 PrimaryDataStore (org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStore)2 StoragePoolVO (org.apache.cloudstack.storage.datastore.db.StoragePoolVO)2 PrimaryDataStoreTO (org.apache.cloudstack.storage.to.PrimaryDataStoreTO)2 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 HostVO (com.cloud.host.HostVO)1 VMTemplateVO (com.cloud.storage.VMTemplateVO)1 DirectDownloadCommand (org.apache.cloudstack.agent.directdownload.DirectDownloadCommand)1 DownloadProtocol (org.apache.cloudstack.agent.directdownload.DirectDownloadCommand.DownloadProtocol)1 HttpDirectDownloadCommand (org.apache.cloudstack.agent.directdownload.HttpDirectDownloadCommand)1 HttpsDirectDownloadCommand (org.apache.cloudstack.agent.directdownload.HttpsDirectDownloadCommand)1 MetalinkDirectDownloadCommand (org.apache.cloudstack.agent.directdownload.MetalinkDirectDownloadCommand)1 NfsDirectDownloadCommand (org.apache.cloudstack.agent.directdownload.NfsDirectDownloadCommand)1