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: ad6b19b1a62736d33de7e124eb6f4f42859fc03c
Proposed branch: ~barryprice/charm-canonical-livepatch/+git/canonical-livepatch-charm:master
Merge into: ~livepatch-charmers/charm-canonical-livepatch:master
Diff against target: 224 lines (+51/-67)
4 files modified
README.md (+0/-6)
config.yaml (+14/-2)
reactive/canonical_livepatch.py (+11/-57)
tests/99-autogen (+26/-2)
Reviewer Review Type Date Requested Status
Barry Price Abstain
Stuart Bishop (community) Approve
Review via email: mp+315749@code.launchpad.net
To post a comment you must log in.
Revision history for this message
Barry Price (barryprice) wrote :

Also upped the timeout for checks to an hour to cover sub-optimal internet connections and/or cloud environments.

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

Mostly looks good.

I think the interface:nrpe-external-master needs to stay though, commented on inline. I suspect when you tested you had nrpe relation hooks hanging around from a previous build.

review: Approve
Revision history for this message
Barry Price (barryprice) wrote :

Local tests are now passing once again, but I want to reinstate the amulet test for nagios_context/servicegroups now those have been reinstated. Will likely pick this up again Monday.

review: Needs Fixing
Revision history for this message
Barry Price (barryprice) wrote :

All tests are now passing.

Revision history for this message
Barry Price (barryprice) :
review: Abstain

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
diff --git a/README.md b/README.md
index 4cf16a5..8ac1e4a 100644
--- a/README.md
+++ b/README.md
@@ -24,10 +24,6 @@ Or via juju config (Juju 2.x)
2424
25 juju config canonical-livepatch livepatch_key=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx25 juju config canonical-livepatch livepatch_key=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
2626
27## Known Limitations and Issues
28
29The Nagios nrpe check integration code is a work in progress (but is fully functional)
30
31# Configuration27# Configuration
3228
33The livepatch_key setting must be configured in order for the software to function correctly - see above.29The livepatch_key setting must be configured in order for the software to function correctly - see above.
@@ -36,8 +32,6 @@ By default, the livepatch software will attempt to retrieve its patch data direc
3632
37Likewise, if you need to use a proxy server to access the Snap Store, snap_proxy can also be set to the address of your proxy server.33Likewise, if you need to use a proxy server to access the Snap Store, snap_proxy can also be set to the address of your proxy server.
3834
39We set the Nagios servicegroups to 'livepatch' by default, this can also be reconfigured to suit your needs.
40
41# Contact Information35# Contact Information
4236
43This charm is maintained here, by the livepatch-charmers team:37This charm is maintained here, by the livepatch-charmers team:
diff --git a/config.yaml b/config.yaml
index 40694b6..298611b 100644
--- a/config.yaml
+++ b/config.yaml
@@ -13,7 +13,19 @@ options:
13 description: |13 description: |
14 The address of a proxy server to use for livepatch traffic14 The address of a proxy server to use for livepatch traffic
15 e.g. http://proxy.example.com:312815 e.g. http://proxy.example.com:3128
16 nagios_context:
17 default: "juju"
18 type: string
19 description: |
20 Used by the nrpe-external-master subordinate charm.
21 A string that will be prepended to instance name to set the host name
22 in nagios. So for instance the hostname would be something like:
23 juju-myservice-0
24 If you're running multiple environments with the same services in them
25 this allows you to differentiate between them.
16 nagios_servicegroups:26 nagios_servicegroups:
17 default: "livepatch"27 default: ""
18 type: string28 type: string
19 description: "Nagios servicegroup(s) for alerts."29 description: >
30 A comma-separated list of nagios servicegroups.
31 If left empty, the nagios_context will be used as the servicegroup.
diff --git a/reactive/canonical_livepatch.py b/reactive/canonical_livepatch.py
index 102df07..4c12fb0 100644
--- a/reactive/canonical_livepatch.py
+++ b/reactive/canonical_livepatch.py
@@ -1,11 +1,11 @@
1from charms.reactive import when, when_not, set_state1from charms.reactive import when, when_not, set_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 nrpe
4from subprocess import check_call, check_output, CalledProcessError5from subprocess import check_call, check_output, CalledProcessError
5from time import sleep6from time import sleep
6from os import path7from os import path
7from yaml import load, dump8from yaml import load, dump
8from glob import glob
99
1010
11def file_to_units(local_path, unit_path):11def file_to_units(local_path, unit_path):
@@ -102,30 +102,6 @@ def restart_livepatch():
102 check_call(cmd, universal_newlines=True)102 check_call(cmd, universal_newlines=True)
103103
104104
105def update_nagios_servicedef(service_name, service_value):
106 for service_file in glob("/var/lib/nagios/export/service__*_check_canonical-livepatch.cfg"):
107 with open(service_file, "r+") as fh:
108 output = ''
109 for line in fh:
110 if line.startswith(' {} '.format(service_name)):
111 els = line.split(' ')
112 new_els = []
113 for e in els:
114 if e == '':
115 new_els.append(e)
116 elif e == service_name:
117 new_els.append(e)
118 else:
119 new_els.append(service_value)
120 new_line = ' '.join(new_els) + "\n"
121 output += new_line
122 else:
123 output += line
124 fh.seek(0)
125 fh.write(output)
126 fh.truncate()
127
128
129@when('snap.installed.canonical-livepatch')105@when('snap.installed.canonical-livepatch')
130@when_not('canonical-livepatch.connected')106@when_not('canonical-livepatch.connected')
131def canonical_livepatch_connect():107def canonical_livepatch_connect():
@@ -184,24 +160,14 @@ def update_livepatch_proxy():
184 hookenv.status_set('active', 'Ready')160 hookenv.status_set('active', 'Ready')
185161
186162
163@when('snap.installed.canonical-livepatch')
187@when('nrpe-external-master.available')164@when('nrpe-external-master.available')
188def init_nagios_checks(nagios):165def init_nagios_checks(nagios):
189 hookenv.status_set('maintenance', 'Configuring Nagios checks')166 hookenv.status_set('maintenance', 'Configuring Nagios checks')
190167
191 # We are a subordinate, try to find the primary hostname168 # Ask charmhelpers.contrib.charmsupport's nrpe to work out our hostname
192 nagios_hostname = None169 hostname = nrpe.get_nagios_hostname()
193 for hostfile in glob("/var/lib/nagios/export/host__*"):170 nrpe_setup = nrpe.NRPE(hostname=hostname)
194 with open(hostfile) as fh:
195 for line in fh:
196 if 'host_name' in line:
197 nagios_hostname = line.split()[1]
198
199 # Otherwise, fall back to our generated hostname, probably wrong!
200 if nagios_hostname is None:
201 nagios_hostname = hookenv.local_unit()
202
203 config = hookenv.config()
204 nagios_servicegroups = config.get('nagios_servicegroups')
205171
206 # install nagios support files172 # install nagios support files
207 file_to_units(173 file_to_units(
@@ -219,23 +185,11 @@ def init_nagios_checks(nagios):
219 '/var/lib/nagios/canonical-livepatch-status.txt',185 '/var/lib/nagios/canonical-livepatch-status.txt',
220 )186 )
221187
222 # use the interface to create the check188 # use charmhelpers to create the check
223 nagios.add_check(["/usr/lib/nagios/plugins/check_canonical-livepatch.py", ],189 nrpe_setup.add_check('check_canonical-livepatch',
224 name="check_canonical-livepatch",190 'Verify canonical-livepatch is working',
225 description="Verify canonical-livepatch is working",191 '/usr/lib/nagios/plugins/check_canonical-livepatch.py'
226 servicegroups=nagios_servicegroups,192 )
227 unit=nagios_hostname)
228
229 # clean up after https://github.com/cmars/nrpe-external-master-interface/issues/5
230 update_nagios_servicedef('host_name', nagios_hostname)
231193
194 nrpe_setup.write()
232 hookenv.status_set('active', 'Ready')195 hookenv.status_set('active', 'Ready')
233
234
235@when('config.changed.nagios_servicegroups')
236def update_nagios_servicegroups():
237 hookenv.status_set('maintenance', 'Configuring Nagios checks')
238
239 config = hookenv.config()
240 nagios_servicegroups = config.get('nagios_servicegroups')
241 update_nagios_servicedef('servicegroups', nagios_servicegroups)
diff --git a/tests/99-autogen b/tests/99-autogen
index 1527d4f..c57bbf4 100755
--- a/tests/99-autogen
+++ b/tests/99-autogen
@@ -2,6 +2,7 @@
22
3import amulet3import amulet
4import unittest4import unittest
5from time import sleep
56
67
7class TestDeployment(unittest.TestCase):8class TestDeployment(unittest.TestCase):
@@ -26,7 +27,7 @@ class TestDeployment(unittest.TestCase):
26 cls.deployment.relate('canonical-livepatch:nrpe-external-master', 'nrpe:nrpe-external-master')27 cls.deployment.relate('canonical-livepatch:nrpe-external-master', 'nrpe:nrpe-external-master')
2728
28 try:29 try:
29 cls.deployment.setup(timeout=900)30 cls.deployment.setup(timeout=3600)
30 cls.deployment.sentry.wait()31 cls.deployment.sentry.wait()
31 except amulet.helpers.TimeoutError:32 except amulet.helpers.TimeoutError:
32 amulet.raise_status(amulet.SKIP, msg="Environment wasn't stood up in time")33 amulet.raise_status(amulet.SKIP, msg="Environment wasn't stood up in time")
@@ -59,21 +60,44 @@ class TestDeployment(unittest.TestCase):
59 output, exit_code = livepatch.run('stat {}'.format(path))60 output, exit_code = livepatch.run('stat {}'.format(path))
60 self.assertEqual(exit_code, 0)61 self.assertEqual(exit_code, 0)
6162
63 def test_nagios_context_change(self):
64 livepatch = self.deployment.sentry['canonical-livepatch'][0]
65
66 test_context_name = 'amulet1'
67
68 # set context name
69 self.deployment.configure('canonical-livepatch', {
70 'nagios_context': test_context_name,
71 })
72
73 # nrpe updates can take a while
74 sleep(30)
75
76 # confirm it showed up in the right place
77 output, exit_code = livepatch.run(
78 'grep {} /var/lib/nagios/export/service__*_check_canonical-livepatch.cfg'.format(test_context_name)
79 )
80 self.assertEqual(exit_code, 0)
81
62 def test_nagios_servicegroup_change(self):82 def test_nagios_servicegroup_change(self):
63 livepatch = self.deployment.sentry['canonical-livepatch'][0]83 livepatch = self.deployment.sentry['canonical-livepatch'][0]
6484
65 test_servicegroup_name = 'amulet'85 test_servicegroup_name = 'amulet2'
6686
67 # set servicegroup name87 # set servicegroup name
68 self.deployment.configure('canonical-livepatch', {88 self.deployment.configure('canonical-livepatch', {
69 'nagios_servicegroups': test_servicegroup_name,89 'nagios_servicegroups': test_servicegroup_name,
70 })90 })
7191
92 # nrpe updates can take a while
93 sleep(30)
94
72 # confirm it showed up in the right place95 # confirm it showed up in the right place
73 output, exit_code = livepatch.run(96 output, exit_code = livepatch.run(
74 'grep {} /var/lib/nagios/export/service__*_check_canonical-livepatch.cfg'.format(test_servicegroup_name)97 'grep {} /var/lib/nagios/export/service__*_check_canonical-livepatch.cfg'.format(test_servicegroup_name)
75 )98 )
76 self.assertEqual(exit_code, 0)99 self.assertEqual(exit_code, 0)
77100
101
78if __name__ == '__main__':102if __name__ == '__main__':
79 unittest.main()103 unittest.main()

Subscribers

People subscribed via source and target branches