| sousaedu | ccfacbb | 2020-11-04 21:44:01 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 2 | # Copyright 2021 Canonical Ltd. |
| sousaedu | ccfacbb | 2020-11-04 21:44:01 +0000 | [diff] [blame] | 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 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 23 | # pylint: disable=E0213 |
| 24 | |
| David Garcia | 5d1ec6e | 2021-03-25 15:04:52 +0100 | [diff] [blame] | 25 | import base64 |
| sousaedu | ccfacbb | 2020-11-04 21:44:01 +0000 | [diff] [blame] | 26 | import logging |
| David Garcia | 5d1ec6e | 2021-03-25 15:04:52 +0100 | [diff] [blame] | 27 | from typing import NoReturn, Optional |
| sousaedu | ccfacbb | 2020-11-04 21:44:01 +0000 | [diff] [blame] | 28 | |
| sousaedu | ccfacbb | 2020-11-04 21:44:01 +0000 | [diff] [blame] | 29 | from ops.main import main |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 30 | from opslib.osm.charm import CharmedOsmBase, RelationsMissing |
| David Garcia | c753dc5 | 2021-03-17 15:28:47 +0100 | [diff] [blame] | 31 | from opslib.osm.interfaces.kafka import KafkaClient |
| 32 | from opslib.osm.interfaces.mongo import MongoClient |
| 33 | from opslib.osm.interfaces.mysql import MysqlClient |
| David Garcia | 5d1ec6e | 2021-03-25 15:04:52 +0100 | [diff] [blame] | 34 | from opslib.osm.pod import ContainerV3Builder, FilesV3Builder, PodSpecV3Builder |
| David Garcia | c753dc5 | 2021-03-17 15:28:47 +0100 | [diff] [blame] | 35 | from opslib.osm.validator import ModelValidator, validator |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 36 | |
| sousaedu | ccfacbb | 2020-11-04 21:44:01 +0000 | [diff] [blame] | 37 | logger = logging.getLogger(__name__) |
| 38 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 39 | PORT = 9090 |
| sousaedu | ccfacbb | 2020-11-04 21:44:01 +0000 | [diff] [blame] | 40 | |
| 41 | |
| David Garcia | 5d1ec6e | 2021-03-25 15:04:52 +0100 | [diff] [blame] | 42 | def _check_certificate_data(name: str, content: str): |
| 43 | if not name or not content: |
| 44 | raise ValueError("certificate name and content must be a non-empty string") |
| 45 | |
| 46 | |
| 47 | def _extract_certificates(certs_config: str): |
| 48 | certificates = {} |
| 49 | if certs_config: |
| 50 | cert_list = certs_config.split(",") |
| 51 | for cert in cert_list: |
| 52 | name, content = cert.split(":") |
| 53 | _check_certificate_data(name, content) |
| 54 | certificates[name] = content |
| 55 | return certificates |
| 56 | |
| 57 | |
| 58 | def decode(content: str): |
| 59 | return base64.b64decode(content.encode("utf-8")).decode("utf-8") |
| 60 | |
| 61 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 62 | class ConfigModel(ModelValidator): |
| 63 | enable_ng_ro: bool |
| 64 | database_commonkey: str |
| sousaedu | 996a560 | 2021-05-03 00:22:43 +0200 | [diff] [blame] | 65 | mongodb_uri: Optional[str] |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 66 | log_level: str |
| sousaedu | 996a560 | 2021-05-03 00:22:43 +0200 | [diff] [blame] | 67 | mysql_host: Optional[str] |
| 68 | mysql_port: Optional[int] |
| 69 | mysql_user: Optional[str] |
| 70 | mysql_password: Optional[str] |
| 71 | mysql_root_password: Optional[str] |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 72 | vim_database: str |
| 73 | ro_database: str |
| 74 | openmano_tenant: str |
| David Garcia | 5d1ec6e | 2021-03-25 15:04:52 +0100 | [diff] [blame] | 75 | certificates: Optional[str] |
| sousaedu | 3ddbbd1 | 2021-08-24 19:57:24 +0100 | [diff] [blame] | 76 | image_pull_policy: Optional[str] |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 77 | |
| 78 | @validator("log_level") |
| 79 | def validate_log_level(cls, v): |
| 80 | if v not in {"INFO", "DEBUG"}: |
| 81 | raise ValueError("value must be INFO or DEBUG") |
| 82 | return v |
| sousaedu | ccfacbb | 2020-11-04 21:44:01 +0000 | [diff] [blame] | 83 | |
| David Garcia | 5d1ec6e | 2021-03-25 15:04:52 +0100 | [diff] [blame] | 84 | @validator("certificates") |
| 85 | def validate_certificates(cls, v): |
| 86 | # Raises an exception if it cannot extract the certificates |
| 87 | _extract_certificates(v) |
| 88 | return v |
| 89 | |
| sousaedu | 996a560 | 2021-05-03 00:22:43 +0200 | [diff] [blame] | 90 | @validator("mongodb_uri") |
| 91 | def validate_mongodb_uri(cls, v): |
| 92 | if v and not v.startswith("mongodb://"): |
| 93 | raise ValueError("mongodb_uri is not properly formed") |
| 94 | return v |
| 95 | |
| 96 | @validator("mysql_port") |
| 97 | def validate_mysql_port(cls, v): |
| 98 | if v and (v <= 0 or v >= 65535): |
| 99 | raise ValueError("Mysql port out of range") |
| 100 | return v |
| 101 | |
| sousaedu | 3ddbbd1 | 2021-08-24 19:57:24 +0100 | [diff] [blame] | 102 | @validator("image_pull_policy") |
| 103 | def validate_image_pull_policy(cls, v): |
| 104 | values = { |
| 105 | "always": "Always", |
| 106 | "ifnotpresent": "IfNotPresent", |
| 107 | "never": "Never", |
| 108 | } |
| 109 | v = v.lower() |
| 110 | if v not in values.keys(): |
| 111 | raise ValueError("value must be always, ifnotpresent or never") |
| 112 | return values[v] |
| 113 | |
| David Garcia | 5d1ec6e | 2021-03-25 15:04:52 +0100 | [diff] [blame] | 114 | @property |
| 115 | def certificates_dict(cls): |
| 116 | return _extract_certificates(cls.certificates) if cls.certificates else {} |
| 117 | |
| sousaedu | ccfacbb | 2020-11-04 21:44:01 +0000 | [diff] [blame] | 118 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 119 | class RoCharm(CharmedOsmBase): |
| 120 | """GrafanaCharm Charm.""" |
| sousaedu | ccfacbb | 2020-11-04 21:44:01 +0000 | [diff] [blame] | 121 | |
| 122 | def __init__(self, *args) -> NoReturn: |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 123 | """Prometheus Charm constructor.""" |
| David Garcia | d680be4 | 2021-08-17 11:03:55 +0200 | [diff] [blame] | 124 | super().__init__( |
| 125 | *args, |
| 126 | oci_image="image", |
| 127 | debug_mode_config_key="debug_mode", |
| 128 | debug_pubkey_config_key="debug_pubkey", |
| 129 | vscode_workspace=VSCODE_WORKSPACE, |
| 130 | ) |
| sousaedu | ccfacbb | 2020-11-04 21:44:01 +0000 | [diff] [blame] | 131 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 132 | self.kafka_client = KafkaClient(self, "kafka") |
| 133 | self.framework.observe(self.on["kafka"].relation_changed, self.configure_pod) |
| 134 | self.framework.observe(self.on["kafka"].relation_broken, self.configure_pod) |
| sousaedu | ccfacbb | 2020-11-04 21:44:01 +0000 | [diff] [blame] | 135 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 136 | self.mysql_client = MysqlClient(self, "mysql") |
| 137 | self.framework.observe(self.on["mysql"].relation_changed, self.configure_pod) |
| 138 | self.framework.observe(self.on["mysql"].relation_broken, self.configure_pod) |
| sousaedu | ccfacbb | 2020-11-04 21:44:01 +0000 | [diff] [blame] | 139 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 140 | self.mongodb_client = MongoClient(self, "mongodb") |
| 141 | self.framework.observe(self.on["mongodb"].relation_changed, self.configure_pod) |
| 142 | self.framework.observe(self.on["mongodb"].relation_broken, self.configure_pod) |
| sousaedu | ccfacbb | 2020-11-04 21:44:01 +0000 | [diff] [blame] | 143 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 144 | self.framework.observe(self.on["ro"].relation_joined, self._publish_ro_info) |
| sousaedu | ccfacbb | 2020-11-04 21:44:01 +0000 | [diff] [blame] | 145 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 146 | def _publish_ro_info(self, event): |
| sousaedu | ccfacbb | 2020-11-04 21:44:01 +0000 | [diff] [blame] | 147 | """Publishes RO information. |
| 148 | |
| 149 | Args: |
| 150 | event (EventBase): RO relation event. |
| 151 | """ |
| 152 | if self.unit.is_leader(): |
| 153 | rel_data = { |
| 154 | "host": self.model.app.name, |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 155 | "port": str(PORT), |
| sousaedu | ccfacbb | 2020-11-04 21:44:01 +0000 | [diff] [blame] | 156 | } |
| 157 | for k, v in rel_data.items(): |
| 158 | event.relation.data[self.app][k] = v |
| 159 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 160 | def _check_missing_dependencies(self, config: ConfigModel): |
| 161 | missing_relations = [] |
| 162 | |
| 163 | if config.enable_ng_ro: |
| 164 | if self.kafka_client.is_missing_data_in_unit(): |
| 165 | missing_relations.append("kafka") |
| sousaedu | 996a560 | 2021-05-03 00:22:43 +0200 | [diff] [blame] | 166 | if not config.mongodb_uri and self.mongodb_client.is_missing_data_in_unit(): |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 167 | missing_relations.append("mongodb") |
| sousaedu | ccfacbb | 2020-11-04 21:44:01 +0000 | [diff] [blame] | 168 | else: |
| sousaedu | 996a560 | 2021-05-03 00:22:43 +0200 | [diff] [blame] | 169 | if not config.mysql_host and self.mysql_client.is_missing_data_in_unit(): |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 170 | missing_relations.append("mysql") |
| 171 | if missing_relations: |
| 172 | raise RelationsMissing(missing_relations) |
| sousaedu | ccfacbb | 2020-11-04 21:44:01 +0000 | [diff] [blame] | 173 | |
| sousaedu | 996a560 | 2021-05-03 00:22:43 +0200 | [diff] [blame] | 174 | def _validate_mysql_config(self, config: ConfigModel): |
| 175 | invalid_values = [] |
| 176 | if not config.mysql_user: |
| 177 | invalid_values.append("Mysql user is empty") |
| 178 | if not config.mysql_password: |
| 179 | invalid_values.append("Mysql password is empty") |
| 180 | if not config.mysql_root_password: |
| 181 | invalid_values.append("Mysql root password empty") |
| 182 | |
| 183 | if invalid_values: |
| 184 | raise ValueError("Invalid values: " + ", ".join(invalid_values)) |
| 185 | |
| David Garcia | 5d1ec6e | 2021-03-25 15:04:52 +0100 | [diff] [blame] | 186 | def _build_cert_files( |
| 187 | self, |
| 188 | config: ConfigModel, |
| 189 | ): |
| 190 | cert_files_builder = FilesV3Builder() |
| 191 | for name, content in config.certificates_dict.items(): |
| 192 | cert_files_builder.add_file(name, decode(content), mode=0o600) |
| 193 | return cert_files_builder.build() |
| 194 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 195 | def build_pod_spec(self, image_info): |
| 196 | # Validate config |
| 197 | config = ConfigModel(**dict(self.config)) |
| sousaedu | 996a560 | 2021-05-03 00:22:43 +0200 | [diff] [blame] | 198 | |
| 199 | if config.enable_ng_ro: |
| 200 | if config.mongodb_uri and not self.mongodb_client.is_missing_data_in_unit(): |
| 201 | raise Exception( |
| 202 | "Mongodb data cannot be provided via config and relation" |
| 203 | ) |
| 204 | else: |
| 205 | if config.mysql_host and not self.mysql_client.is_missing_data_in_unit(): |
| 206 | raise Exception("Mysql data cannot be provided via config and relation") |
| 207 | |
| 208 | if config.mysql_host: |
| 209 | self._validate_mysql_config(config) |
| 210 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 211 | # Check relations |
| 212 | self._check_missing_dependencies(config) |
| sousaedu | 996a560 | 2021-05-03 00:22:43 +0200 | [diff] [blame] | 213 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 214 | # Create Builder for the PodSpec |
| 215 | pod_spec_builder = PodSpecV3Builder() |
| sousaedu | 996a560 | 2021-05-03 00:22:43 +0200 | [diff] [blame] | 216 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 217 | # Build Container |
| sousaedu | 3ddbbd1 | 2021-08-24 19:57:24 +0100 | [diff] [blame] | 218 | container_builder = ContainerV3Builder( |
| 219 | self.app.name, image_info, config.image_pull_policy |
| 220 | ) |
| David Garcia | 5d1ec6e | 2021-03-25 15:04:52 +0100 | [diff] [blame] | 221 | certs_files = self._build_cert_files(config) |
| sousaedu | 996a560 | 2021-05-03 00:22:43 +0200 | [diff] [blame] | 222 | |
| David Garcia | 5d1ec6e | 2021-03-25 15:04:52 +0100 | [diff] [blame] | 223 | if certs_files: |
| 224 | container_builder.add_volume_config("certs", "/certs", certs_files) |
| sousaedu | 996a560 | 2021-05-03 00:22:43 +0200 | [diff] [blame] | 225 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 226 | container_builder.add_port(name=self.app.name, port=PORT) |
| 227 | container_builder.add_http_readiness_probe( |
| 228 | "/ro/" if config.enable_ng_ro else "/openmano/tenants", |
| 229 | PORT, |
| 230 | initial_delay_seconds=10, |
| 231 | period_seconds=10, |
| 232 | timeout_seconds=5, |
| 233 | failure_threshold=3, |
| 234 | ) |
| 235 | container_builder.add_http_liveness_probe( |
| 236 | "/ro/" if config.enable_ng_ro else "/openmano/tenants", |
| 237 | PORT, |
| 238 | initial_delay_seconds=600, |
| 239 | period_seconds=10, |
| 240 | timeout_seconds=5, |
| 241 | failure_threshold=3, |
| 242 | ) |
| 243 | container_builder.add_envs( |
| 244 | { |
| 245 | "OSMRO_LOG_LEVEL": config.log_level, |
| 246 | } |
| 247 | ) |
| sousaedu | 996a560 | 2021-05-03 00:22:43 +0200 | [diff] [blame] | 248 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 249 | if config.enable_ng_ro: |
| 250 | container_builder.add_envs( |
| 251 | { |
| 252 | "OSMRO_MESSAGE_DRIVER": "kafka", |
| 253 | "OSMRO_MESSAGE_HOST": self.kafka_client.host, |
| 254 | "OSMRO_MESSAGE_PORT": self.kafka_client.port, |
| 255 | # MongoDB configuration |
| 256 | "OSMRO_DATABASE_DRIVER": "mongo", |
| sousaedu | 996a560 | 2021-05-03 00:22:43 +0200 | [diff] [blame] | 257 | "OSMRO_DATABASE_URI": config.mongodb_uri |
| 258 | or self.mongodb_client.connection_string, |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 259 | "OSMRO_DATABASE_COMMONKEY": config.database_commonkey, |
| 260 | } |
| sousaedu | ccfacbb | 2020-11-04 21:44:01 +0000 | [diff] [blame] | 261 | ) |
| sousaedu | ccfacbb | 2020-11-04 21:44:01 +0000 | [diff] [blame] | 262 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 263 | else: |
| 264 | container_builder.add_envs( |
| 265 | { |
| sousaedu | 996a560 | 2021-05-03 00:22:43 +0200 | [diff] [blame] | 266 | "RO_DB_HOST": config.mysql_host or self.mysql_client.host, |
| 267 | "RO_DB_OVIM_HOST": config.mysql_host or self.mysql_client.host, |
| 268 | "RO_DB_PORT": config.mysql_port or self.mysql_client.port, |
| 269 | "RO_DB_OVIM_PORT": config.mysql_port or self.mysql_client.port, |
| 270 | "RO_DB_USER": config.mysql_user or self.mysql_client.user, |
| 271 | "RO_DB_OVIM_USER": config.mysql_user or self.mysql_client.user, |
| 272 | "RO_DB_PASSWORD": config.mysql_password |
| 273 | or self.mysql_client.password, |
| 274 | "RO_DB_OVIM_PASSWORD": config.mysql_password |
| 275 | or self.mysql_client.password, |
| 276 | "RO_DB_ROOT_PASSWORD": config.mysql_root_password |
| 277 | or self.mysql_client.root_password, |
| 278 | "RO_DB_OVIM_ROOT_PASSWORD": config.mysql_root_password |
| 279 | or self.mysql_client.root_password, |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 280 | "RO_DB_NAME": config.ro_database, |
| 281 | "RO_DB_OVIM_NAME": config.vim_database, |
| 282 | "OPENMANO_TENANT": config.openmano_tenant, |
| 283 | } |
| 284 | ) |
| 285 | container = container_builder.build() |
| sousaedu | 996a560 | 2021-05-03 00:22:43 +0200 | [diff] [blame] | 286 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 287 | # Add container to pod spec |
| 288 | pod_spec_builder.add_container(container) |
| sousaedu | 996a560 | 2021-05-03 00:22:43 +0200 | [diff] [blame] | 289 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 290 | return pod_spec_builder.build() |
| sousaedu | ccfacbb | 2020-11-04 21:44:01 +0000 | [diff] [blame] | 291 | |
| 292 | |
| David Garcia | d680be4 | 2021-08-17 11:03:55 +0200 | [diff] [blame] | 293 | VSCODE_WORKSPACE = { |
| 294 | "folders": [ |
| 295 | {"path": "/usr/lib/python3/dist-packages/osm_ng_ro"}, |
| 296 | {"path": "/usr/lib/python3/dist-packages/osm_common"}, |
| 297 | {"path": "/usr/lib/python3/dist-packages/osm_ro_plugin"}, |
| 298 | {"path": "/usr/lib/python3/dist-packages/osm_rosdn_arista_cloudvision"}, |
| 299 | {"path": "/usr/lib/python3/dist-packages/osm_rosdn_dpb"}, |
| 300 | {"path": "/usr/lib/python3/dist-packages/osm_rosdn_dynpac"}, |
| 301 | {"path": "/usr/lib/python3/dist-packages/osm_rosdn_floodlightof"}, |
| 302 | {"path": "/usr/lib/python3/dist-packages/osm_rosdn_ietfl2vpn"}, |
| 303 | {"path": "/usr/lib/python3/dist-packages/osm_rosdn_juniper_contrail"}, |
| 304 | {"path": "/usr/lib/python3/dist-packages/osm_rosdn_odlof"}, |
| 305 | {"path": "/usr/lib/python3/dist-packages/osm_rosdn_onos_vpls"}, |
| 306 | {"path": "/usr/lib/python3/dist-packages/osm_rosdn_onosof"}, |
| 307 | {"path": "/usr/lib/python3/dist-packages/osm_rovim_aws"}, |
| 308 | {"path": "/usr/lib/python3/dist-packages/osm_rovim_azure"}, |
| 309 | {"path": "/usr/lib/python3/dist-packages/osm_rovim_fos"}, |
| 310 | {"path": "/usr/lib/python3/dist-packages/osm_rovim_opennebula"}, |
| 311 | {"path": "/usr/lib/python3/dist-packages/osm_rovim_openstack"}, |
| 312 | {"path": "/usr/lib/python3/dist-packages/osm_rovim_openvim"}, |
| 313 | {"path": "/usr/lib/python3/dist-packages/osm_rovim_vmware"}, |
| 314 | ], |
| 315 | "launch": { |
| 316 | "configurations": [ |
| 317 | { |
| 318 | "module": "osm_ng_ro.ro_main", |
| 319 | "name": "NG RO", |
| 320 | "request": "launch", |
| 321 | "type": "python", |
| 322 | "justMyCode": False, |
| 323 | } |
| 324 | ], |
| 325 | "version": "0.2.0", |
| 326 | }, |
| 327 | "settings": {}, |
| 328 | } |
| 329 | |
| sousaedu | ccfacbb | 2020-11-04 21:44:01 +0000 | [diff] [blame] | 330 | if __name__ == "__main__": |
| 331 | main(RoCharm) |