Skip to content

Commit 800e9db

Browse files
committed
CLOUDSTACK-6402: Fix StopCommand so that VMs are not removed accidentally as part of vmsync
Added a new flag 'checkBeforeCleanup' to StopCommand based on which check is done to see if VM is running in HV host. If VM is running then in this case it is not stopped and the operation bails out. Also modified the MS code to call the StopCommand with appropriate value for the flag based on the context. Currently it is only set to 'true' when called from the new vmsync logic based on powerstate of VM. For rest it is set to 'false' meaning no change in behaviour.
1 parent b89765a commit 800e9db

8 files changed

Lines changed: 70 additions & 21 deletions

File tree

‎core/src/com/cloud/agent/api/StopCommand.java‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,26 +25,30 @@ public class StopCommand extends RebootCommand {
2525
private String publicConsoleProxyIpAddress = null;
2626
boolean executeInSequence = false;
2727
private GPUDeviceTO gpuDevice;
28+
boolean checkBeforeCleanup = false;
2829

2930
protected StopCommand() {
3031
}
3132

32-
public StopCommand(VirtualMachine vm, boolean isProxy, String urlPort, String publicConsoleProxyIpAddress, boolean executeInSequence) {
33+
public StopCommand(VirtualMachine vm, boolean isProxy, String urlPort, String publicConsoleProxyIpAddress, boolean executeInSequence, boolean checkBeforeCleanup) {
3334
super(vm);
3435
this.isProxy = isProxy;
3536
this.urlPort = urlPort;
3637
this.publicConsoleProxyIpAddress = publicConsoleProxyIpAddress;
3738
this.executeInSequence = executeInSequence;
39+
this.checkBeforeCleanup = checkBeforeCleanup;
3840
}
3941

40-
public StopCommand(VirtualMachine vm, boolean executeInSequence) {
42+
public StopCommand(VirtualMachine vm, boolean executeInSequence, boolean checkBeforeCleanup) {
4143
super(vm);
4244
this.executeInSequence = executeInSequence;
45+
this.checkBeforeCleanup = checkBeforeCleanup;
4346
}
4447

45-
public StopCommand(String vmName, boolean executeInSequence) {
48+
public StopCommand(String vmName, boolean executeInSequence, boolean checkBeforeCleanup) {
4649
super(vmName);
4750
this.executeInSequence = executeInSequence;
51+
this.checkBeforeCleanup = checkBeforeCleanup;
4852
}
4953

5054
@Override
@@ -71,4 +75,8 @@ public GPUDeviceTO getGpuDevice() {
7175
public void setGpuDevice(GPUDeviceTO gpuDevice) {
7276
this.gpuDevice = gpuDevice;
7377
}
78+
79+
public boolean checkBeforeCleanup() {
80+
return this.checkBeforeCleanup;
81+
}
7482
}

‎engine/orchestration/src/com/cloud/vm/VirtualMachineManagerImpl.java‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,7 +1047,7 @@ public void orchestrateStart(String vmUuid, Map<VirtualMachineProfile.Param, Obj
10471047
s_logger.info("The guru did not like the answers so stopping " + vm);
10481048
}
10491049

1050-
StopCommand cmd = new StopCommand(vm, getExecuteInSequence(vm.getHypervisorType()));
1050+
StopCommand cmd = new StopCommand(vm, getExecuteInSequence(vm.getHypervisorType()), false);
10511051
StopAnswer answer = (StopAnswer) _agentMgr.easySend(destHostId, cmd);
10521052
if ( answer != null ) {
10531053

@@ -1225,9 +1225,9 @@ protected boolean getExecuteInSequence(HypervisorType hypervisorType) {
12251225
}
12261226
}
12271227

1228-
protected boolean sendStop(VirtualMachineGuru guru, VirtualMachineProfile profile, boolean force) {
1228+
protected boolean sendStop(VirtualMachineGuru guru, VirtualMachineProfile profile, boolean force, boolean checkBeforeCleanup) {
12291229
VirtualMachine vm = profile.getVirtualMachine();
1230-
StopCommand stop = new StopCommand(vm, getExecuteInSequence(vm.getHypervisorType()));
1230+
StopCommand stop = new StopCommand(vm, getExecuteInSequence(vm.getHypervisorType()), checkBeforeCleanup);
12311231
try {
12321232
StopAnswer answer = (StopAnswer)_agentMgr.send(vm.getHostId(), stop);
12331233
if (answer != null) {
@@ -1282,7 +1282,7 @@ protected boolean cleanup(VirtualMachineGuru guru, VirtualMachineProfile profile
12821282

12831283
if (step == Step.Started || step == Step.Starting || step == Step.Release) {
12841284
if (vm.getHostId() != null) {
1285-
if (!sendStop(guru, profile, cleanUpEvenIfUnableToStop)) {
1285+
if (!sendStop(guru, profile, cleanUpEvenIfUnableToStop, false)) {
12861286
s_logger.warn("Failed to stop vm " + vm + " in " + State.Starting + " state as a part of cleanup process");
12871287
return false;
12881288
}
@@ -1295,26 +1295,26 @@ protected boolean cleanup(VirtualMachineGuru guru, VirtualMachineProfile profile
12951295
}
12961296
} else if (state == State.Stopping) {
12971297
if (vm.getHostId() != null) {
1298-
if (!sendStop(guru, profile, cleanUpEvenIfUnableToStop)) {
1298+
if (!sendStop(guru, profile, cleanUpEvenIfUnableToStop, false)) {
12991299
s_logger.warn("Failed to stop vm " + vm + " in " + State.Stopping + " state as a part of cleanup process");
13001300
return false;
13011301
}
13021302
}
13031303
} else if (state == State.Migrating) {
13041304
if (vm.getHostId() != null) {
1305-
if (!sendStop(guru, profile, cleanUpEvenIfUnableToStop)) {
1305+
if (!sendStop(guru, profile, cleanUpEvenIfUnableToStop, false)) {
13061306
s_logger.warn("Failed to stop vm " + vm + " in " + State.Migrating + " state as a part of cleanup process");
13071307
return false;
13081308
}
13091309
}
13101310
if (vm.getLastHostId() != null) {
1311-
if (!sendStop(guru, profile, cleanUpEvenIfUnableToStop)) {
1311+
if (!sendStop(guru, profile, cleanUpEvenIfUnableToStop, false)) {
13121312
s_logger.warn("Failed to stop vm " + vm + " in " + State.Migrating + " state as a part of cleanup process");
13131313
return false;
13141314
}
13151315
}
13161316
} else if (state == State.Running) {
1317-
if (!sendStop(guru, profile, cleanUpEvenIfUnableToStop)) {
1317+
if (!sendStop(guru, profile, cleanUpEvenIfUnableToStop, false)) {
13181318
s_logger.warn("Failed to stop vm " + vm + " in " + State.Running + " state as a part of cleanup process");
13191319
return false;
13201320
}
@@ -1488,7 +1488,7 @@ private void advanceStop(VMInstanceVO vm, boolean cleanUpEvenIfUnableToStop) thr
14881488

14891489
vmGuru.prepareStop(profile);
14901490

1491-
StopCommand stop = new StopCommand(vm, getExecuteInSequence(vm.getHypervisorType()));
1491+
StopCommand stop = new StopCommand(vm, getExecuteInSequence(vm.getHypervisorType()), false);
14921492

14931493
boolean stopped = false;
14941494
StopAnswer answer = null;
@@ -2464,11 +2464,11 @@ private void orchestrateReboot(String vmUuid, Map<VirtualMachineProfile.Param, O
24642464
}
24652465

24662466
public Command cleanup(VirtualMachine vm) {
2467-
return new StopCommand(vm, getExecuteInSequence(vm.getHypervisorType()));
2467+
return new StopCommand(vm, getExecuteInSequence(vm.getHypervisorType()), false);
24682468
}
24692469

24702470
public Command cleanup(String vmName) {
2471-
return new StopCommand(vmName, getExecuteInSequence(null));
2471+
return new StopCommand(vmName, getExecuteInSequence(null), false);
24722472

24732473
}
24742474

@@ -4257,7 +4257,7 @@ private void handlePowerOffReportWithNoPendingJobsOnVM(VMInstanceVO vm) {
42574257

42584258
VirtualMachineGuru vmGuru = getVmGuru(vm);
42594259
VirtualMachineProfile profile = new VirtualMachineProfileImpl(vm);
4260-
sendStop(vmGuru, profile, true);
4260+
sendStop(vmGuru, profile, true, true);
42614261

42624262
try {
42634263
stateTransitTo(vm, VirtualMachine.Event.FollowAgentPowerOffReport, null);

‎engine/orchestration/test/com/cloud/vm/VirtualMachineManagerImplTest.java‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -465,12 +465,12 @@ public void testSendStopWithOkAnswer() throws Exception {
465465
VirtualMachineGuru guru = mock(VirtualMachineGuru.class);
466466
VirtualMachine vm = mock(VirtualMachine.class);
467467
VirtualMachineProfile profile = mock(VirtualMachineProfile.class);
468-
StopAnswer answer = new StopAnswer(new StopCommand(vm, false), "ok", true);
468+
StopAnswer answer = new StopAnswer(new StopCommand(vm, false, false), "ok", true);
469469
when(profile.getVirtualMachine()).thenReturn(vm);
470470
when(vm.getHostId()).thenReturn(1L);
471471
when(_agentMgr.send(anyLong(), (Command)any())).thenReturn(answer);
472472

473-
boolean actual = _vmMgr.sendStop(guru, profile, false);
473+
boolean actual = _vmMgr.sendStop(guru, profile, false, false);
474474

475475
Assert.assertTrue(actual);
476476
}
@@ -480,12 +480,12 @@ public void testSendStopWithFailAnswer() throws Exception {
480480
VirtualMachineGuru guru = mock(VirtualMachineGuru.class);
481481
VirtualMachine vm = mock(VirtualMachine.class);
482482
VirtualMachineProfile profile = mock(VirtualMachineProfile.class);
483-
StopAnswer answer = new StopAnswer(new StopCommand(vm, false), "fail", false);
483+
StopAnswer answer = new StopAnswer(new StopCommand(vm, false, false), "fail", false);
484484
when(profile.getVirtualMachine()).thenReturn(vm);
485485
when(vm.getHostId()).thenReturn(1L);
486486
when(_agentMgr.send(anyLong(), (Command)any())).thenReturn(answer);
487487

488-
boolean actual = _vmMgr.sendStop(guru, profile, false);
488+
boolean actual = _vmMgr.sendStop(guru, profile, false, false);
489489

490490
Assert.assertFalse(actual);
491491
}
@@ -499,7 +499,7 @@ public void testSendStopWithNullAnswer() throws Exception {
499499
when(vm.getHostId()).thenReturn(1L);
500500
when(_agentMgr.send(anyLong(), (Command)any())).thenReturn(null);
501501

502-
boolean actual = _vmMgr.sendStop(guru, profile, false);
502+
boolean actual = _vmMgr.sendStop(guru, profile, false, false);
503503

504504
Assert.assertFalse(actual);
505505
}

‎plugins/hypervisors/hyperv/DotNet/ServerResource/HypervResource/HypervResourceController.cs‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1135,7 +1135,18 @@ public JContainer StopCommand([FromBody]dynamic cmd)
11351135
logger.Info(CloudStackTypes.StopCommand + Utils.CleanString(cmd.ToString()));
11361136
string details = null;
11371137
bool result = false;
1138+
bool checkBeforeCleanup = cmd.checkBeforeCleanup;
1139+
String vmName = cmd.vmName;
11381140

1141+
if (checkBeforeCleanup == true)
1142+
{
1143+
ComputerSystem vm = wmiCallsV2.GetComputerSystem(vmName);
1144+
if (vm == null || vm.EnabledState == 2)
1145+
{
1146+
// VM is not available or vm in running state
1147+
return ReturnCloudStackTypedJArray(new { result = false, details = "VM is running on host, bailing out", vm = vmName, contextMap = contextMap }, CloudStackTypes.StopAnswer);
1148+
}
1149+
}
11391150
try
11401151
{
11411152
wmiCallsV2.DestroyVm(cmd);

‎plugins/hypervisors/hyperv/DotNet/ServerResource/ServerResource.Tests/HypervResourceController1Test.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ public void TestStopCommand()
281281
HypervResourceController rsrcServer = new HypervResourceController();
282282
HypervResourceController.wmiCallsV2 = wmiCallsV2;
283283

284-
String sampleStop = "{\"isProxy\":false,\"vmName\":\"i-2-17-VM\",\"contextMap\":{},\"wait\":0}";
284+
String sampleStop = "{\"isProxy\":false,\"vmName\":\"i-2-17-VM\",\"contextMap\":{},\"checkBeforeCleanup\":false,\"wait\":0}";
285285
dynamic jsonStopCmd = JsonConvert.DeserializeObject(sampleStop);
286286

287287
// Act

‎plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3484,6 +3484,18 @@ protected GetVmStatsAnswer execute(GetVmStatsCommand cmd) {
34843484
protected Answer execute(StopCommand cmd) {
34853485
final String vmName = cmd.getVmName();
34863486

3487+
if (cmd.checkBeforeCleanup()) {
3488+
try {
3489+
Connect conn = LibvirtConnection.getConnectionByVmName(vmName);
3490+
Domain vm = conn.domainLookupByName(cmd.getVmName());
3491+
if (vm != null && vm.getInfo().state == DomainInfo.DomainState.VIR_DOMAIN_RUNNING) {
3492+
return new StopAnswer(cmd, "vm is still running on host", false);
3493+
}
3494+
} catch (Exception e) {
3495+
s_logger.debug("Failed to get vm status in case of checkboforecleanup is true", e);
3496+
}
3497+
}
3498+
34873499
State state = null;
34883500
synchronized (_vms) {
34893501
state = _vms.get(vmName);

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2740,6 +2740,18 @@ protected Answer execute(StopCommand cmd) {
27402740
try {
27412741
VirtualMachineMO vmMo = hyperHost.findVmOnHyperHost(cmd.getVmName());
27422742
if (vmMo != null) {
2743+
if (cmd.checkBeforeCleanup()) {
2744+
if (getVmPowerState(vmMo) != PowerState.PowerOff) {
2745+
String msg = "StopCommand is sent for cleanup and VM " + cmd.getVmName() + " is current running. ignore it.";
2746+
s_logger.warn(msg);
2747+
return new StopAnswer(cmd, msg, false);
2748+
} else {
2749+
String msg = "StopCommand is sent for cleanup and VM " + cmd.getVmName() + " is indeed stopped already.";
2750+
s_logger.info(msg);
2751+
return new StopAnswer(cmd, msg, true);
2752+
}
2753+
}
2754+
27432755
State state = null;
27442756
synchronized (_vms) {
27452757
state = _vms.get(cmd.getVmName());

‎plugins/hypervisors/xen/src/com/cloud/hypervisor/xen/resource/CitrixResourceBase.java‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3627,6 +3627,12 @@ public StopAnswer execute(StopCommand cmd) {
36273627
return new StopAnswer(cmd, msg, platformstring, false);
36283628
}
36293629

3630+
if (cmd.checkBeforeCleanup() && vmr.powerState == VmPowerState.RUNNING) {
3631+
String msg = "Vm " + vmName + " is running on host and checkBeforeCleanup flag is set, so bailing out";
3632+
s_logger.debug(msg);
3633+
return new StopAnswer(cmd, msg, false);
3634+
}
3635+
36303636
State state = s_vms.getState(_cluster, vmName);
36313637

36323638
synchronized (_cluster.intern()) {

0 commit comments

Comments
 (0)