Skip to content

Commit b7835d0

Browse files
Fix deploy as is VM start after template deletion (#8115)
1 parent b79e393 commit b7835d0

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() {
@@ -662,11 +666,7 @@ public boolean delete(TemplateProfile profile) {
662666
Pair<Class<?>, Long> tmplt = new Pair<Class<?>, Long>(VirtualMachineTemplate.class, template.getId());
663667
_messageBus.publish(_name, EntityManager.MESSAGE_REMOVE_ENTITY_EVENT, PublishScope.LOCAL, tmplt);
664668

665-
// Remove template details
666-
templateDetailsDao.removeDetails(template.getId());
667-
668-
// Remove deploy-as-is details (if any)
669-
templateDeployAsIsDetailsDao.removeDetails(template.getId());
669+
checkAndRemoveTemplateDetails(template);
670670

671671
// Remove comments (if any)
672672
AnnotationService.EntityType entityType = template.getFormat().equals(ImageFormat.ISO) ?
@@ -677,6 +677,23 @@ public boolean delete(TemplateProfile profile) {
677677
return success;
678678
}
679679

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

0 commit comments

Comments
 (0)