Remove old mongo K8s manifest file and references in the installer
[osm/devops.git] / installers / charm / mariadb-k8s / reactive / mariadb.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 when, when_not, hook
24 from charms.reactive import endpoint_from_flag
25 from charms.reactive.flags import set_flag, get_state, clear_flag
26
27 from charmhelpers.core.hookenv import (
28 log,
29 metadata,
30 config,
31 application_name,
32 )
33 from charms import layer
34 from charms.osm.k8s import is_pod_up, get_service_ip
35
36
37 @hook("upgrade-charm")
38 @when("leadership.is_leader")
39 def upgrade():
40 clear_flag("mariadb-k8s.configured")
41
42
43 @when("config.changed")
44 @when("leadership.is_leader")
45 def restart():
46 clear_flag("mariadb-k8s.configured")
47
48
49 @when_not("mariadb-k8s.configured")
50 @when("leadership.is_leader")
51 def configure():
52 layer.status.maintenance("Configuring mariadb-k8s container")
53
54 spec = make_pod_spec()
55 log("set pod spec:\n{}".format(spec))
56 pod_spec_set(spec)
57
58 set_flag("mariadb-k8s.configured")
59
60
61 @when("mariadb-k8s.configured")
62 def set_mariadb_active():
63 layer.status.active("ready")
64
65
66 @when_not("leadership.is_leader")
67 def non_leaders_active():
68 layer.status.active("ready")
69
70
71 @when("mariadb-k8s.configured", "mysql.database.requested")
72 def provide_database():
73 mysql = endpoint_from_flag("mysql.database.requested")
74
75 if not is_pod_up("mysql"):
76 log("The pod is not ready.")
77 return
78
79 for request, application in mysql.database_requests().items():
80 try:
81
82 log("request -> {0} for app -> {1}".format(request, application))
83 user = get_state("user")
84 password = get_state("password")
85 database_name = get_state("database")
86 root_password = get_state("root_password")
87
88 log("db params: {0}:{1}@{2}".format(user, password, database_name))
89
90 service_ip = get_service_ip("mysql")
91 if service_ip:
92 mysql.provide_database(
93 request_id=request,
94 host=service_ip,
95 port=3306,
96 database_name=database_name,
97 user=user,
98 password=password,
99 root_password=root_password,
100 )
101 mysql.mark_complete()
102 except Exception as e:
103 log("Exception while providing database: {}".format(e))
104
105
106 def make_pod_spec():
107 """Make pod specification for Kubernetes
108
109 Returns:
110 pod_spec: Pod specification for Kubernetes
111 """
112 if config().get("ha-mode"):
113 with open("reactive/spec_template_ha.yaml") as spec_file:
114 pod_spec_template = spec_file.read()
115 image = config().get("ha-image")
116 else:
117 with open("reactive/spec_template.yaml") as spec_file:
118 pod_spec_template = spec_file.read()
119 image = config().get("image")
120
121 md = metadata()
122 cfg = config()
123
124 user = cfg.get("user")
125 password = cfg.get("password")
126 database = cfg.get("database")
127 root_password = cfg.get("root_password")
128 app_name = application_name()
129
130 set_flag("user", user)
131 set_flag("password", password)
132 set_flag("database", database)
133 set_flag("root_password", root_password)
134
135 data = {
136 "name": md.get("name"),
137 "docker_image": image,
138 "application_name": app_name,
139 }
140 data.update(cfg)
141 return pod_spec_template % data