Prepare installer and pods for Rel TWELVE
[osm/devops.git] / installers / charm / lcm-k8s / reactive / lcm.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 logging
15 import yaml
16
17 from charmhelpers.core.hookenv import (
18 log,
19 metadata,
20 config,
21 )
22 from charms import layer
23 from charms.layer.caas_base import pod_spec_set
24 from charms.reactive import endpoint_from_flag
25 from charms.reactive import when, when_not, hook
26 from charms.reactive.flags import set_flag, clear_flag
27
28
29 logger = logging.getLogger(__name__)
30
31
32 @hook("upgrade-charm")
33 @when("leadership.is_leader")
34 def upgrade():
35 clear_flag("lcm-k8s.configured")
36
37
38 @when("config.changed")
39 @when("leadership.is_leader")
40 def restart():
41 clear_flag("lcm-k8s.configured")
42
43
44 @when_not("kafka.ready")
45 @when_not("lcm-k8s.configured")
46 @when("leadership.is_leader")
47 def waiting_for_kafka():
48 layer.status.waiting("Waiting for kafka to be ready")
49
50
51 @when_not("mongo.ready")
52 @when_not("lcm-k8s.configured")
53 @when("leadership.is_leader")
54 def waiting_for_mongo():
55 layer.status.waiting("Waiting for mongo to be ready")
56
57
58 @when_not("ro.ready")
59 @when_not("lcm-k8s.configured")
60 @when("leadership.is_leader")
61 def waiting_for_ro():
62 layer.status.waiting("Waiting for ro to be ready")
63
64
65 @when("kafka.ready", "mongo.ready", "ro.ready")
66 @when_not("lcm-k8s.configured")
67 @when("leadership.is_leader")
68 def configure():
69 layer.status.maintenance("Configuring lcm container")
70 try:
71 kafka = endpoint_from_flag("kafka.ready")
72 mongo = endpoint_from_flag("mongo.ready")
73 osm_ro = endpoint_from_flag("ro.ready")
74
75 if kafka and mongo and osm_ro:
76 kafka_units = kafka.kafkas()
77 kafka_unit = kafka_units[0]
78
79 mongo_uri = mongo.connection_string()
80 log("Mongo URI: {}".format(mongo_uri))
81
82 ros = osm_ro.ros()
83 ro_unit = ros[0]
84
85 if (
86 mongo_uri
87 and kafka_unit["host"]
88 and kafka_unit["port"]
89 and ro_unit["host"]
90 and ro_unit["port"]
91 ):
92 spec = make_pod_spec(
93 ro_unit["host"],
94 ro_unit["port"],
95 kafka_unit["host"],
96 kafka_unit["port"],
97 mongo_uri,
98 )
99
100 log("set pod spec:\n{}".format(spec))
101 pod_spec_set(spec)
102 layer.status.active("creating container")
103 set_flag("lcm-k8s.configured")
104 except Exception as e:
105 layer.status.blocked("k8s spec failed to deploy: {}".format(e))
106
107
108 @when("kafka.ready", "mongo.ready", "ro.ready")
109 @when_not("leadership.is_leader")
110 def non_leaders_active():
111 layer.status.active("ready")
112
113
114 @when("lcm-k8s.configured")
115 @when("leadership.is_leader")
116 def set_lcm_active():
117 layer.status.active("ready")
118
119
120 def make_pod_spec(ro_host, ro_port, kafka_host, kafka_port, mongo_uri):
121 """Make pod specification for Kubernetes
122
123 Args:
124 ro_host (str): RO hostname or IP
125 ro_port (str): RO Port
126 kafka_host (str): Kafka hostname or IP
127 kafka_port (int): Kafka port
128 mongo_uri (str): Mongo URI
129 Returns:
130 pod_spec: Pod specification for Kubernetes
131 """
132
133 with open("reactive/spec_template.yaml") as spec_file:
134 pod_spec_template = spec_file.read()
135
136 cfg = config()
137 md = metadata()
138
139 data = {
140 "name": md.get("name"),
141 "ro_host": ro_host,
142 "ro_port": ro_port,
143 "kafka_host": kafka_host,
144 "kafka_port": kafka_port,
145 "mongo_uri": mongo_uri,
146 }
147 data.update(cfg)
148 logs = pod_spec_template % data
149 spec = yaml.safe_load(pod_spec_template % data)
150 if "vca_apiproxy" in cfg and cfg["vca_apiproxy"] != "":
151 spec["containers"][0]["config"]["OSMLCM_VCA_APIPROXY"] = cfg["vca_apiproxy"]
152 return spec