Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ public interface VMTemplateDao extends GenericDao<VMTemplateVO, Long>, StateDao<

public Long countTemplatesForAccount(long accountId);

public List<VMTemplateVO> listUnRemovedTemplatesByStates(VirtualMachineTemplate.State ...states);

List<VMTemplateVO> findTemplatesToSyncToS3();

void loadDetails(VMTemplateVO tmpl);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ public class VMTemplateDaoImpl extends GenericDaoBase<VMTemplateVO, Long> implem
// private SearchBuilder<VMTemplateVO> updateStateSearch;
private SearchBuilder<VMTemplateVO> AllFieldsSearch;
protected SearchBuilder<VMTemplateVO> ParentTemplateIdSearch;
private SearchBuilder<VMTemplateVO> InactiveUnremovedTmpltSearch;

@Inject
ResourceTagDao _tagsDao;
Expand Down Expand Up @@ -430,6 +431,11 @@ public boolean configure(String name, Map<String, Object> params) throws Configu
ParentTemplateIdSearch.and("state", ParentTemplateIdSearch.entity().getState(), SearchCriteria.Op.EQ);
ParentTemplateIdSearch.done();

InactiveUnremovedTmpltSearch = createSearchBuilder();
InactiveUnremovedTmpltSearch.and("state", InactiveUnremovedTmpltSearch.entity().getState(), SearchCriteria.Op.IN);
InactiveUnremovedTmpltSearch.and("removed", InactiveUnremovedTmpltSearch.entity().getRemoved(), SearchCriteria.Op.NULL);
InactiveUnremovedTmpltSearch.done();
Comment on lines +434 to +437

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this used anywhere?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @DaanHoogland . It was supposed to be referenced at listUnRemovedTemplatesByStates() - fixed it.


return result;
}

Expand Down Expand Up @@ -935,6 +941,13 @@ public Long countTemplatesForAccount(long accountId) {
return customSearch(sc, null).get(0);
}

@Override
public List<VMTemplateVO> listUnRemovedTemplatesByStates(VirtualMachineTemplate.State ...states) {
SearchCriteria<VMTemplateVO> sc = InactiveUnremovedTmpltSearch.create();
sc.setParameters("state", (Object[]) states);
return listBy(sc);
}

@Override
@DB
public boolean remove(Long id) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1153,6 +1153,14 @@ public Pair<Long, Answer> sendToPool(StoragePool pool, long[] hostIdsToTryFirst,
return new Pair<Long, Answer>(result.first(), result.second()[0]);
}

private void cleanupInactiveTemplates() {
List<VMTemplateVO> vmTemplateVOS = _templateDao.listUnRemovedTemplatesByStates(VirtualMachineTemplate.State.Inactive);
for (VMTemplateVO template: vmTemplateVOS) {
template.setRemoved(new Date());
_templateDao.update(template.getId(), template);
}
}

@Override
public void cleanupStorage(boolean recurring) {
GlobalLock scanLock = GlobalLock.getInternLock("storagemgr.cleanup");
Expand Down Expand Up @@ -1324,6 +1332,7 @@ public void cleanupStorage(boolean recurring) {
s_logger.warn("Unable to destroy uploaded template " + template.getUuid() + ". Error details: " + th.getMessage());
}
}
cleanupInactiveTemplates();
} finally {
scanLock.unlock();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@
package com.cloud.template;

import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.stream.Collectors;

import javax.inject.Inject;

Expand Down Expand Up @@ -449,6 +451,18 @@ protected Void createTemplateAsyncCallBack(AsyncCallbackDispatcher<HypervisorTem
return null;
}

boolean cleanupTemplate(VMTemplateVO template, boolean success) {
List<VMTemplateZoneVO> templateZones = templateZoneDao.listByTemplateId(template.getId());
List<Long> zoneIds = templateZones.stream().map(VMTemplateZoneVO::getZoneId).collect(Collectors.toList());
if (zoneIds.size() > 0) {
return success;
}
template.setRemoved(new Date());
template.setState(State.Inactive);
templateDao.update(template.getId(), template);
return success;
}

@Override
@DB
public boolean delete(TemplateProfile profile) {
Expand Down Expand Up @@ -570,7 +584,7 @@ public boolean delete(TemplateProfile profile) {
if (success) {
if ((imageStores != null && imageStores.size() > 1) && (profile.getZoneIdList() != null)) {
//if template is stored in more than one image stores, and the zone id is not null, then don't delete other templates.
return success;
return cleanupTemplate(template, success);
}

// delete all cache entries for this template
Expand All @@ -585,6 +599,7 @@ public boolean delete(TemplateProfile profile) {
if (iStores == null || iStores.size() == 0) {
// Mark template as Inactive.
template.setState(VirtualMachineTemplate.State.Inactive);
_tmpltDao.remove(template.getId());
_tmpltDao.update(template.getId(), template);

// Decrement the number of templates and total secondary storage
Expand Down
5 changes: 2 additions & 3 deletions test/integration/smoke/test_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -1322,6 +1322,7 @@ def test_03_deploy_vm_wrong_checksum(self):
"""
self.template["checksum"]="{MD5}XXXXXXX"
tmpl = Template.register(self.apiclient, self.template, zoneid=self.zone.id, hypervisor=self.hypervisor, randomize_name=False)
self.cleanup.append(tmpl)

try:
virtual_machine = VirtualMachine.create(
Expand All @@ -1332,11 +1333,9 @@ def test_03_deploy_vm_wrong_checksum(self):
domainid=self.account.domainid,
serviceofferingid=self.service_offering.id
)
self.cleanup.append(tmpl)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'd say this, self.cleanup.append(tmpl) is supposed to go to line 1325,
and this line should read self.cleanup.append(virtual_machine)

lines 1339 and 1340 should be removed. As this code is at the moment virtual_machne will be appended to cleanup only if the create fails and self.fail(...) is not called.

a cleanup should go directly after a create (or register) and only if the remove happens for some other reason it should be removed from the list again (if a double delete would be a problem)

self.cleanup.append(virtual_machine)
self.fail("Expected to fail deployment")
except Exception as e:
self.debug("Expected exception")

self.cleanup.append(virtual_machine)
self.cleanup.append(tmpl)
return
1 change: 1 addition & 0 deletions ui/.env.local
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CS_URL=http://10.0.35.240:8080