From 8df96da0a503bdf570ced4695b130ecbf9bc13b4 Mon Sep 17 00:00:00 2001 From: anthony Date: Mon, 8 Nov 2010 21:02:20 -0800 Subject: [PATCH] don't delete template if template check fail, give user a chance to check the template manually use can delete the template though UI --- .../xen/resource/CitrixResourceBase.java | 2 +- .../DummySecondaryStorageResource.java | 2 +- .../com/cloud/storage/template/Processor.java | 1 + .../cloud/storage/template/TemplateInfo.java | 17 ++++--- .../storage/template/TemplateLocation.java | 26 +++++------ .../storage/download/DownloadMonitorImpl.java | 44 +++++++++++++------ 6 files changed, 56 insertions(+), 36 deletions(-) diff --git a/core/src/com/cloud/hypervisor/xen/resource/CitrixResourceBase.java b/core/src/com/cloud/hypervisor/xen/resource/CitrixResourceBase.java index 7d967dc844c..7903ce04f4c 100644 --- a/core/src/com/cloud/hypervisor/xen/resource/CitrixResourceBase.java +++ b/core/src/com/cloud/hypervisor/xen/resource/CitrixResourceBase.java @@ -3872,7 +3872,7 @@ public abstract class CitrixResourceBase implements StoragePoolResource, ServerR VDI pvISO = vids.iterator().next(); String uuid = pvISO.getUuid(conn); Map pvISOtmlt = new HashMap(); - TemplateInfo tmplt = new TemplateInfo("xs-tools.iso", uuid, pvISO.getVirtualSize(conn), true); + TemplateInfo tmplt = new TemplateInfo("xs-tools.iso", uuid, pvISO.getVirtualSize(conn), true, false); pvISOtmlt.put("xs-tools", tmplt); sscmd.setTemplateInfo(pvISOtmlt); } catch (XenAPIException e) { diff --git a/core/src/com/cloud/storage/resource/DummySecondaryStorageResource.java b/core/src/com/cloud/storage/resource/DummySecondaryStorageResource.java index 7f43c306657..3c9f2669a90 100644 --- a/core/src/com/cloud/storage/resource/DummySecondaryStorageResource.java +++ b/core/src/com/cloud/storage/resource/DummySecondaryStorageResource.java @@ -177,7 +177,7 @@ public class DummySecondaryStorageResource extends ServerResourceBase implements Map tmpltInfo = new HashMap(); if (tmplts != null) { for (VMTemplateVO tmplt : tmplts) { - TemplateInfo routingInfo = new TemplateInfo(tmplt.getUniqueName(), TemplateConstants.DEFAULT_SYSTEM_VM_TEMPLATE_PATH + tmplt.getId() + File.separator, false); + TemplateInfo routingInfo = new TemplateInfo(tmplt.getUniqueName(), TemplateConstants.DEFAULT_SYSTEM_VM_TEMPLATE_PATH + tmplt.getId() + File.separator, false, false); tmpltInfo.put(tmplt.getUniqueName(), routingInfo); } } diff --git a/core/src/com/cloud/storage/template/Processor.java b/core/src/com/cloud/storage/template/Processor.java index 43e82a682e4..7a29e31c39e 100644 --- a/core/src/com/cloud/storage/template/Processor.java +++ b/core/src/com/cloud/storage/template/Processor.java @@ -47,5 +47,6 @@ public interface Processor extends Adapter { public long size; public long virtualSize; public String filename; + public boolean isCorrupted; } } diff --git a/core/src/com/cloud/storage/template/TemplateInfo.java b/core/src/com/cloud/storage/template/TemplateInfo.java index bf32e07c4ba..c6625678621 100644 --- a/core/src/com/cloud/storage/template/TemplateInfo.java +++ b/core/src/com/cloud/storage/template/TemplateInfo.java @@ -17,29 +17,30 @@ */ package com.cloud.storage.template; + + public class TemplateInfo { String templateName; String installPath; long size; long id; boolean isPublic; + boolean isCorrupted; protected TemplateInfo() { } - public TemplateInfo(String templateName, String installPath, long size, boolean isPublic) { + public TemplateInfo(String templateName, String installPath, long size, boolean isPublic, boolean isCorrupted) { this.templateName = templateName; this.installPath = installPath; this.size = size; this.isPublic = isPublic; + this.isCorrupted = isCorrupted; } - public TemplateInfo(String templateName, String installPath, boolean isPublic) { - this.templateName = templateName; - this.installPath = installPath; - this.size = 0; - this.isPublic = isPublic; + public TemplateInfo(String templateName, String installPath, boolean isPublic, boolean isCorrupted) { + this(templateName, installPath, 0, isPublic, isCorrupted); } public long getId() { @@ -58,6 +59,10 @@ public class TemplateInfo { return isPublic; } + public boolean isCorrupted() { + return isCorrupted; + } + public void setInstallPath(String installPath) { this.installPath = installPath; } diff --git a/core/src/com/cloud/storage/template/TemplateLocation.java b/core/src/com/cloud/storage/template/TemplateLocation.java index 57785375422..70c9a094878 100644 --- a/core/src/com/cloud/storage/template/TemplateLocation.java +++ b/core/src/com/cloud/storage/template/TemplateLocation.java @@ -38,6 +38,7 @@ public class TemplateLocation { StorageLayer _storage; String _templatePath; + boolean _isCorrupted; File _file; Properties _props; @@ -53,6 +54,7 @@ public class TemplateLocation { _formats = new ArrayList(5); _props = new Properties(); _file = _storage.getFile(_templatePath + Filename); + _isCorrupted = false; } public boolean create(long id, boolean isPublic, String uniqueName) throws IOException { @@ -100,15 +102,18 @@ public class TemplateLocation { FormatInfo info = new FormatInfo(); info.format = format; info.filename = _props.getProperty(format.getFileExtension() + ".filename"); + if( info.filename == null ) { + continue; + } info.size = NumbersUtil.parseLong(_props.getProperty(format.getFileExtension() + ".size"), -1); info.virtualSize = NumbersUtil.parseLong(_props.getProperty(format.getFileExtension() + ".virtualsize"), -1); - _formats.add(info); + _formats.add(info); if (!checkFormatValidity(info)) { + _isCorrupted = true; s_logger.warn("Cleaning up inconsistent information for " + format); - cleanup(format); - } + } } } @@ -116,7 +121,7 @@ public class TemplateLocation { return false; } - return _formats.size() > 0; + return (_formats.size() > 0); } public boolean save() { @@ -148,10 +153,10 @@ public class TemplateLocation { public TemplateInfo getTemplateInfo() { TemplateInfo tmplInfo = new TemplateInfo(); - String[] tokens = _templatePath.split(File.separator); tmplInfo.id = Long.parseLong(_props.getProperty("id")); tmplInfo.installPath = _templatePath + File.separator + _props.getProperty("filename"); tmplInfo.installPath = tmplInfo.installPath.substring(tmplInfo.installPath.indexOf("template")); + tmplInfo.isCorrupted = _isCorrupted; tmplInfo.isPublic = Boolean.parseBoolean(_props.getProperty("public")); tmplInfo.templateName = _props.getProperty("uniquename"); tmplInfo.size = Long.parseLong(_props.getProperty("virtualsize")); @@ -159,16 +164,7 @@ public class TemplateLocation { return tmplInfo; } - protected void cleanup(ImageFormat format) { - FormatInfo info = deleteFormat(format); - if (info != null && info.filename != null) { - boolean r = _storage.delete(_templatePath + info.filename); - if (s_logger.isDebugEnabled()) { - s_logger.debug((r ? "R" : "Unable to r") + "emove " + _templatePath + info.filename); - } - } - } - + public FormatInfo getFormat(ImageFormat format) { for (FormatInfo info : _formats) { if (info.format == format) { diff --git a/server/src/com/cloud/storage/download/DownloadMonitorImpl.java b/server/src/com/cloud/storage/download/DownloadMonitorImpl.java index b0a4428b97a..0b77284bc01 100755 --- a/server/src/com/cloud/storage/download/DownloadMonitorImpl.java +++ b/server/src/com/cloud/storage/download/DownloadMonitorImpl.java @@ -38,6 +38,7 @@ import com.cloud.agent.api.storage.DownloadCommand; import com.cloud.agent.api.storage.DownloadProgressCommand; import com.cloud.agent.api.storage.DownloadProgressCommand.RequestType; import com.cloud.agent.AgentManager; +import com.cloud.alert.AlertManager; import com.cloud.configuration.dao.ConfigurationDao; import com.cloud.dc.DataCenterVO; import com.cloud.dc.dao.DataCenterDao; @@ -88,7 +89,8 @@ public class DownloadMonitorImpl implements DownloadMonitor { StoragePoolHostDao _poolHostDao; @Inject SecondaryStorageVmDao _secStorageVmDao; - + @Inject + AlertManager _alertMgr; @Inject HostDao _serverDao = null; @@ -408,7 +410,7 @@ public class DownloadMonitorImpl implements DownloadMonitor { } @Override - public void handleTemplateSync(long sserverId, Map templateInfo) { + public void handleTemplateSync(long sserverId, Map templateInfos) { HostVO storageHost = _serverDao.findById(sserverId); if (storageHost == null) { s_logger.warn("Huh? Agent id " + sserverId + " does not correspond to a row in hosts table?"); @@ -446,25 +448,41 @@ public class DownloadMonitorImpl implements DownloadMonitor { for (VMTemplateVO tmplt: allTemplates) { String uniqueName = tmplt.getUniqueName(); VMTemplateHostVO tmpltHost = _vmTemplateHostDao.findByHostTemplate(sserverId, tmplt.getId()); - if (templateInfo.containsKey(uniqueName)) { + if (templateInfos.containsKey(uniqueName)) { + TemplateInfo tmpltInfo = templateInfos.remove(uniqueName); toBeDownloaded.remove(tmplt); if (tmpltHost != null) { s_logger.info("Template Sync found " + uniqueName + " already in the template host table"); if (tmpltHost.getDownloadState() != Status.DOWNLOADED) { tmpltHost.setErrorString(""); } - tmpltHost.setDownloadPercent(100); - tmpltHost.setDownloadState(Status.DOWNLOADED); - tmpltHost.setInstallPath(templateInfo.get(uniqueName).getInstallPath()); - tmpltHost.setSize(templateInfo.get(uniqueName).getSize()); - tmpltHost.setLastUpdated(new Date()); + if( tmpltInfo.isCorrupted() ) { + tmpltHost.setDownloadState(Status.DOWNLOAD_ERROR); + tmpltHost.setErrorString("This template is corrupted"); + toBeDownloaded.add(tmplt); + s_logger.info("Template (" + tmplt +") is corrupted"); + if( tmplt.getUrl() == null) { + String msg = "Private Template (" + tmplt +") with install path " + tmpltInfo.getInstallPath() + + "is corrupted, please check in secondary storage: " + tmpltHost.getHostId(); + s_logger.warn(msg); + } else { + toBeDownloaded.add(tmplt); + } + + } else { + tmpltHost.setDownloadPercent(100); + tmpltHost.setDownloadState(Status.DOWNLOADED); + tmpltHost.setInstallPath(tmpltInfo.getInstallPath()); + tmpltHost.setSize(tmpltInfo.getSize()); + tmpltHost.setLastUpdated(new Date()); + } _vmTemplateHostDao.update(tmpltHost.getId(), tmpltHost); } else { - tmpltHost = new VMTemplateHostVO(sserverId, tmplt.getId(), new Date(), 100, Status.DOWNLOADED, null, null, null, templateInfo.get(uniqueName).getInstallPath(), tmplt.getUrl()); - tmpltHost.setSize(templateInfo.get(uniqueName).getSize()); + tmpltHost = new VMTemplateHostVO(sserverId, tmplt.getId(), new Date(), 100, Status.DOWNLOADED, null, null, null, tmpltInfo.getInstallPath(), tmplt.getUrl()); + tmpltHost.setSize(tmpltInfo.getSize()); _vmTemplateHostDao.persist(tmpltHost); } - templateInfo.remove(uniqueName); + continue; } if (tmpltHost != null && tmpltHost.getDownloadState() != Status.DOWNLOADED) { @@ -489,8 +507,8 @@ public class DownloadMonitorImpl implements DownloadMonitor { } } - for (String uniqueName: templateInfo.keySet()) { - TemplateInfo tInfo = templateInfo.get(uniqueName); + for (String uniqueName: templateInfos.keySet()) { + TemplateInfo tInfo = templateInfos.get(uniqueName); DeleteTemplateCommand dtCommand = new DeleteTemplateCommand(tInfo.getInstallPath()); long result = send(sserverId, dtCommand, null); if (result == -1 ){