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
1diff --git a/uvtool/libvirt/kvm.py b/uvtool/libvirt/kvm.py
2index 2fc30ef..218a7f0 100755
3--- a/uvtool/libvirt/kvm.py
4+++ b/uvtool/libvirt/kvm.py
5@@ -57,6 +57,7 @@ DEFAULT_TEMPLATE = '/usr/share/uvtool/libvirt/template.xml'
6 DEFAULT_REMOTE_WAIT_SCRIPT = '/usr/share/uvtool/libvirt/remote-wait.sh'
7 POOL_NAME = 'uvtool'
8
9+LIBVIRT_URI = 'qemu:///system'
10
11 class CLIError(Exception):
12 """An error that should be reflected back to the CLI user."""
13@@ -300,7 +301,7 @@ def create_cow_volume(backing_volume_name, new_volume_name, new_volume_size,
14 conn=None):
15
16 if conn is None:
17- conn = libvirt.open('qemu:///system')
18+ conn = libvirt.open(LIBVIRT_URI)
19
20 pool = conn.storagePoolLookupByName(POOL_NAME)
21 try:
22@@ -320,7 +321,7 @@ def create_cow_volume_by_path(backing_volume_path, new_volume_name,
23 """Create a new libvirt qcow2 volume backed by an existing volume path."""
24
25 if conn is None:
26- conn = libvirt.open('qemu:///system')
27+ conn = libvirt.open(LIBVIRT_URI)
28
29 pool = conn.storagePoolLookupByName(POOL_NAME)
30
31@@ -349,7 +350,7 @@ def get_boot_item_path(item_name, conn=None):
32 this helper gets that path (if one already exists) and
33 returns None if it doesn't."""
34 if conn is None:
35- conn = libvirt.open('qemu:///system')
36+ conn = libvirt.open(LIBVIRT_URI)
37
38 pool = conn.storagePoolLookupByName(POOL_NAME)
39 try:
40@@ -365,7 +366,7 @@ def remove_boot_item(hostname, item, conn=None):
41 item_name = get_boot_item_name(hostname, item)
42
43 if conn is None:
44- conn = libvirt.open('qemu:///system')
45+ conn = libvirt.open(LIBVIRT_URI)
46
47 pool = conn.storagePoolLookupByName(POOL_NAME)
48 try:
49@@ -383,7 +384,7 @@ def get_boot_item(backing_volume_name, hostname, item, conn=None):
50 inability to run as root."""
51
52 if conn is None:
53- conn = libvirt.open('qemu:///system')
54+ conn = libvirt.open(LIBVIRT_URI)
55
56 item_name = get_boot_item_name(hostname, item)
57 item_path = get_boot_item_path(item_name, conn)
58@@ -651,7 +652,7 @@ def create(
59 initrd=initrd,
60 )
61
62- conn = libvirt.open('qemu:///system')
63+ conn = libvirt.open(LIBVIRT_URI)
64 domain = conn.defineXML(xml)
65 if start:
66 try:
67@@ -686,7 +687,7 @@ def delete_domain_volumes(conn, domain):
68
69
70 def destroy(hostname):
71- conn = libvirt.open('qemu:///system')
72+ conn = libvirt.open(LIBVIRT_URI)
73 try:
74 domain = conn.lookupByName(hostname)
75 except libvirt.libvirtError as e:
76@@ -920,7 +921,7 @@ def main_destroy(parser, args):
77 def main_list(parser, args):
78 # TODO Implement this natively while taking into consideration the
79 # connection libvirt.
80- subprocess.check_call('virsh --connect qemu:///system -q list --all',
81+ subprocess.check_call('virsh --connect {} -q list --all'.format(LIBVIRT_URI),
82 shell=True
83 )
84
85@@ -986,7 +987,7 @@ def main_wait_remote(parser, args):
86
87
88 def main_wait(parser, args):
89- conn = libvirt.open('qemu:///system')
90+ conn = libvirt.open(LIBVIRT_URI)
91 domain = conn.lookupByName(args.name)
92 state = domain.state(0)[0]
93 if state != libvirt.VIR_DOMAIN_RUNNING:

Subscribers

People subscribed via source and target branches