Skip to content
Merged
Show file tree
Hide file tree
Changes from 15 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
6 changes: 4 additions & 2 deletions api/src/main/java/com/cloud/vm/UserVmService.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
import com.cloud.template.VirtualMachineTemplate;
import com.cloud.user.Account;
import com.cloud.uservm.UserVm;
import com.cloud.utils.Pair;
import com.cloud.utils.exception.ExecutionException;

public interface UserVmService {
Expand Down Expand Up @@ -514,9 +515,10 @@ UserVm importVM(final DataCenter zone, final Host host, final VirtualMachineTemp

/**
* Unmanage a guest VM from CloudStack
* @return true if the VM is successfully unmanaged, false if not.
*
* @return (true if successful, false if not, hostUuid) if the VM is successfully unmanaged.
*/
boolean unmanageUserVM(Long vmId);
Pair<Boolean, String> unmanageUserVM(Long vmId, Long targetHostId);

UserVm allocateVMFromBackup(CreateVMFromBackupCmd cmd) throws InsufficientCapacityException, ResourceAllocationException, ResourceUnavailableException;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.cloud.exception.ResourceUnavailableException;
import com.cloud.user.Account;
import com.cloud.uservm.UserVm;
import com.cloud.utils.Pair;
import com.cloud.vm.VirtualMachine;
import org.apache.cloudstack.acl.RoleType;
import org.apache.cloudstack.api.APICommand;
Expand All @@ -36,6 +37,7 @@
import org.apache.cloudstack.api.BaseAsyncCmd;
import org.apache.cloudstack.api.Parameter;
import org.apache.cloudstack.api.ServerApiException;
import org.apache.cloudstack.api.response.HostResponse;
import org.apache.cloudstack.api.response.UnmanageVMInstanceResponse;
import org.apache.cloudstack.api.response.UserVmResponse;
import org.apache.cloudstack.context.CallContext;
Expand Down Expand Up @@ -65,6 +67,12 @@ public class UnmanageVMInstanceCmd extends BaseAsyncCmd {
description = "The ID of the virtual machine to unmanage")
private Long vmId;

@Parameter(name = ApiConstants.HOST_ID, type = CommandType.UUID,
entityType = HostResponse.class, required = false,
description = "ID of the host where domain XML is stored for stopped Instance",
Comment thread
sudo87 marked this conversation as resolved.
Outdated
since = "4.22.0")
private Long hostId;

/////////////////////////////////////////////////////
/////////////////// Accessors ///////////////////////
/////////////////////////////////////////////////////
Expand All @@ -83,6 +91,14 @@ public String getEventDescription() {
return "unmanaging VM. VM ID = " + vmId;
}

public Long getHostId() {
return hostId;
}

public void setHostId(Long hostId) {
this.hostId = hostId;
}

/////////////////////////////////////////////////////
/////////////// API Implementation///////////////////
/////////////////////////////////////////////////////
Expand All @@ -93,9 +109,10 @@ public void execute() throws ResourceUnavailableException, InsufficientCapacityE
UnmanageVMInstanceResponse response = new UnmanageVMInstanceResponse();
try {
CallContext.current().setEventDetails("VM ID = " + vmId);
boolean result = unmanagedVMsManager.unmanageVMInstance(vmId);
response.setSuccess(result);
if (result) {
Pair<Boolean, String> result = unmanagedVMsManager.unmanageVMInstance(vmId, hostId);
if (result.first()) {
response.setSuccess(true);
response.setHostId(result.second());
response.setDetails("VM unmanaged successfully");
}
} catch (Exception e) {
Expand Down Expand Up @@ -124,5 +141,4 @@ public ApiCommandResourceType getApiResourceType() {
public Long getApiResourceId() {
return vmId;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ public class UnmanageVMInstanceResponse extends BaseResponse {
@Param(description = "details of the unmanage VM operation")
private String details;

@SerializedName(ApiConstants.HOST_ID)
@Param(description = "The ID of the host used for unmanaged Instance")
private String hostId;

public UnmanageVMInstanceResponse() {
}

Expand All @@ -55,4 +59,12 @@ public String getDetails() {
public void setDetails(String details) {
this.details = details;
}

public String getHostId() {
return hostId;
}

public void setHostId(String hostId) {
this.hostId = hostId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@

package org.apache.cloudstack.vm;

import com.cloud.utils.Pair;

public interface UnmanageVMService {

/**
* Unmanage a guest VM from CloudStack
* @return true if the VM is successfully unmanaged, false if not.
*
* @return (true if successful, false if not, hostUuid) if the VM is successfully unmanaged.
*/
boolean unmanageVMInstance(long vmId);
Pair<Boolean, String> unmanageVMInstance(long vmId, Long paramHostId);
}
27 changes: 27 additions & 0 deletions core/src/main/java/com/cloud/agent/api/UnmanageInstanceAnswer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// 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;

public class UnmanageInstanceAnswer extends Answer {

public UnmanageInstanceAnswer(UnmanageInstanceCommand cmd, boolean success, String details) {
super(cmd, success, details);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//
// 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;

import com.cloud.agent.api.to.VirtualMachineTO;

/**
*/
public class UnmanageInstanceCommand extends Command {
String instanceName;
boolean executeInSequence = false;
VirtualMachineTO vm;

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

public UnmanageInstanceCommand(VirtualMachineTO vm) {
this.vm = vm;
this.instanceName = vm.getName();
}

public UnmanageInstanceCommand(String instanceName) {
this.instanceName = instanceName;
}

public String getInstanceName() {
return instanceName;
}

public VirtualMachineTO getVm() {
return vm;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ static String getHypervisorHostname(String name) {
* - Remove the references of the VM and its volumes, nics, IPs from database
* - Keep the VM as it is on the hypervisor
*/
boolean unmanage(String vmUuid);
Pair<Boolean, String> unmanage(String vmUuid, Long paramHostId);

UserVm restoreVirtualMachine(long vmId, Long newTemplateId, Long rootDiskOfferingId, boolean expunge, Map<String, String> details) throws ResourceUnavailableException, InsufficientCapacityException, ResourceAllocationException;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
import org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStoreDriver;
import org.apache.cloudstack.engine.subsystem.api.storage.StoragePoolAllocator;
import org.apache.cloudstack.engine.subsystem.api.storage.VolumeDataFactory;
import org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo;
import org.apache.cloudstack.framework.ca.Certificate;
import org.apache.cloudstack.framework.config.ConfigKey;
import org.apache.cloudstack.framework.config.Configurable;
Expand Down Expand Up @@ -150,11 +151,13 @@
import com.cloud.agent.api.StopCommand;
import com.cloud.agent.api.UnPlugNicAnswer;
import com.cloud.agent.api.UnPlugNicCommand;
import com.cloud.agent.api.UnmanageInstanceCommand;
import com.cloud.agent.api.UnregisterVMCommand;
import com.cloud.agent.api.VmDiskStatsEntry;
import com.cloud.agent.api.VmNetworkStatsEntry;
import com.cloud.agent.api.VmStatsEntry;
import com.cloud.agent.api.routing.NetworkElementCommand;
import com.cloud.agent.api.to.DataTO;
import com.cloud.agent.api.to.DiskTO;
import com.cloud.agent.api.to.DpdkTO;
import com.cloud.agent.api.to.GPUDeviceTO;
Expand Down Expand Up @@ -2001,7 +2004,7 @@ public boolean getExecuteInSequence(final HypervisorType hypervisorType) {
}

@Override
public boolean unmanage(String vmUuid) {
public Pair<Boolean, String> unmanage(String vmUuid, Long paramHostId) {
VMInstanceVO vm = _vmDao.findByUuid(vmUuid);
if (vm == null || vm.getRemoved() != null) {
throw new CloudRuntimeException("Could not find VM with id = " + vmUuid);
Expand All @@ -2014,6 +2017,10 @@ public boolean unmanage(String vmUuid) {
throw new ConcurrentOperationException(msg);
}

Long agentHostId = vm.getHostId();
if (HypervisorType.KVM.equals(vm.getHypervisorType())) {
agentHostId = persistDomainForKVM(vm, paramHostId);
}
Boolean result = Transaction.execute(new TransactionCallback<Boolean>() {
@Override
public Boolean doInTransaction(TransactionStatus status) {
Expand All @@ -2037,8 +2044,49 @@ public Boolean doInTransaction(TransactionStatus status) {
return true;
}
});
HostVO host = ApiDBUtils.findHostById(agentHostId);
if (host == null) {
throw new CloudRuntimeException("Unable to retrieve host with ID: " + agentHostId);
}
logger.debug("Selected host UUID: {} to unmanage Instance: {}.", host.getUuid(), vm.getName());
return new Pair<>(result, host.getUuid());
}

Long persistDomainForKVM(VMInstanceVO vm, Long paramHostId) {
Long agentHostId = vm.getHostId();
String vmName = vm.getName();
UnmanageInstanceCommand unmanageInstanceCommand;
if (State.Stopped.equals(vm.getState())) {
if (paramHostId == null) {
Pair<Long, Long> clusterAndHostId = findClusterAndHostIdForVm(vm, false);
agentHostId = clusterAndHostId.second();
if (agentHostId == null) {
String errorMsg = "No available host to persist domain XML for Instance: " + vmName;
logger.debug(errorMsg);
throw new CloudRuntimeException(errorMsg);
}
} else {
agentHostId = paramHostId;
}
unmanageInstanceCommand = new UnmanageInstanceCommand(prepVmSpecForUnmanageCmd(vm.getId(), agentHostId)); // reconstruct vmSpec for stopped instance
} else {
unmanageInstanceCommand = new UnmanageInstanceCommand(vmName);
}

return BooleanUtils.isTrue(result);
logger.debug("Selected host ID: {} to persist domain XML for Instance: {}.", agentHostId, vmName);
try {
Answer answer = _agentMgr.send(agentHostId, unmanageInstanceCommand);
if (!answer.getResult()) {
String errorMsg = "Failed to persist domain XML for Instance: " + vmName + " on host ID: " + agentHostId;
logger.debug(errorMsg);
throw new CloudRuntimeException(errorMsg);
}
} catch (AgentUnavailableException | OperationTimedoutException e) {
String errorMsg = "Failed to send command to persist domain XML for Instance: " + vmName + " on host ID: " + agentHostId;
logger.error(errorMsg, e);
throw new CloudRuntimeException(errorMsg);
}
return agentHostId;
}

/**
Expand Down Expand Up @@ -4004,6 +4052,62 @@ private void checkAndSetEnterSetupMode(VirtualMachineTO vmTo, Map<VirtualMachine
vmTo.setEnterHardwareSetup(enterSetup == null ? false : enterSetup);
}

/**
* This method helps constructing vmSpec for Unmanage operation for Stopped Instance
* @param vmId
* @param hostId
* @return VirtualMachineTO
*/
protected VirtualMachineTO prepVmSpecForUnmanageCmd(Long vmId, Long hostId) {
final VMInstanceVO vm = _vmDao.findById(vmId);
final Account owner = _entityMgr.findById(Account.class, vm.getAccountId());
final ServiceOfferingVO offering = _offeringDao.findById(vm.getId(), vm.getServiceOfferingId());
final VirtualMachineTemplate template = _entityMgr.findByIdIncludingRemoved(VirtualMachineTemplate.class, vm.getTemplateId());
Host host = _hostDao.findById(hostId);
VirtualMachineProfileImpl vmProfile = new VirtualMachineProfileImpl(vm, template, offering, owner, null);
updateOverCommitRatioForVmProfile(vmProfile, host.getClusterId());
final List<NicVO> nics = _nicsDao.listByVmId(vmProfile.getId());
Collections.sort(nics, (nic1, nic2) -> {
Long nicId1 = Long.valueOf(nic1.getDeviceId());
Long nicId2 = Long.valueOf(nic2.getDeviceId());
return nicId1.compareTo(nicId2);
});

for (final NicVO nic : nics) {
final Network network = _networkModel.getNetwork(nic.getNetworkId());
final NicProfile nicProfile =
new NicProfile(nic, network, nic.getBroadcastUri(), nic.getIsolationUri(), null, _networkModel.isSecurityGroupSupportedInNetwork(network),
_networkModel.getNetworkTag(vmProfile.getHypervisorType(), network));
vmProfile.addNic(nicProfile);
}

List<VolumeVO> volumes = _volsDao.findUsableVolumesForInstance(vmId);
for (VolumeVO vol: volumes) {
VolumeInfo volumeInfo = volumeDataFactory.getVolume(vol.getId());
DataTO dataTO = volumeInfo.getTO();
DiskTO disk = storageMgr.getDiskWithThrottling(dataTO, vol.getVolumeType(), vol.getDeviceId(), vol.getPath(), vm.getServiceOfferingId(), vol.getDiskOfferingId());
vmProfile.addDisk(disk);
}

Map<String, String> details = vmInstanceDetailsDao.listDetailsKeyPairs(vmId,
List.of(VirtualMachineProfile.Param.BootType.getName(), VirtualMachineProfile.Param.BootMode.getName(),
VirtualMachineProfile.Param.UefiFlag.getName()));

if (details.containsKey(VirtualMachineProfile.Param.BootType.getName())) {
vmProfile.getParameters().put(VirtualMachineProfile.Param.BootType, details.get(VirtualMachineProfile.Param.BootType.getName()));
}

if (details.containsKey(VirtualMachineProfile.Param.BootMode.getName())) {
vmProfile.getParameters().put(VirtualMachineProfile.Param.BootMode, details.get(VirtualMachineProfile.Param.BootMode.getName()));
}

if (details.containsKey(VirtualMachineProfile.Param.UefiFlag.getName())) {
vmProfile.getParameters().put(VirtualMachineProfile.Param.UefiFlag, details.get(VirtualMachineProfile.Param.UefiFlag.getName()));
}

return toVmTO(vmProfile);
}

protected VirtualMachineTO getVmTO(Long vmId) {
final VMInstanceVO vm = _vmDao.findById(vmId);
final VirtualMachineProfile profile = new VirtualMachineProfileImpl(vm);
Expand Down Expand Up @@ -6170,8 +6274,9 @@ public Pair<Long, Long> findClusterAndHostIdForVm(VirtualMachine vm, boolean ski
host = host == null ? _hostDao.findById(hostId) : host;
if (host != null) {
clusterId = host.getClusterId();
return new Pair<>(clusterId, hostId);
}
return new Pair<>(clusterId, hostId);
return findClusterAndHostIdForVmFromVolumes(vm.getId());
}

private Pair<Long, Long> findClusterAndHostIdForVm(VirtualMachine vm) {
Expand Down
Loading
Loading