Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,41 @@ public MetalinkTemplateDownloader(StorageLayer storageLayer, String downloadUrl,
super(storageLayer, downloadUrl, toDir, maxTemplateSize, callback);
String[] parts = _downloadUrl.split("/");
String filename = parts[parts.length - 1];
_callback = callback;
_toFile = toDir + File.separator + filename;
}

@Override
public long download(boolean resume, DownloadCompleteCallback callback) {
if (!status.equals(Status.NOT_STARTED)) {
// Only start downloading if we haven't started yet.
LOGGER.debug("Template download is already started, not starting again. Template: " + _downloadUrl);
if (_status == Status.ABORTED || _status == Status.UNRECOVERABLE_ERROR || _status == Status.DOWNLOAD_FINISHED) {
return 0;
}

LOGGER.info("Starting metalink download from: " + _downloadUrl);
_start = System.currentTimeMillis();

status = Status.IN_PROGRESS;
Script.runSimpleBashScript("aria2c " + _downloadUrl + " -d " + _toDir);
String metalinkFile = _toFile;
Script.runSimpleBashScript("rm -f " + metalinkFile);
String templateFileName = Script.runSimpleBashScript("ls " + _toDir);
String downloadedFile = _toDir + File.separator + templateFileName;
_toFile = _toDir + File.separator + "tmpdownld_";
Script.runSimpleBashScript("mv " + downloadedFile + " " + _toFile);

File file = new File(_toFile);
if (!file.exists()) {
_status = Status.UNRECOVERABLE_ERROR;
LOGGER.error("Error downloading template from: " + _downloadUrl);
return 0;
}
_totalBytes = file.length();
status = Status.DOWNLOAD_FINISHED;
String sizeResult = Script.runSimpleBashScript("ls -als " + _toFile + " | awk '{print $1}'");
long size = Long.parseLong(sizeResult);
return size;
_downloadTime = System.currentTimeMillis() - _start;
if (_callback != null) {
_callback.downloadComplete(status);
}
return _totalBytes;
}

@Override
Expand All @@ -63,4 +82,13 @@ public int getDownloadPercent() {
}
}

@Override
public Status getStatus() {
return status;
}

@Override
public void setStatus(Status status) {
this.status = status;
}
}
6 changes: 4 additions & 2 deletions utils/src/main/java/com/cloud/utils/UriUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,8 @@ private static void checkFormat(String format, String uripath) {
&& (!uripath.toLowerCase().endsWith("ova")
&& !uripath.toLowerCase().endsWith("ova.zip")
&& !uripath.toLowerCase().endsWith("ova.bz2")
&& !uripath.toLowerCase().endsWith("ova.gz")))
&& !uripath.toLowerCase().endsWith("ova.gz")
&& !uripath.toLowerCase().endsWith("metalink")))
|| (format.equalsIgnoreCase("tar")
&& (!uripath.toLowerCase().endsWith("tar")
&& !uripath.toLowerCase().endsWith("tar.zip")
Expand All @@ -468,7 +469,8 @@ private static void checkFormat(String format, String uripath) {
&& (!uripath.toLowerCase().endsWith("iso")
&& !uripath.toLowerCase().endsWith("iso.zip")
&& !uripath.toLowerCase().endsWith("iso.bz2")
&& !uripath.toLowerCase().endsWith("iso.gz")))) {
&& !uripath.toLowerCase().endsWith("iso.gz"))
&& !uripath.toLowerCase().endsWith("metalink"))) {
throw new IllegalArgumentException("Please specify a valid URL. URL:" + uripath + " is an invalid for the format " + format.toLowerCase());
}

Expand Down