-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentrypoint.sh
executable file
·96 lines (79 loc) · 3.26 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/bin/sh
#
# Entrypoint script for the ACME Certificate Cleanup Docker container.
#
# This script initializes the environment, adjusts the UID and GID of the existing 'cleanup' user and group
# according to PUID and PGID environment variables, and runs the acme_cleanup.py application or any provided command.
#
# Author:
# Troy Kelly <[email protected]>
#
# Code History:
# - 2023-10-06: Initial creation.
# - 2023-10-06: Modified to honor PUID=1 and PGID=1 to run as root.
set -e
set -u
set -o pipefail
# Function to handle termination signals.
handle_signal() {
echo "Termination signal received. Exiting gracefully."
exit 0
}
# Trap SIGTERM and SIGINT signals.
trap 'handle_signal' SIGTERM SIGINT
# Main function to execute the acme_cleanup.py script or override with user command.
main() {
# Set default PUID and PGID if not provided.
PUID="${PUID:-1000}"
PGID="${PGID:-1000}"
# If PUID and PGID are 1, we assume the intent is to run as root.
if [ "$PUID" -eq 1 ] && [ "$PGID" -eq 1 ]; then
echo "PUID and PGID are set to 1. Running as root."
# Change to the application directory.
cd /app
# If no arguments are provided or the first argument starts with '-', default to running acme_cleanup.py.
if [ "$#" -eq 0 ] || [ "$(printf '%s' "$1" | cut -c1)" = "-" ]; then
# Ensure required environment variables are set.
if [ -z "${TRAEFIK_DASHBOARD_URL:-}" ]; then
echo "Error: TRAEFIK_DASHBOARD_URL environment variable is not set."
exit 1
fi
# Prepend the Python command and script name.
set -- /usr/bin/env python3 /app/acme_cleanup.py "$@"
fi
# Execute the command as root.
exec "$@"
else
# Get the current UID and GID of the 'cleanup' user and group.
CURRENT_UID=$(id -u cleanup)
CURRENT_GID=$(id -g cleanup)
# If the current UID does not match PUID, update it.
if [ "$CURRENT_UID" -ne "$PUID" ]; then
echo "Updating UID of 'cleanup' from $CURRENT_UID to $PUID"
usermod -u "$PUID" cleanup
fi
# If the current GID does not match PGID, update it.
if [ "$CURRENT_GID" -ne "$PGID" ]; then
echo "Updating GID of 'cleanup' from $CURRENT_GID to $PGID"
groupmod -g "$PGID" cleanup
fi
# Ensure 'cleanup' owns its home directory and application directory.
chown -R cleanup:cleanup /home/cleanup /app
# If no arguments are provided or the first argument starts with '-', default to running acme_cleanup.py.
if [ "$#" -eq 0 ] || [ "$(printf '%s' "$1" | cut -c1)" = "-" ]; then
# Ensure required environment variables are set.
if [ -z "${TRAEFIK_DASHBOARD_URL:-}" ]; then
echo "Error: TRAEFIK_DASHBOARD_URL environment variable is not set."
exit 1
fi
# Prepend the Python command and script name.
set -- /usr/bin/env python3 /app/acme_cleanup.py "$@"
fi
# Change to the application directory.
cd /app
# Execute the command as the 'cleanup' user.
exec su-exec cleanup "$@"
fi
}
# Invoke the main function with all script arguments.
main "$@"