Search in sources :

Example 1 with UnPlugNicAnswer

use of com.cloud.legacymodel.communication.answer.UnPlugNicAnswer in project cosmic by MissionCriticalCloud.

the class VirtualMachineManagerImpl method unplugNic.

public boolean unplugNic(final Network network, final NicTO nic, final VirtualMachineTO vm, final ReservationContext context, final DeployDestination dest) throws ConcurrentOperationException, ResourceUnavailableException {
    boolean result = true;
    final VMInstanceVO router = _vmDao.findById(vm.getId());
    if (router.getState() == State.Running) {
        try {
            final Commands cmds = new Commands(Command.OnError.Stop);
            final UnPlugNicCommand unplugNicCmd = new UnPlugNicCommand(nic, vm.getName());
            cmds.addCommand("unplugnic", unplugNicCmd);
            _agentMgr.send(dest.getHost().getId(), cmds);
            final UnPlugNicAnswer unplugNicAnswer = cmds.getAnswer(UnPlugNicAnswer.class);
            if (!(unplugNicAnswer != null && unplugNicAnswer.getResult())) {
                s_logger.warn("Unable to unplug nic from router " + router);
                result = false;
            }
        } catch (final OperationTimedoutException e) {
            throw new AgentUnavailableException("Unable to unplug nic from rotuer " + router + " from network " + network, dest.getHost().getId(), e);
        }
    } else if (router.getState() == State.Stopped || router.getState() == State.Stopping) {
        s_logger.debug("Vm " + router.getInstanceName() + " is in " + router.getState() + ", so not sending unplug nic command to the backend");
    } else {
        s_logger.warn("Unable to apply unplug nic, Vm " + router + " is not in the right state " + router.getState());
        throw new ResourceUnavailableException("Unable to apply unplug nic on the backend," + " vm " + router + " is not in the right state", DataCenter.class, router.getDataCenterId());
    }
    return result;
}
Also used : OperationTimedoutException(com.cloud.legacymodel.exceptions.OperationTimedoutException) DataCenter(com.cloud.legacymodel.dc.DataCenter) UnPlugNicAnswer(com.cloud.legacymodel.communication.answer.UnPlugNicAnswer) AgentUnavailableException(com.cloud.legacymodel.exceptions.AgentUnavailableException) Commands(com.cloud.agent.manager.Commands) ResourceUnavailableException(com.cloud.legacymodel.exceptions.ResourceUnavailableException) UnPlugNicCommand(com.cloud.legacymodel.communication.command.UnPlugNicCommand)

Example 2 with UnPlugNicAnswer

use of com.cloud.legacymodel.communication.answer.UnPlugNicAnswer in project cosmic by MissionCriticalCloud.

the class CitrixUnPlugNicCommandWrapper method execute.

@Override
public Answer execute(final UnPlugNicCommand command, final CitrixResourceBase citrixResourceBase) {
    final Connection conn = citrixResourceBase.getConnection();
    final String vmName = command.getVmName();
    try {
        final Set<VM> vms = VM.getByNameLabel(conn, vmName);
        if (vms == null || vms.isEmpty()) {
            return new UnPlugNicAnswer(command, false, "Can not find VM " + vmName);
        }
        final VM vm = vms.iterator().next();
        final NicTO nic = command.getNic();
        final String mac = nic.getMac();
        final VIF vif = citrixResourceBase.getVifByMac(conn, vm, mac);
        if (vif != null) {
            vif.unplug(conn);
            final Network network = vif.getNetwork(conn);
            vif.destroy(conn);
            try {
                if (network.getNameLabel(conn).startsWith("VLAN")) {
                    citrixResourceBase.disableVlanNetwork(conn, network);
                }
            } catch (final Exception e) {
            }
        }
        return new UnPlugNicAnswer(command, true, "success");
    } catch (final Exception e) {
        final String msg = " UnPlug Nic failed due to " + e.toString();
        s_logger.warn(msg, e);
        return new UnPlugNicAnswer(command, false, msg);
    }
}
Also used : VIF(com.xensource.xenapi.VIF) UnPlugNicAnswer(com.cloud.legacymodel.communication.answer.UnPlugNicAnswer) VM(com.xensource.xenapi.VM) Network(com.xensource.xenapi.Network) Connection(com.xensource.xenapi.Connection) NicTO(com.cloud.legacymodel.to.NicTO)

Example 3 with UnPlugNicAnswer

use of com.cloud.legacymodel.communication.answer.UnPlugNicAnswer in project cosmic by MissionCriticalCloud.

the class LibvirtUnPlugNicCommandWrapper method execute.

@Override
public Answer execute(final UnPlugNicCommand 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);
        final List<LibvirtVmDef.InterfaceDef> pluggedNics = libvirtComputingResource.getInterfaces(conn, vmName);
        for (final LibvirtVmDef.InterfaceDef pluggedNic : pluggedNics) {
            if (pluggedNic.getMacAddress().equalsIgnoreCase(nic.getMac())) {
                vm.detachDevice(pluggedNic.toString());
                // each interface at this point, so inform all vif drivers
                for (final VifDriver vifDriver : libvirtComputingResource.getAllVifDrivers()) {
                    vifDriver.unplug(pluggedNic);
                }
                return new UnPlugNicAnswer(command, true, "success");
            }
        }
        return new UnPlugNicAnswer(command, true, "success");
    } catch (final LibvirtException e) {
        final String msg = " Unplug Nic failed due to " + e.toString();
        s_logger.warn(msg, e);
        return new UnPlugNicAnswer(command, false, msg);
    } finally {
        if (vm != null) {
            try {
                vm.free();
            } catch (final LibvirtException l) {
                s_logger.trace("Ignoring libvirt error.", l);
            }
        }
    }
}
Also used : LibvirtVmDef(com.cloud.agent.resource.kvm.xml.LibvirtVmDef) LibvirtException(org.libvirt.LibvirtException) UnPlugNicAnswer(com.cloud.legacymodel.communication.answer.UnPlugNicAnswer) Connect(org.libvirt.Connect) Domain(org.libvirt.Domain) VifDriver(com.cloud.agent.resource.kvm.vif.VifDriver) NicTO(com.cloud.legacymodel.to.NicTO)

Aggregations

UnPlugNicAnswer (com.cloud.legacymodel.communication.answer.UnPlugNicAnswer)3 NicTO (com.cloud.legacymodel.to.NicTO)2 Commands (com.cloud.agent.manager.Commands)1 VifDriver (com.cloud.agent.resource.kvm.vif.VifDriver)1 LibvirtVmDef (com.cloud.agent.resource.kvm.xml.LibvirtVmDef)1 UnPlugNicCommand (com.cloud.legacymodel.communication.command.UnPlugNicCommand)1 DataCenter (com.cloud.legacymodel.dc.DataCenter)1 AgentUnavailableException (com.cloud.legacymodel.exceptions.AgentUnavailableException)1 OperationTimedoutException (com.cloud.legacymodel.exceptions.OperationTimedoutException)1 ResourceUnavailableException (com.cloud.legacymodel.exceptions.ResourceUnavailableException)1 Connection (com.xensource.xenapi.Connection)1 Network (com.xensource.xenapi.Network)1 VIF (com.xensource.xenapi.VIF)1 VM (com.xensource.xenapi.VM)1 Connect (org.libvirt.Connect)1 Domain (org.libvirt.Domain)1 LibvirtException (org.libvirt.LibvirtException)1