use of com.cloud.agent.api.storage.DownloadAnswer in project cloudstack by apache.
the class NfsSecondaryStorageResource method registerTemplateOnSwift.
protected Answer registerTemplateOnSwift(DownloadCommand cmd) {
SwiftTO swiftTO = (SwiftTO) cmd.getDataStore();
String path = cmd.getInstallPath();
DataStoreTO cacheStore = cmd.getCacheStore();
if (cacheStore == null || !(cacheStore instanceof NfsTO)) {
return new DownloadAnswer("cache store can't be null", VMTemplateStorageResourceAssoc.Status.DOWNLOAD_ERROR);
}
File file = null;
try {
NfsTO nfsCacheStore = (NfsTO) cacheStore;
String fileName = cmd.getName() + "." + cmd.getFormat().getFileExtension();
file = downloadFromUrlToNfs(cmd.getUrl(), nfsCacheStore, path, fileName);
String container = "T-" + cmd.getId();
String swiftPath = SwiftUtil.putObject(swiftTO, file, container, null);
long virtualSize = getVirtualSize(file, getTemplateFormat(file.getName()));
long size = file.length();
String uniqueName = cmd.getName();
//put metda file
File uniqDir = _storage.createUniqDir();
String metaFileName = uniqDir.getAbsolutePath() + File.separator + _tmpltpp;
_storage.create(uniqDir.getAbsolutePath(), _tmpltpp);
File metaFile = swiftWriteMetadataFile(metaFileName, uniqueName, fileName, size, virtualSize);
SwiftUtil.putObject(swiftTO, metaFile, container, _tmpltpp);
metaFile.delete();
uniqDir.delete();
String md5sum = null;
try (FileInputStream fs = new FileInputStream(file)) {
md5sum = DigestUtils.md5Hex(fs);
} catch (IOException e) {
s_logger.debug("Failed to get md5sum: " + file.getAbsoluteFile());
}
DownloadAnswer answer = new DownloadAnswer(null, 100, null, VMTemplateStorageResourceAssoc.Status.DOWNLOADED, swiftPath, swiftPath, virtualSize, file.length(), md5sum);
return answer;
} catch (IOException e) {
s_logger.debug("Failed to register template into swift", e);
return new DownloadAnswer(e.toString(), VMTemplateStorageResourceAssoc.Status.DOWNLOAD_ERROR);
} finally {
if (file != null) {
file.delete();
}
}
}
use of com.cloud.agent.api.storage.DownloadAnswer in project cloudstack by apache.
the class DownloadState method handleEvent.
public String handleEvent(DownloadEvent event, Object eventObj) {
if (s_logger.isTraceEnabled()) {
getDownloadListener().log("handleEvent, event type=" + event + ", curr state=" + getName(), Level.TRACE);
}
switch(event) {
case DOWNLOAD_ANSWER:
DownloadAnswer answer = (DownloadAnswer) eventObj;
return handleAnswer(answer);
case ABANDON_DOWNLOAD:
return handleAbort();
case TIMEOUT_CHECK:
Date now = new Date();
long update = now.getTime() - dl.getLastUpdated().getTime();
return handleTimeout(update);
case DISCONNECT:
return handleDisconnect();
}
return null;
}
use of com.cloud.agent.api.storage.DownloadAnswer in project cloudstack by apache.
the class DownloadAbandonedState method onEntry.
@Override
public void onEntry(String prevState, DownloadEvent event, Object evtObj) {
super.onEntry(prevState, event, evtObj);
if (!prevState.equalsIgnoreCase(getName())) {
DownloadAnswer answer = new DownloadAnswer("Download canceled", Status.ABANDONED);
getDownloadListener().callback(answer);
getDownloadListener().cancelStatusTask();
getDownloadListener().cancelTimeoutTask();
getDownloadListener().sendCommand(RequestType.ABORT);
}
}
use of com.cloud.agent.api.storage.DownloadAnswer in project cloudstack by apache.
the class DownloadManagerImpl method handleDownloadCommand.
@Override
public DownloadAnswer handleDownloadCommand(SecondaryStorageResource resource, DownloadCommand cmd) {
ResourceType resourceType = cmd.getResourceType();
if (cmd instanceof DownloadProgressCommand) {
return handleDownloadProgressCmd(resource, (DownloadProgressCommand) cmd);
}
if (cmd.getUrl() == null) {
return new DownloadAnswer(resourceType.toString() + " is corrupted on storage due to an invalid url , cannot download", VMTemplateStorageResourceAssoc.Status.DOWNLOAD_ERROR);
}
if (cmd.getName() == null) {
return new DownloadAnswer("Invalid Name", VMTemplateStorageResourceAssoc.Status.DOWNLOAD_ERROR);
}
DataStoreTO dstore = cmd.getDataStore();
String installPathPrefix = cmd.getInstallPath();
// for NFS, we need to get mounted path
if (dstore instanceof NfsTO) {
installPathPrefix = resource.getRootDir(((NfsTO) dstore).getUrl(), _nfsVersion) + File.separator + installPathPrefix;
}
String user = null;
String password = null;
if (cmd.getAuth() != null) {
user = cmd.getAuth().getUserName();
password = cmd.getAuth().getPassword();
}
// TO DO - Define Volume max size as well
long maxDownloadSizeInBytes = (cmd.getMaxDownloadSizeInBytes() == null) ? TemplateDownloader.DEFAULT_MAX_TEMPLATE_SIZE_IN_BYTES : (cmd.getMaxDownloadSizeInBytes());
String jobId = null;
if (dstore instanceof S3TO) {
jobId = downloadS3Template((S3TO) dstore, cmd.getId(), cmd.getUrl(), cmd.getName(), cmd.getFormat(), cmd.isHvm(), cmd.getAccountId(), cmd.getDescription(), cmd.getChecksum(), installPathPrefix, user, password, maxDownloadSizeInBytes, cmd.getProxy(), resourceType);
} else {
jobId = downloadPublicTemplate(cmd.getId(), cmd.getUrl(), cmd.getName(), cmd.getFormat(), cmd.isHvm(), cmd.getAccountId(), cmd.getDescription(), cmd.getChecksum(), installPathPrefix, cmd.getInstallPath(), user, password, maxDownloadSizeInBytes, cmd.getProxy(), resourceType);
}
sleep();
if (jobId == null) {
return new DownloadAnswer("Internal Error", VMTemplateStorageResourceAssoc.Status.DOWNLOAD_ERROR);
}
return new DownloadAnswer(jobId, getDownloadPct(jobId), getDownloadError(jobId), getDownloadStatus2(jobId), getDownloadLocalPath(jobId), getInstallPath(jobId), getDownloadTemplateSize(jobId), getDownloadTemplateSize(jobId), getDownloadCheckSum(jobId));
}
use of com.cloud.agent.api.storage.DownloadAnswer in project cloudstack by apache.
the class DownloadManagerImpl method handleDownloadProgressCmd.
private DownloadAnswer handleDownloadProgressCmd(SecondaryStorageResource resource, DownloadProgressCommand cmd) {
String jobId = cmd.getJobId();
DownloadAnswer answer;
DownloadJob dj = null;
if (jobId != null) {
dj = jobs.get(jobId);
}
if (dj == null) {
if (cmd.getRequest() == RequestType.GET_OR_RESTART) {
DownloadCommand dcmd = new DownloadCommand(cmd);
return handleDownloadCommand(resource, dcmd);
} else {
return new DownloadAnswer("Cannot find job", com.cloud.storage.VMTemplateStorageResourceAssoc.Status.UNKNOWN);
}
}
TemplateDownloader td = dj.getTemplateDownloader();
switch(cmd.getRequest()) {
case GET_STATUS:
break;
case ABORT:
td.stopDownload();
sleep();
break;
case RESTART:
td.stopDownload();
sleep();
threadPool.execute(td);
break;
case PURGE:
td.stopDownload();
answer = new DownloadAnswer(jobId, getDownloadPct(jobId), getDownloadError(jobId), getDownloadStatus2(jobId), getDownloadLocalPath(jobId), getInstallPath(jobId), getDownloadTemplateSize(jobId), getDownloadTemplatePhysicalSize(jobId), getDownloadCheckSum(jobId));
jobs.remove(jobId);
return answer;
default:
// TODO
break;
}
return new DownloadAnswer(jobId, getDownloadPct(jobId), getDownloadError(jobId), getDownloadStatus2(jobId), getDownloadLocalPath(jobId), getInstallPath(jobId), getDownloadTemplateSize(jobId), getDownloadTemplatePhysicalSize(jobId), getDownloadCheckSum(jobId));
}
Aggregations