X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=installers%2Fcharm%2Fosm-nbi%2Fsrc%2Fcharm.py;h=b19beae803d48817969d4555f37deb8ba5abca6d;hb=250b0b60a41df04bb79b8c231803a35e1bff6c66;hp=00ef4259aacdfa9f446462be37dcf8e058af314b;hpb=38f5d5834d610e011b3608fe8cc34b927775204a;p=osm%2Fdevops.git diff --git a/installers/charm/osm-nbi/src/charm.py b/installers/charm/osm-nbi/src/charm.py index 00ef4259..b19beae8 100755 --- a/installers/charm/osm-nbi/src/charm.py +++ b/installers/charm/osm-nbi/src/charm.py @@ -29,7 +29,6 @@ See more: https://charmhub.io/osm import logging from typing import Any, Dict -from urllib.parse import urlparse from charms.data_platform_libs.v0.data_interfaces import DatabaseRequires from charms.kafka_k8s.v0.kafka import KafkaEvents, KafkaRequires @@ -49,7 +48,7 @@ from ops.framework import StoredState from ops.main import main from ops.model import ActiveStatus, Container -from legacy_interfaces import KeystoneClient +from legacy_interfaces import KeystoneClient, PrometheusClient HOSTPATHS = [ HostPath( @@ -87,6 +86,7 @@ class OsmNbiCharm(CharmBase): self.mongodb_client = DatabaseRequires( self, "mongodb", database_name="osm", extra_user_roles="admin" ) + self.prometheus_client = PrometheusClient(self, "prometheus") self.keystone_client = KeystoneClient(self, "keystone") self._observe_charm_events() self.container: Container = self.unit.get_container("nbi") @@ -129,7 +129,6 @@ class OsmNbiCharm(CharmBase): def _on_update_status(self, _=None) -> None: """Handler for the update-status event.""" try: - self._validate_config() self._check_relations() if self.debug_mode.started: return @@ -185,12 +184,13 @@ class OsmNbiCharm(CharmBase): self.on["kafka"].relation_broken: self._on_required_relation_broken, self.mongodb_client.on.database_created: self._on_config_changed, self.on["mongodb"].relation_broken: self._on_required_relation_broken, - self.on["keystone"].relation_changed: self._on_config_changed, - self.on["keystone"].relation_broken: self._on_required_relation_broken, # Action events self.on.get_debug_mode_information_action: self._on_get_debug_mode_information_action, self.on.nbi_relation_joined: self._update_nbi_relation, } + for relation in [self.on[rel_name] for rel_name in ["prometheus", "keystone"]]: + event_handler_mapping[relation.relation_changed] = self._on_config_changed + event_handler_mapping[relation.relation_broken] = self._on_required_relation_broken for event, handler in event_handler_mapping.items(): self.framework.observe(event, handler) @@ -208,14 +208,6 @@ class OsmNbiCharm(CharmBase): CharmError: if charm configuration is invalid. """ logger.debug("validating charm config") - url = self.config.get("prometheus-url") - if not url: - raise CharmError("need prometheus-url config") - if not self._is_valid_url(url): - raise CharmError(f"Invalid value for prometheus-url config: '{url}'") - - def _is_valid_url(self, url) -> bool: - return urlparse(url).hostname is not None def _check_relations(self) -> None: """Validate charm relations. @@ -230,6 +222,8 @@ class OsmNbiCharm(CharmBase): missing_relations.append("kafka") if not self._is_database_available(): missing_relations.append("mongodb") + if self.prometheus_client.is_missing_data_in_app(): + missing_relations.append("prometheus") if self.keystone_client.is_missing_data_in_app(): missing_relations.append("keystone") @@ -259,7 +253,6 @@ class OsmNbiCharm(CharmBase): def _get_layer(self) -> Dict[str, Any]: """Get layer for Pebble.""" - prometheus = urlparse(self.config["prometheus-url"]) return { "summary": "nbi layer", "description": "pebble config layer for nbi", @@ -267,10 +260,11 @@ class OsmNbiCharm(CharmBase): "nbi": { "override": "replace", "summary": "nbi service", - "command": "python3 -m osm_nbi.nbi", + "command": "/bin/sh -c 'cd /app/osm_nbi && python3 -m osm_nbi.nbi'", # cd /app/osm_nbi is needed until we upgrade Juju to 3.x "startup": "enabled", "user": "appuser", "group": "appuser", + "working-dir": "/app/osm_nbi", # This parameter has no effect in juju 2.9.x "environment": { # General configuration "OSMNBI_SERVER_ENABLE_TEST": False, @@ -289,8 +283,8 @@ class OsmNbiCharm(CharmBase): "OSMNBI_STORAGE_COLLECTION": "files", "OSMNBI_STORAGE_URI": self._get_mongodb_uri(), # Prometheus configuration - "OSMNBI_PROMETHEUS_HOST": prometheus.hostname, - "OSMNBI_PROMETHEUS_PORT": prometheus.port if prometheus.port else "", + "OSMNBI_PROMETHEUS_HOST": self.prometheus_client.hostname, + "OSMNBI_PROMETHEUS_PORT": self.prometheus_client.port, # Log configuration "OSMNBI_LOG_LEVEL": self.config["log-level"], # Authentication environments