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) basename += "-%s" % _pretty_arch(self._guest.os.arch)
force_num = False force_num = False
return virtinst.generatename.generate_name(basename, def cb(n):
self.conn.get_backend().lookupByName, 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, start_num=force_num and 1 or 2, force_num=force_num,
sep=not force_num and "-" or "") sep=not force_num and "-" or "")

View File

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

View File

@ -118,7 +118,9 @@ class vmmSnapshotNew(vmmGObjectUI):
def _reset_state(self): def _reset_state(self):
basename = "snapshot" 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( default_name = generatename.generate_name(
basename, cb, sep="", start_num=1, force_num=True) basename, cb, sep="", start_num=1, force_num=True)

View File

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

View File

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

View File

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

View File

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