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: db28bb9e14e9af53c98ab41669bf2703f21ea488
Proposed branch: ~barryprice/charm-canonical-livepatch/+git/canonical-livepatch-charm:master
Merge into: ~livepatch-charmers/charm-canonical-livepatch:master
Diff against target: 228 lines (+26/-89)
3 files modified
Makefile (+4/-0)
actions/actions.py (+1/-5)
reactive/canonical_livepatch.py (+21/-84)
Reviewer Review Type Date Requested Status
Stuart Bishop (community) Approve
Review via email: mp+332337@code.launchpad.net

Commit message

Reformat to 120 character lines

To post a comment you must log in.
Revision history for this message
Tom Haddon (mthaddon) wrote :

This looks fine, but I think it'd be worth encoding the 120 character line limit with a "make lint"

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

:-P

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
diff --git a/Makefile b/Makefile
0new file mode 1006440new file mode 100644
index 0000000..07b57c9
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,4 @@
1#!/usr/bin/make
2
3lint:
4 @flake8 --max-line-length=120
diff --git a/actions/actions.py b/actions/actions.py
index e9bb530..d618758 100755
--- a/actions/actions.py
+++ b/actions/actions.py
@@ -19,11 +19,7 @@ def activate():
19 except CalledProcessError as e:19 except CalledProcessError as e:
20 hookenv.log('Unable to deactivate: {}'.format(str(e)))20 hookenv.log('Unable to deactivate: {}'.format(str(e)))
21 # but let's soldier on...21 # but let's soldier on...
22 cmd = [22 cmd = ['/snap/bin/canonical-livepatch', 'enable', '{}'.format(livepatch_key.strip())]
23 '/snap/bin/canonical-livepatch',
24 'enable',
25 '{}'.format(livepatch_key.strip())
26 ]
27 try:23 try:
28 check_output(cmd, universal_newlines=True)24 check_output(cmd, universal_newlines=True)
29 except CalledProcessError as e:25 except CalledProcessError as e:
diff --git a/reactive/canonical_livepatch.py b/reactive/canonical_livepatch.py
index 8b39eab..e176c45 100644
--- a/reactive/canonical_livepatch.py
+++ b/reactive/canonical_livepatch.py
@@ -27,32 +27,21 @@ def file_to_units(local_path, unit_path):
2727
2828
29def wait_for_path(file_path, timeout=30):29def wait_for_path(file_path, timeout=30):
30 hookenv.log('Waiting for up to {}s for {}'.format(30 hookenv.log('Waiting for up to {}s for {}'.format(timeout, file_path))
31 timeout,
32 file_path)
33 )
34 seconds_waited = 031 seconds_waited = 0
35 interval = 132 interval = 1
36 while not (path.exists(file_path)):33 while not (path.exists(file_path)):
37 hookenv.log('{} does not exist - waiting {}s'.format(34 hookenv.log('{} does not exist - waiting {}s'.format(file_path, interval))
38 file_path,
39 interval)
40 )
41 sleep(interval)35 sleep(interval)
42 seconds_waited += interval36 seconds_waited += interval
43 if seconds_waited >= timeout:37 if seconds_waited >= timeout:
44 hookenv.log(38 hookenv.log('Giving up waiting for {}'.format(file_path), hookenv.ERROR)
45 'Giving up waiting for {}'.format(file_path),
46 hookenv.ERROR
47 )
48 return39 return
49 return40 return
5041
5142
52def wait_for_livepatch():43def wait_for_livepatch():
53 wait_for_path(44 wait_for_path('/var/snap/canonical-livepatch/current/livepatchd-priv.sock')
54 '/var/snap/canonical-livepatch/current/livepatchd-priv.sock'
55 )
5645
5746
58def unit_update(status=None, message=None):47def unit_update(status=None, message=None):
@@ -60,10 +49,7 @@ def unit_update(status=None, message=None):
60 hookenv.status_set(status, message)49 hookenv.status_set(status, message)
61 else:50 else:
62 patch_details = get_patch_details()51 patch_details = get_patch_details()
63 hookenv.status_set(52 hookenv.status_set('active', 'Running kernel {}, patchState: {}'.format(*patch_details))
64 'active',
65 'Running kernel {}, patchState: {}'.format(*patch_details)
66 )
6753
6854
69def get_patch_details():55def get_patch_details():
@@ -90,10 +76,7 @@ def get_patch_details():
90 kernel = k['kernel']76 kernel = k['kernel']
91 patch_state = k['livepatch']['patchState']77 patch_state = k['livepatch']['patchState']
92 except Exception:78 except Exception:
93 hookenv.log(79 hookenv.log('Unable to find patching details in status yaml', hookenv.ERROR)
94 'Unable to find patching details in status yaml',
95 hookenv.ERROR
96 )
97 return kernel, patch_state80 return kernel, patch_state
9881
99 return kernel, patch_state82 return kernel, patch_state
@@ -105,10 +88,7 @@ def get_yaml_if_exists(path_to_yaml):
105 try:88 try:
106 conf_yaml = load(stream)89 conf_yaml = load(stream)
107 except Exception:90 except Exception:
108 hookenv.log(91 hookenv.log('Unable to load YAML from {}'.format(path_to_yaml), hookenv.ERROR)
109 'Unable to load YAML from {}'.format(path_to_yaml),
110 hookenv.ERROR
111 )
112 else:92 else:
113 conf_yaml = {}93 conf_yaml = {}
11494
@@ -149,11 +129,7 @@ def configure_proxies(http_proxy=None, https_proxy=None, no_proxy=None):
149129
150def restart_livepatch():130def restart_livepatch():
151 # do a clean stop of the service first, 'restart' seems fragile right now131 # do a clean stop of the service first, 'restart' seems fragile right now
152 cmd = [132 cmd = ['/bin/systemctl', 'stop', 'snap.canonical-livepatch.canonical-livepatchd.service']
153 '/bin/systemctl',
154 'stop',
155 'snap.canonical-livepatch.canonical-livepatchd.service',
156 ]
157 hookenv.log('Stopping canonical-livepatch service')133 hookenv.log('Stopping canonical-livepatch service')
158 try:134 try:
159 check_call(cmd, universal_newlines=True)135 check_call(cmd, universal_newlines=True)
@@ -161,19 +137,12 @@ def restart_livepatch():
161 hookenv.log('Failed to stop the service', hookenv.ERROR)137 hookenv.log('Failed to stop the service', hookenv.ERROR)
162138
163 # and now try to start it again, it may fail the first time!139 # and now try to start it again, it may fail the first time!
164 cmd = [140 cmd = ['/bin/systemctl', 'start', 'snap.canonical-livepatch.canonical-livepatchd.service']
165 '/bin/systemctl',
166 'start',
167 'snap.canonical-livepatch.canonical-livepatchd.service',
168 ]
169 hookenv.log('Starting canonical-livepatch service')141 hookenv.log('Starting canonical-livepatch service')
170 try:142 try:
171 check_call(cmd, universal_newlines=True)143 check_call(cmd, universal_newlines=True)
172 except CalledProcessError:144 except CalledProcessError:
173 hookenv.log(145 hookenv.log('Failed to start the service - waiting 5s and then trying again', hookenv.ERROR)
174 'Failed to start the service - waiting 5s and then trying again',
175 hookenv.ERROR
176 )
177 sleep(5)146 sleep(5)
178 check_call(cmd, universal_newlines=True)147 check_call(cmd, universal_newlines=True)
179148
@@ -186,66 +155,37 @@ def activate_livepatch():
186 if livepatch_key:155 if livepatch_key:
187 # disable prior to enabling to work around LP#1628823156 # disable prior to enabling to work around LP#1628823
188 cmd = ['/snap/bin/canonical-livepatch', 'disable']157 cmd = ['/snap/bin/canonical-livepatch', 'disable']
189 hookenv.log(158 hookenv.log('Disabling canonical-livepatch ahead of key change to work around LP#1628823')
190 'Disabling canonical-livepatch ahead of key change '
191 'to work around LP#1628823'
192 )
193 try:159 try:
194 check_output(cmd, universal_newlines=True)160 check_output(cmd, universal_newlines=True)
195 except CalledProcessError as e:161 except CalledProcessError as e:
196 hookenv.log(162 hookenv.log('Unable to deactivate: {}'.format(str(e)), hookenv.ERROR)
197 'Unable to deactivate: {}'.format(str(e)),163
198 hookenv.ERROR164 cmd = ['/snap/bin/canonical-livepatch', 'enable', '{}'.format(livepatch_key.strip())]
199 )
200
201 cmd = [
202 '/snap/bin/canonical-livepatch',
203 'enable',
204 '{}'.format(livepatch_key.strip())
205 ]
206 hookenv.log('Activating canonical-livepatch with your key')165 hookenv.log('Activating canonical-livepatch with your key')
207 try:166 try:
208 check_output(cmd, universal_newlines=True)167 check_output(cmd, universal_newlines=True)
209 except CalledProcessError as e:168 except CalledProcessError as e:
210 hookenv.log(169 hookenv.log('Unable to activate: {}'.format(str(e)), hookenv.ERROR)
211 'Unable to activate: {}'.format(str(e)),
212 hookenv.ERROR
213 )
214 remove_state('canonical-livepatch.active')170 remove_state('canonical-livepatch.active')
215 unit_update('blocked', 'Activation failed')171 unit_update('blocked', 'Activation failed')
216 else:172 else:
217 set_state('canonical-livepatch.active')173 set_state('canonical-livepatch.active')
218 unit_update()174 unit_update()
219 else:175 else:
220 hookenv.log(176 hookenv.log('Unable to activate canonical-livepatch as no key has been set', hookenv.ERROR)
221 'Unable to activate canonical-livepatch as no key has been set',
222 hookenv.ERROR
223 )
224 remove_state('canonical-livepatch.active')177 remove_state('canonical-livepatch.active')
225 unit_update(178 unit_update('blocked', 'Service disabled, please set livepatch_key to activate')
226 'blocked',
227 'Service disabled, please set livepatch_key to activate'
228 )
229179
230180
231@when_not('snap.installed.canonical-livepatch')181@when_not('snap.installed.canonical-livepatch')
232def livepatch_supported():182def livepatch_supported():
233 arch = uname()[4]183 arch = uname()[4]
234 opts = layer.options('snap')184 opts = layer.options('snap')
235 supported_archs = opts['canonical-livepatch'].pop(185 supported_archs = opts['canonical-livepatch'].pop('supported-architectures', None)
236 'supported-architectures',
237 None
238 )
239 if supported_archs and arch not in supported_archs:186 if supported_archs and arch not in supported_archs:
240 hookenv.log(187 hookenv.log('Livepatch does not currently support {} architecture'.format(arch))
241 'Livepatch does not currently support {} architecture'.format(188 unit_update('blocked', 'Architecture {} is not supported by livepatch'.format(arch))
242 arch
243 )
244 )
245 unit_update(
246 'blocked',
247 'Architecture {} is not supported by livepatch'.format(arch)
248 )
249 if is_container():189 if is_container():
250 hookenv.log('OS container detected, livepatch is not needed')190 hookenv.log('OS container detected, livepatch is not needed')
251 unit_update('blocked', 'Livepatch is not needed in OS containers')191 unit_update('blocked', 'Livepatch is not needed in OS containers')
@@ -270,10 +210,7 @@ def canonical_livepatch_connect():
270 # Make sure the service is ready for us210 # Make sure the service is ready for us
271 wait_for_livepatch()211 wait_for_livepatch()
272 set_state('canonical-livepatch.connected')212 set_state('canonical-livepatch.connected')
273 unit_update(213 unit_update('blocked', 'Service disabled, please set livepatch_key to activate')
274 'blocked',
275 'Service disabled, please set livepatch_key to activate'
276 )
277214
278215
279@when('canonical-livepatch.connected')216@when('canonical-livepatch.connected')

Subscribers

People subscribed via source and target branches