964050a96ec3b7b88d92621843dfaa8876245788
[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.kafka_k8s.v0.kafka import KafkaEvents, KafkaRequires
34 from charms.nginx_ingress_integrator.v0.ingress import IngressRequires
35 from charms.observability_libs.v1.kubernetes_service_patch import KubernetesServicePatch
36 from charms.osm_libs.v0.utils import (
37 CharmError,
38 DebugMode,
39 HostPath,
40 check_container_ready,
41 check_service_active,
42 )
43 from lightkube.models.core_v1 import ServicePort
44 from ops.charm import ActionEvent, CharmBase
45 from ops.framework import StoredState
46 from ops.main import main
47 from ops.model import ActiveStatus, Container
48
49 from legacy_interfaces import KeystoneClient, MongoClient, PrometheusClient
50
51 HOSTPATHS = [
52 HostPath(
53 config="nbi-hostpath",
54 container_path="/usr/lib/python3/dist-packages/osm_nbi",
55 ),
56 HostPath(
57 config="common-hostpath",
58 container_path="/usr/lib/python3/dist-packages/osm_common",
59 ),
60 ]
61 SERVICE_PORT = 9999
62
63 logger = logging.getLogger(__name__)
64
65
66 class OsmNbiCharm(CharmBase):
67 """OSM NBI Kubernetes sidecar charm."""
68
69 on = KafkaEvents()
70 _stored = StoredState()
71
72 def __init__(self, *args):
73 super().__init__(*args)
74 self.ingress = IngressRequires(
75 self,
76 {
77 "service-hostname": self.external_hostname,
78 "service-name": self.app.name,
79 "service-port": SERVICE_PORT,
80 },
81 )
82 self.kafka = KafkaRequires(self)
83 self.mongodb_client = MongoClient(self, "mongodb")
84 self.prometheus_client = PrometheusClient(self, "prometheus")
85 self.keystone_client = KeystoneClient(self, "keystone")
86 self._observe_charm_events()
87 self.container: Container = self.unit.get_container("nbi")
88 self.debug_mode = DebugMode(self, self._stored, self.container, HOSTPATHS)
89 self._patch_k8s_service()
90
91 @property
92 def external_hostname(self) -> str:
93 """External hostname property.
94
95 Returns:
96 str: the external hostname from config.
97 If not set, return the ClusterIP service name.
98 """
99 return self.config.get("external-hostname") or self.app.name
100
101 # ---------------------------------------------------------------------------
102 # Handlers for Charm Events
103 # ---------------------------------------------------------------------------
104
105 def _on_config_changed(self, _) -> None:
106 """Handler for the config-changed event."""
107 try:
108 self._validate_config()
109 self._check_relations()
110 # Check if the container is ready.
111 # Eventually it will become ready after the first pebble-ready event.
112 check_container_ready(self.container)
113
114 self._configure_service(self.container)
115 self._update_ingress_config()
116
117 # Update charm status
118 self._on_update_status()
119 except CharmError as e:
120 logger.debug(e.message)
121 self.unit.status = e.status
122
123 def _on_update_status(self, _=None) -> None:
124 """Handler for the update-status event."""
125 try:
126 self._check_relations()
127 if self.debug_mode.started:
128 return
129 check_container_ready(self.container)
130 check_service_active(self.container, "nbi")
131 self.unit.status = ActiveStatus()
132 except CharmError as e:
133 logger.debug(e.message)
134 self.unit.status = e.status
135
136 def _on_required_relation_broken(self, _) -> None:
137 """Handler for the kafka-broken event."""
138 # Check Pebble has started in the container
139 try:
140 check_container_ready(self.container)
141 check_service_active(self.container, "nbi")
142 self.container.stop("nbi")
143 except CharmError:
144 pass
145 finally:
146 self._on_update_status()
147
148 def _on_get_debug_mode_information_action(self, event: ActionEvent) -> None:
149 """Handler for the get-debug-mode-information action event."""
150 if not self.debug_mode.started:
151 event.fail("debug-mode has not started. Hint: juju config nbi debug-mode=true")
152 return
153
154 debug_info = {"command": self.debug_mode.command, "password": self.debug_mode.password}
155 event.set_results(debug_info)
156
157 # ---------------------------------------------------------------------------
158 # Validation and configuration and more
159 # ---------------------------------------------------------------------------
160
161 def _patch_k8s_service(self) -> None:
162 port = ServicePort(SERVICE_PORT, name=f"{self.app.name}")
163 self.service_patcher = KubernetesServicePatch(self, [port])
164
165 def _observe_charm_events(self) -> None:
166 event_handler_mapping = {
167 # Core lifecycle events
168 self.on.nbi_pebble_ready: self._on_config_changed,
169 self.on.config_changed: self._on_config_changed,
170 self.on.update_status: self._on_update_status,
171 # Relation events
172 self.on.kafka_available: self._on_config_changed,
173 self.on["kafka"].relation_broken: self._on_required_relation_broken,
174 # Action events
175 self.on.get_debug_mode_information_action: self._on_get_debug_mode_information_action,
176 }
177 for relation in [self.on[rel_name] for rel_name in ["mongodb", "prometheus", "keystone"]]:
178 event_handler_mapping[relation.relation_changed] = self._on_config_changed
179 event_handler_mapping[relation.relation_broken] = self._on_required_relation_broken
180
181 for event, handler in event_handler_mapping.items():
182 self.framework.observe(event, handler)
183
184 def _validate_config(self) -> None:
185 """Validate charm configuration.
186
187 Raises:
188 CharmError: if charm configuration is invalid.
189 """
190 logger.debug("validating charm config")
191
192 def _check_relations(self) -> None:
193 """Validate charm relations.
194
195 Raises:
196 CharmError: if charm configuration is invalid.
197 """
198 logger.debug("check for missing relations")
199 missing_relations = []
200
201 if not self.kafka.host or not self.kafka.port:
202 missing_relations.append("kafka")
203 if self.mongodb_client.is_missing_data_in_unit():
204 missing_relations.append("mongodb")
205 if self.prometheus_client.is_missing_data_in_app():
206 missing_relations.append("prometheus")
207 if self.keystone_client.is_missing_data_in_app():
208 missing_relations.append("keystone")
209
210 if missing_relations:
211 relations_str = ", ".join(missing_relations)
212 one_relation_missing = len(missing_relations) == 1
213 error_msg = f'need {relations_str} relation{"" if one_relation_missing else "s"}'
214 logger.warning(error_msg)
215 raise CharmError(error_msg)
216
217 def _update_ingress_config(self) -> None:
218 """Update ingress config in relation."""
219 ingress_config = {
220 "service-hostname": self.external_hostname,
221 "max-body-size": self.config["max-body-size"],
222 }
223 if "tls-secret-name" in self.config:
224 ingress_config["tls-secret-name"] = self.config["tls-secret-name"]
225 logger.debug(f"updating ingress-config: {ingress_config}")
226 self.ingress.update_config(ingress_config)
227
228 def _configure_service(self, container: Container) -> None:
229 """Add Pebble layer with the nbi service."""
230 logger.debug(f"configuring {self.app.name} service")
231 container.add_layer("nbi", self._get_layer(), combine=True)
232 container.replan()
233
234 def _get_layer(self) -> Dict[str, Any]:
235 """Get layer for Pebble."""
236 return {
237 "summary": "nbi layer",
238 "description": "pebble config layer for nbi",
239 "services": {
240 "nbi": {
241 "override": "replace",
242 "summary": "nbi service",
243 "command": "python3 -m osm_nbi.nbi",
244 "startup": "enabled",
245 "user": "appuser",
246 "group": "appuser",
247 "environment": {
248 # General configuration
249 "OSMNBI_SERVER_ENABLE_TEST": False,
250 "OSMNBI_STATIC_DIR": "/app/osm_nbi/html_public",
251 # Kafka configuration
252 "OSMNBI_MESSAGE_HOST": self.kafka.host,
253 "OSMNBI_MESSAGE_PORT": self.kafka.port,
254 "OSMNBI_MESSAGE_DRIVER": "kafka",
255 # Database configuration
256 "OSMNBI_DATABASE_DRIVER": "mongo",
257 "OSMNBI_DATABASE_URI": self.mongodb_client.connection_string,
258 "OSMNBI_DATABASE_COMMONKEY": self.config["database-commonkey"],
259 # Storage configuration
260 "OSMNBI_STORAGE_DRIVER": "mongo",
261 "OSMNBI_STORAGE_PATH": "/app/storage",
262 "OSMNBI_STORAGE_COLLECTION": "files",
263 "OSMNBI_STORAGE_URI": self.mongodb_client.connection_string,
264 # Prometheus configuration
265 "OSMNBI_PROMETHEUS_HOST": self.prometheus_client.hostname,
266 "OSMNBI_PROMETHEUS_PORT": self.prometheus_client.port,
267 # Log configuration
268 "OSMNBI_LOG_LEVEL": self.config["log-level"],
269 # Authentication environments
270 "OSMNBI_AUTHENTICATION_BACKEND": "keystone",
271 "OSMNBI_AUTHENTICATION_AUTH_URL": self.keystone_client.host,
272 "OSMNBI_AUTHENTICATION_AUTH_PORT": self.keystone_client.port,
273 "OSMNBI_AUTHENTICATION_USER_DOMAIN_NAME": self.keystone_client.user_domain_name,
274 "OSMNBI_AUTHENTICATION_PROJECT_DOMAIN_NAME": self.keystone_client.project_domain_name,
275 "OSMNBI_AUTHENTICATION_SERVICE_USERNAME": self.keystone_client.username,
276 "OSMNBI_AUTHENTICATION_SERVICE_PASSWORD": self.keystone_client.password,
277 "OSMNBI_AUTHENTICATION_SERVICE_PROJECT": self.keystone_client.service,
278 },
279 }
280 },
281 }
282
283
284 if __name__ == "__main__": # pragma: no cover
285 main(OsmNbiCharm)