Search in sources :

Example 1 with VirtualRoutingResource

use of com.cloud.common.virtualnetwork.VirtualRoutingResource in project cosmic by MissionCriticalCloud.

the class LibvirtComputingResourceTest method testNetworkElementCommand.

@Test
public void testNetworkElementCommand() {
    final CheckRouterCommand command = new CheckRouterCommand();
    final VirtualRoutingResource virtRouterResource = Mockito.mock(VirtualRoutingResource.class);
    when(this.libvirtComputingResource.getVirtRouterResource()).thenReturn(virtRouterResource);
    when(virtRouterResource.executeRequest(command)).thenReturn(new CheckRouterAnswer(command, "mock_resource"));
    final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
    assertNotNull(wrapper);
    final Answer answer = wrapper.execute(command, this.libvirtComputingResource);
    assertFalse(answer.getResult());
}
Also used : CheckRouterCommand(com.cloud.legacymodel.communication.command.CheckRouterCommand) Answer(com.cloud.legacymodel.communication.answer.Answer) CheckRouterAnswer(com.cloud.legacymodel.communication.answer.CheckRouterAnswer) AttachAnswer(com.cloud.legacymodel.communication.answer.AttachAnswer) LibvirtRequestWrapper(com.cloud.agent.resource.kvm.wrapper.LibvirtRequestWrapper) VirtualRoutingResource(com.cloud.common.virtualnetwork.VirtualRoutingResource) CheckRouterAnswer(com.cloud.legacymodel.communication.answer.CheckRouterAnswer) Test(org.junit.Test)

Example 2 with VirtualRoutingResource

use of com.cloud.common.virtualnetwork.VirtualRoutingResource in project cosmic by MissionCriticalCloud.

the class LibvirtComputingResourceTest method testCheckSshCommand.

@Test
public void testCheckSshCommand() {
    final String instanceName = "Test";
    final String ip = "127.0.0.1";
    final int port = 22;
    final CheckSshCommand command = new CheckSshCommand(instanceName, ip, port);
    final VirtualRoutingResource virtRouterResource = Mockito.mock(VirtualRoutingResource.class);
    final String privateIp = command.getIp();
    final int cmdPort = command.getPort();
    when(this.libvirtComputingResource.getVirtRouterResource()).thenReturn(virtRouterResource);
    when(virtRouterResource.connect(privateIp, cmdPort)).thenReturn(true);
    final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
    assertNotNull(wrapper);
    final Answer answer = wrapper.execute(command, this.libvirtComputingResource);
    assertTrue(answer.getResult());
    verify(this.libvirtComputingResource, times(1)).getVirtRouterResource();
    verify(virtRouterResource, times(1)).connect(privateIp, cmdPort);
}
Also used : Answer(com.cloud.legacymodel.communication.answer.Answer) CheckRouterAnswer(com.cloud.legacymodel.communication.answer.CheckRouterAnswer) AttachAnswer(com.cloud.legacymodel.communication.answer.AttachAnswer) LibvirtRequestWrapper(com.cloud.agent.resource.kvm.wrapper.LibvirtRequestWrapper) CheckSshCommand(com.cloud.legacymodel.communication.command.CheckSshCommand) VirtualRoutingResource(com.cloud.common.virtualnetwork.VirtualRoutingResource) Test(org.junit.Test)

Example 3 with VirtualRoutingResource

use of com.cloud.common.virtualnetwork.VirtualRoutingResource in project cosmic by MissionCriticalCloud.

the class LibvirtComputingResourceTest method testCheckSshCommandFailure.

@Test
public void testCheckSshCommandFailure() {
    final String instanceName = "Test";
    final String ip = "127.0.0.1";
    final int port = 22;
    final CheckSshCommand command = new CheckSshCommand(instanceName, ip, port);
    final VirtualRoutingResource virtRouterResource = Mockito.mock(VirtualRoutingResource.class);
    final String privateIp = command.getIp();
    final int cmdPort = command.getPort();
    when(this.libvirtComputingResource.getVirtRouterResource()).thenReturn(virtRouterResource);
    when(virtRouterResource.connect(privateIp, cmdPort)).thenReturn(false);
    final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
    assertNotNull(wrapper);
    final Answer answer = wrapper.execute(command, this.libvirtComputingResource);
    assertFalse(answer.getResult());
    verify(this.libvirtComputingResource, times(1)).getVirtRouterResource();
    verify(virtRouterResource, times(1)).connect(privateIp, cmdPort);
}
Also used : Answer(com.cloud.legacymodel.communication.answer.Answer) CheckRouterAnswer(com.cloud.legacymodel.communication.answer.CheckRouterAnswer) AttachAnswer(com.cloud.legacymodel.communication.answer.AttachAnswer) LibvirtRequestWrapper(com.cloud.agent.resource.kvm.wrapper.LibvirtRequestWrapper) CheckSshCommand(com.cloud.legacymodel.communication.command.CheckSshCommand) VirtualRoutingResource(com.cloud.common.virtualnetwork.VirtualRoutingResource) Test(org.junit.Test)

Example 4 with VirtualRoutingResource

use of com.cloud.common.virtualnetwork.VirtualRoutingResource in project cosmic by MissionCriticalCloud.

the class LibvirtStartCommandWrapper method execute.

@Override
public Answer execute(final StartCommand command, final LibvirtComputingResource libvirtComputingResource) {
    final VirtualMachineTO vmSpec = command.getVirtualMachine();
    final String vmName = vmSpec.getName();
    LibvirtVmDef vm = null;
    DomainState state = DomainState.VIR_DOMAIN_SHUTOFF;
    final KvmStoragePoolManager storagePoolMgr = libvirtComputingResource.getStoragePoolMgr();
    final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
    Connect conn = null;
    try {
        vm = libvirtComputingResource.createVmFromSpec(vmSpec);
        conn = libvirtUtilitiesHelper.getConnectionByType(vm.getHvsType());
        final Long remainingMem = getFreeMemory(conn, libvirtComputingResource);
        if (remainingMem == null) {
            return new StartAnswer(command, "Failed to get free memory");
        } else if (remainingMem < vmSpec.getMinRam()) {
            return new StartAnswer(command, "Not enough memory on the host, remaining: " + remainingMem + ", asking: " + vmSpec.getMinRam());
        }
        final NicTO[] nics = vmSpec.getNics();
        libvirtComputingResource.createVbd(conn, vmSpec, vmName, vm);
        if (!storagePoolMgr.connectPhysicalDisksViaVmSpec(vmSpec)) {
            return new StartAnswer(command, "Failed to connect physical disks to host");
        }
        libvirtComputingResource.createVifs(vmSpec, vm);
        s_logger.debug("starting " + vmName + ": " + vm.toString());
        libvirtComputingResource.startVm(conn, vmName, vm.toString());
        // system vms
        if (vmSpec.getType() != VirtualMachineType.User) {
            // pass cmdline with config for the systemvm to configure itself
            if (libvirtComputingResource.passCmdLine(vmName, vmSpec.getBootArgs())) {
                s_logger.debug("Passing cmdline succeeded");
            } else {
                final String errorMessage = "Passing cmdline failed, aborting.";
                s_logger.debug(errorMessage);
                return new StartAnswer(command, errorMessage);
            }
            String controlIp = null;
            for (final NicTO nic : nics) {
                if (nic.getType() == TrafficType.Control) {
                    controlIp = nic.getIp();
                    break;
                }
            }
            // connect to the router by using its linklocal address (that should now be configured)
            s_logger.debug("Starting ssh attempts to " + controlIp);
            final VirtualRoutingResource virtRouterResource = libvirtComputingResource.getVirtRouterResource();
            if (!virtRouterResource.connect(controlIp, 30, 5000)) {
                final String errorMessage = "Unable to login to router via linklocal address " + controlIp + " after 30 tries, aborting.";
                s_logger.debug(errorMessage);
                return new StartAnswer(command, errorMessage);
            }
            s_logger.debug("Successfully completed ssh attempts to " + controlIp);
        }
        state = DomainState.VIR_DOMAIN_RUNNING;
        return new StartAnswer(command);
    } catch (final LibvirtException | InternalErrorException | URISyntaxException e) {
        s_logger.warn("Exception while starting VM: " + ExceptionUtils.getRootCauseMessage(e));
        if (conn != null) {
            libvirtComputingResource.handleVmStartFailure(vm);
        }
        return new StartAnswer(command, e.getMessage());
    } finally {
        if (state != DomainState.VIR_DOMAIN_RUNNING) {
            storagePoolMgr.disconnectPhysicalDisksViaVmSpec(vmSpec);
        }
    }
}
Also used : LibvirtVmDef(com.cloud.agent.resource.kvm.xml.LibvirtVmDef) StartAnswer(com.cloud.legacymodel.communication.answer.StartAnswer) LibvirtException(org.libvirt.LibvirtException) Connect(org.libvirt.Connect) VirtualRoutingResource(com.cloud.common.virtualnetwork.VirtualRoutingResource) InternalErrorException(com.cloud.legacymodel.exceptions.InternalErrorException) URISyntaxException(java.net.URISyntaxException) VirtualMachineTO(com.cloud.legacymodel.to.VirtualMachineTO) DomainState(org.libvirt.DomainInfo.DomainState) KvmStoragePoolManager(com.cloud.agent.resource.kvm.storage.KvmStoragePoolManager) NicTO(com.cloud.legacymodel.to.NicTO)

Example 5 with VirtualRoutingResource

use of com.cloud.common.virtualnetwork.VirtualRoutingResource in project cosmic by MissionCriticalCloud.

the class LibvirtComputingResourceTest method testStartCommand.

@Test
public void testStartCommand() {
    final VirtualMachineTO vmSpec = Mockito.mock(VirtualMachineTO.class);
    final Host host = Mockito.mock(Host.class);
    final boolean executeInSequence = false;
    final StartCommand command = new StartCommand(vmSpec, host, executeInSequence);
    final KvmStoragePoolManager storagePoolMgr = Mockito.mock(KvmStoragePoolManager.class);
    final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class);
    final Connect conn = Mockito.mock(Connect.class);
    final LibvirtVmDef vmDef = Mockito.mock(LibvirtVmDef.class);
    final VirtualRoutingResource virtRouterResource = Mockito.mock(VirtualRoutingResource.class);
    final NicTO nic = Mockito.mock(NicTO.class);
    final NicTO[] nics = new NicTO[] { nic };
    final int[] vms = new int[0];
    final String vmName = "Test";
    final String controlIp = "127.0.0.1";
    when(this.libvirtComputingResource.getStoragePoolMgr()).thenReturn(storagePoolMgr);
    when(vmSpec.getNics()).thenReturn(nics);
    when(vmSpec.getType()).thenReturn(VirtualMachineType.DomainRouter);
    when(vmSpec.getName()).thenReturn(vmName);
    when(this.libvirtComputingResource.createVmFromSpec(vmSpec)).thenReturn(vmDef);
    when(this.libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
    try {
        when(libvirtUtilitiesHelper.getConnectionByType(vmDef.getHvsType())).thenReturn(conn);
        when(conn.listDomains()).thenReturn(vms);
        doNothing().when(this.libvirtComputingResource).createVbd(conn, vmSpec, vmName, vmDef);
    } catch (final LibvirtException e) {
        fail(e.getMessage());
    } catch (final InternalErrorException e) {
        fail(e.getMessage());
    } catch (final URISyntaxException e) {
        fail(e.getMessage());
    }
    when(storagePoolMgr.connectPhysicalDisksViaVmSpec(vmSpec)).thenReturn(true);
    try {
        doNothing().when(this.libvirtComputingResource).createVifs(vmSpec, vmDef);
        when(this.libvirtComputingResource.startVm(conn, vmName, vmDef.toString())).thenReturn("SUCCESS");
        when(vmSpec.getBootArgs()).thenReturn("ls -lart");
        when(this.libvirtComputingResource.passCmdLine(vmName, vmSpec.getBootArgs())).thenReturn(true);
        when(nic.getIp()).thenReturn(controlIp);
        when(nic.getType()).thenReturn(TrafficType.Control);
        when(this.libvirtComputingResource.getVirtRouterResource()).thenReturn(virtRouterResource);
        when(virtRouterResource.connect(controlIp, 30, 5000)).thenReturn(true);
    } catch (final InternalErrorException e) {
        fail(e.getMessage());
    } catch (final LibvirtException e) {
        fail(e.getMessage());
    }
    final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
    assertNotNull(wrapper);
    final Answer answer = wrapper.execute(command, this.libvirtComputingResource);
    assertTrue(answer.getResult());
    verify(this.libvirtComputingResource, times(1)).getStoragePoolMgr();
    verify(this.libvirtComputingResource, times(1)).getLibvirtUtilitiesHelper();
    try {
        verify(libvirtUtilitiesHelper, times(1)).getConnectionByType(vmDef.getHvsType());
    } catch (final LibvirtException e) {
        fail(e.getMessage());
    }
}
Also used : LibvirtVmDef(com.cloud.agent.resource.kvm.xml.LibvirtVmDef) LibvirtException(org.libvirt.LibvirtException) StartCommand(com.cloud.legacymodel.communication.command.StartCommand) Connect(org.libvirt.Connect) VirtualRoutingResource(com.cloud.common.virtualnetwork.VirtualRoutingResource) Host(com.cloud.legacymodel.dc.Host) InternalErrorException(com.cloud.legacymodel.exceptions.InternalErrorException) URISyntaxException(java.net.URISyntaxException) VirtualMachineTO(com.cloud.legacymodel.to.VirtualMachineTO) LibvirtUtilitiesHelper(com.cloud.agent.resource.kvm.wrapper.LibvirtUtilitiesHelper) Answer(com.cloud.legacymodel.communication.answer.Answer) CheckRouterAnswer(com.cloud.legacymodel.communication.answer.CheckRouterAnswer) AttachAnswer(com.cloud.legacymodel.communication.answer.AttachAnswer) LibvirtRequestWrapper(com.cloud.agent.resource.kvm.wrapper.LibvirtRequestWrapper) KvmStoragePoolManager(com.cloud.agent.resource.kvm.storage.KvmStoragePoolManager) NicTO(com.cloud.legacymodel.to.NicTO) Test(org.junit.Test)

Aggregations

VirtualRoutingResource (com.cloud.common.virtualnetwork.VirtualRoutingResource)11 Answer (com.cloud.legacymodel.communication.answer.Answer)8 LibvirtRequestWrapper (com.cloud.agent.resource.kvm.wrapper.LibvirtRequestWrapper)7 AttachAnswer (com.cloud.legacymodel.communication.answer.AttachAnswer)7 CheckRouterAnswer (com.cloud.legacymodel.communication.answer.CheckRouterAnswer)7 Test (org.junit.Test)7 Connect (org.libvirt.Connect)5 LibvirtException (org.libvirt.LibvirtException)5 LibvirtUtilitiesHelper (com.cloud.agent.resource.kvm.wrapper.LibvirtUtilitiesHelper)4 KvmStoragePoolManager (com.cloud.agent.resource.kvm.storage.KvmStoragePoolManager)3 LibvirtVmDef (com.cloud.agent.resource.kvm.xml.LibvirtVmDef)3 InternalErrorException (com.cloud.legacymodel.exceptions.InternalErrorException)3 NicTO (com.cloud.legacymodel.to.NicTO)3 VirtualMachineTO (com.cloud.legacymodel.to.VirtualMachineTO)3 URISyntaxException (java.net.URISyntaxException)3 CheckSshCommand (com.cloud.legacymodel.communication.command.CheckSshCommand)2 RebootRouterCommand (com.cloud.legacymodel.communication.command.RebootRouterCommand)2 StartCommand (com.cloud.legacymodel.communication.command.StartCommand)2 Host (com.cloud.legacymodel.dc.Host)2 CheckSshAnswer (com.cloud.legacymodel.communication.answer.CheckSshAnswer)1