Search in sources :

Example 21 with Connect

use of org.libvirt.Connect in project cloudstack by apache.

the class LibvirtGetVncPortCommandWrapper method execute.

@Override
public Answer execute(final GetVncPortCommand command, final LibvirtComputingResource libvirtComputingResource) {
    try {
        final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
        final Connect conn = libvirtUtilitiesHelper.getConnectionByVmName(command.getName());
        final Integer vncPort = libvirtComputingResource.getVncPort(conn, command.getName());
        return new GetVncPortAnswer(command, libvirtComputingResource.getPrivateIp(), 5900 + vncPort);
    } catch (final LibvirtException e) {
        return new GetVncPortAnswer(command, e.toString());
    }
}
Also used : GetVncPortAnswer(com.cloud.agent.api.GetVncPortAnswer) LibvirtException(org.libvirt.LibvirtException) Connect(org.libvirt.Connect)

Example 22 with Connect

use of org.libvirt.Connect in project cloudstack by apache.

the class LibvirtConnection method getConnectionByVmName.

public static Connect getConnectionByVmName(String vmName) throws LibvirtException {
    HypervisorType[] hypervisors = new HypervisorType[] { HypervisorType.KVM, Hypervisor.HypervisorType.LXC };
    for (HypervisorType hypervisor : hypervisors) {
        try {
            Connect conn = LibvirtConnection.getConnectionByType(hypervisor.toString());
            if (conn.domainLookupByName(vmName) != null) {
                return conn;
            }
        } catch (Exception e) {
            s_logger.debug("Can not find " + hypervisor.toString() + " connection for Instance: " + vmName + ", continuing.");
        }
    }
    s_logger.warn("Can not find a connection for Instance " + vmName + ". Assuming the default connection.");
    // return the default connection
    return getConnection();
}
Also used : HypervisorType(com.cloud.hypervisor.Hypervisor.HypervisorType) Connect(org.libvirt.Connect) LibvirtException(org.libvirt.LibvirtException)

Example 23 with Connect

use of org.libvirt.Connect in project cloudstack by apache.

the class LibvirtComputingResource method stop.

@Override
public boolean stop() {
    try {
        final Connect conn = LibvirtConnection.getConnection();
        conn.close();
    } catch (final LibvirtException e) {
        s_logger.trace("Ignoring libvirt error.", e);
    }
    return true;
}
Also used : LibvirtException(org.libvirt.LibvirtException) Connect(org.libvirt.Connect)

Example 24 with Connect

use of org.libvirt.Connect in project cloudstack by apache.

the class LibvirtComputingResource method prepareNetworkElementCommand.

private ExecutionResult prepareNetworkElementCommand(final SetupGuestNetworkCommand cmd) {
    Connect conn;
    final NicTO nic = cmd.getNic();
    final String routerName = cmd.getAccessDetail(NetworkElementCommand.ROUTER_NAME);
    try {
        conn = LibvirtConnection.getConnectionByVmName(routerName);
        final List<InterfaceDef> pluggedNics = getInterfaces(conn, routerName);
        InterfaceDef routerNic = null;
        for (final InterfaceDef pluggedNic : pluggedNics) {
            if (pluggedNic.getMacAddress().equalsIgnoreCase(nic.getMac())) {
                routerNic = pluggedNic;
                break;
            }
        }
        if (routerNic == null) {
            return new ExecutionResult(false, "Can not find nic with mac " + nic.getMac() + " for VM " + routerName);
        }
        return new ExecutionResult(true, null);
    } catch (final LibvirtException e) {
        final String msg = "Creating guest network failed due to " + e.toString();
        s_logger.warn(msg, e);
        return new ExecutionResult(false, msg);
    }
}
Also used : InterfaceDef(com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef) LibvirtException(org.libvirt.LibvirtException) Connect(org.libvirt.Connect) ExecutionResult(com.cloud.utils.ExecutionResult) NicTO(com.cloud.agent.api.to.NicTO)

Example 25 with Connect

use of org.libvirt.Connect in project cloudstack by apache.

the class LibvirtComputingResourceTest method testPvlanSetupCommandDhcpAdd.

@Test
public void testPvlanSetupCommandDhcpAdd() {
    final String op = "add";
    final URI uri = URI.create("http://localhost");
    final String networkTag = "/105";
    final String dhcpName = "dhcp";
    final String dhcpMac = "00:00:00:00";
    final String dhcpIp = "127.0.0.1";
    final PvlanSetupCommand command = PvlanSetupCommand.createDhcpSetup(op, uri, networkTag, dhcpName, dhcpMac, dhcpIp);
    final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class);
    final Connect conn = Mockito.mock(Connect.class);
    final String guestBridgeName = "br0";
    when(libvirtComputingResource.getGuestBridgeName()).thenReturn(guestBridgeName);
    when(libvirtComputingResource.getTimeout()).thenReturn(Duration.ZERO);
    final String ovsPvlanDhcpHostPath = "/pvlan";
    when(libvirtComputingResource.getOvsPvlanDhcpHostPath()).thenReturn(ovsPvlanDhcpHostPath);
    when(libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
    final List<InterfaceDef> ifaces = new ArrayList<InterfaceDef>();
    final InterfaceDef nic = Mockito.mock(InterfaceDef.class);
    ifaces.add(nic);
    try {
        when(libvirtUtilitiesHelper.getConnectionByVmName(dhcpName)).thenReturn(conn);
        when(libvirtComputingResource.getInterfaces(conn, dhcpName)).thenReturn(ifaces);
    } catch (final LibvirtException 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(dhcpName);
    } catch (final LibvirtException e) {
        fail(e.getMessage());
    }
}
Also used : 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) LibvirtRequestWrapper(com.cloud.hypervisor.kvm.resource.wrapper.LibvirtRequestWrapper) LibvirtException(org.libvirt.LibvirtException) Connect(org.libvirt.Connect) ArrayList(java.util.ArrayList) PvlanSetupCommand(com.cloud.agent.api.PvlanSetupCommand) URI(java.net.URI) LibvirtUtilitiesHelper(com.cloud.hypervisor.kvm.resource.wrapper.LibvirtUtilitiesHelper) Test(org.junit.Test)

Aggregations

Connect (org.libvirt.Connect)113 LibvirtException (org.libvirt.LibvirtException)112 Answer (com.cloud.agent.api.Answer)47 AttachAnswer (org.apache.cloudstack.storage.command.AttachAnswer)42 Test (org.junit.Test)40 CheckRouterAnswer (com.cloud.agent.api.CheckRouterAnswer)39 LibvirtRequestWrapper (com.cloud.hypervisor.kvm.resource.wrapper.LibvirtRequestWrapper)39 LibvirtUtilitiesHelper (com.cloud.hypervisor.kvm.resource.wrapper.LibvirtUtilitiesHelper)39 InternalErrorException (com.cloud.exception.InternalErrorException)33 Domain (org.libvirt.Domain)30 URISyntaxException (java.net.URISyntaxException)25 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)24 NicTO (com.cloud.agent.api.to.NicTO)23 InterfaceDef (com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef)22 KVMStoragePoolManager (com.cloud.hypervisor.kvm.storage.KVMStoragePoolManager)19 VirtualMachineTO (com.cloud.agent.api.to.VirtualMachineTO)16 ConfigurationException (javax.naming.ConfigurationException)15 ArrayList (java.util.ArrayList)14 IOException (java.io.IOException)13 HashMap (java.util.HashMap)11