1 # Copyright 2020 Canonical Ltd.
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
16 from charmhelpers
.core
.hookenv
import log
, metadata
, config
17 from charms
import layer
18 from charms
.layer
.caas_base
import pod_spec_set
19 from charms
.osm
.k8s
import get_service_ip
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
25 @hook("upgrade-charm")
26 @when("leadership.is_leader")
28 clear_flag("ro-k8s.configured")
29 clear_flag("ro-k8s.ready")
32 @when("config.changed")
33 @when("leadership.is_leader")
35 clear_flag("ro-k8s.configured")
36 clear_flag("ro-k8s.ready")
39 @when_not("mongo.ready")
40 @when_not("mysql.available")
41 @when_not("ro-k8s.configured")
42 @when("leadership.is_leader")
43 def waiting_for_mysql():
44 layer
.status
.waiting("Waiting for db to be ready")
45 clear_flag("ro-k8s.ready")
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")
56 @when_not("ro-k8s.ready")
57 @when("mysql.available")
58 def ro_k8s_mysql_ready():
59 set_flag("ro-k8s.ready")
62 @when_not("ro-k8s.ready")
65 def ro_k8s_kafka_mongo_ready():
66 set_flag("ro-k8s.ready")
70 @when_not("ro-k8s.configured")
71 @when("leadership.is_leader")
73 layer
.status
.maintenance("Configuring ro container")
75 mysql
= endpoint_from_flag("mysql.available")
76 kafka
= endpoint_from_flag("kafka.ready")
77 mongo
= endpoint_from_flag("mongo.ready")
80 if mysql
.host() is not None:
86 mysql
.root_password(),
89 kafka_units
= kafka
.kafkas()
90 kafka_unit
= kafka_units
[0]
91 mongo_uri
= mongo
.connection_string()
95 and kafka_unit
["host"]
96 # and kafka_unit["port"]
98 spec
= make_pod_spec_new_ro(
100 # kafka_unit["port"],
104 log("set pod spec:\n{}".format(spec
))
106 layer
.status
.active("creating container")
107 set_flag("ro-k8s.configured")
108 except Exception as e
:
109 layer
.status
.blocked("k8s spec failed to deploy: {}".format(e
))
112 @when("ro-k8s.configured")
113 @when_not("leadership.is_leader")
114 def non_leaders_active():
115 layer
.status
.active("ready")
118 @when("ro-k8s.configured")
120 layer
.status
.active("ready")
123 @when("ro-k8s.configured", "ro.joined")
125 layer
.status
.maintenance("Sending RO configuration")
127 ro
= endpoint_from_flag("ro.joined")
129 service_ip
= get_service_ip("ro")
135 clear_flag("ro.joined")
136 except Exception as e
:
137 log("Fail sending RO configuration: {}".format(e
))
141 mysql_host
, mysql_port
, mysql_user
, mysql_password
, mysql_root_password
143 """Make pod specification for Kubernetes
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
152 pod_spec: Pod specification for Kubernetes
155 with
open("reactive/spec_template.yaml") as spec_file
:
156 pod_spec_template
= spec_file
.read()
162 "name": md
.get("name"),
165 spec
= yaml
.safe_load(pod_spec_template
% data
)
166 spec
["containers"][0]["config"].update(
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"),
185 def make_pod_spec_new_ro(kafka_host
, mongodb_uri
):
186 """Make pod specification for Kubernetes
189 kafka_host (str): Kafka host
190 mongodb_uri (str): Mongodb URI
192 pod_spec: Pod specification for Kubernetes
195 with
open("reactive/spec_template.yaml") as spec_file
:
196 pod_spec_template
= spec_file
.read()
202 "name": md
.get("name"),
205 spec
= yaml
.safe_load(pod_spec_template
% data
)
206 spec
["containers"][0]["config"].update(
208 "OSMRO_DATABASE_URI": mongodb_uri
,
209 "OSMRO_MESSAGE_HOST": kafka_host
,
210 "OSMRO_DATABASE_COMMONKEY": cfg
.get("database_commonkey"),
217 """Returns RO port"""
219 return cfg
.get("advertised-port")