From 93b9689920923b1a27ae0d0b792a845b2e45a624 Mon Sep 17 00:00:00 2001 From: Michael Kenney Date: Mon, 10 Apr 2017 13:30:40 -0600 Subject: [PATCH] Add support for specifying the UID and GID values --- container/run-as-user | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/container/run-as-user b/container/run-as-user index f9faefb..378175d 100755 --- a/container/run-as-user +++ b/container/run-as-user @@ -1,10 +1,12 @@ #!/bin/sh ############################################################################## -# By default, change the dev user's uid and gid to match the user that owns -# the project directory and run a command as that user. If a ~/.ssh directory -# exists and it's not owned by root then switch and run as that user instead -# to use public key authentication. +# If a PUID/PGID enviroment variable exists, use those values for the `uid` +# and `gid` when executing scripts, otherwise change the dev user's uid and +# gid to match the user that owns the project directory and run a command as +# that user. If a ~/.ssh directory exists and it's not owned by root then +# switch and run as that user instead in order to take advantage of public key +# authentication. ############################################################################## stat_dir="/src" @@ -12,8 +14,24 @@ if [ -d "/home/dev/.ssh" ] && [ "0" != "$(stat -c '%g' /home/dev/.ssh)" ] && [ " stat_dir="/home/dev/.ssh" fi -groupmod -g $(stat -c '%g' $stat_dir) -o dev > /dev/null 2>&1 -usermod -u $(stat -c '%u' $stat_dir) -o dev > /dev/null 2>&1 +# if the PUID environment variable exists, assume that is the preferred user id, +# otherwise use the $stat_dir +if [ "" != "$PUID" ]; then + uid=$PUID +else + uid=$(stat -c '%u' $stat_dir) +fi + +# if the PGID environment variable exists, assume that is the preferred group id, +# otherwise use the $stat_dir +if [ "" != "$PGID" ]; then + gid=$PGID +else + gid=$(stat -c '%g' $stat_dir) +fi + +groupmod -g $gid -o dev > /dev/null 2>&1 +usermod -u $uid -o dev > /dev/null 2>&1 chown -R dev:dev ~dev/ > /dev/null 2>&1 sudo -u dev $@