* 'master' of http://github.com/andreaturli/jclouds:
  README improved
  fix runNodeWithTagAndNameAndStoreCredentials: it shoud run the domain not only define it
  improvement on libvirt
This commit is contained in:
Adrian Cole 2010-11-02 09:04:03 -07:00
commit 90e83c256a
6 changed files with 166 additions and 85 deletions

View File

@ -1,2 +1,73 @@
The libvirt library is used to interface with different virtualization technologies (http://libvirt.org/)
libvirt supports:
The Xen hypervisor on Linux and Solaris hosts.
The QEMU emulator
The KVM Linux hypervisor
The LXC Linux container system
The OpenVZ Linux container system
The User Mode Linux paravirtualized kernel
The VirtualBox hypervisor
The VMware ESX and GSX hypervisors
Storage on IDE/SCSI/USB disks, FibreChannel, LVM, iSCSI, NFS and filesystems
Getting Started Guide for jclouds-libvirt
install libvirt on your os
* if os/x, see http://github.com/justinclift/libvirt
* if you are using Linux, let's suppose you want to use KVM:
- install libvirt and KVM (http://www.linux-kvm.org/page/Main_Page).
Remember to run
egrep '(vmx|svm)' /proc/cpuinfo
If nothing is printed, it means that your cpu does not support hardware virtualization.
Verify Installation
$ virsh -c qemu:///system list
Id Name State
----------------------------------
(for Ubuntu users: look also at this good turorial https://help.ubuntu.com/community/KVM)
Create your first guest
- download, for example, an ubuntu 10.04 LTS ISO
- create a libvirt domain by using:
virt-manager: a GUI tool at http://virt-manager.et.redhat.com/
virt-install, a python script developed by Red Hat (sudo apt-get install python-virtinst)
ubuntu-vm-builder, developed by Canonical. (sudo apt-get install ubuntu-vm-builder)
NB: use Javascript tool that generates the parameters for ubuntu-vm-builder: http://people.ubuntu.com/~kirkland/ubuntu-vm-builder.html
Now that you have a libvirt domain, your workstation is ready to use jclouds-libvirt.
You can now download jclouds-libvirt and give a try by running
ComputeServiceContext context = null;
try {
context = new ComputeServiceContextFactory()
.createContext(new StandaloneComputeServiceContextSpec<Domain, Domain, Image, Datacenter>("libvirt",
endpoint, apiversion, identity, credential, new LibvirtComputeServiceContextModule(), ImmutableSet
.<Module> of()));
Template defaultTemplate = context.getComputeService().templateBuilder()
.hardwareId("c7ff2039-a9f1-a659-7f91-e0f82f59d52e").imageId("1").build();
context.getComputeService().runNodesWithTag(domainName, 1, defaultTemplate);
} catch (RunNodesException e) {
e.printStackTrace();
} finally {
if (context != null)
context.close();
}
where identity=your_name, endpoint=qemu:///system
and domainName equals to the name chosen during the creation of libvirt domain
NB: apiversion, credential can be null

View File

@ -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";
}
}

View File

@ -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();

View File

@ -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);
}

View File

@ -83,47 +83,47 @@ 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);
domain.create();
// 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 +142,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 +212,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);
}

View File

@ -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("ttylinux", 1/*, defaultTemplate*/);
} catch (RunNodesException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (context != null)