use of com.cloud.agent.api.ReplugNicAnswer in project cloudstack by apache.
the class VirtualMachineManagerImpl method replugNic.
@Override
public boolean replugNic(final Network network, final NicTO nic, final VirtualMachineTO vm, final Host host) throws ConcurrentOperationException, ResourceUnavailableException, InsufficientCapacityException {
boolean result = true;
final VMInstanceVO router = _vmDao.findById(vm.getId());
if (router.getState() == State.Running) {
try {
final ReplugNicCommand replugNicCmd = new ReplugNicCommand(nic, vm.getName(), vm.getType(), vm.getDetails());
final Commands cmds = new Commands(Command.OnError.Stop);
cmds.addCommand("replugnic", replugNicCmd);
_agentMgr.send(host.getId(), cmds);
final ReplugNicAnswer replugNicAnswer = cmds.getAnswer(ReplugNicAnswer.class);
if (replugNicAnswer == null || !replugNicAnswer.getResult()) {
s_logger.warn("Unable to replug nic for vm " + vm.getName());
result = false;
}
} catch (final OperationTimedoutException e) {
throw new AgentUnavailableException("Unable to plug nic for router " + vm.getName() + " in network " + network, host.getId(), e);
}
} else {
String message = String.format("Unable to apply ReplugNic, VM [%s] is not in the right state (\"Running\"). VM state [%s].", router.toString(), router.getState());
s_logger.warn(message);
throw new ResourceUnavailableException(message, DataCenter.class, router.getDataCenterId());
}
return result;
}
use of com.cloud.agent.api.ReplugNicAnswer in project cloudstack by apache.
the class VmwareResource method execute.
private ReplugNicAnswer execute(ReplugNicCommand cmd) {
getServiceContext().getStockObject(VmwareManager.CONTEXT_STOCK_NAME);
VmwareContext context = getServiceContext();
try {
VmwareHypervisorHost hyperHost = getHyperHost(context);
String vmName = cmd.getVmName();
VirtualMachineMO vmMo = hyperHost.findVmOnHyperHost(vmName);
if (vmMo == null) {
if (hyperHost instanceof HostMO) {
ClusterMO clusterMo = new ClusterMO(hyperHost.getContext(), ((HostMO) hyperHost).getParentMor());
vmMo = clusterMo.findVmOnHyperHost(vmName);
}
}
if (vmMo == null) {
String msg = "Router " + vmName + " no longer exists to execute ReplugNic command";
s_logger.error(msg);
throw new Exception(msg);
}
/*
if(!isVMWareToolsInstalled(vmMo)){
String errMsg = "vmware tools is not installed or not running, cannot add nic to vm " + vmName;
s_logger.debug(errMsg);
return new PlugNicAnswer(cmd, false, "Unable to execute PlugNicCommand due to " + errMsg);
}
*/
// Fallback to E1000 if no specific nicAdapter is passed
VirtualEthernetCardType nicDeviceType = VirtualEthernetCardType.E1000;
Map<String, String> details = cmd.getDetails();
if (details != null) {
nicDeviceType = VirtualEthernetCardType.valueOf((String) details.get("nicAdapter"));
}
NicTO nicTo = cmd.getNic();
VirtualDevice nic = findVirtualNicDevice(vmMo, nicTo.getMac());
if (nic == null) {
return new ReplugNicAnswer(cmd, false, "Nic to replug not found");
}
Pair<ManagedObjectReference, String> networkInfo = prepareNetworkFromNicInfo(vmMo.getRunningHost(), nicTo, false, cmd.getVMType());
String dvSwitchUuid = null;
if (VmwareHelper.isDvPortGroup(networkInfo.first())) {
ManagedObjectReference dcMor = hyperHost.getHyperHostDatacenter();
DatacenterMO dataCenterMo = new DatacenterMO(context, dcMor);
ManagedObjectReference dvsMor = dataCenterMo.getDvSwitchMor(networkInfo.first());
dvSwitchUuid = dataCenterMo.getDvSwitchUuid(dvsMor);
s_logger.info("Preparing NIC device on dvSwitch : " + dvSwitchUuid);
VmwareHelper.updateDvNicDevice(nic, networkInfo.first(), dvSwitchUuid);
} else {
s_logger.info("Preparing NIC device on network " + networkInfo.second());
VmwareHelper.updateNicDevice(nic, networkInfo.first(), networkInfo.second());
}
configureNicDevice(vmMo, nic, VirtualDeviceConfigSpecOperation.EDIT, "ReplugNicCommand");
return new ReplugNicAnswer(cmd, true, "success");
} catch (Exception e) {
s_logger.error("Unexpected exception: ", e);
return new ReplugNicAnswer(cmd, false, "Unable to execute ReplugNicCommand due to " + e.toString());
}
}
use of com.cloud.agent.api.ReplugNicAnswer in project cloudstack by apache.
the class LibvirtReplugNicCommandWrapper method execute.
@Override
public Answer execute(final ReplugNicCommand command, final LibvirtComputingResource libvirtComputingResource) {
final NicTO nic = command.getNic();
final String vmName = command.getVmName();
Domain vm = null;
try {
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
final Connect conn = libvirtUtilitiesHelper.getConnectionByVmName(vmName);
vm = libvirtComputingResource.getDomain(conn, vmName);
InterfaceDef oldPluggedNic = findPluggedNic(libvirtComputingResource, nic, vmName, conn);
final VifDriver newVifDriver = libvirtComputingResource.getVifDriver(nic.getType(), nic.getName());
final InterfaceDef interfaceDef = newVifDriver.plug(nic, "Other PV", oldPluggedNic.getModel().toString(), null);
interfaceDef.setSlot(oldPluggedNic.getSlot());
interfaceDef.setDevName(oldPluggedNic.getDevName());
interfaceDef.setLinkStateUp(false);
oldPluggedNic.setSlot(null);
int i = 0;
do {
i++;
s_logger.debug("ReplugNic: Detaching interface" + oldPluggedNic + " (Attempt: " + i + ")");
vm.detachDevice(oldPluggedNic.toString());
} while (findPluggedNic(libvirtComputingResource, nic, vmName, conn) != null && i <= 10);
s_logger.debug("ReplugNic: Attaching interface" + interfaceDef);
vm.attachDevice(interfaceDef.toString());
interfaceDef.setLinkStateUp(true);
s_logger.debug("ReplugNic: Updating interface" + interfaceDef);
vm.updateDeviceFlags(interfaceDef.toString(), DomainAffect.LIVE.getValue());
// each interface at this point, so inform all vif drivers
for (final VifDriver vifDriver : libvirtComputingResource.getAllVifDrivers()) {
vifDriver.unplug(oldPluggedNic, true);
}
return new ReplugNicAnswer(command, true, "success");
} catch (final LibvirtException | InternalErrorException e) {
final String msg = " Plug Nic failed due to " + e.toString();
s_logger.warn(msg, e);
return new ReplugNicAnswer(command, false, msg);
} finally {
if (vm != null) {
try {
vm.free();
} catch (final LibvirtException l) {
s_logger.trace("Ignoring libvirt error.", l);
}
}
}
}
Aggregations