Skip to content
Open
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 @@ -316,6 +316,11 @@ void checkDeploymentPlan(VirtualMachine virtualMachine, VirtualMachineTemplate t
ServiceOffering serviceOffering, Account systemAccount, DeploymentPlan plan)
throws InsufficientServerCapacityException;

/**
* Removes the volumes and NICs allocated for a VM, so it can be allocated again with another template.
*/
void cleanupAllocatedResources(VirtualMachine vm);

boolean isBlankInstanceDefaultTemplate(VirtualMachineTemplate template);

boolean isBlankInstance(VirtualMachineTemplate template);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6849,6 +6849,13 @@ public void checkDeploymentPlan(VirtualMachine virtualMachine, VirtualMachineTem
}
}

@Override
public void cleanupAllocatedResources(VirtualMachine vm) {
logger.debug("Cleaning up the volumes and NICs allocated for {}", vm);
volumeMgr.cleanupVolumes(vm.getId());
_networkMgr.cleanupNics(new VirtualMachineProfileImpl(vm));
}

@Override
public boolean isBlankInstanceDefaultTemplate(VirtualMachineTemplate template) {
return KVM_BLANK_VM_TEMPLATE_NAME.equals(template.getUniqueName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@
import com.cloud.storage.clvm.ClvmPoolManager;
import org.apache.cloudstack.api.ApiConstants;
import org.apache.cloudstack.context.CallContext;
import org.apache.cloudstack.engine.orchestration.service.NetworkOrchestrationService;
import org.apache.cloudstack.engine.orchestration.service.VolumeOrchestrationService;
import org.apache.cloudstack.engine.subsystem.api.storage.StoragePoolAllocator;
import org.apache.cloudstack.engine.subsystem.api.storage.VolumeDataFactory;
import org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo;
Expand Down Expand Up @@ -252,6 +254,10 @@ public class VirtualMachineManagerImplTest {
@Mock
private NetworkDao networkDao;
@Mock
private VolumeOrchestrationService volumeOrchestrationServiceMock;
@Mock
private NetworkOrchestrationService networkOrchestrationServiceMock;
@Mock
private AccountDao accountDao;
@Mock
private DomainDao domainDao;
Expand Down Expand Up @@ -2241,4 +2247,13 @@ public void testUpdateClvmLockHostForVmVolumes_MultipleClvmPools() throws Except
verify(clvmPoolManagerMock, times(1)).setClvmLockHostId(3L, destHostId);
}

@Test
public void cleanupAllocatedResourcesRemovesVolumesAndNics() {
when(vmInstanceMock.getId()).thenReturn(1L);

virtualMachineManagerImpl.cleanupAllocatedResources(vmInstanceMock);

verify(volumeOrchestrationServiceMock).cleanupVolumes(1L);
verify(networkOrchestrationServiceMock).cleanupNics(Mockito.argThat(profile -> profile.getVirtualMachine() == vmInstanceMock));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,7 @@ protected Map<String, Object> createProxyInstance(long dataCenterId, List<VMTemp
} catch (InsufficientCapacityException e) {
if (templateIterator.hasNext()) {
logger.debug("Unable to allocate proxy {} with {} in {} due to [{}]. Retrying with another template", proxy, template, dc, e.getMessage(), e);
virtualMachineManager.cleanupAllocatedResources(proxy);
continue;
}
throw new CloudRuntimeException(String.format("Failed to allocate proxy [%s] in zone [%s] with available templates", proxy, dc), e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,7 @@ protected Map<String, Object> createSecStorageVmInstance(long dataCenterId, Seco
} catch (InsufficientCapacityException e) {
if (templateIterator.hasNext()) {
logger.debug("Unable to allocate secondary storage {} with {} due to [{}]. Retrying with another template", secStorageVm, template, e.getMessage(), e);
_itMgr.cleanupAllocatedResources(secStorageVm);
continue;
}
throw new CloudRuntimeException("Failed to allocate secondary storage VM [%s] in zone [%s] with available templates", e);
Expand Down
Loading