Skip to content

xenserver: destroy halted vm on expunge #10833

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 12, 2025
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 @@ -35,6 +35,7 @@
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

import com.cloud.agent.api.CleanupVMCommand;
import com.cloud.agent.api.Command;
import com.cloud.agent.api.to.DataObjectType;
import com.cloud.agent.api.to.DataStoreTO;
Expand Down Expand Up @@ -236,4 +237,12 @@
public ConfigKey<?>[] getConfigKeys() {
return new ConfigKey<?>[] {MaxNumberOfVCPUSPerVM};
}

@Override
public List<Command> finalizeExpunge(VirtualMachine vm) {
List<Command> commands = new ArrayList<>();
final CleanupVMCommand cleanupVMCommand = new CleanupVMCommand(vm.getInstanceName(), true);
commands.add(cleanupVMCommand);
return commands;
}

Check warning on line 247 in plugins/hypervisors/xenserver/src/main/java/com/cloud/hypervisor/XenServerGuru.java

View check run for this annotation

Codecov / codecov/patch

plugins/hypervisors/xenserver/src/main/java/com/cloud/hypervisor/XenServerGuru.java#L242-L247

Added lines #L242 - L247 were not covered by tests
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package com.cloud.hypervisor.xenserver.resource.wrapper.xenbase;

import java.util.Iterator;
import java.util.Set;

import org.apache.log4j.Logger;

import com.cloud.agent.api.Answer;
import com.cloud.agent.api.CleanupVMCommand;
import com.cloud.hypervisor.xenserver.resource.CitrixResourceBase;
import com.cloud.resource.CommandWrapper;
import com.cloud.resource.ResourceWrapper;
import com.xensource.xenapi.Connection;
import com.xensource.xenapi.Types;
import com.xensource.xenapi.VM;

@ResourceWrapper(handles = CleanupVMCommand.class)
public class CitrixCleanupVMCommandWrapper extends CommandWrapper<CleanupVMCommand, Answer, CitrixResourceBase> {

private static final Logger s_logger = Logger.getLogger(CitrixCleanupVMCommandWrapper.class);

@Override
public Answer execute(final CleanupVMCommand command, final CitrixResourceBase citrixResourceBase) {

Check warning on line 40 in plugins/hypervisors/xenserver/src/main/java/com/cloud/hypervisor/xenserver/resource/wrapper/xenbase/CitrixCleanupVMCommandWrapper.java

View check run for this annotation

Codecov / codecov/patch

plugins/hypervisors/xenserver/src/main/java/com/cloud/hypervisor/xenserver/resource/wrapper/xenbase/CitrixCleanupVMCommandWrapper.java#L40

Added line #L40 was not covered by tests
if (citrixResourceBase.isDestroyHaltedVms()) {
s_logger.debug(String.format("Cleanup VM is not needed for host with version %s",
citrixResourceBase.getHost().getProductVersion()));
return new Answer(command);

Check warning on line 44 in plugins/hypervisors/xenserver/src/main/java/com/cloud/hypervisor/xenserver/resource/wrapper/xenbase/CitrixCleanupVMCommandWrapper.java

View check run for this annotation

Codecov / codecov/patch

plugins/hypervisors/xenserver/src/main/java/com/cloud/hypervisor/xenserver/resource/wrapper/xenbase/CitrixCleanupVMCommandWrapper.java#L42-L44

Added lines #L42 - L44 were not covered by tests
}
final String vmName = command.getVmName();
try {
final Connection conn = citrixResourceBase.getConnection();
final Set<VM> vms = VM.getByNameLabel(conn, vmName);

Check warning on line 49 in plugins/hypervisors/xenserver/src/main/java/com/cloud/hypervisor/xenserver/resource/wrapper/xenbase/CitrixCleanupVMCommandWrapper.java

View check run for this annotation

Codecov / codecov/patch

plugins/hypervisors/xenserver/src/main/java/com/cloud/hypervisor/xenserver/resource/wrapper/xenbase/CitrixCleanupVMCommandWrapper.java#L46-L49

Added lines #L46 - L49 were not covered by tests
if (vms.isEmpty()) {
return new Answer(command, true, "VM does not exist");

Check warning on line 51 in plugins/hypervisors/xenserver/src/main/java/com/cloud/hypervisor/xenserver/resource/wrapper/xenbase/CitrixCleanupVMCommandWrapper.java

View check run for this annotation

Codecov / codecov/patch

plugins/hypervisors/xenserver/src/main/java/com/cloud/hypervisor/xenserver/resource/wrapper/xenbase/CitrixCleanupVMCommandWrapper.java#L51

Added line #L51 was not covered by tests
}
// destroy vm which is in HALTED state on this host
final Iterator<VM> iter = vms.iterator();

Check warning on line 54 in plugins/hypervisors/xenserver/src/main/java/com/cloud/hypervisor/xenserver/resource/wrapper/xenbase/CitrixCleanupVMCommandWrapper.java

View check run for this annotation

Codecov / codecov/patch

plugins/hypervisors/xenserver/src/main/java/com/cloud/hypervisor/xenserver/resource/wrapper/xenbase/CitrixCleanupVMCommandWrapper.java#L54

Added line #L54 was not covered by tests
while (iter.hasNext()) {
final VM vm = iter.next();
final VM.Record vmr = vm.getRecord(conn);

Check warning on line 57 in plugins/hypervisors/xenserver/src/main/java/com/cloud/hypervisor/xenserver/resource/wrapper/xenbase/CitrixCleanupVMCommandWrapper.java

View check run for this annotation

Codecov / codecov/patch

plugins/hypervisors/xenserver/src/main/java/com/cloud/hypervisor/xenserver/resource/wrapper/xenbase/CitrixCleanupVMCommandWrapper.java#L56-L57

Added lines #L56 - L57 were not covered by tests
if (!Types.VmPowerState.HALTED.equals(vmr.powerState)) {
final String msg = String.format("VM %s is not in %s state", vmName, Types.VmPowerState.HALTED);
s_logger.error(msg);
return new Answer(command, false, msg);

Check warning on line 61 in plugins/hypervisors/xenserver/src/main/java/com/cloud/hypervisor/xenserver/resource/wrapper/xenbase/CitrixCleanupVMCommandWrapper.java

View check run for this annotation

Codecov / codecov/patch

plugins/hypervisors/xenserver/src/main/java/com/cloud/hypervisor/xenserver/resource/wrapper/xenbase/CitrixCleanupVMCommandWrapper.java#L59-L61

Added lines #L59 - L61 were not covered by tests
}
if (citrixResourceBase.isRefNull(vmr.residentOn)) {
continue;

Check warning on line 64 in plugins/hypervisors/xenserver/src/main/java/com/cloud/hypervisor/xenserver/resource/wrapper/xenbase/CitrixCleanupVMCommandWrapper.java

View check run for this annotation

Codecov / codecov/patch

plugins/hypervisors/xenserver/src/main/java/com/cloud/hypervisor/xenserver/resource/wrapper/xenbase/CitrixCleanupVMCommandWrapper.java#L64

Added line #L64 was not covered by tests
}
if (vmr.residentOn.getUuid(conn).equals(citrixResourceBase.getHost().getUuid())) {
continue;

Check warning on line 67 in plugins/hypervisors/xenserver/src/main/java/com/cloud/hypervisor/xenserver/resource/wrapper/xenbase/CitrixCleanupVMCommandWrapper.java

View check run for this annotation

Codecov / codecov/patch

plugins/hypervisors/xenserver/src/main/java/com/cloud/hypervisor/xenserver/resource/wrapper/xenbase/CitrixCleanupVMCommandWrapper.java#L67

Added line #L67 was not covered by tests
}
iter.remove();
}

Check warning on line 70 in plugins/hypervisors/xenserver/src/main/java/com/cloud/hypervisor/xenserver/resource/wrapper/xenbase/CitrixCleanupVMCommandWrapper.java

View check run for this annotation

Codecov / codecov/patch

plugins/hypervisors/xenserver/src/main/java/com/cloud/hypervisor/xenserver/resource/wrapper/xenbase/CitrixCleanupVMCommandWrapper.java#L69-L70

Added lines #L69 - L70 were not covered by tests
for (final VM vm : vms) {
citrixResourceBase.destroyVm(vm, conn, true);
}

Check warning on line 73 in plugins/hypervisors/xenserver/src/main/java/com/cloud/hypervisor/xenserver/resource/wrapper/xenbase/CitrixCleanupVMCommandWrapper.java

View check run for this annotation

Codecov / codecov/patch

plugins/hypervisors/xenserver/src/main/java/com/cloud/hypervisor/xenserver/resource/wrapper/xenbase/CitrixCleanupVMCommandWrapper.java#L72-L73

Added lines #L72 - L73 were not covered by tests

} catch (final Exception e) {
final String msg = String.format("Clean up VM %s fail due to %s", vmName, e);
s_logger.error(msg, e);
return new Answer(command, false, e.getMessage());
}
return new Answer(command);
}

Check warning on line 81 in plugins/hypervisors/xenserver/src/main/java/com/cloud/hypervisor/xenserver/resource/wrapper/xenbase/CitrixCleanupVMCommandWrapper.java

View check run for this annotation

Codecov / codecov/patch

plugins/hypervisors/xenserver/src/main/java/com/cloud/hypervisor/xenserver/resource/wrapper/xenbase/CitrixCleanupVMCommandWrapper.java#L75-L81

Added lines #L75 - L81 were not covered by tests
}
Loading