| 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. |
| David Garcia | 02a5eb9 | 2020-11-28 14:41:22 +0100 | [diff] [blame] | 14 | import yaml |
| 15 | |
| 16 | from charmhelpers.core.hookenv import log, metadata, config |
| 17 | from charms import layer |
| David Garcia | 82c5ffa | 2020-03-09 08:38:17 +0100 | [diff] [blame] | 18 | from charms.layer.caas_base import pod_spec_set |
| David Garcia | 02a5eb9 | 2020-11-28 14:41:22 +0100 | [diff] [blame] | 19 | from charms.osm.k8s import get_service_ip |
| David Garcia | 82c5ffa | 2020-03-09 08:38:17 +0100 | [diff] [blame] | 20 | from charms.reactive import endpoint_from_flag |
| 21 | from charms.reactive import when, when_not, hook |
| 22 | from charms.reactive.flags import set_flag, clear_flag |
| David Garcia | 82c5ffa | 2020-03-09 08:38:17 +0100 | [diff] [blame] | 23 | |
| 24 | |
| 25 | @hook("upgrade-charm") |
| 26 | @when("leadership.is_leader") |
| 27 | def upgrade(): |
| 28 | clear_flag("ro-k8s.configured") |
| David Garcia | 02a5eb9 | 2020-11-28 14:41:22 +0100 | [diff] [blame] | 29 | clear_flag("ro-k8s.ready") |
| David Garcia | 82c5ffa | 2020-03-09 08:38:17 +0100 | [diff] [blame] | 30 | |
| 31 | |
| 32 | @when("config.changed") |
| 33 | @when("leadership.is_leader") |
| 34 | def restart(): |
| 35 | clear_flag("ro-k8s.configured") |
| David Garcia | 02a5eb9 | 2020-11-28 14:41:22 +0100 | [diff] [blame] | 36 | clear_flag("ro-k8s.ready") |
| David Garcia | 82c5ffa | 2020-03-09 08:38:17 +0100 | [diff] [blame] | 37 | |
| David Garcia | d2d52f7 | 2020-11-28 14:38:35 +0100 | [diff] [blame] | 38 | |
| David Garcia | 02a5eb9 | 2020-11-28 14:41:22 +0100 | [diff] [blame] | 39 | @when_not("mongo.ready") |
| David Garcia | d2d52f7 | 2020-11-28 14:38:35 +0100 | [diff] [blame] | 40 | @when_not("mysql.available") |
| David Garcia | 82c5ffa | 2020-03-09 08:38:17 +0100 | [diff] [blame] | 41 | @when_not("ro-k8s.configured") |
| David Garcia | 68faf8d | 2020-09-01 10:12:16 +0200 | [diff] [blame] | 42 | @when("leadership.is_leader") |
| David Garcia | d2d52f7 | 2020-11-28 14:38:35 +0100 | [diff] [blame] | 43 | def waiting_for_mysql(): |
| David Garcia | 02a5eb9 | 2020-11-28 14:41:22 +0100 | [diff] [blame] | 44 | layer.status.waiting("Waiting for db to be ready") |
| 45 | clear_flag("ro-k8s.ready") |
| David Garcia | 82c5ffa | 2020-03-09 08:38:17 +0100 | [diff] [blame] | 46 | |
| 47 | |
| David Garcia | 02a5eb9 | 2020-11-28 14:41:22 +0100 | [diff] [blame] | 48 | @when_not("kafka.ready") |
| 49 | @when_not("ro-k8s.configured") |
| 50 | @when("leadership.is_leader") |
| 51 | def waiting_for_kafka(): |
| 52 | layer.status.waiting("Waiting for kafka to be ready") |
| 53 | clear_flag("ro-k8s.ready") |
| 54 | |
| 55 | |
| 56 | @when_not("ro-k8s.ready") |
| David Garcia | d2d52f7 | 2020-11-28 14:38:35 +0100 | [diff] [blame] | 57 | @when("mysql.available") |
| David Garcia | 02a5eb9 | 2020-11-28 14:41:22 +0100 | [diff] [blame] | 58 | def ro_k8s_mysql_ready(): |
| 59 | set_flag("ro-k8s.ready") |
| 60 | |
| 61 | |
| 62 | @when_not("ro-k8s.ready") |
| 63 | @when("kafka.ready") |
| 64 | @when("mongo.ready") |
| 65 | def ro_k8s_kafka_mongo_ready(): |
| 66 | set_flag("ro-k8s.ready") |
| 67 | |
| 68 | |
| 69 | @when("ro-k8s.ready") |
| David Garcia | 82c5ffa | 2020-03-09 08:38:17 +0100 | [diff] [blame] | 70 | @when_not("ro-k8s.configured") |
| 71 | @when("leadership.is_leader") |
| 72 | def configure(): |
| 73 | layer.status.maintenance("Configuring ro container") |
| 74 | try: |
| David Garcia | d2d52f7 | 2020-11-28 14:38:35 +0100 | [diff] [blame] | 75 | mysql = endpoint_from_flag("mysql.available") |
| David Garcia | 02a5eb9 | 2020-11-28 14:41:22 +0100 | [diff] [blame] | 76 | kafka = endpoint_from_flag("kafka.ready") |
| 77 | mongo = endpoint_from_flag("mongo.ready") |
| 78 | spec = None |
| 79 | if mysql: |
| 80 | if mysql.host() is not None: |
| 81 | spec = make_pod_spec( |
| 82 | mysql.host(), |
| 83 | mysql.port(), |
| 84 | mysql.user(), |
| 85 | mysql.password(), |
| 86 | mysql.root_password(), |
| 87 | ) |
| 88 | elif kafka and mongo: |
| 89 | kafka_units = kafka.kafkas() |
| 90 | kafka_unit = kafka_units[0] |
| 91 | mongo_uri = mongo.connection_string() |
| David Garcia | 82c5ffa | 2020-03-09 08:38:17 +0100 | [diff] [blame] | 92 | |
| David Garcia | 02a5eb9 | 2020-11-28 14:41:22 +0100 | [diff] [blame] | 93 | if ( |
| 94 | mongo_uri |
| 95 | and kafka_unit["host"] |
| 96 | # and kafka_unit["port"] |
| 97 | ): |
| 98 | spec = make_pod_spec_new_ro( |
| 99 | kafka_unit["host"], |
| 100 | # kafka_unit["port"], |
| 101 | mongo_uri, |
| 102 | ) |
| 103 | if spec: |
| David Garcia | d2d52f7 | 2020-11-28 14:38:35 +0100 | [diff] [blame] | 104 | log("set pod spec:\n{}".format(spec)) |
| 105 | pod_spec_set(spec) |
| 106 | layer.status.active("creating container") |
| 107 | set_flag("ro-k8s.configured") |
| David Garcia | 82c5ffa | 2020-03-09 08:38:17 +0100 | [diff] [blame] | 108 | except Exception as e: |
| 109 | layer.status.blocked("k8s spec failed to deploy: {}".format(e)) |
| 110 | |
| 111 | |
| David Garcia | ab11f84 | 2020-12-16 17:25:15 +0100 | [diff] [blame] | 112 | @when("ro-k8s.ready") |
| David Garcia | 68faf8d | 2020-09-01 10:12:16 +0200 | [diff] [blame] | 113 | @when_not("leadership.is_leader") |
| 114 | def non_leaders_active(): |
| 115 | layer.status.active("ready") |
| 116 | |
| 117 | |
| David Garcia | 82c5ffa | 2020-03-09 08:38:17 +0100 | [diff] [blame] | 118 | @when("ro-k8s.configured") |
| 119 | def set_ro_active(): |
| 120 | layer.status.active("ready") |
| 121 | |
| 122 | |
| 123 | @when("ro-k8s.configured", "ro.joined") |
| 124 | def send_config(): |
| 125 | layer.status.maintenance("Sending RO configuration") |
| 126 | try: |
| 127 | ro = endpoint_from_flag("ro.joined") |
| 128 | if ro: |
| 129 | service_ip = get_service_ip("ro") |
| 130 | if service_ip: |
| 131 | ro.send_connection( |
| David Garcia | 02a5eb9 | 2020-11-28 14:41:22 +0100 | [diff] [blame] | 132 | service_ip, |
| 133 | get_ro_port(), |
| David Garcia | 82c5ffa | 2020-03-09 08:38:17 +0100 | [diff] [blame] | 134 | ) |
| 135 | clear_flag("ro.joined") |
| 136 | except Exception as e: |
| 137 | log("Fail sending RO configuration: {}".format(e)) |
| 138 | |
| 139 | |
| David Garcia | d2d52f7 | 2020-11-28 14:38:35 +0100 | [diff] [blame] | 140 | def make_pod_spec( |
| 141 | mysql_host, mysql_port, mysql_user, mysql_password, mysql_root_password |
| 142 | ): |
| David Garcia | 82c5ffa | 2020-03-09 08:38:17 +0100 | [diff] [blame] | 143 | """Make pod specification for Kubernetes |
| 144 | |
| 145 | Args: |
| David Garcia | d2d52f7 | 2020-11-28 14:38:35 +0100 | [diff] [blame] | 146 | mysql_name (str): RO DB name |
| 147 | mysql_host (str): RO DB host |
| 148 | mysql_port (int): RO DB port |
| 149 | mysql_user (str): RO DB user |
| 150 | mysql_password (str): RO DB password |
| David Garcia | 82c5ffa | 2020-03-09 08:38:17 +0100 | [diff] [blame] | 151 | Returns: |
| 152 | pod_spec: Pod specification for Kubernetes |
| 153 | """ |
| 154 | |
| 155 | with open("reactive/spec_template.yaml") as spec_file: |
| 156 | pod_spec_template = spec_file.read() |
| 157 | |
| tierno | a578d71 | 2020-11-25 13:25:08 +0000 | [diff] [blame] | 158 | md = metadata() |
| David Garcia | d2d52f7 | 2020-11-28 14:38:35 +0100 | [diff] [blame] | 159 | cfg = config() |
| David Garcia | 82c5ffa | 2020-03-09 08:38:17 +0100 | [diff] [blame] | 160 | |
| 161 | data = { |
| 162 | "name": md.get("name"), |
| David Garcia | 82c5ffa | 2020-03-09 08:38:17 +0100 | [diff] [blame] | 163 | } |
| 164 | data.update(cfg) |
| David Garcia | 02a5eb9 | 2020-11-28 14:41:22 +0100 | [diff] [blame] | 165 | spec = yaml.safe_load(pod_spec_template % data) |
| 166 | spec["containers"][0]["config"].update( |
| 167 | { |
| 168 | "RO_DB_HOST": mysql_host, |
| 169 | "RO_DB_PORT": mysql_port, |
| 170 | "RO_DB_NAME": cfg.get("ro_database"), |
| 171 | "RO_DB_USER": mysql_user, |
| 172 | "RO_DB_ROOT_PASSWORD": mysql_root_password, |
| 173 | "RO_DB_PASSWORD": mysql_password, |
| 174 | "RO_DB_OVIM_PASSWORD": mysql_password, |
| 175 | "RO_DB_OVIM_HOST": mysql_host, |
| 176 | "RO_DB_OVIM_PORT": mysql_port, |
| 177 | "RO_DB_OVIM_USER": mysql_user, |
| 178 | "RO_DB_OVIM_ROOT_PASSWORD": mysql_root_password, |
| 179 | "RO_DB_OVIM_NAME": cfg.get("vim_database"), |
| 180 | } |
| 181 | ) |
| 182 | return spec |
| David Garcia | 82c5ffa | 2020-03-09 08:38:17 +0100 | [diff] [blame] | 183 | |
| David Garcia | 02a5eb9 | 2020-11-28 14:41:22 +0100 | [diff] [blame] | 184 | |
| 185 | def make_pod_spec_new_ro(kafka_host, mongodb_uri): |
| 186 | """Make pod specification for Kubernetes |
| 187 | |
| 188 | Args: |
| 189 | kafka_host (str): Kafka host |
| 190 | mongodb_uri (str): Mongodb URI |
| 191 | Returns: |
| 192 | pod_spec: Pod specification for Kubernetes |
| 193 | """ |
| 194 | |
| 195 | with open("reactive/spec_template.yaml") as spec_file: |
| 196 | pod_spec_template = spec_file.read() |
| 197 | |
| 198 | md = metadata() |
| 199 | cfg = config() |
| 200 | |
| 201 | data = { |
| 202 | "name": md.get("name"), |
| 203 | } |
| 204 | data.update(cfg) |
| 205 | spec = yaml.safe_load(pod_spec_template % data) |
| 206 | spec["containers"][0]["config"].update( |
| 207 | { |
| 208 | "OSMRO_DATABASE_URI": mongodb_uri, |
| 209 | "OSMRO_MESSAGE_HOST": kafka_host, |
| David Garcia | ae58dab | 2020-11-30 16:29:54 +0100 | [diff] [blame] | 210 | "OSMRO_DATABASE_COMMONKEY": cfg.get("database_commonkey"), |
| David Garcia | 02a5eb9 | 2020-11-28 14:41:22 +0100 | [diff] [blame] | 211 | } |
| 212 | ) |
| 213 | return spec |
| David Garcia | 82c5ffa | 2020-03-09 08:38:17 +0100 | [diff] [blame] | 214 | |
| 215 | |
| 216 | def get_ro_port(): |
| 217 | """Returns RO port""" |
| 218 | cfg = config() |
| 219 | return cfg.get("advertised-port") |