On 07/24/2012 12:57 AM, Yu Mingfei wrote:
Hi Chris:
During your vacation, I discussed the memeber function rename of VM
class with Alex in patchset of domname.
You can refer to mails "[PATCH 0/3]Add virsh domname testcases for
libvirt" on 7/10.
Alex advised me to split xml config operation to another class or
file,but I found you have some work on the edit of xml config.
Just as we discussed in mail "Re: [PATCH 1/3]Add virsh domname testcases
for libvirt"(7/11),
So could you take a look at my implement of vm.rename(),
there is a function named rename_xml_utils(...) in vm.rename(),
is that ok to add this function to your work.
Thanks.
Thanks for replying with the patch to reference. I understand now where
the confusion is. Here's my motivation:
I felt that we were missing a more developer-friendly, low-level XML
processing module. I gathered up the main pain-points in current
autotest code. Then tried to solve most of them in
client/shared/xml_utils.py and by including ElementTree and ElementPath.
For your rename tests and method, instead of a nested function, what I
was thinking of was more of an entirely new module. However, now that I
look at code, a libvirt_xml module would have a circular dependency on
the virsh_*() functions.
The good news is we already talked (a while ago) about moving all
virsh_*() out of libvirt_vm anyway. Assuming virsh_*() stuff is also
moved, the attached patch should get you started (untested).
P.S. Let me know if you want me to help take care of the virsh_*() move,
I'm happy to.
--
Chris Evich, RHCA, RHCE, RHCDS, RHCSS
Quality Assurance Engineer
e-mail: cevich + `@' + redhat.com o: 1-888-RED-HAT1 x44214
diff --git a/client/virt/libvirt_xml.py b/client/virt/libvirt_xml.py
new file mode 100644
index 0000000..9759baf
--- /dev/null
+++ b/client/virt/libvirt_xml.py
@@ -0,0 +1,42 @@
+"""
+Utility classes and functions to help manipulate libvirt XML data.
+
+@copyright: 2012 Red Hat Inc.
+"""
+
+import logging, os.path
+from autotest.client.shared import error, xml_utils
+from autotest.client.virt import libvirt_virsh as virsh
+
+class LibvirtXMLError(exception):
+ pass
+
+class LibvirtXMLVMNameError(ValueError):
+ 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.domain_exists(name, uri):
+ raise LibvirtXMLVMNameError("Cannot rename a VM to an existing name")
+
+ vm_xml = LibvirtXML(virsh.dumpxml(uri=uri))
+ vm_xml.find('name').text = new_name
+
+ if not virsh.undefine(name, uri):
+ logging.error("Undefine %s failed.", self.name)
+ # backup and temporary files removed automatically
+ raise LibvirtXMLVMNameError("Failed to remove VM for renaming")
+ if not virsh.define(vm_xml.name, uri):
+ if not virsh.define(vm_xml.tempsource.name):
+ 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)
_______________________________________________
Autotest mailing list
Autotest@test.kernel.org
http://test.kernel.org/cgi-bin/mailman/listinfo/autotest