2023-08-22 23:35:43 +00:00
|
|
|
#!/bin/bash
|
2020-07-16 18:08:58 +00:00
|
|
|
|
2023-10-31 06:42:12 +00:00
|
|
|
export DATA_DIR="${DATA_DIR:-/data}"
|
|
|
|
export ARCHIVEBOX_USER="${ARCHIVEBOX_USER:-archivebox}"
|
2021-02-16 06:22:36 +00:00
|
|
|
|
2023-10-31 06:50:27 +00:00
|
|
|
# if data directory already exists, autodetect detect owner by looking at files within
|
|
|
|
DETECTED_UID="$(stat -c '%u' "$DATA_DIR/logs/errors.log" 2>/dev/null || echo 911)"
|
|
|
|
DETECTED_GID="$(stat -c '%g' "$DATA_DIR/logs/errors.log" 2>/dev/null || echo 911)"
|
|
|
|
|
|
|
|
# prefer PUID and PGID passsed in explicitly as env vars to autodetected defaults
|
|
|
|
export PUID=${PUID:-$DETECTED_UID}
|
|
|
|
export PGID=${PGID:-$DETECTED_GID}
|
|
|
|
|
|
|
|
# Set the archivebox user to use the configured UID & GID
|
2023-10-31 06:25:51 +00:00
|
|
|
groupmod -o -g "$PGID" "$ARCHIVEBOX_USER" > /dev/null 2>&1
|
2023-10-31 06:42:12 +00:00
|
|
|
usermod -o -u "$PUID" "$ARCHIVEBOX_USER" > /dev/null 2>&1
|
2023-10-20 09:45:54 +00:00
|
|
|
export PUID="$(id -u archivebox)"
|
|
|
|
export PGID="$(id -g archivebox)"
|
2021-02-16 06:22:36 +00:00
|
|
|
|
2023-10-19 01:24:43 +00:00
|
|
|
# Check the permissions of the data dir (or create if it doesn't exist)
|
2021-01-25 23:55:37 +00:00
|
|
|
if [[ -d "$DATA_DIR/archive" ]]; then
|
2023-10-30 22:44:33 +00:00
|
|
|
if touch "$DATA_DIR/archive/.permissions_test_safe_to_delete" 2>/dev/null; then
|
2023-10-19 01:24:43 +00:00
|
|
|
# It's fine, we are able to write to the data directory
|
|
|
|
rm "$DATA_DIR/archive/.permissions_test_safe_to_delete"
|
|
|
|
# echo "[√] Permissions are correct"
|
|
|
|
else
|
2023-10-31 06:25:51 +00:00
|
|
|
echo -e "\n[X] Error: archivebox user (PUID=$PUID) is not able to write to your ./data dir." >&2
|
|
|
|
echo -e " Change ./data to be owned by PUID=$PUID PGID=$PGID on the host and retry:"
|
|
|
|
echo -e " \$ chown -R $PUID:$PGID ./data\n" >&2
|
|
|
|
echo -e " Configure the PUID & PGID environment variables to change the desired owner:" >&2
|
|
|
|
echo -e " https://docs.linuxserver.io/general/understanding-puid-and-pgid\n" >&2
|
2023-10-19 01:24:43 +00:00
|
|
|
exit 1
|
2021-01-25 23:55:37 +00:00
|
|
|
fi
|
|
|
|
else
|
|
|
|
# create data directory
|
2021-03-27 09:28:54 +00:00
|
|
|
mkdir -p "$DATA_DIR/logs"
|
2020-07-21 17:47:21 +00:00
|
|
|
fi
|
2023-10-30 22:44:33 +00:00
|
|
|
|
|
|
|
# force set the ownership of the data dir contents to the archivebox user and group
|
|
|
|
# this is needed because Docker Desktop often does not map user permissions from the host properly
|
2023-10-31 06:42:12 +00:00
|
|
|
chown $PUID:$PGID "$DATA_DIR"
|
|
|
|
chown $PUID:$PGID "$DATA_DIR"/*
|
2021-01-25 23:55:37 +00:00
|
|
|
|
2023-10-31 07:23:19 +00:00
|
|
|
|
|
|
|
export IN_QEMU="$(pmap 1 | grep qemu | wc -l | grep -E '^0$' >/dev/null && echo 'False' || echo 'True')"
|
|
|
|
if [[ "$IN_QEMU" == 'True' ]]; then
|
|
|
|
echo -e "\n[!] Warning: Running $(uname -m) emulated container in QEMU, some things will break!" >&2
|
|
|
|
echo -e " chromium (screenshot, pdf, dom), singlefile, and any dependencies that rely on inotify will not run in QEMU." >&2
|
|
|
|
echo -e " See here for more info: https://github.com/microsoft/playwright/issues/17395#issuecomment-1250830493\n" >&2
|
|
|
|
fi
|
|
|
|
|
2021-01-25 23:55:37 +00:00
|
|
|
# Drop permissions to run commands as the archivebox user
|
2023-10-31 06:25:51 +00:00
|
|
|
if [[ "$1" == /* || "$1" == "bash" || "$1" == "sh" || "$1" == "echo" || "$1" == "cat" || "$1" == "archivebox" ]]; then
|
|
|
|
# handle "docker run archivebox /some/non-archivebox/command" by executing args as direct bash command
|
|
|
|
# e.g. "docker run archivebox /venv/bin/archivebox-alt init"
|
|
|
|
# "docker run archivebox /bin/bash -c '...'"
|
|
|
|
# "docker run archivebox echo test"
|
2023-10-31 06:42:12 +00:00
|
|
|
exec gosu "$PUID" bash -c "$*"
|
2020-08-10 18:15:53 +00:00
|
|
|
else
|
2023-10-31 06:25:51 +00:00
|
|
|
# handle "docker run archivebox add ..." by running args as archivebox $subcommand
|
|
|
|
# e.g. "docker run archivebox add https://example.com"
|
|
|
|
# "docker run archivebox manage createsupseruser"
|
|
|
|
# "docker run archivebox server 0.0.0.0:8000"
|
2023-10-31 06:42:12 +00:00
|
|
|
exec gosu "$PUID" bash -c "archivebox $*"
|
2020-08-10 18:15:53 +00:00
|
|
|
fi
|