Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions container/run-as-user
Original file line number Diff line number Diff line change
@@ -1,19 +1,37 @@
#!/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"
if [ -d "/home/dev/.ssh" ] && [ "0" != "$(stat -c '%g' /home/dev/.ssh)" ] && [ "0" != "$(stat -c '%u' /home/dev/.ssh)" ]; then
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 $@