8ad6f98c8dd346c81313d6a1d3ee922784777190
[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 @when("leadership.is_leader")
38 def waiting_for_mysql():
39 layer.status.waiting("Waiting for mysql to be ready")
40
41
42 @when("mysql.available")
43 @when_not("ro-k8s.configured")
44 @when("leadership.is_leader")
45 def configure():
46 layer.status.maintenance("Configuring ro container")
47 try:
48 mysql = endpoint_from_flag("mysql.available")
49
50 if mysql.host() is not None:
51 spec = make_pod_spec(
52 mysql.host(),
53 mysql.port(),
54 mysql.user(),
55 mysql.password(),
56 mysql.root_password(),
57 )
58
59 log("set pod spec:\n{}".format(spec))
60 pod_spec_set(spec)
61 layer.status.active("creating container")
62 set_flag("ro-k8s.configured")
63 except Exception as e:
64 layer.status.blocked("k8s spec failed to deploy: {}".format(e))
65
66
67 @when("mysql.available")
68 @when_not("leadership.is_leader")
69 def non_leaders_active():
70 layer.status.active("ready")
71
72
73 @when("ro-k8s.configured")
74 def set_ro_active():
75 layer.status.active("ready")
76
77
78 @when("ro-k8s.configured", "ro.joined")
79 def send_config():
80 layer.status.maintenance("Sending RO configuration")
81 try:
82 ro = endpoint_from_flag("ro.joined")
83 if ro:
84 service_ip = get_service_ip("ro")
85 if service_ip:
86 ro.send_connection(
87 service_ip, get_ro_port(),
88 )
89 clear_flag("ro.joined")
90 except Exception as e:
91 log("Fail sending RO configuration: {}".format(e))
92
93
94 def make_pod_spec(
95 mysql_host, mysql_port, mysql_user, mysql_password, mysql_root_password
96 ):
97 """Make pod specification for Kubernetes
98
99 Args:
100 mysql_name (str): RO DB name
101 mysql_host (str): RO DB host
102 mysql_port (int): RO DB port
103 mysql_user (str): RO DB user
104 mysql_password (str): RO DB password
105 Returns:
106 pod_spec: Pod specification for Kubernetes
107 """
108
109 with open("reactive/spec_template.yaml") as spec_file:
110 pod_spec_template = spec_file.read()
111
112 md = metadata()
113 cfg = config()
114
115 data = {
116 "name": md.get("name"),
117 "docker_image": cfg.get("image"),
118 "mysql_host": mysql_host,
119 "mysql_port": mysql_port,
120 "mysql_user": mysql_user,
121 "mysql_password": mysql_password,
122 "mysql_root_password": mysql_root_password,
123 }
124 data.update(cfg)
125
126 return pod_spec_template % data
127
128
129 def get_ro_port():
130 """Returns RO port"""
131 cfg = config()
132 return cfg.get("advertised-port")