Generate the user and group names

Also check if the the user already exists.
This commit is contained in:
David Coppit
2015-06-26 00:19:25 -04:00
parent 44f8af4a50
commit 3888123dcd
2 changed files with 26 additions and 22 deletions

View File

@@ -219,11 +219,8 @@ do
# Wait until it's okay to run the command again, monstering up events as we do so # Wait until it's okay to run the command again, monstering up events as we do so
wait_for_minimum_period $last_run_time wait_for_minimum_period $last_run_time
# Generate a user from the MD5 of the config file echo "$(ts) Running command with user ID $USER_ID and group ID $GROUP_ID"
USER=$(md5sum $CONFIG_FILE | awk '{print $1}') /files/runas.sh $USER_ID $GROUP_ID $COMMAND &
echo "$(ts) Running command"
/files/runas.sh $USER:$USER_ID:$GROUP_ID $COMMAND &
PID=$! PID=$!
last_run_time=$(date +"%s") last_run_time=$(date +"%s")

View File

@@ -1,7 +1,5 @@
#!/bin/bash #!/bin/bash
GROUP=docker
#----------------------------------------------------------------------------------------------------------------------- #-----------------------------------------------------------------------------------------------------------------------
function ts { function ts {
@@ -11,28 +9,37 @@ function ts {
#----------------------------------------------------------------------------------------------------------------------- #-----------------------------------------------------------------------------------------------------------------------
function process_args { function process_args {
local USER_UID_GID=$1 # These are intended to be global
USER_ID=$1
GROUP_ID=$2
if [[ ! "$USER_UID_GID" =~ ^[A-Za-z0-9._][-A-Za-z0-9._]*:[0-9]{1,}:[0-9]{1,}$ ]] if [[ ! "$USER_ID" =~ ^[0-9]{1,}$ ]]
then then
echo "USER_UID_GID value $USER_UID_GID is not valid. It should be of the form <user>:<uid>:<gid>" echo "User ID value $USER_ID is not valid. It must be a whole number"
exit 1 exit 1
fi fi
# These are meant to be global. if [[ ! "$GROUP_ID" =~ ^[0-9]{1,}$ ]]
USER=${USER_UID_GID%:*:*} then
USER_ID=${USER_UID_GID#*:} echo "Group ID value $GROUP_ID is not valid. It must be a whole number"
USER_ID=${USER_ID%:*} exit 1
GROUP_ID=${USER_UID_GID#*:*:} fi
} }
#----------------------------------------------------------------------------------------------------------------------- #-----------------------------------------------------------------------------------------------------------------------
function create_user { function create_user {
local USER=$1 local USER_ID=$1
local USER_ID=$2 local GROUP_ID=$2
local GROUP=$3
local GROUP_ID=$4 USER="user_${USER_ID}_$GROUP_ID"
GROUP="group_${USER_ID}_$GROUP_ID"
if id -u $USER >/dev/null 2>&1
then
echo "$(ts) User \"$USER\" already exists. Skipping creation of user and group..."
return
fi
echo "$(ts) Creating user \"$USER\" (ID $USER_ID) and group \"$GROUP\" (ID $GROUP_ID) to run the command..." echo "$(ts) Creating user \"$USER\" (ID $USER_ID) and group \"$GROUP\" (ID $GROUP_ID) to run the command..."
@@ -48,10 +55,10 @@ function create_user {
process_args "$@" process_args "$@"
# Shift off the arg so that we can exec $@ below # Shift off the args so that we can exec $@ below
shift shift; shift
create_user $USER $USER_ID $GROUP $GROUP_ID create_user $USER_ID $GROUP_ID
echo "$(ts) Running command as user \"$USER\"..." echo "$(ts) Running command as user \"$USER\"..."
exec /sbin/setuser $USER "$@" exec /sbin/setuser $USER "$@"