aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/docker/entrypoint.sh
blob: 4ca13047886bf8cb5725404576e409a92783e2c3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env bash

# Copyright (c) 2021 Egor Tensin <Egor.Tensin@gmail.com>
# 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

readonly base_dir=/usr/src
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.
    # nginx uses a fixed UID:
    # https://github.com/nginxinc/docker-nginx/blob/4785a604aa40e0b0a69047a61e28781a2b0c2069/mainline/alpine-slim/Dockerfile#L16
    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 "$@"
}

main "$@"