| sousaedu | 1dd4c0d | 2020-11-04 17:43:47 +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 | 1dd4c0d | 2020-11-04 17:43:47 +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 | |
| 25 | |
| sousaedu | 1dd4c0d | 2020-11-04 17:43:47 +0000 | [diff] [blame] | 26 | import logging |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 27 | from typing import NoReturn |
| sousaedu | 1dd4c0d | 2020-11-04 17:43:47 +0000 | [diff] [blame] | 28 | |
| David Garcia | c753dc5 | 2021-03-17 15:28:47 +0100 | [diff] [blame] | 29 | |
| sousaedu | 1dd4c0d | 2020-11-04 17:43:47 +0000 | [diff] [blame] | 30 | from ops.main import main |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 31 | from opslib.osm.charm import CharmedOsmBase, RelationsMissing |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 32 | from opslib.osm.interfaces.kafka import KafkaClient |
| David Garcia | c753dc5 | 2021-03-17 15:28:47 +0100 | [diff] [blame] | 33 | from opslib.osm.interfaces.keystone import KeystoneClient |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 34 | from opslib.osm.interfaces.mongo import MongoClient |
| 35 | from opslib.osm.interfaces.prometheus import PrometheusClient |
| David Garcia | c753dc5 | 2021-03-17 15:28:47 +0100 | [diff] [blame] | 36 | from opslib.osm.pod import ContainerV3Builder, PodSpecV3Builder |
| 37 | from opslib.osm.validator import ModelValidator, validator |
| sousaedu | 1dd4c0d | 2020-11-04 17:43:47 +0000 | [diff] [blame] | 38 | |
| 39 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 40 | logger = logging.getLogger(__name__) |
| sousaedu | 1dd4c0d | 2020-11-04 17:43:47 +0000 | [diff] [blame] | 41 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 42 | PORT = 8000 |
| sousaedu | 1dd4c0d | 2020-11-04 17:43:47 +0000 | [diff] [blame] | 43 | |
| 44 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 45 | class ConfigModel(ModelValidator): |
| calvinosanc1 | a43a22f | 2021-03-08 15:20:07 +0100 | [diff] [blame] | 46 | keystone_enabled: bool |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 47 | vca_host: str |
| 48 | vca_user: str |
| David Garcia | c753dc5 | 2021-03-17 15:28:47 +0100 | [diff] [blame] | 49 | vca_secret: str |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 50 | vca_cacert: str |
| 51 | database_commonkey: str |
| 52 | log_level: str |
| 53 | openstack_default_granularity: int |
| 54 | global_request_timeout: int |
| 55 | collector_interval: int |
| 56 | evaluator_interval: int |
| 57 | grafana_url: str |
| 58 | grafana_user: str |
| 59 | grafana_password: str |
| sousaedu | 1dd4c0d | 2020-11-04 17:43:47 +0000 | [diff] [blame] | 60 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 61 | @validator("log_level") |
| 62 | def validate_log_level(cls, v): |
| 63 | if v not in {"INFO", "DEBUG"}: |
| 64 | raise ValueError("value must be INFO or DEBUG") |
| 65 | return v |
| sousaedu | 1dd4c0d | 2020-11-04 17:43:47 +0000 | [diff] [blame] | 66 | |
| 67 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 68 | class MonCharm(CharmedOsmBase): |
| sousaedu | 1dd4c0d | 2020-11-04 17:43:47 +0000 | [diff] [blame] | 69 | def __init__(self, *args) -> NoReturn: |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 70 | super().__init__(*args, oci_image="image") |
| sousaedu | 1dd4c0d | 2020-11-04 17:43:47 +0000 | [diff] [blame] | 71 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 72 | self.kafka_client = KafkaClient(self, "kafka") |
| 73 | self.framework.observe(self.on["kafka"].relation_changed, self.configure_pod) |
| 74 | self.framework.observe(self.on["kafka"].relation_broken, self.configure_pod) |
| sousaedu | 1dd4c0d | 2020-11-04 17:43:47 +0000 | [diff] [blame] | 75 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 76 | self.mongodb_client = MongoClient(self, "mongodb") |
| 77 | self.framework.observe(self.on["mongodb"].relation_changed, self.configure_pod) |
| 78 | self.framework.observe(self.on["mongodb"].relation_broken, self.configure_pod) |
| sousaedu | 1dd4c0d | 2020-11-04 17:43:47 +0000 | [diff] [blame] | 79 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 80 | self.prometheus_client = PrometheusClient(self, "prometheus") |
| sousaedu | 1dd4c0d | 2020-11-04 17:43:47 +0000 | [diff] [blame] | 81 | self.framework.observe( |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 82 | self.on["prometheus"].relation_changed, self.configure_pod |
| sousaedu | 1dd4c0d | 2020-11-04 17:43:47 +0000 | [diff] [blame] | 83 | ) |
| 84 | self.framework.observe( |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 85 | self.on["prometheus"].relation_broken, self.configure_pod |
| sousaedu | 1dd4c0d | 2020-11-04 17:43:47 +0000 | [diff] [blame] | 86 | ) |
| 87 | |
| calvinosanc1 | a43a22f | 2021-03-08 15:20:07 +0100 | [diff] [blame] | 88 | self.keystone_client = KeystoneClient(self, "keystone") |
| 89 | self.framework.observe(self.on["keystone"].relation_changed, self.configure_pod) |
| 90 | self.framework.observe(self.on["keystone"].relation_broken, self.configure_pod) |
| 91 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 92 | def _check_missing_dependencies(self, config: ConfigModel): |
| 93 | missing_relations = [] |
| 94 | |
| 95 | if self.kafka_client.is_missing_data_in_unit(): |
| 96 | missing_relations.append("kafka") |
| 97 | if self.mongodb_client.is_missing_data_in_unit(): |
| 98 | missing_relations.append("mongodb") |
| 99 | if self.prometheus_client.is_missing_data_in_app(): |
| 100 | missing_relations.append("prometheus") |
| calvinosanc1 | a43a22f | 2021-03-08 15:20:07 +0100 | [diff] [blame] | 101 | if config.keystone_enabled: |
| 102 | if self.keystone_client.is_missing_data_in_app(): |
| 103 | missing_relations.append("keystone") |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 104 | |
| 105 | if missing_relations: |
| 106 | raise RelationsMissing(missing_relations) |
| 107 | |
| 108 | def build_pod_spec(self, image_info): |
| 109 | # Validate config |
| 110 | config = ConfigModel(**dict(self.config)) |
| 111 | # Check relations |
| 112 | self._check_missing_dependencies(config) |
| 113 | # Create Builder for the PodSpec |
| 114 | pod_spec_builder = PodSpecV3Builder() |
| 115 | # Build Container |
| 116 | container_builder = ContainerV3Builder(self.app.name, image_info) |
| 117 | container_builder.add_port(name=self.app.name, port=PORT) |
| 118 | container_builder.add_envs( |
| 119 | { |
| 120 | # General configuration |
| 121 | "ALLOW_ANONYMOUS_LOGIN": "yes", |
| 122 | "OSMMON_OPENSTACK_DEFAULT_GRANULARITY": config.openstack_default_granularity, |
| 123 | "OSMMON_GLOBAL_REQUEST_TIMEOUT": config.global_request_timeout, |
| 124 | "OSMMON_GLOBAL_LOGLEVEL": config.log_level, |
| 125 | "OSMMON_COLLECTOR_INTERVAL": config.collector_interval, |
| 126 | "OSMMON_EVALUATOR_INTERVAL": config.evaluator_interval, |
| 127 | # Kafka configuration |
| 128 | "OSMMON_MESSAGE_DRIVER": "kafka", |
| 129 | "OSMMON_MESSAGE_HOST": self.kafka_client.host, |
| 130 | "OSMMON_MESSAGE_PORT": self.kafka_client.port, |
| 131 | # Database configuration |
| 132 | "OSMMON_DATABASE_DRIVER": "mongo", |
| 133 | "OSMMON_DATABASE_URI": self.mongodb_client.connection_string, |
| 134 | "OSMMON_DATABASE_COMMONKEY": config.database_commonkey, |
| 135 | # Prometheus configuration |
| 136 | "OSMMON_PROMETHEUS_URL": f"http://{self.prometheus_client.hostname}:{self.prometheus_client.port}", |
| 137 | # VCA configuration |
| 138 | "OSMMON_VCA_HOST": config.vca_host, |
| 139 | "OSMMON_VCA_USER": config.vca_user, |
| David Garcia | c753dc5 | 2021-03-17 15:28:47 +0100 | [diff] [blame] | 140 | "OSMMON_VCA_SECRET": config.vca_secret, |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 141 | "OSMMON_VCA_CACERT": config.vca_cacert, |
| 142 | "OSMMON_GRAFANA_URL": config.grafana_url, |
| 143 | "OSMMON_GRAFANA_USER": config.grafana_user, |
| 144 | "OSMMON_GRAFANA_PASSWORD": config.grafana_password, |
| 145 | } |
| sousaedu | 1dd4c0d | 2020-11-04 17:43:47 +0000 | [diff] [blame] | 146 | ) |
| calvinosanc1 | a43a22f | 2021-03-08 15:20:07 +0100 | [diff] [blame] | 147 | if config.keystone_enabled: |
| 148 | container_builder.add_envs( |
| 149 | { |
| 150 | "OSMMON_KEYSTONE_ENABLED": True, |
| 151 | "OSMMON_KEYSTONE_URL": self.keystone_client.host, |
| 152 | "OSMMON_KEYSTONE_DOMAIN_NAME": self.keystone_client.user_domain_name, |
| 153 | "OSMMON_KEYSTONE_PROJECT_DOMAIN_NAME": self.keystone_client.project_domain_name, |
| 154 | "OSMMON_KEYSTONE_SERVICE_USER": self.keystone_client.username, |
| 155 | "OSMMON_KEYSTONE_SERVICE_PASSWORD": self.keystone_client.password, |
| 156 | "OSMMON_KEYSTONE_SERVICE_PROJECT": self.keystone_client.service, |
| 157 | } |
| 158 | ) |
| sousaedu | 1dd4c0d | 2020-11-04 17:43:47 +0000 | [diff] [blame] | 159 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 160 | container = container_builder.build() |
| 161 | # Add container to pod spec |
| 162 | pod_spec_builder.add_container(container) |
| 163 | return pod_spec_builder.build() |
| sousaedu | 1dd4c0d | 2020-11-04 17:43:47 +0000 | [diff] [blame] | 164 | |
| 165 | |
| 166 | if __name__ == "__main__": |
| 167 | main(MonCharm) |