Search in sources :

Example 1 with PlugNicAnswer

use of com.cloud.agent.api.PlugNicAnswer in project cloudstack by apache.

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.exception.OperationTimedoutException) AgentUnavailableException(com.cloud.exception.AgentUnavailableException) Commands(com.cloud.agent.manager.Commands) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) PlugNicAnswer(com.cloud.agent.api.PlugNicAnswer) UnPlugNicAnswer(com.cloud.agent.api.UnPlugNicAnswer) PlugNicCommand(com.cloud.agent.api.PlugNicCommand) UnPlugNicCommand(com.cloud.agent.api.UnPlugNicCommand)

Example 2 with PlugNicAnswer

use of com.cloud.agent.api.PlugNicAnswer in project cloudstack by apache.

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<InterfaceDef> pluggedNics = libvirtComputingResource.getInterfaces(conn, vmName);
        Integer nicnum = 0;
        for (final 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 InterfaceDef interfaceDef = vifDriver.plug(nic, "Other PV", "");
        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 : InterfaceDef(com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef) LibvirtException(org.libvirt.LibvirtException) Connect(org.libvirt.Connect) PlugNicAnswer(com.cloud.agent.api.PlugNicAnswer) InternalErrorException(com.cloud.exception.InternalErrorException) Domain(org.libvirt.Domain) VifDriver(com.cloud.hypervisor.kvm.resource.VifDriver) NicTO(com.cloud.agent.api.to.NicTO)

Example 3 with PlugNicAnswer

use of com.cloud.agent.api.PlugNicAnswer in project cloudstack by apache.

the class VmwareResource method execute.

private PlugNicAnswer execute(PlugNicCommand cmd) {
    if (s_logger.isInfoEnabled()) {
        s_logger.info("Executing resource PlugNicCommand " + _gson.toJson(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 PlugNic 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"));
        }
        // find a usable device number in VMware environment
        VirtualDevice[] nicDevices = vmMo.getNicDevices();
        int deviceNumber = -1;
        for (VirtualDevice device : nicDevices) {
            if (device.getUnitNumber() > deviceNumber)
                deviceNumber = device.getUnitNumber();
        }
        deviceNumber++;
        NicTO nicTo = cmd.getNic();
        VirtualDevice nic;
        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);
            nic = VmwareHelper.prepareDvNicDevice(vmMo, networkInfo.first(), nicDeviceType, networkInfo.second(), dvSwitchUuid, nicTo.getMac(), deviceNumber, deviceNumber + 1, true, true);
        } else {
            s_logger.info("Preparing NIC device on network " + networkInfo.second());
            nic = VmwareHelper.prepareNicDevice(vmMo, networkInfo.first(), nicDeviceType, networkInfo.second(), nicTo.getMac(), deviceNumber, deviceNumber + 1, true, true);
        }
        VirtualMachineConfigSpec vmConfigSpec = new VirtualMachineConfigSpec();
        //VirtualDeviceConfigSpec[] deviceConfigSpecArray = new VirtualDeviceConfigSpec[1];
        VirtualDeviceConfigSpec deviceConfigSpec = new VirtualDeviceConfigSpec();
        deviceConfigSpec.setDevice(nic);
        deviceConfigSpec.setOperation(VirtualDeviceConfigSpecOperation.ADD);
        vmConfigSpec.getDeviceChange().add(deviceConfigSpec);
        setNuageVspVrIpInExtraConfig(vmConfigSpec.getExtraConfig(), nicTo, dvSwitchUuid);
        if (!vmMo.configureVm(vmConfigSpec)) {
            throw new Exception("Failed to configure devices when running PlugNicCommand");
        }
        return new PlugNicAnswer(cmd, true, "success");
    } catch (Exception e) {
        s_logger.error("Unexpected exception: ", e);
        return new PlugNicAnswer(cmd, false, "Unable to execute PlugNicCommand due to " + e.toString());
    }
}
Also used : HostMO(com.cloud.hypervisor.vmware.mo.HostMO) VirtualDeviceConfigSpec(com.vmware.vim25.VirtualDeviceConfigSpec) VirtualMachineMO(com.cloud.hypervisor.vmware.mo.VirtualMachineMO) VirtualDevice(com.vmware.vim25.VirtualDevice) 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) VirtualMachineConfigSpec(com.vmware.vim25.VirtualMachineConfigSpec) UnPlugNicAnswer(com.cloud.agent.api.UnPlugNicAnswer) PlugNicAnswer(com.cloud.agent.api.PlugNicAnswer) NicTO(com.cloud.agent.api.to.NicTO) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) DatacenterMO(com.cloud.hypervisor.vmware.mo.DatacenterMO)

Example 4 with PlugNicAnswer

use of com.cloud.agent.api.PlugNicAnswer in project cloudstack by apache.

the class HypervDirectConnectResource method execute.

private PlugNicAnswer execute(final PlugNicCommand cmd) {
    if (s_logger.isInfoEnabled()) {
        s_logger.info("Executing resource PlugNicCommand " + s_gson.toJson(cmd));
    }
    try {
        final String vmName = cmd.getVmName();
        final NicTO nic = cmd.getNic();
        final URI broadcastUri = nic.getBroadcastUri();
        if (BroadcastDomainType.getSchemeValue(broadcastUri) != BroadcastDomainType.Vlan) {
            throw new InternalErrorException("Unable to assign a public IP to a VIF on network " + nic.getBroadcastUri());
        }
        final String vlanId = BroadcastDomainType.getValue(broadcastUri);
        int publicNicInfo = -1;
        publicNicInfo = getVmFreeNicIndex(vmName);
        if (publicNicInfo > 0) {
            modifyNicVlan(vmName, vlanId, publicNicInfo, true, cmd.getNic().getName());
            return new PlugNicAnswer(cmd, true, "success");
        }
        final String msg = " Plug Nic failed for the vm as it has reached max limit of NICs to be added";
        s_logger.warn(msg);
        return new PlugNicAnswer(cmd, false, msg);
    } catch (final Exception e) {
        s_logger.error("Unexpected exception: ", e);
        return new PlugNicAnswer(cmd, false, "Unable to execute PlugNicCommand due to " + e.toString());
    }
}
Also used : InternalErrorException(com.cloud.exception.InternalErrorException) PlugNicAnswer(com.cloud.agent.api.PlugNicAnswer) UnPlugNicAnswer(com.cloud.agent.api.UnPlugNicAnswer) URI(java.net.URI) KeyStoreException(java.security.KeyStoreException) KeyManagementException(java.security.KeyManagementException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) ClientProtocolException(org.apache.http.client.ClientProtocolException) ConnectException(java.net.ConnectException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) RemoteException(java.rmi.RemoteException) InternalErrorException(com.cloud.exception.InternalErrorException) ConfigurationException(javax.naming.ConfigurationException) UnrecoverableKeyException(java.security.UnrecoverableKeyException) CertificateException(java.security.cert.CertificateException) NicTO(com.cloud.agent.api.to.NicTO)

Example 5 with PlugNicAnswer

use of com.cloud.agent.api.PlugNicAnswer in project cloudstack by apache.

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 String deviceId = citrixResourceBase.getLowestAvailableVIFDeviceNum(conn, vm);
        nic.setDeviceId(Integer.parseInt(deviceId));
        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.agent.api.PlugNicAnswer) NicTO(com.cloud.agent.api.to.NicTO)

Aggregations

PlugNicAnswer (com.cloud.agent.api.PlugNicAnswer)6 UnPlugNicAnswer (com.cloud.agent.api.UnPlugNicAnswer)4 NicTO (com.cloud.agent.api.to.NicTO)4 InternalErrorException (com.cloud.exception.InternalErrorException)3 Commands (com.cloud.agent.manager.Commands)2 AgentUnavailableException (com.cloud.exception.AgentUnavailableException)2 OperationTimedoutException (com.cloud.exception.OperationTimedoutException)2 IOException (java.io.IOException)2 ConnectException (java.net.ConnectException)2 RemoteException (java.rmi.RemoteException)2 ConfigurationException (javax.naming.ConfigurationException)2 PlugNicCommand (com.cloud.agent.api.PlugNicCommand)1 UnPlugNicCommand (com.cloud.agent.api.UnPlugNicCommand)1 CloudException (com.cloud.exception.CloudException)1 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)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