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