Several fixes to the charmed installer
[osm/devops.git] / installers / charm / pla / src / charm.py
index 1fc6386..6847580 100755 (executable)
 #   See the License for the specific language governing permissions and
 #   limitations under the License.
 
-import sys
 import logging
 
-sys.path.append("lib")
-
 from ops.charm import CharmBase
-from ops.framework import StoredState, Object
+from ops.framework import StoredState
 from ops.main import main
 from ops.model import (
     ActiveStatus,
     MaintenanceStatus,
     WaitingStatus,
 )
-
-from glob import glob
-from pathlib import Path
-from string import Template
+from typing import NoReturn
 
 logger = logging.getLogger(__name__)
 
@@ -37,8 +31,8 @@ logger = logging.getLogger(__name__)
 class PLACharm(CharmBase):
     state = StoredState()
 
-    def __init__(self, framework, key):
-        super().__init__(framework, key)
+    def __init__(self, *args) -> NoReturn:
+        super().__init__(*args)
         self.state.set_default(spec=None)
         self.state.set_default(kafka_host=None)
         self.state.set_default(kafka_port=None)
@@ -84,7 +78,11 @@ class PLACharm(CharmBase):
         config = self.framework.model.config
 
         ports = [
-            {"name": "port", "containerPort": config["port"], "protocol": "TCP", },
+            {
+                "name": "port",
+                "containerPort": config["port"],
+                "protocol": "TCP",
+            },
         ]
 
         config_spec = {
@@ -102,7 +100,11 @@ class PLACharm(CharmBase):
             "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,
                 }
@@ -123,23 +125,21 @@ class PLACharm(CharmBase):
         """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"
-        )
+        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()