]> git.decadent.org.uk Git - dak.git/blob - config/debian/cronscript
b9cdf25296d47fa7849884e4caaca7067e6a781a
[dak.git] / config / debian / cronscript
1 #!/bin/bash
2 # No way I try to deal with a crippled sh just for POSIX foo.
3
4 # Copyright (C) 2009-2015 Joerg Jaspert <joerg@debian.org>
5 #
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License as
8 # published by the Free Software Foundation; version 2.
9 #
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 # General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19 # Homer: Are you saying you're never going to eat any animal again? What
20 #        about bacon?
21 # Lisa: No.
22 # Homer: Ham?
23 # Lisa: No.
24 # Homer: Pork chops?
25 # Lisa: Dad, those all come from the same animal.
26 # Homer: Heh heh heh. Ooh, yeah, right, Lisa. A wonderful, magical animal.
27
28 # exit on errors
29 set -e
30 # A pipeline's return status is the value of the last (rightmost)
31 # command to exit with a non-zero status, or zero if all commands exit
32 # successfully.
33 set -o pipefail
34 # make sure to only use defined variables
35 set -u
36 # ERR traps should be inherited from functions too. (And command
37 # substitutions and subshells and whatnot, but for us the functions is
38 # the important part here)
39 set -E
40
41 # The extglob shell option is enabled using the shopt builtin, several
42 # extended pattern matching operators are recognized. We use it for
43 # the POSSIBLEARGS and the first case ${ARGS} matching
44 shopt -s extglob
45
46 # And use one locale, no matter what the caller has set
47 export LANG=C.UTF-8
48 export LC_ALL=C.UTF-8
49
50 # import the general variable set.
51 export SCRIPTVARS=/srv/ftp-master.debian.org/dak/config/debian/vars
52 . $SCRIPTVARS
53
54 # One arg please
55 declare -lr ARG=${1:-"meh"}
56
57 # program name is the (lower cased) first argument.
58 PROGRAM="${ARG}"
59
60 # And the following types of cronscripts exists
61 declare -lr POSSIBLEARGS='+(unchecked|dinstall|hourly|daily|weekly|monthly|yearly)'
62
63 # set DEBUG if you want to see a little more logs (needs to be used more)
64 DEBUG=${DEBUG:-0}
65
66 # Check if the argument is a known one. If so, lock us so that only
67 # one copy of the type of cronscript runs. The $type.tasks file is
68 # mandantory, so use that for locking.
69 case ${ARG} in
70     ${POSSIBLEARGS})
71         # Only one of me should ever run.
72         FLOCKER=${FLOCKER:-""}
73         [  "${FLOCKER}"  != "${configdir}/${PROGRAM}.tasks" ] && exec env FLOCKER="${configdir}/${PROGRAM}.tasks" flock -E 0 -en "${configdir}/${PROGRAM}.tasks" "$0" "$@" || :
74         ;;
75     *)
76         cat - <<EOF
77 This is the FTPMaster cronscript. It needs an argument or it won't do
78 anything for you.
79
80 Currently accepted Arguments:
81
82    unchecked - Process the unchecked queue
83    dinstall  - Run a dinstall
84    yearly, hourly, daily, weekly - Run that part
85
86 EOF
87         exit 0
88         ;;
89 esac
90
91 (
92     LOCKFREE=0
93     flock --shared --nonblock 42 || LOCKFREE=1
94
95     # Did we get the lock? (It's shared, so usually we will. But DSA
96     # can take an exclusive one in preparation for a reboot)
97     if [[ ${LOCKFREE} -gt 0 ]]; then
98         echo "Couldn't get (shared) reboot lock"
99         exit 1
100     fi
101
102     # common functions are "outsourced"
103     . "${configdir}/common"
104
105     # Timestamp when we started
106     NOW=$(date "+%Y.%m.%d-%H:%M:%S")
107
108     # Which list of tasks should we run?
109     declare -r TASKLIST="${configdir}/${PROGRAM}.tasks"
110
111     # A logfile for every cron script
112     LOGFILE="${logdir}/${PROGRAM}_${NOW}.log"
113
114     # Each "cronscript" may have a variables and a functions file
115     # that we source
116     for what in variables functions; do
117         if [[ -f ${configdir}/${PROGRAM}.${what} ]]; then
118             . ${configdir}/${PROGRAM}.${what}
119         fi
120     done
121
122     # Get rid of tempfiles at the end
123     trap cleanup EXIT TERM HUP INT QUIT
124
125     case ${ARG} in
126         unchecked)
127             # Do not run during dinstall
128             if [[ -e ${LOCK_DAILY} ]]; then
129                 exit 0;
130             fi
131             # only run one cron.unchecked and also lock against hourly (newoverview)
132             if ! lockfile -r8 ${LOCK_UNCHECKED} 2> /dev/null; then
133                 # log "aborting cron.unchecked because $LOCK_UNCHECKED has already been locked"
134                 exit 0
135             fi
136             TMPFILES="${TMPFILES} ${LOCK_UNCHECKED}"
137             ;;
138         dinstall)
139             ;;
140         hourly)
141             ;;
142         daily)
143             ;;
144         weekly)
145             ;;
146         monthly)
147             ;;
148         yearly)
149             ;;
150         *)
151             error "Unknown arg ${ARG}"
152             exit 42
153             ;;
154     esac
155
156     # An easy access by name for the current log
157     ln -sf ${LOGFILE} ${logdir}/${PROGRAM}
158
159     # And from here, all output to the log please
160     exec >> "$LOGFILE" 2>&1
161
162     # The stage function uses this directory
163     # This amends the stagedir variable from "vars"
164     stagedir="${stagedir}/${PROGRAM}"
165     # Ensure the dir exists
166     mkdir -p ${stagedir}
167
168     # This loop simply wants to be fed by a list of values (see below)
169     # made out of 5 columns.
170     # The first four are the array values for the stage function, the
171     # fifth tells us if we should background the stage call.
172     #
173     #  - FUNC - the function name to call
174     #  - ARGS - Possible arguments to hand to the function. Can be the empty string
175     #  - TIME - The timestamp name. Can be the empty string
176     #  - ERR  - if this is the string false, then the call will be surrounded by
177     #           set +e ... set -e calls, so errors in the function do not exit
178     #           dinstall. Can be the empty string, meaning true.
179     #  - BG   - Background the function stage?
180     #
181     # ATTENTION: Spaces in arguments or timestamp names need to be escaped by \
182     #
183     # NOTE 1: There are two special values for the first column (FUNC).
184     #         STATE   - do not call stage function, call the state
185     #                   function to update the public statefile "where is dinstall"
186     #         NOSTAGE - do not call stage function, call the command directly.
187     #
188     # Note 2: If you want to hand an empty value to the stage function,
189     #         use the word "none" in the list below.
190     while read FUNC ARGS TIME ERR BACKGROUND; do
191         debug "FUNC: $FUNC ARGS: $ARGS TIME: $TIME ERR: $ERR BG: $BACKGROUND"
192
193         # Empty values in the value list are the string "none" (or the
194         # while read loop won't work). Here we ensure that variables that
195         # can be empty, are empty if the string none is set for them.
196         for var in ARGS TIME; do
197             if [[ ${!var} == none ]]; then
198                 typeset ${var}=''
199             fi
200         done
201
202         # ERR/BACKGROUND are boolean, check that they are.
203         for var in ERR BACKGROUND; do
204             if [[ ${!var} != false ]] && [[ ${!var} != true ]]; then
205                 error "Illegal value ${!var} for ${var} (should be true or false), line for function ${FUNC}"
206             fi
207         done
208
209         case ${FUNC} in
210             STATE)
211                 state ${ARGS}
212                 ;;
213             NOSTAGE)
214                 ${ARGS}
215                 ;;
216             *)
217                 GO=(
218                     FUNC=${FUNC}
219                     TIME=${TIME}
220                     ARGS=${ARGS}
221                     ERR=${ERR}
222                 )
223                 if [[ ${BACKGROUND} == true ]]; then
224                     stage $GO &
225                 else
226                     stage $GO
227                 fi
228                 ;;
229         esac
230     done < <(grep -v '^#' ${TASKLIST} )
231
232     # we need to wait for the background processes before the end of the cron script
233     wait
234
235
236     # Common to all cron scripts
237     log "Cron script successful, all done"
238     # Redirect output to another file, as we want to compress our logfile
239     # and ensure its no longer used
240     exec > "$logdir/after${PROGRAM}.log" 2>&1
241
242     case ${ARG} in
243         unchecked)
244         ;;
245         dinstall)
246             logstats ${LOGFILE}
247             state "all done"
248             touch "${DINSTALLEND}"
249             ;;
250         hourly)
251             ;;
252         daily)
253             ;;
254         weekly)
255             ;;
256         monthly)
257             ;;
258         yearly)
259             ;;
260     esac
261
262     # Now, at the very (successful) end of this run, make sure we remove
263     # our stage files, so the next dinstall run will do it all again.
264     rm -f ${stagedir}/*
265     bzip2 -9 ${LOGFILE}
266
267     # Logfile should be gone, remove the symlink
268     [[ -L ${logdir}/${PROGRAM} ]] && [[ ! -f ${logdir}/${PROGRAM} ]] && rm -f ${logdir}/${PROGRAM} || log "Logfile still exists or symlink gone already? Something fishy going on"
269
270     # FIXME: Mail the log when its non-empty
271     [[ -s "${logdir}/after${PROGRAM}.log" ]] || rm "${logdir}/after${PROGRAM}.log"
272
273 # And end the reboot-locked part
274 ) 42</var/run/reboot-lock