blob: 062cc078f8c6e1c5cfb6b33485a612b88fb21f16 [file] [log] [blame]
David Garcia82c5ffa2020-03-09 08:38:17 +01001# 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 Garcia02a5eb92020-11-28 14:41:22 +010014import yaml
15
16from charmhelpers.core.hookenv import log, metadata, config
17from charms import layer
David Garcia82c5ffa2020-03-09 08:38:17 +010018from charms.layer.caas_base import pod_spec_set
David Garcia02a5eb92020-11-28 14:41:22 +010019from charms.osm.k8s import get_service_ip
David Garcia82c5ffa2020-03-09 08:38:17 +010020from charms.reactive import endpoint_from_flag
21from charms.reactive import when, when_not, hook
22from charms.reactive.flags import set_flag, clear_flag
David Garcia82c5ffa2020-03-09 08:38:17 +010023
24
25@hook("upgrade-charm")
26@when("leadership.is_leader")
27def upgrade():
28 clear_flag("ro-k8s.configured")
David Garcia02a5eb92020-11-28 14:41:22 +010029 clear_flag("ro-k8s.ready")
David Garcia82c5ffa2020-03-09 08:38:17 +010030
31
32@when("config.changed")
33@when("leadership.is_leader")
34def restart():
35 clear_flag("ro-k8s.configured")
David Garcia02a5eb92020-11-28 14:41:22 +010036 clear_flag("ro-k8s.ready")
David Garcia82c5ffa2020-03-09 08:38:17 +010037
David Garciad2d52f72020-11-28 14:38:35 +010038
David Garcia02a5eb92020-11-28 14:41:22 +010039@when_not("mongo.ready")
David Garciad2d52f72020-11-28 14:38:35 +010040@when_not("mysql.available")
David Garcia82c5ffa2020-03-09 08:38:17 +010041@when_not("ro-k8s.configured")
David Garcia68faf8d2020-09-01 10:12:16 +020042@when("leadership.is_leader")
David Garciad2d52f72020-11-28 14:38:35 +010043def waiting_for_mysql():
David Garcia02a5eb92020-11-28 14:41:22 +010044 layer.status.waiting("Waiting for db to be ready")
45 clear_flag("ro-k8s.ready")
David Garcia82c5ffa2020-03-09 08:38:17 +010046
47
David Garcia02a5eb92020-11-28 14:41:22 +010048@when_not("kafka.ready")
49@when_not("ro-k8s.configured")
50@when("leadership.is_leader")
51def 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 Garciad2d52f72020-11-28 14:38:35 +010057@when("mysql.available")
David Garcia02a5eb92020-11-28 14:41:22 +010058def 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")
65def ro_k8s_kafka_mongo_ready():
66 set_flag("ro-k8s.ready")
67
68
69@when("ro-k8s.ready")
David Garcia82c5ffa2020-03-09 08:38:17 +010070@when_not("ro-k8s.configured")
71@when("leadership.is_leader")
72def configure():
73 layer.status.maintenance("Configuring ro container")
74 try:
David Garciad2d52f72020-11-28 14:38:35 +010075 mysql = endpoint_from_flag("mysql.available")
David Garcia02a5eb92020-11-28 14:41:22 +010076 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 Garcia82c5ffa2020-03-09 08:38:17 +010092
David Garcia02a5eb92020-11-28 14:41:22 +010093 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 Garciad2d52f72020-11-28 14:38:35 +0100104 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 Garcia82c5ffa2020-03-09 08:38:17 +0100108 except Exception as e:
109 layer.status.blocked("k8s spec failed to deploy: {}".format(e))
110
111
David Garciaab11f842020-12-16 17:25:15 +0100112@when("ro-k8s.ready")
David Garcia68faf8d2020-09-01 10:12:16 +0200113@when_not("leadership.is_leader")
114def non_leaders_active():
115 layer.status.active("ready")
116
117
David Garcia82c5ffa2020-03-09 08:38:17 +0100118@when("ro-k8s.configured")
119def set_ro_active():
120 layer.status.active("ready")
121
122
123@when("ro-k8s.configured", "ro.joined")
124def 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 Garcia02a5eb92020-11-28 14:41:22 +0100132 service_ip,
133 get_ro_port(),
David Garcia82c5ffa2020-03-09 08:38:17 +0100134 )
135 clear_flag("ro.joined")
136 except Exception as e:
137 log("Fail sending RO configuration: {}".format(e))
138
139
David Garciad2d52f72020-11-28 14:38:35 +0100140def make_pod_spec(
141 mysql_host, mysql_port, mysql_user, mysql_password, mysql_root_password
142):
David Garcia82c5ffa2020-03-09 08:38:17 +0100143 """Make pod specification for Kubernetes
144
145 Args:
David Garciad2d52f72020-11-28 14:38:35 +0100146 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 Garcia82c5ffa2020-03-09 08:38:17 +0100151 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
tiernoa578d712020-11-25 13:25:08 +0000158 md = metadata()
David Garciad2d52f72020-11-28 14:38:35 +0100159 cfg = config()
David Garcia82c5ffa2020-03-09 08:38:17 +0100160
161 data = {
162 "name": md.get("name"),
David Garcia82c5ffa2020-03-09 08:38:17 +0100163 }
164 data.update(cfg)
David Garcia02a5eb92020-11-28 14:41:22 +0100165 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 Garcia82c5ffa2020-03-09 08:38:17 +0100183
David Garcia02a5eb92020-11-28 14:41:22 +0100184
185def 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 Garciaae58dab2020-11-30 16:29:54 +0100210 "OSMRO_DATABASE_COMMONKEY": cfg.get("database_commonkey"),
David Garcia02a5eb92020-11-28 14:41:22 +0100211 }
212 )
213 return spec
David Garcia82c5ffa2020-03-09 08:38:17 +0100214
215
216def get_ro_port():
217 """Returns RO port"""
218 cfg = config()
219 return cfg.get("advertised-port")