Prepare installer and pods for Rel TWELVE
[osm/devops.git] / installers / charm / ro-k8s / reactive / ro.py
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 import yaml
15
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
23
24
25 @hook("upgrade-charm")
26 @when("leadership.is_leader")
27 def upgrade():
28 clear_flag("ro-k8s.configured")
29 clear_flag("ro-k8s.ready")
30
31
32 @when("config.changed")
33 @when("leadership.is_leader")
34 def restart():
35 clear_flag("ro-k8s.configured")
36 clear_flag("ro-k8s.ready")
37
38
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")
46
47
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")
57 @when("mysql.available")
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")
70 @when_not("ro-k8s.configured")
71 @when("leadership.is_leader")
72 def configure():
73 layer.status.maintenance("Configuring ro container")
74 try:
75 mysql = endpoint_from_flag("mysql.available")
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()
92
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:
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")
108 except Exception as e:
109 layer.status.blocked("k8s spec failed to deploy: {}".format(e))
110
111
112 @when("ro-k8s.ready")
113 @when_not("leadership.is_leader")
114 def non_leaders_active():
115 layer.status.active("ready")
116
117
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(
132 service_ip,
133 get_ro_port(),
134 )
135 clear_flag("ro.joined")
136 except Exception as e:
137 log("Fail sending RO configuration: {}".format(e))
138
139
140 def make_pod_spec(
141 mysql_host, mysql_port, mysql_user, mysql_password, mysql_root_password
142 ):
143 """Make pod specification for Kubernetes
144
145 Args:
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
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
158 md = metadata()
159 cfg = config()
160
161 data = {
162 "name": md.get("name"),
163 }
164 data.update(cfg)
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
183
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,
210 "OSMRO_DATABASE_COMMONKEY": cfg.get("database_commonkey"),
211 }
212 )
213 return spec
214
215
216 def get_ro_port():
217 """Returns RO port"""
218 cfg = config()
219 return cfg.get("advertised-port")