On 8/2/2012 12:31 AM, Chris Evich wrote:
On 08/01/2012 06:17 AM, Yu Mingfei wrote:
This patch adds a libvirt_xml to virt, to put some operations of libvirt
xml.
And this patch adds a function to XMLBase: remove().
It can remove a subelement with given element name.
Signed-off-by: Yu Mingfei <[email protected]>
---
client/shared/xml_utils.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/xml_utils.py b/client/shared/xml_utils.py
index 6f90aa4..9c78331 100644
--- a/client/shared/xml_utils.py
+++ b/client/shared/xml_utils.py
@@ -140,6 +140,16 @@ class XMLBase(ElementTree.ElementTree, XMLBackup):
except (OSError, IOError):
return False
+ def remove(self, element):
+ """
+ Removes a matching subelement.
+
+ @param element is What to be removed.
+ @return a new element tree.
+ """
+ assert self._root is not None
+ return self._root.remove(element)
+
Best to not use private properties here. Something like this is cleaner:
root = self.get_root()
assert root is not None
return root.remove(element)
It would also be nice to add this to the unittests.
def write(self, filename=None, encoding="UTF-8"):
"""
Write current XML tree to filename, or self.name if None.
diff --git a/client/virt/libvirt_xml.py b/client/virt/libvirt_xml.py
new file mode 100644
index 0000000..32b63d5
--- /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
+
+
+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")
+
+ vm_xml = LibvirtXML(virsh.virsh_dumpxml(name, uri=uri))
+ vm_xml.find('name').text = new_name
+ vm_xml.remove(vm_xml.find('uuid'))
+ vm_xml.write()
+
+ 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(vm_xml.name, uri):
+ if not virsh.virsh_define(vm_xml.tempsource.name):
FYI: I renamed tempsource -> sourcebackupfile because it's more clear
name. However we can actually just use vm_xml.sourcefilename here.
+ 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
This looks good, I'll fixup the above items by hand and work on applying
this to next.
Thanks, tell me please if you need any help.
--
Best Regards
Yu Mingfei
_______________________________________________
Autotest-kernel mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/autotest-kernel