generatename: Move libvirt collision handling to callers

Make every caller pass an explicit cb that handles libvirt
collision processing. Makes it easier to see exactly what is going
on at the call impls
This commit is contained in:
Cole Robinson 2019-06-11 10:05:15 -04:00
parent 4cdf2e4a78
commit 387614c641
7 changed files with 25 additions and 27 deletions

View File

@ -1494,8 +1494,10 @@ class vmmCreate(vmmGObjectUI):
basename += "-%s" % _pretty_arch(self._guest.os.arch)
force_num = False
return virtinst.generatename.generate_name(basename,
self.conn.get_backend().lookupByName,
def cb(n):
return virtinst.generatename.check_libvirt_collision(
self.conn.get_backend().lookupByName, n)
return virtinst.generatename.generate_name(basename, cb,
start_num=force_num and 1 or 2, force_num=force_num,
sep=not force_num and "-" or "")

View File

@ -114,8 +114,10 @@ class vmmCreateNetwork(vmmGObjectUI):
def reset_state(self):
basename = "network"
default_name = generatename.generate_name(
basename, self.conn.get_backend().networkLookupByName)
def cb(n):
return generatename.check_libvirt_collision(
self.conn.get_backend().networkLookupByName, n)
default_name = generatename.generate_name(basename, cb)
self.widget("net-name").set_text(default_name)
self.widget("net-dns-use-netname").set_active(True)

View File

@ -118,7 +118,9 @@ class vmmSnapshotNew(vmmGObjectUI):
def _reset_state(self):
basename = "snapshot"
cb = self.vm.get_backend().snapshotLookupByName
def cb(n):
return generatename.check_libvirt_collision(
self.vm.get_backend().snapshotLookupByName, n)
default_name = generatename.generate_name(
basename, cb, sep="", start_num=1, force_num=True)

View File

@ -493,11 +493,9 @@ class Cloner(object):
clonebase = newname
clonebase = os.path.join(dirname, clonebase)
return generatename.generate_name(
clonebase,
lambda p: DeviceDisk.path_definitely_exists(self.conn, p),
suffix,
lib_collision=False)
def cb(p):
return DeviceDisk.path_definitely_exists(self.conn, p)
return generatename.generate_name(clonebase, cb, suffix=suffix)
def generate_clone_name(self):
# If the orig name is "foo-clone", we don't want the clone to be
@ -512,9 +510,11 @@ class Cloner(object):
start_num = int(str(num_match.group()))
basename = basename.replace(match.group(), "")
def cb(n):
return generatename.check_libvirt_collision(
self.conn.lookupByName, n)
basename = basename + "-clone"
return generatename.generate_name(basename,
self.conn.lookupByName,
return generatename.generate_name(basename, cb,
sep="", start_num=start_num)

View File

@ -8,7 +8,7 @@
import libvirt
def libvirt_collision(collision_cb, val):
def check_libvirt_collision(collision_cb, val):
"""
Run the passed collision function with val as the only argument:
If libvirtError is raised, return False
@ -24,7 +24,7 @@ def libvirt_collision(collision_cb, val):
return check
def generate_name(base, collision_cb, suffix="", lib_collision=True,
def generate_name(base, collision_cb, suffix="",
start_num=1, sep="-", force_num=False):
"""
Generate a new name from the passed base string, verifying it doesn't
@ -42,8 +42,6 @@ def generate_name(base, collision_cb, suffix="", lib_collision=True,
:param base: The base string to use for the name (e.g. "my-orig-vm-clone")
:param collision_cb: A callback function to check for collision,
receives the generated name as its only arg
:param lib_collision: If true, the collision_cb is not a boolean function,
and instead throws a libvirt error on failure
:param start_num: The number to start at for generating non colliding names
:param sep: The separator to use between the basename and the
generated number (default is "-")
@ -51,12 +49,6 @@ def generate_name(base, collision_cb, suffix="", lib_collision=True,
"""
base = str(base)
def collide(n):
if lib_collision:
return libvirt_collision(collision_cb, n)
else:
return collision_cb(n)
numrange = list(range(start_num, start_num + 100000))
if not force_num:
numrange = [None] + numrange
@ -68,7 +60,7 @@ def generate_name(base, collision_cb, suffix="", lib_collision=True,
tryname += ("%s%d" % (sep, i))
tryname += suffix
if not collide(tryname):
if not collision_cb(tryname):
ret = tryname
break

View File

@ -131,7 +131,8 @@ class Guest(XMLBuilder):
for ignore in range(256):
uuid = _randomUUID()
if not generatename.libvirt_collision(conn.lookupByUUID, uuid):
if not generatename.check_libvirt_collision(
conn.lookupByUUID, uuid):
return uuid
logging.error("Failed to generate non-conflicting UUID")

View File

@ -248,8 +248,6 @@ class StoragePool(_StorageObject):
if pool.name == name:
return True
return False
kwargs["lib_collision"] = False
return generatename.generate_name(basename, cb, **kwargs)
@staticmethod
@ -560,7 +558,8 @@ class StorageVolume(_StorageObject):
def cb(tryname):
if tryname in collidelist:
return True
return pool_object.storageVolLookupByName(tryname)
return generatename.check_libvirt_collision(
pool_object.storageVolLookupByName, tryname)
StoragePool.ensure_pool_is_running(pool_object, refresh=True)
return generatename.generate_name(basename, cb, **kwargs)