From fef41a5628d810dbafad1526c8f4a4d8934153ca Mon Sep 17 00:00:00 2001 From: Donkie Date: Tue, 7 Nov 2023 21:45:52 +0100 Subject: [PATCH] Added warning if in docker and data dir isn't mounted Had to remove the VOLUME definition in the dockerfile for this, otherwise the data dir would always be mounted by an anonymous volume if the user didn't mount it correctly, so the python script wouldn't be able to detect it. --- Dockerfile | 1 - spoolman/env.py | 13 +++++++++++++ spoolman/main.py | 16 ++++++++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index adbbb03..49efe66 100644 --- a/Dockerfile +++ b/Dockerfile @@ -66,6 +66,5 @@ ENV BUILD_DATE=${BUILD_DATE} # Run command EXPOSE 8000 -VOLUME ["/home/app/.local/share/spoolman"] ENTRYPOINT ["uvicorn", "spoolman.main:app"] CMD ["--host", "0.0.0.0", "--port", "8000"] diff --git a/spoolman/env.py b/spoolman/env.py index 60042f0..4cfc35f 100644 --- a/spoolman/env.py +++ b/spoolman/env.py @@ -326,3 +326,16 @@ def check_write_permissions() -> None: gid, ) sys.exit(1) + + +def is_docker() -> bool: + """Check if we are running in a docker container.""" + return Path("/.dockerenv").exists() + + +def is_data_dir_mounted() -> bool: + """Check if the data directory is mounted as a shfs.""" + # "mount" will give us a list of all mounted filesystems + mounts = subprocess.run("mount", check=True, stdout=subprocess.PIPE, text=True) # noqa: S603, S607 + data_dir = str(get_data_dir().resolve()) + return any(data_dir in line for line in mounts.stdout.splitlines()) diff --git a/spoolman/main.py b/spoolman/main.py index dc7fe12..0b4401e 100644 --- a/spoolman/main.py +++ b/spoolman/main.py @@ -95,6 +95,22 @@ async def startup() -> None: logger.info("Startup complete.") + if env.is_docker() and not env.is_data_dir_mounted(): + logger.warning("!!!! WARNING !!!!") + logger.warning("!!!! WARNING !!!!") + logger.warning("The data directory is not mounted.") + logger.warning( + 'Spoolman stores its database in the container directory "%s". ' + "If this directory isn't mounted to the host OS, the database will be lost when the container is stopped.", + env.get_data_dir(), + ) + logger.warning( + "Please carefully read the docker part of the README.md file, " + "and ensure your docker-compose file matches the example.", + ) + logger.warning("!!!! WARNING !!!!") + logger.warning("!!!! WARNING !!!!") + if __name__ == "__main__": uvicorn.run(app, host="0.0.0.0", port=8000)