Merge ~vultaire/charm-prometheus-blackbox-exporter:blacken-20.08 into charm-prometheus-blackbox-exporter:master

Proposed by Paul Goins
Status: Merged
Approved by: Xav Paice
Approved revision: a78415e1696568c1403cb2e5ad64a73f3cfce677
Merged at revision: a78415e1696568c1403cb2e5ad64a73f3cfce677
Proposed branch: ~vultaire/charm-prometheus-blackbox-exporter:blacken-20.08
Merge into: charm-prometheus-blackbox-exporter:master
Prerequisite: ~vultaire/charm-prometheus-blackbox-exporter:makefile-20.08
Diff against target: 366 lines (+94/-93)
3 files modified
src/reactive/prometheus_blackbox_exporter.py (+78/-73)
src/tests/functional/tests/test_prometheus_blackbox_exporter.py (+14/-17)
src/tox.ini (+2/-3)
Reviewer Review Type Date Requested Status
Xav Paice (community) Approve
Review via email: mp+388619@code.launchpad.net
To post a comment you must log in.
Revision history for this message
Xav Paice (xavpaice) wrote :

LGTM

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/src/reactive/prometheus_blackbox_exporter.py b/src/reactive/prometheus_blackbox_exporter.py
2index eb4677d..66d551f 100644
3--- a/src/reactive/prometheus_blackbox_exporter.py
4+++ b/src/reactive/prometheus_blackbox_exporter.py
5@@ -24,195 +24,200 @@ from charms.reactive.helpers import any_file_changed, data_changed
6 import yaml
7
8
9-DASHBOARD_PATH = os.getcwd() + '/templates/'
10-SNAP_NAME = 'prometheus-blackbox-exporter'
11-SVC_NAME = 'snap.prometheus-blackbox-exporter.daemon'
12+DASHBOARD_PATH = os.getcwd() + "/templates/"
13+SNAP_NAME = "prometheus-blackbox-exporter"
14+SVC_NAME = "snap.prometheus-blackbox-exporter.daemon"
15 PORT_DEF = 9115
16-BLACKBOX_EXPORTER_YML_TMPL = 'blackbox.yaml.j2'
17-CONF_FILE_PATH = '/var/snap/prometheus-blackbox-exporter/current/blackbox.yml'
18+BLACKBOX_EXPORTER_YML_TMPL = "blackbox.yaml.j2"
19+CONF_FILE_PATH = "/var/snap/prometheus-blackbox-exporter/current/blackbox.yml"
20
21
22 def templates_changed(tmpl_list):
23 """Return list of changed files."""
24- return any_file_changed(['templates/{}'.format(x) for x in tmpl_list])
25+ return any_file_changed(["templates/{}".format(x) for x in tmpl_list])
26
27
28-@when_not('blackbox-exporter.installed')
29+@when_not("blackbox-exporter.installed")
30 def install_packages():
31 """Installs the snap exporter."""
32- hookenv.status_set('maintenance', 'Installing software')
33+ hookenv.status_set("maintenance", "Installing software")
34 config = hookenv.config()
35- channel = config.get('snap_channel', 'stable')
36+ channel = config.get("snap_channel", "stable")
37 snap.install(SNAP_NAME, channel=channel, force_dangerous=False)
38- set_state('blackbox-exporter.installed')
39- set_state('blackbox-exporter.do-check-reconfig')
40+ set_state("blackbox-exporter.installed")
41+ set_state("blackbox-exporter.do-check-reconfig")
42
43
44-@hook('upgrade-charm')
45+@hook("upgrade-charm")
46 def upgrade():
47 """Reset the install state on upgrade, to ensure resource extraction."""
48- hookenv.status_set('maintenance', 'Charm upgrade in progress')
49+ hookenv.status_set("maintenance", "Charm upgrade in progress")
50 update_dashboards_from_resource()
51- set_state('blackbox-exporter.do-restart')
52+ set_state("blackbox-exporter.do-restart")
53
54
55 def get_modules():
56 """Load the modules."""
57 config = hookenv.config()
58 try:
59- modules = yaml.safe_load(config.get('modules'))
60+ modules = yaml.safe_load(config.get("modules"))
61 except yaml.YAMLError as error:
62- hookenv.log('Failed to load modules, error {}'.format(error))
63+ hookenv.log("Failed to load modules, error {}".format(error))
64 return None
65
66- if 'modules' in modules:
67- return yaml.safe_dump(modules['modules'], default_flow_style=False)
68+ if "modules" in modules:
69+ return yaml.safe_dump(modules["modules"], default_flow_style=False)
70 else:
71 return yaml.safe_dump(modules, default_flow_style=False)
72
73
74-@when('blackbox-exporter.installed')
75-@when('blackbox-exporter.do-reconfig-yaml')
76+@when("blackbox-exporter.installed")
77+@when("blackbox-exporter.do-reconfig-yaml")
78 def write_blackbox_exporter_config_yaml():
79 """Render the template."""
80 modules = get_modules()
81- render(source=BLACKBOX_EXPORTER_YML_TMPL,
82- target=CONF_FILE_PATH,
83- context={'modules': modules}
84- )
85+ render(
86+ source=BLACKBOX_EXPORTER_YML_TMPL,
87+ target=CONF_FILE_PATH,
88+ context={"modules": modules},
89+ )
90 hookenv.open_port(PORT_DEF)
91- set_state('blackbox-exporter.do-restart')
92- remove_state('blackbox-exporter.do-reconfig-yaml')
93+ set_state("blackbox-exporter.do-restart")
94+ remove_state("blackbox-exporter.do-reconfig-yaml")
95
96
97-@when('blackbox-exporter.started')
98+@when("blackbox-exporter.started")
99 def check_config():
100 """Check the config once started."""
101- set_state('blackbox-exporter.do-check-reconfig')
102+ set_state("blackbox-exporter.do-check-reconfig")
103
104
105-@when('blackbox-exporter.do-check-reconfig')
106+@when("blackbox-exporter.do-check-reconfig")
107 def check_reconfig_blackbox_exporter():
108 """Configure the exporter."""
109 config = hookenv.config()
110
111- if data_changed('blackbox-exporter.config', config):
112- set_state('blackbox-exporter.do-reconfig-yaml')
113+ if data_changed("blackbox-exporter.config", config):
114+ set_state("blackbox-exporter.do-reconfig-yaml")
115
116 if templates_changed([BLACKBOX_EXPORTER_YML_TMPL]):
117- set_state('blackbox-exporter.do-reconfig-yaml')
118+ set_state("blackbox-exporter.do-reconfig-yaml")
119
120- remove_state('blackbox-exporter.do-check-reconfig')
121+ remove_state("blackbox-exporter.do-check-reconfig")
122
123
124-@when('blackbox-exporter.do-restart')
125+@when("blackbox-exporter.do-restart")
126 def restart_blackbox_exporter():
127 """Restart the exporter."""
128 if not host.service_running(SVC_NAME):
129- hookenv.log('Starting {}...'.format(SVC_NAME))
130+ hookenv.log("Starting {}...".format(SVC_NAME))
131 host.service_start(SVC_NAME)
132 else:
133- hookenv.log('Restarting {}, config file changed...'.format(SVC_NAME))
134+ hookenv.log("Restarting {}, config file changed...".format(SVC_NAME))
135 host.service_restart(SVC_NAME)
136- hookenv.status_set('active', 'Ready')
137- set_state('blackbox-exporter.started')
138- remove_state('blackbox-exporter.do-restart')
139+ hookenv.status_set("active", "Ready")
140+ set_state("blackbox-exporter.started")
141+ remove_state("blackbox-exporter.do-restart")
142
143
144 # Relations
145-@when('blackbox-exporter.started')
146-@when('blackbox-exporter.available')
147+@when("blackbox-exporter.started")
148+@when("blackbox-exporter.available")
149 def configure_blackbox_exporter_relation():
150 """Configure the http relation."""
151- target = endpoint_from_name('blackbox-exporter')
152+ target = endpoint_from_name("blackbox-exporter")
153 target.configure(PORT_DEF)
154- remove_state('blackbox-exporter.configured')
155+ remove_state("blackbox-exporter.configured")
156
157
158-@when('nrpe-external-master.changed')
159+@when("nrpe-external-master.changed")
160 def nrpe_changed():
161 """Trigger nrpe update."""
162- remove_state('blackbox-exporter.configured')
163+ remove_state("blackbox-exporter.configured")
164
165
166-@when('blackbox-exporter.changed')
167+@when("blackbox-exporter.changed")
168 def prometheus_changed():
169 """Trigger prometheus update."""
170- remove_state('blackbox-exporter.configured')
171+ remove_state("blackbox-exporter.configured")
172
173
174-@when('nrpe-external-master.available')
175-@when_not('blackbox-exporter.configured')
176+@when("nrpe-external-master.available")
177+@when_not("blackbox-exporter.configured")
178 def update_nrpe_config(svc):
179 """Configure the nrpe check for the service."""
180- if not os.path.exists('/var/lib/nagios'):
181- hookenv.status_set('blocked', 'Waiting for nrpe package installation')
182+ if not os.path.exists("/var/lib/nagios"):
183+ hookenv.status_set("blocked", "Waiting for nrpe package installation")
184 return
185
186- hookenv.status_set('maintenance', 'Configuring nrpe checks')
187+ hookenv.status_set("maintenance", "Configuring nrpe checks")
188
189 hostname = nrpe.get_nagios_hostname()
190 nrpe_setup = nrpe.NRPE(hostname=hostname)
191- nrpe_setup.add_check(shortname='prometheus_blackbox_exporter_http',
192- check_cmd='check_http -I 127.0.0.1 -p {} -u /metrics'.format(PORT_DEF),
193- description='Prometheus blackbox Exporter HTTP check')
194+ nrpe_setup.add_check(
195+ shortname="prometheus_blackbox_exporter_http",
196+ check_cmd="check_http -I 127.0.0.1 -p {} -u /metrics".format(PORT_DEF),
197+ description="Prometheus blackbox Exporter HTTP check",
198+ )
199 nrpe_setup.write()
200- hookenv.status_set('active', 'Ready')
201- set_state('blackbox-exporter.configured')
202+ hookenv.status_set("active", "Ready")
203+ set_state("blackbox-exporter.configured")
204
205
206-@when('blackbox-exporter.configured')
207-@when_not('nrpe-external-master.available')
208+@when("blackbox-exporter.configured")
209+@when_not("nrpe-external-master.available")
210 def remove_nrpe_check():
211 """Remove the nrpe check."""
212 hostname = nrpe.get_nagios_hostname()
213 nrpe_setup = nrpe.NRPE(hostname=hostname)
214 nrpe_setup.remove_check(shortname="prometheus_blackbox_exporter_http")
215- remove_state('blackbox-exporter.configured')
216+ remove_state("blackbox-exporter.configured")
217
218
219-@when_all('endpoint.dashboards.joined')
220+@when_all("endpoint.dashboards.joined")
221 def register_grafana_dashboards():
222 """After joining to grafana, push the dashboard."""
223- grafana_endpoint = endpoint_from_flag('endpoint.dashboards.joined')
224+ grafana_endpoint = endpoint_from_flag("endpoint.dashboards.joined")
225
226 if grafana_endpoint is None:
227 return
228
229- hookenv.log('Grafana relation joined, push dashboard')
230+ hookenv.log("Grafana relation joined, push dashboard")
231
232 # load pre-distributed dashboards, that may havew been overwritten by resource
233 dash_dir = Path(DASHBOARD_PATH)
234- for dash_file in dash_dir.glob('*.json'):
235+ for dash_file in dash_dir.glob("*.json"):
236 dashboard = dash_file.read_text()
237 grafana_endpoint.register_dashboard(dash_file.stem, dashboard)
238- hookenv.log('Pushed {}'.format(dash_file))
239+ hookenv.log("Pushed {}".format(dash_file))
240
241
242 def update_dashboards_from_resource():
243 """Extract resource zip file into templates directory."""
244- dashboards_zip_resource = hookenv.resource_get('dashboards')
245+ dashboards_zip_resource = hookenv.resource_get("dashboards")
246 if not dashboards_zip_resource:
247- hookenv.log('No dashboards resource found', hookenv.DEBUG)
248+ hookenv.log("No dashboards resource found", hookenv.DEBUG)
249 # no dashboards zip found, go with the default distributed dashboard
250 return
251
252- hookenv.log('Installing dashboards from resource', hookenv.DEBUG)
253+ hookenv.log("Installing dashboards from resource", hookenv.DEBUG)
254 try:
255 shutil.copy(dashboards_zip_resource, DASHBOARD_PATH)
256 except IOError as error:
257- hookenv.log('Problem copying resource: {}'.format(error), hookenv.ERROR)
258+ hookenv.log("Problem copying resource: {}".format(error), hookenv.ERROR)
259 return
260
261 try:
262- with ZipFile(dashboards_zip_resource, 'r') as zipfile:
263+ with ZipFile(dashboards_zip_resource, "r") as zipfile:
264 zipfile.extractall(path=DASHBOARD_PATH)
265- hookenv.log('Extracted dashboards from resource', hookenv.DEBUG)
266+ hookenv.log("Extracted dashboards from resource", hookenv.DEBUG)
267 except BadZipFile as error:
268- hookenv.log('BadZipFile: {}'.format(error), hookenv.ERROR)
269+ hookenv.log("BadZipFile: {}".format(error), hookenv.ERROR)
270 return
271 except PermissionError as error:
272- hookenv.log('Unable to unzip the provided resource: {}'.format(error), hookenv.ERROR)
273+ hookenv.log(
274+ "Unable to unzip the provided resource: {}".format(error), hookenv.ERROR
275+ )
276 return
277
278 register_grafana_dashboards()
279diff --git a/src/tests/functional/tests/test_prometheus_blackbox_exporter.py b/src/tests/functional/tests/test_prometheus_blackbox_exporter.py
280index b02154b..64d6c2f 100644
281--- a/src/tests/functional/tests/test_prometheus_blackbox_exporter.py
282+++ b/src/tests/functional/tests/test_prometheus_blackbox_exporter.py
283@@ -24,9 +24,7 @@ class BasePrometheusBlackboxExporterTest(unittest.TestCase):
284 cls.lead_unit_name = model.get_lead_unit_name(
285 cls.application_name, model_name=cls.model_name
286 )
287- cls.units = model.get_units(
288- cls.application_name, model_name=cls.model_name
289- )
290+ cls.units = model.get_units(cls.application_name, model_name=cls.model_name)
291 cls.prometheus_blackbox_exporter_ip = model.get_app_ips(cls.application_name)[0]
292
293
294@@ -46,9 +44,7 @@ class CharmOperationTest(BasePrometheusBlackboxExporterTest):
295 if response["Code"] == "0":
296 return
297 logging.warning(
298- "Unexpected curl response: {}. Retrying in 30s.".format(
299- response
300- )
301+ "Unexpected curl response: {}. Retrying in 30s.".format(response)
302 )
303 time.sleep(30)
304
305@@ -56,23 +52,24 @@ class CharmOperationTest(BasePrometheusBlackboxExporterTest):
306 self.fail(
307 "Prometheus-blackbox-exporter didn't respond to the command \n"
308 "'{curl_command}' as expected.\n"
309- "Result: {result}".format(
310- curl_command=curl_command, result=response
311- )
312+ "Result: {result}".format(curl_command=curl_command, result=response)
313 )
314
315 def test_02_nrpe_http_check(self):
316 """Verify nrpe check exists."""
317- expected_nrpe_check = "command[check_prometheus_blackbox_exporter_http]={} -I 127.0.0.1 -p {} -u {}".format(
318- "/usr/lib/nagios/plugins/check_http",
319- DEFAULT_API_PORT,
320- DEFAULT_API_URL
321+ expected_nrpe_check = (
322+ "command[check_prometheus_blackbox_exporter_http]={} -I 127.0.0.1 "
323+ "-p {} -u {}"
324+ ).format(
325+ "/usr/lib/nagios/plugins/check_http", DEFAULT_API_PORT, DEFAULT_API_URL
326+ )
327+ logging.debug(
328+ "Verify the nrpe check is created and has the required content..."
329 )
330- logging.debug('Verify the nrpe check is created and has the required content...')
331 cmd = "cat /etc/nagios/nrpe.d/check_prometheus_blackbox_exporter_http.cfg"
332 result = model.run_on_unit(self.lead_unit_name, cmd)
333- code = result.get('Code')
334- if code != '0':
335+ code = result.get("Code")
336+ if code != "0":
337 raise model.CommandRunFailed(cmd, result)
338- content = result.get('Stdout')
339+ content = result.get("Stdout")
340 self.assertTrue(expected_nrpe_check in content)
341diff --git a/src/tox.ini b/src/tox.ini
342index 9cd7d44..9dc4234 100644
343--- a/src/tox.ini
344+++ b/src/tox.ini
345@@ -25,7 +25,7 @@ passenv =
346 [testenv:lint]
347 commands =
348 flake8
349-#TODO black --check --exclude "/(\.eggs|\.git|\.tox|\.venv|\.build|dist|charmhelpers|mod)/" .
350+ black --check --exclude "/(\.eggs|\.git|\.tox|\.venv|\.build|dist|charmhelpers|mod)/" .
351 deps =
352 black
353 flake8
354@@ -43,11 +43,10 @@ exclude =
355 mod,
356 .build
357
358-#max-line-length = 88
359+max-line-length = 88
360 max-complexity = 10
361
362 # From previous tox.ini
363-max-line-length = 120
364 import-order-style = google
365
366 [testenv:black]

Subscribers

People subscribed via source and target branches

to all changes: