Created
August 17, 2017 13:45
-
-
Save ostretsov/8e3112635e8be8e13d8dda03f6e33f96 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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