afd7f459186ea4950526329a6efcbb02172efce2
[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 from charms.layer.caas_base import pod_spec_set
15 from charms.reactive import endpoint_from_flag
16 from charms.reactive import when, when_not, hook
17 from charms.reactive.flags import set_flag, clear_flag
18 from charmhelpers.core.hookenv import log, metadata, config
19 from charms import layer
20 from charms.osm.k8s import get_service_ip
21
22
23 @hook("upgrade-charm")
24 @when("leadership.is_leader")
25 def upgrade():
26 clear_flag("ro-k8s.configured")
27
28
29 @when("config.changed")
30 @when("leadership.is_leader")
31 def restart():
32 clear_flag("ro-k8s.configured")
33
34
35 @when_not("mysql.available")
36 @when_not("ro-k8s.configured")
37 def waiting_for_mysql():
38 layer.status.waiting("Waiting for mysql to be ready")
39
40
41 @when("mysql.available")
42 @when_not("ro-k8s.configured")
43 @when("leadership.is_leader")
44 def configure():
45 layer.status.maintenance("Configuring ro container")
46 try:
47 mysql = endpoint_from_flag("mysql.available")
48
49 spec = make_pod_spec(
50 mysql.host(),
51 mysql.port(),
52 mysql.user(),
53 mysql.password(),
54 mysql.root_password(),
55 )
56
57 log("set pod spec:\n{}".format(spec))
58 pod_spec_set(spec)
59 layer.status.active("creating container")
60 set_flag("ro-k8s.configured")
61 except Exception as e:
62 layer.status.blocked("k8s spec failed to deploy: {}".format(e))
63
64
65 @when("ro-k8s.configured")
66 def set_ro_active():
67 layer.status.active("ready")
68
69
70 @when("ro-k8s.configured", "ro.joined")
71 def send_config():
72 layer.status.maintenance("Sending RO configuration")
73 try:
74 ro = endpoint_from_flag("ro.joined")
75 if ro:
76 service_ip = get_service_ip("ro")
77 if service_ip:
78 ro.send_connection(
79 service_ip, get_ro_port(),
80 )
81 clear_flag("ro.joined")
82 except Exception as e:
83 log("Fail sending RO configuration: {}".format(e))
84
85
86 def make_pod_spec(
87 mysql_host, mysql_port, mysql_user, mysql_password, mysql_root_password
88 ):
89 """Make pod specification for Kubernetes
90
91 Args:
92 mysql_name (str): RO DB name
93 mysql_host (str): RO DB host
94 mysql_port (int): RO DB port
95 mysql_user (str): RO DB user
96 mysql_password (str): RO DB password
97 Returns:
98 pod_spec: Pod specification for Kubernetes
99 """
100
101 with open("reactive/spec_template.yaml") as spec_file:
102 pod_spec_template = spec_file.read()
103
104 md = metadata()
105 cfg = config()
106
107 data = {
108 "name": md.get("name"),
109 "docker_image": cfg.get("image"),
110 "mysql_host": mysql_host,
111 "mysql_port": mysql_port,
112 "mysql_user": mysql_user,
113 "mysql_password": mysql_password,
114 "mysql_root_password": mysql_root_password,
115 }
116 data.update(cfg)
117
118 return pod_spec_template % data
119
120
121 def get_ro_port():
122 """Returns RO port"""
123 cfg = config()
124 return cfg.get("advertised-port")