Search in sources :

Example 6 with DownloadAnswer

use of com.cloud.agent.api.storage.DownloadAnswer in project cloudstack by apache.

the class NfsSecondaryStorageResource method registerTemplateOnSwift.

protected Answer registerTemplateOnSwift(DownloadCommand cmd) {
    SwiftTO swiftTO = (SwiftTO) cmd.getDataStore();
    String path = cmd.getInstallPath();
    DataStoreTO cacheStore = cmd.getCacheStore();
    if (cacheStore == null || !(cacheStore instanceof NfsTO)) {
        return new DownloadAnswer("cache store can't be null", VMTemplateStorageResourceAssoc.Status.DOWNLOAD_ERROR);
    }
    File file = null;
    try {
        NfsTO nfsCacheStore = (NfsTO) cacheStore;
        String fileName = cmd.getName() + "." + cmd.getFormat().getFileExtension();
        file = downloadFromUrlToNfs(cmd.getUrl(), nfsCacheStore, path, fileName);
        String container = "T-" + cmd.getId();
        String swiftPath = SwiftUtil.putObject(swiftTO, file, container, null);
        long virtualSize = getVirtualSize(file, getTemplateFormat(file.getName()));
        long size = file.length();
        String uniqueName = cmd.getName();
        //put metda file
        File uniqDir = _storage.createUniqDir();
        String metaFileName = uniqDir.getAbsolutePath() + File.separator + _tmpltpp;
        _storage.create(uniqDir.getAbsolutePath(), _tmpltpp);
        File metaFile = swiftWriteMetadataFile(metaFileName, uniqueName, fileName, size, virtualSize);
        SwiftUtil.putObject(swiftTO, metaFile, container, _tmpltpp);
        metaFile.delete();
        uniqDir.delete();
        String md5sum = null;
        try (FileInputStream fs = new FileInputStream(file)) {
            md5sum = DigestUtils.md5Hex(fs);
        } catch (IOException e) {
            s_logger.debug("Failed to get md5sum: " + file.getAbsoluteFile());
        }
        DownloadAnswer answer = new DownloadAnswer(null, 100, null, VMTemplateStorageResourceAssoc.Status.DOWNLOADED, swiftPath, swiftPath, virtualSize, file.length(), md5sum);
        return answer;
    } catch (IOException e) {
        s_logger.debug("Failed to register template into swift", e);
        return new DownloadAnswer(e.toString(), VMTemplateStorageResourceAssoc.Status.DOWNLOAD_ERROR);
    } finally {
        if (file != null) {
            file.delete();
        }
    }
}
Also used : DataStoreTO(com.cloud.agent.api.to.DataStoreTO) SwiftTO(com.cloud.agent.api.to.SwiftTO) IOException(java.io.IOException) DownloadAnswer(com.cloud.agent.api.storage.DownloadAnswer) NfsTO(com.cloud.agent.api.to.NfsTO) File(java.io.File) S3Utils.putFile(com.cloud.utils.storage.S3.S3Utils.putFile) FileInputStream(java.io.FileInputStream)

Example 7 with DownloadAnswer

use of com.cloud.agent.api.storage.DownloadAnswer in project cloudstack by apache.

the class DownloadState method handleEvent.

public String handleEvent(DownloadEvent event, Object eventObj) {
    if (s_logger.isTraceEnabled()) {
        getDownloadListener().log("handleEvent, event type=" + event + ", curr state=" + getName(), Level.TRACE);
    }
    switch(event) {
        case DOWNLOAD_ANSWER:
            DownloadAnswer answer = (DownloadAnswer) eventObj;
            return handleAnswer(answer);
        case ABANDON_DOWNLOAD:
            return handleAbort();
        case TIMEOUT_CHECK:
            Date now = new Date();
            long update = now.getTime() - dl.getLastUpdated().getTime();
            return handleTimeout(update);
        case DISCONNECT:
            return handleDisconnect();
    }
    return null;
}
Also used : DownloadAnswer(com.cloud.agent.api.storage.DownloadAnswer) Date(java.util.Date)

Example 8 with DownloadAnswer

use of com.cloud.agent.api.storage.DownloadAnswer in project cloudstack by apache.

the class DownloadAbandonedState method onEntry.

@Override
public void onEntry(String prevState, DownloadEvent event, Object evtObj) {
    super.onEntry(prevState, event, evtObj);
    if (!prevState.equalsIgnoreCase(getName())) {
        DownloadAnswer answer = new DownloadAnswer("Download canceled", Status.ABANDONED);
        getDownloadListener().callback(answer);
        getDownloadListener().cancelStatusTask();
        getDownloadListener().cancelTimeoutTask();
        getDownloadListener().sendCommand(RequestType.ABORT);
    }
}
Also used : DownloadAnswer(com.cloud.agent.api.storage.DownloadAnswer)

Example 9 with DownloadAnswer

use of com.cloud.agent.api.storage.DownloadAnswer 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)

Example 10 with DownloadAnswer

use of com.cloud.agent.api.storage.DownloadAnswer in project cloudstack by apache.

the class DownloadManagerImpl method handleDownloadProgressCmd.

private DownloadAnswer handleDownloadProgressCmd(SecondaryStorageResource resource, DownloadProgressCommand cmd) {
    String jobId = cmd.getJobId();
    DownloadAnswer answer;
    DownloadJob dj = null;
    if (jobId != null) {
        dj = jobs.get(jobId);
    }
    if (dj == null) {
        if (cmd.getRequest() == RequestType.GET_OR_RESTART) {
            DownloadCommand dcmd = new DownloadCommand(cmd);
            return handleDownloadCommand(resource, dcmd);
        } else {
            return new DownloadAnswer("Cannot find job", com.cloud.storage.VMTemplateStorageResourceAssoc.Status.UNKNOWN);
        }
    }
    TemplateDownloader td = dj.getTemplateDownloader();
    switch(cmd.getRequest()) {
        case GET_STATUS:
            break;
        case ABORT:
            td.stopDownload();
            sleep();
            break;
        case RESTART:
            td.stopDownload();
            sleep();
            threadPool.execute(td);
            break;
        case PURGE:
            td.stopDownload();
            answer = new DownloadAnswer(jobId, getDownloadPct(jobId), getDownloadError(jobId), getDownloadStatus2(jobId), getDownloadLocalPath(jobId), getInstallPath(jobId), getDownloadTemplateSize(jobId), getDownloadTemplatePhysicalSize(jobId), getDownloadCheckSum(jobId));
            jobs.remove(jobId);
            return answer;
        default:
            // TODO
            break;
    }
    return new DownloadAnswer(jobId, getDownloadPct(jobId), getDownloadError(jobId), getDownloadStatus2(jobId), getDownloadLocalPath(jobId), getInstallPath(jobId), getDownloadTemplateSize(jobId), getDownloadTemplatePhysicalSize(jobId), getDownloadCheckSum(jobId));
}
Also used : DownloadCommand(org.apache.cloudstack.storage.command.DownloadCommand) 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) DownloadAnswer(com.cloud.agent.api.storage.DownloadAnswer)

Aggregations

DownloadAnswer (com.cloud.agent.api.storage.DownloadAnswer)21 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)5 CreateCmdResult (org.apache.cloudstack.engine.subsystem.api.storage.CreateCmdResult)5 PrimaryStorageDownloadAnswer (com.cloud.agent.api.storage.PrimaryStorageDownloadAnswer)4 NfsTO (com.cloud.agent.api.to.NfsTO)4 MockVolumeVO (com.cloud.simulator.MockVolumeVO)4 DataStore (org.apache.cloudstack.engine.subsystem.api.storage.DataStore)4 DownloadCommand (org.apache.cloudstack.storage.command.DownloadCommand)4 VolumeVO (com.cloud.storage.VolumeVO)3 Date (java.util.Date)3 CopyCmdAnswer (org.apache.cloudstack.storage.command.CopyCmdAnswer)3 VolumeDataStoreVO (org.apache.cloudstack.storage.datastore.db.VolumeDataStoreVO)3 DataStoreTO (com.cloud.agent.api.to.DataStoreTO)2 SwiftTO (com.cloud.agent.api.to.SwiftTO)2 MockSecStorageVO (com.cloud.simulator.MockSecStorageVO)2 VirtualMachineTemplate (com.cloud.template.VirtualMachineTemplate)2 TransactionLegacy (com.cloud.utils.db.TransactionLegacy)2 URISyntaxException (java.net.URISyntaxException)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 ConfigurationException (javax.naming.ConfigurationException)2