# See the License for the specific language governing permissions and
# limitations under the License.
-import sys
+from glob import glob
import logging
-
-sys.path.append("lib")
+from pathlib import Path
+from string import Template
+import sys
from ops.charm import CharmBase
from ops.framework import StoredState, Object
WaitingStatus,
)
-from glob import glob
-from pathlib import Path
-from string import Template
logger = logging.getLogger(__name__)
self.state.set_default(kafka_host=None)
self.state.set_default(kafka_port=None)
self.state.set_default(mongodb_uri=None)
- self.state.set_default(mysql_host=None)
- self.state.set_default(mysql_port=None)
# Observe Charm related events
self.framework.observe(self.on.config_changed, self.on_config_changed)
self.framework.observe(
self.on.mongo_relation_changed, self.on_mongo_relation_changed
)
- self.framework.observe(
- self.on.mysql_relation_changed, self.on_mysql_relation_changed
- )
def _apply_spec(self):
# Only apply the spec if this unit is a leader.
unit = self.model.unit
if not unit.is_leader():
- unit.status = ActiveStatus("Ready")
+ unit.status = ActiveStatus("ready")
return
if not self.state.kafka_host or not self.state.kafka_port:
unit.status = WaitingStatus("Waiting for Kafka")
if not self.state.mongodb_uri:
unit.status = WaitingStatus("Waiting for MongoDB")
return
- if not self.state.mysql_host or not self.state.mysql_port:
- unit.status = WaitingStatus("Waiting for MySQL")
- return
unit.status = MaintenanceStatus("Applying new pod spec")
new_spec = self.make_pod_spec()
if new_spec == self.state.spec:
- unit.status = ActiveStatus("Ready")
+ unit.status = ActiveStatus("ready")
return
self.framework.model.pod.set_spec(new_spec)
self.state.spec = new_spec
- unit.status = ActiveStatus("Ready")
+ unit.status = ActiveStatus("ready")
def make_pod_spec(self):
config = self.framework.model.config
- mysql_uri = "mysql://root:{}@{}:{}/{}".format(
- self.state.mysql_root_password,
- self.state.mysql_host,
- self.state.mysql_port,
- self.state.mysql_database,
- )
ports = [
- {"name": "port", "containerPort": config["port"], "protocol": "TCP",},
+ {
+ "name": "port",
+ "containerPort": config["port"],
+ "protocol": "TCP",
+ },
]
config_spec = {
"OSMPLA_DATABASE_DRIVER": "mongo",
"OSMPLA_DATABASE_URI": self.state.mongodb_uri,
"OSMPLA_GLOBAL_LOG_LEVEL": config["log_level"],
- "OSMPLA_SQL_DATABASE_URI": mysql_uri,
"OSMPLA_DATABASE_COMMONKEY": config["database_common_key"],
}
"containers": [
{
"name": self.framework.model.app.name,
- "image": config["image"],
+ "imageDetails": {
+ "imagePath": config["image"],
+ "username": config["image_username"],
+ "password": config["image_password"],
+ },
"ports": ports,
"config": config_spec,
}
"""Upgrade the charm."""
unit = self.model.unit
unit.status = MaintenanceStatus("Upgrading charm")
- self.on_start(event)
+ self._apply_spec()
def on_kafka_relation_changed(self, event):
- unit = self.model.unit
- if not unit.is_leader():
- return
- self.state.kafka_host = event.relation.data[event.unit].get("host")
- self.state.kafka_port = event.relation.data[event.unit].get("port")
+ kafka_host = event.relation.data[event.unit].get("host")
+ kafka_port = event.relation.data[event.unit].get("port")
+ if kafka_host and self.state.kafka_host != kafka_host:
+ self.state.kafka_host = kafka_host
+ if kafka_port and self.state.kafka_port != kafka_port:
+ self.state.kafka_port = kafka_port
self._apply_spec()
def on_mongo_relation_changed(self, event):
- unit = self.model.unit
- if not unit.is_leader():
- return
- self.state.mongodb_uri = event.relation.data[event.unit].get(
- "connection_string"
- )
- self._apply_spec()
-
- def on_mysql_relation_changed(self, event):
- unit = self.model.unit
- if not unit.is_leader():
- return
- unit_data = event.relation.data[event.unit]
- self.state.mysql_host = unit_data.get("host")
- self.state.mysql_port = unit_data.get("port")
- self.state.mysql_root_password = unit_data.get("root_password")
- self.state.mysql_database = self.model.config["database"]
+ mongodb_uri = event.relation.data[event.unit].get("connection_string")
+ if mongodb_uri and self.state.mongodb_uri != mongodb_uri:
+ self.state.mongodb_uri = mongodb_uri
self._apply_spec()