From 81d68ef3f7b04c0cf0ead26b8a5ccfa7c88b6b83 Mon Sep 17 00:00:00 2001 From: Stefan - Zipkid - Goethals Date: Thu, 10 Oct 2024 07:40:35 +0200 Subject: [PATCH] Update entrypoint Run custom scripts in filename order Add support for custom scripts in /docker-entrypoint.d for different stages of the entrypoint --- puppetserver/docker-entrypoint.sh | 118 +++++++++++++++++++++++++++--- 1 file changed, 108 insertions(+), 10 deletions(-) diff --git a/puppetserver/docker-entrypoint.sh b/puppetserver/docker-entrypoint.sh index 1a16c6f19..170f09581 100755 --- a/puppetserver/docker-entrypoint.sh +++ b/puppetserver/docker-entrypoint.sh @@ -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