Merge ~aieri/charm-nagios:bug/1864192-rebase into ~nagios-charmers/charm-nagios:master

Proposed by Andrea Ieri
Status: Merged
Merged at revision: 233cd954f1327a47c5dca132c10a1a55c299c44f
Proposed branch: ~aieri/charm-nagios:bug/1864192-rebase
Merge into: ~nagios-charmers/charm-nagios:master
Prerequisite: ~aieri/charm-nagios:bug/1864192-charmhelpers-sync
Diff against target: 170 lines (+74/-58)
2 files modified
hooks/common.py (+0/-20)
hooks/monitors-relation-changed (+74/-38)
Reviewer Review Type Date Requested Status
Alvaro Uria (community) Approve
Adam Dyess (community) Approve
Chris Sanders Pending
Peter Sabaini Pending
Review via email: mp+387312@code.launchpad.net

This proposal supersedes a proposal from 2020-07-13.

To post a comment you must log in.
Revision history for this message
Peter Sabaini (peter-sabaini) wrote : Posted in a previous version of this proposal

LGTM, some nits but nothing blocking imo

review: Approve
Revision history for this message
Andrea Ieri (aieri) wrote : Posted in a previous version of this proposal

Thanks, the charm helpers sync script actually comes from https://raw.githubusercontent.com/juju/charm-helpers/master/tools/charm_helpers_sync/charm_helpers_sync.py, it's not part of this repo.

Revision history for this message
Adam Dyess (addyess) wrote : Posted in a previous version of this proposal

Great. No issues

review: Approve
Revision history for this message
Chris Sanders (chris.sanders) wrote : Posted in a previous version of this proposal

A few comments inline, and while I hate to do this I think the charmhelpers and this change need to be split. While reviewing it I'm not convinced that this merge isn't confusing local vs charmhelpers functions. For example 'ingress_address' is defined in this change and I *believe* is only actually used from charmhelpers.

If I'm just having difficulty understanding and you *do* think the change is dependent on charmhelpers you can make this MR dependent on the charmhelpers MR so the changes specific to this bug can be reviewed seperately.

review: Needs Fixing
Revision history for this message
Andrea Ieri (aieri) wrote :

I have addressed the comments, rebased, and split this change into three separate MRs (see prerequisites)

Revision history for this message
Adam Dyess (addyess) wrote :

Yeah, this is much cleaner to read. Thanks Andrea

review: Approve
Revision history for this message
Alvaro Uria (aluria) wrote :

Change lgtm. However, there is no other way to test it than a manual deployment. If unit tests don't exist, I think we should at least convert amulet tests into Zaza tests.

Manual testing looks good, though. +1.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
diff --git a/hooks/common.py b/hooks/common.py
index d569e33..a0b61e7 100644
--- a/hooks/common.py
+++ b/hooks/common.py
@@ -42,11 +42,6 @@ def check_ip(n):
42 except socket.error:42 except socket.error:
43 return False43 return False
4444
45def ingress_address(relation_data):
46 if 'ingress-address' in relation_data:
47 return relation_data['ingress-address']
48 return relation_data['private-address']
49
5045
51def get_local_ingress_address(binding='website'):46def get_local_ingress_address(binding='website'):
52 # using network-get to retrieve the address details if available.47 # using network-get to retrieve the address details if available.
@@ -347,21 +342,6 @@ def apply_host_policy(target_id, owner_unit, owner_relation):
347 ssh_service.save()342 ssh_service.save()
348343
349344
350def get_valid_relations():
351 for x in subprocess.Popen(['relation-ids', 'monitors'],
352 stdout=subprocess.PIPE).stdout:
353 yield x.strip()
354 for x in subprocess.Popen(['relation-ids', 'nagios'],
355 stdout=subprocess.PIPE).stdout:
356 yield x.strip()
357
358
359def get_valid_units(relation_id):
360 for x in subprocess.Popen(['relation-list', '-r', relation_id],
361 stdout=subprocess.PIPE).stdout:
362 yield x.strip()
363
364
365def _replace_in_config(find_me, replacement):345def _replace_in_config(find_me, replacement):
366 with open(INPROGRESS_CFG) as cf:346 with open(INPROGRESS_CFG) as cf:
367 with tempfile.NamedTemporaryFile(dir=INPROGRESS_DIR, delete=False) as new_cf:347 with tempfile.NamedTemporaryFile(dir=INPROGRESS_DIR, delete=False) as new_cf:
diff --git a/hooks/monitors-relation-changed b/hooks/monitors-relation-changed
index c48cdbb..7bb0082 100755
--- a/hooks/monitors-relation-changed
+++ b/hooks/monitors-relation-changed
@@ -18,17 +18,81 @@
1818
19import sys19import sys
20import os20import os
21import subprocess
22import yaml21import yaml
23import json
24import re22import re
2523from collections import defaultdict
2624
27from common import (customize_service, get_pynag_host,25from charmhelpers.core.hookenv import (
28 get_pynag_service, refresh_hostgroups,26 relation_get,
29 get_valid_relations, get_valid_units,27 ingress_address,
30 initialize_inprogress_config, flush_inprogress_config,28 related_units,
31 ingress_address)29 relation_ids,
30 log,
31 DEBUG
32)
33
34from common import (
35 customize_service,
36 get_pynag_host,
37 get_pynag_service,
38 refresh_hostgroups,
39 initialize_inprogress_config,
40 flush_inprogress_config
41)
42
43
44REQUIRED_REL_DATA_KEYS = [
45 'target-address',
46 'monitors',
47 'target-id',
48]
49
50
51def _prepare_relation_data(unit, rid):
52 relation_data = relation_get(unit=unit, rid=rid)
53
54 if not relation_data:
55 msg = (
56 'no relation data found for unit {} in relation {} - '
57 'skipping'.format(unit, rid)
58 )
59 log(msg, level=DEBUG)
60 return {}
61
62 if rid.split(':')[0] == 'nagios':
63 # Fake it for the more generic 'nagios' relation
64 relation_data['target-id'] = unit.replace('/', '-')
65 relation_data['monitors'] = {'monitors': {'remote': {}}}
66
67 if not relation_data.get('target-address'):
68 relation_data['target-address'] = ingress_address(unit=unit, rid=rid)
69
70 for key in REQUIRED_REL_DATA_KEYS:
71 if not relation_data.get(key):
72 # Note: it seems that some applications don't provide monitors over
73 # the relation at first (e.g. gnocchi). After a few hook runs,
74 # though, they add the key. For this reason I think using a logging
75 # level higher than DEBUG could be misleading
76 msg = (
77 '{} not found for unit {} in relation {} - '
78 'skipping'.format(key, unit, rid)
79 )
80 log(msg, level=DEBUG)
81 return {}
82
83 return relation_data
84
85
86def _collect_relation_data():
87 all_relations = defaultdict(dict)
88 for relname in ['nagios', 'monitors']:
89 for relid in relation_ids(relname):
90 for unit in related_units(relid):
91 relation_data = _prepare_relation_data(unit=unit, rid=relid)
92 if relation_data:
93 all_relations[relid][unit] = relation_data
94
95 return all_relations
3296
3397
34def main(argv):98def main(argv):
@@ -43,35 +107,7 @@ def main(argv):
43 relation_settings['target-address'] = argv[3]107 relation_settings['target-address'] = argv[3]
44 all_relations = {'monitors:99': {'testing/0': relation_settings}}108 all_relations = {'monitors:99': {'testing/0': relation_settings}}
45 else:109 else:
46 all_relations = {}110 all_relations = _collect_relation_data()
47 for relid in get_valid_relations():
48 (relname, relnum) = relid.split(':')
49 for unit in get_valid_units(relid):
50 relation_settings = json.loads(
51 subprocess.check_output(['relation-get', '--format=json',
52 '-r', relid,
53 '-',unit]).strip())
54
55 if relation_settings is None or relation_settings == '':
56 continue
57
58 if relname == 'monitors':
59 if ('monitors' not in relation_settings
60 or 'target-id' not in relation_settings):
61 continue
62 if ('target-id' in relation_settings and 'target-address' not in relation_settings):
63 relation_settings['target-address'] = ingress_address(relation_settings)
64
65 else:
66 # Fake it for the more generic 'nagios' relation'
67 relation_settings['target-id'] = unit.replace('/','-')
68 relation_settings['target-address'] = ingress_address(relation_settings)
69 relation_settings['monitors'] = {'monitors': {'remote': {} } }
70
71 if relid not in all_relations:
72 all_relations[relid] = {}
73
74 all_relations[relid][unit] = relation_settings
75111
76 # Hack to work around http://pad.lv/1025478112 # Hack to work around http://pad.lv/1025478
77 targets_with_addresses = set()113 targets_with_addresses = set()

Subscribers

People subscribed via source and target branches