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
1diff --git a/lib/haproxy.py b/lib/haproxy.py
2index 7ab1e74..38509a1 100644
3--- a/lib/haproxy.py
4+++ b/lib/haproxy.py
5@@ -1,6 +1,7 @@
6 import datetime
7 import multiprocessing
8 import os
9+import re
10
11 import jinja2
12
13@@ -24,6 +25,18 @@ class HAProxyConf:
14 def conf_file(self):
15 return os.path.join(self._conf_path, 'haproxy.cfg')
16
17+ @property
18+ def monitoring_password(self):
19+ try:
20+ with open(self.conf_file, 'r') as f:
21+ m = re.search(r"stats auth\s+(\w+):(\w+)", f.read())
22+ if m is not None:
23+ return m.group(2)
24+ else:
25+ return None
26+ except FileNotFoundError:
27+ return None
28+
29 def _generate_stanza_name(self, name, exclude=None):
30 if exclude is None:
31 exclude = []
32@@ -159,7 +172,7 @@ backend backend-{name}
33
34 return rendered_output
35
36- def render(self, config, num_procs=None):
37+ def render(self, config, num_procs=None, monitoring_password=None):
38 if not num_procs:
39 num_procs = multiprocessing.cpu_count()
40
41@@ -170,6 +183,7 @@ backend backend-{name}
42 'listen': self.render_stanza_listen(config),
43 'backend': self.render_stanza_backend(config),
44 'num_procs': num_procs,
45+ 'monitoring_password': monitoring_password or self.monitoring_password,
46 })
47
48 def write(self, content):
49diff --git a/metadata.yaml b/metadata.yaml
50index ceba5c2..a3b5a01 100644
51--- a/metadata.yaml
52+++ b/metadata.yaml
53@@ -17,3 +17,6 @@ provides:
54 nrpe-external-master:
55 interface: nrpe-external-master
56 scope: container
57+ haproxy-statistics:
58+ interface: statistics
59+ scope: container
60diff --git a/reactive/content_cache.py b/reactive/content_cache.py
61index 460f120..01c2c95 100644
62--- a/reactive/content_cache.py
63+++ b/reactive/content_cache.py
64@@ -7,6 +7,7 @@ from copy import deepcopy
65
66 from charms import reactive
67 from charms.layer import status
68+from charmhelpers import context
69 from charmhelpers.core import hookenv, host
70 from charmhelpers.contrib.charmsupport import nrpe
71
72@@ -33,6 +34,12 @@ def upgrade_charm():
73 reactive.set_flag('nrpe-external-master.available')
74
75
76+@reactive.hook('haproxy-statistics-relation-joined', 'haproxy-statistics-relation-changed')
77+def fire_stats_hook():
78+ """We don't have an interface for this relation yet, so just fake it here."""
79+ reactive.set_flag('haproxy-statistics.available')
80+
81+
82 @reactive.when_not('content_cache.installed')
83 def install():
84 reactive.clear_flag('content_cache.active')
85@@ -218,7 +225,12 @@ def configure_haproxy():
86 if not new_conf[cached_site]['locations']:
87 new_conf[cached_site]['locations'][location] = new_cached_loc_conf
88
89- if haproxy.write(haproxy.render(new_conf)):
90+ if haproxy.monitoring_password:
91+ rendered_config = haproxy.render(new_conf, monitoring_password=haproxy.monitoring_password)
92+ else:
93+ rendered_config = haproxy.render(new_conf, monitoring_password=host.pwgen(length=20))
94+
95+ if haproxy.write(rendered_config):
96 service_start_or_restart('haproxy')
97
98 reactive.set_flag('content_cache.haproxy.configured')
99@@ -309,6 +321,19 @@ def configure_nagios():
100 reactive.set_flag('nagios-nrpe.configured')
101
102
103+@reactive.when('content_cache.haproxy.configured')
104+@reactive.when('haproxy-statistics.available')
105+def advertise_stats_endpoint():
106+ rels = context.Relations()
107+ password = HAProxy.HAProxyConf().monitoring_password
108+
109+ for rel in rels['haproxy-statistics'].values():
110+ rel.local['enabled'] = "True"
111+ rel.local['port'] = "10000"
112+ rel.local['user'] = "haproxy"
113+ rel.local['password'] = password
114+
115+
116 def sites_from_config(sites_yaml, sites_secrets=None):
117 conf = yaml.safe_load(sites_yaml)
118 sites = interpolate_secrets(conf, sites_secrets)
119diff --git a/templates/haproxy_cfg.tmpl b/templates/haproxy_cfg.tmpl
120index 0786d83..df8f083 100644
121--- a/templates/haproxy_cfg.tmpl
122+++ b/templates/haproxy_cfg.tmpl
123@@ -39,6 +39,18 @@ defaults
124 errorfile 503 /etc/haproxy/errors/503.http
125 errorfile 504 /etc/haproxy/errors/504.http
126
127+listen stats
128+ bind 127.0.0.1:10000
129+ acl allowed_cidr src 127.0.0.0/8
130+ http-request deny unless allowed_cidr
131+
132+ mode http
133+ stats enable
134+ stats uri /
135+ stats realm Haproxy\ Statistics
136+ stats auth haproxy:{{monitoring_password}}
137+ stats refresh 3
138+
139 {% for stanza in listen -%}
140 {{stanza}}
141 {%- endfor -%}
142diff --git a/tests/unit/files/content_cache_rendered_haproxy_test_output.txt b/tests/unit/files/content_cache_rendered_haproxy_test_output.txt
143index 8a22a4b..f2077ab 100644
144--- a/tests/unit/files/content_cache_rendered_haproxy_test_output.txt
145+++ b/tests/unit/files/content_cache_rendered_haproxy_test_output.txt
146@@ -39,6 +39,18 @@ defaults
147 errorfile 503 /etc/haproxy/errors/503.http
148 errorfile 504 /etc/haproxy/errors/504.http
149
150+listen stats
151+ bind 127.0.0.1:10000
152+ acl allowed_cidr src 127.0.0.0/8
153+ http-request deny unless allowed_cidr
154+
155+ mode http
156+ stats enable
157+ stats uri /
158+ stats realm Haproxy\ Statistics
159+ stats auth haproxy:biometricsarenotsecret
160+ stats refresh 3
161+
162
163 listen combined-80
164 bind 0.0.0.0:80
165diff --git a/tests/unit/files/haproxy_config_rendered_test_output.txt b/tests/unit/files/haproxy_config_rendered_test_output.txt
166index ad46463..5e27f5b 100644
167--- a/tests/unit/files/haproxy_config_rendered_test_output.txt
168+++ b/tests/unit/files/haproxy_config_rendered_test_output.txt
169@@ -39,6 +39,18 @@ defaults
170 errorfile 503 /etc/haproxy/errors/503.http
171 errorfile 504 /etc/haproxy/errors/504.http
172
173+listen stats
174+ bind 127.0.0.1:10000
175+ acl allowed_cidr src 127.0.0.0/8
176+ http-request deny unless allowed_cidr
177+
178+ mode http
179+ stats enable
180+ stats uri /
181+ stats realm Haproxy\ Statistics
182+ stats auth haproxy:biometricsarenotsecret
183+ stats refresh 3
184+
185
186 listen combined-80
187 bind 0.0.0.0:80
188diff --git a/tests/unit/test_content_cache.py b/tests/unit/test_content_cache.py
189index 5228d45..e1390c0 100644
190--- a/tests/unit/test_content_cache.py
191+++ b/tests/unit/test_content_cache.py
192@@ -221,7 +221,8 @@ site1.local:
193
194 with mock.patch('lib.haproxy.HAProxyConf.conf_file', new_callable=mock.PropertyMock) as mock_conf_file:
195 mock_conf_file.return_value = os.path.join(self.tmpdir, 'haproxy.cfg')
196- content_cache.configure_haproxy()
197+ with mock.patch('charmhelpers.core.host.pwgen', return_value="biometricsarenotsecret"):
198+ content_cache.configure_haproxy()
199 self.assertFalse(service_start_or_restart.assert_called_with('haproxy'))
200
201 # Again, this time should be no change so no need to restart HAProxy
202diff --git a/tests/unit/test_haproxy.py b/tests/unit/test_haproxy.py
203index 978a4bf..3d46385 100644
204--- a/tests/unit/test_haproxy.py
205+++ b/tests/unit/test_haproxy.py
206@@ -67,7 +67,8 @@ class TestLibHAProxy(unittest.TestCase):
207 haproxy = HAProxy.HAProxyConf(self.tmpdir)
208 config = self.site_config
209 num_procs = 4
210- self.assertTrue(haproxy.write(haproxy.render(config, num_procs)))
211+ password = "biometricsarenotsecret"
212+ self.assertTrue(haproxy.write(haproxy.render(config, num_procs, monitoring_password=password)))
213 with open(haproxy.conf_file, 'r') as f:
214 new_conf = f.read()
215 with open('tests/unit/files/haproxy_config_rendered_test_output.txt', 'r') as f:

Subscribers

People subscribed via source and target branches