| beierlm | a4a37f7 | 2020-06-26 12:55:01 -0400 | [diff] [blame] | 1 | #!/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 | |
| beierlm | a4a37f7 | 2020-06-26 12:55:01 -0400 | [diff] [blame] | 16 | import logging |
| beierlm | a4a37f7 | 2020-06-26 12:55:01 -0400 | [diff] [blame] | 17 | |
| 18 | from ops.charm import CharmBase |
| David Garcia | 95ba7e1 | 2021-02-03 11:10:28 +0100 | [diff] [blame] | 19 | from ops.framework import StoredState |
| beierlm | a4a37f7 | 2020-06-26 12:55:01 -0400 | [diff] [blame] | 20 | from ops.main import main |
| 21 | from ops.model import ( |
| 22 | ActiveStatus, |
| 23 | MaintenanceStatus, |
| 24 | WaitingStatus, |
| 25 | ) |
| David Garcia | 95ba7e1 | 2021-02-03 11:10:28 +0100 | [diff] [blame] | 26 | from typing import NoReturn |
| beierlm | b1a1c46 | 2020-10-23 14:54:56 -0400 | [diff] [blame] | 27 | |
| beierlm | a4a37f7 | 2020-06-26 12:55:01 -0400 | [diff] [blame] | 28 | logger = logging.getLogger(__name__) |
| 29 | |
| 30 | |
| 31 | class PLACharm(CharmBase): |
| 32 | state = StoredState() |
| 33 | |
| David Garcia | 95ba7e1 | 2021-02-03 11:10:28 +0100 | [diff] [blame] | 34 | def __init__(self, *args) -> NoReturn: |
| 35 | super().__init__(*args) |
| beierlm | a4a37f7 | 2020-06-26 12:55:01 -0400 | [diff] [blame] | 36 | 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 Garcia | fa75eca | 2020-11-04 18:34:41 +0100 | [diff] [blame] | 81 | { |
| 82 | "name": "port", |
| 83 | "containerPort": config["port"], |
| 84 | "protocol": "TCP", |
| 85 | }, |
| beierlm | a4a37f7 | 2020-06-26 12:55:01 -0400 | [diff] [blame] | 86 | ] |
| 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 Garcia | fa75eca | 2020-11-04 18:34:41 +0100 | [diff] [blame] | 103 | "imageDetails": { |
| 104 | "imagePath": config["image"], |
| 105 | "username": config["image_username"], |
| 106 | "password": config["image_password"], |
| 107 | }, |
| beierlm | a4a37f7 | 2020-06-26 12:55:01 -0400 | [diff] [blame] | 108 | "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 Garcia | 68faf8d | 2020-09-01 10:12:16 +0200 | [diff] [blame] | 128 | self._apply_spec() |
| beierlm | a4a37f7 | 2020-06-26 12:55:01 -0400 | [diff] [blame] | 129 | |
| 130 | def on_kafka_relation_changed(self, event): |
| David Garcia | 68faf8d | 2020-09-01 10:12:16 +0200 | [diff] [blame] | 131 | 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 |
| beierlm | a4a37f7 | 2020-06-26 12:55:01 -0400 | [diff] [blame] | 137 | self._apply_spec() |
| 138 | |
| 139 | def on_mongo_relation_changed(self, event): |
| David Garcia | fa75eca | 2020-11-04 18:34:41 +0100 | [diff] [blame] | 140 | mongodb_uri = event.relation.data[event.unit].get("connection_string") |
| David Garcia | 68faf8d | 2020-09-01 10:12:16 +0200 | [diff] [blame] | 141 | if mongodb_uri and self.state.mongodb_uri != mongodb_uri: |
| 142 | self.state.mongodb_uri = mongodb_uri |
| beierlm | a4a37f7 | 2020-06-26 12:55:01 -0400 | [diff] [blame] | 143 | self._apply_spec() |
| 144 | |
| 145 | |
| 146 | if __name__ == "__main__": |
| 147 | main(PLACharm) |