-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Run custom scripts in filename order Add support for custom scripts in /docker-entrypoint.d for different stages of the entrypoint
- Loading branch information
Showing
1 changed file
with
108 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,117 @@ | ||
#!/bin/bash | ||
# bash is required to pass ENV vars with dots as sh cannot | ||
|
||
set -e | ||
set -o errexit # exit on any command failure; use `whatever || true` to accept failures | ||
# use `if something; then` instead of `something; if [ $? -eq 0 ]; then` | ||
# use `rv=0; something || rv=$?` if you really need the exact exit code | ||
set -o pipefail # pipes fail when any command fails, not just the last one. Use: ( whatever || true ) | somethingelse | ||
set -o nounset # exit on use of undeclared var, use `${possibly_undefined-}` to substitute the empty string in that case | ||
# You can assign default values like this: | ||
# `: ${possibly_undefined=default}` | ||
# `: ${possibly_undefined_or_empty:=default}` will also replace an empty (but declared) value | ||
# set -o xtrace | ||
|
||
for f in /docker-entrypoint.d/*.sh; do | ||
pid=0 | ||
|
||
echoerr() { echo "$@" 1>&2; } | ||
|
||
echoerr "Entrypoint PID $$" | ||
|
||
## Pre execution handler | ||
pre_execution_handler() { | ||
for f in /docker-entrypoint.d/*.sh; do | ||
echo "Running $f" | ||
"$f" | ||
done | ||
|
||
if [ -d /docker-custom-entrypoint.d/ ]; then | ||
done | ||
if [ -d /docker-custom-entrypoint.d/ ]; then | ||
find /docker-custom-entrypoint.d/ -type f -name "*.sh" \ | ||
-exec chmod +x {} \; | ||
-exec chmod +x {} \; | ||
sync | ||
find /docker-custom-entrypoint.d/ -type f -name "*.sh" \ | ||
-exec echo Running {} \; -exec {} \; | ||
fi | ||
for f in /docker-custom-entrypoint.d/*.sh; do | ||
echo "Running $f" | ||
"$f" | ||
done | ||
fi | ||
} | ||
|
||
## Post startup handler | ||
post_startup_handler() { | ||
if [ -d /docker-custom-entrypoint.d/ ]; then | ||
if [ -d /docker-custom-entrypoint.d/post-startup/ ]; then | ||
find /docker-custom-entrypoint.d/post-startup/ -type f -name "*.sh" \ | ||
-exec chmod +x {} \; | ||
sync | ||
for f in /docker-custom-entrypoint.d/post-startup/*.sh; do | ||
echo "Running $f" | ||
"$f" | ||
done | ||
fi | ||
fi | ||
} | ||
|
||
## Post execution handler | ||
post_execution_handler() { | ||
if [ -d /docker-custom-entrypoint.d/ ]; then | ||
if [ -d /docker-custom-entrypoint.d/post-execution/ ]; then | ||
find /docker-custom-entrypoint.d/post-execution/ -type f -name "*.sh" \ | ||
-exec chmod +x {} \; | ||
sync | ||
for f in /docker-custom-entrypoint.d/post-execution/*.sh; do | ||
echo "Running $f" | ||
"$f" | ||
done | ||
fi | ||
fi | ||
} | ||
|
||
## Sigterm Handler | ||
sigterm_handler() { | ||
echoerr "Catching SIGTERM" | ||
if [ $pid -ne 0 ]; then | ||
echoerr "sigterm_handler for PID '${pid}' triggered" | ||
# the above if statement is important because it ensures | ||
# that the application has already started. without it you | ||
# could attempt cleanup steps if the application failed to | ||
# start, causing errors. | ||
if [ -d /docker-custom-entrypoint.d/ ]; then | ||
if [ -d /docker-custom-entrypoint.d/sigterm-handler/ ]; then | ||
find /docker-custom-entrypoint.d/sigterm-handler/ -type f -name "*.sh" \ | ||
-exec chmod +x {} \; | ||
sync | ||
for f in /docker-custom-entrypoint.d/sigterm-handler/*.sh; do | ||
echo "Running $f" | ||
"$f" | ||
done | ||
fi | ||
fi | ||
kill -15 "$pid" | ||
wait "$pid" | ||
post_execution_handler | ||
fi | ||
exit 143; # 128 + 15 -- SIGTERM | ||
} | ||
|
||
## Setup signal trap | ||
# on callback execute the specified handler | ||
trap sigterm_handler SIGTERM | ||
|
||
## Initialization | ||
pre_execution_handler | ||
|
||
## Start Process | ||
echoerr "Starting Puppetserver" | ||
# run process in background and record PID | ||
/opt/puppetlabs/bin/puppetserver "$@" & | ||
pid="$!" | ||
|
||
## Post Startup | ||
post_startup_handler | ||
|
||
## Wait forever until app dies | ||
wait "$pid" | ||
return_code="$?" | ||
|
||
exec /opt/puppetlabs/bin/puppetserver "$@" | ||
## Cleanup | ||
post_execution_handler | ||
# echo the return code of the application | ||
exit $return_code |