changeset 4266:9fc3d28bc3f6

core (main): add a mechanism to have a shared temp directory: this directory may be used to share files between backend and frontends. Normally, an os-dependent temporary directory is created for that, but if this option is set, the directory will be created in <local_dir>/<cache_dir>, which may be useful in some use case (e.g. containerized frontends and backend).
author Goffi <goffi@goffi.org>
date Wed, 12 Jun 2024 22:47:34 +0200
parents 2417ad1d0f23
children 32388d743348
files libervia/backend/core/constants.py libervia/backend/core/main.py libervia/backend/memory/memory.py
diffstat 3 files changed, 33 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/libervia/backend/core/constants.py	Wed Jun 12 22:37:04 2024 +0200
+++ b/libervia/backend/core/constants.py	Wed Jun 12 22:47:34 2024 +0200
@@ -193,6 +193,10 @@
     # directory for components specific data
     COMPONENTS_DIR = "components"
     CACHE_DIR = "cache"
+    # Directory to share files between backend and frontends.
+    # It will be a sub-dir of <local_dir>/<cache_dir>
+    LOCAL_SHARED_DIR = "_SHARED_TMP"
+
     # files in file dir are stored for long term
     # files dir is global, i.e. for all profiles
     FILES_DIR = "files"
--- a/libervia/backend/core/main.py	Wed Jun 12 22:37:04 2024 +0200
+++ b/libervia/backend/core/main.py	Wed Jun 12 22:47:34 2024 +0200
@@ -22,6 +22,7 @@
 import hashlib
 import copy
 from pathlib import Path
+import shutil
 from typing import Optional, List, Tuple, Dict, cast
 
 from wokkel.data_form import Option
@@ -104,6 +105,22 @@
             log.exception("bridge can't be initialised, can't start Libervia Backend")
             sys.exit(1)
 
+        # If set, a temporary dir in cache will be used to share files between backend and
+        # frontends, useful when they are separated (e.g. when using containers). If
+        # unset, a temporary dir will be automatically created in os-relevant location.
+        use_local_shared_tmp: bool = C.bool(
+            self.memory.config_get("", "use_local_shared_tmp", C.BOOL_FALSE)
+        )
+        if use_local_shared_tmp:
+            self.local_shared_path = self.get_local_path(
+                None, C.CACHE_DIR, C.LOCAL_SHARED_DIR
+            )
+            shutil.rmtree(self.local_shared_path, ignore_errors=True)
+            self.local_shared_path.mkdir(0o700, parents=True, exist_ok=True)
+        else:
+            self.local_shared_path = None
+
+
         defer.ensureDeferred(self._post_init())
 
     @property
@@ -647,7 +664,11 @@
         log.info("Salut à toi ô mon frère !")
 
     def stopService(self):
-        log.info("Salut aussi à Rantanplan")
+        if self.local_shared_path is not None:
+            log.debug("Cleaning shared temporary dir.")
+            shutil.rmtree(self.local_shared_path, ignore_errors=True)
+        log.info("Salut aussi à Rantanplan.")
+
         return self.plugins_unload()
 
     def run(self):
--- a/libervia/backend/memory/memory.py	Wed Jun 12 22:37:04 2024 +0200
+++ b/libervia/backend/memory/memory.py	Wed Jun 12 22:47:34 2024 +0200
@@ -24,7 +24,7 @@
 import os.path
 from pathlib import Path
 import time
-from typing import Dict, Optional, Tuple
+from typing import Any, Dict, Optional, Tuple
 from uuid import uuid4
 
 import shortuuid
@@ -283,7 +283,12 @@
 
     ## Configuration ##
 
-    def config_get(self, section, name, default=None):
+    def config_get(
+        self,
+        section: str|None,
+        name: str,
+        default: Any = None
+    ) -> str|list|dict:
         """Get the main configuration option
 
         @param section: section of the config file (None or '' for DEFAULT)