From 11b442988dc4bb8c1d562c9768e8a3fb685b4443 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Sat, 3 Dec 2022 17:54:29 +0100 Subject: docker: factor out cron stuff into schedule.sh It can be easily reused that way. --- docker/Dockerfile | 1 + docker/entrypoint.sh | 53 ++++++---------------------------------------- docker/schedule.sh | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+), 46 deletions(-) create mode 100755 docker/schedule.sh diff --git a/docker/Dockerfile b/docker/Dockerfile index 6bc6847..b9f67db 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -22,6 +22,7 @@ ENV SSH_AUTH_SOCK "$ssh_sock_path" COPY ["docker/entrypoint.sh", "/"] COPY ["docker/get_output_dir.py", "/"] +COPY ["docker/schedule.sh", "/"] COPY ["cgitize/", "/usr/src/cgitize/"] WORKDIR /usr/src diff --git a/docker/entrypoint.sh b/docker/entrypoint.sh index 4ca1304..59d4814 100755 --- a/docker/entrypoint.sh +++ b/docker/entrypoint.sh @@ -6,6 +6,11 @@ # Distributed under the MIT License. set -o errexit -o nounset -o pipefail +shopt -s inherit_errexit lastpipe + +script_dir="$( dirname -- "${BASH_SOURCE[0]}" )" +script_dir="$( cd -- "$script_dir" && pwd )" +readonly script_dir readonly base_dir=/usr/src readonly cfg_path=/etc/cgitize/cgitize.toml @@ -13,6 +18,7 @@ readonly cfg_path=/etc/cgitize/cgitize.toml secure_repo_dir() { local dir dir="$( /get_output_dir.py -- "$cfg_path" )" + chmod -- o-rwx "$dir" # This is required so that nginx can access the directory. @@ -21,54 +27,9 @@ secure_repo_dir() { chown -- :101 "$dir" } -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 "$@" - fi - - schedule="$( schedule_to_cron "$schedule" )" - - make_task_script "$@" > /task.sh - chmod +x /task.sh - - # Run the task once when the container is started, regardless of schedule. - /task.sh - - local crontab - crontab="$schedule /task.sh -# This is the new crontab." - - echo "$crontab" | crontab - - exec crond -f -} - main() { secure_repo_dir - setup_cron_task "$@" + exec "$script_dir/schedule.sh" "$@" } main "$@" diff --git a/docker/schedule.sh b/docker/schedule.sh new file mode 100755 index 0000000..0f3fa20 --- /dev/null +++ b/docker/schedule.sh @@ -0,0 +1,60 @@ +#!/usr/bin/env bash + +# Copyright (c) 2021 Egor Tensin +# This file is part of the "cgitize" project. +# For details, see https://github.com/egor-tensin/cgitize. +# Distributed under the MIT License. + +set -o errexit -o nounset -o pipefail +shopt -s inherit_errexit lastpipe + +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 -- "$( pwd )" && $( printf -- ' %q' "$@" )" +} + +setup_cron_task() { + local schedule + schedule="${SCHEDULE:-once}" + + if [ "$schedule" = once ]; then + exec "$@" + fi + + schedule="$( schedule_to_cron "$schedule" )" + + make_task_script "$@" > /task.sh + chmod +x /task.sh + + # Run the task once when the container is started, regardless of schedule. + /task.sh + + local crontab + crontab="$schedule /task.sh +# This is the new crontab." + + echo "$crontab" | crontab - + exec crond -f +} + +main() { + setup_cron_task "$@" +} + +main "$@" -- cgit v1.2.3