Skip to content

Commit 2807d08

Browse files
committed
CLOUDSTACK-4480: Handle DestroyCommand in VmwareResource to evict
unused template from primary storage pool.
1 parent 4218ce4 commit 2807d08

1 file changed

Lines changed: 43 additions & 2 deletions

File tree

‎plugins/hypervisors/vmware/src/com/cloud/hypervisor/vmware/resource/VmwareResource.java‎

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6448,8 +6448,49 @@ public void setRunLevel(int level) {
64486448

64496449
@Override
64506450
public Answer execute(DestroyCommand cmd) {
6451-
// TODO Auto-generated method stub
6452-
return null;
6451+
if (s_logger.isInfoEnabled()) {
6452+
s_logger.info("Executing resource DestroyCommand to evict template from storage pool: " + _gson.toJson(cmd));
6453+
}
6454+
6455+
try {
6456+
VmwareContext context = getServiceContext(null);
6457+
VmwareHypervisorHost hyperHost = getHyperHost(context, null);
6458+
VolumeTO vol = cmd.getVolume();
6459+
6460+
ManagedObjectReference morDs = HypervisorHostHelper.findDatastoreWithBackwardsCompatibility(hyperHost, vol.getPoolUuid());
6461+
if (morDs == null) {
6462+
String msg = "Unable to find datastore based on volume mount point " + vol.getMountPoint();
6463+
s_logger.error(msg);
6464+
throw new Exception(msg);
6465+
}
6466+
6467+
ManagedObjectReference morCluster = hyperHost.getHyperHostCluster();
6468+
ClusterMO clusterMo = new ClusterMO(context, morCluster);
6469+
6470+
VirtualMachineMO vmMo = clusterMo.findVmOnHyperHost(vol.getPath());
6471+
if (vmMo != null) {
6472+
if (s_logger.isInfoEnabled()) {
6473+
s_logger.info("Destroy template volume " + vol.getPath());
6474+
}
6475+
vmMo.destroy();
6476+
}
6477+
else{
6478+
if (s_logger.isInfoEnabled()) {
6479+
s_logger.info("Template volume " + vol.getPath() + " is not found, no need to delete.");
6480+
}
6481+
}
6482+
return new Answer(cmd, true, "Success");
6483+
6484+
} catch (Throwable e) {
6485+
if (e instanceof RemoteException) {
6486+
s_logger.warn("Encounter remote exception to vCenter, invalidate VMware session context");
6487+
invalidateServiceContext(null);
6488+
}
6489+
6490+
String msg = "DestroyCommand failed due to " + VmwareHelper.getExceptionMessage(e);
6491+
s_logger.error(msg, e);
6492+
return new Answer(cmd, false, msg);
6493+
}
64536494
}
64546495
private boolean isVMWareToolsInstalled(VirtualMachineMO vmMo) throws Exception{
64556496
GuestInfo guestInfo = vmMo.getVmGuestInfo();

0 commit comments

Comments
 (0)