blob: f90b407a0a90d5a7ad7d9c2079595783daacfbc4 [file] [log] [blame]
David Garcia18c7a8b2020-07-02 18:36:32 +02001#!/usr/bin/env python3
2# Copyright 2020 Canonical Ltd.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain 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,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16import sys
17import logging
18
19sys.path.append("lib")
20
21from ops.charm import CharmBase
22from ops.framework import StoredState, Object
23from ops.main import main
24from ops.model import (
25 ActiveStatus,
26 MaintenanceStatus,
David Garcia5863d3e2020-07-09 13:14:13 +020027 WaitingStatus,
David Garcia18c7a8b2020-07-02 18:36:32 +020028)
29
30from glob import glob
31from pathlib import Path
32from string import Template
33
34logger = logging.getLogger(__name__)
35
36
37class PLACharm(CharmBase):
38 state = StoredState()
39
40 def __init__(self, framework, key):
41 super().__init__(framework, key)
42 self.state.set_default(spec=None)
David Garcia5863d3e2020-07-09 13:14:13 +020043 self.state.set_default(kafka_host=None)
44 self.state.set_default(kafka_port=None)
45 self.state.set_default(mongodb_uri=None)
46 self.state.set_default(mysql_host=None)
47 self.state.set_default(mysql_port=None)
David Garcia18c7a8b2020-07-02 18:36:32 +020048
49 # Observe Charm related events
50 self.framework.observe(self.on.config_changed, self.on_config_changed)
51 self.framework.observe(self.on.start, self.on_start)
52 self.framework.observe(self.on.upgrade_charm, self.on_upgrade_charm)
53
David Garcia5863d3e2020-07-09 13:14:13 +020054 # Relations
55 self.framework.observe(
56 self.on.kafka_relation_changed, self.on_kafka_relation_changed
57 )
58 self.framework.observe(
59 self.on.mongo_relation_changed, self.on_mongo_relation_changed
60 )
61 self.framework.observe(
62 self.on.mysql_relation_changed, self.on_mysql_relation_changed
63 )
64
David Garcia18c7a8b2020-07-02 18:36:32 +020065 def _apply_spec(self):
66 # Only apply the spec if this unit is a leader.
David Garcia5863d3e2020-07-09 13:14:13 +020067 unit = self.model.unit
68 if not unit.is_leader():
69 unit.status = ActiveStatus("Ready")
David Garcia18c7a8b2020-07-02 18:36:32 +020070 return
David Garcia5863d3e2020-07-09 13:14:13 +020071 if not self.state.kafka_host or not self.state.kafka_port:
72 unit.status = WaitingStatus("Waiting for Kafka")
73 return
74 if not self.state.mongodb_uri:
75 unit.status = WaitingStatus("Waiting for MongoDB")
76 return
77 if not self.state.mysql_host or not self.state.mysql_port:
78 unit.status = WaitingStatus("Waiting for MySQL")
79 return
80
81 unit.status = MaintenanceStatus("Applying new pod spec")
82
David Garcia18c7a8b2020-07-02 18:36:32 +020083 new_spec = self.make_pod_spec()
84 if new_spec == self.state.spec:
David Garcia5863d3e2020-07-09 13:14:13 +020085 unit.status = ActiveStatus("Ready")
David Garcia18c7a8b2020-07-02 18:36:32 +020086 return
87 self.framework.model.pod.set_spec(new_spec)
88 self.state.spec = new_spec
David Garcia5863d3e2020-07-09 13:14:13 +020089 unit.status = ActiveStatus("Ready")
David Garcia18c7a8b2020-07-02 18:36:32 +020090
91 def make_pod_spec(self):
92 config = self.framework.model.config
93
David Garcia5863d3e2020-07-09 13:14:13 +020094 mysql_uri = "mysql://root:{}@{}:{}/{}".format(
95 self.state.mysql_root_password,
96 self.state.mysql_host,
97 self.state.mysql_port,
98 self.state.mysql_database,
99 )
David Garcia18c7a8b2020-07-02 18:36:32 +0200100 ports = [
101 {"name": "port", "containerPort": config["port"], "protocol": "TCP",},
102 ]
103
David Garcia18c7a8b2020-07-02 18:36:32 +0200104 config_spec = {
105 "OSMPLA_MESSAGE_DRIVER": "kafka",
David Garcia5863d3e2020-07-09 13:14:13 +0200106 "OSMPLA_MESSAGE_HOST": self.state.kafka_host,
107 "OSMPLA_MESSAGE_PORT": self.state.kafka_port,
David Garcia18c7a8b2020-07-02 18:36:32 +0200108 "OSMPLA_DATABASE_DRIVER": "mongo",
David Garcia5863d3e2020-07-09 13:14:13 +0200109 "OSMPLA_DATABASE_URI": self.state.mongodb_uri,
David Garcia18c7a8b2020-07-02 18:36:32 +0200110 "OSMPLA_GLOBAL_LOG_LEVEL": config["log_level"],
David Garcia5863d3e2020-07-09 13:14:13 +0200111 "OSMPLA_SQL_DATABASE_URI": mysql_uri,
112 "OSMPLA_DATABASE_COMMONKEY": config["database_common_key"],
David Garcia18c7a8b2020-07-02 18:36:32 +0200113 }
114
115 spec = {
116 "version": 2,
117 "containers": [
118 {
119 "name": self.framework.model.app.name,
David Garcia5863d3e2020-07-09 13:14:13 +0200120 "image": config["image"],
David Garcia18c7a8b2020-07-02 18:36:32 +0200121 "ports": ports,
David Garcia18c7a8b2020-07-02 18:36:32 +0200122 "config": config_spec,
123 }
124 ],
125 }
126
127 return spec
128
129 def on_config_changed(self, event):
130 """Handle changes in configuration"""
David Garcia18c7a8b2020-07-02 18:36:32 +0200131 self._apply_spec()
David Garcia18c7a8b2020-07-02 18:36:32 +0200132
133 def on_start(self, event):
134 """Called when the charm is being installed"""
David Garcia18c7a8b2020-07-02 18:36:32 +0200135 self._apply_spec()
David Garcia18c7a8b2020-07-02 18:36:32 +0200136
137 def on_upgrade_charm(self, event):
138 """Upgrade the charm."""
139 unit = self.model.unit
140 unit.status = MaintenanceStatus("Upgrading charm")
141 self.on_start(event)
142
David Garcia5863d3e2020-07-09 13:14:13 +0200143 def on_kafka_relation_changed(self, event):
144 unit = self.model.unit
145 if not unit.is_leader():
146 return
147 self.state.kafka_host = event.relation.data[event.unit].get("host")
148 self.state.kafka_port = event.relation.data[event.unit].get("port")
149 self._apply_spec()
150
151 def on_mongo_relation_changed(self, event):
152 unit = self.model.unit
153 if not unit.is_leader():
154 return
155 self.state.mongodb_uri = event.relation.data[event.unit].get(
156 "connection_string"
157 )
158 self._apply_spec()
159
160 def on_mysql_relation_changed(self, event):
161 unit = self.model.unit
162 if not unit.is_leader():
163 return
164 unit_data = event.relation.data[event.unit]
165 self.state.mysql_host = unit_data.get("host")
166 self.state.mysql_port = unit_data.get("port")
167 self.state.mysql_root_password = unit_data.get("root_password")
168 self.state.mysql_database = self.model.config["database"]
169 self._apply_spec()
170
David Garcia18c7a8b2020-07-02 18:36:32 +0200171
172if __name__ == "__main__":
173 main(PLACharm)