-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstartup.sh
79 lines (65 loc) · 2 KB
/
startup.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
#!/bin/sh
DIR=$(dirname "$0")
. "$DIR/functions.sh"
# Create empty crontab file
true > /root/crontab
if [ -z "$FILTER" ] && [ -z "$COMPOSE_PROJECT_NAME" ]; then
echo "$(timestamp) | At least one of FILTER or COMPOSE_PROJECT_NAME variables must be defined."
exit 1
fi
if isTrue "$DEBUG"; then
supercronic -debug -inotify /root/crontab &
else
supercronic -inotify /root/crontab &
fi
# Function to update cron jobs based on container labels
update_cron() {
/usr/local/bin/update_cron.sh
}
# File to indicate if an update is already scheduled
UPDATE_SCHEDULED="/tmp/update_scheduled.lock"
# Wrapper function to handle concurrent events
handle_event() {
if [ -f "$UPDATE_SCHEDULED" ]; then
# If update is already scheduled or running, mark the need for a subsequent update
touch "$UPDATE_SCHEDULED"
else
# Schedule the update
touch "$UPDATE_SCHEDULED"
while [ -f "$UPDATE_SCHEDULED" ]; do
# Do not rush
sleep 2
rm -f "$UPDATE_SCHEDULED"
update_cron
done
fi
}
# Initial update
handle_event &
# Define a function to handle cleanup on SIGTERM
cleanup() {
echo "Received SIGTERM, cleaning up..."
rm -f "$UPDATE_SCHEDULED"
exit 0
}
# Set a trap to catch the SIGTERM signal
trap 'cleanup' TERM
# Construct docker events command with required filters
DOCKER_EVENTS_CMD="docker events --filter 'event=start' --filter 'event=die' --filter 'event=destroy'"
# Enforce filtering by cron.enabled=true
DOCKER_EVENTS_CMD="$DOCKER_EVENTS_CMD --filter 'label=cron.enabled=true'"
# Filter by COMPOSE_PROJECT_NAME if set
if [ -n "$COMPOSE_PROJECT_NAME" ]; then
DOCKER_EVENTS_CMD="$DOCKER_EVENTS_CMD --filter 'label=com.docker.compose.project=$COMPOSE_PROJECT_NAME'"
fi
# Enforce filtering by FILTER if set
if [ -n "$FILTER" ]; then
for filter in $FILTER; do
DOCKER_EVENTS_CMD="$DOCKER_EVENTS_CMD --filter '$filter'"
done
fi
# Execute the docker events command
eval "$DOCKER_EVENTS_CMD" | while IFS= read -r LINE; do
log "Docker event detected: $LINE"
handle_event &
done