2 # Copyright 2022 Canonical Ltd.
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
8 # http://www.apache.org/licenses/LICENSE-2.0
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
16 # For those usages not covered by the Apache License, Version 2.0 please
17 # contact: legal@canonical.com
19 # To get in touch with the maintainers, please contact:
20 # osm-charmers@lists.launchpad.net
23 # Learn more at: https://juju.is/docs/sdk
27 See more: https://charmhub.io/osm
31 from typing
import Any
, Dict
33 from charms
.data_platform_libs
.v0
.data_interfaces
import DatabaseRequires
34 from charms
.osm_libs
.v0
.utils
import (
38 check_container_ready
,
41 from charms
.osm_temporal
.v0
.temporal
import TemporalRequires
42 from charms
.osm_vca_integrator
.v0
.vca
import VcaDataChangedEvent
, VcaRequires
43 from ops
.charm
import ActionEvent
, CharmBase
, CharmEvents
44 from ops
.framework
import EventSource
, StoredState
45 from ops
.main
import main
46 from ops
.model
import ActiveStatus
, Container
50 config
="lcm-hostpath",
51 container_path
="/usr/lib/python3/dist-packages/osm_lcm",
54 config
="common-hostpath",
55 container_path
="/usr/lib/python3/dist-packages/osm_common",
58 config
="n2vc-hostpath",
59 container_path
="/usr/lib/python3/dist-packages/n2vc",
63 logger
= logging
.getLogger(__name__
)
66 class LcmEvents(CharmEvents
):
69 vca_data_changed
= EventSource(VcaDataChangedEvent
)
72 class OsmNGLcmCharm(CharmBase
):
73 """OSM LCM Kubernetes sidecar charm."""
75 container_name
= "nglcm"
76 service_name
= "nglcm"
78 _stored
= StoredState()
80 def __init__(self
, *args
):
81 super().__init
__(*args
)
82 self
.vca
= VcaRequires(self
)
83 self
.temporal
= TemporalRequires(self
)
84 self
.mongodb_client
= DatabaseRequires(
85 self
, "mongodb", database_name
="osm", extra_user_roles
="admin"
87 self
._observe
_charm
_events
()
88 self
.container
: Container
= self
.unit
.get_container(self
.container_name
)
89 self
.debug_mode
= DebugMode(self
, self
._stored
, self
.container
, HOSTPATHS
)
91 # ---------------------------------------------------------------------------
92 # Handlers for Charm Events
93 # ---------------------------------------------------------------------------
95 def _on_config_changed(self
, _
) -> None:
96 """Handler for the config-changed event."""
98 self
._validate
_config
()
99 self
._check
_relations
()
100 # Check if the container is ready.
101 # Eventually it will become ready after the first pebble-ready event.
102 check_container_ready(self
.container
)
103 if not self
.debug_mode
.started
:
104 self
._configure
_service
(self
.container
)
106 # Update charm status
107 self
._on
_update
_status
()
108 except CharmError
as e
:
109 logger
.debug(e
.message
)
110 self
.unit
.status
= e
.status
112 def _on_update_status(self
, _
=None) -> None:
113 """Handler for the update-status event."""
115 self
._validate
_config
()
116 self
._check
_relations
()
117 check_container_ready(self
.container
)
118 if self
.debug_mode
.started
:
120 check_service_active(self
.container
, self
.service_name
)
121 self
.unit
.status
= ActiveStatus()
122 except CharmError
as e
:
123 logger
.debug(e
.message
)
124 self
.unit
.status
= e
.status
126 def _on_required_relation_broken(self
, _
) -> None:
127 """Handler for required relation-broken events."""
129 check_container_ready(self
.container
)
130 check_service_active(self
.container
, self
.service_name
)
131 self
.container
.stop(self
.container_name
)
134 self
._on
_update
_status
()
136 def _on_get_debug_mode_information_action(self
, event
: ActionEvent
) -> None:
137 """Handler for the get-debug-mode-information action event."""
138 if not self
.debug_mode
.started
:
140 f
"debug-mode has not started. Hint: juju config {self.app.name} debug-mode=true"
144 debug_info
= {"command": self
.debug_mode
.command
, "password": self
.debug_mode
.password
}
145 event
.set_results(debug_info
)
147 # ---------------------------------------------------------------------------
148 # Validation, configuration and more
149 # ---------------------------------------------------------------------------
151 def _validate_config(self
) -> None:
152 """Validate charm configuration.
155 CharmError: if charm configuration is invalid.
157 logger
.debug("validating charm config")
158 if self
.config
["log-level"].upper() not in [
166 raise CharmError("invalid value for log-level option")
168 def _observe_charm_events(self
) -> None:
169 event_handler_mapping
= {
170 # Core lifecycle events
171 self
.on
.nglcm_pebble_ready
: self
._on
_config
_changed
,
172 self
.on
.config_changed
: self
._on
_config
_changed
,
173 self
.on
.update_status
: self
._on
_update
_status
,
175 self
.mongodb_client
.on
.database_created
: self
._on
_config
_changed
,
176 self
.on
["mongodb"].relation_broken
: self
._on
_required
_relation
_broken
,
177 self
.on
["temporal"].relation_changed
: self
._on
_config
_changed
,
178 self
.on
["temporal"].relation_broken
: self
._on
_required
_relation
_broken
,
179 self
.on
.vca_data_changed
: self
._on
_config
_changed
,
180 self
.on
["vca"].relation_broken
: self
._on
_config
_changed
,
182 self
.on
.get_debug_mode_information_action
: self
._on
_get
_debug
_mode
_information
_action
,
184 for event
, handler
in event_handler_mapping
.items():
185 self
.framework
.observe(event
, handler
)
187 def _check_relations(self
) -> None:
188 """Validate charm relations.
191 CharmError: if charm configuration is invalid.
193 logger
.debug("check for missing relations")
194 missing_relations
= []
196 if not self
._is
_database
_available
():
197 missing_relations
.append("mongodb")
198 if not self
.temporal
.host
or not self
.temporal
.port
:
199 missing_relations
.append("temporal")
201 if missing_relations
:
202 relations_str
= ", ".join(missing_relations
)
203 one_relation_missing
= len(missing_relations
) == 1
204 error_msg
= f
'need {relations_str} relation{"" if one_relation_missing else "s"}'
205 logger
.warning(error_msg
)
206 raise CharmError(error_msg
)
208 def _is_database_available(self
) -> bool:
210 return self
.mongodb_client
.is_resource_created()
214 def _configure_service(self
, container
: Container
) -> None:
215 """Add Pebble layer with the lcm service."""
216 logger
.debug(f
"configuring {self.app.name} service")
217 container
.add_layer("nglcm", self
._get
_layer
(), combine
=True)
220 def _get_layer(self
) -> Dict
[str, Any
]:
221 """Get layer for Pebble."""
223 # General configuration
224 "OSMLCM_GLOBAL_LOGLEVEL": self
.config
["log-level"].upper(),
225 # Database configuration
226 "OSMLCM_DATABASE_DRIVER": "mongo",
227 "OSMLCM_DATABASE_URI": self
._get
_mongodb
_uri
(),
228 "OSMLCM_DATABASE_COMMONKEY": self
.config
["database-commonkey"],
229 # Storage configuration
230 "OSMLCM_STORAGE_DRIVER": "mongo",
231 "OSMLCM_STORAGE_PATH": "/app/storage",
232 "OSMLCM_STORAGE_COLLECTION": "files",
233 "OSMLCM_STORAGE_URI": self
._get
_mongodb
_uri
(),
234 "OSMLCM_VCA_HELM_CA_CERTS": self
.config
["helm-ca-certs"],
235 "OSMLCM_VCA_STABLEREPOURL": self
.config
["helm-stable-repo-url"],
236 # Temporal configuration
237 "OSMLCM_TEMPORAL_HOST": self
.temporal
.host
,
238 "OSMLCM_TEMPORAL_PORT": self
.temporal
.port
,
242 environments
["OSMLCM_VCA_ENDPOINTS"] = self
.vca
.data
.endpoints
243 environments
["OSMLCM_VCA_USER"] = self
.vca
.data
.user
244 environments
["OSMLCM_VCA_PUBKEY"] = self
.vca
.data
.public_key
245 environments
["OSMLCM_VCA_SECRET"] = self
.vca
.data
.secret
246 environments
["OSMLCM_VCA_CACERT"] = self
.vca
.data
.cacert
247 if self
.vca
.data
.lxd_cloud
:
248 environments
["OSMLCM_VCA_CLOUD"] = self
.vca
.data
.lxd_cloud
250 if self
.vca
.data
.k8s_cloud
:
251 environments
["OSMLCM_VCA_K8S_CLOUD"] = self
.vca
.data
.k8s_cloud
252 for key
, value
in self
.vca
.data
.model_configs
.items():
253 env_name
= f
'OSMLCM_VCA_MODEL_CONFIG_{key.upper().replace("-","_")}'
254 environments
[env_name
] = value
257 "summary": "nglcm layer",
258 "description": "pebble config layer for nglcm",
261 "override": "replace",
262 "summary": "nslcm service",
263 "command": "python3 -m osm_lcm.nglcm -c /usr/lib/python3/dist-packages/osm_lcm/nglcm.cfg",
264 "startup": "enabled",
267 "environment": environments
,
271 logger
.info(f
"Layer: {layer_config}")
274 def _get_mongodb_uri(self
):
275 return list(self
.mongodb_client
.fetch_relation_data().values())[0]["uris"]
278 if __name__
== "__main__": # pragma: no cover