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.
18 from ops
.charm
import CharmBase
19 from ops
.framework
import StoredState
20 from ops
.main
import main
21 from ops
.model
import (
26 from typing
import NoReturn
28 logger
= logging
.getLogger(__name__
)
31 class PLACharm(CharmBase
):
34 def __init__(self
, *args
) -> NoReturn
:
35 super().__init
__(*args
)
36 self
.state
.set_default(spec
=None)
37 self
.state
.set_default(kafka_host
=None)
38 self
.state
.set_default(kafka_port
=None)
39 self
.state
.set_default(mongodb_uri
=None)
41 # Observe Charm related events
42 self
.framework
.observe(self
.on
.config_changed
, self
.on_config_changed
)
43 self
.framework
.observe(self
.on
.start
, self
.on_start
)
44 self
.framework
.observe(self
.on
.upgrade_charm
, self
.on_upgrade_charm
)
47 self
.framework
.observe(
48 self
.on
.kafka_relation_changed
, self
.on_kafka_relation_changed
50 self
.framework
.observe(
51 self
.on
.mongo_relation_changed
, self
.on_mongo_relation_changed
54 def _apply_spec(self
):
55 # Only apply the spec if this unit is a leader.
56 unit
= self
.model
.unit
57 if not unit
.is_leader():
58 unit
.status
= ActiveStatus("ready")
60 if not self
.state
.kafka_host
or not self
.state
.kafka_port
:
61 unit
.status
= WaitingStatus("Waiting for Kafka")
63 if not self
.state
.mongodb_uri
:
64 unit
.status
= WaitingStatus("Waiting for MongoDB")
67 unit
.status
= MaintenanceStatus("Applying new pod spec")
69 new_spec
= self
.make_pod_spec()
70 if new_spec
== self
.state
.spec
:
71 unit
.status
= ActiveStatus("ready")
73 self
.framework
.model
.pod
.set_spec(new_spec
)
74 self
.state
.spec
= new_spec
75 unit
.status
= ActiveStatus("ready")
77 def make_pod_spec(self
):
78 config
= self
.framework
.model
.config
83 "containerPort": config
["port"],
89 "OSMPLA_MESSAGE_DRIVER": "kafka",
90 "OSMPLA_MESSAGE_HOST": self
.state
.kafka_host
,
91 "OSMPLA_MESSAGE_PORT": self
.state
.kafka_port
,
92 "OSMPLA_DATABASE_DRIVER": "mongo",
93 "OSMPLA_DATABASE_URI": self
.state
.mongodb_uri
,
94 "OSMPLA_GLOBAL_LOG_LEVEL": config
["log_level"],
95 "OSMPLA_DATABASE_COMMONKEY": config
["database_common_key"],
102 "name": self
.framework
.model
.app
.name
,
104 "imagePath": config
["image"],
105 "username": config
["image_username"],
106 "password": config
["image_password"],
109 "config": config_spec
,
116 def on_config_changed(self
, event
):
117 """Handle changes in configuration"""
120 def on_start(self
, event
):
121 """Called when the charm is being installed"""
124 def on_upgrade_charm(self
, event
):
125 """Upgrade the charm."""
126 unit
= self
.model
.unit
127 unit
.status
= MaintenanceStatus("Upgrading charm")
130 def on_kafka_relation_changed(self
, event
):
131 kafka_host
= event
.relation
.data
[event
.unit
].get("host")
132 kafka_port
= event
.relation
.data
[event
.unit
].get("port")
133 if kafka_host
and self
.state
.kafka_host
!= kafka_host
:
134 self
.state
.kafka_host
= kafka_host
135 if kafka_port
and self
.state
.kafka_port
!= kafka_port
:
136 self
.state
.kafka_port
= kafka_port
139 def on_mongo_relation_changed(self
, event
):
140 mongodb_uri
= event
.relation
.data
[event
.unit
].get("connection_string")
141 if mongodb_uri
and self
.state
.mongodb_uri
!= mongodb_uri
:
142 self
.state
.mongodb_uri
= mongodb_uri
146 if __name__
== "__main__":