Skip to content
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
46 changes: 46 additions & 0 deletions core/src/main/java/com/cloud/agent/api/CleanupVMCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//
// 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.agent.api;

/**
* This command will destroy a leftover VM during the expunge process if it wasn't destroyed before.
*
*/
public class CleanupVMCommand extends Command {
String vmName;
boolean executeInSequence;

public CleanupVMCommand(String vmName) {
this(vmName, false);
}
public CleanupVMCommand(String vmName, boolean executeInSequence) {
this.vmName = vmName;
this.executeInSequence = executeInSequence;
}

@Override
public boolean executeInSequence() {
return executeInSequence;
}

public String getVmName() {
return vmName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.Map;
import java.util.UUID;

import com.cloud.agent.api.CleanupVMCommand;
import javax.inject.Inject;

import com.cloud.agent.api.to.NfsTO;
Expand Down Expand Up @@ -371,6 +372,13 @@ private static String resolveNameInGuid(String guid) {
return tokens[0] + "@" + vCenterIp;
}

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

@Override public List<Command> finalizeExpungeNics(VirtualMachine vm, List<NicProfile> nics) {
List<Command> commands = new ArrayList<Command>();
List<NicVO> nicVOs = nicDao.listByVmId(vm.getId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import java.util.UUID;
import java.util.stream.Collectors;

import com.cloud.agent.api.CleanupVMCommand;
import javax.naming.ConfigurationException;
import javax.xml.datatype.XMLGregorianCalendar;

Expand Down Expand Up @@ -585,6 +586,8 @@ public Answer executeRequest(Command cmd) {
return execute((ResizeVolumeCommand) cmd);
} else if (clz == UnregisterVMCommand.class) {
return execute((UnregisterVMCommand) cmd);
} else if (clz == CleanupVMCommand.class) {
return execute((CleanupVMCommand) cmd);
} else if (cmd instanceof StorageSubSystemCommand) {
checkStorageProcessorAndHandlerNfsVersionAttribute((StorageSubSystemCommand) cmd);
return storageHandler.handleStorageCommands((StorageSubSystemCommand) cmd);
Expand Down Expand Up @@ -5831,6 +5834,26 @@ protected Answer execute(PvlanSetupCommand cmd) {
return new Answer(cmd, true, "success");
}

protected Answer execute(CleanupVMCommand cmd) {
VmwareContext context = getServiceContext();
VmwareHypervisorHost hyperHost = getHyperHost(context);

try {
VirtualMachineMO vmMo = hyperHost.findVmOnHyperHost(cmd.getVmName());
if (vmMo == null) {
String msg = String.format("VM [%s] not found on vCenter, cleanup not needed.", cmd.getVmName());
s_logger.debug(msg);
return new Answer(cmd, true, msg);
}
vmMo.destroy();
Comment thread
gp-santos marked this conversation as resolved.
String msg = String.format("VM [%s] remnants on vCenter cleaned up.", cmd.getVmName());
s_logger.debug(msg);
return new Answer(cmd, true, msg);
} catch (Exception e) {
return new Answer(cmd, false, createLogMessageException(e, cmd));
}
}

protected Answer execute(UnregisterVMCommand cmd) {
VmwareContext context = getServiceContext();
VmwareHypervisorHost hyperHost = getHyperHost(context);
Expand Down