templateBuilder improvement

This commit is contained in:
andreaturli 2010-11-12 22:08:53 +01:00
parent 08b8bb1f00
commit 5ea05a3066
4 changed files with 63 additions and 24 deletions

View File

@ -103,7 +103,7 @@ public class LibvirtComputeService extends BaseComputeService {
@Override @Override
public void destroyNode(String id) { public void destroyNode(String id) {
super.destroyNode(id); super.destroyNode(id);
eliminateDomain(id); //eliminateDomain(id);
} }
private void eliminateDomain(String id) { private void eliminateDomain(String id) {

View File

@ -19,28 +19,31 @@
package org.jclouds.libvirt.compute.domain; package org.jclouds.libvirt.compute.domain;
import static com.google.common.collect.Maps.newLinkedHashMap;
import static org.jclouds.libvirt.LibvirtConstants.PROPERTY_LIBVIRT_DOMAIN_DIR; import static org.jclouds.libvirt.LibvirtConstants.PROPERTY_LIBVIRT_DOMAIN_DIR;
import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.net.URI; import java.net.URI;
import java.util.Map; import java.util.Collection;
import javax.inject.Named; import javax.inject.Named;
import javax.inject.Singleton; import javax.inject.Singleton;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPathExpressionException;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.WildcardFileFilter;
import org.jclouds.Constants; import org.jclouds.Constants;
import org.jclouds.compute.ComputeService;
import org.jclouds.compute.ComputeServiceAdapter; import org.jclouds.compute.ComputeServiceAdapter;
import org.jclouds.compute.config.StandaloneComputeServiceContextModule; import org.jclouds.compute.config.StandaloneComputeServiceContextModule;
import org.jclouds.compute.domain.Hardware; import org.jclouds.compute.domain.Hardware;
import org.jclouds.compute.domain.NodeMetadata; import org.jclouds.compute.domain.NodeMetadata;
import org.jclouds.compute.domain.TemplateBuilder; import org.jclouds.compute.domain.TemplateBuilder;
import org.jclouds.compute.internal.BaseComputeService;
import org.jclouds.compute.suppliers.DefaultLocationSupplier; import org.jclouds.compute.suppliers.DefaultLocationSupplier;
import org.jclouds.domain.Location; import org.jclouds.domain.Location;
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.LibvirtComputeService;
import org.jclouds.libvirt.compute.functions.DatacenterToLocation; import org.jclouds.libvirt.compute.functions.DatacenterToLocation;
import org.jclouds.libvirt.compute.functions.DomainToHardware; import org.jclouds.libvirt.compute.functions.DomainToHardware;
import org.jclouds.libvirt.compute.functions.DomainToNodeMetadata; import org.jclouds.libvirt.compute.functions.DomainToNodeMetadata;
@ -50,15 +53,20 @@ import org.jclouds.rest.annotations.Provider;
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.xml.sax.InputSource;
import org.xml.sax.SAXException;
import com.google.common.base.Charsets;
import com.google.common.base.Function; import com.google.common.base.Function;
import com.google.common.base.Splitter;
import com.google.common.base.Supplier; import com.google.common.base.Supplier;
import com.google.common.collect.Iterables;
import com.google.common.io.Files;
import com.google.inject.Injector; import com.google.inject.Injector;
import com.google.inject.Key; import com.google.inject.Key;
import com.google.inject.Provides; import com.google.inject.Provides;
import com.google.inject.TypeLiteral; import com.google.inject.TypeLiteral;
import com.google.inject.name.Names; import com.google.inject.name.Names;
import com.jamesmurty.utils.XMLBuilder;
/** /**
* *
@ -82,7 +90,7 @@ StandaloneComputeServiceContextModule<Domain, Domain, Image, Datacenter> {
bind(new TypeLiteral<Function<Datacenter, Location>>() { bind(new TypeLiteral<Function<Datacenter, Location>>() {
}).to(DatacenterToLocation.class); }).to(DatacenterToLocation.class);
bind(ComputeService.class).to(LibvirtComputeService.class); //bind(ComputeService.class).to(LibvirtComputeService.class);
} }
@Provides @Provides
@ -95,22 +103,37 @@ StandaloneComputeServiceContextModule<Domain, Domain, Image, Datacenter> {
@Override @Override
protected TemplateBuilder provideTemplate(Injector injector, TemplateBuilder template) { protected TemplateBuilder provideTemplate(Injector injector, TemplateBuilder template) {
//String domainDir = injector.getInstance(Key.get(String.class, Names.named(PROPERTY_LIBVIRT_DOMAIN_DIR))); String domainDir = injector.getInstance(Key.get(String.class, Names.named(PROPERTY_LIBVIRT_DOMAIN_DIR)));
String domainDir = "";
System.out.println("++++ domain dir: " + domainDir);
String hardwareId = searchForHardwareIdInDomainDir(domainDir); String hardwareId = searchForHardwareIdInDomainDir(domainDir);
String image = searchForImageIdInDomainDir(domainDir); String image = searchForImageIdInDomainDir(domainDir);
return template.hardwareId(hardwareId).imageId(image) ; return template.hardwareId(hardwareId).imageId(image) ;
} }
private String searchForImageIdInDomainDir(String domainDir) { private String searchForImageIdInDomainDir(String domainDir) {
// TODO // TODO
return "1"; return "1";
} }
@SuppressWarnings("unchecked")
private String searchForHardwareIdInDomainDir(String domainDir) { private String searchForHardwareIdInDomainDir(String domainDir) {
// TODO
return "c7ff2039-a9f1-a659-7f91-e0f82f59d52e"; Collection<File> xmlDomains = FileUtils.listFiles( new File(domainDir), new WildcardFileFilter("*.xml"), null);
String uuid = "";
try {
String fromXML = Files.toString(Iterables.get(xmlDomains, 0), Charsets.UTF_8);
XMLBuilder builder = XMLBuilder.parse(new InputSource(new StringReader(fromXML)));
uuid = builder.xpathFind("/domain/uuid").getElement().getTextContent();
} catch (IOException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (XPathExpressionException e) {
e.printStackTrace();
}
return uuid;
} }
/* /*

View File

@ -100,7 +100,7 @@ public class LibvirtComputeServiceAdapter implements ComputeServiceAdapter<Domai
// cloning volume // cloning volume
String poolName = storageVol.storagePoolLookupByVolume().getName(); String poolName = storageVol.storagePoolLookupByVolume().getName();
StoragePool storagePool = client.storagePoolLookupByName(poolName); StoragePool storagePool = client.storagePoolLookupByName(poolName);
StorageVol clonedVol = null; //cloneVolume(storagePool, storageVol); StorageVol clonedVol = null;
boolean cloned = false; boolean cloned = false;
int retry = 0; int retry = 0;
while(!cloned && retry<10) { while(!cloned && retry<10) {
@ -166,17 +166,30 @@ public class LibvirtComputeServiceAdapter implements ComputeServiceAdapter<Domai
@Override @Override
public Domain getNode(String id) { public Domain getNode(String id) {
Domain d = null;
try { try {
return client.domainLookupByUUIDString(id); d = client.domainLookupByUUIDString(id);
} catch (LibvirtException e) { } catch (LibvirtException e) {
return propogate(e); if (e.getMessage().indexOf("Domain not found: no domain with matching uuid") != -1)
return null;
propogate(e);
} }
return d;
} }
@Override @Override
public void destroyNode(String id) { public void destroyNode(String id) {
try { try {
client.domainLookupByUUIDString(id).destroy(); client.domainLookupByUUIDString(id).destroy();
XMLBuilder builder = XMLBuilder.parse(new InputSource(new StringReader(
client.domainLookupByUUIDString(id).getXMLDesc(0)
)));
String diskFileName = builder.xpathFind("//devices/disk[@device='disk']/source").getElement().getAttribute("file");
StorageVol storageVol = client.storageVolLookupByPath(diskFileName);
storageVol.delete(0);
client.domainLookupByUUIDString(id).undefine();
} catch (LibvirtException e) { } catch (LibvirtException e) {
propogate(e); propogate(e);
} catch (Exception e) { } catch (Exception e) {

View File

@ -26,6 +26,8 @@ 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.domain.NodeMetadata; import org.jclouds.compute.domain.NodeMetadata;
import org.jclouds.compute.domain.Template;
import org.jclouds.libvirt.compute.LibvirtComputeServiceContextSpec;
import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test; import org.testng.annotations.Test;
@ -54,20 +56,21 @@ public class LibvirtExperimentLiveTest {
ComputeServiceContext context = null; ComputeServiceContext context = null;
try { try {
context = new ComputeServiceContextFactory().createContext(new LibvirtComputeServiceContextSpec( context = new ComputeServiceContextFactory().createContext(new LibvirtComputeServiceContextSpec(
"test:///default", "identity", "credential")); "qemu:///system", "identity", "credential"));
/* /*
*
* /* System.out.println("images " + context.getComputeService().listImages()); * /* System.out.println("images " + context.getComputeService().listImages());
* System.out.println("nodes " + context.getComputeService().listNodes()); * System.out.println("nodes " + context.getComputeService().listNodes());
* System.out.println("hardware profiles " + * System.out.println("hardware profiles " +
* context.getComputeService().listHardwareProfiles()); * context.getComputeService().listHardwareProfiles());
*/ */
/*
* Template defaultTemplate = context.getComputeService().templateBuilder() /* Template defaultTemplate = context.getComputeService().templateBuilder()
* .hardwareId("c7ff2039-a9f1-a659-7f91-e0f82f59d52e").imageId("1") //.locationId("") .hardwareId("d106ae67-5a1b-8f91-b311-83c93bcb0a1f").imageId("1") //.locationId("")
* .build(); .build();*/
*/
/* /*
* We will probably make a default template out of properties at some point You can control * We will probably make a default template out of properties at some point You can control