b034624e20ef0ac7e6fdc14b625751bd90ffcb60
[osm/devops.git] / installers / charm / lcm / src / charm.py
1 #!/usr/bin/env python3
2 # Copyright 2021 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 # pylint: disable=E0213
24
25
26 import logging
27 from typing import NoReturn, Optional
28
29
30 from ops.main import main
31 from opslib.osm.charm import CharmedOsmBase, RelationsMissing
32 from opslib.osm.interfaces.http import HttpClient
33 from opslib.osm.interfaces.kafka import KafkaClient
34 from opslib.osm.interfaces.mongo import MongoClient
35 from opslib.osm.pod import ContainerV3Builder, PodRestartPolicy, PodSpecV3Builder
36 from opslib.osm.validator import ModelValidator, validator
37
38
39 logger = logging.getLogger(__name__)
40
41 PORT = 9999
42
43
44 class ConfigModel(ModelValidator):
45 vca_host: Optional[str]
46 vca_port: Optional[int]
47 vca_user: Optional[str]
48 vca_secret: Optional[str]
49 vca_pubkey: Optional[str]
50 vca_cacert: Optional[str]
51 vca_cloud: Optional[str]
52 vca_k8s_cloud: Optional[str]
53 database_commonkey: str
54 mongodb_uri: Optional[str]
55 log_level: str
56 vca_apiproxy: Optional[str]
57 # Model-config options
58 vca_model_config_agent_metadata_url: Optional[str]
59 vca_model_config_agent_stream: Optional[str]
60 vca_model_config_apt_ftp_proxy: Optional[str]
61 vca_model_config_apt_http_proxy: Optional[str]
62 vca_model_config_apt_https_proxy: Optional[str]
63 vca_model_config_apt_mirror: Optional[str]
64 vca_model_config_apt_no_proxy: Optional[str]
65 vca_model_config_automatically_retry_hooks: Optional[bool]
66 vca_model_config_backup_dir: Optional[str]
67 vca_model_config_cloudinit_userdata: Optional[str]
68 vca_model_config_container_image_metadata_url: Optional[str]
69 vca_model_config_container_image_stream: Optional[str]
70 vca_model_config_container_inherit_properties: Optional[str]
71 vca_model_config_container_networking_method: Optional[str]
72 vca_model_config_default_series: Optional[str]
73 vca_model_config_default_space: Optional[str]
74 vca_model_config_development: Optional[bool]
75 vca_model_config_disable_network_management: Optional[bool]
76 vca_model_config_egress_subnets: Optional[str]
77 vca_model_config_enable_os_refresh_update: Optional[bool]
78 vca_model_config_enable_os_upgrade: Optional[bool]
79 vca_model_config_fan_config: Optional[str]
80 vca_model_config_firewall_mode: Optional[str]
81 vca_model_config_ftp_proxy: Optional[str]
82 vca_model_config_http_proxy: Optional[str]
83 vca_model_config_https_proxy: Optional[str]
84 vca_model_config_ignore_machine_addresses: Optional[bool]
85 vca_model_config_image_metadata_url: Optional[str]
86 vca_model_config_image_stream: Optional[str]
87 vca_model_config_juju_ftp_proxy: Optional[str]
88 vca_model_config_juju_http_proxy: Optional[str]
89 vca_model_config_juju_https_proxy: Optional[str]
90 vca_model_config_juju_no_proxy: Optional[str]
91 vca_model_config_logforward_enabled: Optional[bool]
92 vca_model_config_logging_config: Optional[str]
93 vca_model_config_lxd_snap_channel: Optional[str]
94 vca_model_config_max_action_results_age: Optional[str]
95 vca_model_config_max_action_results_size: Optional[str]
96 vca_model_config_max_status_history_age: Optional[str]
97 vca_model_config_max_status_history_size: Optional[str]
98 vca_model_config_net_bond_reconfigure_delay: Optional[str]
99 vca_model_config_no_proxy: Optional[str]
100 vca_model_config_provisioner_harvest_mode: Optional[str]
101 vca_model_config_proxy_ssh: Optional[bool]
102 vca_model_config_snap_http_proxy: Optional[str]
103 vca_model_config_snap_https_proxy: Optional[str]
104 vca_model_config_snap_store_assertions: Optional[str]
105 vca_model_config_snap_store_proxy: Optional[str]
106 vca_model_config_snap_store_proxy_url: Optional[str]
107 vca_model_config_ssl_hostname_verification: Optional[bool]
108 vca_model_config_test_mode: Optional[bool]
109 vca_model_config_transmit_vendor_metrics: Optional[bool]
110 vca_model_config_update_status_hook_interval: Optional[str]
111 vca_stablerepourl: Optional[str]
112 vca_helm_ca_certs: Optional[str]
113 image_pull_policy: str
114
115 @validator("log_level")
116 def validate_log_level(cls, v):
117 if v not in {"INFO", "DEBUG"}:
118 raise ValueError("value must be INFO or DEBUG")
119 return v
120
121 @validator("mongodb_uri")
122 def validate_mongodb_uri(cls, v):
123 if v and not v.startswith("mongodb://"):
124 raise ValueError("mongodb_uri is not properly formed")
125 return v
126
127 @validator("image_pull_policy")
128 def validate_image_pull_policy(cls, v):
129 values = {
130 "always": "Always",
131 "ifnotpresent": "IfNotPresent",
132 "never": "Never",
133 }
134 v = v.lower()
135 if v not in values.keys():
136 raise ValueError("value must be always, ifnotpresent or never")
137 return values[v]
138
139
140 class LcmCharm(CharmedOsmBase):
141 def __init__(self, *args) -> NoReturn:
142 super().__init__(
143 *args,
144 oci_image="image",
145 debug_mode_config_key="debug_mode",
146 debug_pubkey_config_key="debug_pubkey",
147 vscode_workspace=VSCODE_WORKSPACE,
148 )
149 self.kafka_client = KafkaClient(self, "kafka")
150 self.framework.observe(self.on["kafka"].relation_changed, self.configure_pod)
151 self.framework.observe(self.on["kafka"].relation_broken, self.configure_pod)
152
153 self.mongodb_client = MongoClient(self, "mongodb")
154 self.framework.observe(self.on["mongodb"].relation_changed, self.configure_pod)
155 self.framework.observe(self.on["mongodb"].relation_broken, self.configure_pod)
156
157 self.ro_client = HttpClient(self, "ro")
158 self.framework.observe(self.on["ro"].relation_changed, self.configure_pod)
159 self.framework.observe(self.on["ro"].relation_broken, self.configure_pod)
160
161 def _check_missing_dependencies(self, config: ConfigModel):
162 missing_relations = []
163
164 if self.kafka_client.is_missing_data_in_unit():
165 missing_relations.append("kafka")
166 if not config.mongodb_uri and self.mongodb_client.is_missing_data_in_unit():
167 missing_relations.append("mongodb")
168 if self.ro_client.is_missing_data_in_app():
169 missing_relations.append("ro")
170
171 if missing_relations:
172 raise RelationsMissing(missing_relations)
173
174 def build_pod_spec(self, image_info):
175 # Validate config
176 config = ConfigModel(**dict(self.config))
177
178 if config.mongodb_uri and not self.mongodb_client.is_missing_data_in_unit():
179 raise Exception("Mongodb data cannot be provided via config and relation")
180
181 # Check relations
182 self._check_missing_dependencies(config)
183
184 # Create Builder for the PodSpec
185 pod_spec_builder = PodSpecV3Builder()
186
187 # Add secrets to the pod
188 lcm_secret_name = f"{self.app.name}-lcm-secret"
189 pod_spec_builder.add_secret(
190 lcm_secret_name,
191 {
192 "uri": config.mongodb_uri or self.mongodb_client.connection_string,
193 "commonkey": config.database_commonkey,
194 "helm_ca_certs": config.vca_helm_ca_certs,
195 },
196 )
197
198 # Build Container
199 container_builder = ContainerV3Builder(
200 self.app.name, image_info, config.image_pull_policy
201 )
202 container_builder.add_port(name=self.app.name, port=PORT)
203 container_builder.add_envs(
204 {
205 # General configuration
206 "ALLOW_ANONYMOUS_LOGIN": "yes",
207 "OSMLCM_GLOBAL_LOGLEVEL": config.log_level,
208 # RO configuration
209 "OSMLCM_RO_HOST": self.ro_client.host,
210 "OSMLCM_RO_PORT": self.ro_client.port,
211 "OSMLCM_RO_TENANT": "osm",
212 # Kafka configuration
213 "OSMLCM_MESSAGE_DRIVER": "kafka",
214 "OSMLCM_MESSAGE_HOST": self.kafka_client.host,
215 "OSMLCM_MESSAGE_PORT": self.kafka_client.port,
216 # Database configuration
217 "OSMLCM_DATABASE_DRIVER": "mongo",
218 # Storage configuration
219 "OSMLCM_STORAGE_DRIVER": "mongo",
220 "OSMLCM_STORAGE_PATH": "/app/storage",
221 "OSMLCM_STORAGE_COLLECTION": "files",
222 "OSMLCM_VCA_STABLEREPOURL": config.vca_stablerepourl,
223 }
224 )
225 container_builder.add_secret_envs(
226 secret_name=lcm_secret_name,
227 envs={
228 "OSMLCM_DATABASE_URI": "uri",
229 "OSMLCM_DATABASE_COMMONKEY": "commonkey",
230 "OSMLCM_STORAGE_URI": "uri",
231 "OSMLCM_VCA_HELM_CA_CERTS": "helm_ca_certs",
232 },
233 )
234 if config.vca_host:
235 vca_secret_name = f"{self.app.name}-vca-secret"
236 pod_spec_builder.add_secret(
237 vca_secret_name,
238 {
239 "host": config.vca_host,
240 "port": str(config.vca_port),
241 "user": config.vca_user,
242 "pubkey": config.vca_pubkey,
243 "secret": config.vca_secret,
244 "cacert": config.vca_cacert,
245 "cloud": config.vca_cloud,
246 "k8s_cloud": config.vca_k8s_cloud,
247 },
248 )
249 container_builder.add_secret_envs(
250 secret_name=vca_secret_name,
251 envs={
252 # VCA configuration
253 "OSMLCM_VCA_HOST": "host",
254 "OSMLCM_VCA_PORT": "port",
255 "OSMLCM_VCA_USER": "user",
256 "OSMLCM_VCA_PUBKEY": "pubkey",
257 "OSMLCM_VCA_SECRET": "secret",
258 "OSMLCM_VCA_CACERT": "cacert",
259 "OSMLCM_VCA_CLOUD": "cloud",
260 "OSMLCM_VCA_K8S_CLOUD": "k8s_cloud",
261 },
262 )
263 if config.vca_apiproxy:
264 container_builder.add_env("OSMLCM_VCA_APIPROXY", config.vca_apiproxy)
265
266 model_config_envs = {
267 f"OSMLCM_{k.upper()}": v
268 for k, v in self.config.items()
269 if k.startswith("vca_model_config")
270 }
271 if model_config_envs:
272 container_builder.add_envs(model_config_envs)
273 container = container_builder.build()
274
275 # Add container to pod spec
276 pod_spec_builder.add_container(container)
277
278 # Add restart policy
279 restart_policy = PodRestartPolicy()
280 restart_policy.add_secrets()
281 pod_spec_builder.set_restart_policy(restart_policy)
282
283 return pod_spec_builder.build()
284
285
286 VSCODE_WORKSPACE = {
287 "folders": [
288 {"path": "/usr/lib/python3/dist-packages/osm_lcm"},
289 {"path": "/usr/lib/python3/dist-packages/osm_n2vc"},
290 {"path": "/usr/lib/python3/dist-packages/osm_common"},
291 ],
292 "settings": {},
293 "launch": {
294 "version": "0.2.0",
295 "configurations": [
296 {
297 "name": "LCM",
298 "type": "python",
299 "request": "launch",
300 "module": "osm_lcm.lcm",
301 "justMyCode": False,
302 }
303 ],
304 },
305 }
306
307
308 if __name__ == "__main__":
309 main(LcmCharm)
310
311
312 # class ConfigurePodEvent(EventBase):
313 # """Configure Pod event"""
314
315 # pass
316
317
318 # class LcmEvents(CharmEvents):
319 # """LCM Events"""
320
321 # configure_pod = EventSource(ConfigurePodEvent)
322
323
324 # class LcmCharm(CharmBase):
325 # """LCM Charm."""
326
327 # state = StoredState()
328 # on = LcmEvents()
329
330 # def __init__(self, *args) -> NoReturn:
331 # """LCM Charm constructor."""
332 # super().__init__(*args)
333
334 # # Internal state initialization
335 # self.state.set_default(pod_spec=None)
336
337 # # Message bus data initialization
338 # self.state.set_default(message_host=None)
339 # self.state.set_default(message_port=None)
340
341 # # Database data initialization
342 # self.state.set_default(database_uri=None)
343
344 # # RO data initialization
345 # self.state.set_default(ro_host=None)
346 # self.state.set_default(ro_port=None)
347
348 # self.port = LCM_PORT
349 # self.image = OCIImageResource(self, "image")
350
351 # # Registering regular events
352 # self.framework.observe(self.on.start, self.configure_pod)
353 # self.framework.observe(self.on.config_changed, self.configure_pod)
354 # self.framework.observe(self.on.upgrade_charm, self.configure_pod)
355
356 # # Registering custom internal events
357 # self.framework.observe(self.on.configure_pod, self.configure_pod)
358
359 # # Registering required relation events
360 # self.framework.observe(
361 # self.on.kafka_relation_changed, self._on_kafka_relation_changed
362 # )
363 # self.framework.observe(
364 # self.on.mongodb_relation_changed, self._on_mongodb_relation_changed
365 # )
366 # self.framework.observe(
367 # self.on.ro_relation_changed, self._on_ro_relation_changed
368 # )
369
370 # # Registering required relation broken events
371 # self.framework.observe(
372 # self.on.kafka_relation_broken, self._on_kafka_relation_broken
373 # )
374 # self.framework.observe(
375 # self.on.mongodb_relation_broken, self._on_mongodb_relation_broken
376 # )
377 # self.framework.observe(
378 # self.on.ro_relation_broken, self._on_ro_relation_broken
379 # )
380
381 # def _on_kafka_relation_changed(self, event: EventBase) -> NoReturn:
382 # """Reads information about the kafka relation.
383
384 # Args:
385 # event (EventBase): Kafka relation event.
386 # """
387 # message_host = event.relation.data[event.unit].get("host")
388 # message_port = event.relation.data[event.unit].get("port")
389
390 # if (
391 # message_host
392 # and message_port
393 # and (
394 # self.state.message_host != message_host
395 # or self.state.message_port != message_port
396 # )
397 # ):
398 # self.state.message_host = message_host
399 # self.state.message_port = message_port
400 # self.on.configure_pod.emit()
401
402 # def _on_kafka_relation_broken(self, event: EventBase) -> NoReturn:
403 # """Clears data from kafka relation.
404
405 # Args:
406 # event (EventBase): Kafka relation event.
407 # """
408 # self.state.message_host = None
409 # self.state.message_port = None
410 # self.on.configure_pod.emit()
411
412 # def _on_mongodb_relation_changed(self, event: EventBase) -> NoReturn:
413 # """Reads information about the DB relation.
414
415 # Args:
416 # event (EventBase): DB relation event.
417 # """
418 # database_uri = event.relation.data[event.unit].get("connection_string")
419
420 # if database_uri and self.state.database_uri != database_uri:
421 # self.state.database_uri = database_uri
422 # self.on.configure_pod.emit()
423
424 # def _on_mongodb_relation_broken(self, event: EventBase) -> NoReturn:
425 # """Clears data from mongodb relation.
426
427 # Args:
428 # event (EventBase): DB relation event.
429 # """
430 # self.state.database_uri = None
431 # self.on.configure_pod.emit()
432
433 # def _on_ro_relation_changed(self, event: EventBase) -> NoReturn:
434 # """Reads information about the RO relation.
435
436 # Args:
437 # event (EventBase): Keystone relation event.
438 # """
439 # ro_host = event.relation.data[event.unit].get("host")
440 # ro_port = event.relation.data[event.unit].get("port")
441
442 # if (
443 # ro_host
444 # and ro_port
445 # and (self.state.ro_host != ro_host or self.state.ro_port != ro_port)
446 # ):
447 # self.state.ro_host = ro_host
448 # self.state.ro_port = ro_port
449 # self.on.configure_pod.emit()
450
451 # def _on_ro_relation_broken(self, event: EventBase) -> NoReturn:
452 # """Clears data from ro relation.
453
454 # Args:
455 # event (EventBase): Keystone relation event.
456 # """
457 # self.state.ro_host = None
458 # self.state.ro_port = None
459 # self.on.configure_pod.emit()
460
461 # def _missing_relations(self) -> str:
462 # """Checks if there missing relations.
463
464 # Returns:
465 # str: string with missing relations
466 # """
467 # data_status = {
468 # "kafka": self.state.message_host,
469 # "mongodb": self.state.database_uri,
470 # "ro": self.state.ro_host,
471 # }
472
473 # missing_relations = [k for k, v in data_status.items() if not v]
474
475 # return ", ".join(missing_relations)
476
477 # @property
478 # def relation_state(self) -> Dict[str, Any]:
479 # """Collects relation state configuration for pod spec assembly.
480
481 # Returns:
482 # Dict[str, Any]: relation state information.
483 # """
484 # relation_state = {
485 # "message_host": self.state.message_host,
486 # "message_port": self.state.message_port,
487 # "database_uri": self.state.database_uri,
488 # "ro_host": self.state.ro_host,
489 # "ro_port": self.state.ro_port,
490 # }
491
492 # return relation_state
493
494 # def configure_pod(self, event: EventBase) -> NoReturn:
495 # """Assemble the pod spec and apply it, if possible.
496
497 # Args:
498 # event (EventBase): Hook or Relation event that started the
499 # function.
500 # """
501 # if missing := self._missing_relations():
502 # self.unit.status = BlockedStatus(
503 # "Waiting for {0} relation{1}".format(
504 # missing, "s" if "," in missing else ""
505 # )
506 # )
507 # return
508
509 # if not self.unit.is_leader():
510 # self.unit.status = ActiveStatus("ready")
511 # return
512
513 # self.unit.status = MaintenanceStatus("Assembling pod spec")
514
515 # # Fetch image information
516 # try:
517 # self.unit.status = MaintenanceStatus("Fetching image information")
518 # image_info = self.image.fetch()
519 # except OCIImageResourceError:
520 # self.unit.status = BlockedStatus("Error fetching image information")
521 # return
522
523 # try:
524 # pod_spec = make_pod_spec(
525 # image_info,
526 # self.model.config,
527 # self.relation_state,
528 # self.model.app.name,
529 # self.port,
530 # )
531 # except ValueError as exc:
532 # logger.exception("Config/Relation data validation error")
533 # self.unit.status = BlockedStatus(str(exc))
534 # return
535
536 # if self.state.pod_spec != pod_spec:
537 # self.model.pod.set_spec(pod_spec)
538 # self.state.pod_spec = pod_spec
539
540 # self.unit.status = ActiveStatus("ready")
541
542
543 # if __name__ == "__main__":
544 # main(LcmCharm)