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
.osm_libs
.v0
.utils
import (
37 check_container_ready
,
40 from charms
.osm_temporal
.v0
.temporal
import TemporalRequires
41 from charms
.osm_vca_integrator
.v0
.vca
import VcaDataChangedEvent
, VcaRequires
42 from ops
.charm
import ActionEvent
, CharmBase
, CharmEvents
43 from ops
.framework
import EventSource
, StoredState
44 from ops
.main
import main
45 from ops
.model
import ActiveStatus
, Container
47 from legacy_interfaces
import MongoClient
51 config
="lcm-hostpath",
52 container_path
="/usr/lib/python3/dist-packages/osm_lcm",
55 config
="common-hostpath",
56 container_path
="/usr/lib/python3/dist-packages/osm_common",
59 config
="n2vc-hostpath",
60 container_path
="/usr/lib/python3/dist-packages/n2vc",
64 logger
= logging
.getLogger(__name__
)
67 class LcmEvents(CharmEvents
):
70 vca_data_changed
= EventSource(VcaDataChangedEvent
)
73 class OsmNGLcmCharm(CharmBase
):
74 """OSM LCM Kubernetes sidecar charm."""
76 container_name
= "nglcm"
77 service_name
= "nglcm"
79 _stored
= StoredState()
81 def __init__(self
, *args
):
82 super().__init
__(*args
)
83 self
.vca
= VcaRequires(self
)
84 self
.temporal
= TemporalRequires(self
)
85 self
.mongodb_client
= MongoClient(self
, "mongodb")
86 self
._observe
_charm
_events
()
87 self
.container
: Container
= self
.unit
.get_container(self
.container_name
)
88 self
.debug_mode
= DebugMode(self
, self
._stored
, self
.container
, HOSTPATHS
)
90 # ---------------------------------------------------------------------------
91 # Handlers for Charm Events
92 # ---------------------------------------------------------------------------
94 def _on_config_changed(self
, _
) -> None:
95 """Handler for the config-changed event."""
97 self
._validate
_config
()
98 self
._check
_relations
()
99 # Check if the container is ready.
100 # Eventually it will become ready after the first pebble-ready event.
101 check_container_ready(self
.container
)
102 if not self
.debug_mode
.started
:
103 self
._configure
_service
(self
.container
)
105 # Update charm status
106 self
._on
_update
_status
()
107 except CharmError
as e
:
108 logger
.debug(e
.message
)
109 self
.unit
.status
= e
.status
111 def _on_update_status(self
, _
=None) -> None:
112 """Handler for the update-status event."""
114 self
._validate
_config
()
115 self
._check
_relations
()
116 check_container_ready(self
.container
)
117 if self
.debug_mode
.started
:
119 check_service_active(self
.container
, self
.service_name
)
120 self
.unit
.status
= ActiveStatus()
121 except CharmError
as e
:
122 logger
.debug(e
.message
)
123 self
.unit
.status
= e
.status
125 def _on_required_relation_broken(self
, _
) -> None:
126 """Handler for required relation-broken events."""
128 check_container_ready(self
.container
)
129 check_service_active(self
.container
, self
.service_name
)
130 self
.container
.stop(self
.container_name
)
133 self
._on
_update
_status
()
135 def _on_get_debug_mode_information_action(self
, event
: ActionEvent
) -> None:
136 """Handler for the get-debug-mode-information action event."""
137 if not self
.debug_mode
.started
:
139 f
"debug-mode has not started. Hint: juju config {self.app.name} debug-mode=true"
143 debug_info
= {"command": self
.debug_mode
.command
, "password": self
.debug_mode
.password
}
144 event
.set_results(debug_info
)
146 # ---------------------------------------------------------------------------
147 # Validation, configuration and more
148 # ---------------------------------------------------------------------------
150 def _validate_config(self
) -> None:
151 """Validate charm configuration.
154 CharmError: if charm configuration is invalid.
156 logger
.debug("validating charm config")
157 if self
.config
["log-level"].upper() not in [
165 raise CharmError("invalid value for log-level option")
167 def _observe_charm_events(self
) -> None:
168 event_handler_mapping
= {
169 # Core lifecycle events
170 self
.on
.nglcm_pebble_ready
: self
._on
_config
_changed
,
171 self
.on
.config_changed
: self
._on
_config
_changed
,
172 self
.on
.update_status
: self
._on
_update
_status
,
174 self
.on
["mongodb"].relation_changed
: self
._on
_config
_changed
,
175 self
.on
["mongodb"].relation_broken
: self
._on
_required
_relation
_broken
,
176 self
.on
["temporal"].relation_changed
: self
._on
_config
_changed
,
177 self
.on
["temporal"].relation_broken
: self
._on
_required
_relation
_broken
,
178 self
.on
.vca_data_changed
: self
._on
_config
_changed
,
179 self
.on
["vca"].relation_broken
: self
._on
_config
_changed
,
181 self
.on
.get_debug_mode_information_action
: self
._on
_get
_debug
_mode
_information
_action
,
183 for event
, handler
in event_handler_mapping
.items():
184 self
.framework
.observe(event
, handler
)
186 def _check_relations(self
) -> None:
187 """Validate charm relations.
190 CharmError: if charm configuration is invalid.
192 logger
.debug("check for missing relations")
193 missing_relations
= []
195 if self
.mongodb_client
.is_missing_data_in_unit():
196 missing_relations
.append("mongodb")
197 if not self
.temporal
.host
or not self
.temporal
.port
:
198 missing_relations
.append("temporal")
200 if missing_relations
:
201 relations_str
= ", ".join(missing_relations
)
202 one_relation_missing
= len(missing_relations
) == 1
203 error_msg
= f
'need {relations_str} relation{"" if one_relation_missing else "s"}'
204 logger
.warning(error_msg
)
205 raise CharmError(error_msg
)
207 def _configure_service(self
, container
: Container
) -> None:
208 """Add Pebble layer with the lcm service."""
209 logger
.debug(f
"configuring {self.app.name} service")
210 container
.add_layer("nglcm", self
._get
_layer
(), combine
=True)
213 def _get_layer(self
) -> Dict
[str, Any
]:
214 """Get layer for Pebble."""
216 # General configuration
217 "OSMLCM_GLOBAL_LOGLEVEL": self
.config
["log-level"].upper(),
218 # Database configuration
219 "OSMLCM_DATABASE_DRIVER": "mongo",
220 "OSMLCM_DATABASE_URI": self
.mongodb_client
.connection_string
,
221 "OSMLCM_DATABASE_COMMONKEY": self
.config
["database-commonkey"],
222 # Storage configuration
223 "OSMLCM_STORAGE_DRIVER": "mongo",
224 "OSMLCM_STORAGE_PATH": "/app/storage",
225 "OSMLCM_STORAGE_COLLECTION": "files",
226 "OSMLCM_STORAGE_URI": self
.mongodb_client
.connection_string
,
227 "OSMLCM_VCA_HELM_CA_CERTS": self
.config
["helm-ca-certs"],
228 "OSMLCM_VCA_STABLEREPOURL": self
.config
["helm-stable-repo-url"],
229 # Temporal configuration
230 "OSMLCM_TEMPORAL_HOST": self
.temporal
.host
,
231 "OSMLCM_TEMPORAL_PORT": self
.temporal
.port
,
235 environments
["OSMLCM_VCA_ENDPOINTS"] = self
.vca
.data
.endpoints
236 environments
["OSMLCM_VCA_USER"] = self
.vca
.data
.user
237 environments
["OSMLCM_VCA_PUBKEY"] = self
.vca
.data
.public_key
238 environments
["OSMLCM_VCA_SECRET"] = self
.vca
.data
.secret
239 environments
["OSMLCM_VCA_CACERT"] = self
.vca
.data
.cacert
240 if self
.vca
.data
.lxd_cloud
:
241 environments
["OSMLCM_VCA_CLOUD"] = self
.vca
.data
.lxd_cloud
243 if self
.vca
.data
.k8s_cloud
:
244 environments
["OSMLCM_VCA_K8S_CLOUD"] = self
.vca
.data
.k8s_cloud
245 for key
, value
in self
.vca
.data
.model_configs
.items():
246 env_name
= f
'OSMLCM_VCA_MODEL_CONFIG_{key.upper().replace("-","_")}'
247 environments
[env_name
] = value
250 "summary": "nglcm layer",
251 "description": "pebble config layer for nglcm",
254 "override": "replace",
255 "summary": "nslcm service",
256 "command": "python3 -m osm_lcm.nglcm -c /usr/lib/python3/dist-packages/osm_lcm/nglcm.cfg",
257 "startup": "enabled",
260 "environment": environments
,
264 logger
.info(f
"Layer: {layer_config}")
268 if __name__
== "__main__": # pragma: no cover