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 org.xml.sax.SAXException;
import com.google.common.base.Function; import com.google.common.base.Function;
import com.google.common.base.Throwables;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.jamesmurty.utils.XMLBuilder; import com.jamesmurty.utils.XMLBuilder;
@ -69,7 +70,6 @@ public class DomainToHardware implements Function<Domain, Hardware> {
builder.processors(processors); builder.processors(processors);
builder.ram((int) from.getInfo().maxMem); builder.ram((int) from.getInfo().maxMem);
// TODO volumes
List<Volume> volumes = Lists.newArrayList(); List<Volume> volumes = Lists.newArrayList();
XMLBuilder xmlBuilder = XMLBuilder.parse(new InputSource(new StringReader(from.getXMLDesc(0)))); XMLBuilder xmlBuilder = XMLBuilder.parse(new InputSource(new StringReader(from.getXMLDesc(0))));
Document doc = xmlBuilder.getDocument(); Document doc = xmlBuilder.getDocument();
@ -85,22 +85,22 @@ public class DomainToHardware implements Function<Domain, Hardware> {
} }
builder.volumes((List<Volume>) volumes); builder.volumes((List<Volume>) volumes);
} catch (LibvirtException e) { } catch (LibvirtException e) {
// TODO Auto-generated catch block propagate(e);
e.printStackTrace();
} catch (XPathExpressionException e) { } catch (XPathExpressionException e) {
// TODO Auto-generated catch block propagate(e);
e.printStackTrace();
} catch (ParserConfigurationException e) { } catch (ParserConfigurationException e) {
// TODO Auto-generated catch block propagate(e);
e.printStackTrace();
} catch (SAXException e) { } catch (SAXException e) {
// TODO Auto-generated catch block propagate(e);
e.printStackTrace();
} catch (IOException e) { } catch (IOException e) {
// TODO Auto-generated catch block propagate(e);
e.printStackTrace();
} }
return builder.build(); 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.List;
import java.util.Map; import java.util.Map;
import java.util.Properties; import java.util.Properties;
import java.util.Random;
import java.util.UUID; import java.util.UUID;
import javax.inject.Inject; import javax.inject.Inject;
@ -69,221 +70,216 @@ import com.jamesmurty.utils.XMLBuilder;
@Singleton @Singleton
public class LibvirtComputeServiceAdapter implements ComputeServiceAdapter<Domain, Domain, Image, Datacenter> { public class LibvirtComputeServiceAdapter implements ComputeServiceAdapter<Domain, Domain, Image, Datacenter> {
private final Connect client; private final Connect client;
@Inject @Inject
public LibvirtComputeServiceAdapter(Connect client) { public LibvirtComputeServiceAdapter(Connect client) {
this.client = checkNotNull(client, "client"); this.client = checkNotNull(client, "client");
} }
@Override @Override
public Domain runNodeWithTagAndNameAndStoreCredentials(String tag, String name, Template template, public Domain runNodeWithTagAndNameAndStoreCredentials(String tag, String name, Template template,
Map<String, Credentials> credentialStore) { 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));
//String[] domains; try {
try { Domain domain = null;
//domains = client.listDefinedDomains(); String domainName = tag;
String xmlDesc = ""; domain = client.domainLookupByName(domainName);
Domain domain = null; XMLBuilder builder = XMLBuilder.parse(new InputSource(new StringReader(domain.getXMLDesc(0))));
//for (String domainName : domains) { Document doc = builder.getDocument();
// domain = client.domainLookupByName(domainName); XPathExpression expr = null;
// if (domainName.equals(tag)) { NodeList nodes = null;
String domainName = tag; String xpathString = "//devices/disk[@device='disk']/source/@file"; // +
domain = client.domainLookupByName(domainName); expr = XPathFactory.newInstance().newXPath().compile(xpathString);
System.out.println("domain name " + domain.getName()); nodes = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
XMLBuilder builder = XMLBuilder.parse(new InputSource(new StringReader(domain.getXMLDesc(0)))); String diskFileName = nodes.item(0).getNodeValue();
Document doc = builder.getDocument(); StorageVol storageVol = client.storageVolLookupByPath(diskFileName);
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 // cloning volume
String poolName = "default"; String poolName = "default";
StoragePool storagePool = client.storagePoolLookupByName(poolName); StoragePool storagePool = client.storagePoolLookupByName(poolName);
StorageVol clonedVol = cloneVolume(storagePool, storageVol); StorageVol clonedVol = cloneVolume(storagePool, storageVol);
System.out.println(clonedVol.getXMLDesc(0)); // define Domain
// define Domain String xmlFinal = generateClonedDomainXML(domain.getXMLDesc(0));
String xmlFinal = generateClonedDomainXML(domain.getXMLDesc(0)); domain = client.domainDefineXML(xmlFinal);
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 return domain;
credentialStore.put(domain.getUUIDString() + "", new Credentials("identity", "credential")); } catch (LibvirtException e) {
return propogate(e);
} catch (Exception e) {
return propogate(e);
}
}
//} @Override
//} public Iterable<Domain> listHardwareProfiles() {
return domain; return listNodes();
} catch (LibvirtException e) { }
return propogate(e);
} catch (Exception e) {
return propogate(e);
}
}
@Override @Override
public Iterable<Domain> listHardwareProfiles() { public Iterable<Image> listImages() {
return listNodes(); 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 @Override
public Iterable<Image> listImages() { public Iterable<Domain> listNodes() {
// return ImmutableSet.of(); try {
// TODO List<Domain> domains = Lists.newArrayList();
// return client.listImages(); for (String domain : client.listDefinedDomains()) {
int i = 1; domains.add(client.domainLookupByName(domain));
try { }
String[] domains = client.listDefinedDomains(); return domains;
List<Image> images = Lists.newArrayList(); } catch (LibvirtException e) {
for (String domainName : domains) { return propogate(e);
images.add(new Image(i++, domainName)); }
} }
return images;
} catch (Exception e) {
return propogate(e);
}
}
@Override @Override
public Iterable<Domain> listNodes() { public Iterable<Datacenter> listLocations() {
try { return ImmutableSet.of(new Datacenter(1, "SFO"));
List<Domain> domains = Lists.newArrayList(); }
for (String domain : client.listDefinedDomains()) {
domains.add(client.domainLookupByName(domain));
}
return domains;
} catch (LibvirtException e) {
return propogate(e);
}
}
protected <T> T propogate(LibvirtException e) { @Override
Throwables.propagate(e); public Domain getNode(String id) {
assert false; try {
return null; return client.domainLookupByUUIDString(id);
} } catch (LibvirtException e) {
return propogate(e);
}
}
protected <T> T propogate(Exception e) { @Override
Throwables.propagate(e); public void destroyNode(String id) {
assert false; try {
return null; client.domainLookupByUUIDString(id).finalize();
}
@Override /* TODO
public Iterable<Datacenter> listLocations() { XMLBuilder builder = XMLBuilder.parse(new InputSource(new StringReader(
return ImmutableSet.of(new Datacenter(1, "SFO")); 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 @Override
public Domain getNode(String id) { public void rebootNode(String id) {
try { try {
return client.domainLookupByUUIDString(id); client.domainLookupByUUIDString(id).reboot(0);
} catch (LibvirtException e) { } catch (LibvirtException e) {
return propogate(e); propogate(e);
} }
} }
@Override protected <T> T propogate(LibvirtException e) {
public void destroyNode(String id) { Throwables.propagate(e);
try { assert false;
client.domainLookupByUUIDString(id).destroy(); return null;
} catch (LibvirtException e) { }
propogate(e);
}
}
@Override protected <T> T propogate(Exception e) {
public void rebootNode(String id) { Throwables.propagate(e);
try { assert false;
client.domainLookupByUUIDString(id).reboot(0); return null;
} catch (LibvirtException e) { }
propogate(e);
} 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, private static String generateClonedVolumeXML(String fromXML) throws ParserConfigurationException, SAXException,
XPathExpressionException, ParserConfigurationException, SAXException, IOException, TransformerException { IOException, XPathExpressionException, 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, Properties outputProperties = new Properties();
IOException, XPathExpressionException, TransformerException { // 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(); XMLBuilder builder = XMLBuilder.parse(new InputSource(new StringReader(fromXML)));
// 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))); 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"; return builder.asString(outputProperties);
builder.xpathFind("//volume/name").t(cloneAppend); }
builder.xpathFind("//volume/key").t(cloneAppend);
builder.xpathFind("//volume/target/path").t(cloneAppend);
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, Properties outputProperties = new Properties();
IOException, XPathExpressionException, TransformerException { // 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(); XMLBuilder builder = XMLBuilder.parse(new InputSource(new StringReader(fromXML)));
// 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))); 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); builder.xpathFind("//domain/devices/interface/mac").a("address", "52:54:00:5c:dd:eb");
// change uuid domain return builder.asString(outputProperties);
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/disk/source").a("file", "/var/lib/libvirt/images/ttylinux.img-clone"); @Override
// TODO generate valid MAC address public void resumeNode(String id) {
builder.xpathFind("//domain/devices/interface/mac").a("address", "52:54:00:5c:dd:eb"); try {
return builder.asString(outputProperties); client.domainLookupByUUIDString(id).resume();
} } catch (LibvirtException e) {
propogate(e);
}
}
@Override @Override
public void resumeNode(String id) { public void suspendNode(String id) {
try { try {
client.domainLookupByUUIDString(id).resume(); client.domainLookupByUUIDString(id).suspend();
} catch (LibvirtException e) { } catch (LibvirtException e) {
propogate(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 static com.google.common.base.Preconditions.checkNotNull;
import java.util.Set;
import org.jclouds.compute.ComputeServiceContext; import org.jclouds.compute.ComputeServiceContext;
import org.jclouds.compute.ComputeServiceContextFactory; import org.jclouds.compute.ComputeServiceContextFactory;
import org.jclouds.compute.RunNodesException; import org.jclouds.compute.RunNodesException;
import org.jclouds.compute.StandaloneComputeServiceContextSpec; 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.Datacenter;
import org.jclouds.libvirt.Image; import org.jclouds.libvirt.Image;
import org.jclouds.libvirt.compute.domain.LibvirtComputeServiceContextModule; import org.jclouds.libvirt.compute.domain.LibvirtComputeServiceContextModule;
@ -64,7 +66,8 @@ public class LibvirtExperimentLiveTest {
.createContext(new StandaloneComputeServiceContextSpec<Domain, Domain, Image, Datacenter>("libvirt", .createContext(new StandaloneComputeServiceContextSpec<Domain, Domain, Image, Datacenter>("libvirt",
endpoint, apiversion, identity, credential, new LibvirtComputeServiceContextModule(), ImmutableSet endpoint, apiversion, identity, credential, new LibvirtComputeServiceContextModule(), ImmutableSet
.<Module> of())); .<Module> of()));
/*
/* /*
System.out.println("images " + context.getComputeService().listImages()); 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 * You can control the default template via overriding a method in standalonecomputeservicexontextmodule
*/ */
context.getComputeService().runNodesWithTag("ttylinux", 1/*, defaultTemplate*/); Set<? extends NodeMetadata> nodeMetadataSet = context.getComputeService().runNodesWithTag("ttylinux", 1);
} catch (RunNodesException e) { 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 // TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
} finally { } finally {