Add image_username and image_password to charms
[osm/devops.git] / installers / charm / ui-k8s / reactive / ui.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 #
15 from charms.layer.caas_base import pod_spec_set
16 from charms.reactive import endpoint_from_flag
17 from charms.reactive import when, when_not, hook
18 from charms.reactive.flags import set_flag, clear_flag
19 from charmhelpers.core.hookenv import (
20 log,
21 metadata,
22 config,
23 )
24 from charms import layer
25
26
27 @hook("upgrade-charm")
28 @when("leadership.is_leader")
29 def upgrade():
30 clear_flag("ui-k8s.configured")
31
32
33 @when("config.changed")
34 @when("leadership.is_leader")
35 def restart():
36 clear_flag("ui-k8s.configured")
37
38
39 @when_not("mysql.available")
40 @when_not("ui-k8s.configured")
41 @when("leadership.is_leader")
42 def waiting_for_mysql():
43 layer.status.waiting("Waiting for mysql to be available")
44
45
46 @when_not("nbi.ready")
47 @when_not("ui-k8s.configured")
48 @when("leadership.is_leader")
49 def waiting_for_nbi():
50 layer.status.waiting("Waiting for nbi to be available")
51
52
53 @when("mysql.available", "nbi.ready")
54 @when_not("ui-k8s.configured")
55 @when("leadership.is_leader")
56 def configure():
57
58 layer.status.maintenance("Configuring ui container")
59 try:
60 mysql = endpoint_from_flag("mysql.available")
61 nbi = endpoint_from_flag("nbi.ready")
62 nbi_unit = nbi.nbis()[0]
63 nbi_host = "{}".format(nbi_unit["host"])
64 spec = make_pod_spec(
65 mysql.host(),
66 mysql.port(),
67 mysql.user(),
68 mysql.password(),
69 mysql.root_password(),
70 nbi_host,
71 )
72 log("set pod spec:\n{}".format(spec))
73 pod_spec_set(spec)
74 set_flag("ui-k8s.configured")
75 except Exception as e:
76 layer.status.blocked("k8s spec failed to deploy: {}".format(e))
77
78
79 @when("mysql.available", "nbi.ready")
80 @when_not("leadership.is_leader")
81 def non_leaders_active():
82 layer.status.active("ready")
83
84
85 @when("ui-k8s.configured")
86 def set_ui_active():
87 layer.status.active("ready")
88
89
90 def make_pod_spec(
91 mysql_host, mysql_port, mysql_user, mysql_password, mysql_root_password, nbi_host
92 ):
93 """Make pod specification for Kubernetes
94
95 Args:
96 mysql_name (str): UI DB name
97 mysql_host (str): UI DB host
98 mysql_port (int): UI DB port
99 mysql_user (str): UI DB user
100 mysql_password (str): UI DB password
101 nbi_uri (str): NBI URI
102 Returns:
103 pod_spec: Pod specification for Kubernetes
104 """
105
106 with open("reactive/spec_template.yaml") as spec_file:
107 pod_spec_template = spec_file.read()
108
109 md = metadata()
110 cfg = config()
111
112 data = {
113 "name": md.get("name"),
114 "mysql_host": mysql_host,
115 "mysql_port": mysql_port,
116 "mysql_user": mysql_user,
117 "mysql_password": mysql_password,
118 "mysql_root_password": mysql_root_password,
119 "nbi_host": nbi_host,
120 }
121 data.update(cfg)
122
123 return pod_spec_template % data
124
125
126 def get_ui_port():
127 """Returns UI port"""
128 cfg = config()
129 return cfg.get("advertised-port")