Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
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,56 @@
//
// 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() {
//VR start doesn't go through queue
if (instanceName != null && instanceName.startsWith("r-")) {
return false;
}
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 @@ -49,7 +49,6 @@
import javax.naming.ConfigurationException;
import javax.persistence.EntityExistsException;


import org.apache.cloudstack.affinity.dao.AffinityGroupVMMapDao;
import org.apache.cloudstack.annotation.AnnotationService;
import org.apache.cloudstack.annotation.dao.AnnotationDao;
Expand All @@ -71,6 +70,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 +150,14 @@
import com.cloud.agent.api.StopCommand;
import com.cloud.agent.api.UnPlugNicAnswer;
import com.cloud.agent.api.UnPlugNicCommand;
import com.cloud.agent.api.UnmanageInstanceAnswer;
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 @@ -297,8 +300,8 @@
import com.cloud.vm.VirtualMachine.State;
import com.cloud.vm.dao.NicDao;
import com.cloud.vm.dao.UserVmDao;
import com.cloud.vm.dao.VMInstanceDetailsDao;
import com.cloud.vm.dao.VMInstanceDao;
import com.cloud.vm.dao.VMInstanceDetailsDao;
import com.cloud.vm.snapshot.VMSnapshotManager;
import com.cloud.vm.snapshot.VMSnapshotVO;
import com.cloud.vm.snapshot.dao.VMSnapshotDao;
Expand Down Expand Up @@ -1170,6 +1173,8 @@ protected void checkAndAttemptMigrateVmAcrossCluster(final VMInstanceVO vm, fina
markVolumesInPool(vm, answer);
}



protected void updateVmMetadataManufacturerAndProduct(VirtualMachineTO vmTO, VMInstanceVO vm) {
String metadataManufacturer = VmMetadataManufacturer.valueIn(vm.getDataCenterId());
if (StringUtils.isBlank(metadataManufacturer)) {
Expand Down Expand Up @@ -2014,31 +2019,52 @@ public boolean unmanage(String vmUuid) {
throw new ConcurrentOperationException(msg);
}

Boolean result = Transaction.execute(new TransactionCallback<Boolean>() {
@Override
public Boolean doInTransaction(TransactionStatus status) {
boolean isDomainXMLPreserved = true;
// persist domain for kvm host
if (HypervisorType.KVM.equals(vm.getHypervisorType())) {
long hostId = vm.getHostId();
UnmanageInstanceCommand unmanageInstanceCommand;
if (State.Stopped.equals(vm.getState())) {
hostId = vm.getLastHostId();
unmanageInstanceCommand = new UnmanageInstanceCommand(prepareVmTO(vm.getId(), hostId)); // reconstruct vmSpec
} else {
unmanageInstanceCommand = new UnmanageInstanceCommand(vm.getName());
}
try {
Answer answer = _agentMgr.send(hostId, unmanageInstanceCommand);
isDomainXMLPreserved = (answer instanceof UnmanageInstanceAnswer && answer.getResult());
} catch (Exception ex) {
isDomainXMLPreserved = false;
}
}

logger.debug("Unmanaging VM {}", vm);
if (isDomainXMLPreserved) {
logger.debug("Unmanaging VM {}", vm);
Boolean result = Transaction.execute(new TransactionCallback<Boolean>() {
@Override
public Boolean doInTransaction(TransactionStatus status) {
final VirtualMachineProfile profile = new VirtualMachineProfileImpl(vm);
final VirtualMachineGuru guru = getVmGuru(vm);

final VirtualMachineProfile profile = new VirtualMachineProfileImpl(vm);
final VirtualMachineGuru guru = getVmGuru(vm);
try {
unmanageVMSnapshots(vm);
unmanageVMNics(profile, vm);
unmanageVMVolumes(vm);

try {
unmanageVMSnapshots(vm);
unmanageVMNics(profile, vm);
unmanageVMVolumes(vm);
guru.finalizeUnmanage(vm);
} catch (Exception e) {
logger.error("Error while unmanaging VM {}", vm, e);
return false;
}

guru.finalizeUnmanage(vm);
} catch (Exception e) {
logger.error("Error while unmanaging VM {}", vm, e);
return false;
return true;
}

return true;
}
});

return BooleanUtils.isTrue(result);
});
return BooleanUtils.isTrue(result);
} else {
logger.error("Error encountered persisting domainXML for vm: {}", vm.getName());
}
return false;
}

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

protected VirtualMachineTO prepareVmTO(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 volTO = volumeInfo.getTO();
DiskTO disk = storageMgr.getDiskWithThrottling(volTO, vol.getVolumeType(), vol.getDeviceId(), vol.getPath(), vm.getServiceOfferingId(), vol.getDiskOfferingId());
vmProfile.addDisk(disk);
}
return toVmTO(vmProfile);
}

protected VirtualMachineTO getVmTO(Long vmId) {
final VMInstanceVO vm = _vmDao.findById(vmId);
final VirtualMachineProfile profile = new VirtualMachineProfileImpl(vm);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.ThreadContext;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.ThreadContext;
import org.apache.xerces.impl.xpath.regex.Match;
import org.joda.time.Duration;
import org.libvirt.Connect;
Expand Down Expand Up @@ -170,7 +170,6 @@
import com.cloud.host.Host.Type;
import com.cloud.hypervisor.Hypervisor.HypervisorType;
import com.cloud.hypervisor.kvm.dpdk.DpdkHelper;
import com.cloud.hypervisor.kvm.resource.disconnecthook.DisconnectHook;
import com.cloud.hypervisor.kvm.resource.LibvirtVMDef.ChannelDef;
import com.cloud.hypervisor.kvm.resource.LibvirtVMDef.ClockDef;
import com.cloud.hypervisor.kvm.resource.LibvirtVMDef.ConsoleDef;
Expand Down Expand Up @@ -199,6 +198,7 @@
import com.cloud.hypervisor.kvm.resource.LibvirtVMDef.WatchDogDef;
import com.cloud.hypervisor.kvm.resource.LibvirtVMDef.WatchDogDef.WatchDogAction;
import com.cloud.hypervisor.kvm.resource.LibvirtVMDef.WatchDogDef.WatchDogModel;
import com.cloud.hypervisor.kvm.resource.disconnecthook.DisconnectHook;
import com.cloud.hypervisor.kvm.resource.rolling.maintenance.RollingMaintenanceAgentExecutor;
import com.cloud.hypervisor.kvm.resource.rolling.maintenance.RollingMaintenanceExecutor;
import com.cloud.hypervisor.kvm.resource.rolling.maintenance.RollingMaintenanceServiceExecutor;
Expand Down Expand Up @@ -240,7 +240,6 @@
import com.cloud.vm.VirtualMachine;
import com.cloud.vm.VirtualMachine.PowerState;
import com.cloud.vm.VmDetailConstants;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
//
// 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.kvm.resource.wrapper;

import java.net.URISyntaxException;

import org.libvirt.Connect;
import org.libvirt.Domain;
import org.libvirt.LibvirtException;

import com.cloud.agent.api.Answer;
import com.cloud.agent.api.UnmanageInstanceAnswer;
import com.cloud.agent.api.UnmanageInstanceCommand;
import com.cloud.agent.api.to.VirtualMachineTO;
import com.cloud.exception.InternalErrorException;
import com.cloud.hypervisor.kvm.resource.LibvirtComputingResource;
import com.cloud.hypervisor.kvm.resource.LibvirtKvmAgentHook;
import com.cloud.hypervisor.kvm.resource.LibvirtVMDef;
import com.cloud.resource.CommandWrapper;
import com.cloud.resource.ResourceWrapper;

@ResourceWrapper(handles = UnmanageInstanceCommand.class)
public final class LibvirtUnmanageInstanceCommandWrapper extends CommandWrapper<UnmanageInstanceCommand, Answer, LibvirtComputingResource> {


@Override
public Answer execute(final UnmanageInstanceCommand command, final LibvirtComputingResource libvirtComputingResource) {
String instanceName = command.getInstanceName();
VirtualMachineTO vmSpec = command.getVm();
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
logger.debug("Attempting to unmanage KVM instance: {}", instanceName);
Domain dom = null;
Connect conn = null;
try {
if (vmSpec == null) {
conn = libvirtUtilitiesHelper.getConnectionByVmName(instanceName);
dom = conn.domainLookupByName(instanceName);
String domainXML = dom.getXMLDesc(1);
conn.domainDefineXML(domainXML).free();
} else {
// define domain using reconstructed vmSpec
logger.debug("Unmanaging Stopped KVM instance: {}", instanceName);
LibvirtVMDef vm = libvirtComputingResource.createVMFromSpec(vmSpec);
libvirtComputingResource.createVbd(conn, vmSpec, instanceName, vm);
conn = libvirtUtilitiesHelper.getConnectionByType(vm.getHvsType());
String vmInitialSpecification = vm.toString();
String vmFinalSpecification = performXmlTransformHook(vmInitialSpecification, libvirtComputingResource);
conn.domainDefineXML(vmFinalSpecification).free();
}
logger.debug("Successfully unmanaged KVM instance: {}", instanceName);
return new UnmanageInstanceAnswer(command, true, "Successfully unmanaged");
} catch (final LibvirtException e) {
logger.warn("LibvirtException occurred during unmanaging instance: {} ", instanceName, e);
return new UnmanageInstanceAnswer(command, false, e.getMessage());
} catch (final URISyntaxException | InternalErrorException e) {
logger.warn("URISyntaxException ", e);
return new UnmanageInstanceAnswer(command, false, e.getMessage());
} finally {
if (dom != null) {
try {
dom.free();
} catch (LibvirtException e) {
logger.error("Ignore libvirt error on free.", e);
}
}
}
}

private String performXmlTransformHook(String vmInitialSpecification, final LibvirtComputingResource libvirtComputingResource) {
String vmFinalSpecification;
try {
// if transformer fails, everything must go as it's just skipped.
LibvirtKvmAgentHook t = libvirtComputingResource.getTransformer();
vmFinalSpecification = (String) t.handle(vmInitialSpecification);
if (null == vmFinalSpecification) {
logger.warn("Libvirt XML transformer returned NULL, will use XML specification unchanged.");
vmFinalSpecification = vmInitialSpecification;
}
} catch(Exception e) {
logger.warn("Exception occurred when handling LibVirt XML transformer hook: {}", e);
vmFinalSpecification = vmInitialSpecification;
}
return vmFinalSpecification;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2287,10 +2287,11 @@ public boolean unmanageVMInstance(long vmId) {

performUnmanageVMInstancePrechecks(vmVO);

Long hostId = findSuitableHostId(vmVO);
boolean isVMStopped = VirtualMachine.State.Stopped.equals(vmVO.getState());
Long hostId = isVMStopped ? vmVO.getLastHostId() : findSuitableHostId(vmVO);
String instanceName = vmVO.getInstanceName();

if (!existsVMToUnmanage(instanceName, hostId)) {
if (!isVMStopped && !existsVMToUnmanage(instanceName, hostId)) {
throw new CloudRuntimeException(String.format("VM %s is not found in the hypervisor", vmVO));
}

Expand Down
Loading