| David Garcia | 18c7a8b | 2020-07-02 18:36:32 +0200 | [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 | |
| 16 | import sys |
| 17 | import logging |
| 18 | |
| 19 | sys.path.append("lib") |
| 20 | |
| 21 | from ops.charm import CharmBase |
| 22 | from ops.framework import StoredState, Object |
| 23 | from ops.main import main |
| 24 | from ops.model import ( |
| 25 | ActiveStatus, |
| 26 | MaintenanceStatus, |
| 27 | ) |
| 28 | |
| 29 | from glob import glob |
| 30 | from pathlib import Path |
| 31 | from string import Template |
| 32 | |
| 33 | logger = logging.getLogger(__name__) |
| 34 | |
| 35 | |
| 36 | class PLACharm(CharmBase): |
| 37 | state = StoredState() |
| 38 | |
| 39 | def __init__(self, framework, key): |
| 40 | super().__init__(framework, key) |
| 41 | self.state.set_default(spec=None) |
| 42 | |
| 43 | # Observe Charm related events |
| 44 | self.framework.observe(self.on.config_changed, self.on_config_changed) |
| 45 | self.framework.observe(self.on.start, self.on_start) |
| 46 | self.framework.observe(self.on.upgrade_charm, self.on_upgrade_charm) |
| 47 | |
| 48 | def _apply_spec(self): |
| 49 | # Only apply the spec if this unit is a leader. |
| 50 | if not self.framework.model.unit.is_leader(): |
| 51 | return |
| 52 | new_spec = self.make_pod_spec() |
| 53 | if new_spec == self.state.spec: |
| 54 | return |
| 55 | self.framework.model.pod.set_spec(new_spec) |
| 56 | self.state.spec = new_spec |
| 57 | |
| 58 | def make_pod_spec(self): |
| 59 | config = self.framework.model.config |
| 60 | |
| 61 | ports = [ |
| 62 | {"name": "port", "containerPort": config["port"], "protocol": "TCP",}, |
| 63 | ] |
| 64 | |
| 65 | kubernetes = { |
| 66 | "readinessProbe": { |
| 67 | "tcpSocket": {"port": config["port"]}, |
| 68 | "timeoutSeconds": 5, |
| 69 | "periodSeconds": 5, |
| 70 | "initialDelaySeconds": 10, |
| 71 | }, |
| 72 | "livenessProbe": { |
| 73 | "tcpSocket": {"port": config["port"]}, |
| 74 | "timeoutSeconds": 5, |
| 75 | "initialDelaySeconds": 45, |
| 76 | }, |
| 77 | } |
| 78 | config_spec = { |
| 79 | "OSMPLA_MESSAGE_DRIVER": "kafka", |
| 80 | "OSMPLA_MESSAGE_HOST": config["kafka_host"], |
| 81 | "OSMPLA_MESSAGE_PORT": config["kafka_port"], |
| 82 | "OSMPLA_DATABASE_DRIVER": "mongo", |
| 83 | "OSMPLA_DATABASE_URI": config["mongo_uri"], |
| 84 | "OSMPLA_GLOBAL_LOG_LEVEL": config["log_level"], |
| 85 | } |
| 86 | |
| 87 | spec = { |
| 88 | "version": 2, |
| 89 | "containers": [ |
| 90 | { |
| 91 | "name": self.framework.model.app.name, |
| 92 | "image": "{}".format(config["image"]), |
| 93 | "ports": ports, |
| 94 | "kubernetes": kubernetes, |
| 95 | "config": config_spec, |
| 96 | } |
| 97 | ], |
| 98 | } |
| 99 | |
| 100 | return spec |
| 101 | |
| 102 | def on_config_changed(self, event): |
| 103 | """Handle changes in configuration""" |
| 104 | unit = self.model.unit |
| 105 | unit.status = MaintenanceStatus("Applying new pod spec") |
| 106 | self._apply_spec() |
| 107 | unit.status = ActiveStatus("Ready") |
| 108 | |
| 109 | def on_start(self, event): |
| 110 | """Called when the charm is being installed""" |
| 111 | unit = self.model.unit |
| 112 | unit.status = MaintenanceStatus("Applying pod spec") |
| 113 | self._apply_spec() |
| 114 | unit.status = ActiveStatus("Ready") |
| 115 | |
| 116 | def on_upgrade_charm(self, event): |
| 117 | """Upgrade the charm.""" |
| 118 | unit = self.model.unit |
| 119 | unit.status = MaintenanceStatus("Upgrading charm") |
| 120 | self.on_start(event) |
| 121 | |
| 122 | |
| 123 | if __name__ == "__main__": |
| 124 | main(PLACharm) |