Skip to content

Commit e8e1dd9

Browse files
authored
Improve user management in development environment script
Updated usermod commands to handle existing users more safely and added error handling for getent.
1 parent 043b407 commit e8e1dd9

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

drupal/rootfs/etc/s6-overlay/scripts/development-environment.sh

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,29 @@ set -e
66
if [[ "${DEVELOPMENT_ENVIRONMENT}" != "true" ]]; then
77
exit 0
88
fi
9+
910
if [[ -z "${UID}" ]]; then
1011
exit 0
1112
fi
1213

13-
# Get the current user for this UID (if any)
14-
EXISTING_USER=$(getent passwd ${UID} | cut -d: -f1)
14+
# Get the current user for this UID (if any) - don't fail if not found
15+
EXISTING_USER=$(getent passwd "${UID}" 2>/dev/null | cut -d: -f1 || true)
1516

1617
if [ -z "$EXISTING_USER" ]; then
1718
# UID doesn't exist, safe to change nginx user
18-
usermod -u ${UID} nginx
19+
usermod -u "${UID}" nginx
1920
elif [ "$EXISTING_USER" != "nginx" ]; then
2021
# UID exists but belongs to another user
21-
usermod -u $((UID + 10000)) "$EXISTING_USER"
22-
usermod -u ${UID} nginx
22+
# Move existing user out of the way
23+
NEW_UID=$((UID + 10000))
24+
usermod -u "${NEW_UID}" "$EXISTING_USER" || true
25+
usermod -u "${UID}" nginx
2326
fi
2427

28+
# Fix ownership if needed
2529
if [[ "$(stat -c %u /var/www/drupal)" != "${UID}" ]]; then
2630
chown -R nginx:nginx /var/www/drupal
2731
fi
2832

29-
# always ensure nginx has access to the socket
33+
# Always ensure nginx has access to the socket
3034
chown -R nginx:nginx /run/php-fpm83
31-

0 commit comments

Comments
 (0)