backport of master fixes

This commit is contained in:
Daan Hoogland 2018-01-09 15:10:09 +01:00
parent 0a68b73ad0
commit 23b1c4ddf6
7 changed files with 29 additions and 65 deletions

View File

@ -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);

View File

@ -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;
}
}

View File

@ -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();
}
}

View File

@ -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;
}
}

View File

@ -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 {

View File

@ -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 {

View File

@ -58,13 +58,6 @@
<artifactId>cloud-utils</artifactId>
<version>${project.version}</version>
</dependency>
<!--
<dependency>
<groupId>org.apache.cloudstack</groupId>
<artifactId>cloud-server</artifactId>
<version>${project.version}</version>
</dependency>
-->
</dependencies>
<build>
<plugins>