Files

49 lines
1.8 KiB
Bash

#!/bin/bash
# entrypoint.sh - Entrypoint script to handle user creation and permission setup at runtime
# Set default values if not provided
USER_ID=${LOCAL_USER_ID:-$(id -u 1000)}
GROUP_ID=${LOCAL_GROUP_ID:-$(id -g 1000)}
# In case the environment variables are not set properly, detect them from the workspace volume
if [ "$USER_ID" = "$(id -u 0)" ] || [ "$USER_ID" = "0" ]; then
# Detect the UID and GID of the user that owns the workspace directory
if [ -d "/workspace" ]; then
USER_ID=$(stat -c %u /workspace 2>/dev/null || echo 1000)
GROUP_ID=$(stat -c %g /workspace 2>/dev/null || echo 1000)
else
USER_ID=${LOCAL_USER_ID:-1000}
GROUP_ID=${LOCAL_GROUP_ID:-1000}
fi
fi
echo "Starting with USER_ID=$USER_ID and GROUP_ID=$GROUP_ID"
# Create the group with specified GID
groupadd -f -g $GROUP_ID -o TSYS-Tools 2>/dev/null || groupmod -g $GROUP_ID -o TSYS-Tools
# Create the user with specified UID and add to the group
useradd -f -u $USER_ID -g $GROUP_ID -m -s /bin/bash -o TSYS-Tools 2>/dev/null || usermod -u $USER_ID -g $GROUP_ID -o TSYS-Tools
# Add user to sudo group for any necessary operations
usermod -aG sudo TSYS-Tools 2>/dev/null || true
# Make sure workspace directory exists and has proper permissions
mkdir -p /workspace
chown -R $USER_ID:$GROUP_ID /workspace
# Set up proper permissions for .local (if they exist)
mkdir -p /home/TSYS-Tools/.local
chown $USER_ID:$GROUP_ID /home/TSYS-Tools/.local
# Set up proper permissions for Jupyter (if they exist)
mkdir -p /home/TSYS-Tools/.jupyter
chown $USER_ID:$GROUP_ID /home/TSYS-Tools/.jupyter
# If there are additional arguments, run them as the created user
if [ $# -gt 0 ]; then
exec su -p TSYS-Tools -c "$*"
else
# Otherwise start an interactive bash shell as the created user
exec su -p TSYS-Tools -c "/bin/bash"
fi