first version of runNodeWithTag

This commit is contained in:
andreaturli 2010-10-29 17:12:13 +02:00
parent 5711495cb0
commit dd0778a949
5 changed files with 305 additions and 146 deletions

View File

@ -51,6 +51,11 @@
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.jamesmurty.utils</groupId>
<artifactId>java-xmlbuilder</artifactId>
<version>0.3</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId> <groupId>${project.groupId}</groupId>
<artifactId>jclouds-core</artifactId> <artifactId>jclouds-core</artifactId>
<version>${project.version}</version> <version>${project.version}</version>

View File

@ -19,19 +19,35 @@
package org.jclouds.libvirt.compute.functions; package org.jclouds.libvirt.compute.functions;
import java.io.IOException;
import java.io.StringReader;
import java.util.List; import java.util.List;
import javax.inject.Singleton; import javax.inject.Singleton;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.jclouds.compute.domain.Hardware; import org.jclouds.compute.domain.Hardware;
import org.jclouds.compute.domain.HardwareBuilder; import org.jclouds.compute.domain.HardwareBuilder;
import org.jclouds.compute.domain.Processor; import org.jclouds.compute.domain.Processor;
import org.jclouds.compute.domain.Volume;
import org.jclouds.compute.domain.internal.VolumeImpl;
import org.libvirt.Domain; import org.libvirt.Domain;
import org.libvirt.LibvirtException; import org.libvirt.LibvirtException;
import org.libvirt.StorageVol;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import com.google.common.base.Function; import com.google.common.base.Function;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.jamesmurty.utils.XMLBuilder;
/** /**
* @author Adrian Cole * @author Adrian Cole
*/ */
@ -41,9 +57,9 @@ public class DomainToHardware implements Function<Domain, Hardware> {
@Override @Override
public Hardware apply(Domain from) { public Hardware apply(Domain from) {
HardwareBuilder builder = new HardwareBuilder(); HardwareBuilder builder = new HardwareBuilder();
try { try {
builder.id(from.getUUIDString()); builder.id(from.getUUIDString());
builder.providerId(from.getID() + ""); builder.providerId(from.getID() + "");
builder.name(from.getName()); builder.name(from.getName());
List<Processor> processors = Lists.newArrayList(); List<Processor> processors = Lists.newArrayList();
@ -54,9 +70,34 @@ public class DomainToHardware implements Function<Domain, Hardware> {
builder.ram((int) from.getInfo().maxMem); builder.ram((int) from.getInfo().maxMem);
// TODO volumes // TODO volumes
List<Volume> volumes = Lists.newArrayList();
XMLBuilder xmlBuilder = XMLBuilder.parse(new InputSource(new StringReader(from.getXMLDesc(0))));
Document doc = xmlBuilder.getDocument();
XPathExpression expr = XPathFactory.newInstance().newXPath().compile("//devices/disk[@device='disk']/source/@file");
NodeList nodes = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
String diskFileName = nodes.item(0).getNodeValue();
for (int i = 0; i < nodes.getLength(); i++) {
StorageVol storageVol = from.getConnect().storageVolLookupByPath(diskFileName);
String id = storageVol.getKey();
float size = new Long(storageVol.getInfo().capacity).floatValue();
volumes.add(new VolumeImpl(id, Volume.Type.LOCAL, size, null, true, false));
}
builder.volumes((List<Volume>) volumes);
} catch (LibvirtException e) { } catch (LibvirtException e) {
// TODO Auto-generated catch block // TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
} catch (XPathExpressionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }
return builder.build(); return builder.build();
} }

View File

@ -52,7 +52,7 @@ public class LibvirtImageToImage implements Function<org.jclouds.libvirt.Image,
OsFamily family = null; OsFamily family = null;
try { try {
family = OsFamily.fromValue(from.name); family = OsFamily.fromValue(from.name);
builder.operatingSystem(new OperatingSystemBuilder().name(from.name).family(family).build()); builder.operatingSystem(new OperatingSystemBuilder().name(from.name).family(family).description("ubuntu").build());
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
logger.debug("<< didn't match os(%s)", from); logger.debug("<< didn't match os(%s)", from);
} }

View File

@ -2,11 +2,21 @@ package org.jclouds.libvirt.compute.strategy;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
import java.io.IOException;
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.UUID;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Singleton; import javax.inject.Singleton;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.jclouds.compute.ComputeService; import org.jclouds.compute.ComputeService;
import org.jclouds.compute.ComputeServiceAdapter; import org.jclouds.compute.ComputeServiceAdapter;
@ -17,11 +27,20 @@ import org.jclouds.libvirt.Image;
import org.libvirt.Connect; import org.libvirt.Connect;
import org.libvirt.Domain; import org.libvirt.Domain;
import org.libvirt.LibvirtException; import org.libvirt.LibvirtException;
import org.libvirt.StoragePool;
import org.libvirt.StorageVol;
import org.libvirt.jna.Libvirt; import org.libvirt.jna.Libvirt;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import com.google.common.base.Throwables; import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.jamesmurty.utils.XMLBuilder;
/** /**
* defines the connection between the {@link Libvirt} implementation and the jclouds * defines the connection between the {@link Libvirt} implementation and the jclouds
@ -47,29 +66,54 @@ public class LibvirtComputeServiceAdapter implements ComputeServiceAdapter<Domai
// store the credentials so that later functions can use them // store the credentials so that later functions can use them
// credentialStore.put(from.id + "", new Credentials(from.loginUser, from.password)); // credentialStore.put(from.id + "", new Credentials(from.loginUser, from.password));
String xmlDesc ="<domain type='kvm'>" + "<name>test</name>" + "<uuid>abcf2039-a9f1-a659-7f91-e0f82f59d52e</uuid>" + String[] domains;
"<memory>524288</memory>" + try {
"<currentMemory>524288</currentMemory>" + domains = client.listDefinedDomains();
"<vcpu>1</vcpu>" +
"<os><type arch='i686' machine='pc-0.12'>hvm</type><boot dev='hd'/></os>" +
"<features><acpi/> <apic/> <pae/> </features>" +
"<clock offset='utc'/>" +
"<on_poweroff>destroy</on_poweroff>"+
"<on_reboot>restart</on_reboot>"+
"<on_crash>restart</on_crash>"+
"<devices><emulator>/usr/bin/kvm</emulator><disk type='file' device='disk'><driver name='qemu' type='raw'/><source file='/var/lib/libvirt/images/test.img'/> <target dev='vda' bus='virtio'/> </disk> <disk type='block' device='cdrom'> <driver name='qemu' type='raw'/> <target dev='hdc' bus='ide'/><readonly/></disk> <interface type='network'> <mac address='52:54:00:05:cf:92'/> <source network='default'/> <model type='virtio'/> </interface> <console type='pty'> <target port='0'/> </console> <console type='pty'> <target port='0'/> </console> <input type='mouse' bus='ps2'/> <graphics type='vnc' port='-1' autoport='yes'/> <video> <model type='cirrus' vram='9216' heads='1'/> </video> </devices>"+
"</domain>";
Domain domain = null; Domain domain = null;
try { String xmlDesc = "";
client.domainDefineXML(xmlDesc); for (String domainName : domains) {
domain = client.domainCreateXML(xmlDesc, 1); domain = client.domainLookupByName(domainName);
} catch (LibvirtException e) { if(domainName.equals("ttylinux")) {
// TODO Auto-generated catch block domain = client.domainLookupByName(domainName);
e.printStackTrace(); xmlDesc = domain.getXMLDesc(0);
System.out.println("domain: " + domain.getUUIDString());
XMLBuilder builder = XMLBuilder.parse(new InputSource(
new StringReader(xmlDesc)));
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();
System.out.println("\n *** diskFileName " + diskFileName);
StorageVol storageVol = client.storageVolLookupByPath(diskFileName);
System.out.println(storageVol.getXMLDesc(0));
// cloning volume
String poolName = "default";
StoragePool storagePool = client.storagePoolLookupByName(poolName );
StorageVol clonedVol = cloneVolume(storagePool, storageVol);
//System.out.println(generateClonedDomainXML(xmlDesc));
domain = client.domainDefineXML(generateClonedDomainXML(xmlDesc));
}
} }
return domain; return domain;
} catch (LibvirtException e) {
return propogate(e);
} catch (Exception e) {
return propogate(e);
} }
}
@Override @Override
public Iterable<Domain> listHardware() { public Iterable<Domain> listHardware() {
@ -78,30 +122,23 @@ public class LibvirtComputeServiceAdapter implements ComputeServiceAdapter<Domai
@Override @Override
public Iterable<Image> listImages() { public Iterable<Image> listImages() {
return ImmutableSet.of(); //return ImmutableSet.of();
// TODO // TODO
// return client.listImages(); // return client.listImages();
}
@Override List<Image> images = Lists.newArrayList();
public Iterable<Domain> listNodes() { images.add(new Image(1, "ubuntu"));
try { return images;
List<Domain> domains = Lists.newArrayList();
for (int domain : client.listDomains()) {
domains.add(client.domainLookupByID(domain));
}
return domains;
} catch (LibvirtException e) {
return propogate(e);
}
} }
// @Override // @Override
// public Iterable<Domain> listNodes() { // public Iterable<Domain> listNodes() {
// try { // try {
// List<Domain> domains = Lists.newArrayList(); // List<Domain> domains = Lists.newArrayList();
// for (String domain : client.listDefinedDomains()) { // for (int domain : client.listDomains()) {
// domains.add(client.domainLookupByName(domain)); // domains.add(client.domainLookupByID(domain));
// } // }
// return domains; // return domains;
// } catch (LibvirtException e) { // } catch (LibvirtException e) {
@ -109,12 +146,31 @@ public class LibvirtComputeServiceAdapter implements ComputeServiceAdapter<Domai
// } // }
// } // }
@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);
}
}
protected <T> T propogate(LibvirtException e) { protected <T> T propogate(LibvirtException e) {
Throwables.propagate(e); Throwables.propagate(e);
assert false; assert false;
return null; return null;
} }
protected <T> T propogate(Exception e) {
Throwables.propagate(e);
assert false;
return null;
}
@Override @Override
public Iterable<Datacenter> listLocations() { public Iterable<Datacenter> listLocations() {
return ImmutableSet.of(new Datacenter(1, "SFO")); return ImmutableSet.of(new Datacenter(1, "SFO"));
@ -156,4 +212,61 @@ public class LibvirtComputeServiceAdapter implements ComputeServiceAdapter<Domai
} }
private static StorageVol cloneVolume(StoragePool storagePool, StorageVol from)
throws LibvirtException, XPathExpressionException, ParserConfigurationException, SAXException, IOException, TransformerException {
String fromXML = from.getXMLDesc(0);
String clonedXML = generateClonedVolumeXML(fromXML);
System.out.println(clonedXML);
//return null;
return storagePool.storageVolCreateXMLFrom(clonedXML, from, 0);
}
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");
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);
return builder.asString(outputProperties);
}
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");
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);
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);
}
} }

View File

@ -68,10 +68,11 @@ public class LibvirtExperimentLiveTest {
.<Module> of())); .<Module> of()));
System.out.println("images " + context.getComputeService().listImages()); System.out.println("images " + context.getComputeService().listImages());
System.out.println("nodes " + context.getComputeService().listNodes());
System.out.println("hardware profiles " + context.getComputeService().listHardwareProfiles()); System.out.println("hardware profiles " + context.getComputeService().listHardwareProfiles());
Template defaultTemplate = context.getComputeService().templateBuilder() Template defaultTemplate = context.getComputeService().templateBuilder()
//.hardwareId("").locationId("").imageId("") .hardwareId("c7ff2039-a9f1-a659-7f91-e0f82f59d52e").imageId("1") //.locationId("")
.build(); .build();
/* /*
@ -81,8 +82,7 @@ public class LibvirtExperimentLiveTest {
// context.getComputeService().templateOptions().; // context.getComputeService().templateOptions().;
context.getComputeService().runNodesWithTag("test", 1); context.getComputeService().runNodesWithTag("ttylinux", 1, defaultTemplate);
System.out.println(context.getComputeService().listNodes());
} catch (RunNodesException e) { } catch (RunNodesException e) {
e.printStackTrace(); e.printStackTrace();