Skip to content

Commit

Permalink
server/config: generalize container support
Browse files Browse the repository at this point in the history
Allow running in Kubernetes, podman, and LXC, besides plain docker-compose,
without having to fake out /.dockerenv in non-Docker environments.
  • Loading branch information
zakame authored and neobooru committed Nov 10, 2024
1 parent 46e3295 commit b721865
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions server/szurubooru/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def _merge(left: Dict, right: Dict) -> Dict:
return left


def _docker_config() -> Dict:
def _container_config() -> Dict:
if "TEST_ENVIRONMENT" not in os.environ:
for key in ["POSTGRES_USER", "POSTGRES_PASSWORD", "POSTGRES_HOST"]:
if key not in os.environ:
Expand Down Expand Up @@ -49,6 +49,15 @@ def _file_config(filename: str) -> Dict:
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:
ret = _file_config("config.yaml.dist")
if os.path.isfile("config.yaml"):
Expand All @@ -57,8 +66,8 @@ def _read_config() -> Dict:
logger.warning(
"'config.yaml' should be a file, not a directory, skipping"
)
if os.path.exists("/.dockerenv"):
ret = _merge(ret, _docker_config())
if _running_inside_container():
ret = _merge(ret, _container_config())
return ret


Expand Down

0 comments on commit b721865

Please sign in to comment.