diff --git a/api/src/com/cloud/agent/api/downloadTemplateFromSwiftToSecondaryStorageCommand.java b/api/src/com/cloud/agent/api/downloadTemplateFromSwiftToSecondaryStorageCommand.java index 610e477ca1b..7f31bccfc24 100644 --- a/api/src/com/cloud/agent/api/downloadTemplateFromSwiftToSecondaryStorageCommand.java +++ b/api/src/com/cloud/agent/api/downloadTemplateFromSwiftToSecondaryStorageCommand.java @@ -34,18 +34,20 @@ public class downloadTemplateFromSwiftToSecondaryStorageCommand extends Command private Long dcId; private Long accountId; private Long templateId; + private String path; protected downloadTemplateFromSwiftToSecondaryStorageCommand() { } - public downloadTemplateFromSwiftToSecondaryStorageCommand(SwiftTO swift, String secondaryStorageUrl, Long dcId, Long accountId, Long templateId, int wait) { + public downloadTemplateFromSwiftToSecondaryStorageCommand(SwiftTO swift, String secondaryStorageUrl, Long dcId, Long accountId, Long templateId, String path, int wait) { this.swift = swift; this.secondaryStorageUrl = secondaryStorageUrl; this.dcId = dcId; this.accountId = accountId; this.templateId = templateId; + this.path = path; setWait(wait); } @@ -73,6 +75,14 @@ public class downloadTemplateFromSwiftToSecondaryStorageCommand extends Command return templateId; } + public String getPath() { + return path; + } + + public void setPath(String path) { + this.path = path; + } + @Override public boolean executeInSequence() { // TODO Auto-generated method stub diff --git a/core/src/com/cloud/storage/VMTemplateSwiftVO.java b/core/src/com/cloud/storage/VMTemplateSwiftVO.java index e538c55209c..5eff1d1a2a8 100755 --- a/core/src/com/cloud/storage/VMTemplateSwiftVO.java +++ b/core/src/com/cloud/storage/VMTemplateSwiftVO.java @@ -51,16 +51,20 @@ public class VMTemplateSwiftVO { @Column(name = GenericDaoBase.CREATED_COLUMN) private Date created = null; + @Column(name = "path") + private String path; + @Column(name = "size") private long size; @Column(name = "physical_size") private long physicalSize; - public VMTemplateSwiftVO(long swiftId, long templateId, Date created, long size, long physicalSize) { + public VMTemplateSwiftVO(long swiftId, long templateId, Date created, String path, long size, long physicalSize) { this.swiftId = swiftId; this.templateId = templateId; this.created = created; + this.path = path; this.size = size; this.physicalSize = physicalSize; } @@ -81,6 +85,10 @@ public class VMTemplateSwiftVO { return created; } + public String getPath() { + return path; + } + public long getSwiftId() { return swiftId; } diff --git a/core/src/com/cloud/storage/resource/NfsSecondaryStorageResource.java b/core/src/com/cloud/storage/resource/NfsSecondaryStorageResource.java index 8ea82e1d3a0..7f877af6a3d 100755 --- a/core/src/com/cloud/storage/resource/NfsSecondaryStorageResource.java +++ b/core/src/com/cloud/storage/resource/NfsSecondaryStorageResource.java @@ -174,18 +174,39 @@ public class NfsSecondaryStorageResource extends ServerResourceBase implements S String secondaryStorageUrl = cmd.getSecondaryStorageUrl(); Long accountId = cmd.getAccountId(); Long templateId = cmd.getTemplateId(); + String path = cmd.getPath(); + String errMsg; + String lDir = null; try { String parent = getRootDir(secondaryStorageUrl); - String lPath = parent + "/template/tmpl/" + accountId.toString() + "/" + templateId.toString(); - String result = swiftDownloadContainer(swift, "T-" + templateId.toString(), lPath); + lDir = parent + "/template/tmpl/" + accountId.toString() + "/" + templateId.toString(); + String result = createLocalDir(lDir); if (result != null) { - String errMsg = "failed to download template from Swift to secondary storage " + lPath + " , err=" + result; + errMsg = "downloadTemplateFromSwiftToSecondaryStorageCommand failed due to Create local directory failed"; s_logger.warn(errMsg); - return new Answer(cmd, false, errMsg); + throw new InternalErrorException(errMsg); + } + String lPath = lDir + "/" + path; + result = swiftDownload(swift, "T-" + templateId.toString(), path, lPath); + if (result != null) { + errMsg = "failed to download template " + path + " from Swift to secondary storage " + lPath + " , err=" + result; + s_logger.warn(errMsg); + throw new CloudRuntimeException(errMsg); + } + path = "template.properties"; + lPath = lDir + "/" + path; + result = swiftDownload(swift, "T-" + templateId.toString(), path, lPath); + if (result != null) { + errMsg = "failed to download template " + path + " from Swift to secondary storage " + lPath + " , err=" + result; + s_logger.warn(errMsg); + throw new CloudRuntimeException(errMsg); } return new Answer(cmd, true, "success"); } catch (Exception e) { - String errMsg = cmd + " Command failed due to " + e.toString(); + if (lDir != null) { + deleteLocalDir(lDir); + } + errMsg = cmd + " Command failed due to " + e.toString(); s_logger.warn(errMsg, e); return new Answer(cmd, false, errMsg); } diff --git a/server/src/com/cloud/storage/allocator/AbstractStoragePoolAllocator.java b/server/src/com/cloud/storage/allocator/AbstractStoragePoolAllocator.java index 770d14674fb..e79f03093b6 100755 --- a/server/src/com/cloud/storage/allocator/AbstractStoragePoolAllocator.java +++ b/server/src/com/cloud/storage/allocator/AbstractStoragePoolAllocator.java @@ -244,14 +244,14 @@ public abstract class AbstractStoragePoolAllocator extends AdapterBase implement if (templateHostVO == null) { VMTemplateSwiftVO templateSwiftVO = _swiftMgr.findByTmpltId(template.getId()); if (templateSwiftVO == null) { - s_logger.info("Did not find template downloaded on secondary hosts in zone " + plan.getDataCenterId()); + s_logger.error("Did not find template downloaded on secondary hosts in zone " + plan.getDataCenterId()); + return false; } long templateSize = templateSwiftVO.getPhysicalSize(); if (templateSize == 0) { templateSize = templateSwiftVO.getSize(); } totalAllocatedSize += (templateSize + _extraBytesPerVolume); - return false; } else { long templateSize = templateHostVO.getPhysicalSize(); if ( templateSize == 0 ){ diff --git a/server/src/com/cloud/storage/dao/VMTemplateHostDaoImpl.java b/server/src/com/cloud/storage/dao/VMTemplateHostDaoImpl.java index 796be63f9e1..d07e7c3c0e5 100755 --- a/server/src/com/cloud/storage/dao/VMTemplateHostDaoImpl.java +++ b/server/src/com/cloud/storage/dao/VMTemplateHostDaoImpl.java @@ -88,13 +88,14 @@ public class VMTemplateHostDaoImpl extends GenericDaoBase listByTemplateId(long templateId) { SearchCriteria sc = TemplateSearch.create(); sc.setParameters("template_id", templateId); - sc.setParameters("destroyed", false); + sc.setParameters("destroyed", false); return listIncludingRemovedBy(sc); } @@ -184,7 +188,8 @@ public class VMTemplateHostDaoImpl extends GenericDaoBase listByOnlyTemplateId(long templateId) { SearchCriteria sc = TemplateSearch.create(); - sc.setParameters("template_id", templateId); + sc.setParameters("template_id", templateId); + sc.setParameters("destroyed", false); return listIncludingRemovedBy(sc); } @@ -193,16 +198,17 @@ public class VMTemplateHostDaoImpl extends GenericDaoBase sc = HostTemplateSearch.create(); sc.setParameters("host_id", hostId); sc.setParameters("template_id", templateId); - sc.setParameters("download_state", Status.DOWNLOADED.toString()); - return findOneIncludingRemovedBy(sc); + sc.setParameters("destroyed", false); + return findOneIncludingRemovedBy(sc); } @Override public List listByTemplateStatus(long templateId, VMTemplateHostVO.Status downloadState) { SearchCriteria sc = TemplateStatusSearch.create(); sc.setParameters("template_id", templateId); - sc.setParameters("download_state", downloadState.toString()); - return listIncludingRemovedBy(sc); + sc.setParameters("download_state", downloadState.toString()); + sc.setParameters("destroyed", false); + return listIncludingRemovedBy(sc); } @Override @@ -232,6 +238,7 @@ public class VMTemplateHostDaoImpl extends GenericDaoBase listByTemplateStates(long templateId, VMTemplateHostVO.Status... states) { SearchCriteria sc = TemplateStatesSearch.create(); sc.setParameters("states", (Object[])states); - sc.setParameters("template_id", templateId); - - return search(sc, null); + sc.setParameters("template_id", templateId); + sc.setParameters("destroyed", false); + return search(sc, null); } @Override public List listByState(VMTemplateHostVO.Status state) { SearchCriteria sc = createSearchCriteria(); sc.addAnd("downloadState", SearchCriteria.Op.EQ, state); + sc.addAnd("destroyed", SearchCriteria.Op.EQ, false); return search(sc, null); } @@ -301,8 +309,9 @@ public class VMTemplateHostDaoImpl extends GenericDaoBase listByHostTemplate(long hostId, long templateId) { SearchCriteria sc = HostTemplateSearch.create(); sc.setParameters("host_id", hostId); - sc.setParameters("template_id", templateId); - return listIncludingRemovedBy(sc); + sc.setParameters("template_id", templateId); + sc.setParameters("destroyed", false); + return listIncludingRemovedBy(sc); } @Override @@ -329,6 +338,7 @@ public class VMTemplateHostDaoImpl extends GenericDaoBase sc = HostTemplateSearch.create(); sc.setParameters("host_id", hostId); sc.setParameters("template_id", templateId); + sc.setParameters("destroyed", false); if (!lock) return findOneIncludingRemovedBy(sc); else @@ -346,6 +356,7 @@ public class VMTemplateHostDaoImpl extends GenericDaoBase