blob: 4e0b4bed4a2c857e72c66b64f450ee4572580d5d [file] [log] [blame]
sousaeduabe73212020-11-04 15:13:35 +00001#!/usr/bin/env python3
David Garcia49379ce2021-02-24 13:48:22 +01002# Copyright 2021 Canonical Ltd.
sousaeduabe73212020-11-04 15:13:35 +00003#
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
David Garcia49379ce2021-02-24 13:48:22 +010023# pylint: disable=E0213
24
25
sousaeduabe73212020-11-04 15:13:35 +000026import logging
David Garciac753dc52021-03-17 15:28:47 +010027from typing import NoReturn, Optional
28
sousaeduabe73212020-11-04 15:13:35 +000029
sousaeduabe73212020-11-04 15:13:35 +000030from ops.main import main
David Garcia49379ce2021-02-24 13:48:22 +010031from opslib.osm.charm import CharmedOsmBase, RelationsMissing
David Garciac753dc52021-03-17 15:28:47 +010032from opslib.osm.interfaces.http import HttpClient
David Garcia49379ce2021-02-24 13:48:22 +010033from opslib.osm.interfaces.kafka import KafkaClient
34from opslib.osm.interfaces.mongo import MongoClient
David Garciac753dc52021-03-17 15:28:47 +010035from opslib.osm.pod import ContainerV3Builder, PodSpecV3Builder
36from opslib.osm.validator import ModelValidator, validator
David Garcia49379ce2021-02-24 13:48:22 +010037
sousaeduabe73212020-11-04 15:13:35 +000038
39logger = logging.getLogger(__name__)
40
David Garcia49379ce2021-02-24 13:48:22 +010041PORT = 9999
sousaeduabe73212020-11-04 15:13:35 +000042
43
David Garcia49379ce2021-02-24 13:48:22 +010044class ConfigModel(ModelValidator):
David Garciacda4fbc2021-05-05 21:13:58 +020045 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]
David Garcia49379ce2021-02-24 13:48:22 +010053 database_commonkey: str
sousaedu996a5602021-05-03 00:22:43 +020054 mongodb_uri: Optional[str]
David Garcia49379ce2021-02-24 13:48:22 +010055 log_level: str
56 vca_apiproxy: Optional[str]
David Garciac753dc52021-03-17 15:28:47 +010057 # 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]
sousaeduabe73212020-11-04 15:13:35 +0000111
David Garcia49379ce2021-02-24 13:48:22 +0100112 @validator("log_level")
113 def validate_log_level(cls, v):
114 if v not in {"INFO", "DEBUG"}:
115 raise ValueError("value must be INFO or DEBUG")
116 return v
sousaeduabe73212020-11-04 15:13:35 +0000117
sousaedu996a5602021-05-03 00:22:43 +0200118 @validator("mongodb_uri")
119 def validate_mongodb_uri(cls, v):
120 if v and not v.startswith("mongodb://"):
121 raise ValueError("mongodb_uri is not properly formed")
122 return v
123
sousaeduabe73212020-11-04 15:13:35 +0000124
David Garcia49379ce2021-02-24 13:48:22 +0100125class LcmCharm(CharmedOsmBase):
sousaeduabe73212020-11-04 15:13:35 +0000126 def __init__(self, *args) -> NoReturn:
David Garcia49379ce2021-02-24 13:48:22 +0100127 super().__init__(*args, oci_image="image")
sousaeduabe73212020-11-04 15:13:35 +0000128
David Garcia49379ce2021-02-24 13:48:22 +0100129 self.kafka_client = KafkaClient(self, "kafka")
130 self.framework.observe(self.on["kafka"].relation_changed, self.configure_pod)
131 self.framework.observe(self.on["kafka"].relation_broken, self.configure_pod)
sousaeduabe73212020-11-04 15:13:35 +0000132
David Garcia49379ce2021-02-24 13:48:22 +0100133 self.mongodb_client = MongoClient(self, "mongodb")
134 self.framework.observe(self.on["mongodb"].relation_changed, self.configure_pod)
135 self.framework.observe(self.on["mongodb"].relation_broken, self.configure_pod)
sousaeduabe73212020-11-04 15:13:35 +0000136
David Garcia49379ce2021-02-24 13:48:22 +0100137 self.ro_client = HttpClient(self, "ro")
138 self.framework.observe(self.on["ro"].relation_changed, self.configure_pod)
139 self.framework.observe(self.on["ro"].relation_broken, self.configure_pod)
sousaeduabe73212020-11-04 15:13:35 +0000140
David Garcia49379ce2021-02-24 13:48:22 +0100141 def _check_missing_dependencies(self, config: ConfigModel):
142 missing_relations = []
sousaeduabe73212020-11-04 15:13:35 +0000143
David Garcia49379ce2021-02-24 13:48:22 +0100144 if self.kafka_client.is_missing_data_in_unit():
145 missing_relations.append("kafka")
sousaedu996a5602021-05-03 00:22:43 +0200146 if not config.mongodb_uri and self.mongodb_client.is_missing_data_in_unit():
David Garcia49379ce2021-02-24 13:48:22 +0100147 missing_relations.append("mongodb")
148 if self.ro_client.is_missing_data_in_app():
149 missing_relations.append("ro")
sousaeduabe73212020-11-04 15:13:35 +0000150
David Garcia49379ce2021-02-24 13:48:22 +0100151 if missing_relations:
152 raise RelationsMissing(missing_relations)
sousaeduabe73212020-11-04 15:13:35 +0000153
David Garcia49379ce2021-02-24 13:48:22 +0100154 def build_pod_spec(self, image_info):
155 # Validate config
156 config = ConfigModel(**dict(self.config))
sousaedu996a5602021-05-03 00:22:43 +0200157
158 if config.mongodb_uri and not self.mongodb_client.is_missing_data_in_unit():
159 raise Exception("Mongodb data cannot be provided via config and relation")
160
David Garcia49379ce2021-02-24 13:48:22 +0100161 # Check relations
162 self._check_missing_dependencies(config)
sousaedu996a5602021-05-03 00:22:43 +0200163
David Garcia49379ce2021-02-24 13:48:22 +0100164 # Create Builder for the PodSpec
165 pod_spec_builder = PodSpecV3Builder()
sousaedu996a5602021-05-03 00:22:43 +0200166
David Garcia49379ce2021-02-24 13:48:22 +0100167 # Build Container
168 container_builder = ContainerV3Builder(self.app.name, image_info)
169 container_builder.add_port(name=self.app.name, port=PORT)
170 container_builder.add_envs(
171 {
172 # General configuration
173 "ALLOW_ANONYMOUS_LOGIN": "yes",
174 "OSMLCM_GLOBAL_LOGLEVEL": config.log_level,
175 # RO configuration
176 "OSMLCM_RO_HOST": self.ro_client.host,
177 "OSMLCM_RO_PORT": self.ro_client.port,
178 "OSMLCM_RO_TENANT": "osm",
179 # Kafka configuration
180 "OSMLCM_MESSAGE_DRIVER": "kafka",
181 "OSMLCM_MESSAGE_HOST": self.kafka_client.host,
182 "OSMLCM_MESSAGE_PORT": self.kafka_client.port,
183 # Database configuration
184 "OSMLCM_DATABASE_DRIVER": "mongo",
sousaedu996a5602021-05-03 00:22:43 +0200185 "OSMLCM_DATABASE_URI": config.mongodb_uri
186 or self.mongodb_client.connection_string,
David Garcia49379ce2021-02-24 13:48:22 +0100187 "OSMLCM_DATABASE_COMMONKEY": config.database_commonkey,
188 # Storage configuration
189 "OSMLCM_STORAGE_DRIVER": "mongo",
190 "OSMLCM_STORAGE_PATH": "/app/storage",
191 "OSMLCM_STORAGE_COLLECTION": "files",
sousaeduf5e7f4272021-05-17 21:17:37 +0200192 "OSMLCM_STORAGE_URI": config.mongodb_uri
193 or self.mongodb_client.connection_string,
David Garcia49379ce2021-02-24 13:48:22 +0100194 }
sousaeduabe73212020-11-04 15:13:35 +0000195 )
David Garciacda4fbc2021-05-05 21:13:58 +0200196 if config.vca_host:
197 container_builder.add_envs(
198 {
199 # VCA configuration
200 "OSMLCM_VCA_HOST": config.vca_host,
201 "OSMLCM_VCA_PORT": config.vca_port,
202 "OSMLCM_VCA_USER": config.vca_user,
203 "OSMLCM_VCA_PUBKEY": config.vca_pubkey,
204 "OSMLCM_VCA_SECRET": config.vca_secret,
205 "OSMLCM_VCA_CACERT": config.vca_cacert,
206 "OSMLCM_VCA_CLOUD": config.vca_cloud,
207 "OSMLCM_VCA_K8S_CLOUD": config.vca_k8s_cloud,
208 }
209 )
210 if config.vca_apiproxy:
211 container_builder.add_env("OSMLCM_VCA_APIPROXY", config.vca_apiproxy)
sousaeduabe73212020-11-04 15:13:35 +0000212
David Garciacda4fbc2021-05-05 21:13:58 +0200213 model_config_envs = {
214 f"OSMLCM_{k.upper()}": v
215 for k, v in self.config.items()
216 if k.startswith("vca_model_config")
217 }
218 if model_config_envs:
219 container_builder.add_envs(model_config_envs)
David Garcia49379ce2021-02-24 13:48:22 +0100220 container = container_builder.build()
sousaedu996a5602021-05-03 00:22:43 +0200221
David Garcia49379ce2021-02-24 13:48:22 +0100222 # Add container to pod spec
223 pod_spec_builder.add_container(container)
sousaedu996a5602021-05-03 00:22:43 +0200224
David Garcia49379ce2021-02-24 13:48:22 +0100225 return pod_spec_builder.build()
sousaeduabe73212020-11-04 15:13:35 +0000226
227
228if __name__ == "__main__":
229 main(LcmCharm)
David Garcia49379ce2021-02-24 13:48:22 +0100230
231
232# class ConfigurePodEvent(EventBase):
233# """Configure Pod event"""
234
235# pass
236
237
238# class LcmEvents(CharmEvents):
239# """LCM Events"""
240
241# configure_pod = EventSource(ConfigurePodEvent)
242
243
244# class LcmCharm(CharmBase):
245# """LCM Charm."""
246
247# state = StoredState()
248# on = LcmEvents()
249
250# def __init__(self, *args) -> NoReturn:
251# """LCM Charm constructor."""
252# super().__init__(*args)
253
254# # Internal state initialization
255# self.state.set_default(pod_spec=None)
256
257# # Message bus data initialization
258# self.state.set_default(message_host=None)
259# self.state.set_default(message_port=None)
260
261# # Database data initialization
262# self.state.set_default(database_uri=None)
263
264# # RO data initialization
265# self.state.set_default(ro_host=None)
266# self.state.set_default(ro_port=None)
267
268# self.port = LCM_PORT
269# self.image = OCIImageResource(self, "image")
270
271# # Registering regular events
272# self.framework.observe(self.on.start, self.configure_pod)
273# self.framework.observe(self.on.config_changed, self.configure_pod)
274# self.framework.observe(self.on.upgrade_charm, self.configure_pod)
275
276# # Registering custom internal events
277# self.framework.observe(self.on.configure_pod, self.configure_pod)
278
279# # Registering required relation events
280# self.framework.observe(
281# self.on.kafka_relation_changed, self._on_kafka_relation_changed
282# )
283# self.framework.observe(
284# self.on.mongodb_relation_changed, self._on_mongodb_relation_changed
285# )
286# self.framework.observe(
287# self.on.ro_relation_changed, self._on_ro_relation_changed
288# )
289
290# # Registering required relation broken events
291# self.framework.observe(
292# self.on.kafka_relation_broken, self._on_kafka_relation_broken
293# )
294# self.framework.observe(
295# self.on.mongodb_relation_broken, self._on_mongodb_relation_broken
296# )
297# self.framework.observe(
298# self.on.ro_relation_broken, self._on_ro_relation_broken
299# )
300
301# def _on_kafka_relation_changed(self, event: EventBase) -> NoReturn:
302# """Reads information about the kafka relation.
303
304# Args:
305# event (EventBase): Kafka relation event.
306# """
307# message_host = event.relation.data[event.unit].get("host")
308# message_port = event.relation.data[event.unit].get("port")
309
310# if (
311# message_host
312# and message_port
313# and (
314# self.state.message_host != message_host
315# or self.state.message_port != message_port
316# )
317# ):
318# self.state.message_host = message_host
319# self.state.message_port = message_port
320# self.on.configure_pod.emit()
321
322# def _on_kafka_relation_broken(self, event: EventBase) -> NoReturn:
323# """Clears data from kafka relation.
324
325# Args:
326# event (EventBase): Kafka relation event.
327# """
328# self.state.message_host = None
329# self.state.message_port = None
330# self.on.configure_pod.emit()
331
332# def _on_mongodb_relation_changed(self, event: EventBase) -> NoReturn:
333# """Reads information about the DB relation.
334
335# Args:
336# event (EventBase): DB relation event.
337# """
338# database_uri = event.relation.data[event.unit].get("connection_string")
339
340# if database_uri and self.state.database_uri != database_uri:
341# self.state.database_uri = database_uri
342# self.on.configure_pod.emit()
343
344# def _on_mongodb_relation_broken(self, event: EventBase) -> NoReturn:
345# """Clears data from mongodb relation.
346
347# Args:
348# event (EventBase): DB relation event.
349# """
350# self.state.database_uri = None
351# self.on.configure_pod.emit()
352
353# def _on_ro_relation_changed(self, event: EventBase) -> NoReturn:
354# """Reads information about the RO relation.
355
356# Args:
357# event (EventBase): Keystone relation event.
358# """
359# ro_host = event.relation.data[event.unit].get("host")
360# ro_port = event.relation.data[event.unit].get("port")
361
362# if (
363# ro_host
364# and ro_port
365# and (self.state.ro_host != ro_host or self.state.ro_port != ro_port)
366# ):
367# self.state.ro_host = ro_host
368# self.state.ro_port = ro_port
369# self.on.configure_pod.emit()
370
371# def _on_ro_relation_broken(self, event: EventBase) -> NoReturn:
372# """Clears data from ro relation.
373
374# Args:
375# event (EventBase): Keystone relation event.
376# """
377# self.state.ro_host = None
378# self.state.ro_port = None
379# self.on.configure_pod.emit()
380
381# def _missing_relations(self) -> str:
382# """Checks if there missing relations.
383
384# Returns:
385# str: string with missing relations
386# """
387# data_status = {
388# "kafka": self.state.message_host,
389# "mongodb": self.state.database_uri,
390# "ro": self.state.ro_host,
391# }
392
393# missing_relations = [k for k, v in data_status.items() if not v]
394
395# return ", ".join(missing_relations)
396
397# @property
398# def relation_state(self) -> Dict[str, Any]:
399# """Collects relation state configuration for pod spec assembly.
400
401# Returns:
402# Dict[str, Any]: relation state information.
403# """
404# relation_state = {
405# "message_host": self.state.message_host,
406# "message_port": self.state.message_port,
407# "database_uri": self.state.database_uri,
408# "ro_host": self.state.ro_host,
409# "ro_port": self.state.ro_port,
410# }
411
412# return relation_state
413
414# def configure_pod(self, event: EventBase) -> NoReturn:
415# """Assemble the pod spec and apply it, if possible.
416
417# Args:
418# event (EventBase): Hook or Relation event that started the
419# function.
420# """
421# if missing := self._missing_relations():
422# self.unit.status = BlockedStatus(
423# "Waiting for {0} relation{1}".format(
424# missing, "s" if "," in missing else ""
425# )
426# )
427# return
428
429# if not self.unit.is_leader():
430# self.unit.status = ActiveStatus("ready")
431# return
432
433# self.unit.status = MaintenanceStatus("Assembling pod spec")
434
435# # Fetch image information
436# try:
437# self.unit.status = MaintenanceStatus("Fetching image information")
438# image_info = self.image.fetch()
439# except OCIImageResourceError:
440# self.unit.status = BlockedStatus("Error fetching image information")
441# return
442
443# try:
444# pod_spec = make_pod_spec(
445# image_info,
446# self.model.config,
447# self.relation_state,
448# self.model.app.name,
449# self.port,
450# )
451# except ValueError as exc:
452# logger.exception("Config/Relation data validation error")
453# self.unit.status = BlockedStatus(str(exc))
454# return
455
456# if self.state.pod_spec != pod_spec:
457# self.model.pod.set_spec(pod_spec)
458# self.state.pod_spec = pod_spec
459
460# self.unit.status = ActiveStatus("ready")
461
462
463# if __name__ == "__main__":
464# main(LcmCharm)