Merge release branch 4.18 to main

* 4.18:
  Fix deploy as is VM start after template deletion (#8115)
This commit is contained in:
Daan Hoogland 2023-11-14 14:05:39 +01:00
commit c7100c3d75
1 changed files with 22 additions and 5 deletions

View File

@ -29,6 +29,8 @@ import java.util.stream.Collectors;
import javax.inject.Inject;
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.CheckUrlCommand;
import org.apache.cloudstack.annotation.AnnotationService;
@ -142,6 +144,8 @@ public class HypervisorTemplateAdapter extends TemplateAdapterBase {
private TemplateDeployAsIsDetailsDao templateDeployAsIsDetailsDao;
@Inject
private AnnotationDao annotationDao;
@Inject
VMInstanceDao _vmInstanceDao;
@Override
public String getName() {
@ -666,11 +670,7 @@ public class HypervisorTemplateAdapter extends TemplateAdapterBase {
Pair<Class<?>, Long> tmplt = new Pair<Class<?>, Long>(VirtualMachineTemplate.class, template.getId());
_messageBus.publish(_name, EntityManager.MESSAGE_REMOVE_ENTITY_EVENT, PublishScope.LOCAL, tmplt);
// Remove template details
templateDetailsDao.removeDetails(template.getId());
// Remove deploy-as-is details (if any)
templateDeployAsIsDetailsDao.removeDetails(template.getId());
checkAndRemoveTemplateDetails(template);
// Remove comments (if any)
AnnotationService.EntityType entityType = template.getFormat().equals(ImageFormat.ISO) ?
@ -681,6 +681,23 @@ public class HypervisorTemplateAdapter extends TemplateAdapterBase {
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
public TemplateProfile prepareDelete(DeleteTemplateCmd cmd) {
TemplateProfile profile = super.prepareDelete(cmd);