blob: 98c3297be76dc239af55b6928c7860424a671b7d [file] [log] [blame]
sousaedu1dd4c0d2020-11-04 17:43:47 +00001#!/usr/bin/env python3
David Garcia49379ce2021-02-24 13:48:22 +01002# Copyright 2021 Canonical Ltd.
sousaedu1dd4c0d2020-11-04 17:43:47 +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
25
sousaedu1dd4c0d2020-11-04 17:43:47 +000026import logging
David Garcia49379ce2021-02-24 13:48:22 +010027from typing import NoReturn
sousaedu1dd4c0d2020-11-04 17:43:47 +000028
sousaedu1dd4c0d2020-11-04 17:43:47 +000029from ops.main import main
sousaedu1dd4c0d2020-11-04 17:43:47 +000030
David Garcia49379ce2021-02-24 13:48:22 +010031from opslib.osm.charm import CharmedOsmBase, RelationsMissing
sousaedu1dd4c0d2020-11-04 17:43:47 +000032
David Garcia49379ce2021-02-24 13:48:22 +010033from opslib.osm.pod import (
34 ContainerV3Builder,
35 PodSpecV3Builder,
36)
sousaedu1dd4c0d2020-11-04 17:43:47 +000037
David Garcia49379ce2021-02-24 13:48:22 +010038from opslib.osm.validator import (
39 ModelValidator,
40 validator,
41)
42
43from opslib.osm.interfaces.kafka import KafkaClient
44from opslib.osm.interfaces.mongo import MongoClient
45from opslib.osm.interfaces.prometheus import PrometheusClient
sousaedu1dd4c0d2020-11-04 17:43:47 +000046
47
David Garcia49379ce2021-02-24 13:48:22 +010048logger = logging.getLogger(__name__)
sousaedu1dd4c0d2020-11-04 17:43:47 +000049
David Garcia49379ce2021-02-24 13:48:22 +010050PORT = 8000
sousaedu1dd4c0d2020-11-04 17:43:47 +000051
52
David Garcia49379ce2021-02-24 13:48:22 +010053class ConfigModel(ModelValidator):
54 vca_host: str
55 vca_user: str
56 vca_password: str
57 vca_cacert: str
58 database_commonkey: str
59 log_level: str
60 openstack_default_granularity: int
61 global_request_timeout: int
62 collector_interval: int
63 evaluator_interval: int
64 grafana_url: str
65 grafana_user: str
66 grafana_password: str
sousaedu1dd4c0d2020-11-04 17:43:47 +000067
David Garcia49379ce2021-02-24 13:48:22 +010068 @validator("log_level")
69 def validate_log_level(cls, v):
70 if v not in {"INFO", "DEBUG"}:
71 raise ValueError("value must be INFO or DEBUG")
72 return v
sousaedu1dd4c0d2020-11-04 17:43:47 +000073
74
David Garcia49379ce2021-02-24 13:48:22 +010075class MonCharm(CharmedOsmBase):
sousaedu1dd4c0d2020-11-04 17:43:47 +000076 def __init__(self, *args) -> NoReturn:
David Garcia49379ce2021-02-24 13:48:22 +010077 super().__init__(*args, oci_image="image")
sousaedu1dd4c0d2020-11-04 17:43:47 +000078
David Garcia49379ce2021-02-24 13:48:22 +010079 self.kafka_client = KafkaClient(self, "kafka")
80 self.framework.observe(self.on["kafka"].relation_changed, self.configure_pod)
81 self.framework.observe(self.on["kafka"].relation_broken, self.configure_pod)
sousaedu1dd4c0d2020-11-04 17:43:47 +000082
David Garcia49379ce2021-02-24 13:48:22 +010083 self.mongodb_client = MongoClient(self, "mongodb")
84 self.framework.observe(self.on["mongodb"].relation_changed, self.configure_pod)
85 self.framework.observe(self.on["mongodb"].relation_broken, self.configure_pod)
sousaedu1dd4c0d2020-11-04 17:43:47 +000086
David Garcia49379ce2021-02-24 13:48:22 +010087 self.prometheus_client = PrometheusClient(self, "prometheus")
sousaedu1dd4c0d2020-11-04 17:43:47 +000088 self.framework.observe(
David Garcia49379ce2021-02-24 13:48:22 +010089 self.on["prometheus"].relation_changed, self.configure_pod
sousaedu1dd4c0d2020-11-04 17:43:47 +000090 )
91 self.framework.observe(
David Garcia49379ce2021-02-24 13:48:22 +010092 self.on["prometheus"].relation_broken, self.configure_pod
sousaedu1dd4c0d2020-11-04 17:43:47 +000093 )
94
David Garcia49379ce2021-02-24 13:48:22 +010095 def _check_missing_dependencies(self, config: ConfigModel):
96 missing_relations = []
97
98 if self.kafka_client.is_missing_data_in_unit():
99 missing_relations.append("kafka")
100 if self.mongodb_client.is_missing_data_in_unit():
101 missing_relations.append("mongodb")
102 if self.prometheus_client.is_missing_data_in_app():
103 missing_relations.append("prometheus")
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,
140 "OSMMON_VCA_SECRET": config.vca_password,
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 }
sousaedu1dd4c0d2020-11-04 17:43:47 +0000146 )
147
David Garcia49379ce2021-02-24 13:48:22 +0100148 container = container_builder.build()
149 # Add container to pod spec
150 pod_spec_builder.add_container(container)
151 return pod_spec_builder.build()
sousaedu1dd4c0d2020-11-04 17:43:47 +0000152
153
154if __name__ == "__main__":
155 main(MonCharm)