On 07/30/2012 05:30 AM, Yu Mingfei wrote:
This patch adds a function to ElementTree: remove().
It can remove a subelement with given element name.
And this patch adds a libvirt_xml to virt, to put some operations of
libvirt xml.
Signed-off-by: Yu Mingfei <[email protected]>
---
client/shared/ElementTree.py | 10 ++++++++++
client/virt/libvirt_xml.py | 42
++++++++++++++++++++++++++++++++++++++++++
2 files changed, 52 insertions(+), 0 deletions(-)
create mode 100644 client/virt/libvirt_xml.py
diff --git a/client/shared/ElementTree.py b/client/shared/ElementTree.py
index 7dbc72e..50c1d82 100644
--- a/client/shared/ElementTree.py
+++ b/client/shared/ElementTree.py
@@ -647,6 +647,16 @@ class ElementTree:
return self._root.findall(path)
##
+ # Removes a matching subelement.
+ #
+ # @param element is What to be removed.
+ # @return a new element tree.
+
+ def remove(self, element):
+ assert self._root is not None
+ return self._root.remove(element)
+
+ ##
# Writes the element tree to a file, as XML.
#
# @param file A file name, or a file object opened for writing.
I realize I've done a little bit dangerous modifying of this bundled
module, however we definitely don't want the interface to diverge from
upstream python (2.7). Since there's already a remove() method in the
Element class, are you sure this issue even needed?
If instead you'd prefer to extend my xml_utils classes, please feel free
to do so. If this were something in 2.7 but not in 2.6, I'd be more
comfortable adding it. Otherwise, I really want to avoid extending
ElementTree module unless absolutely necessary.
diff --git a/client/virt/libvirt_xml.py b/client/virt/libvirt_xml.py
new file mode 100644
index 0000000..79a1382
--- /dev/null
+++ b/client/virt/libvirt_xml.py
@@ -0,0 +1,42 @@
+import logging, os.path
+from autotest.client.shared import error, xml_utils
+from autotest.client.virt import libvirt_vm as virsh
This virsh namespace may change back to 'virsh_foo()', based on my RFC
to the list...errrr...where did it go? Blast, I'll have to re-send it.
Personally I like virsh.foo() better, but it's a fairly invasive change
and will break third-party stuff :( I'll re-post my RFC to to see what
others comment.
+
+class LibvirtXMLError(Exception):
+ pass
+
+class LibvirtXMLVMNameError(LibvirtXMLError):
+ pass
+
+class LibvirtXML(xml_utils.XMLBase):
+ pass
+
+def vm_rename(name, new_name, uri=""):
+ """
+ Rename a vm within libvirt
+
+ @param name: The current name for the vm.
+ @param new_name: The new name for the vm.
+ """
+ if name == new_name or virsh.virsh_domain_exists(new_name, uri):
+ raise LibvirtXMLVMNameError("Cannot rename a VM to an existing
name")
+
+ # Two xml files to define and undefine
+ old_vm_xml = "/tmp/old_vm.xml"
+ new_vm_xml = "/tmp/new_vm.xml"
Static temp files are dangerous, however they're also not needed here -
see below...
+ virsh.virsh_dumpxml(name, to_file=old_vm_xml, uri=uri)
Using a file here isn't necessary, you can pump the XML in string-form,
directly into LibvirtXML class. It will automatically create two unique
temporary files, 4.e.x. named:
vm_xml.name and vm_xml.tempsource.name
The first one you make changes on, the second is there as a backup. If
you don't think this setup is useful, let's fix it in xml_utils :)
+ vm_xml = LibvirtXML(old_vm_xml)
+ vm_xml.find('name').text = new_name
+ vm_xml.remove(vm_xml.find('uuid'))
+ vm_xml.write(new_vm_xml)
This write to another file can also be avoided. Just use
vm_xml.write(), and pass vm_xml.name in to virsh_define below.
+
+ if not virsh.virsh_undefine(name, uri):
+ logging.error("Undefine %s failed.", name)
+ # backup and temporary files removed automatically
+ raise LibvirtXMLVMNameError("Failed to remove VM for renaming")
+ if not virsh.virsh_define(new_vm_xml, uri):
+ if not virsh.virsh_define(old_vm_xml):
Here, instead of 'old_vm_xml', you can just pass it
vm_xml.tempsource.name which is the backup. I probably should have
made this more clear or chosen a better name.
I'll fix this in xml_utils since other people will probably be confused
as well...
+ raise LibvirtXMLVMNameError("Failed to restore original VM
on "
+ "rename exception.")
+ raise LibvirtXMLVMNameError("Failed to define renamed vm from
XML")
+ logging.debug("Sucessfully renamed %s to %s.", name, new_name)
--
1.7.1
--
Best Regards
Yu Mingfei
--
Chris Evich, RHCA, RHCE, RHCDS, RHCSS
Quality Assurance Engineer
e-mail: cevich + `@' + redhat.com o: 1-888-RED-HAT1 x44214
_______________________________________________
Autotest-kernel mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/autotest-kernel