guest: Drop removeOld parameter, just use Guest.replace

This commit is contained in:
Cole Robinson 2013-08-09 14:16:59 -04:00
parent 87789548ba
commit 090f663a6a
2 changed files with 54 additions and 56 deletions

View File

@ -62,6 +62,7 @@ class Cloner(object):
self._skip_target = []
self._preserve = True
self._clone_running = False
self._replace = False
# Default clone policy for back compat: don't clone readonly,
# shareable, or empty disks
@ -69,9 +70,6 @@ class Cloner(object):
self.CLONE_POLICY_NO_SHAREABLE,
self.CLONE_POLICY_NO_EMPTYMEDIA]
# Throwaway guest to use for easy validation
self._valid_guest = Guest(conn)
# Generate a random UUID at the start
self.clone_uuid = util.generate_uuid(conn)
@ -101,7 +99,8 @@ class Cloner(object):
return self._clone_name
def set_clone_name(self, name):
try:
self._valid_guest.name = name
Guest.validate_name(self.conn, name,
check_collision=not self.replace)
except ValueError, e:
raise ValueError(_("Invalid name for new guest: %s") % e)
@ -111,7 +110,7 @@ class Cloner(object):
def set_clone_uuid(self, uuid):
try:
self._valid_guest.uuid = uuid
util.validate_uuid(uuid)
except ValueError, e:
raise ValueError(_("Invalid uuid for new guest: %s") % e)
@ -250,9 +249,9 @@ class Cloner(object):
"cloning.")
def _get_replace(self):
return self._valid_guest.replace
return self._replace
def _set_replace(self, val):
self._valid_guest.replace = bool(val)
self._replace = bool(val)
replace = property(_get_replace, _set_replace,
doc="If enabled, don't check for clone name collision, "
"simply undefine any conflicting guest.")
@ -413,9 +412,6 @@ class Cloner(object):
self.setup_original()
self.setup_clone()
def remove_original_vm(self, force=None):
return self._valid_guest.remove_original_vm(force=force)
def start_duplicate(self, meter=None):
"""
Actually perform the duplication: cloning disks if needed and defining
@ -429,7 +425,8 @@ class Cloner(object):
dom = None
try:
# Replace orig VM if required
self.remove_original_vm()
Guest.check_vm_collision(self.conn, self.clone_name,
do_remove=self.replace)
# Define domain early to catch any xml errors before duping storage
dom = self.conn.defineXML(self.clone_xml)

View File

@ -160,6 +160,48 @@ class Guest(XMLBuilder):
return cpustr
@staticmethod
def check_vm_collision(conn, name, do_remove):
"""
Remove the existing VM with the same name if requested, or error
if there is a collision.
"""
vm = None
try:
vm = conn.lookupByName(name)
except libvirt.libvirtError:
pass
if vm is None:
return
if not do_remove:
raise RuntimeError(_("Domain named %s already exists!") % name)
try:
logging.debug("Explicitly replacing guest '%s'", name)
if vm.ID() != -1:
logging.info("Destroying guest '%s'", name)
vm.destroy()
logging.info("Undefining guest '%s'", name)
vm.undefine()
except libvirt.libvirtError, e:
raise RuntimeError(_("Could not remove old vm '%s': %s") %
(str(e)))
@staticmethod
def validate_name(conn, name, check_collision):
util.validate_name(_("Guest"), name, lencheck=True)
if not check_collision:
return
try:
conn.lookupByName(name)
except:
return
raise ValueError(_("Guest name '%s' is already in use.") % name)
_XML_ROOT_XPATH = "/domain"
_XML_PROP_ORDER = ["type", "name", "uuid", "description",
@ -201,16 +243,7 @@ class Guest(XMLBuilder):
def _validate_name(self, val):
if val == self.name:
return
util.validate_name(_("Guest"), val, lencheck=True)
if self.replace:
return
try:
self.conn.lookupByName(val)
except:
return
raise ValueError(_("Guest name '%s' is already in use.") % val)
self.validate_name(self.conn, val, check_collision=not self.replace)
name = XMLProperty(xpath="./name", validate_cb=_validate_name)
def _set_memory(self, val):
@ -604,40 +637,7 @@ class Guest(XMLBuilder):
# Actual install methods #
##########################
def remove_original_vm(self, force=None):
"""
Remove the existing VM with the same name if requested, or error
if there is a collision.
"""
if force is None:
force = self.replace
vm = None
try:
vm = self.conn.lookupByName(self.name)
except libvirt.libvirtError:
pass
if vm is None:
return
if not force:
raise RuntimeError(_("Domain named %s already exists!") %
self.name)
try:
logging.debug("Explicitly replacing guest '%s'", self.name)
if vm.ID() != -1:
logging.info("Destroying guest '%s'", self.name)
vm.destroy()
logging.info("Undefining guest '%s'", self.name)
vm.undefine()
except libvirt.libvirtError, e:
raise RuntimeError(_("Could not remove old vm '%s': %s") %
(self.name, str(e)))
def start_install(self, meter=None, removeOld=None,
def start_install(self, meter=None,
dry=False, return_xml=False, noboot=False):
"""
Begin the guest install (stage1).
@ -662,7 +662,8 @@ class Guest(XMLBuilder):
return
# Remove existing VM if requested
self.remove_original_vm(removeOld)
self.check_vm_collision(self.conn, self.name,
do_remove=self.replace)
self.domain = self._create_guest(meter,
start_xml, final_xml, is_initial,