blob: af407e928a308228d0cb8f6f53a4678a866c87df [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]
David Garcia1d5c2212021-05-28 16:24:24 +0200111 vca_stablerepourl: Optional[str]
sousaedu86866012021-07-30 14:40:47 +0200112 vca_helm_ca_certs: Optional[str]
sousaedu3ddbbd12021-08-24 19:57:24 +0100113 image_pull_policy: Optional[str]
sousaeduabe73212020-11-04 15:13:35 +0000114
David Garcia49379ce2021-02-24 13:48:22 +0100115 @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
sousaeduabe73212020-11-04 15:13:35 +0000120
sousaedu996a5602021-05-03 00:22:43 +0200121 @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
sousaedu3ddbbd12021-08-24 19:57:24 +0100127 @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
sousaeduabe73212020-11-04 15:13:35 +0000139
David Garcia49379ce2021-02-24 13:48:22 +0100140class LcmCharm(CharmedOsmBase):
sousaeduabe73212020-11-04 15:13:35 +0000141 def __init__(self, *args) -> NoReturn:
David Garciad680be42021-08-17 11:03:55 +0200142 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 )
David Garcia49379ce2021-02-24 13:48:22 +0100149 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)
sousaeduabe73212020-11-04 15:13:35 +0000152
David Garcia49379ce2021-02-24 13:48:22 +0100153 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)
sousaeduabe73212020-11-04 15:13:35 +0000156
David Garcia49379ce2021-02-24 13:48:22 +0100157 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)
sousaeduabe73212020-11-04 15:13:35 +0000160
David Garcia49379ce2021-02-24 13:48:22 +0100161 def _check_missing_dependencies(self, config: ConfigModel):
162 missing_relations = []
sousaeduabe73212020-11-04 15:13:35 +0000163
David Garcia49379ce2021-02-24 13:48:22 +0100164 if self.kafka_client.is_missing_data_in_unit():
165 missing_relations.append("kafka")
sousaedu996a5602021-05-03 00:22:43 +0200166 if not config.mongodb_uri and self.mongodb_client.is_missing_data_in_unit():
David Garcia49379ce2021-02-24 13:48:22 +0100167 missing_relations.append("mongodb")
168 if self.ro_client.is_missing_data_in_app():
169 missing_relations.append("ro")
sousaeduabe73212020-11-04 15:13:35 +0000170
David Garcia49379ce2021-02-24 13:48:22 +0100171 if missing_relations:
172 raise RelationsMissing(missing_relations)
sousaeduabe73212020-11-04 15:13:35 +0000173
David Garcia49379ce2021-02-24 13:48:22 +0100174 def build_pod_spec(self, image_info):
175 # Validate config
176 config = ConfigModel(**dict(self.config))
sousaedu996a5602021-05-03 00:22:43 +0200177
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
David Garcia49379ce2021-02-24 13:48:22 +0100181 # Check relations
182 self._check_missing_dependencies(config)
sousaedu996a5602021-05-03 00:22:43 +0200183
David Garcia49379ce2021-02-24 13:48:22 +0100184 # Create Builder for the PodSpec
185 pod_spec_builder = PodSpecV3Builder()
sousaedu996a5602021-05-03 00:22:43 +0200186
David Garcia49379ce2021-02-24 13:48:22 +0100187 # Build Container
sousaedu3ddbbd12021-08-24 19:57:24 +0100188 container_builder = ContainerV3Builder(
189 self.app.name, image_info, config.image_pull_policy
190 )
David Garcia49379ce2021-02-24 13:48:22 +0100191 container_builder.add_port(name=self.app.name, port=PORT)
192 container_builder.add_envs(
193 {
194 # General configuration
195 "ALLOW_ANONYMOUS_LOGIN": "yes",
196 "OSMLCM_GLOBAL_LOGLEVEL": config.log_level,
197 # RO configuration
198 "OSMLCM_RO_HOST": self.ro_client.host,
199 "OSMLCM_RO_PORT": self.ro_client.port,
200 "OSMLCM_RO_TENANT": "osm",
201 # Kafka configuration
202 "OSMLCM_MESSAGE_DRIVER": "kafka",
203 "OSMLCM_MESSAGE_HOST": self.kafka_client.host,
204 "OSMLCM_MESSAGE_PORT": self.kafka_client.port,
205 # Database configuration
206 "OSMLCM_DATABASE_DRIVER": "mongo",
sousaedu996a5602021-05-03 00:22:43 +0200207 "OSMLCM_DATABASE_URI": config.mongodb_uri
208 or self.mongodb_client.connection_string,
David Garcia49379ce2021-02-24 13:48:22 +0100209 "OSMLCM_DATABASE_COMMONKEY": config.database_commonkey,
210 # Storage configuration
211 "OSMLCM_STORAGE_DRIVER": "mongo",
212 "OSMLCM_STORAGE_PATH": "/app/storage",
213 "OSMLCM_STORAGE_COLLECTION": "files",
sousaeduf5e7f4272021-05-17 21:17:37 +0200214 "OSMLCM_STORAGE_URI": config.mongodb_uri
215 or self.mongodb_client.connection_string,
David Garcia1d5c2212021-05-28 16:24:24 +0200216 "OSMLCM_VCA_STABLEREPOURL": config.vca_stablerepourl,
sousaedu86866012021-07-30 14:40:47 +0200217 "OSMLCM_VCA_HELM_CA_CERTS": config.vca_helm_ca_certs,
David Garcia49379ce2021-02-24 13:48:22 +0100218 }
sousaeduabe73212020-11-04 15:13:35 +0000219 )
David Garciacda4fbc2021-05-05 21:13:58 +0200220 if config.vca_host:
221 container_builder.add_envs(
222 {
223 # VCA configuration
224 "OSMLCM_VCA_HOST": config.vca_host,
225 "OSMLCM_VCA_PORT": config.vca_port,
226 "OSMLCM_VCA_USER": config.vca_user,
227 "OSMLCM_VCA_PUBKEY": config.vca_pubkey,
228 "OSMLCM_VCA_SECRET": config.vca_secret,
229 "OSMLCM_VCA_CACERT": config.vca_cacert,
230 "OSMLCM_VCA_CLOUD": config.vca_cloud,
231 "OSMLCM_VCA_K8S_CLOUD": config.vca_k8s_cloud,
232 }
233 )
234 if config.vca_apiproxy:
235 container_builder.add_env("OSMLCM_VCA_APIPROXY", config.vca_apiproxy)
sousaeduabe73212020-11-04 15:13:35 +0000236
David Garciacda4fbc2021-05-05 21:13:58 +0200237 model_config_envs = {
238 f"OSMLCM_{k.upper()}": v
239 for k, v in self.config.items()
240 if k.startswith("vca_model_config")
241 }
242 if model_config_envs:
243 container_builder.add_envs(model_config_envs)
David Garcia49379ce2021-02-24 13:48:22 +0100244 container = container_builder.build()
sousaedu996a5602021-05-03 00:22:43 +0200245
David Garcia49379ce2021-02-24 13:48:22 +0100246 # Add container to pod spec
247 pod_spec_builder.add_container(container)
sousaedu996a5602021-05-03 00:22:43 +0200248
David Garcia49379ce2021-02-24 13:48:22 +0100249 return pod_spec_builder.build()
sousaeduabe73212020-11-04 15:13:35 +0000250
251
David Garciad680be42021-08-17 11:03:55 +0200252VSCODE_WORKSPACE = {
253 "folders": [
254 {"path": "/usr/lib/python3/dist-packages/osm_lcm"},
255 {"path": "/usr/lib/python3/dist-packages/osm_n2vc"},
256 {"path": "/usr/lib/python3/dist-packages/osm_common"},
257 ],
258 "settings": {},
259 "launch": {
260 "version": "0.2.0",
261 "configurations": [
262 {
263 "name": "LCM",
264 "type": "python",
265 "request": "launch",
266 "module": "osm_lcm.lcm",
267 "justMyCode": False,
268 }
269 ],
270 },
271}
272
273
sousaeduabe73212020-11-04 15:13:35 +0000274if __name__ == "__main__":
275 main(LcmCharm)
David Garcia49379ce2021-02-24 13:48:22 +0100276
277
278# class ConfigurePodEvent(EventBase):
279# """Configure Pod event"""
280
281# pass
282
283
284# class LcmEvents(CharmEvents):
285# """LCM Events"""
286
287# configure_pod = EventSource(ConfigurePodEvent)
288
289
290# class LcmCharm(CharmBase):
291# """LCM Charm."""
292
293# state = StoredState()
294# on = LcmEvents()
295
296# def __init__(self, *args) -> NoReturn:
297# """LCM Charm constructor."""
298# super().__init__(*args)
299
300# # Internal state initialization
301# self.state.set_default(pod_spec=None)
302
303# # Message bus data initialization
304# self.state.set_default(message_host=None)
305# self.state.set_default(message_port=None)
306
307# # Database data initialization
308# self.state.set_default(database_uri=None)
309
310# # RO data initialization
311# self.state.set_default(ro_host=None)
312# self.state.set_default(ro_port=None)
313
314# self.port = LCM_PORT
315# self.image = OCIImageResource(self, "image")
316
317# # Registering regular events
318# self.framework.observe(self.on.start, self.configure_pod)
319# self.framework.observe(self.on.config_changed, self.configure_pod)
320# self.framework.observe(self.on.upgrade_charm, self.configure_pod)
321
322# # Registering custom internal events
323# self.framework.observe(self.on.configure_pod, self.configure_pod)
324
325# # Registering required relation events
326# self.framework.observe(
327# self.on.kafka_relation_changed, self._on_kafka_relation_changed
328# )
329# self.framework.observe(
330# self.on.mongodb_relation_changed, self._on_mongodb_relation_changed
331# )
332# self.framework.observe(
333# self.on.ro_relation_changed, self._on_ro_relation_changed
334# )
335
336# # Registering required relation broken events
337# self.framework.observe(
338# self.on.kafka_relation_broken, self._on_kafka_relation_broken
339# )
340# self.framework.observe(
341# self.on.mongodb_relation_broken, self._on_mongodb_relation_broken
342# )
343# self.framework.observe(
344# self.on.ro_relation_broken, self._on_ro_relation_broken
345# )
346
347# def _on_kafka_relation_changed(self, event: EventBase) -> NoReturn:
348# """Reads information about the kafka relation.
349
350# Args:
351# event (EventBase): Kafka relation event.
352# """
353# message_host = event.relation.data[event.unit].get("host")
354# message_port = event.relation.data[event.unit].get("port")
355
356# if (
357# message_host
358# and message_port
359# and (
360# self.state.message_host != message_host
361# or self.state.message_port != message_port
362# )
363# ):
364# self.state.message_host = message_host
365# self.state.message_port = message_port
366# self.on.configure_pod.emit()
367
368# def _on_kafka_relation_broken(self, event: EventBase) -> NoReturn:
369# """Clears data from kafka relation.
370
371# Args:
372# event (EventBase): Kafka relation event.
373# """
374# self.state.message_host = None
375# self.state.message_port = None
376# self.on.configure_pod.emit()
377
378# def _on_mongodb_relation_changed(self, event: EventBase) -> NoReturn:
379# """Reads information about the DB relation.
380
381# Args:
382# event (EventBase): DB relation event.
383# """
384# database_uri = event.relation.data[event.unit].get("connection_string")
385
386# if database_uri and self.state.database_uri != database_uri:
387# self.state.database_uri = database_uri
388# self.on.configure_pod.emit()
389
390# def _on_mongodb_relation_broken(self, event: EventBase) -> NoReturn:
391# """Clears data from mongodb relation.
392
393# Args:
394# event (EventBase): DB relation event.
395# """
396# self.state.database_uri = None
397# self.on.configure_pod.emit()
398
399# def _on_ro_relation_changed(self, event: EventBase) -> NoReturn:
400# """Reads information about the RO relation.
401
402# Args:
403# event (EventBase): Keystone relation event.
404# """
405# ro_host = event.relation.data[event.unit].get("host")
406# ro_port = event.relation.data[event.unit].get("port")
407
408# if (
409# ro_host
410# and ro_port
411# and (self.state.ro_host != ro_host or self.state.ro_port != ro_port)
412# ):
413# self.state.ro_host = ro_host
414# self.state.ro_port = ro_port
415# self.on.configure_pod.emit()
416
417# def _on_ro_relation_broken(self, event: EventBase) -> NoReturn:
418# """Clears data from ro relation.
419
420# Args:
421# event (EventBase): Keystone relation event.
422# """
423# self.state.ro_host = None
424# self.state.ro_port = None
425# self.on.configure_pod.emit()
426
427# def _missing_relations(self) -> str:
428# """Checks if there missing relations.
429
430# Returns:
431# str: string with missing relations
432# """
433# data_status = {
434# "kafka": self.state.message_host,
435# "mongodb": self.state.database_uri,
436# "ro": self.state.ro_host,
437# }
438
439# missing_relations = [k for k, v in data_status.items() if not v]
440
441# return ", ".join(missing_relations)
442
443# @property
444# def relation_state(self) -> Dict[str, Any]:
445# """Collects relation state configuration for pod spec assembly.
446
447# Returns:
448# Dict[str, Any]: relation state information.
449# """
450# relation_state = {
451# "message_host": self.state.message_host,
452# "message_port": self.state.message_port,
453# "database_uri": self.state.database_uri,
454# "ro_host": self.state.ro_host,
455# "ro_port": self.state.ro_port,
456# }
457
458# return relation_state
459
460# def configure_pod(self, event: EventBase) -> NoReturn:
461# """Assemble the pod spec and apply it, if possible.
462
463# Args:
464# event (EventBase): Hook or Relation event that started the
465# function.
466# """
467# if missing := self._missing_relations():
468# self.unit.status = BlockedStatus(
469# "Waiting for {0} relation{1}".format(
470# missing, "s" if "," in missing else ""
471# )
472# )
473# return
474
475# if not self.unit.is_leader():
476# self.unit.status = ActiveStatus("ready")
477# return
478
479# self.unit.status = MaintenanceStatus("Assembling pod spec")
480
481# # Fetch image information
482# try:
483# self.unit.status = MaintenanceStatus("Fetching image information")
484# image_info = self.image.fetch()
485# except OCIImageResourceError:
486# self.unit.status = BlockedStatus("Error fetching image information")
487# return
488
489# try:
490# pod_spec = make_pod_spec(
491# image_info,
492# self.model.config,
493# self.relation_state,
494# self.model.app.name,
495# self.port,
496# )
497# except ValueError as exc:
498# logger.exception("Config/Relation data validation error")
499# self.unit.status = BlockedStatus(str(exc))
500# return
501
502# if self.state.pod_spec != pod_spec:
503# self.model.pod.set_spec(pod_spec)
504# self.state.pod_spec = pod_spec
505
506# self.unit.status = ActiveStatus("ready")
507
508
509# if __name__ == "__main__":
510# main(LcmCharm)