-
Notifications
You must be signed in to change notification settings - Fork 0
/
entrypoint.sh
53 lines (44 loc) · 1.44 KB
/
entrypoint.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/bin/bash
set -euo pipefail
log() {
echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $*"
}
# Get the user ID and group ID from environment variables
USER_ID=${LOCAL_USER_ID:-9001}
GROUP_ID=${LOCAL_GROUP_ID:-9001}
log "Starting entrypoint script"
log "Ensuring user with UID: $USER_ID, GID: $GROUP_ID"
# Create group if it doesn't exist
if ! getent group appgroup > /dev/null 2>&1; then
groupadd -g $GROUP_ID appgroup
else
# If group exists but with wrong GID, update it
if [ "$(getent group appgroup | cut -d: -f3)" != "$GROUP_ID" ]; then
groupmod -g $GROUP_ID appgroup
fi
fi
# Create user if it doesn't exist
if ! id -u appuser > /dev/null 2>&1; then
useradd -u $USER_ID -g $GROUP_ID -m appuser
else
# If user exists but with wrong UID or GID, update it
if [ "$(id -u appuser)" != "$USER_ID" ] || [ "$(id -g appuser)" != "$GROUP_ID" ]; then
usermod -u $USER_ID -g $GROUP_ID appuser
fi
fi
# Set ownership of the necessary directories
chown -R appuser:appgroup /app/logs /app/state
# Perform initialization checks
if [ ! -f /app/settings.ini ]; then
log "Error: settings.ini not found"
exit 1
elif [ ! -s /app/settings.ini ]; then
log "Warning: settings.ini is empty"
fi
# Handle SIGTERM
trap 'log "Caught SIGTERM signal, shutting down..."; kill -TERM $child' TERM
log "Starting main application"
# Switch to the new user and run the main application
exec gosu appuser python /app/app/main.py &
child=$!
wait $child