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 def waiting_for_mysql():
42 layer
.status
.waiting("Waiting for mysql to be available")
45 @when_not("nbi.ready")
46 @when_not("ui-k8s.configured")
47 def waiting_for_nbi():
48 layer
.status
.waiting("Waiting for nbi to be available")
51 @when("mysql.available", "nbi.ready")
52 @when_not("ui-k8s.configured")
53 @when("leadership.is_leader")
56 layer
.status
.maintenance("Configuring ui container")
58 mysql
= endpoint_from_flag("mysql.available")
59 nbi
= endpoint_from_flag("nbi.ready")
60 nbi_unit
= nbi
.nbis()[0]
61 nbi_host
= "{}".format(nbi_unit
["host"])
67 mysql
.root_password(),
70 log("set pod spec:\n{}".format(spec
))
72 set_flag("ui-k8s.configured")
73 except Exception as e
:
74 layer
.status
.blocked("k8s spec failed to deploy: {}".format(e
))
77 @when("ui-k8s.configured")
79 layer
.status
.active("ready")
83 mysql_host
, mysql_port
, mysql_user
, mysql_password
, mysql_root_password
, nbi_host
85 """Make pod specification for Kubernetes
88 mysql_name (str): UI DB name
89 mysql_host (str): UI DB host
90 mysql_port (int): UI DB port
91 mysql_user (str): UI DB user
92 mysql_password (str): UI DB password
93 nbi_uri (str): NBI URI
95 pod_spec: Pod specification for Kubernetes
98 with
open("reactive/spec_template.yaml") as spec_file
:
99 pod_spec_template
= spec_file
.read()
105 "name": md
.get("name"),
106 "docker_image": cfg
.get("image"),
107 "mysql_host": mysql_host
,
108 "mysql_port": mysql_port
,
109 "mysql_user": mysql_user
,
110 "mysql_password": mysql_password
,
111 "mysql_root_password": mysql_root_password
,
112 "nbi_host": nbi_host
,
116 return pod_spec_template
% data
120 """Returns UI port"""
122 return cfg
.get("advertised-port")