Skip to content

Instantly share code, notes, and snippets.

@ostretsov
Created August 17, 2017 13:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ostretsov/8e3112635e8be8e13d8dda03f6e33f96 to your computer and use it in GitHub Desktop.
Save ostretsov/8e3112635e8be8e13d8dda03f6e33f96 to your computer and use it in GitHub Desktop.
#!/bin/sh
##############################################################################
# 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"
# 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
# Ensure the correct user id is available in the sudoers file (if the
# specified UID already existed in the image)
id -nu $uid > /dev/null 2>&1
if [ 0 -eq $? ]; then
echo "$(id -nu $uid) ALL=(ALL:ALL) NOPASSWD: ALL" >> /etc/sudoers
fi
# update the dev user with the specified UID/GID values
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 $@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment