blob: 6847580f063c03993db856c7d53957cff64f04d3 [file] [log] [blame]
beierlma4a37f72020-06-26 12:55:01 -04001#!/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
beierlma4a37f72020-06-26 12:55:01 -040016import logging
beierlma4a37f72020-06-26 12:55:01 -040017
18from ops.charm import CharmBase
David Garcia95ba7e12021-02-03 11:10:28 +010019from ops.framework import StoredState
beierlma4a37f72020-06-26 12:55:01 -040020from ops.main import main
21from ops.model import (
22 ActiveStatus,
23 MaintenanceStatus,
24 WaitingStatus,
25)
David Garcia95ba7e12021-02-03 11:10:28 +010026from typing import NoReturn
beierlmb1a1c462020-10-23 14:54:56 -040027
beierlma4a37f72020-06-26 12:55:01 -040028logger = logging.getLogger(__name__)
29
30
31class PLACharm(CharmBase):
32 state = StoredState()
33
David Garcia95ba7e12021-02-03 11:10:28 +010034 def __init__(self, *args) -> NoReturn:
35 super().__init__(*args)
beierlma4a37f72020-06-26 12:55:01 -040036 self.state.set_default(spec=None)
37 self.state.set_default(kafka_host=None)
38 self.state.set_default(kafka_port=None)
39 self.state.set_default(mongodb_uri=None)
40
41 # Observe Charm related events
42 self.framework.observe(self.on.config_changed, self.on_config_changed)
43 self.framework.observe(self.on.start, self.on_start)
44 self.framework.observe(self.on.upgrade_charm, self.on_upgrade_charm)
45
46 # Relations
47 self.framework.observe(
48 self.on.kafka_relation_changed, self.on_kafka_relation_changed
49 )
50 self.framework.observe(
51 self.on.mongo_relation_changed, self.on_mongo_relation_changed
52 )
53
54 def _apply_spec(self):
55 # Only apply the spec if this unit is a leader.
56 unit = self.model.unit
57 if not unit.is_leader():
58 unit.status = ActiveStatus("ready")
59 return
60 if not self.state.kafka_host or not self.state.kafka_port:
61 unit.status = WaitingStatus("Waiting for Kafka")
62 return
63 if not self.state.mongodb_uri:
64 unit.status = WaitingStatus("Waiting for MongoDB")
65 return
66
67 unit.status = MaintenanceStatus("Applying new pod spec")
68
69 new_spec = self.make_pod_spec()
70 if new_spec == self.state.spec:
71 unit.status = ActiveStatus("ready")
72 return
73 self.framework.model.pod.set_spec(new_spec)
74 self.state.spec = new_spec
75 unit.status = ActiveStatus("ready")
76
77 def make_pod_spec(self):
78 config = self.framework.model.config
79
80 ports = [
David Garciafa75eca2020-11-04 18:34:41 +010081 {
82 "name": "port",
83 "containerPort": config["port"],
84 "protocol": "TCP",
85 },
beierlma4a37f72020-06-26 12:55:01 -040086 ]
87
88 config_spec = {
89 "OSMPLA_MESSAGE_DRIVER": "kafka",
90 "OSMPLA_MESSAGE_HOST": self.state.kafka_host,
91 "OSMPLA_MESSAGE_PORT": self.state.kafka_port,
92 "OSMPLA_DATABASE_DRIVER": "mongo",
93 "OSMPLA_DATABASE_URI": self.state.mongodb_uri,
94 "OSMPLA_GLOBAL_LOG_LEVEL": config["log_level"],
95 "OSMPLA_DATABASE_COMMONKEY": config["database_common_key"],
96 }
97
98 spec = {
99 "version": 2,
100 "containers": [
101 {
102 "name": self.framework.model.app.name,
David Garciafa75eca2020-11-04 18:34:41 +0100103 "imageDetails": {
104 "imagePath": config["image"],
105 "username": config["image_username"],
106 "password": config["image_password"],
107 },
beierlma4a37f72020-06-26 12:55:01 -0400108 "ports": ports,
109 "config": config_spec,
110 }
111 ],
112 }
113
114 return spec
115
116 def on_config_changed(self, event):
117 """Handle changes in configuration"""
118 self._apply_spec()
119
120 def on_start(self, event):
121 """Called when the charm is being installed"""
122 self._apply_spec()
123
124 def on_upgrade_charm(self, event):
125 """Upgrade the charm."""
126 unit = self.model.unit
127 unit.status = MaintenanceStatus("Upgrading charm")
David Garcia68faf8d2020-09-01 10:12:16 +0200128 self._apply_spec()
beierlma4a37f72020-06-26 12:55:01 -0400129
130 def on_kafka_relation_changed(self, event):
David Garcia68faf8d2020-09-01 10:12:16 +0200131 kafka_host = event.relation.data[event.unit].get("host")
132 kafka_port = event.relation.data[event.unit].get("port")
133 if kafka_host and self.state.kafka_host != kafka_host:
134 self.state.kafka_host = kafka_host
135 if kafka_port and self.state.kafka_port != kafka_port:
136 self.state.kafka_port = kafka_port
beierlma4a37f72020-06-26 12:55:01 -0400137 self._apply_spec()
138
139 def on_mongo_relation_changed(self, event):
David Garciafa75eca2020-11-04 18:34:41 +0100140 mongodb_uri = event.relation.data[event.unit].get("connection_string")
David Garcia68faf8d2020-09-01 10:12:16 +0200141 if mongodb_uri and self.state.mongodb_uri != mongodb_uri:
142 self.state.mongodb_uri = mongodb_uri
beierlma4a37f72020-06-26 12:55:01 -0400143 self._apply_spec()
144
145
146if __name__ == "__main__":
147 main(PLACharm)