From 23b1c4ddf625b41f2ae14dc7333b42d046f9abae Mon Sep 17 00:00:00 2001 From: Daan Hoogland Date: Tue, 9 Jan 2018 15:10:09 +0100 Subject: [PATCH] backport of master fixes --- .../DirectTemplateDownloaderImpl.java | 3 -- .../HttpDirectTemplateDownloader.java | 45 +++++++------------ .../HttpsDirectTemplateDownloader.java | 15 ++----- .../download/NfsDirectTemplateDownloader.java | 18 +++----- .../template/RegisterTemplateCmdByAdmin.java | 2 +- .../user/template/RegisterTemplateCmd.java | 4 +- engine/orchestration/pom.xml | 7 --- 7 files changed, 29 insertions(+), 65 deletions(-) diff --git a/agent/src/com/cloud/agent/direct/download/DirectTemplateDownloaderImpl.java b/agent/src/com/cloud/agent/direct/download/DirectTemplateDownloaderImpl.java index 802f88e8cf7..708a52cb1ff 100644 --- a/agent/src/com/cloud/agent/direct/download/DirectTemplateDownloaderImpl.java +++ b/agent/src/com/cloud/agent/direct/download/DirectTemplateDownloaderImpl.java @@ -46,8 +46,6 @@ public abstract class DirectTemplateDownloaderImpl implements DirectTemplateDown /** * Return direct download temporary path to download template - * @param templateId - * @return */ protected static String getDirectDownloadTempPath(Long templateId) { String templateIdAsString = String.valueOf(templateId); @@ -57,7 +55,6 @@ public abstract class DirectTemplateDownloaderImpl implements DirectTemplateDown /** * Create folder on path if it does not exist - * @param path */ protected void createFolder(String path) { File dir = new File(path); diff --git a/agent/src/com/cloud/agent/direct/download/HttpDirectTemplateDownloader.java b/agent/src/com/cloud/agent/direct/download/HttpDirectTemplateDownloader.java index 2e92521e88e..554d196a903 100644 --- a/agent/src/com/cloud/agent/direct/download/HttpDirectTemplateDownloader.java +++ b/agent/src/com/cloud/agent/direct/download/HttpDirectTemplateDownloader.java @@ -19,6 +19,7 @@ package com.cloud.agent.direct.download; +import com.cloud.utils.exception.CloudRuntimeException; import org.apache.commons.collections.MapUtils; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpMethod; @@ -27,11 +28,14 @@ import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager; import org.apache.commons.httpclient.NoHttpResponseException; import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.params.HttpMethodParams; +import org.apache.commons.io.IOUtils; import org.apache.log4j.Logger; import java.io.File; -import java.io.IOException; +import java.io.FileOutputStream; import java.io.InputStream; +import java.io.IOException; +import java.io.OutputStream; import java.io.RandomAccessFile; import java.util.Map; @@ -96,41 +100,24 @@ public class HttpDirectTemplateDownloader extends DirectTemplateDownloaderImpl { @Override public boolean downloadTemplate() { - File f = new File(getDownloadedFilePath()); + try { + client.executeMethod(request); + } catch (IOException e) { + throw new CloudRuntimeException("Error on HTTP request: " + e.getMessage()); + } + return performDownload(); + } + + protected boolean performDownload() { try ( InputStream in = request.getResponseBodyAsStream(); - RandomAccessFile out = new RandomAccessFile(f, "rw"); + OutputStream out = new FileOutputStream(getDownloadedFilePath()); ) { - client.executeMethod(request); - out.seek(0); - copyBytes(in, out); + IOUtils.copy(in, out); } catch (IOException e) { s_logger.error("Error downloading template " + getTemplateId() + e.getMessage()); return false; } return true; } - - protected boolean copyBytes(InputStream in, RandomAccessFile out) throws IOException { - int bytes; - byte[] block = new byte[CHUNK_SIZE]; - long offset = 0; - boolean done = false; - while (!done) { - if ((bytes = in.read(block, 0, CHUNK_SIZE)) > -1) { - offset = writeBlock(bytes, out, block, offset); - } else { - done = true; - } - } - out.getFD().sync(); - return false; - } - - protected long writeBlock(int bytes, RandomAccessFile out, byte[] block, long offset) throws IOException { - out.write(block, 0, bytes); - offset += bytes; - out.seek(offset); - return offset; - } } \ No newline at end of file diff --git a/agent/src/com/cloud/agent/direct/download/HttpsDirectTemplateDownloader.java b/agent/src/com/cloud/agent/direct/download/HttpsDirectTemplateDownloader.java index 88f56dc6b1f..fb54238df12 100644 --- a/agent/src/com/cloud/agent/direct/download/HttpsDirectTemplateDownloader.java +++ b/agent/src/com/cloud/agent/direct/download/HttpsDirectTemplateDownloader.java @@ -33,8 +33,6 @@ import javax.net.ssl.SSLContext; import java.io.File; import java.io.FileInputStream; import java.io.IOException; -import java.io.InputStream; -import java.io.RandomAccessFile; import java.security.KeyManagementException; import java.security.KeyStore; import java.security.KeyStoreException; @@ -81,18 +79,11 @@ public class HttpsDirectTemplateDownloader extends HttpDirectTemplateDownloader @Override public boolean downloadTemplate() { - File f = new File(getDownloadedFilePath()); - try ( - InputStream in = request.getResponseBodyAsStream(); - RandomAccessFile out = new RandomAccessFile(f, "rw"); - ) { + try { httpsClient.execute(req); - out.seek(0); - copyBytes(in, out); } catch (IOException e) { - s_logger.error("Error downloading template " + getTemplateId() + e.getMessage()); - return false; + throw new CloudRuntimeException("Error on HTTPS request: " + e.getMessage()); } - return true; + return performDownload(); } } diff --git a/agent/src/com/cloud/agent/direct/download/NfsDirectTemplateDownloader.java b/agent/src/com/cloud/agent/direct/download/NfsDirectTemplateDownloader.java index 2b5e7cb7fb5..a031068f540 100644 --- a/agent/src/com/cloud/agent/direct/download/NfsDirectTemplateDownloader.java +++ b/agent/src/com/cloud/agent/direct/download/NfsDirectTemplateDownloader.java @@ -58,17 +58,13 @@ public class NfsDirectTemplateDownloader extends DirectTemplateDownloaderImpl { @Override public boolean downloadTemplate() { - try { - String mountSrcUuid = UUID.randomUUID().toString(); - String mount = String.format(mountCommand, srcHost + ":" + srcPath, "/mnt/" + mountSrcUuid); - Script.runSimpleBashScript(mount); - String downloadDir = getDestPoolPath() + File.separator + getDirectDownloadTempPath(getTemplateId()); - setDownloadedFilePath(downloadDir + File.separator + getFileNameFromUrl()); - Script.runSimpleBashScript("cp /mnt/" + mountSrcUuid + srcPath + " " + getDownloadedFilePath()); - Script.runSimpleBashScript("umount /mnt/" + mountSrcUuid); - } catch (Exception e) { - return false; - } + String mountSrcUuid = UUID.randomUUID().toString(); + String mount = String.format(mountCommand, srcHost + ":" + srcPath, "/mnt/" + mountSrcUuid); + Script.runSimpleBashScript(mount); + String downloadDir = getDestPoolPath() + File.separator + getDirectDownloadTempPath(getTemplateId()); + setDownloadedFilePath(downloadDir + File.separator + getFileNameFromUrl()); + Script.runSimpleBashScript("cp /mnt/" + mountSrcUuid + srcPath + " " + getDownloadedFilePath()); + Script.runSimpleBashScript("umount /mnt/" + mountSrcUuid); return true; } } \ No newline at end of file diff --git a/api/src/org/apache/cloudstack/api/command/admin/template/RegisterTemplateCmdByAdmin.java b/api/src/org/apache/cloudstack/api/command/admin/template/RegisterTemplateCmdByAdmin.java index c93fd368109..ba42b168d4f 100644 --- a/api/src/org/apache/cloudstack/api/command/admin/template/RegisterTemplateCmdByAdmin.java +++ b/api/src/org/apache/cloudstack/api/command/admin/template/RegisterTemplateCmdByAdmin.java @@ -40,7 +40,7 @@ public class RegisterTemplateCmdByAdmin extends RegisterTemplateCmd { @Override public void execute() throws ResourceAllocationException{ - if (isDirectDownload() && !hypervisor.equals(Hypervisor.HypervisorType.KVM.toString())) { + if (isDirectDownload() && !getHypervisor().equals(Hypervisor.HypervisorType.KVM.toString())) { throw new ServerApiException(ApiErrorCode.PARAM_ERROR, "Parameter directdownload is only allowed for KVM templates"); } try { diff --git a/api/src/org/apache/cloudstack/api/command/user/template/RegisterTemplateCmd.java b/api/src/org/apache/cloudstack/api/command/user/template/RegisterTemplateCmd.java index ce1a9e1247f..c9a02306a29 100644 --- a/api/src/org/apache/cloudstack/api/command/user/template/RegisterTemplateCmd.java +++ b/api/src/org/apache/cloudstack/api/command/user/template/RegisterTemplateCmd.java @@ -70,7 +70,7 @@ public class RegisterTemplateCmd extends BaseCmd { private String format; @Parameter(name = ApiConstants.HYPERVISOR, type = CommandType.STRING, required = true, description = "the target hypervisor for the template") - protected String hypervisor; + private String hypervisor; @Parameter(name = ApiConstants.IS_FEATURED, type = CommandType.BOOLEAN, description = "true if this template is a featured template, false otherwise") private Boolean featured; @@ -269,7 +269,7 @@ public class RegisterTemplateCmd extends BaseCmd { @Override public void execute() throws ResourceAllocationException { - if (isDirectDownload() && !hypervisor.equals(Hypervisor.HypervisorType.KVM.toString())) { + if (isDirectDownload() && !getHypervisor().equals(Hypervisor.HypervisorType.KVM.toString())) { throw new ServerApiException(ApiErrorCode.PARAM_ERROR, "Parameter directdownload is only allowed for KVM templates"); } try { diff --git a/engine/orchestration/pom.xml b/engine/orchestration/pom.xml index 23c0d1c5873..3322fd103ad 100755 --- a/engine/orchestration/pom.xml +++ b/engine/orchestration/pom.xml @@ -58,13 +58,6 @@ cloud-utils ${project.version} -