Skip to content

Commit e68abcd

Browse files
authored
xenserver: destroy halted vm on expunge (#10833)
* xenserver: destroy halted vm on expunge Signed-off-by: Abhishek Kumar <[email protected]>
1 parent 0a090f4 commit e68abcd

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed

plugins/hypervisors/xenserver/src/main/java/com/cloud/hypervisor/XenServerGuru.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.apache.commons.lang3.StringUtils;
3636
import org.apache.log4j.Logger;
3737

38+
import com.cloud.agent.api.CleanupVMCommand;
3839
import com.cloud.agent.api.Command;
3940
import com.cloud.agent.api.to.DataObjectType;
4041
import com.cloud.agent.api.to.DataStoreTO;
@@ -236,4 +237,12 @@ public String getConfigComponentName() {
236237
public ConfigKey<?>[] getConfigKeys() {
237238
return new ConfigKey<?>[] {MaxNumberOfVCPUSPerVM};
238239
}
240+
241+
@Override
242+
public List<Command> finalizeExpunge(VirtualMachine vm) {
243+
List<Command> commands = new ArrayList<>();
244+
final CleanupVMCommand cleanupVMCommand = new CleanupVMCommand(vm.getInstanceName(), true);
245+
commands.add(cleanupVMCommand);
246+
return commands;
247+
}
239248
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package com.cloud.hypervisor.xenserver.resource.wrapper.xenbase;
19+
20+
import java.util.Iterator;
21+
import java.util.Set;
22+
23+
import org.apache.log4j.Logger;
24+
25+
import com.cloud.agent.api.Answer;
26+
import com.cloud.agent.api.CleanupVMCommand;
27+
import com.cloud.hypervisor.xenserver.resource.CitrixResourceBase;
28+
import com.cloud.resource.CommandWrapper;
29+
import com.cloud.resource.ResourceWrapper;
30+
import com.xensource.xenapi.Connection;
31+
import com.xensource.xenapi.Types;
32+
import com.xensource.xenapi.VM;
33+
34+
@ResourceWrapper(handles = CleanupVMCommand.class)
35+
public class CitrixCleanupVMCommandWrapper extends CommandWrapper<CleanupVMCommand, Answer, CitrixResourceBase> {
36+
37+
private static final Logger s_logger = Logger.getLogger(CitrixCleanupVMCommandWrapper.class);
38+
39+
@Override
40+
public Answer execute(final CleanupVMCommand command, final CitrixResourceBase citrixResourceBase) {
41+
if (citrixResourceBase.isDestroyHaltedVms()) {
42+
s_logger.debug(String.format("Cleanup VM is not needed for host with version %s",
43+
citrixResourceBase.getHost().getProductVersion()));
44+
return new Answer(command);
45+
}
46+
final String vmName = command.getVmName();
47+
try {
48+
final Connection conn = citrixResourceBase.getConnection();
49+
final Set<VM> vms = VM.getByNameLabel(conn, vmName);
50+
if (vms.isEmpty()) {
51+
return new Answer(command, true, "VM does not exist");
52+
}
53+
// destroy vm which is in HALTED state on this host
54+
final Iterator<VM> iter = vms.iterator();
55+
while (iter.hasNext()) {
56+
final VM vm = iter.next();
57+
final VM.Record vmr = vm.getRecord(conn);
58+
if (!Types.VmPowerState.HALTED.equals(vmr.powerState)) {
59+
final String msg = String.format("VM %s is not in %s state", vmName, Types.VmPowerState.HALTED);
60+
s_logger.error(msg);
61+
return new Answer(command, false, msg);
62+
}
63+
if (citrixResourceBase.isRefNull(vmr.residentOn)) {
64+
continue;
65+
}
66+
if (vmr.residentOn.getUuid(conn).equals(citrixResourceBase.getHost().getUuid())) {
67+
continue;
68+
}
69+
iter.remove();
70+
}
71+
for (final VM vm : vms) {
72+
citrixResourceBase.destroyVm(vm, conn, true);
73+
}
74+
75+
} catch (final Exception e) {
76+
final String msg = String.format("Clean up VM %s fail due to %s", vmName, e);
77+
s_logger.error(msg, e);
78+
return new Answer(command, false, e.getMessage());
79+
}
80+
return new Answer(command);
81+
}
82+
}

0 commit comments

Comments
 (0)