From bc360f499d021f1c81f34aeb9e522cff69de9313 Mon Sep 17 00:00:00 2001 From: nit Date: Thu, 12 May 2011 18:12:48 +0530 Subject: [PATCH] bug 9397 : CreateTemplate - generate MD5 checksum of the template. The code will calculate the checksum after the template has been created in the secondary storage. This code would be executed by ssvm. This would be agnostic of the way the template was created (not hypervisor specific). status 9397: resolved fixed --- .../storage/download/DownloadMonitorImpl.java | 47 +++++++++++++++++-- server/src/com/cloud/vm/UserVmManager.java | 2 + .../src/com/cloud/vm/UserVmManagerImpl.java | 10 +++- 3 files changed, 54 insertions(+), 5 deletions(-) mode change 100644 => 100755 server/src/com/cloud/vm/UserVmManager.java diff --git a/server/src/com/cloud/storage/download/DownloadMonitorImpl.java b/server/src/com/cloud/storage/download/DownloadMonitorImpl.java index 29fce31f63c..278ce402fff 100755 --- a/server/src/com/cloud/storage/download/DownloadMonitorImpl.java +++ b/server/src/com/cloud/storage/download/DownloadMonitorImpl.java @@ -68,9 +68,13 @@ import com.cloud.storage.template.TemplateConstants; import com.cloud.storage.template.TemplateInfo; import com.cloud.user.Account; import com.cloud.utils.component.Inject; +import com.cloud.utils.db.JoinBuilder; +import com.cloud.utils.db.SearchBuilder; +import com.cloud.utils.db.SearchCriteria; import com.cloud.utils.exception.CloudRuntimeException; import com.cloud.vm.SecondaryStorageVm; import com.cloud.vm.SecondaryStorageVmVO; +import com.cloud.vm.UserVmManager; import com.cloud.vm.VirtualMachine.State; import com.cloud.vm.dao.SecondaryStorageVmDao; @@ -83,8 +87,7 @@ import com.cloud.vm.dao.SecondaryStorageVmDao; @Local(value={DownloadMonitor.class}) public class DownloadMonitorImpl implements DownloadMonitor { static final Logger s_logger = Logger.getLogger(DownloadMonitorImpl.class); - - private static final String DEFAULT_HTTP_COPY_PORT = "80"; + @Inject VMTemplateHostDao _vmTemplateHostDao; @Inject @@ -108,6 +111,9 @@ public class DownloadMonitorImpl implements DownloadMonitor { private AgentManager _agentMgr; @Inject ConfigurationDao _configDao; + @Inject + UserVmManager _vmMgr; + @Inject private UsageEventDao _usageEventDao; @@ -120,7 +126,7 @@ public class DownloadMonitorImpl implements DownloadMonitor { private String _name; private Boolean _sslCopy = new Boolean(false); private String _copyAuthPasswd; - + protected SearchBuilder ReadyTemplateStatesSearch; Timer _timer; @@ -146,6 +152,20 @@ public class DownloadMonitorImpl implements DownloadMonitor { _copyAuthPasswd = configs.get("secstorage.copy.password"); _agentMgr.registerForHostEvents(new DownloadListener(this), true, false, false); + + ReadyTemplateStatesSearch = _vmTemplateHostDao.createSearchBuilder(); + ReadyTemplateStatesSearch.and("download_state", ReadyTemplateStatesSearch.entity().getDownloadState(), SearchCriteria.Op.EQ); + ReadyTemplateStatesSearch.and("destroyed", ReadyTemplateStatesSearch.entity().getDestroyed(), SearchCriteria.Op.EQ); + ReadyTemplateStatesSearch.and("host_id", ReadyTemplateStatesSearch.entity().getHostId(), SearchCriteria.Op.EQ); + + SearchBuilder TemplatesWithNoChecksumSearch = _templateDao.createSearchBuilder(); + TemplatesWithNoChecksumSearch.and("checksum", TemplatesWithNoChecksumSearch.entity().getChecksum(), SearchCriteria.Op.NULL); + + ReadyTemplateStatesSearch.join("vm_template", TemplatesWithNoChecksumSearch, TemplatesWithNoChecksumSearch.entity().getId(), + ReadyTemplateStatesSearch.entity().getTemplateId(), JoinBuilder.JoinType.INNER); + TemplatesWithNoChecksumSearch.done(); + ReadyTemplateStatesSearch.done(); + return true; } @@ -582,6 +602,10 @@ public class DownloadMonitorImpl implements DownloadMonitor { String description = "Deleted template " + tInfo.getTemplateName() + " on secondary storage " + sserverId + " since it isn't in the database, result=" + result; s_logger.info(description); } + + //This code is mostly for migration purposes so that we have checksum for all the templates + checksumSync(sserverId); + } @@ -600,6 +624,23 @@ public class DownloadMonitorImpl implements DownloadMonitor { } } + private void checksumSync(long hostId){ + SearchCriteria sc = ReadyTemplateStatesSearch.create(); + sc.setParameters("download_state", com.cloud.storage.VMTemplateStorageResourceAssoc.Status.DOWNLOADED); + + List templateHostRefList = _vmTemplateHostDao.search(sc, null); + s_logger.debug("Found " +templateHostRefList.size()+ " templates with no checksum. Will ask for computation"); + for(VMTemplateHostVO templateHostRef : templateHostRefList){ + s_logger.debug("Getting checksum for template - " + templateHostRef.getTemplateId()); + String checksum = _vmMgr.getChecksum(hostId, templateHostRef.getInstallPath()); + VMTemplateVO template = _templateDao.findById(templateHostRef.getTemplateId()); + s_logger.debug("Setting checksum " +checksum+ " for template - " + template.getName()); + template.setChecksum(checksum); + _templateDao.update(template.getId(), template); + } + + } + private Long getMaxTemplateSizeInBytes() { try { return Long.parseLong(_configDao.getValue("max.template.iso.size")) * 1024L * 1024L * 1024L; diff --git a/server/src/com/cloud/vm/UserVmManager.java b/server/src/com/cloud/vm/UserVmManager.java old mode 100644 new mode 100755 index 75d6a15d0f4..08fc5bda36f --- a/server/src/com/cloud/vm/UserVmManager.java +++ b/server/src/com/cloud/vm/UserVmManager.java @@ -87,4 +87,6 @@ public interface UserVmManager extends VirtualMachineGuru{ * @return List of UserVMs. */ List searchForUserVMs(Criteria c); + + String getChecksum(Long hostId, String templatePath); } diff --git a/server/src/com/cloud/vm/UserVmManagerImpl.java b/server/src/com/cloud/vm/UserVmManagerImpl.java index 97553d5f47c..ad06b01e7c2 100755 --- a/server/src/com/cloud/vm/UserVmManagerImpl.java +++ b/server/src/com/cloud/vm/UserVmManagerImpl.java @@ -1584,11 +1584,16 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService, Manager return privateTemplate; } - private String getChecksum(Long hostId, String templatePath){ + + @Override + public String getChecksum(Long hostId, String templatePath){ Answer answer; try { answer = _agentMgr.send(hostId, new ComputeChecksumCommand(templatePath)); - return answer.getDetails(); + if(answer.getResult()) + return answer.getDetails(); + else + return null; } catch (AgentUnavailableException e) { s_logger.error("Unable to send ComputeChecksumCommand to " + hostId, e); } catch (OperationTimedoutException e) { @@ -1596,6 +1601,7 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService, Manager } return null; } + // used for vm transitioning to error state private void updateVmStateForFailedVmCreation(Long vmId) { UserVmVO vm = _vmDao.findById(vmId);