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);
}
}
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());
}
}
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());
}
}
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());
}
}
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());
}
}
Aggregations