Search in sources :

Example 1 with ScpTemplateDownloader

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

the class DownloadManagerImpl method downloadPublicTemplate.

@Override
public String downloadPublicTemplate(long id, String url, String name, ImageFormat format, boolean hvm, Long accountId, String descr, String cksum, String installPathPrefix, String templatePath, String user, String password, long maxTemplateSizeInBytes, Proxy proxy, ResourceType resourceType) {
    UUID uuid = UUID.randomUUID();
    String jobId = uuid.toString();
    String tmpDir = installPathPrefix;
    try {
        if (!_storage.mkdirs(tmpDir)) {
            s_logger.warn("Unable to create " + tmpDir);
            return "Unable to create " + tmpDir;
        }
        // TO DO - define constant for volume properties.
        File file = ResourceType.TEMPLATE == resourceType ? _storage.getFile(tmpDir + File.separator + TemplateLocation.Filename) : _storage.getFile(tmpDir + File.separator + "volume.properties");
        if (file.exists()) {
            if (!file.delete()) {
                s_logger.warn("Deletion of file '" + file.getAbsolutePath() + "' failed.");
            }
        }
        if (!file.createNewFile()) {
            s_logger.warn("Unable to create new file: " + file.getAbsolutePath());
            return "Unable to create new file: " + file.getAbsolutePath();
        }
        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 HttpTemplateDownloader(_storage, url, tmpDir, new Completion(jobId), maxTemplateSizeInBytes, user, password, proxy, resourceType);
            } else if (uri.getScheme().equalsIgnoreCase("file")) {
                td = new LocalTemplateDownloader(_storage, url, tmpDir, maxTemplateSizeInBytes, new Completion(jobId));
            } else if (uri.getScheme().equalsIgnoreCase("scp")) {
                td = new ScpTemplateDownloader(_storage, url, tmpDir, maxTemplateSizeInBytes, new Completion(jobId));
            } else if (uri.getScheme().equalsIgnoreCase("nfs") || uri.getScheme().equalsIgnoreCase("cifs")) {
                td = null;
                // TODO: implement this.
                throw new CloudRuntimeException("Scheme is not supported " + url);
            } else {
                throw new CloudRuntimeException("Scheme is not supported " + url);
            }
        } else {
            throw new CloudRuntimeException("Unable to download from URL: " + url);
        }
        // NOTE the difference between installPathPrefix and templatePath
        // here. instalPathPrefix is the absolute path for template
        // including mount directory
        // on ssvm, while templatePath is the final relative path on
        // secondary storage.
        DownloadJob dj = new DownloadJob(td, jobId, id, name, format, hvm, accountId, descr, cksum, installPathPrefix, resourceType);
        dj.setTmpltPath(templatePath);
        jobs.put(jobId, dj);
        threadPool.execute(td);
        return jobId;
    } catch (IOException e) {
        s_logger.warn("Unable to download to " + tmpDir, e);
        return null;
    }
}
Also used : HttpTemplateDownloader(com.cloud.storage.template.HttpTemplateDownloader) LocalTemplateDownloader(com.cloud.storage.template.LocalTemplateDownloader) 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) IOException(java.io.IOException) URI(java.net.URI) ScpTemplateDownloader(com.cloud.storage.template.ScpTemplateDownloader) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) UUID(java.util.UUID) File(java.io.File)

Aggregations

HttpTemplateDownloader (com.cloud.storage.template.HttpTemplateDownloader)1 LocalTemplateDownloader (com.cloud.storage.template.LocalTemplateDownloader)1 S3TemplateDownloader (com.cloud.storage.template.S3TemplateDownloader)1 ScpTemplateDownloader (com.cloud.storage.template.ScpTemplateDownloader)1 TemplateDownloader (com.cloud.storage.template.TemplateDownloader)1 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)1 File (java.io.File)1 IOException (java.io.IOException)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 UUID (java.util.UUID)1