server/config: generalize container support

Allow running in Kubernetes, podman, and LXC, besides plain docker-compose,
without having to fake out /.dockerenv in non-Docker environments.
This commit is contained in:
Zak B. Elep 2024-11-04 00:56:11 +08:00 committed by Neo
parent 46e3295003
commit b721865931

View file

@ -21,7 +21,7 @@ def _merge(left: Dict, right: Dict) -> Dict:
return left return left
def _docker_config() -> Dict: def _container_config() -> Dict:
if "TEST_ENVIRONMENT" not in os.environ: if "TEST_ENVIRONMENT" not in os.environ:
for key in ["POSTGRES_USER", "POSTGRES_PASSWORD", "POSTGRES_HOST"]: for key in ["POSTGRES_USER", "POSTGRES_PASSWORD", "POSTGRES_HOST"]:
if key not in os.environ: if key not in os.environ:
@ -49,6 +49,15 @@ def _file_config(filename: str) -> Dict:
return yaml.load(handle.read(), Loader=yaml.SafeLoader) or {} return yaml.load(handle.read(), Loader=yaml.SafeLoader) or {}
def _running_inside_container() -> bool:
env = os.environ.keys()
return (
os.path.exists("/.dockerenv")
or "KUBERNETES_SERVICE_HOST" in env
or "container" in env # set by lxc/podman
)
def _read_config() -> Dict: def _read_config() -> Dict:
ret = _file_config("config.yaml.dist") ret = _file_config("config.yaml.dist")
if os.path.isfile("config.yaml"): if os.path.isfile("config.yaml"):
@ -57,8 +66,8 @@ def _read_config() -> Dict:
logger.warning( logger.warning(
"'config.yaml' should be a file, not a directory, skipping" "'config.yaml' should be a file, not a directory, skipping"
) )
if os.path.exists("/.dockerenv"): if _running_inside_container():
ret = _merge(ret, _docker_config()) ret = _merge(ret, _container_config())
return ret return ret