Search in sources :

Example 36 with NicTO

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

the class VmwareResource method postNvpConfigBeforeStart.

private static void postNvpConfigBeforeStart(VirtualMachineMO vmMo, VirtualMachineTO vmSpec) throws Exception {
    /**
         * We need to configure the port on the DV switch after the host is
         * connected. So make this happen between the configure and start of
         * the VM
         */
    int nicIndex = 0;
    for (NicTO nicTo : sortNicsByDeviceId(vmSpec.getNics())) {
        if (nicTo.getBroadcastType() == BroadcastDomainType.Lswitch) {
            // We need to create a port with a unique vlan and pass the key to the nic device
            s_logger.trace("Nic " + nicTo.toString() + " is connected to an NVP logicalswitch");
            VirtualDevice nicVirtualDevice = vmMo.getNicDeviceByIndex(nicIndex);
            if (nicVirtualDevice == null) {
                //FIXME Generic exceptions are bad
                throw new Exception("Failed to find a VirtualDevice for nic " + nicIndex);
            }
            VirtualDeviceBackingInfo backing = nicVirtualDevice.getBacking();
            if (backing instanceof VirtualEthernetCardDistributedVirtualPortBackingInfo) {
                // This NIC is connected to a Distributed Virtual Switch
                VirtualEthernetCardDistributedVirtualPortBackingInfo portInfo = (VirtualEthernetCardDistributedVirtualPortBackingInfo) backing;
                DistributedVirtualSwitchPortConnection port = portInfo.getPort();
                String portKey = port.getPortKey();
                String portGroupKey = port.getPortgroupKey();
                String dvSwitchUuid = port.getSwitchUuid();
                s_logger.debug("NIC " + nicTo.toString() + " is connected to dvSwitch " + dvSwitchUuid + " pg " + portGroupKey + " port " + portKey);
                ManagedObjectReference dvSwitchManager = vmMo.getContext().getVimClient().getServiceContent().getDvSwitchManager();
                ManagedObjectReference dvSwitch = vmMo.getContext().getVimClient().getService().queryDvsByUuid(dvSwitchManager, dvSwitchUuid);
                // Get all ports
                DistributedVirtualSwitchPortCriteria criteria = new DistributedVirtualSwitchPortCriteria();
                criteria.setInside(true);
                criteria.getPortgroupKey().add(portGroupKey);
                List<DistributedVirtualPort> dvPorts = vmMo.getContext().getVimClient().getService().fetchDVPorts(dvSwitch, criteria);
                DistributedVirtualPort vmDvPort = null;
                List<Integer> usedVlans = new ArrayList<Integer>();
                for (DistributedVirtualPort dvPort : dvPorts) {
                    // Find the port for this NIC by portkey
                    if (portKey.equals(dvPort.getKey())) {
                        vmDvPort = dvPort;
                    }
                    VMwareDVSPortSetting settings = (VMwareDVSPortSetting) dvPort.getConfig().getSetting();
                    VmwareDistributedVirtualSwitchVlanIdSpec vlanId = (VmwareDistributedVirtualSwitchVlanIdSpec) settings.getVlan();
                    s_logger.trace("Found port " + dvPort.getKey() + " with vlan " + vlanId.getVlanId());
                    if (vlanId.getVlanId() > 0 && vlanId.getVlanId() < 4095) {
                        usedVlans.add(vlanId.getVlanId());
                    }
                }
                if (vmDvPort == null) {
                    throw new Exception("Empty port list from dvSwitch for nic " + nicTo.toString());
                }
                DVPortConfigInfo dvPortConfigInfo = vmDvPort.getConfig();
                VMwareDVSPortSetting settings = (VMwareDVSPortSetting) dvPortConfigInfo.getSetting();
                VmwareDistributedVirtualSwitchVlanIdSpec vlanId = (VmwareDistributedVirtualSwitchVlanIdSpec) settings.getVlan();
                BoolPolicy blocked = settings.getBlocked();
                if (blocked.isValue() == Boolean.TRUE) {
                    s_logger.trace("Port is blocked, set a vlanid and unblock");
                    DVPortConfigSpec dvPortConfigSpec = new DVPortConfigSpec();
                    VMwareDVSPortSetting edittedSettings = new VMwareDVSPortSetting();
                    // Unblock
                    blocked.setValue(Boolean.FALSE);
                    blocked.setInherited(Boolean.FALSE);
                    edittedSettings.setBlocked(blocked);
                    // Set vlan
                    int i;
                    for (i = 1; i < 4095; i++) {
                        if (!usedVlans.contains(i))
                            break;
                    }
                    // FIXME should be a determined
                    vlanId.setVlanId(i);
                    // based on usage
                    vlanId.setInherited(false);
                    edittedSettings.setVlan(vlanId);
                    dvPortConfigSpec.setSetting(edittedSettings);
                    dvPortConfigSpec.setOperation("edit");
                    dvPortConfigSpec.setKey(portKey);
                    List<DVPortConfigSpec> dvPortConfigSpecs = new ArrayList<DVPortConfigSpec>();
                    dvPortConfigSpecs.add(dvPortConfigSpec);
                    ManagedObjectReference task = vmMo.getContext().getVimClient().getService().reconfigureDVPortTask(dvSwitch, dvPortConfigSpecs);
                    if (!vmMo.getContext().getVimClient().waitForTask(task)) {
                        throw new Exception("Failed to configure the dvSwitch port for nic " + nicTo.toString());
                    }
                    s_logger.debug("NIC " + nicTo.toString() + " connected to vlan " + i);
                } else {
                    s_logger.trace("Port already configured and set to vlan " + vlanId.getVlanId());
                }
            } else if (backing instanceof VirtualEthernetCardNetworkBackingInfo) {
            // This NIC is connected to a Virtual Switch
            // Nothing to do
            } else if (backing instanceof VirtualEthernetCardOpaqueNetworkBackingInfo) {
            //if NSX API VERSION >= 4.2, connect to br-int (nsx.network), do not create portgroup else previous behaviour
            //OK, connected to OpaqueNetwork
            } else {
                s_logger.error("nic device backing is of type " + backing.getClass().getName());
                //FIXME Generic exceptions are bad
                throw new Exception("Incompatible backing for a VirtualDevice for nic " + nicIndex);
            }
        }
        nicIndex++;
    }
}
Also used : DVPortConfigSpec(com.vmware.vim25.DVPortConfigSpec) VMwareDVSPortSetting(com.vmware.vim25.VMwareDVSPortSetting) VirtualEthernetCardDistributedVirtualPortBackingInfo(com.vmware.vim25.VirtualEthernetCardDistributedVirtualPortBackingInfo) VirtualEthernetCardNetworkBackingInfo(com.vmware.vim25.VirtualEthernetCardNetworkBackingInfo) VirtualEthernetCardOpaqueNetworkBackingInfo(com.vmware.vim25.VirtualEthernetCardOpaqueNetworkBackingInfo) DistributedVirtualPort(com.vmware.vim25.DistributedVirtualPort) VirtualDevice(com.vmware.vim25.VirtualDevice) ArrayList(java.util.ArrayList) VirtualDeviceBackingInfo(com.vmware.vim25.VirtualDeviceBackingInfo) DistributedVirtualSwitchPortConnection(com.vmware.vim25.DistributedVirtualSwitchPortConnection) BoolPolicy(com.vmware.vim25.BoolPolicy) 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) DVPortConfigInfo(com.vmware.vim25.DVPortConfigInfo) DistributedVirtualSwitchPortCriteria(com.vmware.vim25.DistributedVirtualSwitchPortCriteria) VmwareDistributedVirtualSwitchVlanIdSpec(com.vmware.vim25.VmwareDistributedVirtualSwitchVlanIdSpec) NicTO(com.cloud.agent.api.to.NicTO) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 37 with NicTO

use of com.cloud.agent.api.to.NicTO 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 38 with NicTO

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

the class VmwareResource method execute.

protected Answer execute(PrepareForMigrationCommand cmd) {
    if (s_logger.isInfoEnabled()) {
        s_logger.info("Executing resource PrepareForMigrationCommand: " + _gson.toJson(cmd));
    }
    VirtualMachineTO vm = cmd.getVirtualMachine();
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("Preparing host for migrating " + vm);
    }
    final String vmName = vm.getName();
    try {
        VmwareHypervisorHost hyperHost = getHyperHost(getServiceContext());
        VmwareManager mgr = hyperHost.getContext().getStockObject(VmwareManager.CONTEXT_STOCK_NAME);
        // find VM through datacenter (VM is not at the target host yet)
        VirtualMachineMO vmMo = hyperHost.findVmOnPeerHyperHost(vmName);
        if (vmMo == null) {
            s_logger.info("VM " + vmName + " was not found in the cluster of host " + hyperHost.getHyperHostName() + ". Looking for the VM in datacenter.");
            ManagedObjectReference dcMor = hyperHost.getHyperHostDatacenter();
            DatacenterMO dcMo = new DatacenterMO(hyperHost.getContext(), dcMor);
            vmMo = dcMo.findVm(vmName);
            if (vmMo == null) {
                String msg = "VM " + vmName + " does not exist in VMware datacenter";
                s_logger.error(msg);
                throw new Exception(msg);
            }
        }
        NicTO[] nics = vm.getNics();
        for (NicTO nic : nics) {
            // prepare network on the host
            prepareNetworkFromNicInfo(new HostMO(getServiceContext(), _morHyperHost), nic, false, cmd.getVirtualMachine().getType());
        }
        Pair<String, Long> secStoreUrlAndId = mgr.getSecondaryStorageStoreUrlAndId(Long.parseLong(_dcId));
        String secStoreUrl = secStoreUrlAndId.first();
        Long secStoreId = secStoreUrlAndId.second();
        if (secStoreUrl == null) {
            String msg = "secondary storage for dc " + _dcId + " is not ready yet?";
            throw new Exception(msg);
        }
        mgr.prepareSecondaryStorageStore(secStoreUrl, secStoreId);
        ManagedObjectReference morSecDs = prepareSecondaryDatastoreOnHost(secStoreUrl);
        if (morSecDs == null) {
            String msg = "Failed to prepare secondary storage on host, secondary store url: " + secStoreUrl;
            throw new Exception(msg);
        }
        return new PrepareForMigrationAnswer(cmd);
    } catch (Throwable e) {
        if (e instanceof RemoteException) {
            s_logger.warn("Encounter remote exception to vCenter, invalidate VMware session context");
            invalidateServiceContext();
        }
        String msg = "Unexcpeted exception " + VmwareHelper.getExceptionMessage(e);
        s_logger.error(msg, e);
        return new PrepareForMigrationAnswer(cmd, msg);
    }
}
Also used : VmwareManager(com.cloud.hypervisor.vmware.manager.VmwareManager) HostMO(com.cloud.hypervisor.vmware.mo.HostMO) VirtualMachineMO(com.cloud.hypervisor.vmware.mo.VirtualMachineMO) VmwareHypervisorHost(com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost) VirtualMachineTO(com.cloud.agent.api.to.VirtualMachineTO) 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) PrepareForMigrationAnswer(com.cloud.agent.api.PrepareForMigrationAnswer) RemoteException(java.rmi.RemoteException) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) DatacenterMO(com.cloud.hypervisor.vmware.mo.DatacenterMO) NicTO(com.cloud.agent.api.to.NicTO)

Example 39 with NicTO

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

the class VmwareResource method prepareNetworkElementCommand.

protected ExecutionResult prepareNetworkElementCommand(SetupGuestNetworkCommand cmd) {
    NicTO nic = cmd.getNic();
    String routerIp = getRouterSshControlIp(cmd);
    String domrName = cmd.getAccessDetail(NetworkElementCommand.ROUTER_NAME);
    try {
        int ethDeviceNum = findRouterEthDeviceIndex(domrName, routerIp, nic.getMac());
        nic.setDeviceId(ethDeviceNum);
    } catch (Exception e) {
        String msg = "Prepare SetupGuestNetwork failed due to " + e.toString();
        s_logger.warn(msg, e);
        return new ExecutionResult(false, msg);
    }
    return new ExecutionResult(true, null);
}
Also used : ExecutionResult(com.cloud.utils.ExecutionResult) 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) NicTO(com.cloud.agent.api.to.NicTO)

Example 40 with NicTO

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

the class ConfigHelperTest method generateLoadBalancerConfigCommand.

protected LoadBalancerConfigCommand generateLoadBalancerConfigCommand() {
    final List<LoadBalancerTO> lbs = new ArrayList<>();
    final List<LbDestination> dests = new ArrayList<>();
    dests.add(new LbDestination(80, 8080, "10.1.10.2", false));
    dests.add(new LbDestination(80, 8080, "10.1.10.2", true));
    lbs.add(new LoadBalancerTO(UUID.randomUUID().toString(), "64.10.1.10", 80, "tcp", "algo", false, false, false, dests));
    final LoadBalancerTO[] arrayLbs = new LoadBalancerTO[lbs.size()];
    lbs.toArray(arrayLbs);
    final NicTO nic = new NicTO();
    final LoadBalancerConfigCommand cmd = new LoadBalancerConfigCommand(arrayLbs, "64.10.2.10", "10.1.10.2", "192.168.1.2", nic, null, "1000", false);
    cmd.setAccessDetail(NetworkElementCommand.ROUTER_IP, "10.1.10.2");
    cmd.setAccessDetail(NetworkElementCommand.ROUTER_NAME, ROUTERNAME);
    return cmd;
}
Also used : ArrayList(java.util.ArrayList) LoadBalancerTO(com.cloud.agent.api.to.LoadBalancerTO) LbDestination(com.cloud.network.lb.LoadBalancingRule.LbDestination) NicTO(com.cloud.agent.api.to.NicTO) LoadBalancerConfigCommand(com.cloud.agent.api.routing.LoadBalancerConfigCommand)

Aggregations

NicTO (com.cloud.agent.api.to.NicTO)99 VirtualMachineTO (com.cloud.agent.api.to.VirtualMachineTO)42 Answer (com.cloud.agent.api.Answer)31 Test (org.junit.Test)30 InternalErrorException (com.cloud.exception.InternalErrorException)28 LibvirtException (org.libvirt.LibvirtException)27 ArrayList (java.util.ArrayList)25 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)24 ConfigurationException (javax.naming.ConfigurationException)23 Connect (org.libvirt.Connect)23 URISyntaxException (java.net.URISyntaxException)22 AttachAnswer (org.apache.cloudstack.storage.command.AttachAnswer)21 IOException (java.io.IOException)20 CheckRouterAnswer (com.cloud.agent.api.CheckRouterAnswer)19 LibvirtRequestWrapper (com.cloud.hypervisor.kvm.resource.wrapper.LibvirtRequestWrapper)19 LibvirtUtilitiesHelper (com.cloud.hypervisor.kvm.resource.wrapper.LibvirtUtilitiesHelper)19 Connection (com.xensource.xenapi.Connection)18 KVMStoragePoolManager (com.cloud.hypervisor.kvm.storage.KVMStoragePoolManager)14 UnPlugNicCommand (com.cloud.agent.api.UnPlugNicCommand)13 URI (java.net.URI)12