Fix deploy as is VM start after template deletion (#8115)

This commit is contained in:
Harikrishna 2023-11-14 14:01:53 +05:30 committed by GitHub
parent b79e3937b4
commit b7835d02d2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -29,6 +29,8 @@ import java.util.stream.Collectors;
import javax.inject.Inject; import javax.inject.Inject;
import com.cloud.domain.Domain; import com.cloud.domain.Domain;
import com.cloud.vm.VMInstanceVO;
import com.cloud.vm.dao.VMInstanceDao;
import org.apache.cloudstack.agent.directdownload.CheckUrlAnswer; import org.apache.cloudstack.agent.directdownload.CheckUrlAnswer;
import org.apache.cloudstack.agent.directdownload.CheckUrlCommand; import org.apache.cloudstack.agent.directdownload.CheckUrlCommand;
import org.apache.cloudstack.annotation.AnnotationService; import org.apache.cloudstack.annotation.AnnotationService;
@ -142,6 +144,8 @@ public class HypervisorTemplateAdapter extends TemplateAdapterBase {
private TemplateDeployAsIsDetailsDao templateDeployAsIsDetailsDao; private TemplateDeployAsIsDetailsDao templateDeployAsIsDetailsDao;
@Inject @Inject
private AnnotationDao annotationDao; private AnnotationDao annotationDao;
@Inject
VMInstanceDao _vmInstanceDao;
@Override @Override
public String getName() { public String getName() {
@ -662,11 +666,7 @@ public class HypervisorTemplateAdapter extends TemplateAdapterBase {
Pair<Class<?>, Long> tmplt = new Pair<Class<?>, Long>(VirtualMachineTemplate.class, template.getId()); Pair<Class<?>, Long> tmplt = new Pair<Class<?>, Long>(VirtualMachineTemplate.class, template.getId());
_messageBus.publish(_name, EntityManager.MESSAGE_REMOVE_ENTITY_EVENT, PublishScope.LOCAL, tmplt); _messageBus.publish(_name, EntityManager.MESSAGE_REMOVE_ENTITY_EVENT, PublishScope.LOCAL, tmplt);
// Remove template details checkAndRemoveTemplateDetails(template);
templateDetailsDao.removeDetails(template.getId());
// Remove deploy-as-is details (if any)
templateDeployAsIsDetailsDao.removeDetails(template.getId());
// Remove comments (if any) // Remove comments (if any)
AnnotationService.EntityType entityType = template.getFormat().equals(ImageFormat.ISO) ? AnnotationService.EntityType entityType = template.getFormat().equals(ImageFormat.ISO) ?
@ -677,6 +677,23 @@ public class HypervisorTemplateAdapter extends TemplateAdapterBase {
return success; return success;
} }
/**
* removes details of the template and
* if the template is registered as deploy as is,
* then it also deletes the details related to deploy as is only if there are no VMs using the template
* @param template
*/
void checkAndRemoveTemplateDetails(VMTemplateVO template) {
templateDetailsDao.removeDetails(template.getId());
if (template.isDeployAsIs()) {
List<VMInstanceVO> vmInstanceVOList = _vmInstanceDao.listNonExpungedByTemplate(template.getId());
if (CollectionUtils.isEmpty(vmInstanceVOList)) {
templateDeployAsIsDetailsDao.removeDetails(template.getId());
}
}
}
@Override @Override
public TemplateProfile prepareDelete(DeleteTemplateCmd cmd) { public TemplateProfile prepareDelete(DeleteTemplateCmd cmd) {
TemplateProfile profile = super.prepareDelete(cmd); TemplateProfile profile = super.prepareDelete(cmd);