| 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] |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 76 | |
| 77 | @validator("log_level") |
| 78 | def validate_log_level(cls, v): |
| 79 | if v not in {"INFO", "DEBUG"}: |
| 80 | raise ValueError("value must be INFO or DEBUG") |
| 81 | return v |
| sousaedu | ccfacbb | 2020-11-04 21:44:01 +0000 | [diff] [blame] | 82 | |
| David Garcia | 5d1ec6e | 2021-03-25 15:04:52 +0100 | [diff] [blame] | 83 | @validator("certificates") |
| 84 | def validate_certificates(cls, v): |
| 85 | # Raises an exception if it cannot extract the certificates |
| 86 | _extract_certificates(v) |
| 87 | return v |
| 88 | |
| sousaedu | 996a560 | 2021-05-03 00:22:43 +0200 | [diff] [blame] | 89 | @validator("mongodb_uri") |
| 90 | def validate_mongodb_uri(cls, v): |
| 91 | if v and not v.startswith("mongodb://"): |
| 92 | raise ValueError("mongodb_uri is not properly formed") |
| 93 | return v |
| 94 | |
| 95 | @validator("mysql_port") |
| 96 | def validate_mysql_port(cls, v): |
| 97 | if v and (v <= 0 or v >= 65535): |
| 98 | raise ValueError("Mysql port out of range") |
| 99 | return v |
| 100 | |
| David Garcia | 5d1ec6e | 2021-03-25 15:04:52 +0100 | [diff] [blame] | 101 | @property |
| 102 | def certificates_dict(cls): |
| 103 | return _extract_certificates(cls.certificates) if cls.certificates else {} |
| 104 | |
| sousaedu | ccfacbb | 2020-11-04 21:44:01 +0000 | [diff] [blame] | 105 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 106 | class RoCharm(CharmedOsmBase): |
| 107 | """GrafanaCharm Charm.""" |
| sousaedu | ccfacbb | 2020-11-04 21:44:01 +0000 | [diff] [blame] | 108 | |
| 109 | def __init__(self, *args) -> NoReturn: |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 110 | """Prometheus Charm constructor.""" |
| 111 | super().__init__(*args, oci_image="image") |
| sousaedu | ccfacbb | 2020-11-04 21:44:01 +0000 | [diff] [blame] | 112 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 113 | self.kafka_client = KafkaClient(self, "kafka") |
| 114 | self.framework.observe(self.on["kafka"].relation_changed, self.configure_pod) |
| 115 | self.framework.observe(self.on["kafka"].relation_broken, self.configure_pod) |
| sousaedu | ccfacbb | 2020-11-04 21:44:01 +0000 | [diff] [blame] | 116 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 117 | self.mysql_client = MysqlClient(self, "mysql") |
| 118 | self.framework.observe(self.on["mysql"].relation_changed, self.configure_pod) |
| 119 | self.framework.observe(self.on["mysql"].relation_broken, self.configure_pod) |
| sousaedu | ccfacbb | 2020-11-04 21:44:01 +0000 | [diff] [blame] | 120 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 121 | self.mongodb_client = MongoClient(self, "mongodb") |
| 122 | self.framework.observe(self.on["mongodb"].relation_changed, self.configure_pod) |
| 123 | self.framework.observe(self.on["mongodb"].relation_broken, self.configure_pod) |
| sousaedu | ccfacbb | 2020-11-04 21:44:01 +0000 | [diff] [blame] | 124 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 125 | self.framework.observe(self.on["ro"].relation_joined, self._publish_ro_info) |
| sousaedu | ccfacbb | 2020-11-04 21:44:01 +0000 | [diff] [blame] | 126 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 127 | def _publish_ro_info(self, event): |
| sousaedu | ccfacbb | 2020-11-04 21:44:01 +0000 | [diff] [blame] | 128 | """Publishes RO information. |
| 129 | |
| 130 | Args: |
| 131 | event (EventBase): RO relation event. |
| 132 | """ |
| 133 | if self.unit.is_leader(): |
| 134 | rel_data = { |
| 135 | "host": self.model.app.name, |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 136 | "port": str(PORT), |
| sousaedu | ccfacbb | 2020-11-04 21:44:01 +0000 | [diff] [blame] | 137 | } |
| 138 | for k, v in rel_data.items(): |
| 139 | event.relation.data[self.app][k] = v |
| 140 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 141 | def _check_missing_dependencies(self, config: ConfigModel): |
| 142 | missing_relations = [] |
| 143 | |
| 144 | if config.enable_ng_ro: |
| 145 | if self.kafka_client.is_missing_data_in_unit(): |
| 146 | missing_relations.append("kafka") |
| sousaedu | 996a560 | 2021-05-03 00:22:43 +0200 | [diff] [blame] | 147 | 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] | 148 | missing_relations.append("mongodb") |
| sousaedu | ccfacbb | 2020-11-04 21:44:01 +0000 | [diff] [blame] | 149 | else: |
| sousaedu | 996a560 | 2021-05-03 00:22:43 +0200 | [diff] [blame] | 150 | 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] | 151 | missing_relations.append("mysql") |
| 152 | if missing_relations: |
| 153 | raise RelationsMissing(missing_relations) |
| sousaedu | ccfacbb | 2020-11-04 21:44:01 +0000 | [diff] [blame] | 154 | |
| sousaedu | 996a560 | 2021-05-03 00:22:43 +0200 | [diff] [blame] | 155 | def _validate_mysql_config(self, config: ConfigModel): |
| 156 | invalid_values = [] |
| 157 | if not config.mysql_user: |
| 158 | invalid_values.append("Mysql user is empty") |
| 159 | if not config.mysql_password: |
| 160 | invalid_values.append("Mysql password is empty") |
| 161 | if not config.mysql_root_password: |
| 162 | invalid_values.append("Mysql root password empty") |
| 163 | |
| 164 | if invalid_values: |
| 165 | raise ValueError("Invalid values: " + ", ".join(invalid_values)) |
| 166 | |
| David Garcia | 5d1ec6e | 2021-03-25 15:04:52 +0100 | [diff] [blame] | 167 | def _build_cert_files( |
| 168 | self, |
| 169 | config: ConfigModel, |
| 170 | ): |
| 171 | cert_files_builder = FilesV3Builder() |
| 172 | for name, content in config.certificates_dict.items(): |
| 173 | cert_files_builder.add_file(name, decode(content), mode=0o600) |
| 174 | return cert_files_builder.build() |
| 175 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 176 | def build_pod_spec(self, image_info): |
| 177 | # Validate config |
| 178 | config = ConfigModel(**dict(self.config)) |
| sousaedu | 996a560 | 2021-05-03 00:22:43 +0200 | [diff] [blame] | 179 | |
| 180 | if config.enable_ng_ro: |
| 181 | if config.mongodb_uri and not self.mongodb_client.is_missing_data_in_unit(): |
| 182 | raise Exception( |
| 183 | "Mongodb data cannot be provided via config and relation" |
| 184 | ) |
| 185 | else: |
| 186 | if config.mysql_host and not self.mysql_client.is_missing_data_in_unit(): |
| 187 | raise Exception("Mysql data cannot be provided via config and relation") |
| 188 | |
| 189 | if config.mysql_host: |
| 190 | self._validate_mysql_config(config) |
| 191 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 192 | # Check relations |
| 193 | self._check_missing_dependencies(config) |
| sousaedu | 996a560 | 2021-05-03 00:22:43 +0200 | [diff] [blame] | 194 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 195 | # Create Builder for the PodSpec |
| 196 | pod_spec_builder = PodSpecV3Builder() |
| sousaedu | 996a560 | 2021-05-03 00:22:43 +0200 | [diff] [blame] | 197 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 198 | # Build Container |
| 199 | container_builder = ContainerV3Builder(self.app.name, image_info) |
| David Garcia | 5d1ec6e | 2021-03-25 15:04:52 +0100 | [diff] [blame] | 200 | certs_files = self._build_cert_files(config) |
| sousaedu | 996a560 | 2021-05-03 00:22:43 +0200 | [diff] [blame] | 201 | |
| David Garcia | 5d1ec6e | 2021-03-25 15:04:52 +0100 | [diff] [blame] | 202 | if certs_files: |
| 203 | container_builder.add_volume_config("certs", "/certs", certs_files) |
| sousaedu | 996a560 | 2021-05-03 00:22:43 +0200 | [diff] [blame] | 204 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 205 | container_builder.add_port(name=self.app.name, port=PORT) |
| 206 | container_builder.add_http_readiness_probe( |
| 207 | "/ro/" if config.enable_ng_ro else "/openmano/tenants", |
| 208 | PORT, |
| 209 | initial_delay_seconds=10, |
| 210 | period_seconds=10, |
| 211 | timeout_seconds=5, |
| 212 | failure_threshold=3, |
| 213 | ) |
| 214 | container_builder.add_http_liveness_probe( |
| 215 | "/ro/" if config.enable_ng_ro else "/openmano/tenants", |
| 216 | PORT, |
| 217 | initial_delay_seconds=600, |
| 218 | period_seconds=10, |
| 219 | timeout_seconds=5, |
| 220 | failure_threshold=3, |
| 221 | ) |
| 222 | container_builder.add_envs( |
| 223 | { |
| 224 | "OSMRO_LOG_LEVEL": config.log_level, |
| 225 | } |
| 226 | ) |
| sousaedu | 996a560 | 2021-05-03 00:22:43 +0200 | [diff] [blame] | 227 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 228 | if config.enable_ng_ro: |
| 229 | container_builder.add_envs( |
| 230 | { |
| 231 | "OSMRO_MESSAGE_DRIVER": "kafka", |
| 232 | "OSMRO_MESSAGE_HOST": self.kafka_client.host, |
| 233 | "OSMRO_MESSAGE_PORT": self.kafka_client.port, |
| 234 | # MongoDB configuration |
| 235 | "OSMRO_DATABASE_DRIVER": "mongo", |
| sousaedu | 996a560 | 2021-05-03 00:22:43 +0200 | [diff] [blame] | 236 | "OSMRO_DATABASE_URI": config.mongodb_uri |
| 237 | or self.mongodb_client.connection_string, |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 238 | "OSMRO_DATABASE_COMMONKEY": config.database_commonkey, |
| 239 | } |
| sousaedu | ccfacbb | 2020-11-04 21:44:01 +0000 | [diff] [blame] | 240 | ) |
| sousaedu | ccfacbb | 2020-11-04 21:44:01 +0000 | [diff] [blame] | 241 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 242 | else: |
| 243 | container_builder.add_envs( |
| 244 | { |
| sousaedu | 996a560 | 2021-05-03 00:22:43 +0200 | [diff] [blame] | 245 | "RO_DB_HOST": config.mysql_host or self.mysql_client.host, |
| 246 | "RO_DB_OVIM_HOST": config.mysql_host or self.mysql_client.host, |
| 247 | "RO_DB_PORT": config.mysql_port or self.mysql_client.port, |
| 248 | "RO_DB_OVIM_PORT": config.mysql_port or self.mysql_client.port, |
| 249 | "RO_DB_USER": config.mysql_user or self.mysql_client.user, |
| 250 | "RO_DB_OVIM_USER": config.mysql_user or self.mysql_client.user, |
| 251 | "RO_DB_PASSWORD": config.mysql_password |
| 252 | or self.mysql_client.password, |
| 253 | "RO_DB_OVIM_PASSWORD": config.mysql_password |
| 254 | or self.mysql_client.password, |
| 255 | "RO_DB_ROOT_PASSWORD": config.mysql_root_password |
| 256 | or self.mysql_client.root_password, |
| 257 | "RO_DB_OVIM_ROOT_PASSWORD": config.mysql_root_password |
| 258 | or self.mysql_client.root_password, |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 259 | "RO_DB_NAME": config.ro_database, |
| 260 | "RO_DB_OVIM_NAME": config.vim_database, |
| 261 | "OPENMANO_TENANT": config.openmano_tenant, |
| 262 | } |
| 263 | ) |
| 264 | container = container_builder.build() |
| sousaedu | 996a560 | 2021-05-03 00:22:43 +0200 | [diff] [blame] | 265 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 266 | # Add container to pod spec |
| 267 | pod_spec_builder.add_container(container) |
| sousaedu | 996a560 | 2021-05-03 00:22:43 +0200 | [diff] [blame] | 268 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 269 | return pod_spec_builder.build() |
| sousaedu | ccfacbb | 2020-11-04 21:44:01 +0000 | [diff] [blame] | 270 | |
| 271 | |
| 272 | if __name__ == "__main__": |
| 273 | main(RoCharm) |