Skip to content

Commit

Permalink
Entrypoint: Check root user, check addgroup/adduser command exists
Browse files Browse the repository at this point in the history
  • Loading branch information
luigi311 committed Sep 13, 2024
1 parent 34d62c9 commit fa01345
Showing 1 changed file with 32 additions and 22 deletions.
54 changes: 32 additions & 22 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,33 @@

set -e

# Create group and user based on environment variables
if [ ! "$(getent group "$PGID")" ]; then
# If groupadd exists, use it
if command -v groupadd > /dev/null; then
groupadd -g "$PGID" jellyplex_group
else
addgroup -g "$PGID" jellyplex_group
# Check if user is root
if [ "$(id -u)" = '0' ]; then
# Create group and user based on environment variables
if [ ! "$(getent group "$PGID")" ]; then
# If groupadd exists, use it
if command -v groupadd > /dev/null; then
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
else
adduser -D -H -u "$PUID" -G jellyplex_group jellyplex_user
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

# 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
# Get directory of log and mark file to create base folder if it doesnt exist
LOG_DIR=$(dirname "$LOG_FILE")
# If LOG_DIR is set, create the directory
if [ -n "$LOG_DIR" ]; then
Expand All @@ -36,8 +40,14 @@ if [ -n "$MARK_DIR" ]; then
mkdir -p "$MARK_DIR"
fi

chown -R "$PUID:$PGID" "$LOG_DIR"
chown -R "$PUID:$PGID" "$MARK_DIR"
# If root run as the created user
if [ "$(id -u)" = '0' ]; then
chown -R "$PUID:$PGID" "$LOG_DIR"
chown -R "$PUID:$PGID" "$MARK_DIR"

# Run the application as the created user
exec gosu "$PUID:$PGID" "$@"
fi

# Run the application as the created user
exec gosu "$PUID:$PGID" "$@"
# Run the application as the current user
exec "$@"

0 comments on commit fa01345

Please sign in to comment.