| David Garcia | 081f469 | 2020-07-02 18:17:56 +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 NGUICharm(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 | # self.framework.observe( |
| 48 | # self.on.nbi_relation_joined, self.on_nbi_relation_joined |
| 49 | # ) |
| 50 | |
| 51 | def _apply_spec(self): |
| 52 | # Only apply the spec if this unit is a leader. |
| 53 | if not self.framework.model.unit.is_leader(): |
| 54 | return |
| 55 | new_spec = self.make_pod_spec() |
| 56 | if new_spec == self.state.spec: |
| 57 | return |
| 58 | self.framework.model.pod.set_spec(new_spec) |
| 59 | self.state.spec = new_spec |
| 60 | |
| 61 | def make_pod_spec(self): |
| 62 | config = self.framework.model.config |
| 63 | |
| 64 | ports = [ |
| 65 | {"name": "port", "containerPort": config["port"], "protocol": "TCP",}, |
| 66 | ] |
| 67 | |
| 68 | kubernetes = { |
| 69 | "readinessProbe": { |
| 70 | "tcpSocket": {"port": config["port"]}, |
| 71 | "timeoutSeconds": 5, |
| 72 | "periodSeconds": 5, |
| 73 | "initialDelaySeconds": 10, |
| 74 | }, |
| 75 | "livenessProbe": { |
| 76 | "tcpSocket": {"port": config["port"]}, |
| 77 | "timeoutSeconds": 5, |
| 78 | "initialDelaySeconds": 45, |
| 79 | }, |
| 80 | } |
| 81 | config_spec = { |
| 82 | "port": config["port"], |
| 83 | "server_name": config["server_name"], |
| 84 | "client_max_body_size": config["client_max_body_size"], |
| 85 | "nbi_hostname": config["nbi_hostname"], |
| 86 | "nbi_port": config["nbi_port"], |
| 87 | } |
| 88 | |
| 89 | files = [ |
| 90 | { |
| 91 | "name": "configuration", |
| 92 | "mountPath": "/etc/nginx/sites-available/", |
| 93 | "files": { |
| 94 | Path(filename) |
| 95 | .name: Template(Path(filename).read_text()) |
| 96 | .substitute(config_spec) |
| 97 | for filename in glob("files/*") |
| 98 | }, |
| 99 | }, |
| 100 | ] |
| 101 | logger.debug(files) |
| 102 | spec = { |
| 103 | "version": 2, |
| 104 | "containers": [ |
| 105 | { |
| 106 | "name": self.framework.model.app.name, |
| 107 | "image": "{}".format(config["image"]), |
| 108 | "ports": ports, |
| 109 | "kubernetes": kubernetes, |
| 110 | "files": files, |
| 111 | } |
| 112 | ], |
| 113 | } |
| 114 | |
| 115 | return spec |
| 116 | |
| 117 | def on_config_changed(self, event): |
| 118 | """Handle changes in configuration""" |
| 119 | unit = self.model.unit |
| 120 | unit.status = MaintenanceStatus("Applying new pod spec") |
| 121 | self._apply_spec() |
| 122 | unit.status = ActiveStatus("Ready") |
| 123 | |
| 124 | def on_start(self, event): |
| 125 | """Called when the charm is being installed""" |
| 126 | unit = self.model.unit |
| 127 | unit.status = MaintenanceStatus("Applying pod spec") |
| 128 | self._apply_spec() |
| 129 | unit.status = ActiveStatus("Ready") |
| 130 | |
| 131 | def on_upgrade_charm(self, event): |
| 132 | """Upgrade the charm.""" |
| 133 | unit = self.model.unit |
| 134 | unit.status = MaintenanceStatus("Upgrading charm") |
| 135 | self.on_start(event) |
| 136 | |
| 137 | # def on_nbi_relation_joined(self, event): |
| 138 | # unit = self.model.unit |
| 139 | # if not unit.is_leader(): |
| 140 | # return |
| 141 | # config = self.framework.model.config |
| 142 | # unit = MaintenanceStatus("Sending connection data") |
| 143 | |
| 144 | # unit = ActiveStatus("Ready") |
| 145 | |
| 146 | |
| 147 | if __name__ == "__main__": |
| 148 | main(NGUICharm) |