Search in sources :

Example 1 with ReplugNicAnswer

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;
}
Also used : OperationTimedoutException(com.cloud.exception.OperationTimedoutException) ReplugNicCommand(com.cloud.agent.api.ReplugNicCommand) AgentUnavailableException(com.cloud.exception.AgentUnavailableException) Commands(com.cloud.agent.manager.Commands) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) ReplugNicAnswer(com.cloud.agent.api.ReplugNicAnswer)

Example 2 with ReplugNicAnswer

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());
    }
}
Also used : HostMO(com.cloud.hypervisor.vmware.mo.HostMO) VirtualMachineMO(com.cloud.hypervisor.vmware.mo.VirtualMachineMO) VirtualDevice(com.vmware.vim25.VirtualDevice) ReplugNicAnswer(com.cloud.agent.api.ReplugNicAnswer) VirtualEthernetCardType(com.cloud.hypervisor.vmware.mo.VirtualEthernetCardType) VmwareHypervisorHost(com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost) ClusterMO(com.cloud.hypervisor.vmware.mo.ClusterMO) ConnectException(java.net.ConnectException) IOException(java.io.IOException) RemoteException(java.rmi.RemoteException) InternalErrorException(com.cloud.exception.InternalErrorException) CloudException(com.cloud.exception.CloudException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ConfigurationException(javax.naming.ConfigurationException) VmwareContext(com.cloud.hypervisor.vmware.util.VmwareContext) NicTO(com.cloud.agent.api.to.NicTO) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) DatacenterMO(com.cloud.hypervisor.vmware.mo.DatacenterMO)

Example 3 with ReplugNicAnswer

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);
            }
        }
    }
}
Also used : InterfaceDef(com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef) LibvirtException(org.libvirt.LibvirtException) Connect(org.libvirt.Connect) ReplugNicAnswer(com.cloud.agent.api.ReplugNicAnswer) InternalErrorException(com.cloud.exception.InternalErrorException) Domain(org.libvirt.Domain) VifDriver(com.cloud.hypervisor.kvm.resource.VifDriver) NicTO(com.cloud.agent.api.to.NicTO)

Aggregations

ReplugNicAnswer (com.cloud.agent.api.ReplugNicAnswer)3 NicTO (com.cloud.agent.api.to.NicTO)2 InternalErrorException (com.cloud.exception.InternalErrorException)2 ReplugNicCommand (com.cloud.agent.api.ReplugNicCommand)1 Commands (com.cloud.agent.manager.Commands)1 AgentUnavailableException (com.cloud.exception.AgentUnavailableException)1 CloudException (com.cloud.exception.CloudException)1 OperationTimedoutException (com.cloud.exception.OperationTimedoutException)1 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)1 InterfaceDef (com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef)1 VifDriver (com.cloud.hypervisor.kvm.resource.VifDriver)1 ClusterMO (com.cloud.hypervisor.vmware.mo.ClusterMO)1 DatacenterMO (com.cloud.hypervisor.vmware.mo.DatacenterMO)1 HostMO (com.cloud.hypervisor.vmware.mo.HostMO)1 VirtualEthernetCardType (com.cloud.hypervisor.vmware.mo.VirtualEthernetCardType)1 VirtualMachineMO (com.cloud.hypervisor.vmware.mo.VirtualMachineMO)1 VmwareHypervisorHost (com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost)1 VmwareContext (com.cloud.hypervisor.vmware.util.VmwareContext)1 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)1 ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)1