Search in sources :

Example 76 with Connect

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

the class LibvirtStorageAdaptor method deleteStoragePool.

@Override
public boolean deleteStoragePool(String uuid) {
    s_logger.info("Attempting to remove storage pool " + uuid + " from libvirt");
    Connect conn = null;
    try {
        conn = LibvirtConnection.getConnection();
    } catch (LibvirtException e) {
        throw new CloudRuntimeException(e.toString());
    }
    StoragePool sp = null;
    Secret s = null;
    try {
        sp = conn.storagePoolLookupByUUIDString(uuid);
    } catch (LibvirtException e) {
        s_logger.warn("Storage pool " + uuid + " doesn't exist in libvirt. Assuming it is already removed");
        return true;
    }
    /*
         * Some storage pools, like RBD also have 'secret' information stored in libvirt
         * Destroy them if they exist
         */
    try {
        s = conn.secretLookupByUUIDString(uuid);
    } catch (LibvirtException e) {
        s_logger.info("Storage pool " + uuid + " has no corresponding secret. Not removing any secret.");
    }
    try {
        if (sp.isPersistent() == 1) {
            sp.destroy();
            sp.undefine();
        } else {
            sp.destroy();
        }
        sp.free();
        if (s != null) {
            s.undefine();
            s.free();
        }
        s_logger.info("Storage pool " + uuid + " was succesfully removed from libvirt.");
        return true;
    } catch (LibvirtException e) {
        // handle ebusy error when pool is quickly destroyed
        if (e.toString().contains("exit status 16")) {
            String targetPath = _mountPoint + File.separator + uuid;
            s_logger.error("deleteStoragePool removed pool from libvirt, but libvirt had trouble unmounting the pool. Trying umount location " + targetPath + "again in a few seconds");
            String result = Script.runSimpleBashScript("sleep 5 && umount " + targetPath);
            if (result == null) {
                s_logger.error("Succeeded in unmounting " + targetPath);
                return true;
            }
            s_logger.error("Failed to unmount " + targetPath);
        }
        throw new CloudRuntimeException(e.toString(), e);
    }
}
Also used : Secret(org.libvirt.Secret) LibvirtException(org.libvirt.LibvirtException) StoragePool(org.libvirt.StoragePool) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) Connect(org.libvirt.Connect)

Example 77 with Connect

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

the class LibvirtComputingResourceTest method testGetVmStatsCommand.

@Test
public void testGetVmStatsCommand() {
    final Connect conn = Mockito.mock(Connect.class);
    final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class);
    final String vmName = "Test";
    final String uuid = "e8d6b4d0-bc6d-4613-b8bb-cb9e0600f3c6";
    final List<String> vms = new ArrayList<String>();
    vms.add(vmName);
    final GetVmStatsCommand command = new GetVmStatsCommand(vms, uuid, "hostname");
    when(libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
    try {
        when(libvirtUtilitiesHelper.getConnectionByVmName(vmName)).thenReturn(conn);
    } catch (final LibvirtException 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(vmName);
    } catch (final LibvirtException e) {
        fail(e.getMessage());
    }
}
Also used : 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) LibvirtUtilitiesHelper(com.cloud.hypervisor.kvm.resource.wrapper.LibvirtUtilitiesHelper) GetVmStatsCommand(com.cloud.agent.api.GetVmStatsCommand) Test(org.junit.Test)

Example 78 with Connect

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

the class LibvirtComputingResourceTest method testSecurityGroupRulesCmdFalse.

@Test
public void testSecurityGroupRulesCmdFalse() {
    final String guestIp = "127.0.0.1";
    final String guestIp6 = "2001:db8::cad:40ff:fefd:75c4";
    final String guestMac = "00:00:00:00";
    final String vmName = "Test";
    final Long vmId = 1l;
    final String signature = "signature";
    final Long seqNum = 1l;
    final IpPortAndProto[] ingressRuleSet = new IpPortAndProto[] { Mockito.mock(IpPortAndProto.class) };
    final IpPortAndProto[] egressRuleSet = new IpPortAndProto[] { Mockito.mock(IpPortAndProto.class) };
    final List<String> secIps = new Vector<String>();
    final List<String> cidrs = new Vector<String>();
    cidrs.add("0.0.0.0/0");
    final SecurityGroupRulesCmd command = new SecurityGroupRulesCmd(guestIp, guestIp6, guestMac, vmName, vmId, signature, seqNum, ingressRuleSet, egressRuleSet, secIps);
    final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class);
    final Connect conn = Mockito.mock(Connect.class);
    final List<InterfaceDef> nics = new ArrayList<InterfaceDef>();
    final InterfaceDef interfaceDef = Mockito.mock(InterfaceDef.class);
    nics.add(interfaceDef);
    when(libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
    when(libvirtComputingResource.getInterfaces(conn, command.getVmName())).thenReturn(nics);
    try {
        when(libvirtUtilitiesHelper.getConnectionByVmName(command.getVmName())).thenReturn(conn);
    } catch (final LibvirtException e) {
        fail(e.getMessage());
    }
    when(ingressRuleSet[0].getProto()).thenReturn("tcp");
    when(ingressRuleSet[0].getStartPort()).thenReturn(22);
    when(ingressRuleSet[0].getEndPort()).thenReturn(22);
    when(ingressRuleSet[0].getAllowedCidrs()).thenReturn(cidrs);
    when(egressRuleSet[0].getProto()).thenReturn("tcp");
    when(egressRuleSet[0].getStartPort()).thenReturn(22);
    when(egressRuleSet[0].getEndPort()).thenReturn(22);
    when(egressRuleSet[0].getAllowedCidrs()).thenReturn(cidrs);
    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());
    } catch (final LibvirtException e) {
        fail(e.getMessage());
    }
}
Also used : SecurityGroupRulesCmd(com.cloud.agent.api.SecurityGroupRulesCmd) LibvirtException(org.libvirt.LibvirtException) Connect(org.libvirt.Connect) ArrayList(java.util.ArrayList) IpPortAndProto(com.cloud.agent.api.SecurityGroupRulesCmd.IpPortAndProto) 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) LibvirtRequestWrapper(com.cloud.hypervisor.kvm.resource.wrapper.LibvirtRequestWrapper) Vector(java.util.Vector) Test(org.junit.Test)

Example 79 with Connect

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

the class LibvirtComputingResourceTest method testRebootCommand.

@Test
public void testRebootCommand() {
    final Connect conn = Mockito.mock(Connect.class);
    final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class);
    final String vmName = "Test";
    final RebootCommand command = new RebootCommand(vmName, true);
    when(libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
    try {
        when(libvirtUtilitiesHelper.getConnectionByVmName(vmName)).thenReturn(conn);
    } catch (final LibvirtException 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(vmName);
    } catch (final LibvirtException e) {
        fail(e.getMessage());
    }
}
Also used : 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) RebootCommand(com.cloud.agent.api.RebootCommand) LibvirtException(org.libvirt.LibvirtException) Connect(org.libvirt.Connect) LibvirtUtilitiesHelper(com.cloud.hypervisor.kvm.resource.wrapper.LibvirtUtilitiesHelper) Test(org.junit.Test)

Example 80 with Connect

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

the class LibvirtComputingResource method prepareNetworkElementCommand.

protected ExecutionResult prepareNetworkElementCommand(final IpAssocVpcCommand cmd) {
    Connect conn;
    final String routerName = cmd.getAccessDetail(NetworkElementCommand.ROUTER_NAME);
    try {
        conn = LibvirtConnection.getConnectionByVmName(routerName);
        final IpAddressTO[] ips = cmd.getIpAddresses();
        Integer devNum = 0;
        final Map<String, Integer> broadcastUriToNicNum = new HashMap<String, Integer>();
        final List<InterfaceDef> pluggedNics = getInterfaces(conn, routerName);
        for (final InterfaceDef pluggedNic : pluggedNics) {
            final String pluggedVlan = pluggedNic.getBrName();
            if (pluggedVlan.equalsIgnoreCase(_linkLocalBridgeName)) {
                broadcastUriToNicNum.put("LinkLocal", devNum);
            } else if (pluggedVlan.equalsIgnoreCase(_publicBridgeName) || pluggedVlan.equalsIgnoreCase(_privBridgeName) || pluggedVlan.equalsIgnoreCase(_guestBridgeName)) {
                broadcastUriToNicNum.put(BroadcastDomainType.Vlan.toUri(Vlan.UNTAGGED).toString(), devNum);
            } else {
                broadcastUriToNicNum.put(getBroadcastUriFromBridge(pluggedVlan), devNum);
            }
            devNum++;
        }
        for (final IpAddressTO ip : ips) {
            ip.setNicDevId(broadcastUriToNicNum.get(ip.getBroadcastUri()));
        }
        return new ExecutionResult(true, null);
    } catch (final LibvirtException e) {
        s_logger.error("Ip Assoc failure on applying one ip due to exception:  ", e);
        return new ExecutionResult(false, e.getMessage());
    }
}
Also used : InterfaceDef(com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef) IpAddressTO(com.cloud.agent.api.to.IpAddressTO) LibvirtException(org.libvirt.LibvirtException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Connect(org.libvirt.Connect) ExecutionResult(com.cloud.utils.ExecutionResult)

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