Search in sources :

Example 1 with Type

use of com.cloud.vm.VirtualMachine.Type in project cloudstack by apache.

the class LibvirtComputingResourceTest method testPlugNicCommandNoMatchMack.

@Test
public void testPlugNicCommandNoMatchMack() {
    final NicTO nic = Mockito.mock(NicTO.class);
    final String instanceName = "Test";
    final Type vmtype = Type.DomainRouter;
    final PlugNicCommand command = new PlugNicCommand(nic, instanceName, vmtype);
    final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class);
    final Connect conn = Mockito.mock(Connect.class);
    final Domain vm = Mockito.mock(Domain.class);
    final VifDriver vifDriver = Mockito.mock(VifDriver.class);
    final InterfaceDef interfaceDef = Mockito.mock(InterfaceDef.class);
    final List<InterfaceDef> nics = new ArrayList<InterfaceDef>();
    final InterfaceDef intDef = Mockito.mock(InterfaceDef.class);
    nics.add(intDef);
    when(libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
    when(libvirtComputingResource.getInterfaces(conn, command.getVmName())).thenReturn(nics);
    when(intDef.getDevName()).thenReturn("eth0");
    when(intDef.getBrName()).thenReturn("br0");
    when(intDef.getMacAddress()).thenReturn("00:00:00:00");
    when(nic.getMac()).thenReturn("00:00:00:01");
    try {
        when(libvirtUtilitiesHelper.getConnectionByVmName(command.getVmName())).thenReturn(conn);
        when(libvirtComputingResource.getDomain(conn, instanceName)).thenReturn(vm);
        when(libvirtComputingResource.getVifDriver(nic.getType())).thenReturn(vifDriver);
        when(vifDriver.plug(nic, "Other PV", "")).thenReturn(interfaceDef);
        when(interfaceDef.toString()).thenReturn("Interface");
        final String interfaceDefStr = interfaceDef.toString();
        doNothing().when(vm).attachDevice(interfaceDefStr);
    } catch (final LibvirtException e) {
        fail(e.getMessage());
    } catch (final InternalErrorException e) {
        fail(e.getMessage());
    }
    final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
    assertNotNull(wrapper);
    final Answer answer = wrapper.execute(command, libvirtComputingResource);
    assertTrue(answer.getResult());
    verify(libvirtComputingResource, times(1)).getLibvirtUtilitiesHelper();
    try {
        verify(libvirtUtilitiesHelper, times(1)).getConnectionByVmName(command.getVmName());
        verify(libvirtComputingResource, times(1)).getDomain(conn, instanceName);
        verify(libvirtComputingResource, times(1)).getVifDriver(nic.getType());
        verify(vifDriver, times(1)).plug(nic, "Other PV", "");
    } catch (final LibvirtException e) {
        fail(e.getMessage());
    } catch (final InternalErrorException e) {
        fail(e.getMessage());
    }
}
Also used : LibvirtException(org.libvirt.LibvirtException) Connect(org.libvirt.Connect) ArrayList(java.util.ArrayList) InternalErrorException(com.cloud.exception.InternalErrorException) LibvirtUtilitiesHelper(com.cloud.hypervisor.kvm.resource.wrapper.LibvirtUtilitiesHelper) InterfaceDef(com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef) AttachAnswer(org.apache.cloudstack.storage.command.AttachAnswer) Answer(com.cloud.agent.api.Answer) CheckRouterAnswer(com.cloud.agent.api.CheckRouterAnswer) TrafficType(com.cloud.network.Networks.TrafficType) Type(com.cloud.vm.VirtualMachine.Type) StoragePoolType(com.cloud.storage.Storage.StoragePoolType) BootloaderType(com.cloud.template.VirtualMachineTemplate.BootloaderType) LibvirtRequestWrapper(com.cloud.hypervisor.kvm.resource.wrapper.LibvirtRequestWrapper) Domain(org.libvirt.Domain) PlugNicCommand(com.cloud.agent.api.PlugNicCommand) UnPlugNicCommand(com.cloud.agent.api.UnPlugNicCommand) NicTO(com.cloud.agent.api.to.NicTO) Test(org.junit.Test)

Example 2 with Type

use of com.cloud.vm.VirtualMachine.Type in project cloudstack by apache.

the class LibvirtComputingResourceTest method testNetworkRulesSystemVmCommandFailure.

@SuppressWarnings("unchecked")
@Test
public void testNetworkRulesSystemVmCommandFailure() {
    final String vmName = "Test";
    final Type type = Type.SecondaryStorageVm;
    final NetworkRulesSystemVmCommand command = new NetworkRulesSystemVmCommand(vmName, type);
    final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class);
    when(libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
    try {
        when(libvirtUtilitiesHelper.getConnectionByVmName(command.getVmName())).thenThrow(LibvirtException.class);
    } catch (final LibvirtException e) {
        fail(e.getMessage());
    }
    final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
    assertNotNull(wrapper);
    final Answer answer = wrapper.execute(command, libvirtComputingResource);
    assertFalse(answer.getResult());
    try {
        verify(libvirtUtilitiesHelper, times(1)).getConnectionByVmName(command.getVmName());
    } catch (final LibvirtException e) {
        fail(e.getMessage());
    }
    verify(libvirtComputingResource, times(1)).getLibvirtUtilitiesHelper();
}
Also used : AttachAnswer(org.apache.cloudstack.storage.command.AttachAnswer) Answer(com.cloud.agent.api.Answer) CheckRouterAnswer(com.cloud.agent.api.CheckRouterAnswer) TrafficType(com.cloud.network.Networks.TrafficType) Type(com.cloud.vm.VirtualMachine.Type) StoragePoolType(com.cloud.storage.Storage.StoragePoolType) BootloaderType(com.cloud.template.VirtualMachineTemplate.BootloaderType) LibvirtRequestWrapper(com.cloud.hypervisor.kvm.resource.wrapper.LibvirtRequestWrapper) LibvirtException(org.libvirt.LibvirtException) NetworkRulesSystemVmCommand(com.cloud.agent.api.NetworkRulesSystemVmCommand) LibvirtUtilitiesHelper(com.cloud.hypervisor.kvm.resource.wrapper.LibvirtUtilitiesHelper) Test(org.junit.Test)

Example 3 with Type

use of com.cloud.vm.VirtualMachine.Type in project cloudstack by apache.

the class Ovm3HypervisorResourceTest method createVm.

public VirtualMachineTO createVm(String vmName) throws Ovm3ResourceException {
    con = support.prepConnectionResults();
    Xen vdata = new Xen(con);
    Xen.Vm vm = vdata.getVmConfig(vmName);
    vdata.listVm(xen.getRepoId(), xen.getVmId());
    // Ovm3VmGuestTypes types = new Ovm3VmGuestTypes();
    Long id = 1L;
    String instanceName = vm.getVmName();
    VirtualMachine.Type type = Type.User;
    // vm.getVmCpus();
    int cpus = 1;
    Integer speed = 0;
    long minRam = vm.getVmMemory();
    long maxRam = vm.getVmMemory();
    BootloaderType bootloader = BootloaderType.PyGrub;
    String os = "Oracle Enterprise Linux 6.0 (64-bit)";
    boolean enableHA = true;
    boolean limitCpuUse = false;
    String vncPassword = "gobbeldygoo";
    // public StartCommand(VirtualMachineTO vm, Host host, boolean
    // executeInSequence) {
    // ./api/src/com/cloud/agent/api/to/VirtualMachineTO.java
    VirtualMachineTO vmspec = new VirtualMachineTO(id, instanceName, type, cpus, speed, minRam, maxRam, bootloader, os, enableHA, limitCpuUse, vncPassword);
    vmspec.setBootArgs("");
    addDisksToSpec(vmspec, vm.getVmDisks());
    addNicsToSpec(vmspec, vm.getVmVifs());
    return vmspec;
}
Also used : Xen(com.cloud.hypervisor.ovm3.objects.Xen) Type(com.cloud.vm.VirtualMachine.Type) BootloaderType(com.cloud.template.VirtualMachineTemplate.BootloaderType) VirtualMachineTO(com.cloud.agent.api.to.VirtualMachineTO) VirtualMachine(com.cloud.vm.VirtualMachine)

Example 4 with Type

use of com.cloud.vm.VirtualMachine.Type in project cloudstack by apache.

the class LibvirtComputingResourceTest method testPlugNicCommandInternalError.

@SuppressWarnings("unchecked")
@Test
public void testPlugNicCommandInternalError() {
    final NicTO nic = Mockito.mock(NicTO.class);
    final String instanceName = "Test";
    final Type vmtype = Type.DomainRouter;
    final PlugNicCommand command = new PlugNicCommand(nic, instanceName, vmtype);
    final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class);
    final Connect conn = Mockito.mock(Connect.class);
    final Domain vm = Mockito.mock(Domain.class);
    final VifDriver vifDriver = Mockito.mock(VifDriver.class);
    final List<InterfaceDef> nics = new ArrayList<InterfaceDef>();
    final InterfaceDef intDef = Mockito.mock(InterfaceDef.class);
    nics.add(intDef);
    when(libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
    when(libvirtComputingResource.getInterfaces(conn, command.getVmName())).thenReturn(nics);
    when(intDef.getDevName()).thenReturn("eth0");
    when(intDef.getBrName()).thenReturn("br0");
    when(intDef.getMacAddress()).thenReturn("00:00:00:00");
    when(nic.getMac()).thenReturn("00:00:00:01");
    try {
        when(libvirtUtilitiesHelper.getConnectionByVmName(command.getVmName())).thenReturn(conn);
        when(libvirtComputingResource.getDomain(conn, instanceName)).thenReturn(vm);
        when(libvirtComputingResource.getVifDriver(nic.getType())).thenReturn(vifDriver);
        when(vifDriver.plug(nic, "Other PV", "")).thenThrow(InternalErrorException.class);
    } catch (final LibvirtException e) {
        fail(e.getMessage());
    } catch (final InternalErrorException e) {
        fail(e.getMessage());
    }
    final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
    assertNotNull(wrapper);
    final Answer answer = wrapper.execute(command, libvirtComputingResource);
    assertFalse(answer.getResult());
    verify(libvirtComputingResource, times(1)).getLibvirtUtilitiesHelper();
    try {
        verify(libvirtUtilitiesHelper, times(1)).getConnectionByVmName(command.getVmName());
        verify(libvirtComputingResource, times(1)).getDomain(conn, instanceName);
        verify(libvirtComputingResource, times(1)).getVifDriver(nic.getType());
        verify(vifDriver, times(1)).plug(nic, "Other PV", "");
    } catch (final LibvirtException e) {
        fail(e.getMessage());
    } catch (final InternalErrorException e) {
        fail(e.getMessage());
    }
}
Also used : LibvirtException(org.libvirt.LibvirtException) Connect(org.libvirt.Connect) ArrayList(java.util.ArrayList) InternalErrorException(com.cloud.exception.InternalErrorException) LibvirtUtilitiesHelper(com.cloud.hypervisor.kvm.resource.wrapper.LibvirtUtilitiesHelper) InterfaceDef(com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef) AttachAnswer(org.apache.cloudstack.storage.command.AttachAnswer) Answer(com.cloud.agent.api.Answer) CheckRouterAnswer(com.cloud.agent.api.CheckRouterAnswer) TrafficType(com.cloud.network.Networks.TrafficType) Type(com.cloud.vm.VirtualMachine.Type) StoragePoolType(com.cloud.storage.Storage.StoragePoolType) BootloaderType(com.cloud.template.VirtualMachineTemplate.BootloaderType) LibvirtRequestWrapper(com.cloud.hypervisor.kvm.resource.wrapper.LibvirtRequestWrapper) Domain(org.libvirt.Domain) PlugNicCommand(com.cloud.agent.api.PlugNicCommand) UnPlugNicCommand(com.cloud.agent.api.UnPlugNicCommand) NicTO(com.cloud.agent.api.to.NicTO) Test(org.junit.Test)

Example 5 with Type

use of com.cloud.vm.VirtualMachine.Type in project cloudstack by apache.

the class VMwareGuru method implement.

@Override
public VirtualMachineTO implement(VirtualMachineProfile vm) {
    VirtualMachineTO to = toVirtualMachineTO(vm);
    to.setBootloader(BootloaderType.HVM);
    Map<String, String> details = to.getDetails();
    if (details == null)
        details = new HashMap<String, String>();
    Type vmType = vm.getType();
    boolean userVm = !(vmType.equals(VirtualMachine.Type.DomainRouter) || vmType.equals(VirtualMachine.Type.ConsoleProxy) || vmType.equals(VirtualMachine.Type.SecondaryStorageVm));
    String nicDeviceType = details.get(VmDetailConstants.NIC_ADAPTER);
    if (!userVm) {
        if (nicDeviceType == null) {
            details.put(VmDetailConstants.NIC_ADAPTER, _vmwareMgr.getSystemVMDefaultNicAdapterType());
        } else {
            try {
                VirtualEthernetCardType.valueOf(nicDeviceType);
            } catch (Exception e) {
                s_logger.warn("Invalid NIC device type " + nicDeviceType + " is specified in VM details, switch to default E1000");
                details.put(VmDetailConstants.NIC_ADAPTER, VirtualEthernetCardType.E1000.toString());
            }
        }
    } else {
        // for user-VM, use E1000 as default
        if (nicDeviceType == null) {
            details.put(VmDetailConstants.NIC_ADAPTER, VirtualEthernetCardType.E1000.toString());
        } else {
            try {
                VirtualEthernetCardType.valueOf(nicDeviceType);
            } catch (Exception e) {
                s_logger.warn("Invalid NIC device type " + nicDeviceType + " is specified in VM details, switch to default E1000");
                details.put(VmDetailConstants.NIC_ADAPTER, VirtualEthernetCardType.E1000.toString());
            }
        }
    }
    String diskDeviceType = details.get(VmDetailConstants.ROOT_DISK_CONTROLLER);
    if (userVm) {
        if (diskDeviceType == null) {
            details.put(VmDetailConstants.ROOT_DISK_CONTROLLER, _vmwareMgr.getRootDiskController());
        }
    }
    String diskController = details.get(VmDetailConstants.DATA_DISK_CONTROLLER);
    if (userVm) {
        if (diskController == null) {
            details.put(VmDetailConstants.DATA_DISK_CONTROLLER, DiskControllerType.lsilogic.toString());
        }
    }
    List<NicProfile> nicProfiles = vm.getNics();
    for (NicProfile nicProfile : nicProfiles) {
        if (nicProfile.getTrafficType() == TrafficType.Guest) {
            if (_networkMgr.isProviderSupportServiceInNetwork(nicProfile.getNetworkId(), Service.Firewall, Provider.CiscoVnmc)) {
                details.put("ConfigureVServiceInNexus", Boolean.TRUE.toString());
            }
            break;
        }
    }
    long clusterId = getClusterId(vm.getId());
    details.put(VmwareReserveCpu.key(), VmwareReserveCpu.valueIn(clusterId).toString());
    details.put(VmwareReserveMemory.key(), VmwareReserveMemory.valueIn(clusterId).toString());
    to.setDetails(details);
    if (vmType.equals(VirtualMachine.Type.DomainRouter)) {
        NicProfile publicNicProfile = null;
        for (NicProfile nicProfile : nicProfiles) {
            if (nicProfile.getTrafficType() == TrafficType.Public) {
                publicNicProfile = nicProfile;
                break;
            }
        }
        if (publicNicProfile != null) {
            NicTO[] nics = to.getNics();
            // reserve extra NICs
            NicTO[] expandedNics = new NicTO[nics.length + _vmwareMgr.getRouterExtraPublicNics()];
            int i = 0;
            int deviceId = -1;
            for (i = 0; i < nics.length; i++) {
                expandedNics[i] = nics[i];
                if (nics[i].getDeviceId() > deviceId)
                    deviceId = nics[i].getDeviceId();
            }
            deviceId++;
            long networkId = publicNicProfile.getNetworkId();
            NetworkVO network = _networkDao.findById(networkId);
            for (; i < nics.length + _vmwareMgr.getRouterExtraPublicNics(); i++) {
                NicTO nicTo = new NicTO();
                nicTo.setDeviceId(deviceId++);
                nicTo.setBroadcastType(publicNicProfile.getBroadcastType());
                nicTo.setType(publicNicProfile.getTrafficType());
                nicTo.setIp("0.0.0.0");
                nicTo.setNetmask("255.255.255.255");
                try {
                    String mac = _networkMgr.getNextAvailableMacAddressInNetwork(networkId);
                    nicTo.setMac(mac);
                } catch (InsufficientAddressCapacityException e) {
                    throw new CloudRuntimeException("unable to allocate mac address on network: " + networkId);
                }
                nicTo.setDns1(publicNicProfile.getIPv4Dns1());
                nicTo.setDns2(publicNicProfile.getIPv4Dns2());
                if (publicNicProfile.getIPv4Gateway() != null) {
                    nicTo.setGateway(publicNicProfile.getIPv4Gateway());
                } else {
                    nicTo.setGateway(network.getGateway());
                }
                nicTo.setDefaultNic(false);
                nicTo.setBroadcastUri(publicNicProfile.getBroadCastUri());
                nicTo.setIsolationuri(publicNicProfile.getIsolationUri());
                Integer networkRate = _networkMgr.getNetworkRate(network.getId(), null);
                nicTo.setNetworkRateMbps(networkRate);
                expandedNics[i] = nicTo;
            }
            to.setNics(expandedNics);
        }
        StringBuffer sbMacSequence = new StringBuffer();
        for (NicTO nicTo : sortNicsByDeviceId(to.getNics())) {
            sbMacSequence.append(nicTo.getMac()).append("|");
        }
        if (!sbMacSequence.toString().isEmpty()) {
            sbMacSequence.deleteCharAt(sbMacSequence.length() - 1);
            String bootArgs = to.getBootArgs();
            to.setBootArgs(bootArgs + " nic_macs=" + sbMacSequence.toString());
        }
    }
    // Should only be done on user machines
    if (userVm) {
        configureNestedVirtualization(details, to);
    }
    // Determine the VM's OS description
    GuestOSVO guestOS = _guestOsDao.findByIdIncludingRemoved(vm.getVirtualMachine().getGuestOSId());
    to.setOs(guestOS.getDisplayName());
    to.setHostName(vm.getHostName());
    HostVO host = _hostDao.findById(vm.getVirtualMachine().getHostId());
    GuestOSHypervisorVO guestOsMapping = null;
    if (host != null) {
        guestOsMapping = _guestOsHypervisorDao.findByOsIdAndHypervisor(guestOS.getId(), getHypervisorType().toString(), host.getHypervisorVersion());
    }
    if (guestOsMapping == null || host == null) {
        to.setPlatformEmulator(null);
    } else {
        to.setPlatformEmulator(guestOsMapping.getGuestOsName());
    }
    return to;
}
Also used : NetworkVO(com.cloud.network.dao.NetworkVO) HashMap(java.util.HashMap) InsufficientAddressCapacityException(com.cloud.exception.InsufficientAddressCapacityException) GuestOSVO(com.cloud.storage.GuestOSVO) NicProfile(com.cloud.vm.NicProfile) VirtualMachineTO(com.cloud.agent.api.to.VirtualMachineTO) InsufficientAddressCapacityException(com.cloud.exception.InsufficientAddressCapacityException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) HostVO(com.cloud.host.HostVO) GuestOSHypervisorVO(com.cloud.storage.GuestOSHypervisorVO) TrafficType(com.cloud.network.Networks.TrafficType) Type(com.cloud.vm.VirtualMachine.Type) DiskControllerType(com.cloud.hypervisor.vmware.mo.DiskControllerType) BootloaderType(com.cloud.template.VirtualMachineTemplate.BootloaderType) DataObjectType(com.cloud.agent.api.to.DataObjectType) BroadcastDomainType(com.cloud.network.Networks.BroadcastDomainType) VirtualEthernetCardType(com.cloud.hypervisor.vmware.mo.VirtualEthernetCardType) HypervisorType(com.cloud.hypervisor.Hypervisor.HypervisorType) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) NicTO(com.cloud.agent.api.to.NicTO)

Aggregations

BootloaderType (com.cloud.template.VirtualMachineTemplate.BootloaderType)8 Type (com.cloud.vm.VirtualMachine.Type)8 TrafficType (com.cloud.network.Networks.TrafficType)7 Answer (com.cloud.agent.api.Answer)6 CheckRouterAnswer (com.cloud.agent.api.CheckRouterAnswer)6 LibvirtRequestWrapper (com.cloud.hypervisor.kvm.resource.wrapper.LibvirtRequestWrapper)6 LibvirtUtilitiesHelper (com.cloud.hypervisor.kvm.resource.wrapper.LibvirtUtilitiesHelper)6 StoragePoolType (com.cloud.storage.Storage.StoragePoolType)6 AttachAnswer (org.apache.cloudstack.storage.command.AttachAnswer)6 Test (org.junit.Test)6 LibvirtException (org.libvirt.LibvirtException)6 NicTO (com.cloud.agent.api.to.NicTO)5 PlugNicCommand (com.cloud.agent.api.PlugNicCommand)4 UnPlugNicCommand (com.cloud.agent.api.UnPlugNicCommand)4 Connect (org.libvirt.Connect)4 InterfaceDef (com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef)3 ArrayList (java.util.ArrayList)3 Domain (org.libvirt.Domain)3 NetworkRulesSystemVmCommand (com.cloud.agent.api.NetworkRulesSystemVmCommand)2 VirtualMachineTO (com.cloud.agent.api.to.VirtualMachineTO)2