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