From 718ddec618c022301675f8d766ec30b2c1d1f3c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= <40151420+ldericher@users.noreply.github.com> Date: Tue, 1 Mar 2022 17:57:56 +0100 Subject: [PATCH 1/3] modularity --- bin/kiwi-cron | 8 ++++++-- libexec/kiwi-cron/create_schedule | 21 ++++++------------- libexec/kiwi-cron/print_cron_schedule | 11 ---------- libexec/kiwi-cron/print_crontab | 26 ++++++++++++++++++++++++ libexec/kiwi-cron/schedule_dirs | 29 ++------------------------- 5 files changed, 40 insertions(+), 55 deletions(-) delete mode 100755 libexec/kiwi-cron/print_cron_schedule diff --git a/bin/kiwi-cron b/bin/kiwi-cron index ce925f9..bbc3af9 100755 --- a/bin/kiwi-cron +++ b/bin/kiwi-cron @@ -1,5 +1,9 @@ #!/bin/sh -# replace crontab, run crond -/usr/local/libexec/kiwi-cron/schedule_dirs | crontab - +# replace crontab +/usr/local/libexec/kiwi-cron/schedule_dirs \ + | /usr/local/libexec/kiwi-cron/print_crontab \ + | crontab - + +# run crond exec crond -fd 8 \ No newline at end of file diff --git a/libexec/kiwi-cron/create_schedule b/libexec/kiwi-cron/create_schedule index 792b346..863b5b4 100755 --- a/libexec/kiwi-cron/create_schedule +++ b/libexec/kiwi-cron/create_schedule @@ -1,8 +1,5 @@ #!/bin/sh -this_script="$( readlink -f "${0}" )" -this_dir="${this_script%/*}" - cs_every="${1:-1}" cs_units="${2}" cs_command="${3}" @@ -36,27 +33,21 @@ cs_rand_month=$(( RANDOM % 2 + 1 )) case "${cs_units}" in minute) - "${this_dir}/print_cron_schedule" \ - "*${cs_every}" "*" "*" "*" "*" "${cs_command}" + echo "*${cs_every}" "*" "*" "*" "*" "${cs_command}" ;; hour) - "${this_dir}/print_cron_schedule" \ - "${cs_rand_min}" "*${cs_every}" "*" "*" "*" "${cs_command}" + echo "${cs_rand_min}" "*${cs_every}" "*" "*" "*" "${cs_command}" ;; day) - "${this_dir}/print_cron_schedule" \ - "${cs_rand_min}" "${cs_rand_hour}" "*${cs_every}" "*" "*" "${cs_command}" + echo "${cs_rand_min}" "${cs_rand_hour}" "*${cs_every}" "*" "*" "${cs_command}" ;; week) - "${this_dir}/print_cron_schedule" \ - "${cs_rand_min}" "${cs_rand_hour}" "*" "*" "${cs_rand_weekday}" "${cs_command}" + echo "${cs_rand_min}" "${cs_rand_hour}" "*" "*" "${cs_rand_weekday}" "${cs_command}" ;; month) - "${this_dir}/print_cron_schedule" \ - "${cs_rand_min}" "${cs_rand_hour}" "${cs_rand_day}" "*${cs_every}" "*" "${cs_command}" + echo "${cs_rand_min}" "${cs_rand_hour}" "${cs_rand_day}" "*${cs_every}" "*" "${cs_command}" ;; year) - "${this_dir}/print_cron_schedule" \ - "${cs_rand_min}" "${cs_rand_hour}" "${cs_rand_day}" "${cs_rand_month}" "*" "${cs_command}" + echo "${cs_rand_min}" "${cs_rand_hour}" "${cs_rand_day}" "${cs_rand_month}" "*" "${cs_command}" ;; esac diff --git a/libexec/kiwi-cron/print_cron_schedule b/libexec/kiwi-cron/print_cron_schedule deleted file mode 100755 index 10067c9..0000000 --- a/libexec/kiwi-cron/print_cron_schedule +++ /dev/null @@ -1,11 +0,0 @@ -#!/bin/sh - -pcs_minute="${1}" -pcs_hour="${2}" -pcs_day="${3}" -pcs_month="${4}" -pcs_weekday="${5}" -pcs_command="${6}" - -printf '%-8s%-8s%-8s%-8s%-8s%s\n' \ - "${pcs_minute}" "${pcs_hour}" "${pcs_day}" "${pcs_month}" "${pcs_weekday}" "${pcs_command}" diff --git a/libexec/kiwi-cron/print_crontab b/libexec/kiwi-cron/print_crontab index 13f4793..a2c6a29 100755 --- a/libexec/kiwi-cron/print_crontab +++ b/libexec/kiwi-cron/print_crontab @@ -1,2 +1,28 @@ #!/bin/sh +print_cron_schedule () { + pcs_minute="${1}" + pcs_hour="${2}" + pcs_day="${3}" + pcs_month="${4}" + pcs_weekday="${5}" + pcs_command="${6}" + + printf '%-8s%-8s%-8s%-8s%-8s%s\n' \ + "${pcs_minute}" "${pcs_hour}" "${pcs_day}" "${pcs_month}" "${pcs_weekday}" "${pcs_command}" +} + +# header +echo '# crontab generated by kiwi-cron' +echo '# generation time: '"$(date)" +echo '#' + +# short documentation line +print_cron_schedule \ + "# min" "hour" "day" "month" "weekday" "command" + +# schedules +while read -r pc_min pc_hour pc_day pc_month pc_weekday pc_command; do + print_cron_schedule \ + "${pc_min}" "${pc_hour}" "${pc_day}" "${pc_month}" "${pc_weekday}" "${pc_command}" +done diff --git a/libexec/kiwi-cron/schedule_dirs b/libexec/kiwi-cron/schedule_dirs index 8214536..e9b79aa 100755 --- a/libexec/kiwi-cron/schedule_dirs +++ b/libexec/kiwi-cron/schedule_dirs @@ -3,17 +3,6 @@ this_script="$( readlink -f "${0}" )" this_dir="${this_script%/*}" -crontab='' -crontab_line () { - if [ -z "${crontab}" ]; then - crontab="${1}" - else - crontab="$( - printf '%s\n%s' "${crontab}" "${1}"; - )" - fi -} - dir_has_no_files () { dhnf_directory="${1}" @@ -47,14 +36,6 @@ dir_to_unit () { esac } -crontab_line '# crontab generated by kiwi-cron' -crontab_line '# generation time: '"$(date)" -crontab_line '#' -crontab_line "$( - "${this_dir}/print_cron_schedule" \ - "# min" "hour" "day" "month" "weekday" "command" -)" - # check *ly dirs in "/kiwi-cron" directory schedule_dirs="$( find "/kiwi-cron" -type d -name "*ly" -mindepth 1 -maxdepth 1 @@ -71,9 +52,7 @@ for schedule_dir in ${schedule_dirs}; do units="${dir_name%ly}" units="$( dir_to_unit "${units}" )" - crontab_line "$( - "${this_dir}/create_schedule" "1" "${units}" "run-parts '${schedule_dir}'" - )" + "${this_dir}/create_schedule" "1" "${units}" "run-parts '${schedule_dir}'" done # check dirs in "/kiwi-cron/every" directory @@ -93,9 +72,5 @@ for schedule_dir in ${every_schedule_dirs}; do units="$( echo "${dir_name}" | awk -F_ '{print $2}' )" units="${units%s}" - crontab_line "$( - "${this_dir}/create_schedule" "${every}" "${units}" "run-parts '${schedule_dir}'" - )" + "${this_dir}/create_schedule" "${every}" "${units}" "run-parts '${schedule_dir}'" done - -echo "${crontab}" From 0cd472a08f556c0783cdb0db124dfe1fbea5cc5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= <40151420+ldericher@users.noreply.github.com> Date: Tue, 1 Mar 2022 18:40:05 +0100 Subject: [PATCH 2/3] modularity --- bin/kiwi-cron | 10 +++----- libexec/kiwi-cron/create_schedule | 36 ++++++++++++++-------------- libexec/kiwi-cron/run_cron | 40 +++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 25 deletions(-) create mode 100644 libexec/kiwi-cron/run_cron diff --git a/bin/kiwi-cron b/bin/kiwi-cron index bbc3af9..c1e2707 100755 --- a/bin/kiwi-cron +++ b/bin/kiwi-cron @@ -1,9 +1,5 @@ #!/bin/sh -# replace crontab -/usr/local/libexec/kiwi-cron/schedule_dirs \ - | /usr/local/libexec/kiwi-cron/print_crontab \ - | crontab - - -# run crond -exec crond -fd 8 \ No newline at end of file +exec /usr/local/libexec/kiwi-cron/run_cron \ + "${@}" \ + /usr/local/libexec/kiwi-cron/schedule_dirs diff --git a/libexec/kiwi-cron/create_schedule b/libexec/kiwi-cron/create_schedule index 863b5b4..34ed88c 100755 --- a/libexec/kiwi-cron/create_schedule +++ b/libexec/kiwi-cron/create_schedule @@ -32,22 +32,22 @@ cs_rand_weekday=$(( (6 + RANDOM % 2) % 7 )) cs_rand_month=$(( RANDOM % 2 + 1 )) case "${cs_units}" in -minute) - echo "*${cs_every}" "*" "*" "*" "*" "${cs_command}" - ;; -hour) - echo "${cs_rand_min}" "*${cs_every}" "*" "*" "*" "${cs_command}" - ;; -day) - echo "${cs_rand_min}" "${cs_rand_hour}" "*${cs_every}" "*" "*" "${cs_command}" - ;; -week) - echo "${cs_rand_min}" "${cs_rand_hour}" "*" "*" "${cs_rand_weekday}" "${cs_command}" - ;; -month) - echo "${cs_rand_min}" "${cs_rand_hour}" "${cs_rand_day}" "*${cs_every}" "*" "${cs_command}" - ;; -year) - echo "${cs_rand_min}" "${cs_rand_hour}" "${cs_rand_day}" "${cs_rand_month}" "*" "${cs_command}" - ;; + minute) + echo "*${cs_every}" "*" "*" "*" "*" "${cs_command}" + ;; + hour) + echo "${cs_rand_min}" "*${cs_every}" "*" "*" "*" "${cs_command}" + ;; + day) + echo "${cs_rand_min}" "${cs_rand_hour}" "*${cs_every}" "*" "*" "${cs_command}" + ;; + week) + echo "${cs_rand_min}" "${cs_rand_hour}" "*" "*" "${cs_rand_weekday}" "${cs_command}" + ;; + month) + echo "${cs_rand_min}" "${cs_rand_hour}" "${cs_rand_day}" "*${cs_every}" "*" "${cs_command}" + ;; + year) + echo "${cs_rand_min}" "${cs_rand_hour}" "${cs_rand_day}" "${cs_rand_month}" "*" "${cs_command}" + ;; esac diff --git a/libexec/kiwi-cron/run_cron b/libexec/kiwi-cron/run_cron new file mode 100644 index 0000000..347aabb --- /dev/null +++ b/libexec/kiwi-cron/run_cron @@ -0,0 +1,40 @@ +#!/bin/sh + +# check for CLI options +while getopts ':n' rc_optarg; do + case "${rc_optarg}" in + n) + rc_dry_run="yes" + ;; + + *) + echo "Invalid option: -${OPTARG}." + exit 2 + esac +done + +shift "$(( OPTIND - 1 ))" + +# CLI arguments +rc_executable="${1}" + + +if [ -n "${rc_dry_run}" ]; then + # verbose, dry run + set -x + + "${rc_executable}" \ + | /usr/local/libexec/kiwi-cron/print_crontab + + exit 0 +fi + +# replace crontab +"${rc_executable}" \ + | /usr/local/libexec/kiwi-cron/print_crontab \ + | crontab - + +# hand over to crond +exec crond -fd 8 + +exit 0 \ No newline at end of file From 1d0ef14827f35915bd1783e027f1644dd77f6122 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= <40151420+ldericher@users.noreply.github.com> Date: Tue, 1 Mar 2022 18:44:09 +0100 Subject: [PATCH 3/3] Time Zones --- Dockerfile | 3 +++ README.md | 6 ++++++ libexec/kiwi-cron/run_cron | 4 ++++ 3 files changed, 13 insertions(+) mode change 100644 => 100755 libexec/kiwi-cron/run_cron diff --git a/Dockerfile b/Dockerfile index 0b29a95..57569c9 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,7 @@ FROM alpine:latest +ENV TZ=Etc/UTC + RUN set -ex; \ \ mkdir -pm 755 /kiwi-cron; \ @@ -16,6 +18,7 @@ RUN set -ex; \ \ apk --no-cache add \ docker-cli \ + tzdata \ ; COPY bin /usr/local/bin/ diff --git a/README.md b/README.md index 141c7a5..fe0507d 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,12 @@ For each subdirectory, a random valid cron schedule is generated, so that: Cron schedules are regenerated once on each startup, only for directories that have files. +## Time Zones + +`kiwi-cron` images include the `tzdata` package and automatically handle `/etc/localtime` on startup. By default, "Etc/UTC" is set as the container time zone. + +To use a different time zone, change the container environment variable `TZ` to your liking, e.g. "Europe/Berlin". + ## Finer granularity: The `/kiwi-cron/every` directory Directories like `/kiwi-cron/every/5_minutes` will run scripts every 5 minutes. diff --git a/libexec/kiwi-cron/run_cron b/libexec/kiwi-cron/run_cron old mode 100644 new mode 100755 index 347aabb..8bfe7eb --- a/libexec/kiwi-cron/run_cron +++ b/libexec/kiwi-cron/run_cron @@ -1,5 +1,9 @@ #!/bin/sh +# set timezone +export TZ="${TZ:-Etc/UTC}" +ln -sf "/usr/share/zoneinfo/${TZ}" "/etc/localtime" + # check for CLI options while getopts ':n' rc_optarg; do case "${rc_optarg}" in