Skip to content

Commit bc360f4

Browse files
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
1 parent d8b7f5c commit bc360f4

3 files changed

Lines changed: 54 additions & 5 deletions

File tree

‎server/src/com/cloud/storage/download/DownloadMonitorImpl.java‎

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,13 @@
6868
import com.cloud.storage.template.TemplateInfo;
6969
import com.cloud.user.Account;
7070
import com.cloud.utils.component.Inject;
71+
import com.cloud.utils.db.JoinBuilder;
72+
import com.cloud.utils.db.SearchBuilder;
73+
import com.cloud.utils.db.SearchCriteria;
7174
import com.cloud.utils.exception.CloudRuntimeException;
7275
import com.cloud.vm.SecondaryStorageVm;
7376
import com.cloud.vm.SecondaryStorageVmVO;
77+
import com.cloud.vm.UserVmManager;
7478
import com.cloud.vm.VirtualMachine.State;
7579
import com.cloud.vm.dao.SecondaryStorageVmDao;
7680

@@ -83,8 +87,7 @@
8387
@Local(value={DownloadMonitor.class})
8488
public class DownloadMonitorImpl implements DownloadMonitor {
8589
static final Logger s_logger = Logger.getLogger(DownloadMonitorImpl.class);
86-
87-
private static final String DEFAULT_HTTP_COPY_PORT = "80";
90+
8891
@Inject
8992
VMTemplateHostDao _vmTemplateHostDao;
9093
@Inject
@@ -108,6 +111,9 @@ public class DownloadMonitorImpl implements DownloadMonitor {
108111
private AgentManager _agentMgr;
109112
@Inject
110113
ConfigurationDao _configDao;
114+
@Inject
115+
UserVmManager _vmMgr;
116+
111117

112118
@Inject
113119
private UsageEventDao _usageEventDao;
@@ -120,7 +126,7 @@ public class DownloadMonitorImpl implements DownloadMonitor {
120126
private String _name;
121127
private Boolean _sslCopy = new Boolean(false);
122128
private String _copyAuthPasswd;
123-
129+
protected SearchBuilder<VMTemplateHostVO> ReadyTemplateStatesSearch;
124130

125131
Timer _timer;
126132

@@ -146,6 +152,20 @@ public boolean configure(String name, Map<String, Object> params) {
146152
_copyAuthPasswd = configs.get("secstorage.copy.password");
147153

148154
_agentMgr.registerForHostEvents(new DownloadListener(this), true, false, false);
155+
156+
ReadyTemplateStatesSearch = _vmTemplateHostDao.createSearchBuilder();
157+
ReadyTemplateStatesSearch.and("download_state", ReadyTemplateStatesSearch.entity().getDownloadState(), SearchCriteria.Op.EQ);
158+
ReadyTemplateStatesSearch.and("destroyed", ReadyTemplateStatesSearch.entity().getDestroyed(), SearchCriteria.Op.EQ);
159+
ReadyTemplateStatesSearch.and("host_id", ReadyTemplateStatesSearch.entity().getHostId(), SearchCriteria.Op.EQ);
160+
161+
SearchBuilder<VMTemplateVO> TemplatesWithNoChecksumSearch = _templateDao.createSearchBuilder();
162+
TemplatesWithNoChecksumSearch.and("checksum", TemplatesWithNoChecksumSearch.entity().getChecksum(), SearchCriteria.Op.NULL);
163+
164+
ReadyTemplateStatesSearch.join("vm_template", TemplatesWithNoChecksumSearch, TemplatesWithNoChecksumSearch.entity().getId(),
165+
ReadyTemplateStatesSearch.entity().getTemplateId(), JoinBuilder.JoinType.INNER);
166+
TemplatesWithNoChecksumSearch.done();
167+
ReadyTemplateStatesSearch.done();
168+
149169
return true;
150170
}
151171

@@ -582,6 +602,10 @@ public void handleTemplateSync(long sserverId, Map<String, TemplateInfo> templat
582602
String description = "Deleted template " + tInfo.getTemplateName() + " on secondary storage " + sserverId + " since it isn't in the database, result=" + result;
583603
s_logger.info(description);
584604
}
605+
606+
//This code is mostly for migration purposes so that we have checksum for all the templates
607+
checksumSync(sserverId);
608+
585609

586610
}
587611

@@ -600,6 +624,23 @@ public void cancelAllDownloads(Long templateId) {
600624
}
601625
}
602626

627+
private void checksumSync(long hostId){
628+
SearchCriteria<VMTemplateHostVO> sc = ReadyTemplateStatesSearch.create();
629+
sc.setParameters("download_state", com.cloud.storage.VMTemplateStorageResourceAssoc.Status.DOWNLOADED);
630+
631+
List<VMTemplateHostVO> templateHostRefList = _vmTemplateHostDao.search(sc, null);
632+
s_logger.debug("Found " +templateHostRefList.size()+ " templates with no checksum. Will ask for computation");
633+
for(VMTemplateHostVO templateHostRef : templateHostRefList){
634+
s_logger.debug("Getting checksum for template - " + templateHostRef.getTemplateId());
635+
String checksum = _vmMgr.getChecksum(hostId, templateHostRef.getInstallPath());
636+
VMTemplateVO template = _templateDao.findById(templateHostRef.getTemplateId());
637+
s_logger.debug("Setting checksum " +checksum+ " for template - " + template.getName());
638+
template.setChecksum(checksum);
639+
_templateDao.update(template.getId(), template);
640+
}
641+
642+
}
643+
603644
private Long getMaxTemplateSizeInBytes() {
604645
try {
605646
return Long.parseLong(_configDao.getValue("max.template.iso.size")) * 1024L * 1024L * 1024L;

‎server/src/com/cloud/vm/UserVmManager.java‎

100644100755
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,6 @@ public interface UserVmManager extends VirtualMachineGuru<UserVmVO>{
8787
* @return List of UserVMs.
8888
*/
8989
List<UserVmVO> searchForUserVMs(Criteria c);
90+
91+
String getChecksum(Long hostId, String templatePath);
9092
}

‎server/src/com/cloud/vm/UserVmManagerImpl.java‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1584,18 +1584,24 @@ public VMTemplateVO createPrivateTemplate(CreateTemplateCmd command) throws Clou
15841584

15851585
return privateTemplate;
15861586
}
1587-
private String getChecksum(Long hostId, String templatePath){
1587+
1588+
@Override
1589+
public String getChecksum(Long hostId, String templatePath){
15881590
Answer answer;
15891591
try {
15901592
answer = _agentMgr.send(hostId, new ComputeChecksumCommand(templatePath));
1591-
return answer.getDetails();
1593+
if(answer.getResult())
1594+
return answer.getDetails();
1595+
else
1596+
return null;
15921597
} catch (AgentUnavailableException e) {
15931598
s_logger.error("Unable to send ComputeChecksumCommand to " + hostId, e);
15941599
} catch (OperationTimedoutException e) {
15951600
s_logger.error("Unable to send ComputeChecksumCommand to " + hostId, e);
15961601
}
15971602
return null;
15981603
}
1604+
15991605
// used for vm transitioning to error state
16001606
private void updateVmStateForFailedVmCreation(Long vmId) {
16011607
UserVmVO vm = _vmDao.findById(vmId);

0 commit comments

Comments
 (0)