Merge ~barryprice/charm-canonical-livepatch/+git/canonical-livepatch-charm:master into ~livepatch-charmers/charm-canonical-livepatch:master
- Git
- lp:~barryprice/charm-canonical-livepatch/+git/canonical-livepatch-charm
- master
- Merge into master
Proposed by
Barry Price
Status: | Merged |
---|---|
Merged at revision: | d3c18df3d4a7d750a49a5cbaa8efd54597cc5cc0 |
Proposed branch: | ~barryprice/charm-canonical-livepatch/+git/canonical-livepatch-charm:master |
Merge into: | ~livepatch-charmers/charm-canonical-livepatch:master |
Diff against target: |
309 lines (+97/-38) 3 files modified
files/check_canonical-livepatch.py (+8/-6) reactive/canonical_livepatch.py (+63/-23) tests/99-autogen (+26/-9) |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Stuart Bishop (community) | Approve | ||
Review via email: mp+316306@code.launchpad.net |
Commit message
Description of the change
To post a comment you must log in.
Preview Diff
[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1 | diff --git a/files/check_canonical-livepatch.py b/files/check_canonical-livepatch.py | |||
2 | index 27c9f69..74c61e5 100644 | |||
3 | --- a/files/check_canonical-livepatch.py | |||
4 | +++ b/files/check_canonical-livepatch.py | |||
5 | @@ -7,17 +7,19 @@ import nagios_plugin | |||
6 | 7 | from subprocess import check_output | 7 | from subprocess import check_output |
7 | 8 | 8 | ||
8 | 9 | 9 | ||
10 | 10 | ################################################################################ | 10 | ############################################################################## |
11 | 11 | 11 | ||
12 | 12 | def check_package_installed(): | 12 | def check_package_installed(): |
13 | 13 | cmd = ['snap', 'list', 'canonical-livepatch'] | 13 | cmd = ['snap', 'list', 'canonical-livepatch'] |
14 | 14 | try: | 14 | try: |
15 | 15 | check_output(cmd, universal_newlines=True) | 15 | check_output(cmd, universal_newlines=True) |
16 | 16 | except: | 16 | except: |
18 | 17 | raise nagios_plugin.CriticalError("canonical-livepatch snap is not installed") | 17 | raise nagios_plugin.CriticalError( |
19 | 18 | "canonical-livepatch snap is not installed" | ||
20 | 19 | ) | ||
21 | 18 | 20 | ||
22 | 19 | 21 | ||
24 | 20 | ################################################################################ | 22 | ############################################################################## |
25 | 21 | 23 | ||
26 | 22 | def check_vmlinuz(): | 24 | def check_vmlinuz(): |
27 | 23 | vmlinuz = '/vmlinuz' | 25 | vmlinuz = '/vmlinuz' |
28 | @@ -39,7 +41,7 @@ def check_vmlinuz(): | |||
29 | 39 | return kernel_version.strip() | 41 | return kernel_version.strip() |
30 | 40 | 42 | ||
31 | 41 | 43 | ||
33 | 42 | ################################################################################ | 44 | ############################################################################## |
34 | 43 | 45 | ||
35 | 44 | def check_status(): | 46 | def check_status(): |
36 | 45 | livepatch_output_path = '/var/lib/nagios/canonical-livepatch-status.txt' | 47 | livepatch_output_path = '/var/lib/nagios/canonical-livepatch-status.txt' |
37 | @@ -67,7 +69,7 @@ def check_status(): | |||
38 | 67 | raise nagios_plugin.WarnError(wrn) | 69 | raise nagios_plugin.WarnError(wrn) |
39 | 68 | 70 | ||
40 | 69 | 71 | ||
42 | 70 | ################################################################################ | 72 | ############################################################################## |
43 | 71 | 73 | ||
44 | 72 | def main(): | 74 | def main(): |
45 | 73 | nagios_plugin.try_check(check_package_installed) | 75 | nagios_plugin.try_check(check_package_installed) |
46 | @@ -75,7 +77,7 @@ def main(): | |||
47 | 75 | print("OK - canonical-livepatch seems to be installed and working") | 77 | print("OK - canonical-livepatch seems to be installed and working") |
48 | 76 | 78 | ||
49 | 77 | 79 | ||
51 | 78 | ################################################################################ | 80 | ############################################################################## |
52 | 79 | 81 | ||
53 | 80 | if __name__ == "__main__": | 82 | if __name__ == "__main__": |
54 | 81 | main() | 83 | main() |
55 | diff --git a/reactive/canonical_livepatch.py b/reactive/canonical_livepatch.py | |||
56 | index 14f75ec..e511669 100644 | |||
57 | --- a/reactive/canonical_livepatch.py | |||
58 | +++ b/reactive/canonical_livepatch.py | |||
59 | @@ -26,11 +26,17 @@ def file_to_units(local_path, unit_path): | |||
60 | 26 | 26 | ||
61 | 27 | 27 | ||
62 | 28 | def wait_for_path(file_path, timeout=30): | 28 | def wait_for_path(file_path, timeout=30): |
64 | 29 | hookenv.log('Waiting for up to {}s for {}'.format(timeout, file_path)) | 29 | hookenv.log('Waiting for up to {}s for {}'.format( |
65 | 30 | timeout, | ||
66 | 31 | file_path) | ||
67 | 32 | ) | ||
68 | 30 | seconds_waited = 0 | 33 | seconds_waited = 0 |
69 | 31 | interval = 1 | 34 | interval = 1 |
70 | 32 | while not (path.exists(file_path)): | 35 | while not (path.exists(file_path)): |
72 | 33 | hookenv.log('{} does not exist - waiting {}s'.format(file_path, interval)) | 36 | hookenv.log('{} does not exist - waiting {}s'.format( |
73 | 37 | file_path, | ||
74 | 38 | interval) | ||
75 | 39 | ) | ||
76 | 34 | sleep(interval) | 40 | sleep(interval) |
77 | 35 | seconds_waited += interval | 41 | seconds_waited += interval |
78 | 36 | if seconds_waited >= timeout: | 42 | if seconds_waited >= timeout: |
79 | @@ -40,14 +46,18 @@ def wait_for_path(file_path, timeout=30): | |||
80 | 40 | 46 | ||
81 | 41 | 47 | ||
82 | 42 | def wait_for_livepatch(): | 48 | def wait_for_livepatch(): |
84 | 43 | wait_for_path('/var/snap/canonical-livepatch/current/livepatchd-priv.sock') | 49 | wait_for_path( |
85 | 50 | '/var/snap/canonical-livepatch/current/livepatchd-priv.sock' | ||
86 | 51 | ) | ||
87 | 44 | 52 | ||
88 | 45 | 53 | ||
89 | 46 | def unit_update(status=None, message=None): | 54 | def unit_update(status=None, message=None): |
90 | 47 | if status and message: | 55 | if status and message: |
91 | 48 | hookenv.status_set(status, message) | 56 | hookenv.status_set(status, message) |
92 | 49 | else: | 57 | else: |
94 | 50 | hookenv.status_set('active', 'Effective kernel {}'.format(get_equiv_kernel_version())) | 58 | hookenv.status_set('active', 'Effective kernel {}'.format( |
95 | 59 | get_equiv_kernel_version()) | ||
96 | 60 | ) | ||
97 | 51 | 61 | ||
98 | 52 | 62 | ||
99 | 53 | def get_equiv_kernel_version(): | 63 | def get_equiv_kernel_version(): |
100 | @@ -78,18 +88,25 @@ def get_equiv_kernel_version(): | |||
101 | 78 | return version_string | 88 | return version_string |
102 | 79 | 89 | ||
103 | 80 | 90 | ||
109 | 81 | def configure_proxies(http_proxy=None, https_proxy=None, no_proxy=None): | 91 | def get_yaml_if_exists(path_to_yaml): |
110 | 82 | config_path = '/var/snap/canonical-livepatch/common/config' | 92 | if path.exists(path_to_yaml): |
111 | 83 | 93 | with open(path_to_yaml, 'r') as stream: | |
107 | 84 | if path.exists(config_path): | ||
108 | 85 | with open(config_path, 'r') as stream: | ||
112 | 86 | try: | 94 | try: |
113 | 87 | conf_yaml = load(stream) | 95 | conf_yaml = load(stream) |
116 | 88 | except: | 96 | except Exception: |
117 | 89 | hookenv.log('Unable to load YAML from {}'.format(config_path)) | 97 | hookenv.log( |
118 | 98 | 'Unable to load YAML from {}'.format(path_to_yaml) | ||
119 | 99 | ) | ||
120 | 90 | else: | 100 | else: |
121 | 91 | conf_yaml = {} | 101 | conf_yaml = {} |
122 | 92 | 102 | ||
123 | 103 | return conf_yaml | ||
124 | 104 | |||
125 | 105 | |||
126 | 106 | def configure_proxies(http_proxy=None, https_proxy=None, no_proxy=None): | ||
127 | 107 | config_path = '/var/snap/canonical-livepatch/common/config' | ||
128 | 108 | conf_yaml = get_yaml_if_exists(config_path) | ||
129 | 109 | |||
130 | 93 | if conf_yaml.get('http-proxy'): | 110 | if conf_yaml.get('http-proxy'): |
131 | 94 | if http_proxy is None: | 111 | if http_proxy is None: |
132 | 95 | del conf_yaml['http-proxy'] | 112 | del conf_yaml['http-proxy'] |
133 | @@ -120,7 +137,11 @@ def configure_proxies(http_proxy=None, https_proxy=None, no_proxy=None): | |||
134 | 120 | 137 | ||
135 | 121 | def restart_livepatch(): | 138 | def restart_livepatch(): |
136 | 122 | # do a clean stop of the service first, 'restart' seems fragile right now | 139 | # do a clean stop of the service first, 'restart' seems fragile right now |
138 | 123 | cmd = ['/usr/sbin/service', 'snap.canonical-livepatch.canonical-livepatchd', 'stop'] | 140 | cmd = [ |
139 | 141 | '/usr/sbin/service', | ||
140 | 142 | 'snap.canonical-livepatch.canonical-livepatchd', | ||
141 | 143 | 'stop' | ||
142 | 144 | ] | ||
143 | 124 | hookenv.log('Stopping canonical-livepatch service') | 145 | hookenv.log('Stopping canonical-livepatch service') |
144 | 125 | try: | 146 | try: |
145 | 126 | check_call(cmd, universal_newlines=True) | 147 | check_call(cmd, universal_newlines=True) |
146 | @@ -128,12 +149,18 @@ def restart_livepatch(): | |||
147 | 128 | hookenv.log('Failed to stop the service') | 149 | hookenv.log('Failed to stop the service') |
148 | 129 | 150 | ||
149 | 130 | # and now try to start it again, it may fail the first time! | 151 | # and now try to start it again, it may fail the first time! |
151 | 131 | cmd = ['/usr/sbin/service', 'snap.canonical-livepatch.canonical-livepatchd', 'start'] | 152 | cmd = [ |
152 | 153 | '/usr/sbin/service', | ||
153 | 154 | 'snap.canonical-livepatch.canonical-livepatchd', | ||
154 | 155 | 'start' | ||
155 | 156 | ] | ||
156 | 132 | hookenv.log('Starting canonical-livepatch service') | 157 | hookenv.log('Starting canonical-livepatch service') |
157 | 133 | try: | 158 | try: |
158 | 134 | check_call(cmd, universal_newlines=True) | 159 | check_call(cmd, universal_newlines=True) |
159 | 135 | except CalledProcessError: | 160 | except CalledProcessError: |
161 | 136 | hookenv.log('Failed to start the service - waiting 5s and then trying again') | 161 | hookenv.log( |
162 | 162 | 'Failed to start the service - waiting 5s and then trying again' | ||
163 | 163 | ) | ||
164 | 137 | sleep(5) | 164 | sleep(5) |
165 | 138 | check_call(cmd, universal_newlines=True) | 165 | check_call(cmd, universal_newlines=True) |
166 | 139 | 166 | ||
167 | @@ -158,13 +185,20 @@ def update_key(): | |||
168 | 158 | if livepatch_key: | 185 | if livepatch_key: |
169 | 159 | # disable prior to enabling to work around LP#1628823 | 186 | # disable prior to enabling to work around LP#1628823 |
170 | 160 | cmd = ['/snap/bin/canonical-livepatch', 'disable'] | 187 | cmd = ['/snap/bin/canonical-livepatch', 'disable'] |
172 | 161 | hookenv.log('Disabling canonical-livepatch ahead of key change to work around LP#1628823') | 188 | hookenv.log( |
173 | 189 | 'Disabling canonical-livepatch ahead of key change ' | ||
174 | 190 | + 'to work around LP#1628823' | ||
175 | 191 | ) | ||
176 | 162 | try: | 192 | try: |
177 | 163 | check_output(cmd, universal_newlines=True) | 193 | check_output(cmd, universal_newlines=True) |
178 | 164 | except CalledProcessError as e: | 194 | except CalledProcessError as e: |
179 | 165 | hookenv.log('Unable to deactivate: {}'.format(str(e))) | 195 | hookenv.log('Unable to deactivate: {}'.format(str(e))) |
180 | 166 | 196 | ||
182 | 167 | cmd = ['/snap/bin/canonical-livepatch', 'enable', '{}'.format(livepatch_key.strip())] | 197 | cmd = [ |
183 | 198 | '/snap/bin/canonical-livepatch', | ||
184 | 199 | 'enable', | ||
185 | 200 | '{}'.format(livepatch_key.strip()) | ||
186 | 201 | ] | ||
187 | 168 | hookenv.log('Activating canonical-livepatch with your key') | 202 | hookenv.log('Activating canonical-livepatch with your key') |
188 | 169 | try: | 203 | try: |
189 | 170 | check_output(cmd, universal_newlines=True) | 204 | check_output(cmd, universal_newlines=True) |
190 | @@ -176,9 +210,14 @@ def update_key(): | |||
191 | 176 | set_state('canonical-livepatch.active') | 210 | set_state('canonical-livepatch.active') |
192 | 177 | unit_update() | 211 | unit_update() |
193 | 178 | else: | 212 | else: |
195 | 179 | hookenv.log('Unable to activate canonical-livepatch as no key has been set') | 213 | hookenv.log( |
196 | 214 | 'Unable to activate canonical-livepatch as no key has been set' | ||
197 | 215 | ) | ||
198 | 180 | remove_state('canonical-livepatch.active') | 216 | remove_state('canonical-livepatch.active') |
200 | 181 | unit_update('blocked', 'Service disabled, please set livepatch_key to activate') | 217 | unit_update( |
201 | 218 | 'blocked', | ||
202 | 219 | 'Service disabled, please set livepatch_key to activate' | ||
203 | 220 | ) | ||
204 | 182 | 221 | ||
205 | 183 | 222 | ||
206 | 184 | @when('canonical-livepatch.connected') | 223 | @when('canonical-livepatch.connected') |
207 | @@ -195,7 +234,7 @@ def update_livepatch_proxy(): | |||
208 | 195 | # Make sure the service is ready for us | 234 | # Make sure the service is ready for us |
209 | 196 | wait_for_livepatch() | 235 | wait_for_livepatch() |
210 | 197 | 236 | ||
212 | 198 | # force a key update, registration may have been blocked before proxy was set | 237 | # force a key update, we may have been blocked before proxy was set |
213 | 199 | update_key() | 238 | update_key() |
214 | 200 | 239 | ||
215 | 201 | 240 | ||
216 | @@ -224,10 +263,11 @@ def init_nagios_checks(nagios): | |||
217 | 224 | ) | 263 | ) |
218 | 225 | 264 | ||
219 | 226 | # use charmhelpers to create the check | 265 | # use charmhelpers to create the check |
224 | 227 | nrpe_setup.add_check('check_canonical-livepatch', | 266 | nrpe_setup.add_check( |
225 | 228 | 'Verify canonical-livepatch is working', | 267 | 'check_canonical-livepatch', |
226 | 229 | '/usr/lib/nagios/plugins/check_canonical-livepatch.py' | 268 | 'Verify canonical-livepatch is working', |
227 | 230 | ) | 269 | '/usr/lib/nagios/plugins/check_canonical-livepatch.py' |
228 | 270 | ) | ||
229 | 231 | 271 | ||
230 | 232 | nrpe_setup.write() | 272 | nrpe_setup.write() |
231 | 233 | 273 | ||
232 | diff --git a/tests/99-autogen b/tests/99-autogen | |||
233 | index c57bbf4..05824f0 100755 | |||
234 | --- a/tests/99-autogen | |||
235 | +++ b/tests/99-autogen | |||
236 | @@ -20,17 +20,29 @@ class TestDeployment(unittest.TestCase): | |||
237 | 20 | cls.deployment.add('nrpe') | 20 | cls.deployment.add('nrpe') |
238 | 21 | 21 | ||
239 | 22 | # relate subordinates to parent charm | 22 | # relate subordinates to parent charm |
242 | 23 | cls.deployment.relate('postgresql:juju-info', 'canonical-livepatch:juju-info') | 23 | cls.deployment.relate( |
243 | 24 | cls.deployment.relate('postgresql:nrpe-external-master', 'nrpe:nrpe-external-master') | 24 | 'postgresql:juju-info', |
244 | 25 | 'canonical-livepatch:juju-info' | ||
245 | 26 | ) | ||
246 | 27 | cls.deployment.relate( | ||
247 | 28 | 'postgresql:nrpe-external-master', | ||
248 | 29 | 'nrpe:nrpe-external-master' | ||
249 | 30 | ) | ||
250 | 25 | 31 | ||
251 | 26 | # relate livepatch to nrpe for its own nagios checks | 32 | # relate livepatch to nrpe for its own nagios checks |
253 | 27 | cls.deployment.relate('canonical-livepatch:nrpe-external-master', 'nrpe:nrpe-external-master') | 33 | cls.deployment.relate( |
254 | 34 | 'canonical-livepatch:nrpe-external-master', | ||
255 | 35 | 'nrpe:nrpe-external-master' | ||
256 | 36 | ) | ||
257 | 28 | 37 | ||
258 | 29 | try: | 38 | try: |
259 | 30 | cls.deployment.setup(timeout=3600) | 39 | cls.deployment.setup(timeout=3600) |
260 | 31 | cls.deployment.sentry.wait() | 40 | cls.deployment.sentry.wait() |
261 | 32 | except amulet.helpers.TimeoutError: | 41 | except amulet.helpers.TimeoutError: |
263 | 33 | amulet.raise_status(amulet.SKIP, msg="Environment wasn't stood up in time") | 42 | amulet.raise_status( |
264 | 43 | amulet.SKIP, | ||
265 | 44 | msg="Environment wasn't stood up in time" | ||
266 | 45 | ) | ||
267 | 34 | except: | 46 | except: |
268 | 35 | raise | 47 | raise |
269 | 36 | 48 | ||
270 | @@ -38,13 +50,15 @@ class TestDeployment(unittest.TestCase): | |||
271 | 38 | livepatch = self.deployment.sentry['canonical-livepatch'][0] | 50 | livepatch = self.deployment.sentry['canonical-livepatch'][0] |
272 | 39 | 51 | ||
273 | 40 | # verify the snap was installed | 52 | # verify the snap was installed |
275 | 41 | output, exit_code = livepatch.run('stat /snap/bin/canonical-livepatch') | 53 | output, exit_code = livepatch.run( |
276 | 54 | 'stat /snap/bin/canonical-livepatch' | ||
277 | 55 | ) | ||
278 | 42 | self.assertEqual(exit_code, 0) | 56 | self.assertEqual(exit_code, 0) |
279 | 43 | 57 | ||
280 | 44 | def test_status(self): | 58 | def test_status(self): |
281 | 45 | livepatch = self.deployment.sentry['canonical-livepatch'][0] | 59 | livepatch = self.deployment.sentry['canonical-livepatch'][0] |
282 | 46 | 60 | ||
284 | 47 | # run a status check - we expect this to return code 1 due to no access key | 61 | # run a status check - we expect this to return 1 due to no access key |
285 | 48 | output, exit_code = livepatch.run('sudo canonical-livepatch status') | 62 | output, exit_code = livepatch.run('sudo canonical-livepatch status') |
286 | 49 | self.assertEqual(exit_code, 1) | 63 | self.assertEqual(exit_code, 1) |
287 | 50 | 64 | ||
288 | @@ -75,8 +89,9 @@ class TestDeployment(unittest.TestCase): | |||
289 | 75 | 89 | ||
290 | 76 | # confirm it showed up in the right place | 90 | # confirm it showed up in the right place |
291 | 77 | output, exit_code = livepatch.run( | 91 | output, exit_code = livepatch.run( |
294 | 78 | 'grep {} /var/lib/nagios/export/service__*_check_canonical-livepatch.cfg'.format(test_context_name) | 92 | 'grep {} /var/lib/nagios/export/' |
295 | 79 | ) | 93 | + 'service__*_check_canonical-livepatch' |
296 | 94 | + '.cfg'.format(test_context_name)) | ||
297 | 80 | self.assertEqual(exit_code, 0) | 95 | self.assertEqual(exit_code, 0) |
298 | 81 | 96 | ||
299 | 82 | def test_nagios_servicegroup_change(self): | 97 | def test_nagios_servicegroup_change(self): |
300 | @@ -94,7 +109,9 @@ class TestDeployment(unittest.TestCase): | |||
301 | 94 | 109 | ||
302 | 95 | # confirm it showed up in the right place | 110 | # confirm it showed up in the right place |
303 | 96 | output, exit_code = livepatch.run( | 111 | output, exit_code = livepatch.run( |
305 | 97 | 'grep {} /var/lib/nagios/export/service__*_check_canonical-livepatch.cfg'.format(test_servicegroup_name) | 112 | 'grep {} /var/lib/nagios/export/' |
306 | 113 | + 'service__*_check_canonical-livepatch' | ||
307 | 114 | + '.cfg'.format(test_servicegroup_name) | ||
308 | 98 | ) | 115 | ) |
309 | 99 | self.assertEqual(exit_code, 0) | 116 | self.assertEqual(exit_code, 0) |
310 | 100 | 117 |
Looks like a pile of trivial lint fixes to me.
+1