Entrypoint: Check root user, check addgroup/adduser command exists

pull/195/head
Luis Garcia 2024-09-13 09:56:34 -06:00
parent 34d62c9021
commit fa0134551f
1 changed files with 34 additions and 24 deletions

View File

@ -2,29 +2,33 @@
set -e set -e
# Create group and user based on environment variables # Check if user is root
if [ ! "$(getent group "$PGID")" ]; then if [ "$(id -u)" = '0' ]; then
# If groupadd exists, use it # Create group and user based on environment variables
if command -v groupadd > /dev/null; then if [ ! "$(getent group "$PGID")" ]; then
groupadd -g "$PGID" jellyplex_group # If groupadd exists, use it
else if command -v groupadd > /dev/null; then
addgroup -g "$PGID" jellyplex_group groupadd -g "$PGID" jellyplex_group
elif command -v addgroup > /dev/null; then
addgroup -g "$PGID" jellyplex_group
fi
fi fi
if [ ! "$(getent passwd "$PUID")" ]; then
# If useradd exists, use it
if command -v useradd > /dev/null; then
useradd --no-create-home -u "$PUID" -g "$PGID" jellyplex_user
elif command -v adduser > /dev/null; then
adduser -D -H -u "$PUID" -G jellyplex_group jellyplex_user
fi
fi
else
# If user is not root, set the PUID and PGID to the current user
PUID=$(id -u)
PGID=$(id -g)
fi fi
if [ ! "$(getent passwd "$PUID")" ]; then # Get directory of log and mark file to create base folder if it doesnt exist
# If useradd exists, use it
if command -v useradd > /dev/null; then
useradd --no-create-home -u "$PUID" -g "$PGID" jellyplex_user
else
adduser -D -H -u "$PUID" -G jellyplex_group jellyplex_user
fi
fi
# Adjust ownership of the application directory
chown -R "$PUID:$PGID" /app
# Get directory of log and mark file to create base folder if it doesnt exist and change permissions
LOG_DIR=$(dirname "$LOG_FILE") LOG_DIR=$(dirname "$LOG_FILE")
# If LOG_DIR is set, create the directory # If LOG_DIR is set, create the directory
if [ -n "$LOG_DIR" ]; then if [ -n "$LOG_DIR" ]; then
@ -36,8 +40,14 @@ if [ -n "$MARK_DIR" ]; then
mkdir -p "$MARK_DIR" mkdir -p "$MARK_DIR"
fi fi
chown -R "$PUID:$PGID" "$LOG_DIR" # If root run as the created user
chown -R "$PUID:$PGID" "$MARK_DIR" if [ "$(id -u)" = '0' ]; then
chown -R "$PUID:$PGID" "$LOG_DIR"
chown -R "$PUID:$PGID" "$MARK_DIR"
# Run the application as the created user # Run the application as the created user
exec gosu "$PUID:$PGID" "$@" exec gosu "$PUID:$PGID" "$@"
fi
# Run the application as the current user
exec "$@"