change charm installation for the New generation RO
[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 @when_not("kafka.ready")
35 @when_not("ro-k8s.configured")
36 @when("leadership.is_leader")
37 def waiting_for_kafka():
38 layer.status.waiting("Waiting for kafka to be ready")
39
40
41 @when_not("mongo.ready")
42 @when_not("ro-k8s.configured")
43 @when("leadership.is_leader")
44 def waiting_for_mongo():
45 layer.status.waiting("Waiting for mongo to be ready")
46
47
48 @when("kafka.ready", "mongo.ready")
49 @when_not("ro-k8s.configured")
50 @when("leadership.is_leader")
51 def configure():
52 layer.status.maintenance("Configuring ro container")
53 try:
54 kafka = endpoint_from_flag("kafka.ready")
55 mongo = endpoint_from_flag("mongo.ready")
56
57 if kafka and mongo:
58 kafka_units = kafka.kafkas()
59 kafka_unit = kafka_units[0]
60
61 mongo_uri = mongo.connection_string()
62 log("Mongo URI: {}".format(mongo_uri))
63
64 if (
65 mongo_uri
66 and kafka_unit["host"]
67 and kafka_unit["port"]
68 and ro_unit["host"]
69 and ro_unit["port"]
70 ):
71 spec = make_pod_spec(
72 kafka_unit["host"],
73 kafka_unit["port"],
74 mongo_uri,
75 )
76
77 log("set pod spec:\n{}".format(spec))
78 pod_spec_set(spec)
79 layer.status.active("creating container")
80 set_flag("ro-k8s.configured")
81 except Exception as e:
82 layer.status.blocked("k8s spec failed to deploy: {}".format(e))
83
84
85 @when("kafka.ready", "mongo.ready")
86 @when_not("leadership.is_leader")
87 def non_leaders_active():
88 layer.status.active("ready")
89
90
91 @when("ro-k8s.configured")
92 def set_ro_active():
93 layer.status.active("ready")
94
95
96 @when("ro-k8s.configured", "ro.joined")
97 def send_config():
98 layer.status.maintenance("Sending RO configuration")
99 try:
100 ro = endpoint_from_flag("ro.joined")
101 if ro:
102 service_ip = get_service_ip("ro")
103 if service_ip:
104 ro.send_connection(
105 service_ip, get_ro_port(),
106 )
107 clear_flag("ro.joined")
108 except Exception as e:
109 log("Fail sending RO configuration: {}".format(e))
110
111
112 def make_pod_spec(kafka_host, kafka_port, mongo_uri):
113 """Make pod specification for Kubernetes
114
115 Args:
116 kafka_host (str): Kafka hostname or IP
117 kafka_port (int): Kafka port
118 mongo_uri (str): Mongo URI
119 Returns:
120 pod_spec: Pod specification for Kubernetes
121 """
122
123 with open("reactive/spec_template.yaml") as spec_file:
124 pod_spec_template = spec_file.read()
125
126 cfg = config()
127 md = metadata()
128
129 data = {
130 "name": md.get("name"),
131 "kafka_host": kafka_host,
132 "kafka_port": kafka_port,
133 "mongo_uri": mongo_uri,
134 }
135 data.update(cfg)
136
137 return pod_spec_template % data
138
139
140 def get_ro_port():
141 """Returns RO port"""
142 cfg = config()
143 return cfg.get("advertised-port")