From e7d240b7e488c09179e3bde136be61f5c45a4e75 Mon Sep 17 00:00:00 2001 From: Goffi Date: Wed, 12 Jun 2024 22:47:34 +0200 Subject: [PATCH] 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). --- libervia/backend/core/constants.py | 4 ++++ libervia/backend/core/main.py | 23 ++++++++++++++++++++++- libervia/backend/memory/memory.py | 9 +++++++-- 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/libervia/backend/core/constants.py b/libervia/backend/core/constants.py index 47711b06..b92cc5b6 100644 --- a/libervia/backend/core/constants.py +++ b/libervia/backend/core/constants.py @@ -193,6 +193,10 @@ class Const(object): # 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 --git a/libervia/backend/core/main.py b/libervia/backend/core/main.py index 86e86c87..66a87d2f 100644 --- a/libervia/backend/core/main.py +++ b/libervia/backend/core/main.py @@ -22,6 +22,7 @@ import uuid 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 @@ class LiberviaBackend(service.Service): 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 @@ class LiberviaBackend(service.Service): 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 --git a/libervia/backend/memory/memory.py b/libervia/backend/memory/memory.py index fd1bf2ca..4d6b02b7 100644 --- a/libervia/backend/memory/memory.py +++ b/libervia/backend/memory/memory.py @@ -24,7 +24,7 @@ import mimetypes 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 @@ class Memory: ## 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)