# HG changeset patch # User Goffi # Date 1718225254 -7200 # Node ID 9fc3d28bc3f6723cc8fdae43854b4f7b01c9caa5 # Parent 2417ad1d0f2370c57fabc7de0e077f2e0121fd1f 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 /, which may be useful in some use case (e.g. containerized frontends and backend). diff -r 2417ad1d0f23 -r 9fc3d28bc3f6 libervia/backend/core/constants.py --- 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_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" diff -r 2417ad1d0f23 -r 9fc3d28bc3f6 libervia/backend/core/main.py --- 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): diff -r 2417ad1d0f23 -r 9fc3d28bc3f6 libervia/backend/memory/memory.py --- 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)