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
1diff --git a/reactive/canonical_livepatch.py b/reactive/canonical_livepatch.py
2index 5ff2774..49f07fb 100644
3--- a/reactive/canonical_livepatch.py
4+++ b/reactive/canonical_livepatch.py
5@@ -1,4 +1,4 @@
6-from charms.reactive import when, when_not, set_state
7+from charms.reactive import when, when_not, set_state, remove_state
8 from charmhelpers.core.host import write_file
9 from charmhelpers.core import hookenv
10 from charmhelpers.contrib.charmsupport import nrpe
11@@ -6,6 +6,7 @@ from subprocess import check_call, check_output, CalledProcessError
12 from time import sleep
13 from os import path
14 from yaml import load, dump
15+from platform import release
16
17
18 def file_to_units(local_path, unit_path):
19@@ -42,6 +43,39 @@ def wait_for_livepatch():
20 wait_for_path('/var/snap/canonical-livepatch/current/livepatchd-priv.sock')
21
22
23+def unit_update(status=None, message=None):
24+ if status and message:
25+ hookenv.status_set(status, message)
26+ else:
27+ hookenv.status_set('active', 'Effective kernel {}'.format(get_equiv_kernel_version()))
28+
29+
30+def get_equiv_kernel_version():
31+ # default to actual running kernel
32+ version_string = release()
33+ livepatch_status = ''
34+
35+ cmd = ['/snap/bin/canonical-livepatch', 'status']
36+ try:
37+ livepatch_status = check_output(cmd, universal_newlines=True)
38+ except CalledProcessError as e:
39+ hookenv.log('Unable to get status: {}'.format(str(e)))
40+
41+ # status will usually pass YAML (but not always!)
42+ try:
43+ status_yaml = load(livepatch_status)
44+ except:
45+ hookenv.log('Unable to parse status yaml')
46+
47+ # even if we got YAML, be paranoid
48+ try:
49+ version_string = status_yaml['kernel']
50+ except:
51+ hookenv.log('Unable to find kernel line in status yaml')
52+
53+ return version_string
54+
55+
56 def configure_proxies(http_proxy=None, https_proxy=None, no_proxy=None):
57 config_path = '/var/snap/canonical-livepatch/common/config'
58
59@@ -109,14 +143,13 @@ def canonical_livepatch_connect():
60 # Make sure the service is ready for us
61 wait_for_livepatch()
62
63- hookenv.status_set('active', 'Ready')
64 set_state('canonical-livepatch.connected')
65
66
67 @when('canonical-livepatch.connected')
68 @when('config.changed.livepatch_key')
69 def update_key():
70- hookenv.status_set('maintenance', 'Updating API key')
71+ unit_update('maintenance', 'Updating API key')
72 config = hookenv.config()
73
74 livepatch_key = config.get('livepatch_key')
75@@ -135,18 +168,21 @@ def update_key():
76 check_output(cmd, universal_newlines=True)
77 except CalledProcessError as e:
78 hookenv.log('Unable to activate: {}'.format(str(e)))
79- hookenv.status_set('blocked', 'Activation failed')
80+ remove_state('canonical-livepatch.active')
81+ unit_update('blocked', 'Activation failed')
82 else:
83- hookenv.status_set('active', 'Ready')
84+ set_state('canonical-livepatch.active')
85+ unit_update()
86 else:
87 hookenv.log('Unable to activate canonical-livepatch as no key has been set')
88- hookenv.status_set('blocked', 'Service disabled, please set livepatch_key to activate')
89+ remove_state('canonical-livepatch.active')
90+ unit_update('blocked', 'Service disabled, please set livepatch_key to activate')
91
92
93 @when('canonical-livepatch.connected')
94 @when('config.changed.livepatch_proxy')
95 def update_livepatch_proxy():
96- hookenv.status_set('maintenance', 'Configuring proxy servers')
97+ unit_update('maintenance', 'Configuring proxy servers')
98 config = hookenv.config()
99 proxy_url = config.get('livepatch_proxy')
100 configure_proxies(http_proxy=proxy_url, https_proxy=proxy_url)
101@@ -157,13 +193,13 @@ def update_livepatch_proxy():
102 # Make sure the service is ready for us
103 wait_for_livepatch()
104
105- hookenv.status_set('active', 'Ready')
106+ # force a key update, registration may have been blocked before proxy was set
107+ update_key()
108
109
110 @when('snap.installed.canonical-livepatch')
111 @when('nrpe-external-master.available')
112 def init_nagios_checks(nagios):
113- hookenv.status_set('maintenance', 'Configuring Nagios checks')
114
115 # Ask charmhelpers.contrib.charmsupport's nrpe to work out our hostname
116 hostname = nrpe.get_nagios_hostname()
117@@ -192,4 +228,9 @@ def init_nagios_checks(nagios):
118 )
119
120 nrpe_setup.write()
121- hookenv.status_set('active', 'Ready')
122+
123+
124+@when('snap.installed.canonical-livepatch')
125+@when('canonical-livepatch.active')
126+def update_kernel_version():
127+ unit_update()

Subscribers

People subscribed via source and target branches