Fixing hostpaths of RO side car charm, updating osm_lib library.
[osm/devops.git] / installers / charm / osm-nbi / lib / charms / osm_libs / v0 / utils.py
index b7009c4..df3da94 100644 (file)
@@ -107,6 +107,7 @@ class MyCharm(CharmBase):
 
 - Get pod IP with `get_pod_ip()`
 """
+from dataclasses import dataclass
 import logging
 import secrets
 import socket
@@ -136,7 +137,7 @@ LIBAPI = 0
 
 # Increment this PATCH version before using `charmcraft publish-lib` or reset
 # to 0 if you are raising the major API version
-LIBPATCH = 2
+LIBPATCH = 3
 
 logger = logging.getLogger(__name__)
 
@@ -231,17 +232,30 @@ wait
 """
 
 
+@dataclass
+class SubModule:
+    """Represent RO Submodules."""
+    sub_module_path: str
+    container_path: str
+
+
 class HostPath:
     """Represents a hostpath."""
-
-    def __init__(self, config: str, container_path: str) -> None:
+    def __init__(self, config: str, container_path: str, submodules: dict = None) -> None:
         mount_path_items = config.split("-")
         mount_path_items.reverse()
         self.mount_path = "/" + "/".join(mount_path_items)
         self.config = config
-        self.container_path = container_path
-        self.module_name = container_path.split("/")[-1]
-
+        self.sub_module_dict = {}
+        if submodules:
+            for submodule in submodules.keys():
+                self.sub_module_dict[submodule] = SubModule(
+                    sub_module_path=self.mount_path + "/" + submodule,
+                    container_path=submodules[submodule],
+                )
+        else:
+            self.container_path = container_path
+            self.module_name = container_path.split("/")[-1]
 
 class DebugMode(Object):
     """Class to handle the debug-mode."""
@@ -416,15 +430,28 @@ class DebugMode(Object):
         # Add symlinks to mounted hostpaths
         for hostpath in mounted_hostpaths:
             logger.debug(f"adding symlink for {hostpath.config}")
-            self.container.exec(["rm", "-rf", hostpath.container_path]).wait_output()
-            self.container.exec(
-                [
-                    "ln",
-                    "-s",
-                    f"{hostpath.mount_path}/{hostpath.module_name}",
-                    hostpath.container_path,
-                ]
-            )
+            if len(hostpath.sub_module_dict) > 0:
+                for sub_module in hostpath.sub_module_dict.keys():
+                    self.container.exec(["rm", "-rf", hostpath.sub_module_dict[sub_module].container_path]).wait_output()
+                    self.container.exec(
+                        [
+                            "ln",
+                            "-s",
+                            hostpath.sub_module_dict[sub_module].sub_module_path,
+                            hostpath.sub_module_dict[sub_module].container_path,
+                        ]
+                    )
+
+            else:
+                self.container.exec(["rm", "-rf", hostpath.container_path]).wait_output()
+                self.container.exec(
+                    [
+                        "ln",
+                        "-s",
+                        f"{hostpath.mount_path}/{hostpath.module_name}",
+                        hostpath.container_path,
+                    ]
+                )
 
     def _configure_hostpaths(self, hostpaths: List[HostPath]):
         client = Client()