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: 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)
Reviewer Review Type Date Requested Status
Stuart Bishop (community) Approve
Review via email: mp+316306@code.launchpad.net
To post a comment you must log in.
Revision history for this message
Stuart Bishop (stub) wrote :

Looks like a pile of trivial lint fixes to me.

+1

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
diff --git a/files/check_canonical-livepatch.py b/files/check_canonical-livepatch.py
index 27c9f69..74c61e5 100644
--- a/files/check_canonical-livepatch.py
+++ b/files/check_canonical-livepatch.py
@@ -7,17 +7,19 @@ import nagios_plugin
7from subprocess import check_output7from subprocess import check_output
88
99
10################################################################################10##############################################################################
1111
12def check_package_installed():12def check_package_installed():
13 cmd = ['snap', 'list', 'canonical-livepatch']13 cmd = ['snap', 'list', 'canonical-livepatch']
14 try:14 try:
15 check_output(cmd, universal_newlines=True)15 check_output(cmd, universal_newlines=True)
16 except:16 except:
17 raise nagios_plugin.CriticalError("canonical-livepatch snap is not installed")17 raise nagios_plugin.CriticalError(
18 "canonical-livepatch snap is not installed"
19 )
1820
1921
20################################################################################22##############################################################################
2123
22def check_vmlinuz():24def check_vmlinuz():
23 vmlinuz = '/vmlinuz'25 vmlinuz = '/vmlinuz'
@@ -39,7 +41,7 @@ def check_vmlinuz():
39 return kernel_version.strip()41 return kernel_version.strip()
4042
4143
42################################################################################44##############################################################################
4345
44def check_status():46def check_status():
45 livepatch_output_path = '/var/lib/nagios/canonical-livepatch-status.txt'47 livepatch_output_path = '/var/lib/nagios/canonical-livepatch-status.txt'
@@ -67,7 +69,7 @@ def check_status():
67 raise nagios_plugin.WarnError(wrn)69 raise nagios_plugin.WarnError(wrn)
6870
6971
70################################################################################72##############################################################################
7173
72def main():74def main():
73 nagios_plugin.try_check(check_package_installed)75 nagios_plugin.try_check(check_package_installed)
@@ -75,7 +77,7 @@ def main():
75 print("OK - canonical-livepatch seems to be installed and working")77 print("OK - canonical-livepatch seems to be installed and working")
7678
7779
78################################################################################80##############################################################################
7981
80if __name__ == "__main__":82if __name__ == "__main__":
81 main()83 main()
diff --git a/reactive/canonical_livepatch.py b/reactive/canonical_livepatch.py
index 14f75ec..e511669 100644
--- a/reactive/canonical_livepatch.py
+++ b/reactive/canonical_livepatch.py
@@ -26,11 +26,17 @@ def file_to_units(local_path, unit_path):
2626
2727
28def wait_for_path(file_path, timeout=30):28def wait_for_path(file_path, timeout=30):
29 hookenv.log('Waiting for up to {}s for {}'.format(timeout, file_path))29 hookenv.log('Waiting for up to {}s for {}'.format(
30 timeout,
31 file_path)
32 )
30 seconds_waited = 033 seconds_waited = 0
31 interval = 134 interval = 1
32 while not (path.exists(file_path)):35 while not (path.exists(file_path)):
33 hookenv.log('{} does not exist - waiting {}s'.format(file_path, interval))36 hookenv.log('{} does not exist - waiting {}s'.format(
37 file_path,
38 interval)
39 )
34 sleep(interval)40 sleep(interval)
35 seconds_waited += interval41 seconds_waited += interval
36 if seconds_waited >= timeout:42 if seconds_waited >= timeout:
@@ -40,14 +46,18 @@ def wait_for_path(file_path, timeout=30):
4046
4147
42def wait_for_livepatch():48def wait_for_livepatch():
43 wait_for_path('/var/snap/canonical-livepatch/current/livepatchd-priv.sock')49 wait_for_path(
50 '/var/snap/canonical-livepatch/current/livepatchd-priv.sock'
51 )
4452
4553
46def unit_update(status=None, message=None):54def unit_update(status=None, message=None):
47 if status and message:55 if status and message:
48 hookenv.status_set(status, message)56 hookenv.status_set(status, message)
49 else:57 else:
50 hookenv.status_set('active', 'Effective kernel {}'.format(get_equiv_kernel_version()))58 hookenv.status_set('active', 'Effective kernel {}'.format(
59 get_equiv_kernel_version())
60 )
5161
5262
53def get_equiv_kernel_version():63def get_equiv_kernel_version():
@@ -78,18 +88,25 @@ def get_equiv_kernel_version():
78 return version_string88 return version_string
7989
8090
81def configure_proxies(http_proxy=None, https_proxy=None, no_proxy=None):91def get_yaml_if_exists(path_to_yaml):
82 config_path = '/var/snap/canonical-livepatch/common/config'92 if path.exists(path_to_yaml):
8393 with open(path_to_yaml, 'r') as stream:
84 if path.exists(config_path):
85 with open(config_path, 'r') as stream:
86 try:94 try:
87 conf_yaml = load(stream)95 conf_yaml = load(stream)
88 except:96 except Exception:
89 hookenv.log('Unable to load YAML from {}'.format(config_path))97 hookenv.log(
98 'Unable to load YAML from {}'.format(path_to_yaml)
99 )
90 else:100 else:
91 conf_yaml = {}101 conf_yaml = {}
92102
103 return conf_yaml
104
105
106def configure_proxies(http_proxy=None, https_proxy=None, no_proxy=None):
107 config_path = '/var/snap/canonical-livepatch/common/config'
108 conf_yaml = get_yaml_if_exists(config_path)
109
93 if conf_yaml.get('http-proxy'):110 if conf_yaml.get('http-proxy'):
94 if http_proxy is None:111 if http_proxy is None:
95 del conf_yaml['http-proxy']112 del conf_yaml['http-proxy']
@@ -120,7 +137,11 @@ def configure_proxies(http_proxy=None, https_proxy=None, no_proxy=None):
120137
121def restart_livepatch():138def restart_livepatch():
122 # do a clean stop of the service first, 'restart' seems fragile right now139 # do a clean stop of the service first, 'restart' seems fragile right now
123 cmd = ['/usr/sbin/service', 'snap.canonical-livepatch.canonical-livepatchd', 'stop']140 cmd = [
141 '/usr/sbin/service',
142 'snap.canonical-livepatch.canonical-livepatchd',
143 'stop'
144 ]
124 hookenv.log('Stopping canonical-livepatch service')145 hookenv.log('Stopping canonical-livepatch service')
125 try:146 try:
126 check_call(cmd, universal_newlines=True)147 check_call(cmd, universal_newlines=True)
@@ -128,12 +149,18 @@ def restart_livepatch():
128 hookenv.log('Failed to stop the service')149 hookenv.log('Failed to stop the service')
129150
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!
131 cmd = ['/usr/sbin/service', 'snap.canonical-livepatch.canonical-livepatchd', 'start']152 cmd = [
153 '/usr/sbin/service',
154 'snap.canonical-livepatch.canonical-livepatchd',
155 'start'
156 ]
132 hookenv.log('Starting canonical-livepatch service')157 hookenv.log('Starting canonical-livepatch service')
133 try:158 try:
134 check_call(cmd, universal_newlines=True)159 check_call(cmd, universal_newlines=True)
135 except CalledProcessError:160 except CalledProcessError:
136 hookenv.log('Failed to start the service - waiting 5s and then trying again')161 hookenv.log(
162 'Failed to start the service - waiting 5s and then trying again'
163 )
137 sleep(5)164 sleep(5)
138 check_call(cmd, universal_newlines=True)165 check_call(cmd, universal_newlines=True)
139166
@@ -158,13 +185,20 @@ def update_key():
158 if livepatch_key:185 if livepatch_key:
159 # disable prior to enabling to work around LP#1628823186 # disable prior to enabling to work around LP#1628823
160 cmd = ['/snap/bin/canonical-livepatch', 'disable']187 cmd = ['/snap/bin/canonical-livepatch', 'disable']
161 hookenv.log('Disabling canonical-livepatch ahead of key change to work around LP#1628823')188 hookenv.log(
189 'Disabling canonical-livepatch ahead of key change '
190 + 'to work around LP#1628823'
191 )
162 try:192 try:
163 check_output(cmd, universal_newlines=True)193 check_output(cmd, universal_newlines=True)
164 except CalledProcessError as e:194 except CalledProcessError as e:
165 hookenv.log('Unable to deactivate: {}'.format(str(e)))195 hookenv.log('Unable to deactivate: {}'.format(str(e)))
166196
167 cmd = ['/snap/bin/canonical-livepatch', 'enable', '{}'.format(livepatch_key.strip())]197 cmd = [
198 '/snap/bin/canonical-livepatch',
199 'enable',
200 '{}'.format(livepatch_key.strip())
201 ]
168 hookenv.log('Activating canonical-livepatch with your key')202 hookenv.log('Activating canonical-livepatch with your key')
169 try:203 try:
170 check_output(cmd, universal_newlines=True)204 check_output(cmd, universal_newlines=True)
@@ -176,9 +210,14 @@ def update_key():
176 set_state('canonical-livepatch.active')210 set_state('canonical-livepatch.active')
177 unit_update()211 unit_update()
178 else:212 else:
179 hookenv.log('Unable to activate canonical-livepatch as no key has been set')213 hookenv.log(
214 'Unable to activate canonical-livepatch as no key has been set'
215 )
180 remove_state('canonical-livepatch.active')216 remove_state('canonical-livepatch.active')
181 unit_update('blocked', 'Service disabled, please set livepatch_key to activate')217 unit_update(
218 'blocked',
219 'Service disabled, please set livepatch_key to activate'
220 )
182221
183222
184@when('canonical-livepatch.connected')223@when('canonical-livepatch.connected')
@@ -195,7 +234,7 @@ def update_livepatch_proxy():
195 # Make sure the service is ready for us234 # Make sure the service is ready for us
196 wait_for_livepatch()235 wait_for_livepatch()
197236
198 # force a key update, registration may have been blocked before proxy was set237 # force a key update, we may have been blocked before proxy was set
199 update_key()238 update_key()
200239
201240
@@ -224,10 +263,11 @@ def init_nagios_checks(nagios):
224 )263 )
225264
226 # use charmhelpers to create the check265 # use charmhelpers to create the check
227 nrpe_setup.add_check('check_canonical-livepatch',266 nrpe_setup.add_check(
228 'Verify canonical-livepatch is working',267 'check_canonical-livepatch',
229 '/usr/lib/nagios/plugins/check_canonical-livepatch.py'268 'Verify canonical-livepatch is working',
230 )269 '/usr/lib/nagios/plugins/check_canonical-livepatch.py'
270 )
231271
232 nrpe_setup.write()272 nrpe_setup.write()
233273
diff --git a/tests/99-autogen b/tests/99-autogen
index c57bbf4..05824f0 100755
--- a/tests/99-autogen
+++ b/tests/99-autogen
@@ -20,17 +20,29 @@ class TestDeployment(unittest.TestCase):
20 cls.deployment.add('nrpe')20 cls.deployment.add('nrpe')
2121
22 # relate subordinates to parent charm22 # relate subordinates to parent charm
23 cls.deployment.relate('postgresql:juju-info', 'canonical-livepatch:juju-info')23 cls.deployment.relate(
24 cls.deployment.relate('postgresql:nrpe-external-master', 'nrpe:nrpe-external-master')24 'postgresql:juju-info',
25 'canonical-livepatch:juju-info'
26 )
27 cls.deployment.relate(
28 'postgresql:nrpe-external-master',
29 'nrpe:nrpe-external-master'
30 )
2531
26 # relate livepatch to nrpe for its own nagios checks32 # relate livepatch to nrpe for its own nagios checks
27 cls.deployment.relate('canonical-livepatch:nrpe-external-master', 'nrpe:nrpe-external-master')33 cls.deployment.relate(
34 'canonical-livepatch:nrpe-external-master',
35 'nrpe:nrpe-external-master'
36 )
2837
29 try:38 try:
30 cls.deployment.setup(timeout=3600)39 cls.deployment.setup(timeout=3600)
31 cls.deployment.sentry.wait()40 cls.deployment.sentry.wait()
32 except amulet.helpers.TimeoutError:41 except amulet.helpers.TimeoutError:
33 amulet.raise_status(amulet.SKIP, msg="Environment wasn't stood up in time")42 amulet.raise_status(
43 amulet.SKIP,
44 msg="Environment wasn't stood up in time"
45 )
34 except:46 except:
35 raise47 raise
3648
@@ -38,13 +50,15 @@ class TestDeployment(unittest.TestCase):
38 livepatch = self.deployment.sentry['canonical-livepatch'][0]50 livepatch = self.deployment.sentry['canonical-livepatch'][0]
3951
40 # verify the snap was installed52 # verify the snap was installed
41 output, exit_code = livepatch.run('stat /snap/bin/canonical-livepatch')53 output, exit_code = livepatch.run(
54 'stat /snap/bin/canonical-livepatch'
55 )
42 self.assertEqual(exit_code, 0)56 self.assertEqual(exit_code, 0)
4357
44 def test_status(self):58 def test_status(self):
45 livepatch = self.deployment.sentry['canonical-livepatch'][0]59 livepatch = self.deployment.sentry['canonical-livepatch'][0]
4660
47 # run a status check - we expect this to return code 1 due to no access key61 # run a status check - we expect this to return 1 due to no access key
48 output, exit_code = livepatch.run('sudo canonical-livepatch status')62 output, exit_code = livepatch.run('sudo canonical-livepatch status')
49 self.assertEqual(exit_code, 1)63 self.assertEqual(exit_code, 1)
5064
@@ -75,8 +89,9 @@ class TestDeployment(unittest.TestCase):
7589
76 # confirm it showed up in the right place90 # confirm it showed up in the right place
77 output, exit_code = livepatch.run(91 output, exit_code = livepatch.run(
78 'grep {} /var/lib/nagios/export/service__*_check_canonical-livepatch.cfg'.format(test_context_name)92 'grep {} /var/lib/nagios/export/'
79 )93 + 'service__*_check_canonical-livepatch'
94 + '.cfg'.format(test_context_name))
80 self.assertEqual(exit_code, 0)95 self.assertEqual(exit_code, 0)
8196
82 def test_nagios_servicegroup_change(self):97 def test_nagios_servicegroup_change(self):
@@ -94,7 +109,9 @@ class TestDeployment(unittest.TestCase):
94109
95 # confirm it showed up in the right place110 # confirm it showed up in the right place
96 output, exit_code = livepatch.run(111 output, exit_code = livepatch.run(
97 'grep {} /var/lib/nagios/export/service__*_check_canonical-livepatch.cfg'.format(test_servicegroup_name)112 'grep {} /var/lib/nagios/export/'
113 + 'service__*_check_canonical-livepatch'
114 + '.cfg'.format(test_servicegroup_name)
98 )115 )
99 self.assertEqual(exit_code, 0)116 self.assertEqual(exit_code, 0)
100117

Subscribers

People subscribed via source and target branches