From 33ab7fd4ec1204c9a917fb04674d43ab6aec304e Mon Sep 17 00:00:00 2001 From: Nick Sweeting Date: Mon, 10 Aug 2020 14:15:53 -0400 Subject: [PATCH] autodetect when running inside docker and provide hints --- Dockerfile | 7 ++++--- archivebox/config/__init__.py | 1 + archivebox/config/stubs.py | 1 + archivebox/main.py | 16 +++++++++++----- bin/docker_entrypoint.sh | 23 ++++++++++++++++------- 5 files changed, 33 insertions(+), 15 deletions(-) diff --git a/Dockerfile b/Dockerfile index 460175d9..c6b898e7 100644 --- a/Dockerfile +++ b/Dockerfile @@ -71,11 +71,12 @@ RUN python -m venv --clear --symlinks "$VENV_PATH" \ VOLUME "$DATA_PATH" WORKDIR "$DATA_PATH" EXPOSE 8000 -ENV CHROME_BINARY=google-chrome \ +ENV IN_DOCKER=True \ + CHROME_BINARY=google-chrome \ CHROME_SANDBOX=False \ SINGLEFILE_BINARY="$EXTRA_PATH/SingleFile-master/cli/single-file" RUN env ALLOW_ROOT=True archivebox version -ENTRYPOINT ["dumb-init", "--", "/app/bin/docker_entrypoint.sh", "archivebox"] -CMD ["server", "0.0.0.0:8000"] +ENTRYPOINT ["dumb-init", "--", "/app/bin/docker_entrypoint.sh"] +CMD ["archivebox", "server", "0.0.0.0:8000"] diff --git a/archivebox/config/__init__.py b/archivebox/config/__init__.py index 9bdea244..e70c2fb0 100644 --- a/archivebox/config/__init__.py +++ b/archivebox/config/__init__.py @@ -45,6 +45,7 @@ CONFIG_DEFAULTS: Dict[str, ConfigDefaultDict] = { 'IS_TTY': {'type': bool, 'default': lambda _: sys.stdout.isatty()}, 'USE_COLOR': {'type': bool, 'default': lambda c: c['IS_TTY']}, 'SHOW_PROGRESS': {'type': bool, 'default': lambda c: c['IS_TTY']}, + 'IN_DOCKER': {'type': bool, 'default': False}, # TODO: 'SHOW_HINTS': {'type: bool, 'default': True}, }, diff --git a/archivebox/config/stubs.py b/archivebox/config/stubs.py index 6df90396..68a442eb 100644 --- a/archivebox/config/stubs.py +++ b/archivebox/config/stubs.py @@ -29,6 +29,7 @@ class ConfigDict(BaseConfig, total=False): IS_TTY: bool USE_COLOR: bool SHOW_PROGRESS: bool + IN_DOCKER: bool OUTPUT_DIR: str CONFIG_FILE: str diff --git a/archivebox/main.py b/archivebox/main.py index be915ca9..652f2d5e 100644 --- a/archivebox/main.py +++ b/archivebox/main.py @@ -57,7 +57,8 @@ from .config import ( stderr, ConfigDict, ANSI, - # IS_TTY, + IS_TTY, + IN_DOCKER, USER, ARCHIVEBOX_BINARY, ONLY_NEW, @@ -178,6 +179,10 @@ def help(out_dir: str=OUTPUT_DIR) -> None: else: print('{green}Welcome to ArchiveBox v{}!{reset}'.format(VERSION, **ANSI)) print() + if IN_DOCKER: + print('When using Docker, you need to mount a volume to use as your data dir:') + print(' docker run -v /some/path:/data archivebox ...') + print() print('To import an existing archive (from a previous version of ArchiveBox):') print(' 1. cd into your data dir OUTPUT_DIR (usually ArchiveBox/output) and run:') print(' 2. archivebox init') @@ -186,9 +191,6 @@ def help(out_dir: str=OUTPUT_DIR) -> None: print(' 1. Create an empty directory, then cd into it and run:') print(' 2. archivebox init') print() - print('If using Docker, you need to mount a volume to use as your data dir:') - print(' docker run -v /some/path:/data archivebox ...') - print() print('For more information, see the documentation here:') print(' https://github.com/pirate/ArchiveBox/wiki') @@ -1060,10 +1062,14 @@ def manage(args: Optional[List[str]]=None, out_dir: str=OUTPUT_DIR) -> None: """Run an ArchiveBox Django management command""" check_data_folder(out_dir=out_dir) - setup_django(out_dir) from django.core.management import execute_from_command_line + if (args and "createsuperuser" in args) and (IN_DOCKER and not IS_TTY): + stderr('[!] Warning: you need to pass -it to use interactive commands in docker', color='lightyellow') + stderr(' docker run -it archivebox manage {}'.format(' '.join(args or ['...'])), color='lightyellow') + stderr() + execute_from_command_line([f'{ARCHIVEBOX_BINARY} manage', *(args or ['help'])]) diff --git a/bin/docker_entrypoint.sh b/bin/docker_entrypoint.sh index 7e5836e3..c70d7f27 100755 --- a/bin/docker_entrypoint.sh +++ b/bin/docker_entrypoint.sh @@ -1,7 +1,5 @@ #!/usr/bin/env bash -COMMAND="$*" - # Autodetect UID,GID of host user based on ownership of files in the data volume DATA_DIR="${DATA_DIR:-/data}" ARCHIVEBOX_USER="${ARCHIVEBOX_USER:-archivebox}" @@ -18,8 +16,19 @@ if [[ "$USID" != 0 && "$GRID" != 0 ]]; then chown "$USID":"$GRID" "$DATA_DIR/*" > /dev/null 2>&1 || true fi -# run django as the new archivebox user -# any files touched will have the same uid,gid -# inside docker and outside docker on the host -gosu "$ARCHIVEBOX_USER" bash -c "$COMMAND" -# e.g. "archivebox server" +# Run commands as the new archivebox user in Docker. +# Any files touched will have the same uid & gid +# inside Docker and outside on the host machine. +if [[ "$1" == /* || "$1" == "echo" || "$1" == "archivebox" ]]; then + # arg 1 is a binary, execute it verbatim + # e.g. "archivebox init" + # "/bin/bash" + # "echo" + gosu "$ARCHIVEBOX_USER" bash -c "$*" +else + # no command given, assume args were meant to be passed to archivebox cmd + # e.g. "add https://example.com" + # "manage createsupseruser" + # "server 0.0.0.0:8000" + gosu "$ARCHIVEBOX_USER" bash -c "archivebox $*" +fi