Search in sources :

Example 1 with S3TemplateDownloader

use of com.cloud.storage.template.S3TemplateDownloader in project cloudstack by apache.

the class DownloadManagerImpl method setDownloadStatus.

/**
     * Get notified of change of job status. Executed in context of downloader
     * thread
     *
     * @param jobId
     *            the id of the job
     * @param status
     *            the status of the job
     */
public void setDownloadStatus(String jobId, Status status) {
    DownloadJob dj = jobs.get(jobId);
    if (dj == null) {
        s_logger.warn("setDownloadStatus for jobId: " + jobId + ", status=" + status + " no job found");
        return;
    }
    TemplateDownloader td = dj.getTemplateDownloader();
    s_logger.info("Download Completion for jobId: " + jobId + ", status=" + status);
    s_logger.info("local: " + td.getDownloadLocalPath() + ", bytes=" + td.getDownloadedBytes() + ", error=" + td.getDownloadError() + ", pct=" + td.getDownloadPercent());
    switch(status) {
        case ABORTED:
        case NOT_STARTED:
        case UNRECOVERABLE_ERROR:
            // TODO
            dj.cleanup();
            break;
        case UNKNOWN:
            return;
        case IN_PROGRESS:
            s_logger.info("Resuming jobId: " + jobId + ", status=" + status);
            td.setResume(true);
            threadPool.execute(td);
            break;
        case RECOVERABLE_ERROR:
            threadPool.execute(td);
            break;
        case DOWNLOAD_FINISHED:
            if (td instanceof S3TemplateDownloader) {
                // For S3 and Swift, which are considered "remote",
                // as in the file cannot be accessed locally,
                // we run the postRemoteDownload() method.
                td.setDownloadError("Download success, starting install ");
                String result = postRemoteDownload(jobId);
                if (result != null) {
                    s_logger.error("Failed post download install: " + result);
                    td.setStatus(Status.UNRECOVERABLE_ERROR);
                    td.setDownloadError("Failed post download install: " + result);
                    ((S3TemplateDownloader) td).cleanupAfterError();
                } else {
                    td.setStatus(Status.POST_DOWNLOAD_FINISHED);
                    td.setDownloadError("Install completed successfully at " + new SimpleDateFormat().format(new Date()));
                }
            } else {
                // For other TemplateDownloaders where files are locally available,
                // we run the postLocalDownload() method.
                td.setDownloadError("Download success, starting install ");
                String result = postLocalDownload(jobId);
                if (result != null) {
                    s_logger.error("Failed post download script: " + result);
                    td.setStatus(Status.UNRECOVERABLE_ERROR);
                    td.setDownloadError("Failed post download script: " + result);
                } else {
                    td.setStatus(Status.POST_DOWNLOAD_FINISHED);
                    td.setDownloadError("Install completed successfully at " + new SimpleDateFormat().format(new Date()));
                }
            }
            dj.cleanup();
            break;
        default:
            break;
    }
}
Also used : 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) S3TemplateDownloader(com.cloud.storage.template.S3TemplateDownloader) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Example 2 with S3TemplateDownloader

use of com.cloud.storage.template.S3TemplateDownloader in project cloudstack by apache.

the class DownloadManagerImpl method downloadS3Template.

@Override
public String downloadS3Template(S3TO s3, long id, String url, String name, ImageFormat format, boolean hvm, Long accountId, String descr, String cksum, String installPathPrefix, String user, String password, long maxTemplateSizeInBytes, Proxy proxy, ResourceType resourceType) {
    UUID uuid = UUID.randomUUID();
    String jobId = uuid.toString();
    URI uri;
    try {
        uri = new URI(url);
    } catch (URISyntaxException e) {
        throw new CloudRuntimeException("URI is incorrect: " + url);
    }
    TemplateDownloader td;
    if ((uri != null) && (uri.getScheme() != null)) {
        if (uri.getScheme().equalsIgnoreCase("http") || uri.getScheme().equalsIgnoreCase("https")) {
            td = new S3TemplateDownloader(s3, url, installPathPrefix, new Completion(jobId), maxTemplateSizeInBytes, user, password, proxy, resourceType);
        } else {
            throw new CloudRuntimeException("Scheme is not supported " + url);
        }
    } else {
        throw new CloudRuntimeException("Unable to download from URL: " + url);
    }
    DownloadJob dj = new DownloadJob(td, jobId, id, name, format, hvm, accountId, descr, cksum, installPathPrefix, resourceType);
    dj.setTmpltPath(installPathPrefix);
    jobs.put(jobId, dj);
    threadPool.execute(td);
    return jobId;
}
Also used : CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) 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) URISyntaxException(java.net.URISyntaxException) UUID(java.util.UUID) S3TemplateDownloader(com.cloud.storage.template.S3TemplateDownloader) URI(java.net.URI)

Example 3 with S3TemplateDownloader

use of com.cloud.storage.template.S3TemplateDownloader in project cloudstack by apache.

the class DownloadManagerImpl method postRemoteDownload.

/**
     * Post remote download activity (install and cleanup). Executed in context of the downloader thread.
     */
private String postRemoteDownload(String jobId) {
    String result = null;
    DownloadJob dnld = jobs.get(jobId);
    S3TemplateDownloader td = (S3TemplateDownloader) dnld.getTemplateDownloader();
    if (td.getFileExtension().equalsIgnoreCase("QCOW2")) {
        try {
            InputStream inputStream = td.getS3ObjectInputStream();
            dnld.setTemplatesize(QCOW2Utils.getVirtualSize(inputStream));
            inputStream.close();
        } catch (IOException e) {
            result = "Couldn't read QCOW2 virtual size. Error: " + e.getMessage();
        }
    } else {
        // For the other formats, both the virtual
        // and actual file size are set the same.
        dnld.setTemplatesize(td.getTotalBytes());
    }
    dnld.setTemplatePhysicalSize(td.getTotalBytes());
    dnld.setTmpltPath(td.getDownloadLocalPath());
    return result;
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) S3TemplateDownloader(com.cloud.storage.template.S3TemplateDownloader)

Aggregations

S3TemplateDownloader (com.cloud.storage.template.S3TemplateDownloader)3 HttpTemplateDownloader (com.cloud.storage.template.HttpTemplateDownloader)2 LocalTemplateDownloader (com.cloud.storage.template.LocalTemplateDownloader)2 ScpTemplateDownloader (com.cloud.storage.template.ScpTemplateDownloader)2 TemplateDownloader (com.cloud.storage.template.TemplateDownloader)2 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 UUID (java.util.UUID)1