Skip to content
This repository was archived by the owner on Mar 11, 2019. It is now read-only.

Commit 2402e03

Browse files
committed
Cache connection URI in objects
Rather than fetch it numerous times throughout our objects
1 parent 1b84a30 commit 2402e03

9 files changed

Lines changed: 64 additions & 48 deletions

virt-install

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ def check_option_collisions(options, guest):
408408
if options.cdrom or options.livecd:
409409
fail(_("Paravirtualized guests cannot install off cdrom media."))
410410

411-
if options.location and virtinst.util.is_uri_remote(guest.conn.getURI()):
411+
if options.location and guest.is_remote():
412412
fail(_("--location can not be specified for remote connections."))
413413

414414
if not options.location and options.extra:
@@ -564,14 +564,14 @@ def start_install(guest, continue_inst, options):
564564
virtinst.VirtualGraphics.TYPE_SPICE]:
565565
logging.debug("Launching virt-viewer for graphics type '%s'" %
566566
gtype)
567-
return vnc_console(dom, guest.conn.getURI())
567+
return vnc_console(dom, guest.get_uri())
568568
else:
569569
logging.debug("No viewer to launch for graphics type '%s'" %
570570
gtype)
571571
return None # SDL needs no viewer app
572572
else:
573573
logging.debug("Connecting to text console")
574-
return txt_console(dom, guest.conn.getURI())
574+
return txt_console(dom, guest.get_uri())
575575

576576
# There are two main cases we care about:
577577
#

virtinst/DistroInstaller.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,7 @@ def set_location(self, val):
9999
is_tuple = False
100100
validated = True
101101
self._location_is_path = True
102-
is_local = (not self.conn or
103-
not _util.is_uri_remote(self.conn.getURI()))
102+
is_local = (not self.conn or not self.is_remote())
104103

105104
# Basic validation
106105
if type(val) is not str and (type(val) is not tuple and len(val) != 2):
@@ -157,7 +156,7 @@ def set_location(self, val):
157156

158157
if (not self._location_is_path and val.startswith("nfs:") and not
159158
User.current().has_priv(User.PRIV_NFS_MOUNT,
160-
(self.conn and self.conn.getURI()))):
159+
(self.conn and self.get_uri()))):
161160
raise ValueError(_('Privilege is required for NFS installations'))
162161

163162
self._location = val

virtinst/VirtualDisk.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,7 +1064,7 @@ def __get_default_driver(self):
10641064
drvtype = self._driverType
10651065

10661066
if self.conn:
1067-
is_qemu = _util.is_qemu(self.conn)
1067+
is_qemu = self.is_qemu()
10681068
if is_qemu and not drvname:
10691069
drvname = self.DRIVER_QEMU
10701070

@@ -1166,7 +1166,7 @@ def __validate_params(self):
11661166
storage_capable = bool(self.conn and
11671167
_util.is_storage_capable(self.conn))
11681168

1169-
if self._is_remote():
1169+
if self.is_remote():
11701170
if not storage_capable:
11711171
raise ValueError(_("Connection doesn't support remote "
11721172
"storage."))
@@ -1436,7 +1436,7 @@ def _get_xml_config(self, disknode=None):
14361436
drvxml += " io='%s'" % self.driver_io
14371437

14381438
if drvxml and self.driver_name is None:
1439-
if _util.is_qemu(self.conn):
1439+
if self.is_qemu():
14401440
self.driver_name = "qemu"
14411441

14421442
if not self.driver_name is None:
@@ -1557,7 +1557,7 @@ def _support_selinux(self):
15571557
# our label guesses are built with svirt in mind
15581558
return False
15591559

1560-
elif self._is_remote():
1560+
elif self.is_remote():
15611561
return False
15621562

15631563
elif not _util.have_selinux():

virtinst/VirtualHostDevice.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import libvirt
2424

2525
from virtinst import support
26-
from virtinst import _util
2726
from virtinst import _virtinst as _
2827
from XMLBuilderDomain import _xml_property
2928

@@ -99,7 +98,7 @@ def __init__(self, conn, nodedev=None,
9998
return
10099

101100
self.managed = True
102-
if _util.get_uri_driver(self.conn.getURI()).lower() == "xen":
101+
if self.is_xen():
103102
self.managed = False
104103

105104

virtinst/VirtualNetworkInterface.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ def is_conflict_net(self, conn, mac=None):
315315
return (False, None)
316316

317317
# Not supported for remote connections yet
318-
if self._is_remote():
318+
if self.is_remote():
319319
return (False, None)
320320

321321
vms, inactive_vm = _util.fetch_all_guests(conn)

virtinst/XMLBuilderDomain.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -331,18 +331,15 @@ def __init__(self, conn=None, parsexml=None, parsexmlnode=None,
331331
@param parsexmlnode: Option xpathNode to use
332332
@param caps: Capabilities() instance
333333
"""
334-
if conn:
335-
if not isinstance(conn, libvirt.virConnect):
336-
raise ValueError(_("'conn' must be a virConnect instance"))
337-
self._conn = conn
338-
334+
self._conn = None
335+
self._conn_uri = None
336+
self.__remote = False
339337
self.__caps = None
340-
self.__remote = None
341338
self._xml_node = None
342339
self._xml_ctx = None
343340

344-
if self.conn:
345-
self.__remote = _util.is_uri_remote(self.conn.getURI())
341+
if conn:
342+
self.set_conn(conn)
346343

347344
if caps:
348345
if not isinstance(caps, CapabilitiesParser.Capabilities):
@@ -358,20 +355,26 @@ def set_conn(self, val):
358355
if not isinstance(val, libvirt.virConnect):
359356
raise ValueError(_("'conn' must be a virConnect instance."))
360357
self._conn = val
358+
self._conn_uri = self._conn.getURI()
359+
self.__remote = _util.is_uri_remote(self._conn_uri)
361360
conn = property(get_conn, set_conn)
362361

362+
def get_uri(self):
363+
return self._conn_uri
364+
363365
def _get_caps(self):
364366
if not self.__caps and self.conn:
365367
self.__caps = CapabilitiesParser.parse(self.conn.getCapabilities())
366368
return self.__caps
367369

368-
def _is_remote(self):
370+
def is_remote(self):
369371
return bool(self.__remote)
370-
371-
def _get_uri(self):
372-
if self.conn:
373-
return self.conn.getURI()
374-
return None
372+
def is_qemu(self):
373+
return _util.is_qemu(self.conn, self.get_uri())
374+
def is_qemu_system(self):
375+
return _util.is_qemu_system(self.conn, self.get_uri())
376+
def is_xen(self):
377+
return _util.is_xen(self.conn, self.get_uri())
375378

376379
def _check_bool(self, val, name):
377380
if val not in [True, False]:

virtinst/_util.py

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -399,8 +399,19 @@ def default_bridge2(conn=None):
399399

400400
return None
401401

402-
def is_qemu_system(conn):
403-
if not conn:
402+
def _get_uri_to_split(conn, uri):
403+
if not conn and not uri:
404+
return None
405+
406+
if type(conn) is str:
407+
uri = conn
408+
elif uri is None:
409+
uri = conn.getURI()
410+
return uri
411+
412+
def is_qemu_system(conn, uri=None):
413+
uri = _get_uri_to_split(conn, uri)
414+
if not uri:
404415
return False
405416

406417
(scheme, ignore, ignore,
@@ -409,18 +420,22 @@ def is_qemu_system(conn):
409420
return True
410421
return False
411422

412-
def is_qemu(conn):
413-
if not conn:
423+
def is_qemu(conn, uri=None):
424+
uri = _get_uri_to_split(conn, uri)
425+
if not uri:
414426
return False
415427

416-
if type(conn) is str:
417-
uri = conn
418-
else:
419-
uri = conn.getURI()
420-
421428
scheme = uri_split(uri)[0]
422429
return scheme.startswith("qemu")
423430

431+
def is_xen(conn, uri=None):
432+
uri = _get_uri_to_split(conn, uri)
433+
if not uri:
434+
return False
435+
436+
scheme = uri_split(uri)[0]
437+
return scheme.startswith("xen")
438+
424439
def parse_node_helper(xml, root_name, callback, exec_class=ValueError):
425440
"""
426441
Parse the passed XML, expecting root as root_name, and pass the

virtinst/cli.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ def nice_exit():
457457
sys.exit(0)
458458

459459
def virsh_start_cmd(guest):
460-
return ("virsh --connect %s start %s" % (guest.conn.getURI(), guest.name))
460+
return ("virsh --connect %s start %s" % (guest.get_uri(), guest.name))
461461

462462
def install_fail(guest):
463463
virshcmd = virsh_start_cmd(guest)
@@ -835,10 +835,10 @@ def get_cpuset(cpuset, mem, guest):
835835

836836
return
837837

838-
def _default_network_opts(conn):
838+
def _default_network_opts(guest):
839839
opts = ""
840-
if User.current().has_priv(User.PRIV_CREATE_NETWORK, conn.getURI()):
841-
net = _util.default_network(conn)
840+
if User.current().has_priv(User.PRIV_CREATE_NETWORK, guest.get_uri()):
841+
net = _util.default_network(guest.conn)
842842
opts = "%s=%s" % (net[0], net[1])
843843
else:
844844
opts = "user"
@@ -868,7 +868,7 @@ def padlist(l, padsize):
868868

869869
for idx in range(len(networks)):
870870
if networks[idx] is None:
871-
networks[idx] = _default_network_opts(guest.conn)
871+
networks[idx] = _default_network_opts(guest)
872872

873873
return networks, macs
874874

virtinst/support.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -318,15 +318,14 @@ def _local_lib_ver():
318318
return libvirt.getVersion()
319319

320320
# Version of libvirt library/daemon on the connection (could be remote)
321-
def _daemon_lib_ver(conn, force_version, minimum_libvirt_version):
321+
def _daemon_lib_ver(conn, uri, force_version, minimum_libvirt_version):
322322
# Always force the required version if it's after the version which
323323
# has getLibVersion
324324
if force_version or minimum_libvirt_version >= 7004:
325325
default_ret = 0
326326
else:
327327
default_ret = 100000000000
328328

329-
uri = conn.getURI()
330329
if not _util.is_uri_remote(uri):
331330
return _local_lib_ver()
332331

@@ -339,8 +338,8 @@ def _daemon_lib_ver(conn, force_version, minimum_libvirt_version):
339338
return conn.getLibVersion()
340339

341340
# Return the hypervisor version
342-
def _hv_ver(conn):
343-
drv_type = _util.get_uri_driver(conn.getURI())
341+
def _hv_ver(conn, uri):
342+
drv_type = _util.get_uri_driver(uri)
344343
args = ()
345344

346345
cmd = _get_command("getVersion", obj=conn)
@@ -396,7 +395,8 @@ def get_value(key):
396395
key_list.remove(key)
397396
return support_info.get(key)
398397

399-
drv_type = _util.get_uri_driver(conn.getURI())
398+
uri = conn.getURI()
399+
drv_type = _util.get_uri_driver(uri)
400400
is_rhel6 = _get_rhel6()
401401
force_version = get_value("force_version") or False
402402

@@ -418,9 +418,9 @@ def get_value(key):
418418
flag = get_value("flag")
419419

420420
actual_lib_ver = _local_lib_ver()
421-
actual_daemon_ver = _daemon_lib_ver(conn, force_version,
421+
actual_daemon_ver = _daemon_lib_ver(conn, uri, force_version,
422422
minimum_libvirt_version)
423-
actual_drv_ver = _hv_ver(conn)
423+
actual_drv_ver = _hv_ver(conn, uri)
424424

425425
# Make sure there are no keys left in the key_list. This will
426426
# ensure we didn't mistype anything above, or in the support_dict

0 commit comments

Comments
 (0)