templateId);
LOGGER.error(errorMessage);
return errorMessage;
}
final VMTemplateS3VO templateS3VO = findByTemplateId(templateId);
if (templateS3VO == null) {
final String errorMessage = format(
"Failed to download template id %1$s from S3 because it does not exist in S3.",
templateId);
LOGGER.error(errorMessage);
return errorMessage;
}
final S3TO s3 = getS3TO(templateS3VO.getS3Id());
if (s3 == null) {
final String errorMessage = format(
"Failed to download template id %1$s from S3 because S3 id %2$s does not exist.",
templateId, templateS3VO);
LOGGER.error(errorMessage);
return errorMessage;
}
final HostVO secondaryStorageHost = secondaryStorageVMManager
.findSecondaryStorageHost(dataCenterId);
if (secondaryStorageHost == null) {
final String errorMessage = format(
"Unable to find secondary storage host for zone id %1$s.",
dataCenterId);
LOGGER.error(errorMessage);
throw new CloudRuntimeException(errorMessage);
}
final long accountId = template.getAccountId();
final DownloadTemplateFromS3ToSecondaryStorageCommand cmd = new DownloadTemplateFromS3ToSecondaryStorageCommand(
s3, accountId, templateId, secondaryStorageHost.getName(),
primaryStorageDownloadWait);
try {
executeWithNoWaitLock(determineLockId(accountId, templateId),
new Callable<Void>() {
@Override
public Void call() throws Exception {
final Answer answer = agentManager.sendToSSVM(
dataCenterId, cmd);
if (answer == null || !answer.getResult()) {
final String errMsg = String
.format("Failed to download template from S3 to secondary storage due to %1$s",
(answer == null ? "answer is null"
: answer.getDetails()));
LOGGER.error(errMsg);
throw new CloudRuntimeException(errMsg);
}
final String installPath = join(
asList("template", "tmpl", accountId,
templateId), File.separator);
final VMTemplateHostVO tmpltHost = new VMTemplateHostVO(
secondaryStorageHost.getId(), templateId,
now(), 100, Status.DOWNLOADED, null, null,
null, installPath, template.getUrl());
tmpltHost.setSize(templateS3VO.getSize());
tmpltHost.setPhysicalSize(templateS3VO
.getPhysicalSize());
vmTemplateHostDao.persist(tmpltHost);
return null;