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 PLACharm(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
)
48 def _apply_spec(self
):
49 # Only apply the spec if this unit is a leader.
50 if not self
.framework
.model
.unit
.is_leader():
52 new_spec
= self
.make_pod_spec()
53 if new_spec
== self
.state
.spec
:
55 self
.framework
.model
.pod
.set_spec(new_spec
)
56 self
.state
.spec
= new_spec
58 def make_pod_spec(self
):
59 config
= self
.framework
.model
.config
62 {"name": "port", "containerPort": config
["port"], "protocol": "TCP",},
67 "tcpSocket": {"port": config
["port"]},
70 "initialDelaySeconds": 10,
73 "tcpSocket": {"port": config
["port"]},
75 "initialDelaySeconds": 45,
79 "OSMPLA_MESSAGE_DRIVER": "kafka",
80 "OSMPLA_MESSAGE_HOST": config
["kafka_host"],
81 "OSMPLA_MESSAGE_PORT": config
["kafka_port"],
82 "OSMPLA_DATABASE_DRIVER": "mongo",
83 "OSMPLA_DATABASE_URI": config
["mongo_uri"],
84 "OSMPLA_GLOBAL_LOG_LEVEL": config
["log_level"],
91 "name": self
.framework
.model
.app
.name
,
92 "image": "{}".format(config
["image"]),
94 "kubernetes": kubernetes
,
95 "config": config_spec
,
102 def on_config_changed(self
, event
):
103 """Handle changes in configuration"""
104 unit
= self
.model
.unit
105 unit
.status
= MaintenanceStatus("Applying new pod spec")
107 unit
.status
= ActiveStatus("Ready")
109 def on_start(self
, event
):
110 """Called when the charm is being installed"""
111 unit
= self
.model
.unit
112 unit
.status
= MaintenanceStatus("Applying pod spec")
114 unit
.status
= ActiveStatus("Ready")
116 def on_upgrade_charm(self
, event
):
117 """Upgrade the charm."""
118 unit
= self
.model
.unit
119 unit
.status
= MaintenanceStatus("Upgrading charm")
123 if __name__
== "__main__":