484841acca971355fbdec44f389b711437df8575
[osm/devops.git] / installers / charm / osm-nbi / src / charm.py
1 #!/usr/bin/env python3
2 # Copyright 2022 Canonical Ltd.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License"); you may
5 # not use this file except in compliance with the License. You may obtain
6 # a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 # License for the specific language governing permissions and limitations
14 # under the License.
15 #
16 # For those usages not covered by the Apache License, Version 2.0 please
17 # contact: legal@canonical.com
18 #
19 # To get in touch with the maintainers, please contact:
20 # osm-charmers@lists.launchpad.net
21 #
22 #
23 # Learn more at: https://juju.is/docs/sdk
24
25 """OSM NBI charm.
26
27 See more: https://charmhub.io/osm
28 """
29
30 import logging
31 from typing import Any, Dict
32
33 from charms.data_platform_libs.v0.data_interfaces import DatabaseRequires
34 from charms.kafka_k8s.v0.kafka import KafkaEvents, KafkaRequires
35 from charms.nginx_ingress_integrator.v0.ingress import IngressRequires
36 from charms.observability_libs.v1.kubernetes_service_patch import KubernetesServicePatch
37 from charms.osm_libs.v0.utils import (
38 CharmError,
39 DebugMode,
40 HostPath,
41 check_container_ready,
42 check_service_active,
43 )
44 from charms.osm_nbi.v0.nbi import NbiProvides
45 from charms.osm_temporal.v0.temporal import TemporalRequires
46 from lightkube.models.core_v1 import ServicePort
47 from ops.charm import ActionEvent, CharmBase, RelationJoinedEvent
48 from ops.framework import StoredState
49 from ops.main import main
50 from ops.model import ActiveStatus, Container
51
52 from legacy_interfaces import KeystoneClient, PrometheusClient
53
54 HOSTPATHS = [
55 HostPath(
56 config="nbi-hostpath",
57 container_path="/usr/lib/python3/dist-packages/osm_nbi",
58 ),
59 HostPath(
60 config="common-hostpath",
61 container_path="/usr/lib/python3/dist-packages/osm_common",
62 ),
63 ]
64 SERVICE_PORT = 9999
65
66 logger = logging.getLogger(__name__)
67
68
69 class OsmNbiCharm(CharmBase):
70 """OSM NBI Kubernetes sidecar charm."""
71
72 on = KafkaEvents()
73 _stored = StoredState()
74
75 def __init__(self, *args):
76 super().__init__(*args)
77 self.ingress = IngressRequires(
78 self,
79 {
80 "service-hostname": self.external_hostname,
81 "service-name": self.app.name,
82 "service-port": SERVICE_PORT,
83 },
84 )
85 self.kafka = KafkaRequires(self)
86 self.nbi = NbiProvides(self)
87 self.temporal = TemporalRequires(self)
88 self.mongodb_client = DatabaseRequires(
89 self, "mongodb", database_name="osm", extra_user_roles="admin"
90 )
91 self.prometheus_client = PrometheusClient(self, "prometheus")
92 self.keystone_client = KeystoneClient(self, "keystone")
93 self._observe_charm_events()
94 self.container: Container = self.unit.get_container("nbi")
95 self.debug_mode = DebugMode(self, self._stored, self.container, HOSTPATHS)
96 self._patch_k8s_service()
97
98 @property
99 def external_hostname(self) -> str:
100 """External hostname property.
101
102 Returns:
103 str: the external hostname from config.
104 If not set, return the ClusterIP service name.
105 """
106 return self.config.get("external-hostname") or self.app.name
107
108 # ---------------------------------------------------------------------------
109 # Handlers for Charm Events
110 # ---------------------------------------------------------------------------
111
112 def _on_config_changed(self, _) -> None:
113 """Handler for the config-changed event."""
114 try:
115 self._validate_config()
116 self._check_relations()
117 # Check if the container is ready.
118 # Eventually it will become ready after the first pebble-ready event.
119 check_container_ready(self.container)
120
121 if not self.debug_mode.started:
122 self._configure_service(self.container)
123 self._update_ingress_config()
124 self._update_nbi_relation()
125 # Update charm status
126 self._on_update_status()
127 except CharmError as e:
128 logger.debug(e.message)
129 self.unit.status = e.status
130
131 def _on_update_status(self, _=None) -> None:
132 """Handler for the update-status event."""
133 try:
134 self._check_relations()
135 if self.debug_mode.started:
136 return
137 check_container_ready(self.container)
138 check_service_active(self.container, "nbi")
139 self.unit.status = ActiveStatus()
140 except CharmError as e:
141 logger.debug(e.message)
142 self.unit.status = e.status
143
144 def _on_required_relation_broken(self, _) -> None:
145 """Handler for the kafka-broken event."""
146 # Check Pebble has started in the container
147 try:
148 check_container_ready(self.container)
149 check_service_active(self.container, "nbi")
150 self.container.stop("nbi")
151 except CharmError:
152 pass
153 finally:
154 self._on_update_status()
155
156 def _update_nbi_relation(self, event: RelationJoinedEvent = None) -> None:
157 """Handler for the nbi-relation-joined event."""
158 if self.unit.is_leader():
159 self.nbi.set_host_info(self.app.name, SERVICE_PORT, event.relation if event else None)
160
161 def _on_get_debug_mode_information_action(self, event: ActionEvent) -> None:
162 """Handler for the get-debug-mode-information action event."""
163 if not self.debug_mode.started:
164 event.fail("debug-mode has not started. Hint: juju config nbi debug-mode=true")
165 return
166
167 debug_info = {"command": self.debug_mode.command, "password": self.debug_mode.password}
168 event.set_results(debug_info)
169
170 # ---------------------------------------------------------------------------
171 # Validation and configuration and more
172 # ---------------------------------------------------------------------------
173
174 def _patch_k8s_service(self) -> None:
175 port = ServicePort(SERVICE_PORT, name=f"{self.app.name}")
176 self.service_patcher = KubernetesServicePatch(self, [port])
177
178 def _observe_charm_events(self) -> None:
179 event_handler_mapping = {
180 # Core lifecycle events
181 self.on.nbi_pebble_ready: self._on_config_changed,
182 self.on.config_changed: self._on_config_changed,
183 self.on.update_status: self._on_update_status,
184 # Relation events
185 self.on.kafka_available: self._on_config_changed,
186 self.on["kafka"].relation_broken: self._on_required_relation_broken,
187 self.mongodb_client.on.database_created: self._on_config_changed,
188 self.on["mongodb"].relation_broken: self._on_required_relation_broken,
189 # Action events
190 self.on.get_debug_mode_information_action: self._on_get_debug_mode_information_action,
191 self.on.nbi_relation_joined: self._update_nbi_relation,
192 self.on["temporal"].relation_changed: self._on_config_changed,
193 self.on["temporal"].relation_broken: self._on_required_relation_broken,
194 }
195 for relation in [self.on[rel_name] for rel_name in ["prometheus", "keystone"]]:
196 event_handler_mapping[relation.relation_changed] = self._on_config_changed
197 event_handler_mapping[relation.relation_broken] = self._on_required_relation_broken
198
199 for event, handler in event_handler_mapping.items():
200 self.framework.observe(event, handler)
201
202 def _is_database_available(self) -> bool:
203 try:
204 return self.mongodb_client.is_resource_created()
205 except KeyError:
206 return False
207
208 def _validate_config(self) -> None:
209 """Validate charm configuration.
210
211 Raises:
212 CharmError: if charm configuration is invalid.
213 """
214 logger.debug("validating charm config")
215
216 def _check_relations(self) -> None:
217 """Validate charm relations.
218
219 Raises:
220 CharmError: if charm configuration is invalid.
221 """
222 logger.debug("check for missing relations")
223 missing_relations = []
224
225 if not self.kafka.host or not self.kafka.port:
226 missing_relations.append("kafka")
227 if not self._is_database_available():
228 missing_relations.append("mongodb")
229 if self.prometheus_client.is_missing_data_in_app():
230 missing_relations.append("prometheus")
231 if self.keystone_client.is_missing_data_in_app():
232 missing_relations.append("keystone")
233 if not self.temporal.host or not self.temporal.port:
234 missing_relations.append("temporal")
235
236 if missing_relations:
237 relations_str = ", ".join(missing_relations)
238 one_relation_missing = len(missing_relations) == 1
239 error_msg = f'need {relations_str} relation{"" if one_relation_missing else "s"}'
240 logger.warning(error_msg)
241 raise CharmError(error_msg)
242
243 def _update_ingress_config(self) -> None:
244 """Update ingress config in relation."""
245 ingress_config = {
246 "service-hostname": self.external_hostname,
247 "max-body-size": self.config["max-body-size"],
248 }
249 if "tls-secret-name" in self.config:
250 ingress_config["tls-secret-name"] = self.config["tls-secret-name"]
251 logger.debug(f"updating ingress-config: {ingress_config}")
252 self.ingress.update_config(ingress_config)
253
254 def _configure_service(self, container: Container) -> None:
255 """Add Pebble layer with the nbi service."""
256 logger.debug(f"configuring {self.app.name} service")
257 container.add_layer("nbi", self._get_layer(), combine=True)
258 container.replan()
259
260 def _get_layer(self) -> Dict[str, Any]:
261 """Get layer for Pebble."""
262 return {
263 "summary": "nbi layer",
264 "description": "pebble config layer for nbi",
265 "services": {
266 "nbi": {
267 "override": "replace",
268 "summary": "nbi service",
269 "command": "python3 -m osm_nbi.nbi",
270 "startup": "enabled",
271 "user": "appuser",
272 "group": "appuser",
273 "environment": {
274 # General configuration
275 "OSMNBI_SERVER_ENABLE_TEST": False,
276 "OSMNBI_STATIC_DIR": "/app/osm_nbi/html_public",
277 # Kafka configuration
278 "OSMNBI_MESSAGE_HOST": self.kafka.host,
279 "OSMNBI_MESSAGE_PORT": self.kafka.port,
280 "OSMNBI_MESSAGE_DRIVER": "kafka",
281 # Database configuration
282 "OSMNBI_DATABASE_DRIVER": "mongo",
283 "OSMNBI_DATABASE_URI": self._get_mongodb_uri(),
284 "OSMNBI_DATABASE_COMMONKEY": self.config["database-commonkey"],
285 # Storage configuration
286 "OSMNBI_STORAGE_DRIVER": "mongo",
287 "OSMNBI_STORAGE_PATH": "/app/storage",
288 "OSMNBI_STORAGE_COLLECTION": "files",
289 "OSMNBI_STORAGE_URI": self._get_mongodb_uri(),
290 # Prometheus configuration
291 "OSMNBI_PROMETHEUS_HOST": self.prometheus_client.hostname,
292 "OSMNBI_PROMETHEUS_PORT": self.prometheus_client.port,
293 # Log configuration
294 "OSMNBI_LOG_LEVEL": self.config["log-level"],
295 # Authentication environments
296 "OSMNBI_AUTHENTICATION_BACKEND": "keystone",
297 "OSMNBI_AUTHENTICATION_AUTH_URL": self.keystone_client.host,
298 "OSMNBI_AUTHENTICATION_AUTH_PORT": self.keystone_client.port,
299 "OSMNBI_AUTHENTICATION_USER_DOMAIN_NAME": self.keystone_client.user_domain_name,
300 "OSMNBI_AUTHENTICATION_PROJECT_DOMAIN_NAME": self.keystone_client.project_domain_name,
301 "OSMNBI_AUTHENTICATION_SERVICE_USERNAME": self.keystone_client.username,
302 "OSMNBI_AUTHENTICATION_SERVICE_PASSWORD": self.keystone_client.password,
303 "OSMNBI_AUTHENTICATION_SERVICE_PROJECT": self.keystone_client.service,
304 # DISABLING INTERNAL SSL SERVER
305 "OSMNBI_SERVER_SSL_MODULE": "",
306 "OSMNBI_SERVER_SSL_CERTIFICATE": "",
307 "OSMNBI_SERVER_SSL_PRIVATE_KEY": "",
308 "OSMNBI_SERVER_SSL_PASS_PHRASE": "",
309 # Temporal configuration
310 "OSMNBI_TEMPORAL_HOST": self.temporal.host,
311 "OSMNBI_TEMPORAL_PORT": self.temporal.port,
312 },
313 }
314 },
315 }
316
317 def _get_mongodb_uri(self):
318 return list(self.mongodb_client.fetch_relation_data().values())[0]["uris"]
319
320
321 if __name__ == "__main__": # pragma: no cover
322 main(OsmNbiCharm)