domainId,volumeId,macAddress creation generalized; suspend/resume ok

This commit is contained in:
andreaturli 2010-11-04 16:13:36 +01:00
parent 69342379a5
commit 34fc21114e
4 changed files with 65 additions and 69 deletions

View File

@ -1,3 +0,0 @@
domain name generalization
volume name generalization
macaddress

View File

@ -77,7 +77,6 @@ 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

@ -23,6 +23,7 @@ import static com.google.common.base.Preconditions.checkNotNull;
import java.io.IOException;
import java.io.StringReader;
import java.security.SecureRandom;
import java.util.List;
import java.util.Map;
import java.util.Properties;
@ -57,6 +58,7 @@ import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import com.google.common.base.Splitter;
import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
@ -80,7 +82,6 @@ public class LibvirtComputeServiceAdapter implements ComputeServiceAdapter<Domai
@Override
public Domain runNodeWithTagAndNameAndStoreCredentials(String tag, String name, Template template,
Map<String, Credentials> credentialStore) {
try {
Domain domain = null;
String domainName = tag;
@ -94,20 +95,19 @@ public class LibvirtComputeServiceAdapter implements ComputeServiceAdapter<Domai
nodes = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
String diskFileName = nodes.item(0).getNodeValue();
StorageVol storageVol = client.storageVolLookupByPath(diskFileName);
// cloning volume
String poolName = "default";
String poolName = storageVol.storagePoolLookupByVolume().getName();
StoragePool storagePool = client.storagePoolLookupByName(poolName);
StorageVol clonedVol = cloneVolume(storagePool, storageVol);
// define Domain
String xmlFinal = generateClonedDomainXML(domain.getXMLDesc(0));
domain = client.domainDefineXML(xmlFinal);
domain.create();
String xmlFinal = generateClonedDomainXML(domain.getXMLDesc(0), clonedVol);
Domain newDomain = client.domainDefineXML(xmlFinal);
newDomain.create();
// store the credentials so that later functions can use them
credentialStore.put(domain.getUUIDString() + "", new Credentials("identity", "credential"));
return domain;
return newDomain;
} catch (LibvirtException e) {
return propogate(e);
} catch (Exception e) {
@ -210,60 +210,9 @@ public class LibvirtComputeServiceAdapter implements ComputeServiceAdapter<Domai
XPathExpressionException, ParserConfigurationException, SAXException, IOException, TransformerException {
String fromXML = from.getXMLDesc(0);
String clonedXML = generateClonedVolumeXML(fromXML);
return storagePool.storageVolCreateXMLFrom(clonedXML, from, 0);
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);
String fromVolPath = builder.xpathFind("//domain/devices/disk/source").getElement().getAttribute("file");
builder.xpathFind("//domain/devices/disk/source").a("file", fromVolPath + cloneAppend);
// TODO generate valid MAC address
String fromMACaddress = builder.xpathFind("//domain/devices/interface/mac").getElement().getAttribute("address");
builder.xpathFind("//domain/devices/interface/mac").a("address", "52:54:00:5c:dd:eb");
return builder.asString(outputProperties);
}
@Override
public void resumeNode(String id) {
try {
@ -280,6 +229,59 @@ public class LibvirtComputeServiceAdapter implements ComputeServiceAdapter<Domai
} catch (LibvirtException e) {
propogate(e);
}
}
private static String generateClonedVolumeXML(String fromXML) throws ParserConfigurationException, SAXException,
IOException, XPathExpressionException, TransformerException {
Properties outputProperties = generateOutputXMLProperties();
XMLBuilder builder = XMLBuilder.parse(new InputSource(new StringReader(fromXML)));
String nodeNamingConvention = "%s-%s";
String tag = "-clone";
String suffix = String.format(nodeNamingConvention, tag, Integer.toHexString(new SecureRandom().nextInt(4095)));
builder.xpathFind("//volume/name").t(suffix);
builder.xpathFind("//volume/key").t(suffix);
builder.xpathFind("//volume/target/path").t(suffix);
return builder.asString(outputProperties);
}
}
private static String generateClonedDomainXML(String fromXML, StorageVol clonedVol) throws ParserConfigurationException, SAXException,
IOException, XPathExpressionException, TransformerException, LibvirtException {
Properties outputProperties = generateOutputXMLProperties();
XMLBuilder builder = XMLBuilder.parse(new InputSource(new StringReader(fromXML)));
String nodeNamingConvention = "%s-%s";
String tag = "-clone";
String suffix = String.format(nodeNamingConvention, tag, Integer.toHexString(new SecureRandom().nextInt(4095)));
builder.xpathFind("//domain/name").t(suffix);
// 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);
//String fromVolPath = builder.xpathFind("//domain/devices/disk/source").getElement().getAttribute("file");
builder.xpathFind("//domain/devices/disk/source").a("file", clonedVol.getPath());
// generate valid MAC address
String fromMACaddress = builder.xpathFind("//domain/devices/interface/mac").getElement().getAttribute("address");
String lastMACoctet = Integer.toHexString(new SecureRandom().nextInt(255));
builder.xpathFind("//domain/devices/interface/mac").a("address",
fromMACaddress.substring(0, fromMACaddress.lastIndexOf(":")+1) + lastMACoctet
);
return builder.asString(outputProperties);
}
private static Properties generateOutputXMLProperties() {
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");
return outputProperties;
}
}

View File

@ -87,16 +87,14 @@ public class LibvirtExperimentLiveTest {
*/
Set<? extends NodeMetadata> nodeMetadataSet = context.getComputeService().runNodesWithTag("ttylinux", 1);
for (NodeMetadata nodeMetadata : nodeMetadataSet) {
context.getComputeService().suspendNode(nodeMetadata.getId());
Thread.sleep(2000);
Thread.sleep(3000);
context.getComputeService().resumeNode(nodeMetadata.getId());
// TODO seems that destroy is intended to be a force shutoff, not a delete VM ...
//context.getComputeService().destroyNode(nodeMetadata.getId());
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (context != null)