mirror of https://github.com/apache/jclouds.git
improvement on libvirt
This commit is contained in:
parent
a45532f05c
commit
85f0ef58fd
|
@ -29,6 +29,7 @@ import org.jclouds.compute.ComputeServiceAdapter;
|
|||
import org.jclouds.compute.config.StandaloneComputeServiceContextModule;
|
||||
import org.jclouds.compute.domain.Hardware;
|
||||
import org.jclouds.compute.domain.NodeMetadata;
|
||||
import org.jclouds.compute.domain.TemplateBuilder;
|
||||
import org.jclouds.compute.suppliers.DefaultLocationSupplier;
|
||||
import org.jclouds.domain.Location;
|
||||
import org.jclouds.libvirt.Datacenter;
|
||||
|
@ -45,37 +46,58 @@ import org.libvirt.LibvirtException;
|
|||
|
||||
import com.google.common.base.Function;
|
||||
import com.google.common.base.Supplier;
|
||||
import com.google.inject.Injector;
|
||||
import com.google.inject.Key;
|
||||
import com.google.inject.Provides;
|
||||
import com.google.inject.TypeLiteral;
|
||||
import com.google.inject.name.Names;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Adrian Cole
|
||||
*/
|
||||
public class LibvirtComputeServiceContextModule extends
|
||||
StandaloneComputeServiceContextModule<Domain, Domain, Image, Datacenter> {
|
||||
@Override
|
||||
protected void configure() {
|
||||
super.configure();
|
||||
bind(new TypeLiteral<ComputeServiceAdapter<Domain, Domain, Image, Datacenter>>() {
|
||||
}).to(LibvirtComputeServiceAdapter.class);
|
||||
bind(new TypeLiteral<Supplier<Location>>() {
|
||||
}).to(DefaultLocationSupplier.class);
|
||||
bind(new TypeLiteral<Function<Domain, NodeMetadata>>() {
|
||||
}).to(DomainToNodeMetadata.class);
|
||||
bind(new TypeLiteral<Function<Image, org.jclouds.compute.domain.Image>>() {
|
||||
}).to(LibvirtImageToImage.class);
|
||||
bind(new TypeLiteral<Function<Domain, Hardware>>() {
|
||||
}).to(DomainToHardware.class);
|
||||
bind(new TypeLiteral<Function<Datacenter, Location>>() {
|
||||
}).to(DatacenterToLocation.class);
|
||||
}
|
||||
StandaloneComputeServiceContextModule<Domain, Domain, Image, Datacenter> {
|
||||
@Override
|
||||
protected void configure() {
|
||||
super.configure();
|
||||
bind(new TypeLiteral<ComputeServiceAdapter<Domain, Domain, Image, Datacenter>>() {
|
||||
}).to(LibvirtComputeServiceAdapter.class);
|
||||
bind(new TypeLiteral<Supplier<Location>>() {
|
||||
}).to(DefaultLocationSupplier.class);
|
||||
bind(new TypeLiteral<Function<Domain, NodeMetadata>>() {
|
||||
}).to(DomainToNodeMetadata.class);
|
||||
bind(new TypeLiteral<Function<Image, org.jclouds.compute.domain.Image>>() {
|
||||
}).to(LibvirtImageToImage.class);
|
||||
bind(new TypeLiteral<Function<Domain, Hardware>>() {
|
||||
}).to(DomainToHardware.class);
|
||||
bind(new TypeLiteral<Function<Datacenter, Location>>() {
|
||||
}).to(DatacenterToLocation.class);
|
||||
}
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
protected Connect createConnection(@Provider URI endpoint, @Named(Constants.PROPERTY_IDENTITY) String identity,
|
||||
@Named(Constants.PROPERTY_CREDENTIAL) String credential) throws LibvirtException {
|
||||
// ConnectAuth connectAuth = null;
|
||||
return new Connect(endpoint.toASCIIString());
|
||||
}
|
||||
@Provides
|
||||
@Singleton
|
||||
protected Connect createConnection(@Provider URI endpoint, @Named(Constants.PROPERTY_IDENTITY) String identity,
|
||||
@Named(Constants.PROPERTY_CREDENTIAL) String credential) throws LibvirtException {
|
||||
// ConnectAuth connectAuth = null;
|
||||
return new Connect(endpoint.toASCIIString());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected TemplateBuilder provideTemplate(Injector injector, TemplateBuilder template) {
|
||||
String domainDir = injector.getInstance(Key.get(String.class, Names.named("jclouds.libvirt.domain-dir")));
|
||||
String hardwareId = searchForHardwareIdInDomainDir(domainDir);
|
||||
String image = searchForImageIdInDomainDir(domainDir);
|
||||
return template.hardwareId(hardwareId).imageId(image) ;
|
||||
}
|
||||
|
||||
private String searchForImageIdInDomainDir(String domainDir) {
|
||||
// TODO
|
||||
return "1";
|
||||
}
|
||||
|
||||
private String searchForHardwareIdInDomainDir(String domainDir) {
|
||||
// TODO
|
||||
return "c7ff2039-a9f1-a659-7f91-e0f82f59d52e";
|
||||
}
|
||||
}
|
|
@ -77,6 +77,7 @@ public class DomainToHardware implements Function<Domain, Hardware> {
|
|||
NodeList nodes = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
|
||||
String diskFileName = nodes.item(0).getNodeValue();
|
||||
for (int i = 0; i < nodes.getLength(); i++) {
|
||||
System.out.println("disk " + diskFileName);
|
||||
StorageVol storageVol = from.getConnect().storageVolLookupByPath(diskFileName);
|
||||
String id = storageVol.getKey();
|
||||
float size = new Long(storageVol.getInfo().capacity).floatValue();
|
||||
|
|
|
@ -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).description("ubuntu").build());
|
||||
builder.operatingSystem(new OperatingSystemBuilder().name(from.name).family(family).description(from.name).build());
|
||||
} catch (IllegalArgumentException e) {
|
||||
logger.debug("<< didn't match os(%s)", from);
|
||||
}
|
||||
|
|
|
@ -83,47 +83,45 @@ public class LibvirtComputeServiceAdapter implements ComputeServiceAdapter<Domai
|
|||
// 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
|
||||
// store the credentials so that later functions can use them
|
||||
// credentialStore.put(from.id + "", new Credentials(from.loginUser, from.password));
|
||||
|
||||
String[] domains;
|
||||
//String[] domains;
|
||||
try {
|
||||
domains = client.listDefinedDomains();
|
||||
|
||||
//domains = client.listDefinedDomains();
|
||||
String xmlDesc = "";
|
||||
Domain domain = null;
|
||||
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;
|
||||
//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();
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
||||
System.out.println(clonedVol.getXMLDesc(0));
|
||||
// define Domain
|
||||
String xmlFinal = generateClonedDomainXML(domain.getXMLDesc(0));
|
||||
domain = client.domainDefineXML(xmlFinal);
|
||||
|
||||
// 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);
|
||||
|
@ -142,26 +140,19 @@ public class LibvirtComputeServiceAdapter implements ComputeServiceAdapter<Domai
|
|||
// return ImmutableSet.of();
|
||||
// TODO
|
||||
// return client.listImages();
|
||||
|
||||
List<Image> images = Lists.newArrayList();
|
||||
images.add(new Image(1, "ubuntu"));
|
||||
return images;
|
||||
|
||||
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 (int domain : client.listDomains()) {
|
||||
// domains.add(client.domainLookupByID(domain));
|
||||
// }
|
||||
// return domains;
|
||||
// } catch (LibvirtException e) {
|
||||
// return propogate(e);
|
||||
// }
|
||||
// }
|
||||
|
||||
@Override
|
||||
public Iterable<Domain> listNodes() {
|
||||
try {
|
||||
|
@ -219,20 +210,10 @@ public class LibvirtComputeServiceAdapter implements ComputeServiceAdapter<Domai
|
|||
}
|
||||
}
|
||||
|
||||
public void createDomain() throws LibvirtException {
|
||||
Domain domain = client.domainDefineXML("<domain type='test' id='2'>" + " <name>deftest</name>"
|
||||
+ " <uuid>004b96e1-2d78-c30f-5aa5-f03c87d21e70</uuid>" + " <memory>8388608</memory>"
|
||||
+ " <vcpu>2</vcpu>" + " <os><type arch='i686'>hvm</type></os>" + " <on_reboot>restart</on_reboot>"
|
||||
+ " <on_poweroff>destroy</on_poweroff>" + " <on_crash>restart</on_crash>" + "</domain>");
|
||||
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
|
@ -279,7 +260,7 @@ public class LibvirtComputeServiceAdapter implements ComputeServiceAdapter<Domai
|
|||
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");
|
||||
builder.xpathFind("//domain/devices/disk/source").a("file", "/var/lib/libvirt/images/ubuntu.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);
|
||||
|
|
|
@ -65,24 +65,28 @@ public class LibvirtExperimentLiveTest {
|
|||
endpoint, apiversion, identity, credential, new LibvirtComputeServiceContextModule(), ImmutableSet
|
||||
.<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("c7ff2039-a9f1-a659-7f91-e0f82f59d52e").imageId("1") //.locationId("")
|
||||
.build();
|
||||
|
||||
*/
|
||||
|
||||
/*
|
||||
* We will probably make a default template out of properties at some point
|
||||
* You can control the default template via overriding a method in standalonecomputeservicexontextmodule
|
||||
*/
|
||||
|
||||
|
||||
// context.getComputeService().templateOptions().;
|
||||
context.getComputeService().runNodesWithTag("ttylinux", 1, defaultTemplate);
|
||||
|
||||
} catch (RunNodesException e) {
|
||||
|
||||
context.getComputeService().runNodesWithTag("ubuntu", 1/*, defaultTemplate*/);
|
||||
|
||||
} catch (RunNodesException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
if (context != null)
|
||||
|
|
Loading…
Reference in New Issue