| David Garcia | 82c5ffa | 2020-03-09 08:38:17 +0100 | [diff] [blame] | 1 | # Copyright 2020 Canonical Ltd. |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | # |
| 15 | from charms.layer.caas_base import pod_spec_set |
| 16 | from charms.reactive import endpoint_from_flag |
| 17 | from charms.reactive import when, when_not, hook |
| 18 | from charms.reactive.flags import set_flag, clear_flag |
| 19 | from charmhelpers.core.hookenv import ( |
| 20 | log, |
| 21 | metadata, |
| 22 | config, |
| 23 | ) |
| 24 | from charms import layer |
| 25 | |
| 26 | |
| 27 | @hook("upgrade-charm") |
| 28 | @when("leadership.is_leader") |
| 29 | def upgrade(): |
| 30 | clear_flag("ui-k8s.configured") |
| 31 | |
| 32 | |
| 33 | @when("config.changed") |
| 34 | @when("leadership.is_leader") |
| 35 | def restart(): |
| 36 | clear_flag("ui-k8s.configured") |
| 37 | |
| 38 | |
| 39 | @when_not("mysql.available") |
| 40 | @when_not("ui-k8s.configured") |
| 41 | def waiting_for_mysql(): |
| 42 | layer.status.waiting("Waiting for mysql to be available") |
| 43 | |
| 44 | |
| 45 | @when_not("nbi.ready") |
| 46 | @when_not("ui-k8s.configured") |
| 47 | def waiting_for_nbi(): |
| 48 | layer.status.waiting("Waiting for nbi to be available") |
| 49 | |
| 50 | |
| 51 | @when("mysql.available", "nbi.ready") |
| 52 | @when_not("ui-k8s.configured") |
| 53 | @when("leadership.is_leader") |
| 54 | def configure(): |
| 55 | |
| 56 | layer.status.maintenance("Configuring ui container") |
| 57 | try: |
| 58 | mysql = endpoint_from_flag("mysql.available") |
| 59 | nbi = endpoint_from_flag("nbi.ready") |
| 60 | nbi_unit = nbi.nbis()[0] |
| 61 | nbi_host = "{}".format(nbi_unit["host"]) |
| 62 | spec = make_pod_spec( |
| 63 | mysql.host(), |
| 64 | mysql.port(), |
| 65 | mysql.user(), |
| 66 | mysql.password(), |
| 67 | mysql.root_password(), |
| 68 | nbi_host, |
| 69 | ) |
| 70 | log("set pod spec:\n{}".format(spec)) |
| 71 | pod_spec_set(spec) |
| 72 | set_flag("ui-k8s.configured") |
| 73 | except Exception as e: |
| 74 | layer.status.blocked("k8s spec failed to deploy: {}".format(e)) |
| 75 | |
| 76 | |
| 77 | @when("ui-k8s.configured") |
| 78 | def set_ui_active(): |
| 79 | layer.status.active("ready") |
| 80 | |
| 81 | |
| 82 | def make_pod_spec( |
| 83 | mysql_host, mysql_port, mysql_user, mysql_password, mysql_root_password, nbi_host |
| 84 | ): |
| 85 | """Make pod specification for Kubernetes |
| 86 | |
| 87 | Args: |
| 88 | mysql_name (str): UI DB name |
| 89 | mysql_host (str): UI DB host |
| 90 | mysql_port (int): UI DB port |
| 91 | mysql_user (str): UI DB user |
| 92 | mysql_password (str): UI DB password |
| 93 | nbi_uri (str): NBI URI |
| 94 | Returns: |
| 95 | pod_spec: Pod specification for Kubernetes |
| 96 | """ |
| 97 | |
| 98 | with open("reactive/spec_template.yaml") as spec_file: |
| 99 | pod_spec_template = spec_file.read() |
| 100 | |
| 101 | md = metadata() |
| 102 | cfg = config() |
| 103 | |
| 104 | data = { |
| 105 | "name": md.get("name"), |
| 106 | "docker_image": cfg.get("image"), |
| 107 | "mysql_host": mysql_host, |
| 108 | "mysql_port": mysql_port, |
| 109 | "mysql_user": mysql_user, |
| 110 | "mysql_password": mysql_password, |
| 111 | "mysql_root_password": mysql_root_password, |
| 112 | "nbi_host": nbi_host, |
| 113 | } |
| 114 | data.update(cfg) |
| 115 | |
| 116 | return pod_spec_template % data |
| 117 | |
| 118 | |
| 119 | def get_ui_port(): |
| 120 | """Returns UI port""" |
| 121 | cfg = config() |
| 122 | return cfg.get("advertised-port") |