use of com.cloud.agent.direct.download.DirectTemplateDownloader in project cloudstack by apache.
the class KVMStorageProcessor method handleDownloadTemplateToPrimaryStorage.
@Override
public Answer handleDownloadTemplateToPrimaryStorage(DirectDownloadCommand cmd) {
final PrimaryDataStoreTO pool = cmd.getDestPool();
DirectTemplateDownloader downloader;
KVMPhysicalDisk template;
KVMStoragePool destPool = null;
try {
s_logger.debug("Verifying temporary location for downloading the template exists on the host");
String temporaryDownloadPath = resource.getDirectDownloadTemporaryDownloadPath();
if (!isLocationAccessible(temporaryDownloadPath)) {
String msg = "The temporary location path for downloading templates does not exist: " + temporaryDownloadPath + " on this host";
s_logger.error(msg);
return new DirectDownloadAnswer(false, msg, true);
}
Long templateSize = null;
if (StringUtils.isNotBlank(cmd.getUrl())) {
String url = cmd.getUrl();
templateSize = UriUtils.getRemoteSize(url);
}
s_logger.debug("Checking for free space on the host for downloading the template with physical size: " + templateSize + " and virtual size: " + cmd.getTemplateSize());
if (!isEnoughSpaceForDownloadTemplateOnTemporaryLocation(templateSize)) {
String msg = "Not enough space on the defined temporary location to download the template " + cmd.getTemplateId();
s_logger.error(msg);
return new DirectDownloadAnswer(false, msg, true);
}
destPool = storagePoolMgr.getStoragePool(pool.getPoolType(), pool.getUuid());
downloader = getDirectTemplateDownloaderFromCommand(cmd, destPool, temporaryDownloadPath);
s_logger.debug("Trying to download template");
Pair<Boolean, String> result = downloader.downloadTemplate();
if (!result.first()) {
s_logger.warn("Couldn't download template");
return new DirectDownloadAnswer(false, "Unable to download template", true);
}
String tempFilePath = result.second();
if (!downloader.validateChecksum()) {
s_logger.warn("Couldn't validate template checksum");
return new DirectDownloadAnswer(false, "Checksum validation failed", false);
}
final TemplateObjectTO destTemplate = cmd.getDestData();
String destTemplatePath = (destTemplate != null) ? destTemplate.getPath() : null;
if (!storagePoolMgr.connectPhysicalDisk(pool.getPoolType(), pool.getUuid(), destTemplatePath, null)) {
s_logger.warn("Unable to connect physical disk at path: " + destTemplatePath + ", in storage pool id: " + pool.getUuid());
}
template = storagePoolMgr.createPhysicalDiskFromDirectDownloadTemplate(tempFilePath, destTemplatePath, destPool, cmd.getFormat(), cmd.getWaitInMillSeconds());
if (!storagePoolMgr.disconnectPhysicalDisk(pool.getPoolType(), pool.getUuid(), destTemplatePath)) {
s_logger.warn("Unable to disconnect physical disk at path: " + destTemplatePath + ", in storage pool id: " + pool.getUuid());
}
} catch (CloudRuntimeException e) {
s_logger.warn("Error downloading template " + cmd.getTemplateId() + " due to: " + e.getMessage());
return new DirectDownloadAnswer(false, "Unable to download template: " + e.getMessage(), true);
} catch (IllegalArgumentException e) {
return new DirectDownloadAnswer(false, "Unable to create direct downloader: " + e.getMessage(), true);
}
return new DirectDownloadAnswer(true, template.getSize(), template.getName());
}
Aggregations