diff options
author | Cole Robinson <crobinso@redhat.com> | 2020-01-27 12:13:23 -0500 |
---|---|---|
committer | Cole Robinson <crobinso@redhat.com> | 2020-01-27 13:08:12 -0500 |
commit | dbcebeb73441a2ea01c83fccdbc39d9c687afa92 (patch) | |
tree | f770f39e76f5678d5e7640675fbbb1e2f9b3acf1 /virtinst/cloner.py | |
parent | 796ba8f3c52f3e85e358a4676d5b24782af5e732 (diff) | |
download | virt-manager-dbcebeb73441a2ea01c83fccdbc39d9c687afa92.tar.gz |
guest: Move VM replace helper to cloner
It's the only user. Rework it a bit and add full coverage
Signed-off-by: Cole Robinson <crobinso@redhat.com>
Diffstat (limited to 'virtinst/cloner.py')
-rw-r--r-- | virtinst/cloner.py | 27 |
1 files changed, 25 insertions, 2 deletions
diff --git a/virtinst/cloner.py b/virtinst/cloner.py index ac6987b0..97712837 100644 --- a/virtinst/cloner.py +++ b/virtinst/cloner.py @@ -23,6 +23,29 @@ from .storage import StorageVolume from .devices import DeviceChannel +def _replace_vm(conn, name): + """ + Remove the existing VM with the same name if requested + """ + try: + vm = conn.lookupByName(name) + except libvirt.libvirtError: + return + + try: + + log.debug("Explicitly replacing guest '%s'", name) + if vm.ID() != -1: + log.debug("Destroying guest '%s'", name) + vm.destroy() + + log.debug("Undefining guest '%s'", name) + vm.undefine() + except libvirt.libvirtError as e: # pragma: no cover + raise RuntimeError(_("Could not remove old vm '%s': %s") % + (str(e))) + + class Cloner(object): # Reasons why we don't default to cloning. @@ -447,8 +470,8 @@ class Cloner(object): dom = None try: # Replace orig VM if required - Guest.check_vm_collision(self.conn, self.clone_name, - do_remove=self.replace) + if self.replace: + _replace_vm(self.conn, self.clone_name) # Define domain early to catch any xml errors before duping storage dom = self.conn.defineXML(self.clone_xml) |