2022-03-01 13:55:05 +01:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
# INITIALIZES CONTAINER PACKAGES AND PERMISSIONS
|
|
|
|
|
|
|
|
# abort if not executed as root
|
2022-03-16 19:09:53 +01:00
|
|
|
if [[ $(/usr/bin/id -u) != "0" ]]; then
|
2022-03-01 13:55:05 +01:00
|
|
|
echo "Usage: run ${0##*/} as root" 1>&2
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# regular expressions
|
|
|
|
re='^[0-9]+$'
|
|
|
|
|
|
|
|
# detect environment
|
|
|
|
case $DOCKER_ENV in
|
|
|
|
prod)
|
2022-03-16 19:09:53 +01:00
|
|
|
export PATH="/usr/local/sbin:/usr/sbin:/sbin:/bin:/scripts:/opt/photoprism/bin:/usr/local/bin:/usr/bin";
|
2022-03-11 09:05:42 +01:00
|
|
|
INIT_SCRIPTS="/scripts"
|
|
|
|
CHOWN_DIRS=("/photoprism" "/opt/photoprism")
|
|
|
|
CHMOD_DIRS=("/opt/photoprism")
|
2022-03-01 13:55:05 +01:00
|
|
|
;;
|
|
|
|
|
|
|
|
develop)
|
2022-03-16 19:09:53 +01:00
|
|
|
export PATH="/usr/local/sbin:/usr/sbin:/sbin:/bin:/scripts:/usr/local/go/bin:/go/bin:/usr/local/bin:/usr/bin";
|
2022-03-02 10:30:07 +01:00
|
|
|
INIT_SCRIPTS="/go/src/github.com/photoprism/photoprism/scripts/dist"
|
2022-03-01 13:55:05 +01:00
|
|
|
CHOWN_DIRS=("/go /photoprism" "/opt/photoprism" "/tmp/photoprism")
|
|
|
|
CHMOD_DIRS=("/photoprism" "/opt/photoprism" "/tmp/photoprism")
|
|
|
|
;;
|
|
|
|
|
|
|
|
*)
|
|
|
|
echo "unsupported init environment \"$DOCKER_ENV\"";
|
|
|
|
exit
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
if [[ ${PHOTOPRISM_UID} =~ $re ]] && [[ ${PHOTOPRISM_UID} != "0" ]]; then
|
|
|
|
if [[ ${PHOTOPRISM_GID} =~ $re ]] && [[ ${PHOTOPRISM_GID} != "0" ]]; then
|
2022-03-16 19:09:53 +01:00
|
|
|
/usr/sbin/groupadd -g "${PHOTOPRISM_GID}" "group_${PHOTOPRISM_GID}" 2>/dev/null
|
|
|
|
/usr/sbin/useradd -o -u "${PHOTOPRISM_UID}" -g "${PHOTOPRISM_GID}" -d "/photoprism" "user_${PHOTOPRISM_UID}" 2>/dev/null
|
|
|
|
/usr/sbin/usermod -g "${PHOTOPRISM_GID}" "user_${PHOTOPRISM_UID}" 2>/dev/null
|
2022-03-01 13:55:05 +01:00
|
|
|
|
|
|
|
if [[ -z ${PHOTOPRISM_DISABLE_CHOWN} ]]; then
|
|
|
|
echo "updating filesystem permissions..."
|
|
|
|
echo "PHOTOPRISM_DISABLE_CHOWN: \"true\" disables filesystem permission updates"
|
2022-03-16 19:09:53 +01:00
|
|
|
/bin/chown --preserve-root -Rcf "${PHOTOPRISM_UID}:${PHOTOPRISM_GID}" "${CHOWN_DIRS[@]}"
|
|
|
|
/bin/chmod --preserve-root -Rcf u+rwX "${CHMOD_DIRS[@]}"
|
2022-03-01 13:55:05 +01:00
|
|
|
fi
|
|
|
|
else
|
2022-03-16 19:09:53 +01:00
|
|
|
/usr/sbin/useradd -o -u "${PHOTOPRISM_UID}" -g 1000 -d "/photoprism" "user_${PHOTOPRISM_UID}" 2>/dev/null
|
|
|
|
/usr/sbin/usermod -g 1000 "user_${PHOTOPRISM_UID}" 2>/dev/null
|
2022-03-01 13:55:05 +01:00
|
|
|
|
|
|
|
if [[ -z ${PHOTOPRISM_DISABLE_CHOWN} ]]; then
|
|
|
|
echo "updating filesystem permissions..."
|
|
|
|
echo "PHOTOPRISM_DISABLE_CHOWN: \"true\" disables filesystem permission updates"
|
2022-03-16 19:09:53 +01:00
|
|
|
/bin/chown --preserve-root -Rcf "${PHOTOPRISM_UID}" "${CHOWN_DIRS[@]}"
|
|
|
|
/bin/chmod --preserve-root -Rcf u+rwX "${CHMOD_DIRS[@]}"
|
2022-03-01 13:55:05 +01:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
# do nothing if PHOTOPRISM_INIT was not set
|
|
|
|
if [[ -z ${PHOTOPRISM_INIT} ]]; then
|
|
|
|
exit
|
|
|
|
fi
|
|
|
|
|
2022-03-11 09:05:42 +01:00
|
|
|
INIT_LOCK="/scripts/.init-lock"
|
2022-03-01 13:55:05 +01:00
|
|
|
|
2022-03-16 19:09:53 +01:00
|
|
|
# execute targets via /usr/bin/make
|
2022-03-01 13:55:05 +01:00
|
|
|
if [[ ! -e ${INIT_LOCK} ]]; then
|
|
|
|
for INIT_TARGET in $PHOTOPRISM_INIT; do
|
|
|
|
echo "init $INIT_TARGET..."
|
2022-03-16 19:09:53 +01:00
|
|
|
/usr/bin/make -C "$INIT_SCRIPTS" "$INIT_TARGET"
|
2022-03-01 13:55:05 +01:00
|
|
|
done
|
|
|
|
|
|
|
|
echo 1 >${INIT_LOCK}
|
|
|
|
fi
|