Merge ~agherzan/uvtool:ag/libvirtcon into uvtool:main

Proposed by Andrei Gherzan
Status: Merged
Merged at revision: f5bc1e601ca8a6e091943508b0578ca1b428eb9d
Proposed branch: ~agherzan/uvtool:ag/libvirtcon
Merge into: uvtool:main
Diff against target: 93 lines (+10/-9)
1 file modified
uvtool/libvirt/kvm.py (+10/-9)
Reviewer Review Type Date Requested Status
Robie Basak Pending
Review via email: mp+435587@code.launchpad.net
To post a comment you must log in.
Revision history for this message
Robie Basak (racb) wrote :

Thanks!

Revision history for this message
Andrei Gherzan (agherzan) wrote :

Thanks for taking a look.

Revision history for this message
Robie Basak (racb) wrote :

FWIW, you probably noticed that the way this is done makes it painful to make the URI a configurable option. This diff reminded me that I thought about this when I wrote it. I imagined that I would need to add some kind of state object to carry user options and pass it around everywhere, to avoid a global variable. Maybe a class, or just a namedtuple or something. To get the initial version out the door I kicked that can down the road, and here we are many years later and you're the first person to notice :-)

Revision history for this message
Robie Basak (racb) wrote :

> I imagined that I would need to add some kind of state object to carry user options...

Or indeed something to carry a single persistent libvirt connection around the call stack, which would probably be better.

Revision history for this message
Andrei Gherzan (agherzan) wrote :

I agree that some sort of state would be useful here but I don't dislike too much the global variable approach either. It would get a bit convoluted because you would need to pass the state across the tool's invocations which makes it non-trivial. For the sake of the current functionality, I would say the current approach is reasonable. I've done this refactoring to force further developemnt to keep all the commands in sync in terms of the libvirt URI and avoid discrepancies as found with the list command.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
diff --git a/uvtool/libvirt/kvm.py b/uvtool/libvirt/kvm.py
index 2fc30ef..218a7f0 100755
--- a/uvtool/libvirt/kvm.py
+++ b/uvtool/libvirt/kvm.py
@@ -57,6 +57,7 @@ DEFAULT_TEMPLATE = '/usr/share/uvtool/libvirt/template.xml'
57DEFAULT_REMOTE_WAIT_SCRIPT = '/usr/share/uvtool/libvirt/remote-wait.sh'57DEFAULT_REMOTE_WAIT_SCRIPT = '/usr/share/uvtool/libvirt/remote-wait.sh'
58POOL_NAME = 'uvtool'58POOL_NAME = 'uvtool'
5959
60LIBVIRT_URI = 'qemu:///system'
6061
61class CLIError(Exception):62class CLIError(Exception):
62 """An error that should be reflected back to the CLI user."""63 """An error that should be reflected back to the CLI user."""
@@ -300,7 +301,7 @@ def create_cow_volume(backing_volume_name, new_volume_name, new_volume_size,
300 conn=None):301 conn=None):
301302
302 if conn is None:303 if conn is None:
303 conn = libvirt.open('qemu:///system')304 conn = libvirt.open(LIBVIRT_URI)
304305
305 pool = conn.storagePoolLookupByName(POOL_NAME)306 pool = conn.storagePoolLookupByName(POOL_NAME)
306 try:307 try:
@@ -320,7 +321,7 @@ def create_cow_volume_by_path(backing_volume_path, new_volume_name,
320 """Create a new libvirt qcow2 volume backed by an existing volume path."""321 """Create a new libvirt qcow2 volume backed by an existing volume path."""
321322
322 if conn is None:323 if conn is None:
323 conn = libvirt.open('qemu:///system')324 conn = libvirt.open(LIBVIRT_URI)
324325
325 pool = conn.storagePoolLookupByName(POOL_NAME)326 pool = conn.storagePoolLookupByName(POOL_NAME)
326327
@@ -349,7 +350,7 @@ def get_boot_item_path(item_name, conn=None):
349 this helper gets that path (if one already exists) and350 this helper gets that path (if one already exists) and
350 returns None if it doesn't."""351 returns None if it doesn't."""
351 if conn is None:352 if conn is None:
352 conn = libvirt.open('qemu:///system')353 conn = libvirt.open(LIBVIRT_URI)
353354
354 pool = conn.storagePoolLookupByName(POOL_NAME)355 pool = conn.storagePoolLookupByName(POOL_NAME)
355 try:356 try:
@@ -365,7 +366,7 @@ def remove_boot_item(hostname, item, conn=None):
365 item_name = get_boot_item_name(hostname, item)366 item_name = get_boot_item_name(hostname, item)
366367
367 if conn is None:368 if conn is None:
368 conn = libvirt.open('qemu:///system')369 conn = libvirt.open(LIBVIRT_URI)
369370
370 pool = conn.storagePoolLookupByName(POOL_NAME)371 pool = conn.storagePoolLookupByName(POOL_NAME)
371 try:372 try:
@@ -383,7 +384,7 @@ def get_boot_item(backing_volume_name, hostname, item, conn=None):
383 inability to run as root."""384 inability to run as root."""
384385
385 if conn is None:386 if conn is None:
386 conn = libvirt.open('qemu:///system')387 conn = libvirt.open(LIBVIRT_URI)
387388
388 item_name = get_boot_item_name(hostname, item)389 item_name = get_boot_item_name(hostname, item)
389 item_path = get_boot_item_path(item_name, conn)390 item_path = get_boot_item_path(item_name, conn)
@@ -651,7 +652,7 @@ def create(
651 initrd=initrd,652 initrd=initrd,
652 )653 )
653654
654 conn = libvirt.open('qemu:///system')655 conn = libvirt.open(LIBVIRT_URI)
655 domain = conn.defineXML(xml)656 domain = conn.defineXML(xml)
656 if start:657 if start:
657 try:658 try:
@@ -686,7 +687,7 @@ def delete_domain_volumes(conn, domain):
686687
687688
688def destroy(hostname):689def destroy(hostname):
689 conn = libvirt.open('qemu:///system')690 conn = libvirt.open(LIBVIRT_URI)
690 try:691 try:
691 domain = conn.lookupByName(hostname)692 domain = conn.lookupByName(hostname)
692 except libvirt.libvirtError as e:693 except libvirt.libvirtError as e:
@@ -920,7 +921,7 @@ def main_destroy(parser, args):
920def main_list(parser, args):921def main_list(parser, args):
921 # TODO Implement this natively while taking into consideration the922 # TODO Implement this natively while taking into consideration the
922 # connection libvirt.923 # connection libvirt.
923 subprocess.check_call('virsh --connect qemu:///system -q list --all',924 subprocess.check_call('virsh --connect {} -q list --all'.format(LIBVIRT_URI),
924 shell=True925 shell=True
925 )926 )
926927
@@ -986,7 +987,7 @@ def main_wait_remote(parser, args):
986987
987988
988def main_wait(parser, args):989def main_wait(parser, args):
989 conn = libvirt.open('qemu:///system')990 conn = libvirt.open(LIBVIRT_URI)
990 domain = conn.lookupByName(args.name)991 domain = conn.lookupByName(args.name)
991 state = domain.state(0)[0]992 state = domain.state(0)[0]
992 if state != libvirt.VIR_DOMAIN_RUNNING:993 if state != libvirt.VIR_DOMAIN_RUNNING:

Subscribers

People subscribed via source and target branches