cosmetic fixes

This commit is contained in:
andreaturli 2010-11-02 18:35:09 +01:00
parent 3e30910b86
commit b0a9263486
3 changed files with 211 additions and 205 deletions

View File

@ -44,6 +44,7 @@ import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import com.google.common.base.Function;
import com.google.common.base.Throwables;
import com.google.common.collect.Lists;
import com.jamesmurty.utils.XMLBuilder;
@ -69,7 +70,6 @@ public class DomainToHardware implements Function<Domain, Hardware> {
builder.processors(processors);
builder.ram((int) from.getInfo().maxMem);
// TODO volumes
List<Volume> volumes = Lists.newArrayList();
XMLBuilder xmlBuilder = XMLBuilder.parse(new InputSource(new StringReader(from.getXMLDesc(0))));
Document doc = xmlBuilder.getDocument();
@ -85,22 +85,22 @@ public class DomainToHardware implements Function<Domain, Hardware> {
}
builder.volumes((List<Volume>) volumes);
} catch (LibvirtException e) {
// TODO Auto-generated catch block
e.printStackTrace();
propagate(e);
} catch (XPathExpressionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
propagate(e);
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
propagate(e);
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
propagate(e);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
propagate(e);
}
return builder.build();
}
}
protected <T> T propagate(Exception e) {
Throwables.propagate(e);
assert false;
return null;
}
}

View File

@ -26,6 +26,7 @@ import java.io.StringReader;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Random;
import java.util.UUID;
import javax.inject.Inject;
@ -69,221 +70,216 @@ import com.jamesmurty.utils.XMLBuilder;
@Singleton
public class LibvirtComputeServiceAdapter implements ComputeServiceAdapter<Domain, Domain, Image, Datacenter> {
private final Connect client;
private final Connect client;
@Inject
public LibvirtComputeServiceAdapter(Connect client) {
this.client = checkNotNull(client, "client");
}
@Inject
public LibvirtComputeServiceAdapter(Connect client) {
this.client = checkNotNull(client, "client");
}
@Override
public Domain runNodeWithTagAndNameAndStoreCredentials(String tag, String name, Template template,
Map<String, Credentials> credentialStore) {
// create the backend object using parameters from the template.
// Domain from = client.createDomainInDC(template.getLocation().getId(), name,
// Integer.parseInt(template.getImage().getProviderId()),
// Integer.parseInt(template.getHardware().getProviderId()));
// store the credentials so that later functions can use them
// credentialStore.put(from.id + "", new Credentials(from.loginUser, from.password));
@Override
public Domain runNodeWithTagAndNameAndStoreCredentials(String tag, String name, Template template,
Map<String, Credentials> credentialStore) {
//String[] domains;
try {
//domains = client.listDefinedDomains();
String xmlDesc = "";
Domain domain = null;
//for (String domainName : domains) {
// domain = client.domainLookupByName(domainName);
// if (domainName.equals(tag)) {
String domainName = tag;
domain = client.domainLookupByName(domainName);
System.out.println("domain name " + domain.getName());
XMLBuilder builder = XMLBuilder.parse(new InputSource(new StringReader(domain.getXMLDesc(0))));
Document doc = builder.getDocument();
XPathExpression expr = null;
NodeList nodes = null;
String xpathString = "//devices/disk[@device='disk']/source/@file"; // +
expr = XPathFactory.newInstance().newXPath().compile(xpathString);
nodes = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
String diskFileName = nodes.item(0).getNodeValue();
StorageVol storageVol = client.storageVolLookupByPath(diskFileName);
try {
Domain domain = null;
String domainName = tag;
domain = client.domainLookupByName(domainName);
XMLBuilder builder = XMLBuilder.parse(new InputSource(new StringReader(domain.getXMLDesc(0))));
Document doc = builder.getDocument();
XPathExpression expr = null;
NodeList nodes = null;
String xpathString = "//devices/disk[@device='disk']/source/@file"; // +
expr = XPathFactory.newInstance().newXPath().compile(xpathString);
nodes = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
String diskFileName = nodes.item(0).getNodeValue();
StorageVol storageVol = client.storageVolLookupByPath(diskFileName);
// cloning volume
String poolName = "default";
StoragePool storagePool = client.storagePoolLookupByName(poolName);
StorageVol clonedVol = cloneVolume(storagePool, storageVol);
// cloning volume
String poolName = "default";
StoragePool storagePool = client.storagePoolLookupByName(poolName);
StorageVol clonedVol = cloneVolume(storagePool, storageVol);
System.out.println(clonedVol.getXMLDesc(0));
// define Domain
String xmlFinal = generateClonedDomainXML(domain.getXMLDesc(0));
domain = client.domainDefineXML(xmlFinal);
// define Domain
String xmlFinal = generateClonedDomainXML(domain.getXMLDesc(0));
domain = client.domainDefineXML(xmlFinal);
domain.create();
domain.create();
// store the credentials so that later functions can use them
credentialStore.put(domain.getUUIDString() + "", new Credentials("identity", "credential"));
// store the credentials so that later functions can use them
credentialStore.put(domain.getUUIDString() + "", new Credentials("identity", "credential"));
return domain;
} catch (LibvirtException e) {
return propogate(e);
} catch (Exception e) {
return propogate(e);
}
}
//}
//}
return domain;
} catch (LibvirtException e) {
return propogate(e);
} catch (Exception e) {
return propogate(e);
}
}
@Override
public Iterable<Domain> listHardwareProfiles() {
return listNodes();
}
@Override
public Iterable<Domain> listHardwareProfiles() {
return listNodes();
}
@Override
public Iterable<Image> listImages() {
int i = 1;
try {
String[] domains = client.listDefinedDomains();
List<Image> images = Lists.newArrayList();
for (String domainName : domains) {
images.add(new Image(i++, domainName));
}
return images;
} catch (Exception e) {
return propogate(e);
}
}
@Override
public Iterable<Image> listImages() {
// return ImmutableSet.of();
// TODO
// return client.listImages();
int i = 1;
try {
String[] domains = client.listDefinedDomains();
List<Image> images = Lists.newArrayList();
for (String domainName : domains) {
images.add(new Image(i++, domainName));
}
return images;
} catch (Exception e) {
return propogate(e);
}
}
@Override
public Iterable<Domain> listNodes() {
try {
List<Domain> domains = Lists.newArrayList();
for (String domain : client.listDefinedDomains()) {
domains.add(client.domainLookupByName(domain));
}
return domains;
} catch (LibvirtException e) {
return propogate(e);
}
}
@Override
public Iterable<Domain> listNodes() {
try {
List<Domain> domains = Lists.newArrayList();
for (String domain : client.listDefinedDomains()) {
domains.add(client.domainLookupByName(domain));
}
return domains;
} catch (LibvirtException e) {
return propogate(e);
}
}
@Override
public Iterable<Datacenter> listLocations() {
return ImmutableSet.of(new Datacenter(1, "SFO"));
}
protected <T> T propogate(LibvirtException e) {
Throwables.propagate(e);
assert false;
return null;
}
@Override
public Domain getNode(String id) {
try {
return client.domainLookupByUUIDString(id);
} catch (LibvirtException e) {
return propogate(e);
}
}
protected <T> T propogate(Exception e) {
Throwables.propagate(e);
assert false;
return null;
}
@Override
public void destroyNode(String id) {
try {
client.domainLookupByUUIDString(id).finalize();
@Override
public Iterable<Datacenter> listLocations() {
return ImmutableSet.of(new Datacenter(1, "SFO"));
}
/* TODO
XMLBuilder builder = XMLBuilder.parse(new InputSource(new StringReader(
client.domainLookupByUUIDString(id).getXMLDesc(0)
)));
String diskFileName = builder.xpathFind("//devices/disk[@device='disk']/source").getElement().getAttribute("file");
System.out.println(" :P " +diskFileName);
StorageVol storageVol = client.storageVolLookupByPath(diskFileName);
storageVol.delete(0);
client.domainLookupByUUIDString(id).undefine();
*/
} catch (LibvirtException e) {
propogate(e);
} catch (Exception e) {
propogate(e);
}
}
@Override
public Domain getNode(String id) {
try {
return client.domainLookupByUUIDString(id);
} catch (LibvirtException e) {
return propogate(e);
}
}
@Override
public void rebootNode(String id) {
try {
client.domainLookupByUUIDString(id).reboot(0);
} catch (LibvirtException e) {
propogate(e);
}
}
@Override
public void destroyNode(String id) {
try {
client.domainLookupByUUIDString(id).destroy();
} catch (LibvirtException e) {
propogate(e);
}
}
protected <T> T propogate(LibvirtException e) {
Throwables.propagate(e);
assert false;
return null;
}
@Override
public void rebootNode(String id) {
try {
client.domainLookupByUUIDString(id).reboot(0);
} catch (LibvirtException e) {
propogate(e);
}
}
protected <T> T propogate(Exception e) {
Throwables.propagate(e);
assert false;
return null;
}
private static StorageVol cloneVolume(StoragePool storagePool, StorageVol from) throws LibvirtException,
XPathExpressionException, ParserConfigurationException, SAXException, IOException, TransformerException {
String fromXML = from.getXMLDesc(0);
String clonedXML = generateClonedVolumeXML(fromXML);
return storagePool.storageVolCreateXMLFrom(clonedXML, from, 0);
}
private static StorageVol cloneVolume(StoragePool storagePool, StorageVol from) throws LibvirtException,
XPathExpressionException, ParserConfigurationException, SAXException, IOException, TransformerException {
String fromXML = from.getXMLDesc(0);
String clonedXML = generateClonedVolumeXML(fromXML);
return storagePool.storageVolCreateXMLFrom(clonedXML, from, 0);
}
private static String generateClonedVolumeXML(String fromXML) throws ParserConfigurationException, SAXException,
IOException, XPathExpressionException, TransformerException {
private static String generateClonedVolumeXML(String fromXML) throws ParserConfigurationException, SAXException,
IOException, XPathExpressionException, TransformerException {
Properties outputProperties = new Properties();
// Explicitly identify the output as an XML document
outputProperties.put(javax.xml.transform.OutputKeys.METHOD, "xml");
// Pretty-print the XML output (doesn't work in all cases)
outputProperties.put(javax.xml.transform.OutputKeys.INDENT, "yes");
// Get 2-space indenting when using the Apache transformer
outputProperties.put("{http://xml.apache.org/xslt}indent-amount", "2");
Properties outputProperties = new Properties();
// Explicitly identify the output as an XML document
outputProperties.put(javax.xml.transform.OutputKeys.METHOD, "xml");
// Pretty-print the XML output (doesn't work in all cases)
outputProperties.put(javax.xml.transform.OutputKeys.INDENT, "yes");
// Get 2-space indenting when using the Apache transformer
outputProperties.put("{http://xml.apache.org/xslt}indent-amount", "2");
XMLBuilder builder = XMLBuilder.parse(new InputSource(new StringReader(fromXML)));
XMLBuilder builder = XMLBuilder.parse(new InputSource(new StringReader(fromXML)));
String cloneAppend = "-clone";
builder.xpathFind("//volume/name").t(cloneAppend);
builder.xpathFind("//volume/key").t(cloneAppend);
builder.xpathFind("//volume/target/path").t(cloneAppend);
String cloneAppend = "-clone";
builder.xpathFind("//volume/name").t(cloneAppend);
builder.xpathFind("//volume/key").t(cloneAppend);
builder.xpathFind("//volume/target/path").t(cloneAppend);
return builder.asString(outputProperties);
}
return builder.asString(outputProperties);
}
private static String generateClonedDomainXML(String fromXML) throws ParserConfigurationException, SAXException,
IOException, XPathExpressionException, TransformerException {
private static String generateClonedDomainXML(String fromXML) throws ParserConfigurationException, SAXException,
IOException, XPathExpressionException, TransformerException {
Properties outputProperties = new Properties();
// Explicitly identify the output as an XML document
outputProperties.put(javax.xml.transform.OutputKeys.METHOD, "xml");
// Pretty-print the XML output (doesn't work in all cases)
outputProperties.put(javax.xml.transform.OutputKeys.INDENT, "yes");
// Get 2-space indenting when using the Apache transformer
outputProperties.put("{http://xml.apache.org/xslt}indent-amount", "2");
Properties outputProperties = new Properties();
// Explicitly identify the output as an XML document
outputProperties.put(javax.xml.transform.OutputKeys.METHOD, "xml");
// Pretty-print the XML output (doesn't work in all cases)
outputProperties.put(javax.xml.transform.OutputKeys.INDENT, "yes");
// Get 2-space indenting when using the Apache transformer
outputProperties.put("{http://xml.apache.org/xslt}indent-amount", "2");
XMLBuilder builder = XMLBuilder.parse(new InputSource(new StringReader(fromXML)));
XMLBuilder builder = XMLBuilder.parse(new InputSource(new StringReader(fromXML)));
String cloneAppend = "-clone";
builder.xpathFind("//domain/name").t(cloneAppend);
// change uuid domain
Element oldChild = builder.xpathFind("//domain/uuid").getElement();
Node newNode = oldChild.cloneNode(true);
newNode.getFirstChild().setNodeValue(UUID.randomUUID().toString());
builder.getDocument().getDocumentElement().replaceChild(newNode, oldChild);
String cloneAppend = "-clone";
String fromVolPath = builder.xpathFind("//domain/devices/disk/source").getElement().getAttribute("file");
builder.xpathFind("//domain/devices/disk/source").a("file", fromVolPath + cloneAppend);
// TODO generate valid MAC address
String fromMACaddress = builder.xpathFind("//domain/devices/interface/mac").getElement().getAttribute("address");
builder.xpathFind("//domain/name").t(cloneAppend);
// change uuid domain
Element oldChild = builder.xpathFind("//domain/uuid").getElement();
Node newNode = oldChild.cloneNode(true);
newNode.getFirstChild().setNodeValue(UUID.randomUUID().toString());
builder.getDocument().getDocumentElement().replaceChild(newNode, oldChild);
builder.xpathFind("//domain/devices/interface/mac").a("address", "52:54:00:5c:dd:eb");
return builder.asString(outputProperties);
}
builder.xpathFind("//domain/devices/disk/source").a("file", "/var/lib/libvirt/images/ttylinux.img-clone");
// TODO generate valid MAC address
builder.xpathFind("//domain/devices/interface/mac").a("address", "52:54:00:5c:dd:eb");
return builder.asString(outputProperties);
}
@Override
public void resumeNode(String id) {
try {
client.domainLookupByUUIDString(id).resume();
} catch (LibvirtException e) {
propogate(e);
}
}
@Override
public void resumeNode(String id) {
try {
client.domainLookupByUUIDString(id).resume();
} catch (LibvirtException e) {
propogate(e);
}
}
@Override
public void suspendNode(String id) {
try {
client.domainLookupByUUIDString(id).suspend();
} catch (LibvirtException e) {
propogate(e);
}
}
@Override
public void suspendNode(String id) {
try {
client.domainLookupByUUIDString(id).suspend();
} catch (LibvirtException e) {
propogate(e);
}
}
}

View File

@ -21,11 +21,13 @@ package org.jclouds.libvirt.compute;
import static com.google.common.base.Preconditions.checkNotNull;
import java.util.Set;
import org.jclouds.compute.ComputeServiceContext;
import org.jclouds.compute.ComputeServiceContextFactory;
import org.jclouds.compute.RunNodesException;
import org.jclouds.compute.StandaloneComputeServiceContextSpec;
import org.jclouds.compute.domain.Template;
import org.jclouds.compute.domain.NodeMetadata;
import org.jclouds.libvirt.Datacenter;
import org.jclouds.libvirt.Image;
import org.jclouds.libvirt.compute.domain.LibvirtComputeServiceContextModule;
@ -64,7 +66,8 @@ public class LibvirtExperimentLiveTest {
.createContext(new StandaloneComputeServiceContextSpec<Domain, Domain, Image, Datacenter>("libvirt",
endpoint, apiversion, identity, credential, new LibvirtComputeServiceContextModule(), ImmutableSet
.<Module> of()));
/*
/*
System.out.println("images " + context.getComputeService().listImages());
@ -83,9 +86,16 @@ public class LibvirtExperimentLiveTest {
* You can control the default template via overriding a method in standalonecomputeservicexontextmodule
*/
context.getComputeService().runNodesWithTag("ttylinux", 1/*, defaultTemplate*/);
} catch (RunNodesException e) {
Set<? extends NodeMetadata> nodeMetadataSet = context.getComputeService().runNodesWithTag("ttylinux", 1);
for (NodeMetadata nodeMetadata : nodeMetadataSet) {
context.getComputeService().suspendNode(nodeMetadata.getId());
Thread.sleep(2000);
context.getComputeService().resumeNode(nodeMetadata.getId());
// TODO seems that destroy is intended to be a force shutoff, not a delete VM ...
//context.getComputeService().destroyNode(nodeMetadata.getId());
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {