From 6148b355ff329f44947886ff4210eeab3db8b241 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Sat, 3 Dec 2022 03:33:30 +0100 Subject: docker: refactor entrypoint.sh --- docker/entrypoint.sh | 65 +++++++++++++++++++++++++++++++++++----------------- 1 file changed, 44 insertions(+), 21 deletions(-) (limited to 'docker/entrypoint.sh') diff --git a/docker/entrypoint.sh b/docker/entrypoint.sh index 6e755b3..5674e36 100755 --- a/docker/entrypoint.sh +++ b/docker/entrypoint.sh @@ -7,32 +7,55 @@ set -o errexit -o nounset -o pipefail -schedule="${SCHEDULE:-once}" - -case "$schedule" in - once) +readonly base_dir=/usr/src + +schedule_to_cron() { + local schedule + for schedule; do + case "$schedule" in + 15min) echo '*/15 * * * *' ;; + hourly) echo '0 * * * *' ;; + daily) echo '0 0 * * *' ;; + weekly) echo '0 0 * * 1' ;; + monthly) echo '0 0 1 * *' ;; + *) + echo "$schedule" + ;; + esac + done +} + +make_task_script() { + echo "#!/bin/bash +cd -- "$base_dir" &&$( printf -- ' %q' "$@" )" +} + +setup_cron_task() { + local schedule + schedule="${SCHEDULE:-once}" + + if [ "$schedule" = once ]; then exec "$@" - ;; - 15min) schedule='*/15 * * * *' ;; - hourly) schedule='0 * * * *' ;; - daily) schedule='0 0 * * *' ;; - weekly) schedule='0 0 * * 1' ;; - monthly) schedule='0 0 1 * *' ;; - *) ;; -esac + fi -script="#!/bin/bash -cd /usr/src &&$( printf -- ' %q' "$@" )" + schedule="$( schedule_to_cron "$schedule" )" -echo "$script" > /task.sh -chmod +x /task.sh + make_task_script "$@" > /task.sh + chmod +x /task.sh -# Run the task once when the container is started, regardless of schedule. -/task.sh + # Run the task once when the container is started, regardless of schedule. + /task.sh -crontab="$schedule /task.sh + local crontab + crontab="$schedule /task.sh # This is the new crontab." -echo "$crontab" | crontab - + echo "$crontab" | crontab - + exec crond -f +} + +main() { + setup_cron_task "$@" +} -exec crond -f +main "$@" -- cgit v1.2.3