blob: 917b54a903de48c4ef191cf45a95e4ef85457109 [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
David Garcia5d1ec6e2021-03-25 15:04:52 +010026import base64
sousaedu1dd4c0d2020-11-04 17:43:47 +000027import logging
David Garcia5d1ec6e2021-03-25 15:04:52 +010028from typing import NoReturn, Optional
sousaedu1dd4c0d2020-11-04 17:43:47 +000029
David Garciac753dc52021-03-17 15:28:47 +010030
sousaedu1dd4c0d2020-11-04 17:43:47 +000031from ops.main import main
David Garcia49379ce2021-02-24 13:48:22 +010032from opslib.osm.charm import CharmedOsmBase, RelationsMissing
David Garcia49379ce2021-02-24 13:48:22 +010033from opslib.osm.interfaces.kafka import KafkaClient
David Garciac753dc52021-03-17 15:28:47 +010034from opslib.osm.interfaces.keystone import KeystoneClient
David Garcia49379ce2021-02-24 13:48:22 +010035from opslib.osm.interfaces.mongo import MongoClient
36from opslib.osm.interfaces.prometheus import PrometheusClient
David Garcia141d9352021-09-08 17:48:40 +020037from opslib.osm.pod import (
38 ContainerV3Builder,
39 FilesV3Builder,
40 PodRestartPolicy,
41 PodSpecV3Builder,
42)
David Garciac753dc52021-03-17 15:28:47 +010043from opslib.osm.validator import ModelValidator, validator
sousaedu1dd4c0d2020-11-04 17:43:47 +000044
45
David Garcia49379ce2021-02-24 13:48:22 +010046logger = logging.getLogger(__name__)
sousaedu1dd4c0d2020-11-04 17:43:47 +000047
David Garcia49379ce2021-02-24 13:48:22 +010048PORT = 8000
sousaedu1dd4c0d2020-11-04 17:43:47 +000049
50
David Garcia5d1ec6e2021-03-25 15:04:52 +010051def _check_certificate_data(name: str, content: str):
52 if not name or not content:
53 raise ValueError("certificate name and content must be a non-empty string")
54
55
56def _extract_certificates(certs_config: str):
57 certificates = {}
58 if certs_config:
59 cert_list = certs_config.split(",")
60 for cert in cert_list:
61 name, content = cert.split(":")
62 _check_certificate_data(name, content)
63 certificates[name] = content
64 return certificates
65
66
67def decode(content: str):
68 return base64.b64decode(content.encode("utf-8")).decode("utf-8")
69
70
David Garcia49379ce2021-02-24 13:48:22 +010071class ConfigModel(ModelValidator):
calvinosanc1a43a22f2021-03-08 15:20:07 +010072 keystone_enabled: bool
David Garcia49379ce2021-02-24 13:48:22 +010073 vca_host: str
74 vca_user: str
David Garciac753dc52021-03-17 15:28:47 +010075 vca_secret: str
David Garcia49379ce2021-02-24 13:48:22 +010076 vca_cacert: str
77 database_commonkey: str
sousaedu996a5602021-05-03 00:22:43 +020078 mongodb_uri: Optional[str]
David Garcia49379ce2021-02-24 13:48:22 +010079 log_level: str
80 openstack_default_granularity: int
81 global_request_timeout: int
82 collector_interval: int
83 evaluator_interval: int
84 grafana_url: str
85 grafana_user: str
86 grafana_password: str
David Garcia5d1ec6e2021-03-25 15:04:52 +010087 certificates: Optional[str]
sousaedu0dc25b32021-08-30 16:33:33 +010088 image_pull_policy: str
sousaedu540d9372021-09-29 01:53:30 +010089 debug_mode: bool
90 security_context: bool
sousaedu1dd4c0d2020-11-04 17:43:47 +000091
David Garcia49379ce2021-02-24 13:48:22 +010092 @validator("log_level")
93 def validate_log_level(cls, v):
94 if v not in {"INFO", "DEBUG"}:
95 raise ValueError("value must be INFO or DEBUG")
96 return v
sousaedu1dd4c0d2020-11-04 17:43:47 +000097
David Garcia5d1ec6e2021-03-25 15:04:52 +010098 @validator("certificates")
99 def validate_certificates(cls, v):
100 # Raises an exception if it cannot extract the certificates
101 _extract_certificates(v)
102 return v
103
sousaedu996a5602021-05-03 00:22:43 +0200104 @validator("mongodb_uri")
105 def validate_mongodb_uri(cls, v):
106 if v and not v.startswith("mongodb://"):
107 raise ValueError("mongodb_uri is not properly formed")
108 return v
109
sousaedu3ddbbd12021-08-24 19:57:24 +0100110 @validator("image_pull_policy")
111 def validate_image_pull_policy(cls, v):
112 values = {
113 "always": "Always",
114 "ifnotpresent": "IfNotPresent",
115 "never": "Never",
116 }
117 v = v.lower()
118 if v not in values.keys():
119 raise ValueError("value must be always, ifnotpresent or never")
120 return values[v]
121
David Garcia5d1ec6e2021-03-25 15:04:52 +0100122 @property
123 def certificates_dict(cls):
124 return _extract_certificates(cls.certificates) if cls.certificates else {}
125
sousaedu1dd4c0d2020-11-04 17:43:47 +0000126
David Garcia49379ce2021-02-24 13:48:22 +0100127class MonCharm(CharmedOsmBase):
sousaedu1dd4c0d2020-11-04 17:43:47 +0000128 def __init__(self, *args) -> NoReturn:
David Garciad680be42021-08-17 11:03:55 +0200129 super().__init__(
130 *args,
131 oci_image="image",
132 debug_mode_config_key="debug_mode",
133 debug_pubkey_config_key="debug_pubkey",
134 vscode_workspace=VSCODE_WORKSPACE,
135 )
sousaedu1dd4c0d2020-11-04 17:43:47 +0000136
David Garcia49379ce2021-02-24 13:48:22 +0100137 self.kafka_client = KafkaClient(self, "kafka")
138 self.framework.observe(self.on["kafka"].relation_changed, self.configure_pod)
139 self.framework.observe(self.on["kafka"].relation_broken, self.configure_pod)
sousaedu1dd4c0d2020-11-04 17:43:47 +0000140
David Garcia49379ce2021-02-24 13:48:22 +0100141 self.mongodb_client = MongoClient(self, "mongodb")
142 self.framework.observe(self.on["mongodb"].relation_changed, self.configure_pod)
143 self.framework.observe(self.on["mongodb"].relation_broken, self.configure_pod)
sousaedu1dd4c0d2020-11-04 17:43:47 +0000144
David Garcia49379ce2021-02-24 13:48:22 +0100145 self.prometheus_client = PrometheusClient(self, "prometheus")
sousaedu1dd4c0d2020-11-04 17:43:47 +0000146 self.framework.observe(
David Garcia49379ce2021-02-24 13:48:22 +0100147 self.on["prometheus"].relation_changed, self.configure_pod
sousaedu1dd4c0d2020-11-04 17:43:47 +0000148 )
149 self.framework.observe(
David Garcia49379ce2021-02-24 13:48:22 +0100150 self.on["prometheus"].relation_broken, self.configure_pod
sousaedu1dd4c0d2020-11-04 17:43:47 +0000151 )
152
calvinosanc1a43a22f2021-03-08 15:20:07 +0100153 self.keystone_client = KeystoneClient(self, "keystone")
154 self.framework.observe(self.on["keystone"].relation_changed, self.configure_pod)
155 self.framework.observe(self.on["keystone"].relation_broken, self.configure_pod)
156
David Garcia49379ce2021-02-24 13:48:22 +0100157 def _check_missing_dependencies(self, config: ConfigModel):
158 missing_relations = []
159
160 if self.kafka_client.is_missing_data_in_unit():
161 missing_relations.append("kafka")
sousaedu996a5602021-05-03 00:22:43 +0200162 if not config.mongodb_uri and self.mongodb_client.is_missing_data_in_unit():
David Garcia49379ce2021-02-24 13:48:22 +0100163 missing_relations.append("mongodb")
164 if self.prometheus_client.is_missing_data_in_app():
165 missing_relations.append("prometheus")
calvinosanc1a43a22f2021-03-08 15:20:07 +0100166 if config.keystone_enabled:
167 if self.keystone_client.is_missing_data_in_app():
168 missing_relations.append("keystone")
David Garcia49379ce2021-02-24 13:48:22 +0100169
170 if missing_relations:
171 raise RelationsMissing(missing_relations)
172
David Garcia5d1ec6e2021-03-25 15:04:52 +0100173 def _build_cert_files(
174 self,
175 config: ConfigModel,
176 ):
177 cert_files_builder = FilesV3Builder()
178 for name, content in config.certificates_dict.items():
179 cert_files_builder.add_file(name, decode(content), mode=0o600)
180 return cert_files_builder.build()
181
David Garcia49379ce2021-02-24 13:48:22 +0100182 def build_pod_spec(self, image_info):
183 # Validate config
184 config = ConfigModel(**dict(self.config))
sousaedu996a5602021-05-03 00:22:43 +0200185
186 if config.mongodb_uri and not self.mongodb_client.is_missing_data_in_unit():
187 raise Exception("Mongodb data cannot be provided via config and relation")
188
David Garcia49379ce2021-02-24 13:48:22 +0100189 # Check relations
190 self._check_missing_dependencies(config)
sousaedu996a5602021-05-03 00:22:43 +0200191
sousaedu540d9372021-09-29 01:53:30 +0100192 security_context_enabled = (
193 config.security_context if not config.debug_mode else False
194 )
195
David Garcia49379ce2021-02-24 13:48:22 +0100196 # Create Builder for the PodSpec
sousaedu540d9372021-09-29 01:53:30 +0100197 pod_spec_builder = PodSpecV3Builder(
198 enable_security_context=security_context_enabled
199 )
sousaedu996a5602021-05-03 00:22:43 +0200200
David Garcia141d9352021-09-08 17:48:40 +0200201 # Add secrets to the pod
202 mongodb_secret_name = f"{self.app.name}-mongodb-secret"
203 pod_spec_builder.add_secret(
204 mongodb_secret_name,
205 {
206 "uri": config.mongodb_uri or self.mongodb_client.connection_string,
207 "commonkey": config.database_commonkey,
208 },
209 )
210 grafana_secret_name = f"{self.app.name}-grafana-secret"
211 pod_spec_builder.add_secret(
212 grafana_secret_name,
213 {
214 "url": config.grafana_url,
215 "user": config.grafana_user,
216 "password": config.grafana_password,
217 },
218 )
219
220 vca_secret_name = f"{self.app.name}-vca-secret"
221 pod_spec_builder.add_secret(
222 vca_secret_name,
223 {
224 "host": config.vca_host,
225 "user": config.vca_user,
226 "secret": config.vca_secret,
227 "cacert": config.vca_cacert,
228 },
229 )
230
David Garcia49379ce2021-02-24 13:48:22 +0100231 # Build Container
sousaedu3ddbbd12021-08-24 19:57:24 +0100232 container_builder = ContainerV3Builder(
sousaedu540d9372021-09-29 01:53:30 +0100233 self.app.name,
234 image_info,
235 config.image_pull_policy,
236 run_as_non_root=security_context_enabled,
sousaedu3ddbbd12021-08-24 19:57:24 +0100237 )
David Garcia5d1ec6e2021-03-25 15:04:52 +0100238 certs_files = self._build_cert_files(config)
sousaedu996a5602021-05-03 00:22:43 +0200239
David Garcia5d1ec6e2021-03-25 15:04:52 +0100240 if certs_files:
241 container_builder.add_volume_config("certs", "/certs", certs_files)
sousaedu996a5602021-05-03 00:22:43 +0200242
David Garcia49379ce2021-02-24 13:48:22 +0100243 container_builder.add_port(name=self.app.name, port=PORT)
244 container_builder.add_envs(
245 {
246 # General configuration
247 "ALLOW_ANONYMOUS_LOGIN": "yes",
248 "OSMMON_OPENSTACK_DEFAULT_GRANULARITY": config.openstack_default_granularity,
249 "OSMMON_GLOBAL_REQUEST_TIMEOUT": config.global_request_timeout,
250 "OSMMON_GLOBAL_LOGLEVEL": config.log_level,
251 "OSMMON_COLLECTOR_INTERVAL": config.collector_interval,
252 "OSMMON_EVALUATOR_INTERVAL": config.evaluator_interval,
253 # Kafka configuration
254 "OSMMON_MESSAGE_DRIVER": "kafka",
255 "OSMMON_MESSAGE_HOST": self.kafka_client.host,
256 "OSMMON_MESSAGE_PORT": self.kafka_client.port,
257 # Database configuration
258 "OSMMON_DATABASE_DRIVER": "mongo",
David Garcia49379ce2021-02-24 13:48:22 +0100259 # Prometheus configuration
260 "OSMMON_PROMETHEUS_URL": f"http://{self.prometheus_client.hostname}:{self.prometheus_client.port}",
David Garcia49379ce2021-02-24 13:48:22 +0100261 }
sousaedu1dd4c0d2020-11-04 17:43:47 +0000262 )
David Garcia141d9352021-09-08 17:48:40 +0200263 container_builder.add_secret_envs(
264 secret_name=mongodb_secret_name,
265 envs={
266 "OSMMON_DATABASE_URI": "uri",
267 "OSMMON_DATABASE_COMMONKEY": "commonkey",
268 },
269 )
270 container_builder.add_secret_envs(
271 secret_name=vca_secret_name,
272 envs={
273 "OSMMON_VCA_HOST": "host",
274 "OSMMON_VCA_USER": "user",
275 "OSMMON_VCA_SECRET": "secret",
276 "OSMMON_VCA_CACERT": "cacert",
277 },
278 )
279 container_builder.add_secret_envs(
280 secret_name=grafana_secret_name,
281 envs={
282 "OSMMON_GRAFANA_URL": "url",
283 "OSMMON_GRAFANA_USER": "user",
284 "OSMMON_GRAFANA_PASSWORD": "password",
285 },
286 )
calvinosanc1a43a22f2021-03-08 15:20:07 +0100287 if config.keystone_enabled:
David Garcia141d9352021-09-08 17:48:40 +0200288 keystone_secret_name = f"{self.app.name}-keystone-secret"
289 pod_spec_builder.add_secret(
290 keystone_secret_name,
calvinosanc1a43a22f2021-03-08 15:20:07 +0100291 {
David Garcia141d9352021-09-08 17:48:40 +0200292 "url": self.keystone_client.host,
293 "user_domain": self.keystone_client.user_domain_name,
294 "project_domain": self.keystone_client.project_domain_name,
295 "service_username": self.keystone_client.username,
296 "service_password": self.keystone_client.password,
297 "service_project": self.keystone_client.service,
298 },
299 )
300 container_builder.add_env("OSMMON_KEYSTONE_ENABLED", True)
301 container_builder.add_secret_envs(
302 secret_name=keystone_secret_name,
303 envs={
304 "OSMMON_KEYSTONE_URL": "url",
305 "OSMMON_KEYSTONE_DOMAIN_NAME": "user_domain",
306 "OSMMON_KEYSTONE_PROJECT_DOMAIN_NAME": "project_domain",
307 "OSMMON_KEYSTONE_SERVICE_USER": "service_username",
308 "OSMMON_KEYSTONE_SERVICE_PASSWORD": "service_password",
309 "OSMMON_KEYSTONE_SERVICE_PROJECT": "service_project",
310 },
calvinosanc1a43a22f2021-03-08 15:20:07 +0100311 )
David Garcia49379ce2021-02-24 13:48:22 +0100312 container = container_builder.build()
sousaedu996a5602021-05-03 00:22:43 +0200313
David Garcia141d9352021-09-08 17:48:40 +0200314 # Add restart policy
315 restart_policy = PodRestartPolicy()
316 restart_policy.add_secrets()
317 pod_spec_builder.set_restart_policy(restart_policy)
318
David Garcia49379ce2021-02-24 13:48:22 +0100319 # Add container to pod spec
320 pod_spec_builder.add_container(container)
sousaedu996a5602021-05-03 00:22:43 +0200321
David Garcia49379ce2021-02-24 13:48:22 +0100322 return pod_spec_builder.build()
sousaedu1dd4c0d2020-11-04 17:43:47 +0000323
324
David Garciad680be42021-08-17 11:03:55 +0200325VSCODE_WORKSPACE = {
326 "folders": [
327 {"path": "/usr/lib/python3/dist-packages/osm_mon"},
328 {"path": "/usr/lib/python3/dist-packages/osm_common"},
329 {"path": "/usr/lib/python3/dist-packages/n2vc"},
330 ],
331 "settings": {},
332 "launch": {
333 "version": "0.2.0",
334 "configurations": [
335 {
336 "name": "MON Server",
337 "type": "python",
338 "request": "launch",
339 "module": "osm_mon.cmd.mon_server",
340 "justMyCode": False,
341 },
342 {
343 "name": "MON evaluator",
344 "type": "python",
345 "request": "launch",
346 "module": "osm_mon.cmd.mon_evaluator",
347 "justMyCode": False,
348 },
349 {
350 "name": "MON collector",
351 "type": "python",
352 "request": "launch",
353 "module": "osm_mon.cmd.mon_collector",
354 "justMyCode": False,
355 },
356 {
357 "name": "MON dashboarder",
358 "type": "python",
359 "request": "launch",
360 "module": "osm_mon.cmd.mon_dashboarder",
361 "justMyCode": False,
362 },
363 ],
364 },
365}
sousaedu1dd4c0d2020-11-04 17:43:47 +0000366if __name__ == "__main__":
367 main(MonCharm)