Prepare installer and pods for Rel TWELVE
[osm/devops.git] / installers / charm / mongodb-k8s / reactive / mongo.py
1 # Copyright 2021 Canonical Ltd.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License"); you may
4 # not use this file except in compliance with the License. You may obtain
5 # 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, WITHOUT
11 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 # License for the specific language governing permissions and limitations
13 # under the License.
14 #
15 # For those usages not covered by the Apache License, Version 2.0 please
16 # contact: legal@canonical.com
17 #
18 # To get in touch with the maintainers, please contact:
19 # osm-charmers@lists.launchpad.net
20 ##
21
22 from charms.layer.caas_base import pod_spec_set
23 from charms.reactive import endpoint_from_flag
24 from charms.reactive import when, when_not, hook
25 from charms.reactive.flags import set_flag, clear_flag
26 from charmhelpers.core.hookenv import log, metadata, config, goal_state
27 from charms import layer
28
29
30 @hook("upgrade-charm")
31 @when("leadership.is_leader")
32 def upgrade():
33 clear_flag("mongodb-k8s.configured")
34
35
36 @when("config.changed")
37 @when("leadership.is_leader")
38 def restart():
39 clear_flag("mongodb-k8s.configured")
40
41
42 @when_not("mongodb-k8s.configured")
43 @when("leadership.is_leader")
44 def configure():
45 layer.status.maintenance("Configuring MongoDB container")
46 try:
47 spec = make_pod_spec()
48 log("set pod spec:\n{}".format(spec))
49 pod_spec_set(spec)
50 set_flag("mongodb-k8s.configured")
51 except Exception as e:
52 layer.status.blocked("k8s spec failed to deploy: {}".format(e))
53
54
55 @when("mongodb-k8s.configured")
56 def set_mongodb_active():
57 layer.status.active("ready")
58
59
60 @when_not("leadership.is_leader")
61 def non_leaders_active():
62 layer.status.active("ready")
63
64
65 @when("mongodb-k8s.configured", "mongo.joined")
66 def send_config():
67 layer.status.maintenance("Sending mongo configuration")
68 try:
69 mongo = endpoint_from_flag("mongo.joined")
70 if mongo:
71 cfg = config()
72 mongo_uri = "mongodb://"
73 for i, unit in enumerate(goal_state()["units"]):
74 pod_base_name = unit.split("/")[0]
75 service_name = cfg.get("service-name")
76 pod_name = "{}-{}".format(pod_base_name, i)
77 if i:
78 mongo_uri += ","
79 mongo_uri += "{}.{}:{}".format(
80 pod_name, service_name, get_mongodb_port()
81 )
82 if cfg.get("enable-sidecar"):
83 mongo_uri += "/?replicaSet={}".format(get_mongodb_replset())
84 log("Mongo URI: {}".format(mongo_uri))
85 mongo.send_connection_string(mongo_uri)
86 clear_flag("mongo.joined")
87 except Exception as e:
88 log("Exception sending config: {}".format(e))
89 clear_flag("mongo.joined")
90
91
92 def make_pod_spec():
93 """Make pod specification for Kubernetes
94
95 Returns:
96 pod_spec: Pod specification for Kubernetes
97 """
98 md = metadata()
99 cfg = config()
100
101 if cfg.get("enable-sidecar"):
102 with open("reactive/spec_template_ha.yaml") as spec_file:
103 pod_spec_template = spec_file.read()
104 else:
105 with open("reactive/spec_template.yaml") as spec_file:
106 pod_spec_template = spec_file.read()
107
108 data = {
109 "name": md.get("name"),
110 "docker_image": cfg.get("mongodb-image"),
111 "sc_docker_image": cfg.get("sidecar-image"),
112 "pod_labels": "juju-app={}".format(cfg.get("advertised-hostname")),
113 }
114
115 data.update(cfg)
116 return pod_spec_template % data
117
118
119 def get_mongodb_port():
120 """Returns MongoDB port"""
121 cfg = config()
122 return cfg.get("advertised-port")
123
124
125 def get_mongodb_replset():
126 """Returns MongoDB port"""
127 cfg = config()
128 return cfg.get("replica-set")