Search in sources :

Example 1 with PlugNicAnswer

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

the class VirtualMachineManagerImpl method plugNic.

public boolean plugNic(final Network network, final NicTO nic, final VirtualMachineTO vm, final ReservationContext context, final DeployDestination dest) throws ConcurrentOperationException, ResourceUnavailableException, InsufficientCapacityException {
    boolean result = true;
    final VMInstanceVO router = _vmDao.findById(vm.getId());
    if (router.getState() == State.Running) {
        try {
            final PlugNicCommand plugNicCmd = new PlugNicCommand(nic, vm.getName(), vm.getType(), vm.getDetails());
            final Commands cmds = new Commands(Command.OnError.Stop);
            cmds.addCommand("plugnic", plugNicCmd);
            _agentMgr.send(dest.getHost().getId(), cmds);
            final PlugNicAnswer plugNicAnswer = cmds.getAnswer(PlugNicAnswer.class);
            if (!(plugNicAnswer != null && plugNicAnswer.getResult())) {
                s_logger.warn("Unable to plug 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, dest.getHost().getId(), e);
        }
    } else {
        s_logger.warn("Unable to apply PlugNic, vm " + router + " is not in the right state " + router.getState());
        throw new ResourceUnavailableException("Unable to apply PlugNic on the backend," + " vm " + vm + " is not in the right state", DataCenter.class, router.getDataCenterId());
    }
    return result;
}
Also used : OperationTimedoutException(com.cloud.legacymodel.exceptions.OperationTimedoutException) AgentUnavailableException(com.cloud.legacymodel.exceptions.AgentUnavailableException) Commands(com.cloud.agent.manager.Commands) ResourceUnavailableException(com.cloud.legacymodel.exceptions.ResourceUnavailableException) UnPlugNicAnswer(com.cloud.legacymodel.communication.answer.UnPlugNicAnswer) PlugNicAnswer(com.cloud.legacymodel.communication.answer.PlugNicAnswer) PlugNicCommand(com.cloud.legacymodel.communication.command.PlugNicCommand) UnPlugNicCommand(com.cloud.legacymodel.communication.command.UnPlugNicCommand)

Example 2 with PlugNicAnswer

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

the class LibvirtPlugNicCommandWrapper method execute.

@Override
public Answer execute(final PlugNicCommand 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);
        Integer nicnum = 0;
        for (final LibvirtVmDef.InterfaceDef pluggedNic : pluggedNics) {
            if (pluggedNic.getMacAddress().equalsIgnoreCase(nic.getMac())) {
                s_logger.debug("found existing nic for mac " + pluggedNic.getMacAddress() + " at index " + nicnum);
                return new PlugNicAnswer(command, true, "success");
            }
            nicnum++;
        }
        final VifDriver vifDriver = libvirtComputingResource.getVifDriver(nic.getType());
        final LibvirtVmDef.InterfaceDef interfaceDef = vifDriver.plug(nic, "Default - VirtIO capable OS (64-bit)", "");
        vm.attachDevice(interfaceDef.toString());
        return new PlugNicAnswer(command, true, "success");
    } catch (final LibvirtException e) {
        final String msg = " Plug Nic failed due to " + e.toString();
        s_logger.warn(msg, e);
        return new PlugNicAnswer(command, false, msg);
    } catch (final InternalErrorException e) {
        final String msg = " Plug Nic failed due to " + e.toString();
        s_logger.warn(msg, e);
        return new PlugNicAnswer(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) Connect(org.libvirt.Connect) InternalErrorException(com.cloud.legacymodel.exceptions.InternalErrorException) VifDriver(com.cloud.agent.resource.kvm.vif.VifDriver) PlugNicAnswer(com.cloud.legacymodel.communication.answer.PlugNicAnswer) Domain(org.libvirt.Domain) NicTO(com.cloud.legacymodel.to.NicTO)

Example 3 with PlugNicAnswer

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

the class CitrixPlugNicCommandWrapper method execute.

@Override
public Answer execute(final PlugNicCommand 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 PlugNicAnswer(command, false, "Can not find VM " + vmName);
        }
        final VM vm = vms.iterator().next();
        final NicTO nic = command.getNic();
        String mac = nic.getMac();
        final Set<VIF> routerVIFs = vm.getVIFs(conn);
        mac = mac.trim();
        int counter = 0;
        for (final VIF vif : routerVIFs) {
            final String lmac = vif.getMAC(conn);
            if (lmac.trim().equals(mac)) {
                counter++;
            }
        }
        // redundant.
        if (counter > 2) {
            final String msg = " Plug Nic failed due to a VIF with the same mac " + nic.getMac() + " exists in more than 2 routers.";
            s_logger.error(msg);
            return new PlugNicAnswer(command, false, msg);
        }
        // Wilder Rodrigues - replaced this code with the code above.
        // VIF vif = getVifByMac(conn, vm, nic.getMac());
        // if (vif != null) {
        // final String msg = " Plug Nic failed due to a VIF with the same mac " + nic.getMac() + " exists";
        // s_logger.warn(msg);
        // return new PlugNicAnswer(cmd, false, msg);
        // }
        final VIF vif = citrixResourceBase.createVif(conn, vmName, vm, null, nic);
        // vif = createVif(conn, vmName, vm, null, nic);
        vif.plug(conn);
        return new PlugNicAnswer(command, true, "success");
    } catch (final Exception e) {
        final String msg = " Plug Nic failed due to " + e.toString();
        s_logger.error(msg, e);
        return new PlugNicAnswer(command, false, msg);
    }
}
Also used : VIF(com.xensource.xenapi.VIF) VM(com.xensource.xenapi.VM) Connection(com.xensource.xenapi.Connection) PlugNicAnswer(com.cloud.legacymodel.communication.answer.PlugNicAnswer) NicTO(com.cloud.legacymodel.to.NicTO)

Aggregations

PlugNicAnswer (com.cloud.legacymodel.communication.answer.PlugNicAnswer)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 UnPlugNicAnswer (com.cloud.legacymodel.communication.answer.UnPlugNicAnswer)1 PlugNicCommand (com.cloud.legacymodel.communication.command.PlugNicCommand)1 UnPlugNicCommand (com.cloud.legacymodel.communication.command.UnPlugNicCommand)1 AgentUnavailableException (com.cloud.legacymodel.exceptions.AgentUnavailableException)1 InternalErrorException (com.cloud.legacymodel.exceptions.InternalErrorException)1 OperationTimedoutException (com.cloud.legacymodel.exceptions.OperationTimedoutException)1 ResourceUnavailableException (com.cloud.legacymodel.exceptions.ResourceUnavailableException)1 Connection (com.xensource.xenapi.Connection)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