1 # Copyright 2020 Canonical Ltd.
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
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 (
24 from charms
import layer
27 @hook("upgrade-charm")
28 @when("leadership.is_leader")
30 clear_flag("ui-k8s.configured")
33 @when("config.changed")
34 @when("leadership.is_leader")
36 clear_flag("ui-k8s.configured")
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")
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")
53 @when("mysql.available", "nbi.ready")
54 @when_not("ui-k8s.configured")
55 @when("leadership.is_leader")
58 layer
.status
.maintenance("Configuring ui container")
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"])
69 mysql
.root_password(),
72 log("set pod spec:\n{}".format(spec
))
74 set_flag("ui-k8s.configured")
75 except Exception as e
:
76 layer
.status
.blocked("k8s spec failed to deploy: {}".format(e
))
79 @when("mysql.available", "nbi.ready")
80 @when_not("leadership.is_leader")
81 def non_leaders_active():
82 layer
.status
.active("ready")
85 @when("ui-k8s.configured")
87 layer
.status
.active("ready")
91 mysql_host
, mysql_port
, mysql_user
, mysql_password
, mysql_root_password
, nbi_host
93 """Make pod specification for Kubernetes
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
103 pod_spec: Pod specification for Kubernetes
106 with
open("reactive/spec_template.yaml") as spec_file
:
107 pod_spec_template
= spec_file
.read()
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
,
123 return pod_spec_template
% data
127 """Returns UI port"""
129 return cfg
.get("advertised-port")