Merge ~nick-moffitt/content-cache-charm:monitoring into content-cache-charm:master

Proposed by Nick Moffitt
Status: Merged
Approved by: Tom Haddon
Approved revision: a53c1dd216fec39c9f864a71cdb73c8529a9b89b
Merged at revision: ec50bf6d44c864aba264f1c58eb8ef4f698089b0
Proposed branch: ~nick-moffitt/content-cache-charm:monitoring
Merge into: content-cache-charm:master
Diff against target: 215 lines (+84/-4)
8 files modified
lib/haproxy.py (+15/-1)
metadata.yaml (+3/-0)
reactive/content_cache.py (+26/-1)
templates/haproxy_cfg.tmpl (+12/-0)
tests/unit/files/content_cache_rendered_haproxy_test_output.txt (+12/-0)
tests/unit/files/haproxy_config_rendered_test_output.txt (+12/-0)
tests/unit/test_content_cache.py (+2/-1)
tests/unit/test_haproxy.py (+2/-1)
Reviewer Review Type Date Requested Status
Tom Haddon Approve
Review via email: mp+366520@code.launchpad.net

Commit message

Add support for telegraf LP#1824568

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

One comment inline

Revision history for this message
Nick Moffitt (nick-moffitt) :
Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote :

This merge proposal is being monitored by mergebot. Change the status to Approved to merge.

Revision history for this message
Tom Haddon (mthaddon) wrote :

LGTM

review: Approve
Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote :

Change successfully merged at revision ec50bf6d44c864aba264f1c58eb8ef4f698089b0

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
diff --git a/lib/haproxy.py b/lib/haproxy.py
index 7ab1e74..38509a1 100644
--- a/lib/haproxy.py
+++ b/lib/haproxy.py
@@ -1,6 +1,7 @@
1import datetime1import datetime
2import multiprocessing2import multiprocessing
3import os3import os
4import re
45
5import jinja26import jinja2
67
@@ -24,6 +25,18 @@ class HAProxyConf:
24 def conf_file(self):25 def conf_file(self):
25 return os.path.join(self._conf_path, 'haproxy.cfg')26 return os.path.join(self._conf_path, 'haproxy.cfg')
2627
28 @property
29 def monitoring_password(self):
30 try:
31 with open(self.conf_file, 'r') as f:
32 m = re.search(r"stats auth\s+(\w+):(\w+)", f.read())
33 if m is not None:
34 return m.group(2)
35 else:
36 return None
37 except FileNotFoundError:
38 return None
39
27 def _generate_stanza_name(self, name, exclude=None):40 def _generate_stanza_name(self, name, exclude=None):
28 if exclude is None:41 if exclude is None:
29 exclude = []42 exclude = []
@@ -159,7 +172,7 @@ backend backend-{name}
159172
160 return rendered_output173 return rendered_output
161174
162 def render(self, config, num_procs=None):175 def render(self, config, num_procs=None, monitoring_password=None):
163 if not num_procs:176 if not num_procs:
164 num_procs = multiprocessing.cpu_count()177 num_procs = multiprocessing.cpu_count()
165178
@@ -170,6 +183,7 @@ backend backend-{name}
170 'listen': self.render_stanza_listen(config),183 'listen': self.render_stanza_listen(config),
171 'backend': self.render_stanza_backend(config),184 'backend': self.render_stanza_backend(config),
172 'num_procs': num_procs,185 'num_procs': num_procs,
186 'monitoring_password': monitoring_password or self.monitoring_password,
173 })187 })
174188
175 def write(self, content):189 def write(self, content):
diff --git a/metadata.yaml b/metadata.yaml
index ceba5c2..a3b5a01 100644
--- a/metadata.yaml
+++ b/metadata.yaml
@@ -17,3 +17,6 @@ provides:
17 nrpe-external-master:17 nrpe-external-master:
18 interface: nrpe-external-master18 interface: nrpe-external-master
19 scope: container19 scope: container
20 haproxy-statistics:
21 interface: statistics
22 scope: container
diff --git a/reactive/content_cache.py b/reactive/content_cache.py
index 460f120..01c2c95 100644
--- a/reactive/content_cache.py
+++ b/reactive/content_cache.py
@@ -7,6 +7,7 @@ from copy import deepcopy
77
8from charms import reactive8from charms import reactive
9from charms.layer import status9from charms.layer import status
10from charmhelpers import context
10from charmhelpers.core import hookenv, host11from charmhelpers.core import hookenv, host
11from charmhelpers.contrib.charmsupport import nrpe12from charmhelpers.contrib.charmsupport import nrpe
1213
@@ -33,6 +34,12 @@ def upgrade_charm():
33 reactive.set_flag('nrpe-external-master.available')34 reactive.set_flag('nrpe-external-master.available')
3435
3536
37@reactive.hook('haproxy-statistics-relation-joined', 'haproxy-statistics-relation-changed')
38def fire_stats_hook():
39 """We don't have an interface for this relation yet, so just fake it here."""
40 reactive.set_flag('haproxy-statistics.available')
41
42
36@reactive.when_not('content_cache.installed')43@reactive.when_not('content_cache.installed')
37def install():44def install():
38 reactive.clear_flag('content_cache.active')45 reactive.clear_flag('content_cache.active')
@@ -218,7 +225,12 @@ def configure_haproxy():
218 if not new_conf[cached_site]['locations']:225 if not new_conf[cached_site]['locations']:
219 new_conf[cached_site]['locations'][location] = new_cached_loc_conf226 new_conf[cached_site]['locations'][location] = new_cached_loc_conf
220227
221 if haproxy.write(haproxy.render(new_conf)):228 if haproxy.monitoring_password:
229 rendered_config = haproxy.render(new_conf, monitoring_password=haproxy.monitoring_password)
230 else:
231 rendered_config = haproxy.render(new_conf, monitoring_password=host.pwgen(length=20))
232
233 if haproxy.write(rendered_config):
222 service_start_or_restart('haproxy')234 service_start_or_restart('haproxy')
223235
224 reactive.set_flag('content_cache.haproxy.configured')236 reactive.set_flag('content_cache.haproxy.configured')
@@ -309,6 +321,19 @@ def configure_nagios():
309 reactive.set_flag('nagios-nrpe.configured')321 reactive.set_flag('nagios-nrpe.configured')
310322
311323
324@reactive.when('content_cache.haproxy.configured')
325@reactive.when('haproxy-statistics.available')
326def advertise_stats_endpoint():
327 rels = context.Relations()
328 password = HAProxy.HAProxyConf().monitoring_password
329
330 for rel in rels['haproxy-statistics'].values():
331 rel.local['enabled'] = "True"
332 rel.local['port'] = "10000"
333 rel.local['user'] = "haproxy"
334 rel.local['password'] = password
335
336
312def sites_from_config(sites_yaml, sites_secrets=None):337def sites_from_config(sites_yaml, sites_secrets=None):
313 conf = yaml.safe_load(sites_yaml)338 conf = yaml.safe_load(sites_yaml)
314 sites = interpolate_secrets(conf, sites_secrets)339 sites = interpolate_secrets(conf, sites_secrets)
diff --git a/templates/haproxy_cfg.tmpl b/templates/haproxy_cfg.tmpl
index 0786d83..df8f083 100644
--- a/templates/haproxy_cfg.tmpl
+++ b/templates/haproxy_cfg.tmpl
@@ -39,6 +39,18 @@ defaults
39 errorfile 503 /etc/haproxy/errors/503.http39 errorfile 503 /etc/haproxy/errors/503.http
40 errorfile 504 /etc/haproxy/errors/504.http40 errorfile 504 /etc/haproxy/errors/504.http
4141
42listen stats
43 bind 127.0.0.1:10000
44 acl allowed_cidr src 127.0.0.0/8
45 http-request deny unless allowed_cidr
46
47 mode http
48 stats enable
49 stats uri /
50 stats realm Haproxy\ Statistics
51 stats auth haproxy:{{monitoring_password}}
52 stats refresh 3
53
42{% for stanza in listen -%}54{% for stanza in listen -%}
43{{stanza}}55{{stanza}}
44{%- endfor -%}56{%- endfor -%}
diff --git a/tests/unit/files/content_cache_rendered_haproxy_test_output.txt b/tests/unit/files/content_cache_rendered_haproxy_test_output.txt
index 8a22a4b..f2077ab 100644
--- a/tests/unit/files/content_cache_rendered_haproxy_test_output.txt
+++ b/tests/unit/files/content_cache_rendered_haproxy_test_output.txt
@@ -39,6 +39,18 @@ defaults
39 errorfile 503 /etc/haproxy/errors/503.http39 errorfile 503 /etc/haproxy/errors/503.http
40 errorfile 504 /etc/haproxy/errors/504.http40 errorfile 504 /etc/haproxy/errors/504.http
4141
42listen stats
43 bind 127.0.0.1:10000
44 acl allowed_cidr src 127.0.0.0/8
45 http-request deny unless allowed_cidr
46
47 mode http
48 stats enable
49 stats uri /
50 stats realm Haproxy\ Statistics
51 stats auth haproxy:biometricsarenotsecret
52 stats refresh 3
53
4254
43listen combined-8055listen combined-80
44 bind 0.0.0.0:8056 bind 0.0.0.0:80
diff --git a/tests/unit/files/haproxy_config_rendered_test_output.txt b/tests/unit/files/haproxy_config_rendered_test_output.txt
index ad46463..5e27f5b 100644
--- a/tests/unit/files/haproxy_config_rendered_test_output.txt
+++ b/tests/unit/files/haproxy_config_rendered_test_output.txt
@@ -39,6 +39,18 @@ defaults
39 errorfile 503 /etc/haproxy/errors/503.http39 errorfile 503 /etc/haproxy/errors/503.http
40 errorfile 504 /etc/haproxy/errors/504.http40 errorfile 504 /etc/haproxy/errors/504.http
4141
42listen stats
43 bind 127.0.0.1:10000
44 acl allowed_cidr src 127.0.0.0/8
45 http-request deny unless allowed_cidr
46
47 mode http
48 stats enable
49 stats uri /
50 stats realm Haproxy\ Statistics
51 stats auth haproxy:biometricsarenotsecret
52 stats refresh 3
53
4254
43listen combined-8055listen combined-80
44 bind 0.0.0.0:8056 bind 0.0.0.0:80
diff --git a/tests/unit/test_content_cache.py b/tests/unit/test_content_cache.py
index 5228d45..e1390c0 100644
--- a/tests/unit/test_content_cache.py
+++ b/tests/unit/test_content_cache.py
@@ -221,7 +221,8 @@ site1.local:
221221
222 with mock.patch('lib.haproxy.HAProxyConf.conf_file', new_callable=mock.PropertyMock) as mock_conf_file:222 with mock.patch('lib.haproxy.HAProxyConf.conf_file', new_callable=mock.PropertyMock) as mock_conf_file:
223 mock_conf_file.return_value = os.path.join(self.tmpdir, 'haproxy.cfg')223 mock_conf_file.return_value = os.path.join(self.tmpdir, 'haproxy.cfg')
224 content_cache.configure_haproxy()224 with mock.patch('charmhelpers.core.host.pwgen', return_value="biometricsarenotsecret"):
225 content_cache.configure_haproxy()
225 self.assertFalse(service_start_or_restart.assert_called_with('haproxy'))226 self.assertFalse(service_start_or_restart.assert_called_with('haproxy'))
226227
227 # Again, this time should be no change so no need to restart HAProxy228 # Again, this time should be no change so no need to restart HAProxy
diff --git a/tests/unit/test_haproxy.py b/tests/unit/test_haproxy.py
index 978a4bf..3d46385 100644
--- a/tests/unit/test_haproxy.py
+++ b/tests/unit/test_haproxy.py
@@ -67,7 +67,8 @@ class TestLibHAProxy(unittest.TestCase):
67 haproxy = HAProxy.HAProxyConf(self.tmpdir)67 haproxy = HAProxy.HAProxyConf(self.tmpdir)
68 config = self.site_config68 config = self.site_config
69 num_procs = 469 num_procs = 4
70 self.assertTrue(haproxy.write(haproxy.render(config, num_procs)))70 password = "biometricsarenotsecret"
71 self.assertTrue(haproxy.write(haproxy.render(config, num_procs, monitoring_password=password)))
71 with open(haproxy.conf_file, 'r') as f:72 with open(haproxy.conf_file, 'r') as f:
72 new_conf = f.read()73 new_conf = f.read()
73 with open('tests/unit/files/haproxy_config_rendered_test_output.txt', 'r') as f:74 with open('tests/unit/files/haproxy_config_rendered_test_output.txt', 'r') as f:

Subscribers

People subscribed via source and target branches