Merge ~barryprice/charm-canonical-livepatch/+git/canonical-livepatch-charm:master into ~livepatch-charmers/charm-canonical-livepatch:master

Proposed by Barry Price
Status: Merged
Merged at revision: eb1c8e580767f3cfa328c46c76c6b3c0d473bd63
Proposed branch: ~barryprice/charm-canonical-livepatch/+git/canonical-livepatch-charm:master
Merge into: ~livepatch-charmers/charm-canonical-livepatch:master
Diff against target: 127 lines (+51/-10)
1 file modified
reactive/canonical_livepatch.py (+51/-10)
Reviewer Review Type Date Requested Status
Stuart Bishop (community) Approve
Review via email: mp+315878@code.launchpad.net

Description of the change

Adding an "Effective kernel" message to the unit status message to allow kernel version information to be visible at a glance.

To post a comment you must log in.
Revision history for this message
Barry Price (barryprice) wrote :

Amulet tests all pass

Revision history for this message
Stuart Bishop (stub) wrote :

A few style issues inline, but otherwise good.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
diff --git a/reactive/canonical_livepatch.py b/reactive/canonical_livepatch.py
index 5ff2774..49f07fb 100644
--- a/reactive/canonical_livepatch.py
+++ b/reactive/canonical_livepatch.py
@@ -1,4 +1,4 @@
1from charms.reactive import when, when_not, set_state1from charms.reactive import when, when_not, set_state, remove_state
2from charmhelpers.core.host import write_file2from charmhelpers.core.host import write_file
3from charmhelpers.core import hookenv3from charmhelpers.core import hookenv
4from charmhelpers.contrib.charmsupport import nrpe4from charmhelpers.contrib.charmsupport import nrpe
@@ -6,6 +6,7 @@ from subprocess import check_call, check_output, CalledProcessError
6from time import sleep6from time import sleep
7from os import path7from os import path
8from yaml import load, dump8from yaml import load, dump
9from platform import release
910
1011
11def file_to_units(local_path, unit_path):12def file_to_units(local_path, unit_path):
@@ -42,6 +43,39 @@ def wait_for_livepatch():
42 wait_for_path('/var/snap/canonical-livepatch/current/livepatchd-priv.sock')43 wait_for_path('/var/snap/canonical-livepatch/current/livepatchd-priv.sock')
4344
4445
46def unit_update(status=None, message=None):
47 if status and message:
48 hookenv.status_set(status, message)
49 else:
50 hookenv.status_set('active', 'Effective kernel {}'.format(get_equiv_kernel_version()))
51
52
53def get_equiv_kernel_version():
54 # default to actual running kernel
55 version_string = release()
56 livepatch_status = ''
57
58 cmd = ['/snap/bin/canonical-livepatch', 'status']
59 try:
60 livepatch_status = check_output(cmd, universal_newlines=True)
61 except CalledProcessError as e:
62 hookenv.log('Unable to get status: {}'.format(str(e)))
63
64 # status will usually pass YAML (but not always!)
65 try:
66 status_yaml = load(livepatch_status)
67 except:
68 hookenv.log('Unable to parse status yaml')
69
70 # even if we got YAML, be paranoid
71 try:
72 version_string = status_yaml['kernel']
73 except:
74 hookenv.log('Unable to find kernel line in status yaml')
75
76 return version_string
77
78
45def configure_proxies(http_proxy=None, https_proxy=None, no_proxy=None):79def configure_proxies(http_proxy=None, https_proxy=None, no_proxy=None):
46 config_path = '/var/snap/canonical-livepatch/common/config'80 config_path = '/var/snap/canonical-livepatch/common/config'
4781
@@ -109,14 +143,13 @@ def canonical_livepatch_connect():
109 # Make sure the service is ready for us143 # Make sure the service is ready for us
110 wait_for_livepatch()144 wait_for_livepatch()
111145
112 hookenv.status_set('active', 'Ready')
113 set_state('canonical-livepatch.connected')146 set_state('canonical-livepatch.connected')
114147
115148
116@when('canonical-livepatch.connected')149@when('canonical-livepatch.connected')
117@when('config.changed.livepatch_key')150@when('config.changed.livepatch_key')
118def update_key():151def update_key():
119 hookenv.status_set('maintenance', 'Updating API key')152 unit_update('maintenance', 'Updating API key')
120 config = hookenv.config()153 config = hookenv.config()
121154
122 livepatch_key = config.get('livepatch_key')155 livepatch_key = config.get('livepatch_key')
@@ -135,18 +168,21 @@ def update_key():
135 check_output(cmd, universal_newlines=True)168 check_output(cmd, universal_newlines=True)
136 except CalledProcessError as e:169 except CalledProcessError as e:
137 hookenv.log('Unable to activate: {}'.format(str(e)))170 hookenv.log('Unable to activate: {}'.format(str(e)))
138 hookenv.status_set('blocked', 'Activation failed')171 remove_state('canonical-livepatch.active')
172 unit_update('blocked', 'Activation failed')
139 else:173 else:
140 hookenv.status_set('active', 'Ready')174 set_state('canonical-livepatch.active')
175 unit_update()
141 else:176 else:
142 hookenv.log('Unable to activate canonical-livepatch as no key has been set')177 hookenv.log('Unable to activate canonical-livepatch as no key has been set')
143 hookenv.status_set('blocked', 'Service disabled, please set livepatch_key to activate')178 remove_state('canonical-livepatch.active')
179 unit_update('blocked', 'Service disabled, please set livepatch_key to activate')
144180
145181
146@when('canonical-livepatch.connected')182@when('canonical-livepatch.connected')
147@when('config.changed.livepatch_proxy')183@when('config.changed.livepatch_proxy')
148def update_livepatch_proxy():184def update_livepatch_proxy():
149 hookenv.status_set('maintenance', 'Configuring proxy servers')185 unit_update('maintenance', 'Configuring proxy servers')
150 config = hookenv.config()186 config = hookenv.config()
151 proxy_url = config.get('livepatch_proxy')187 proxy_url = config.get('livepatch_proxy')
152 configure_proxies(http_proxy=proxy_url, https_proxy=proxy_url)188 configure_proxies(http_proxy=proxy_url, https_proxy=proxy_url)
@@ -157,13 +193,13 @@ def update_livepatch_proxy():
157 # Make sure the service is ready for us193 # Make sure the service is ready for us
158 wait_for_livepatch()194 wait_for_livepatch()
159195
160 hookenv.status_set('active', 'Ready')196 # force a key update, registration may have been blocked before proxy was set
197 update_key()
161198
162199
163@when('snap.installed.canonical-livepatch')200@when('snap.installed.canonical-livepatch')
164@when('nrpe-external-master.available')201@when('nrpe-external-master.available')
165def init_nagios_checks(nagios):202def init_nagios_checks(nagios):
166 hookenv.status_set('maintenance', 'Configuring Nagios checks')
167203
168 # Ask charmhelpers.contrib.charmsupport's nrpe to work out our hostname204 # Ask charmhelpers.contrib.charmsupport's nrpe to work out our hostname
169 hostname = nrpe.get_nagios_hostname()205 hostname = nrpe.get_nagios_hostname()
@@ -192,4 +228,9 @@ def init_nagios_checks(nagios):
192 )228 )
193229
194 nrpe_setup.write()230 nrpe_setup.write()
195 hookenv.status_set('active', 'Ready')231
232
233@when('snap.installed.canonical-livepatch')
234@when('canonical-livepatch.active')
235def update_kernel_version():
236 unit_update()

Subscribers

People subscribed via source and target branches