Skip to content

Commit 2fe7575

Browse files
harikrishna-patnaladhslove
authored andcommitted
Fix deploy as is VM start after template deletion (apache#8115)
1 parent 82de527 commit 2fe7575

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

server/src/main/java/com/cloud/template/HypervisorTemplateAdapter.java

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
import javax.inject.Inject;
3030

3131
import com.cloud.domain.Domain;
32+
import com.cloud.vm.VMInstanceVO;
33+
import com.cloud.vm.dao.VMInstanceDao;
3234
import org.apache.cloudstack.agent.directdownload.CheckUrlAnswer;
3335
import org.apache.cloudstack.agent.directdownload.CheckUrlCommand;
3436
import org.apache.cloudstack.annotation.AnnotationService;
@@ -142,6 +144,8 @@ public class HypervisorTemplateAdapter extends TemplateAdapterBase {
142144
private TemplateDeployAsIsDetailsDao templateDeployAsIsDetailsDao;
143145
@Inject
144146
private AnnotationDao annotationDao;
147+
@Inject
148+
VMInstanceDao _vmInstanceDao;
145149

146150
@Override
147151
public String getName() {
@@ -666,11 +670,7 @@ public boolean delete(TemplateProfile profile) {
666670
Pair<Class<?>, Long> tmplt = new Pair<Class<?>, Long>(VirtualMachineTemplate.class, template.getId());
667671
_messageBus.publish(_name, EntityManager.MESSAGE_REMOVE_ENTITY_EVENT, PublishScope.LOCAL, tmplt);
668672

669-
// Remove template details
670-
templateDetailsDao.removeDetails(template.getId());
671-
672-
// Remove deploy-as-is details (if any)
673-
templateDeployAsIsDetailsDao.removeDetails(template.getId());
673+
checkAndRemoveTemplateDetails(template);
674674

675675
// Remove comments (if any)
676676
AnnotationService.EntityType entityType = template.getFormat().equals(ImageFormat.ISO) ?
@@ -681,6 +681,23 @@ public boolean delete(TemplateProfile profile) {
681681
return success;
682682
}
683683

684+
/**
685+
* removes details of the template and
686+
* if the template is registered as deploy as is,
687+
* then it also deletes the details related to deploy as is only if there are no VMs using the template
688+
* @param template
689+
*/
690+
void checkAndRemoveTemplateDetails(VMTemplateVO template) {
691+
templateDetailsDao.removeDetails(template.getId());
692+
693+
if (template.isDeployAsIs()) {
694+
List<VMInstanceVO> vmInstanceVOList = _vmInstanceDao.listNonExpungedByTemplate(template.getId());
695+
if (CollectionUtils.isEmpty(vmInstanceVOList)) {
696+
templateDeployAsIsDetailsDao.removeDetails(template.getId());
697+
}
698+
}
699+
}
700+
684701
@Override
685702
public TemplateProfile prepareDelete(DeleteTemplateCmd cmd) {
686703
TemplateProfile profile = super.prepareDelete(cmd);

0 commit comments

Comments
 (0)