From 64e0f8ee7a35d31a296dcadb2f6021de59773a16 Mon Sep 17 00:00:00 2001 From: Mike Burgh Date: Thu, 14 Nov 2019 15:31:26 -0600 Subject: [PATCH 1/3] Initial support for passing UID/GID --- Dockerfile | 2 ++ entrypoint.sh | 43 +++++++++++++++++++++++++++++++++++++++---- 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/Dockerfile b/Dockerfile index 16a4993..a4e7713 100644 --- a/Dockerfile +++ b/Dockerfile @@ -22,6 +22,8 @@ ENV FORCE_SYNC= ENV CHECK_URL= ENV FAIL_URL= ENV TZ= +ENV UID= +ENV GID= RUN apk --no-cache add ca-certificates fuse wget dcron tzdata diff --git a/entrypoint.sh b/entrypoint.sh index 42d0cec..1f951eb 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -19,6 +19,41 @@ then exit 1 fi +# Make sure UID and GID are both supplied +if [ -z "$GID" -a ! -z "$UID" ] || [ -z "$UID" -a ! -z "$GID" ] +then + echo "WARNING: Must supply both UID and GID or neither. Stopping." + exit 1 +fi + +# Process UID and GID +if [ ! -z "$GID" ] +then + + #Get group name or add it! + GROUP=$(getent group "$GID" | cut -d: -f1) + USER=rclone + if [ -z "$GROUP" ] + then + GROUP="rclone" + addgroup --gid "$GID" "$GROUP" + fi + + #check if it already exists + if [ ! $(getent passwd "$UID") ] + then + adduser \ + --disabled-password \ + --gecos "" \ + --no-create-home \ + --ingroup "$GROUP" \ + --uid "$UID" \ + "$USER" >/dev/null + fi +else + USER="root" +fi + # Re-write cron shortcut case "$(echo "$CRON" | tr '[:lower:]' '[:upper:]')" in *@YEARLY* ) echo "INFO: Cron shortcut $CRON re-written to 0 0 1 1 *" && CRONS="0 0 1 1 *";; @@ -45,7 +80,7 @@ rm -f /tmp/sync.pid if [ -z "$SYNC_SRC" ] || [ -z "$SYNC_DEST" ] then echo "INFO: No SYNC_SRC and SYNC_DEST found. Starting rclone config" - rclone config $RCLONE_OPTS + su "$USER" -c "rclone config $RCLONE_OPTS" echo "INFO: Define SYNC_SRC and SYNC_DEST to start sync process." else # SYNC_SRC and SYNC_DEST setup @@ -61,13 +96,13 @@ else then echo "INFO: No CRON setting found. Running sync once." echo "INFO: Add CRON=\"0 0 * * *\" to perform sync every midnight" - /sync.sh + su "$USER" -c /sync.sh else if [ -z "$FORCE_SYNC" ] then echo "INFO: Add FORCE_SYNC=1 to perform a sync upon boot" else - /sync.sh + su "$USER" -c /sync.sh fi if [ ! -z "$SYNC_ONCE" ] @@ -78,7 +113,7 @@ else # Setup cron schedule crontab -d - echo "$CRONS /sync.sh >>/tmp/sync.log 2>&1" > /tmp/crontab.tmp + echo "$CRONS su "$USER" -c /sync.sh >>/tmp/sync.log 2>&1" > /tmp/crontab.tmp if [ -z "$CRON_ABORT" ] then echo "INFO: Add CRON_ABORT=\"0 6 * * *\" to cancel outstanding sync at 6am" From ab32406f18f2e3b6d8195f7c8eb58fce4400e9d2 Mon Sep 17 00:00:00 2001 From: Mike Burgh Date: Thu, 14 Nov 2019 15:32:53 -0600 Subject: [PATCH 2/3] typo --- entrypoint.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/entrypoint.sh b/entrypoint.sh index 1f951eb..f5bbc3d 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -113,7 +113,7 @@ else # Setup cron schedule crontab -d - echo "$CRONS su "$USER" -c /sync.sh >>/tmp/sync.log 2>&1" > /tmp/crontab.tmp + echo "$CRONS su $USER -c /sync.sh >>/tmp/sync.log 2>&1" > /tmp/crontab.tmp if [ -z "$CRON_ABORT" ] then echo "INFO: Add CRON_ABORT=\"0 6 * * *\" to cancel outstanding sync at 6am" From ad02bcf0250dc9e9c3649de182e77f5414dae796 Mon Sep 17 00:00:00 2001 From: Mike Burgh Date: Thu, 14 Nov 2019 19:03:59 -0600 Subject: [PATCH 3/3] Don't assume root, and switch to user if they arleady exist --- entrypoint.sh | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/entrypoint.sh b/entrypoint.sh index f5bbc3d..ad45340 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -30,18 +30,19 @@ fi if [ ! -z "$GID" ] then - #Get group name or add it! + #Get group name or add it GROUP=$(getent group "$GID" | cut -d: -f1) - USER=rclone if [ -z "$GROUP" ] then - GROUP="rclone" + GROUP=rclone addgroup --gid "$GID" "$GROUP" fi - #check if it already exists - if [ ! $(getent passwd "$UID") ] + #get user or add it + USER=$(getent passwd "$UID" | cut -d: -f1) + if [ -z "$USER" ] then + USER=rclone adduser \ --disabled-password \ --gecos "" \ @@ -51,7 +52,7 @@ then "$USER" >/dev/null fi else - USER="root" + USER=$(whoami) fi # Re-write cron shortcut