1+ #! /bin/sh
2+
3+ APP_USER=$1
4+ APP_GROUP=$2
5+ APP_USER_ID=$3
6+ APP_GROUP_ID=$4
7+
8+ new_user_id_exists=$( id ${APP_USER_ID} > /dev/null 2>&1 ; echo $? )
9+ if [ " $new_user_id_exists " = " 0" ]; then
10+ (>&2 echo " ERROR: APP_USER_ID $APP_USER_ID already exists - Aborting!" );
11+ exit 1;
12+ fi
13+
14+ new_group_id_exists=$( getent group ${APP_GROUP_ID} > /dev/null 2>&1 ; echo $? )
15+ if [ " $new_group_id_exists " = " 0" ]; then
16+ (>&2 echo " ERROR: APP_GROUP_ID $APP_GROUP_ID already exists - Aborting!" );
17+ exit 1;
18+ fi
19+
20+ old_user_id=$( id -u ${APP_USER} )
21+ old_user_exists=$( id -u ${APP_USER} > /dev/null 2>&1 ; echo $? )
22+ old_group_id=$( getent group ${APP_GROUP} | cut -d: -f3)
23+ old_group_exists=$( getent group ${APP_GROUP} > /dev/null 2>&1 ; echo $? )
24+
25+ if [ " $old_group_id " != " ${APP_GROUP_ID} " ]; then
26+ # create the group
27+ groupadd -f ${APP_GROUP}
28+ # and the correct id
29+ groupmod -g ${APP_GROUP_ID} ${APP_GROUP}
30+ if [ " $old_group_exists " = " 0" ]; then
31+ # set the permissions of all "old" files and folder to the new group
32+ find / -group $old_group_id -exec chgrp -h ${APP_GROUP} {} \; || true
33+ fi
34+ fi
35+
36+ if [ " $old_user_id " != " ${APP_USER_ID} " ]; then
37+ # create the user if it does not exist
38+ if [ " $old_user_exists " != " 0" ]; then
39+ useradd ${APP_USER} -g ${APP_GROUP}
40+ fi
41+
42+ # make sure the home directory exists with the correct permissions
43+ mkdir -p /home/${APP_USER} && chmod 755 /home/${APP_USER} && chown ${APP_USER} :${APP_GROUP} /home/${APP_USER}
44+
45+ # change the user id, set the home directory and make sure the user has a login shell
46+ usermod -u ${APP_USER_ID} -m -d /home/${APP_USER} ${APP_USER} -s $( which bash)
47+
48+ if [ " $old_user_exists " = " 0" ]; then
49+ # set the permissions of all "old" files and folder to the new user
50+ find / -user $old_user_id -exec chown -h ${APP_USER} {} \; || true
51+ fi
52+ fi
0 commit comments