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
public void destroyNode(String id) {
super.destroyNode(id);
eliminateDomain(id);
//eliminateDomain(id);
}
private void eliminateDomain(String id) {

View File

@ -19,28 +19,31 @@
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 java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.net.URI;
import java.util.Map;
import java.util.Collection;
import javax.inject.Named;
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.compute.ComputeService;
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.internal.BaseComputeService;
import org.jclouds.compute.suppliers.DefaultLocationSupplier;
import org.jclouds.domain.Location;
import org.jclouds.libvirt.Datacenter;
import org.jclouds.libvirt.Image;
import org.jclouds.libvirt.compute.LibvirtComputeService;
import org.jclouds.libvirt.compute.functions.DatacenterToLocation;
import org.jclouds.libvirt.compute.functions.DomainToHardware;
import org.jclouds.libvirt.compute.functions.DomainToNodeMetadata;
@ -50,15 +53,20 @@ import org.jclouds.rest.annotations.Provider;
import org.libvirt.Connect;
import org.libvirt.Domain;
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.Splitter;
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.Key;
import com.google.inject.Provides;
import com.google.inject.TypeLiteral;
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>>() {
}).to(DatacenterToLocation.class);
bind(ComputeService.class).to(LibvirtComputeService.class);
//bind(ComputeService.class).to(LibvirtComputeService.class);
}
@Provides
@ -95,22 +103,37 @@ StandaloneComputeServiceContextModule<Domain, Domain, Image, Datacenter> {
@Override
protected TemplateBuilder provideTemplate(Injector injector, TemplateBuilder template) {
//String domainDir = injector.getInstance(Key.get(String.class, Names.named(PROPERTY_LIBVIRT_DOMAIN_DIR)));
String domainDir = "";
System.out.println("++++ domain dir: " + domainDir);
String domainDir = injector.getInstance(Key.get(String.class, Names.named(PROPERTY_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";
}
@SuppressWarnings("unchecked")
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
String poolName = storageVol.storagePoolLookupByVolume().getName();
StoragePool storagePool = client.storagePoolLookupByName(poolName);
StorageVol clonedVol = null; //cloneVolume(storagePool, storageVol);
StorageVol clonedVol = null;
boolean cloned = false;
int retry = 0;
while(!cloned && retry<10) {
@ -166,17 +166,30 @@ public class LibvirtComputeServiceAdapter implements ComputeServiceAdapter<Domai
@Override
public Domain getNode(String id) {
Domain d = null;
try {
return client.domainLookupByUUIDString(id);
d = client.domainLookupByUUIDString(id);
} 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
public void destroyNode(String id) {
try {
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) {
propogate(e);
} catch (Exception e) {

View File

@ -26,6 +26,8 @@ import java.util.Set;
import org.jclouds.compute.ComputeServiceContext;
import org.jclouds.compute.ComputeServiceContextFactory;
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.Test;
@ -54,20 +56,21 @@ public class LibvirtExperimentLiveTest {
ComputeServiceContext context = null;
try {
context = new ComputeServiceContextFactory().createContext(new LibvirtComputeServiceContextSpec(
"test:///default", "identity", "credential"));
"qemu:///system", "identity", "credential"));
/*
*
* /* 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();
*/
/* Template defaultTemplate = context.getComputeService().templateBuilder()
.hardwareId("d106ae67-5a1b-8f91-b311-83c93bcb0a1f").imageId("1") //.locationId("")
.build();*/
/*
* We will probably make a default template out of properties at some point You can control