2 # Copyright 2020 Canonical Ltd.
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
19 sys
.path
.append("lib")
21 from ops
.charm
import CharmBase
22 from ops
.framework
import StoredState
, Object
23 from ops
.main
import main
24 from ops
.model
import (
30 from pathlib
import Path
31 from string
import Template
33 logger
= logging
.getLogger(__name__
)
36 class NGUICharm(CharmBase
):
39 def __init__(self
, framework
, key
):
40 super().__init
__(framework
, key
)
41 self
.state
.set_default(spec
=None)
43 # Observe Charm related events
44 self
.framework
.observe(self
.on
.config_changed
, self
.on_config_changed
)
45 self
.framework
.observe(self
.on
.start
, self
.on_start
)
46 self
.framework
.observe(self
.on
.upgrade_charm
, self
.on_upgrade_charm
)
47 # self.framework.observe(
48 # self.on.nbi_relation_joined, self.on_nbi_relation_joined
51 def _apply_spec(self
):
52 # Only apply the spec if this unit is a leader.
53 if not self
.framework
.model
.unit
.is_leader():
55 new_spec
= self
.make_pod_spec()
56 if new_spec
== self
.state
.spec
:
58 self
.framework
.model
.pod
.set_spec(new_spec
)
59 self
.state
.spec
= new_spec
61 def make_pod_spec(self
):
62 config
= self
.framework
.model
.config
65 {"name": "port", "containerPort": config
["port"], "protocol": "TCP",},
70 "tcpSocket": {"port": config
["port"]},
73 "initialDelaySeconds": 10,
76 "tcpSocket": {"port": config
["port"]},
78 "initialDelaySeconds": 45,
82 "port": config
["port"],
83 "server_name": config
["server_name"],
84 "client_max_body_size": config
["client_max_body_size"],
85 "nbi_hostname": config
["nbi_hostname"],
86 "nbi_port": config
["nbi_port"],
91 "name": "configuration",
92 "mountPath": "/etc/nginx/sites-available/",
95 .name
: Template(Path(filename
).read_text())
96 .substitute(config_spec
)
97 for filename
in glob("files/*")
106 "name": self
.framework
.model
.app
.name
,
107 "image": "{}".format(config
["image"]),
109 "kubernetes": kubernetes
,
117 def on_config_changed(self
, event
):
118 """Handle changes in configuration"""
119 unit
= self
.model
.unit
120 unit
.status
= MaintenanceStatus("Applying new pod spec")
122 unit
.status
= ActiveStatus("Ready")
124 def on_start(self
, event
):
125 """Called when the charm is being installed"""
126 unit
= self
.model
.unit
127 unit
.status
= MaintenanceStatus("Applying pod spec")
129 unit
.status
= ActiveStatus("Ready")
131 def on_upgrade_charm(self
, event
):
132 """Upgrade the charm."""
133 unit
= self
.model
.unit
134 unit
.status
= MaintenanceStatus("Upgrading charm")
137 # def on_nbi_relation_joined(self, event):
138 # unit = self.model.unit
139 # if not unit.is_leader():
141 # config = self.framework.model.config
142 # unit = MaintenanceStatus("Sending connection data")
144 # unit = ActiveStatus("Ready")
147 if __name__
== "__main__":