mirror of https://github.com/apache/jclouds.git
first version of runNodeWithTag
This commit is contained in:
parent
5711495cb0
commit
dd0778a949
|
@ -51,6 +51,11 @@
|
|||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.jamesmurty.utils</groupId>
|
||||
<artifactId>java-xmlbuilder</artifactId>
|
||||
<version>0.3</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>${project.groupId}</groupId>
|
||||
<artifactId>jclouds-core</artifactId>
|
||||
<version>${project.version}</version>
|
||||
|
|
|
@ -19,19 +19,35 @@
|
|||
|
||||
package org.jclouds.libvirt.compute.functions;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.StringReader;
|
||||
import java.util.List;
|
||||
|
||||
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.HardwareBuilder;
|
||||
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.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.collect.Lists;
|
||||
|
||||
import com.jamesmurty.utils.XMLBuilder;
|
||||
|
||||
/**
|
||||
* @author Adrian Cole
|
||||
*/
|
||||
|
@ -41,9 +57,9 @@ public class DomainToHardware implements Function<Domain, Hardware> {
|
|||
@Override
|
||||
public Hardware apply(Domain from) {
|
||||
HardwareBuilder builder = new HardwareBuilder();
|
||||
|
||||
try {
|
||||
builder.id(from.getUUIDString());
|
||||
|
||||
builder.providerId(from.getID() + "");
|
||||
builder.name(from.getName());
|
||||
List<Processor> processors = Lists.newArrayList();
|
||||
|
@ -54,9 +70,34 @@ public class DomainToHardware implements Function<Domain, Hardware> {
|
|||
|
||||
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();
|
||||
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) {
|
||||
// TODO Auto-generated catch block
|
||||
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();
|
||||
}
|
||||
|
|
|
@ -52,7 +52,7 @@ public class LibvirtImageToImage implements Function<org.jclouds.libvirt.Image,
|
|||
OsFamily family = null;
|
||||
try {
|
||||
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) {
|
||||
logger.debug("<< didn't match os(%s)", from);
|
||||
}
|
||||
|
|
|
@ -2,11 +2,21 @@ package org.jclouds.libvirt.compute.strategy;
|
|||
|
||||
import static com.google.common.base.Preconditions.checkNotNull;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.StringReader;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
import java.util.UUID;
|
||||
|
||||
import javax.inject.Inject;
|
||||
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.ComputeServiceAdapter;
|
||||
|
@ -17,11 +27,20 @@ import org.jclouds.libvirt.Image;
|
|||
import org.libvirt.Connect;
|
||||
import org.libvirt.Domain;
|
||||
import org.libvirt.LibvirtException;
|
||||
import org.libvirt.StoragePool;
|
||||
import org.libvirt.StorageVol;
|
||||
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.collect.ImmutableSet;
|
||||
import com.google.common.collect.Lists;
|
||||
import com.jamesmurty.utils.XMLBuilder;
|
||||
|
||||
/**
|
||||
* 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
|
||||
// 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>" +
|
||||
"<memory>524288</memory>" +
|
||||
"<currentMemory>524288</currentMemory>" +
|
||||
"<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>";
|
||||
String[] domains;
|
||||
try {
|
||||
domains = client.listDefinedDomains();
|
||||
|
||||
Domain domain = null;
|
||||
try {
|
||||
client.domainDefineXML(xmlDesc);
|
||||
domain = client.domainCreateXML(xmlDesc, 1);
|
||||
} catch (LibvirtException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
String xmlDesc = "";
|
||||
for (String domainName : domains) {
|
||||
domain = client.domainLookupByName(domainName);
|
||||
if(domainName.equals("ttylinux")) {
|
||||
domain = client.domainLookupByName(domainName);
|
||||
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;
|
||||
} catch (LibvirtException e) {
|
||||
return propogate(e);
|
||||
} catch (Exception e) {
|
||||
return propogate(e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Iterable<Domain> listHardware() {
|
||||
|
@ -78,30 +122,23 @@ public class LibvirtComputeServiceAdapter implements ComputeServiceAdapter<Domai
|
|||
|
||||
@Override
|
||||
public Iterable<Image> listImages() {
|
||||
return ImmutableSet.of();
|
||||
//return ImmutableSet.of();
|
||||
// TODO
|
||||
// return client.listImages();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterable<Domain> listNodes() {
|
||||
try {
|
||||
List<Domain> domains = Lists.newArrayList();
|
||||
for (int domain : client.listDomains()) {
|
||||
domains.add(client.domainLookupByID(domain));
|
||||
}
|
||||
return domains;
|
||||
} catch (LibvirtException e) {
|
||||
return propogate(e);
|
||||
}
|
||||
List<Image> images = Lists.newArrayList();
|
||||
images.add(new Image(1, "ubuntu"));
|
||||
return images;
|
||||
|
||||
|
||||
}
|
||||
|
||||
// @Override
|
||||
// public Iterable<Domain> listNodes() {
|
||||
// try {
|
||||
// List<Domain> domains = Lists.newArrayList();
|
||||
// for (String domain : client.listDefinedDomains()) {
|
||||
// domains.add(client.domainLookupByName(domain));
|
||||
// for (int domain : client.listDomains()) {
|
||||
// domains.add(client.domainLookupByID(domain));
|
||||
// }
|
||||
// return domains;
|
||||
// } 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) {
|
||||
Throwables.propagate(e);
|
||||
assert false;
|
||||
return null;
|
||||
}
|
||||
|
||||
protected <T> T propogate(Exception e) {
|
||||
Throwables.propagate(e);
|
||||
assert false;
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterable<Datacenter> listLocations() {
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -68,10 +68,11 @@ public class LibvirtExperimentLiveTest {
|
|||
.<Module> of()));
|
||||
|
||||
System.out.println("images " + context.getComputeService().listImages());
|
||||
System.out.println("nodes " + context.getComputeService().listNodes());
|
||||
System.out.println("hardware profiles " + context.getComputeService().listHardwareProfiles());
|
||||
|
||||
Template defaultTemplate = context.getComputeService().templateBuilder()
|
||||
//.hardwareId("").locationId("").imageId("")
|
||||
.hardwareId("c7ff2039-a9f1-a659-7f91-e0f82f59d52e").imageId("1") //.locationId("")
|
||||
.build();
|
||||
|
||||
/*
|
||||
|
@ -81,8 +82,7 @@ public class LibvirtExperimentLiveTest {
|
|||
|
||||
|
||||
// context.getComputeService().templateOptions().;
|
||||
context.getComputeService().runNodesWithTag("test", 1);
|
||||
System.out.println(context.getComputeService().listNodes());
|
||||
context.getComputeService().runNodesWithTag("ttylinux", 1, defaultTemplate);
|
||||
|
||||
} catch (RunNodesException e) {
|
||||
e.printStackTrace();
|
||||
|
|
Loading…
Reference in New Issue