blob: 2a8c110d1ac902d4aef50fbf28f1999d2fa3aaed [file] [log] [blame]
sousaeduccfacbb2020-11-04 21:44:01 +00001#!/usr/bin/env python3
David Garcia49379ce2021-02-24 13:48:22 +01002# Copyright 2021 Canonical Ltd.
sousaeduccfacbb2020-11-04 21:44:01 +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
David Garcia5d1ec6e2021-03-25 15:04:52 +010025import base64
sousaeduccfacbb2020-11-04 21:44:01 +000026import logging
David Garcia5d1ec6e2021-03-25 15:04:52 +010027from typing import NoReturn, Optional
sousaeduccfacbb2020-11-04 21:44:01 +000028
sousaeduccfacbb2020-11-04 21:44:01 +000029from ops.main import main
David Garcia49379ce2021-02-24 13:48:22 +010030from opslib.osm.charm import CharmedOsmBase, RelationsMissing
David Garciac753dc52021-03-17 15:28:47 +010031from opslib.osm.interfaces.kafka import KafkaClient
32from opslib.osm.interfaces.mongo import MongoClient
33from opslib.osm.interfaces.mysql import MysqlClient
David Garcia141d9352021-09-08 17:48:40 +020034from opslib.osm.pod import (
35 ContainerV3Builder,
36 FilesV3Builder,
37 PodRestartPolicy,
38 PodSpecV3Builder,
39)
David Garciac753dc52021-03-17 15:28:47 +010040from opslib.osm.validator import ModelValidator, validator
David Garcia49379ce2021-02-24 13:48:22 +010041
sousaeduccfacbb2020-11-04 21:44:01 +000042logger = logging.getLogger(__name__)
43
David Garcia49379ce2021-02-24 13:48:22 +010044PORT = 9090
sousaeduccfacbb2020-11-04 21:44:01 +000045
46
David Garcia5d1ec6e2021-03-25 15:04:52 +010047def _check_certificate_data(name: str, content: str):
48 if not name or not content:
49 raise ValueError("certificate name and content must be a non-empty string")
50
51
52def _extract_certificates(certs_config: str):
53 certificates = {}
54 if certs_config:
55 cert_list = certs_config.split(",")
56 for cert in cert_list:
57 name, content = cert.split(":")
58 _check_certificate_data(name, content)
59 certificates[name] = content
60 return certificates
61
62
63def decode(content: str):
64 return base64.b64decode(content.encode("utf-8")).decode("utf-8")
65
66
David Garcia49379ce2021-02-24 13:48:22 +010067class ConfigModel(ModelValidator):
68 enable_ng_ro: bool
69 database_commonkey: str
sousaedu996a5602021-05-03 00:22:43 +020070 mongodb_uri: Optional[str]
David Garcia49379ce2021-02-24 13:48:22 +010071 log_level: str
sousaedu996a5602021-05-03 00:22:43 +020072 mysql_host: Optional[str]
73 mysql_port: Optional[int]
74 mysql_user: Optional[str]
75 mysql_password: Optional[str]
76 mysql_root_password: Optional[str]
David Garcia49379ce2021-02-24 13:48:22 +010077 vim_database: str
78 ro_database: str
79 openmano_tenant: str
David Garcia5d1ec6e2021-03-25 15:04:52 +010080 certificates: Optional[str]
sousaedu0dc25b32021-08-30 16:33:33 +010081 image_pull_policy: str
sousaedu540d9372021-09-29 01:53:30 +010082 debug_mode: bool
83 security_context: bool
David Garcia49379ce2021-02-24 13:48:22 +010084
85 @validator("log_level")
86 def validate_log_level(cls, v):
87 if v not in {"INFO", "DEBUG"}:
88 raise ValueError("value must be INFO or DEBUG")
89 return v
sousaeduccfacbb2020-11-04 21:44:01 +000090
David Garcia5d1ec6e2021-03-25 15:04:52 +010091 @validator("certificates")
92 def validate_certificates(cls, v):
93 # Raises an exception if it cannot extract the certificates
94 _extract_certificates(v)
95 return v
96
sousaedu996a5602021-05-03 00:22:43 +020097 @validator("mongodb_uri")
98 def validate_mongodb_uri(cls, v):
99 if v and not v.startswith("mongodb://"):
100 raise ValueError("mongodb_uri is not properly formed")
101 return v
102
103 @validator("mysql_port")
104 def validate_mysql_port(cls, v):
105 if v and (v <= 0 or v >= 65535):
106 raise ValueError("Mysql port out of range")
107 return v
108
sousaedu3ddbbd12021-08-24 19:57:24 +0100109 @validator("image_pull_policy")
110 def validate_image_pull_policy(cls, v):
111 values = {
112 "always": "Always",
113 "ifnotpresent": "IfNotPresent",
114 "never": "Never",
115 }
116 v = v.lower()
117 if v not in values.keys():
118 raise ValueError("value must be always, ifnotpresent or never")
119 return values[v]
120
David Garcia5d1ec6e2021-03-25 15:04:52 +0100121 @property
122 def certificates_dict(cls):
123 return _extract_certificates(cls.certificates) if cls.certificates else {}
124
sousaeduccfacbb2020-11-04 21:44:01 +0000125
David Garcia49379ce2021-02-24 13:48:22 +0100126class RoCharm(CharmedOsmBase):
127 """GrafanaCharm Charm."""
sousaeduccfacbb2020-11-04 21:44:01 +0000128
129 def __init__(self, *args) -> NoReturn:
David Garcia49379ce2021-02-24 13:48:22 +0100130 """Prometheus Charm constructor."""
David Garciad680be42021-08-17 11:03:55 +0200131 super().__init__(
132 *args,
133 oci_image="image",
134 debug_mode_config_key="debug_mode",
135 debug_pubkey_config_key="debug_pubkey",
136 vscode_workspace=VSCODE_WORKSPACE,
137 )
sousaeduccfacbb2020-11-04 21:44:01 +0000138
David Garcia49379ce2021-02-24 13:48:22 +0100139 self.kafka_client = KafkaClient(self, "kafka")
140 self.framework.observe(self.on["kafka"].relation_changed, self.configure_pod)
141 self.framework.observe(self.on["kafka"].relation_broken, self.configure_pod)
sousaeduccfacbb2020-11-04 21:44:01 +0000142
David Garcia49379ce2021-02-24 13:48:22 +0100143 self.mysql_client = MysqlClient(self, "mysql")
144 self.framework.observe(self.on["mysql"].relation_changed, self.configure_pod)
145 self.framework.observe(self.on["mysql"].relation_broken, self.configure_pod)
sousaeduccfacbb2020-11-04 21:44:01 +0000146
David Garcia49379ce2021-02-24 13:48:22 +0100147 self.mongodb_client = MongoClient(self, "mongodb")
148 self.framework.observe(self.on["mongodb"].relation_changed, self.configure_pod)
149 self.framework.observe(self.on["mongodb"].relation_broken, self.configure_pod)
sousaeduccfacbb2020-11-04 21:44:01 +0000150
David Garcia49379ce2021-02-24 13:48:22 +0100151 self.framework.observe(self.on["ro"].relation_joined, self._publish_ro_info)
sousaeduccfacbb2020-11-04 21:44:01 +0000152
David Garcia49379ce2021-02-24 13:48:22 +0100153 def _publish_ro_info(self, event):
sousaeduccfacbb2020-11-04 21:44:01 +0000154 """Publishes RO information.
155
156 Args:
157 event (EventBase): RO relation event.
158 """
159 if self.unit.is_leader():
160 rel_data = {
161 "host": self.model.app.name,
David Garcia49379ce2021-02-24 13:48:22 +0100162 "port": str(PORT),
sousaeduccfacbb2020-11-04 21:44:01 +0000163 }
164 for k, v in rel_data.items():
165 event.relation.data[self.app][k] = v
166
David Garcia49379ce2021-02-24 13:48:22 +0100167 def _check_missing_dependencies(self, config: ConfigModel):
168 missing_relations = []
169
170 if config.enable_ng_ro:
171 if self.kafka_client.is_missing_data_in_unit():
172 missing_relations.append("kafka")
sousaedu996a5602021-05-03 00:22:43 +0200173 if not config.mongodb_uri and self.mongodb_client.is_missing_data_in_unit():
David Garcia49379ce2021-02-24 13:48:22 +0100174 missing_relations.append("mongodb")
sousaeduccfacbb2020-11-04 21:44:01 +0000175 else:
sousaedu996a5602021-05-03 00:22:43 +0200176 if not config.mysql_host and self.mysql_client.is_missing_data_in_unit():
David Garcia49379ce2021-02-24 13:48:22 +0100177 missing_relations.append("mysql")
178 if missing_relations:
179 raise RelationsMissing(missing_relations)
sousaeduccfacbb2020-11-04 21:44:01 +0000180
sousaedu996a5602021-05-03 00:22:43 +0200181 def _validate_mysql_config(self, config: ConfigModel):
182 invalid_values = []
183 if not config.mysql_user:
184 invalid_values.append("Mysql user is empty")
185 if not config.mysql_password:
186 invalid_values.append("Mysql password is empty")
187 if not config.mysql_root_password:
188 invalid_values.append("Mysql root password empty")
189
190 if invalid_values:
191 raise ValueError("Invalid values: " + ", ".join(invalid_values))
192
David Garcia5d1ec6e2021-03-25 15:04:52 +0100193 def _build_cert_files(
194 self,
195 config: ConfigModel,
196 ):
197 cert_files_builder = FilesV3Builder()
198 for name, content in config.certificates_dict.items():
199 cert_files_builder.add_file(name, decode(content), mode=0o600)
200 return cert_files_builder.build()
201
David Garcia49379ce2021-02-24 13:48:22 +0100202 def build_pod_spec(self, image_info):
203 # Validate config
204 config = ConfigModel(**dict(self.config))
sousaedu996a5602021-05-03 00:22:43 +0200205
206 if config.enable_ng_ro:
207 if config.mongodb_uri and not self.mongodb_client.is_missing_data_in_unit():
208 raise Exception(
209 "Mongodb data cannot be provided via config and relation"
210 )
211 else:
212 if config.mysql_host and not self.mysql_client.is_missing_data_in_unit():
213 raise Exception("Mysql data cannot be provided via config and relation")
214
215 if config.mysql_host:
216 self._validate_mysql_config(config)
217
David Garcia49379ce2021-02-24 13:48:22 +0100218 # Check relations
219 self._check_missing_dependencies(config)
sousaedu996a5602021-05-03 00:22:43 +0200220
sousaedu540d9372021-09-29 01:53:30 +0100221 security_context_enabled = (
222 config.security_context if not config.debug_mode else False
223 )
224
David Garcia49379ce2021-02-24 13:48:22 +0100225 # Create Builder for the PodSpec
sousaedu540d9372021-09-29 01:53:30 +0100226 pod_spec_builder = PodSpecV3Builder(
227 enable_security_context=security_context_enabled
228 )
sousaedu996a5602021-05-03 00:22:43 +0200229
David Garcia49379ce2021-02-24 13:48:22 +0100230 # Build Container
sousaedu3ddbbd12021-08-24 19:57:24 +0100231 container_builder = ContainerV3Builder(
sousaedu540d9372021-09-29 01:53:30 +0100232 self.app.name,
233 image_info,
234 config.image_pull_policy,
235 run_as_non_root=security_context_enabled,
sousaedu3ddbbd12021-08-24 19:57:24 +0100236 )
David Garcia5d1ec6e2021-03-25 15:04:52 +0100237 certs_files = self._build_cert_files(config)
sousaedu996a5602021-05-03 00:22:43 +0200238
David Garcia5d1ec6e2021-03-25 15:04:52 +0100239 if certs_files:
240 container_builder.add_volume_config("certs", "/certs", certs_files)
sousaedu996a5602021-05-03 00:22:43 +0200241
David Garcia49379ce2021-02-24 13:48:22 +0100242 container_builder.add_port(name=self.app.name, port=PORT)
243 container_builder.add_http_readiness_probe(
244 "/ro/" if config.enable_ng_ro else "/openmano/tenants",
245 PORT,
246 initial_delay_seconds=10,
247 period_seconds=10,
248 timeout_seconds=5,
249 failure_threshold=3,
250 )
251 container_builder.add_http_liveness_probe(
252 "/ro/" if config.enable_ng_ro else "/openmano/tenants",
253 PORT,
254 initial_delay_seconds=600,
255 period_seconds=10,
256 timeout_seconds=5,
257 failure_threshold=3,
258 )
259 container_builder.add_envs(
260 {
261 "OSMRO_LOG_LEVEL": config.log_level,
262 }
263 )
sousaedu996a5602021-05-03 00:22:43 +0200264
David Garcia49379ce2021-02-24 13:48:22 +0100265 if config.enable_ng_ro:
David Garcia141d9352021-09-08 17:48:40 +0200266 # Add secrets to the pod
267 mongodb_secret_name = f"{self.app.name}-mongodb-secret"
268 pod_spec_builder.add_secret(
269 mongodb_secret_name,
270 {
271 "uri": config.mongodb_uri or self.mongodb_client.connection_string,
272 "commonkey": config.database_commonkey,
273 },
274 )
David Garcia49379ce2021-02-24 13:48:22 +0100275 container_builder.add_envs(
276 {
277 "OSMRO_MESSAGE_DRIVER": "kafka",
278 "OSMRO_MESSAGE_HOST": self.kafka_client.host,
279 "OSMRO_MESSAGE_PORT": self.kafka_client.port,
280 # MongoDB configuration
281 "OSMRO_DATABASE_DRIVER": "mongo",
David Garcia49379ce2021-02-24 13:48:22 +0100282 }
sousaeduccfacbb2020-11-04 21:44:01 +0000283 )
David Garcia141d9352021-09-08 17:48:40 +0200284 container_builder.add_secret_envs(
285 secret_name=mongodb_secret_name,
286 envs={
287 "OSMRO_DATABASE_URI": "uri",
288 "OSMRO_DATABASE_COMMONKEY": "commonkey",
289 },
290 )
291 restart_policy = PodRestartPolicy()
292 restart_policy.add_secrets(secret_names=(mongodb_secret_name,))
293 pod_spec_builder.set_restart_policy(restart_policy)
sousaeduccfacbb2020-11-04 21:44:01 +0000294
David Garcia49379ce2021-02-24 13:48:22 +0100295 else:
296 container_builder.add_envs(
297 {
sousaedu996a5602021-05-03 00:22:43 +0200298 "RO_DB_HOST": config.mysql_host or self.mysql_client.host,
299 "RO_DB_OVIM_HOST": config.mysql_host or self.mysql_client.host,
300 "RO_DB_PORT": config.mysql_port or self.mysql_client.port,
301 "RO_DB_OVIM_PORT": config.mysql_port or self.mysql_client.port,
302 "RO_DB_USER": config.mysql_user or self.mysql_client.user,
303 "RO_DB_OVIM_USER": config.mysql_user or self.mysql_client.user,
304 "RO_DB_PASSWORD": config.mysql_password
305 or self.mysql_client.password,
306 "RO_DB_OVIM_PASSWORD": config.mysql_password
307 or self.mysql_client.password,
308 "RO_DB_ROOT_PASSWORD": config.mysql_root_password
309 or self.mysql_client.root_password,
310 "RO_DB_OVIM_ROOT_PASSWORD": config.mysql_root_password
311 or self.mysql_client.root_password,
David Garcia49379ce2021-02-24 13:48:22 +0100312 "RO_DB_NAME": config.ro_database,
313 "RO_DB_OVIM_NAME": config.vim_database,
314 "OPENMANO_TENANT": config.openmano_tenant,
315 }
316 )
317 container = container_builder.build()
sousaedu996a5602021-05-03 00:22:43 +0200318
David Garcia49379ce2021-02-24 13:48:22 +0100319 # Add container to pod spec
320 pod_spec_builder.add_container(container)
sousaedu996a5602021-05-03 00:22:43 +0200321
David Garcia49379ce2021-02-24 13:48:22 +0100322 return pod_spec_builder.build()
sousaeduccfacbb2020-11-04 21:44:01 +0000323
324
David Garciad680be42021-08-17 11:03:55 +0200325VSCODE_WORKSPACE = {
326 "folders": [
327 {"path": "/usr/lib/python3/dist-packages/osm_ng_ro"},
328 {"path": "/usr/lib/python3/dist-packages/osm_common"},
329 {"path": "/usr/lib/python3/dist-packages/osm_ro_plugin"},
330 {"path": "/usr/lib/python3/dist-packages/osm_rosdn_arista_cloudvision"},
331 {"path": "/usr/lib/python3/dist-packages/osm_rosdn_dpb"},
332 {"path": "/usr/lib/python3/dist-packages/osm_rosdn_dynpac"},
333 {"path": "/usr/lib/python3/dist-packages/osm_rosdn_floodlightof"},
334 {"path": "/usr/lib/python3/dist-packages/osm_rosdn_ietfl2vpn"},
335 {"path": "/usr/lib/python3/dist-packages/osm_rosdn_juniper_contrail"},
336 {"path": "/usr/lib/python3/dist-packages/osm_rosdn_odlof"},
337 {"path": "/usr/lib/python3/dist-packages/osm_rosdn_onos_vpls"},
338 {"path": "/usr/lib/python3/dist-packages/osm_rosdn_onosof"},
339 {"path": "/usr/lib/python3/dist-packages/osm_rovim_aws"},
340 {"path": "/usr/lib/python3/dist-packages/osm_rovim_azure"},
341 {"path": "/usr/lib/python3/dist-packages/osm_rovim_fos"},
342 {"path": "/usr/lib/python3/dist-packages/osm_rovim_opennebula"},
343 {"path": "/usr/lib/python3/dist-packages/osm_rovim_openstack"},
344 {"path": "/usr/lib/python3/dist-packages/osm_rovim_openvim"},
345 {"path": "/usr/lib/python3/dist-packages/osm_rovim_vmware"},
346 ],
347 "launch": {
348 "configurations": [
349 {
350 "module": "osm_ng_ro.ro_main",
351 "name": "NG RO",
352 "request": "launch",
353 "type": "python",
354 "justMyCode": False,
355 }
356 ],
357 "version": "0.2.0",
358 },
359 "settings": {},
360}
361
sousaeduccfacbb2020-11-04 21:44:01 +0000362if __name__ == "__main__":
363 main(RoCharm)