Merge pull request #195 from luigi311/fix_user

Fix user
pull/196/head v6.1.1
Luigi311 2024-09-13 17:01:45 -06:00 committed by GitHub
commit 7e13c14636
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 43 additions and 25 deletions

View File

@ -2,7 +2,7 @@ FROM python:3.11-alpine
ENV PUID=1000 ENV PUID=1000
ENV PGID=1000 ENV PGID=1000
ENV GOSU_VERSION 1.17 ENV GOSU_VERSION=1.17
RUN apk add --no-cache tini dos2unix RUN apk add --no-cache tini dos2unix

View File

@ -2,29 +2,39 @@
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
echo "User is root, checking if we need to create a user and group based on environment variables"
# Create group and user based on environment variables
if [ ! "$(getent group "$PGID")" ]; then
# If groupadd exists, use it # If groupadd exists, use it
if command -v groupadd > /dev/null; then if command -v groupadd > /dev/null; then
groupadd -g "$PGID" jellyplex_group groupadd -g "$PGID" jellyplex_watched
else elif command -v addgroup > /dev/null; then
addgroup -g "$PGID" jellyplex_group addgroup -g "$PGID" jellyplex_watched
fi
fi fi
fi
if [ ! "$(getent passwd "$PUID")" ]; then # If user id does not exist, create the user
# If useradd exists, use it if [ ! "$(getent passwd "$PUID")" ]; then
if command -v useradd > /dev/null; then if command -v useradd > /dev/null; then
useradd --no-create-home -u "$PUID" -g "$PGID" jellyplex_user useradd --no-create-home -u "$PUID" -g "$PGID" jellyplex_watched
else elif command -v adduser > /dev/null; then
adduser -D -H -u "$PUID" -G jellyplex_group jellyplex_user # Get the group name based on the PGID since adduser does not have a flag to specify the group id
# and if the group id already exists the group name will be sommething unexpected
GROUPNAME=$(getent group "$PGID" | cut -d: -f1)
# Use alpine busybox adduser syntax
adduser -D -H -u "$PUID" -G "$GROUPNAME" jellyplex_watched
fi 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
# Adjust ownership of the application directory # Get directory of log and mark file to create base folder if it doesnt exist
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 +46,16 @@ if [ -n "$MARK_DIR" ]; then
mkdir -p "$MARK_DIR" mkdir -p "$MARK_DIR"
fi fi
chown -R "$PUID:$PGID" "$LOG_DIR" echo "Starting JellyPlex-Watched with UID: $PUID and GID: $PGID"
chown -R "$PUID:$PGID" "$MARK_DIR"
# Run the application as the created user # If root run as the created user
exec gosu "$PUID:$PGID" "$@" 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 current user
exec "$@"