Merge ~lihuiguo/charm-nagios:bug/1945723 into charm-nagios:master

Proposed by Linda Guo
Status: Merged
Approved by: James Troup
Approved revision: d31671415a99a6d923190d00f83f3a3347d4866f
Merged at revision: 9ad5dc8672b4d2fec83d92e6fdbd2c97eb5b0d9d
Proposed branch: ~lihuiguo/charm-nagios:bug/1945723
Merge into: charm-nagios:master
Diff against target: 188 lines (+125/-0)
7 files modified
config.yaml (+4/-0)
hooks/application-dashboard-relation-changed (+1/-0)
hooks/application-dashboard-relation-joined (+1/-0)
hooks/application_dashboard_relation.py (+75/-0)
hooks/upgrade_charm.py (+3/-0)
metadata.yaml (+2/-0)
tests/unit/test_registration_relation_joined.py (+39/-0)
Reviewer Review Type Date Requested Status
🤖 prod-jenkaas-bootstack continuous-integration Approve
James Troup (community) Needs Fixing
BootStack Reviewers Pending
Review via email: mp+409472@code.launchpad.net

Commit message

add registration relation to register nagios to Homer dashboard[1]

[1] https://launchpad.net/charm-homer-dashboard

To post a comment you must log in.
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
🤖 prod-jenkaas-bootstack (prod-jenkaas-bootstack) wrote :

A CI job is currently in progress. A follow up comment will be added when it completes.

Revision history for this message
🤖 prod-jenkaas-bootstack (prod-jenkaas-bootstack) wrote :
review: Approve (continuous-integration)
Revision history for this message
🤖 prod-jenkaas-bootstack (prod-jenkaas-bootstack) wrote :

A CI job is currently in progress. A follow up comment will be added when it completes.

Revision history for this message
🤖 prod-jenkaas-bootstack (prod-jenkaas-bootstack) wrote :
review: Approve (continuous-integration)
Revision history for this message
James Troup (elmo) wrote :

As discussed, let's please come up with clearer relation/interface names (e.g. 'application-dashboard' and 'register-application').

Other comments inline.

review: Needs Fixing
Revision history for this message
Linda Guo (lihuiguo) wrote :

updated the code according to the review comments

Revision history for this message
🤖 prod-jenkaas-bootstack (prod-jenkaas-bootstack) wrote :

A CI job is currently in progress. A follow up comment will be added when it completes.

Revision history for this message
🤖 prod-jenkaas-bootstack (prod-jenkaas-bootstack) wrote :
review: Approve (continuous-integration)
Revision history for this message
🤖 prod-jenkaas-bootstack (prod-jenkaas-bootstack) wrote :

A CI job is currently in progress. A follow up comment will be added when it completes.

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

Change successfully merged at revision 9ad5dc8672b4d2fec83d92e6fdbd2c97eb5b0d9d

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/config.yaml b/config.yaml
2index 765d70b..0739d64 100644
3--- a/config.yaml
4+++ b/config.yaml
5@@ -355,3 +355,7 @@ options:
6 - name: snmp_collector
7 host: /path/to/snmp/helper ....
8 service: /path/to/snmp/helper ....
9+ site_name:
10+ type: string
11+ default: ''
12+ description: An unique site name for Nagios deployment
13\ No newline at end of file
14diff --git a/hooks/application-dashboard-relation-changed b/hooks/application-dashboard-relation-changed
15new file mode 120000
16index 0000000..a13f386
17--- /dev/null
18+++ b/hooks/application-dashboard-relation-changed
19@@ -0,0 +1 @@
20+application_dashboard_relation.py
21\ No newline at end of file
22diff --git a/hooks/application-dashboard-relation-joined b/hooks/application-dashboard-relation-joined
23new file mode 120000
24index 0000000..a13f386
25--- /dev/null
26+++ b/hooks/application-dashboard-relation-joined
27@@ -0,0 +1 @@
28+application_dashboard_relation.py
29\ No newline at end of file
30diff --git a/hooks/application_dashboard_relation.py b/hooks/application_dashboard_relation.py
31new file mode 100755
32index 0000000..54a9596
33--- /dev/null
34+++ b/hooks/application_dashboard_relation.py
35@@ -0,0 +1,75 @@
36+#!/usr/bin/python
37+# Copyright Canonical 2021 Canonical Ltd. All Rights Reserved
38+#
39+# This program is free software: you can redistribute it and/or modify
40+# it under the terms of the GNU General Public License as published by
41+# the Free Software Foundation, either version 3 of the License, or
42+# (at your option) any later version.
43+#
44+# This program is distributed in the hope that it will be useful,
45+# but WITHOUT ANY WARRANTY; without even the implied warranty of
46+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
47+# GNU General Public License for more details.
48+#
49+# You should have received a copy of the GNU General Public License
50+# along with this program. If not, see <http://www.gnu.org/licenses/>.
51+import os
52+import sys
53+
54+from charmhelpers.core.hookenv import (
55+ Hooks,
56+ UnregisteredHookError,
57+ config,
58+ is_leader,
59+ log,
60+ relation_ids,
61+ relation_set,
62+ unit_public_ip,
63+)
64+
65+hooks = Hooks()
66+
67+
68+@hooks.hook("config-changed")
69+@hooks.hook("application-dashboard-relation-joined")
70+@hooks.hook("application-dashboard-relation-changed")
71+def application_dashboard_relation_changed(relation_id=None, remote_unit=None):
72+ """Register Nagios URL in dashboard charm such as Homer."""
73+ if not is_leader():
74+ return
75+ relations = relation_ids("application-dashboard")
76+ if not relations:
77+ return
78+ tls_configured = config("ssl_key")
79+ scheme = "https://" if tls_configured else "http://"
80+ url = scheme + unit_public_ip()
81+ if config("site_name"):
82+ subtitle = "[{}] Monitoring and alerting".format(config("site_name"))
83+ group = "[{}] LMA".format(config("site_name"))
84+ else:
85+ subtitle = "Monitoring and alerting"
86+ group = "LMA"
87+ icon_file = os.environ.get("JUJU_CHARM_DIR", None) + "/icon.svg"
88+ icon_data = None
89+ if os.path.exists(icon_file):
90+ with open(icon_file) as f:
91+ icon_data = f.read()
92+ for rid in relations:
93+ relation_set(
94+ rid,
95+ app=True,
96+ relation_settings={
97+ "name": "Nagios",
98+ "url": url,
99+ "subtitle": subtitle,
100+ "icon": icon_data,
101+ "group": group,
102+ },
103+ )
104+
105+
106+if __name__ == "__main__":
107+ try:
108+ hooks.execute(sys.argv)
109+ except UnregisteredHookError as e:
110+ log("Unknown hook {} - skipping.".format(e))
111diff --git a/hooks/upgrade_charm.py b/hooks/upgrade_charm.py
112index 32f6613..816d41d 100755
113--- a/hooks/upgrade_charm.py
114+++ b/hooks/upgrade_charm.py
115@@ -20,6 +20,8 @@ except ImportError:
116 "DEBIAN_FRONTEND=noninteractive apt-get -qy install python-enum34", shell=True
117 )
118
119+from application_dashboard_relation import application_dashboard_relation_changed
120+
121 from charmhelpers import fetch
122 from charmhelpers.contrib import ssl
123 from charmhelpers.core import hookenv, host
124@@ -678,6 +680,7 @@ update_cgi_config()
125 update_contacts()
126 update_password("nagiosro", ro_password)
127 configure_livestatus_xinetd()
128+application_dashboard_relation_changed()
129
130 if password:
131 update_password(nagiosadmin, password)
132diff --git a/metadata.yaml b/metadata.yaml
133index 21ceda1..e89c497 100644
134--- a/metadata.yaml
135+++ b/metadata.yaml
136@@ -16,5 +16,7 @@ requires:
137 interface: juju-info
138 monitors:
139 interface: monitors
140+ application-dashboard:
141+ interface: register-application
142 extra-bindings:
143 public:
144diff --git a/tests/unit/test_registration_relation_joined.py b/tests/unit/test_registration_relation_joined.py
145new file mode 100644
146index 0000000..0161f47
147--- /dev/null
148+++ b/tests/unit/test_registration_relation_joined.py
149@@ -0,0 +1,39 @@
150+import application_dashboard_relation
151+
152+import mock
153+
154+
155+@mock.patch("application_dashboard_relation.relation_set")
156+@mock.patch("application_dashboard_relation.unit_public_ip")
157+@mock.patch("application_dashboard_relation.is_leader")
158+@mock.patch("application_dashboard_relation.relation_ids")
159+@mock.patch("application_dashboard_relation.config")
160+@mock.patch("os.environ.get")
161+def test_main(
162+ environ_get, config, relation_ids, is_leader, unit_public_ip, relation_set
163+):
164+ hostname = "nagios.com"
165+ unit_public_ip.return_value = hostname
166+ relation_id = "application-dashboard:0"
167+ environ_get.return_value = ""
168+ relation_ids.return_value = relation_id
169+ site_name = "test"
170+ config.return_value = site_name
171+ application_dashboard_relation.application_dashboard_relation_changed()
172+ subtitle = "[{}] Monitoring and alerting".format(site_name)
173+ group = "[{}] LMA".format(site_name)
174+ name = "Nagios"
175+ tls_configured = config("ssl_key")
176+ scheme = "https" if tls_configured else "http"
177+
178+ relation_set.assert_called_with(
179+ "0",
180+ app=True,
181+ relation_settings={
182+ "name": name,
183+ "url": "{}://{}".format(scheme, hostname),
184+ "subtitle": subtitle,
185+ "icon": None,
186+ "group": group,
187+ },
188+ )

Subscribers

People subscribed via source and target branches

to all changes: