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).
This commit is contained in:
Goffi 2024-06-12 22:47:34 +02:00
commit e7d240b7e4
3 changed files with 33 additions and 3 deletions

View file

@ -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_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"

View file

@ -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):

View file

@ -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)