blob: ae92d9835b09f92969a74029cd6721e23bb54689 [file] [log] [blame]
sousaeduccfacbb2020-11-04 21:44:01 +00001#!/usr/bin/env python3
David Garcia49379ce2021-02-24 13:48:22 +01002# Copyright 2021 Canonical Ltd.
sousaeduccfacbb2020-11-04 21:44:01 +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
sousaeduccfacbb2020-11-04 21:44:01 +000025import logging
David Garcia49379ce2021-02-24 13:48:22 +010026from typing import NoReturn
sousaeduccfacbb2020-11-04 21:44:01 +000027
sousaeduccfacbb2020-11-04 21:44:01 +000028from ops.main import main
sousaeduccfacbb2020-11-04 21:44:01 +000029
David Garcia49379ce2021-02-24 13:48:22 +010030from opslib.osm.charm import CharmedOsmBase, RelationsMissing
31
32from opslib.osm.pod import (
33 ContainerV3Builder,
34 PodSpecV3Builder,
35)
36
37
38from opslib.osm.validator import (
39 ModelValidator,
40 validator,
41)
42
43from opslib.osm.interfaces.kafka import KafkaClient
44from opslib.osm.interfaces.mysql import MysqlClient
45from opslib.osm.interfaces.mongo import MongoClient
46
sousaeduccfacbb2020-11-04 21:44:01 +000047
48logger = logging.getLogger(__name__)
49
David Garcia49379ce2021-02-24 13:48:22 +010050PORT = 9090
sousaeduccfacbb2020-11-04 21:44:01 +000051
52
David Garcia49379ce2021-02-24 13:48:22 +010053class ConfigModel(ModelValidator):
54 enable_ng_ro: bool
55 database_commonkey: str
56 log_level: str
57 vim_database: str
58 ro_database: str
59 openmano_tenant: str
60
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
sousaeduccfacbb2020-11-04 21:44:01 +000066
67
David Garcia49379ce2021-02-24 13:48:22 +010068class RoCharm(CharmedOsmBase):
69 """GrafanaCharm Charm."""
sousaeduccfacbb2020-11-04 21:44:01 +000070
71 def __init__(self, *args) -> NoReturn:
David Garcia49379ce2021-02-24 13:48:22 +010072 """Prometheus Charm constructor."""
73 super().__init__(*args, oci_image="image")
sousaeduccfacbb2020-11-04 21:44:01 +000074
David Garcia49379ce2021-02-24 13:48:22 +010075 self.kafka_client = KafkaClient(self, "kafka")
76 self.framework.observe(self.on["kafka"].relation_changed, self.configure_pod)
77 self.framework.observe(self.on["kafka"].relation_broken, self.configure_pod)
sousaeduccfacbb2020-11-04 21:44:01 +000078
David Garcia49379ce2021-02-24 13:48:22 +010079 self.mysql_client = MysqlClient(self, "mysql")
80 self.framework.observe(self.on["mysql"].relation_changed, self.configure_pod)
81 self.framework.observe(self.on["mysql"].relation_broken, self.configure_pod)
sousaeduccfacbb2020-11-04 21:44:01 +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)
sousaeduccfacbb2020-11-04 21:44:01 +000086
David Garcia49379ce2021-02-24 13:48:22 +010087 self.framework.observe(self.on["ro"].relation_joined, self._publish_ro_info)
sousaeduccfacbb2020-11-04 21:44:01 +000088
David Garcia49379ce2021-02-24 13:48:22 +010089 def _publish_ro_info(self, event):
sousaeduccfacbb2020-11-04 21:44:01 +000090 """Publishes RO information.
91
92 Args:
93 event (EventBase): RO relation event.
94 """
95 if self.unit.is_leader():
96 rel_data = {
97 "host": self.model.app.name,
David Garcia49379ce2021-02-24 13:48:22 +010098 "port": str(PORT),
sousaeduccfacbb2020-11-04 21:44:01 +000099 }
100 for k, v in rel_data.items():
101 event.relation.data[self.app][k] = v
102
David Garcia49379ce2021-02-24 13:48:22 +0100103 def _check_missing_dependencies(self, config: ConfigModel):
104 missing_relations = []
105
106 if config.enable_ng_ro:
107 if self.kafka_client.is_missing_data_in_unit():
108 missing_relations.append("kafka")
109 if self.mongodb_client.is_missing_data_in_unit():
110 missing_relations.append("mongodb")
sousaeduccfacbb2020-11-04 21:44:01 +0000111 else:
David Garcia49379ce2021-02-24 13:48:22 +0100112 if self.mysql_client.is_missing_data_in_unit():
113 missing_relations.append("mysql")
114 if missing_relations:
115 raise RelationsMissing(missing_relations)
sousaeduccfacbb2020-11-04 21:44:01 +0000116
David Garcia49379ce2021-02-24 13:48:22 +0100117 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_http_readiness_probe(
128 "/ro/" if config.enable_ng_ro else "/openmano/tenants",
129 PORT,
130 initial_delay_seconds=10,
131 period_seconds=10,
132 timeout_seconds=5,
133 failure_threshold=3,
134 )
135 container_builder.add_http_liveness_probe(
136 "/ro/" if config.enable_ng_ro else "/openmano/tenants",
137 PORT,
138 initial_delay_seconds=600,
139 period_seconds=10,
140 timeout_seconds=5,
141 failure_threshold=3,
142 )
143 container_builder.add_envs(
144 {
145 "OSMRO_LOG_LEVEL": config.log_level,
146 }
147 )
148 if config.enable_ng_ro:
149 container_builder.add_envs(
150 {
151 "OSMRO_MESSAGE_DRIVER": "kafka",
152 "OSMRO_MESSAGE_HOST": self.kafka_client.host,
153 "OSMRO_MESSAGE_PORT": self.kafka_client.port,
154 # MongoDB configuration
155 "OSMRO_DATABASE_DRIVER": "mongo",
156 "OSMRO_DATABASE_URI": self.mongodb_client.connection_string,
157 "OSMRO_DATABASE_COMMONKEY": config.database_commonkey,
158 }
sousaeduccfacbb2020-11-04 21:44:01 +0000159 )
sousaeduccfacbb2020-11-04 21:44:01 +0000160
David Garcia49379ce2021-02-24 13:48:22 +0100161 else:
162 container_builder.add_envs(
163 {
164 "RO_DB_HOST": self.mysql_client.host,
165 "RO_DB_OVIM_HOST": self.mysql_client.host,
166 "RO_DB_PORT": self.mysql_client.port,
167 "RO_DB_OVIM_PORT": self.mysql_client.port,
168 "RO_DB_USER": self.mysql_client.user,
169 "RO_DB_OVIM_USER": self.mysql_client.user,
170 "RO_DB_PASSWORD": self.mysql_client.password,
171 "RO_DB_OVIM_PASSWORD": self.mysql_client.password,
172 "RO_DB_ROOT_PASSWORD": self.mysql_client.root_password,
173 "RO_DB_OVIM_ROOT_PASSWORD": self.mysql_client.root_password,
174 "RO_DB_NAME": config.ro_database,
175 "RO_DB_OVIM_NAME": config.vim_database,
176 "OPENMANO_TENANT": config.openmano_tenant,
177 }
178 )
179 container = container_builder.build()
180 # Add container to pod spec
181 pod_spec_builder.add_container(container)
182 return pod_spec_builder.build()
sousaeduccfacbb2020-11-04 21:44:01 +0000183
184
185if __name__ == "__main__":
186 main(RoCharm)