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