]> git.decadent.org.uk Git - dak.git/blob - config/debian/cronscript
There can be only one
[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 # common functions are "outsourced"
92 . "${configdir}/common"
93
94 # Timestamp when we started
95 NOW=$(date "+%Y.%m.%d-%H:%M:%S")
96
97 # Which list of tasks should we run?
98 declare -r TASKLIST="${configdir}/${PROGRAM}.tasks"
99
100 # A logfile for every cron script
101 LOGFILE="${logdir}/${PROGRAM}_${NOW}.log"
102
103 # Each "cronscript" may have a variables and a functions file
104 # that we source
105 for what in variables functions; do
106     if [[ -f ${configdir}/${PROGRAM}.${what} ]]; then
107         . ${configdir}/${PROGRAM}.${what}
108     fi
109 done
110
111 # Get rid of tempfiles at the end
112 trap cleanup EXIT TERM HUP INT QUIT
113
114 case ${ARG} in
115     unchecked)
116         # Do not run during dinstall
117         if [[ -e ${LOCK_DAILY} ]]; then
118             exit 0;
119         fi
120         # only run one cron.unchecked and also lock against hourly (newoverview)
121         if ! lockfile -r8 ${LOCK_UNCHECKED} 2> /dev/null; then
122             # log "aborting cron.unchecked because $LOCK_UNCHECKED has already been locked"
123             exit 0
124         fi
125         TMPFILES="${TMPFILES} ${LOCK_UNCHECKED}"
126         ;;
127     dinstall)
128         ;;
129     hourly)
130         ;;
131     daily)
132         ;;
133     weekly)
134         ;;
135     monthly)
136         ;;
137     yearly)
138         ;;
139     *)
140         error "Unknown arg ${ARG}"
141         exit 42
142     ;;
143 esac
144
145 # An easy access by name for the current log
146 ln -sf ${LOGFILE} ${logdir}/${PROGRAM}
147
148 # And from here, all output to the log please
149 exec >> "$LOGFILE" 2>&1
150
151 # The stage function uses this directory
152 # This amends the stagedir variable from "vars"
153 stagedir="${stagedir}/${PROGRAM}"
154 # Ensure the dir exists
155 mkdir -p ${stagedir}
156
157 # This loop simply wants to be fed by a list of values (see below)
158 # made out of 5 columns.
159 # The first four are the array values for the stage function, the
160 # fifth tells us if we should background the stage call.
161 #
162 #  - FUNC - the function name to call
163 #  - ARGS - Possible arguments to hand to the function. Can be the empty string
164 #  - TIME - The timestamp name. Can be the empty string
165 #  - ERR  - if this is the string false, then the call will be surrounded by
166 #           set +e ... set -e calls, so errors in the function do not exit
167 #           dinstall. Can be the empty string, meaning true.
168 #  - BG   - Background the function stage?
169 #
170 # ATTENTION: Spaces in arguments or timestamp names need to be escaped by \
171 #
172 # NOTE 1: There are two special values for the first column (FUNC).
173 #         STATE   - do not call stage function, call the state
174 #                   function to update the public statefile "where is dinstall"
175 #         NOSTAGE - do not call stage function, call the command directly.
176 #
177 # Note 2: If you want to hand an empty value to the stage function,
178 #         use the word "none" in the list below.
179 while read FUNC ARGS TIME ERR BACKGROUND; do
180     debug "FUNC: $FUNC ARGS: $ARGS TIME: $TIME ERR: $ERR BG: $BACKGROUND"
181
182     # Empty values in the value list are the string "none" (or the
183     # while read loop won't work). Here we ensure that variables that
184     # can be empty, are empty if the string none is set for them.
185     for var in ARGS TIME; do
186         if [[ ${!var} == none ]]; then
187             typeset ${var}=''
188         fi
189     done
190
191     # ERR/BACKGROUND are boolean, check that they are.
192     for var in ERR BACKGROUND; do
193         if [[ ${!var} != false ]] && [[ ${!var} != true ]]; then
194             error "Illegal value ${!var} for ${var} (should be true or false), line for function ${FUNC}"
195         fi
196     done
197
198     case ${FUNC} in
199         STATE)
200             state ${ARGS}
201         ;;
202         NOSTAGE)
203             ${ARGS}
204         ;;
205         *)
206             GO=(
207                 FUNC=${FUNC}
208                 TIME=${TIME}
209                 ARGS=${ARGS}
210                 ERR=${ERR}
211             )
212             if [[ ${BACKGROUND} == true ]]; then
213                 stage $GO &
214             else
215                 stage $GO
216             fi
217         ;;
218     esac
219 done < <(grep -v '^#' ${TASKLIST} )
220
221 # we need to wait for the background processes before the end of the cron script
222 wait
223
224
225 # Common to all cron scripts
226 log "Cron script successful, all done"
227 # Redirect output to another file, as we want to compress our logfile
228 # and ensure its no longer used
229 exec > "$logdir/after${PROGRAM}.log" 2>&1
230
231 case ${ARG} in
232     unchecked)
233         ;;
234     dinstall)
235         logstats ${LOGFILE}
236         state "all done"
237         touch "${DINSTALLEND}"
238         ;;
239     hourly)
240         ;;
241     daily)
242         ;;
243     weekly)
244         ;;
245     monthly)
246         ;;
247     yearly)
248         ;;
249 esac
250
251 # Now, at the very (successful) end of this run, make sure we remove
252 # our stage files, so the next dinstall run will do it all again.
253 rm -f ${stagedir}/*
254 bzip2 -9 ${LOGFILE}
255
256 # Logfile should be gone, remove the symlink
257 [[ -L ${logdir}/${PROGRAM} ]] && [[ ! -f ${logdir}/${PROGRAM} ]] && rm -f ${logdir}/${PROGRAM} || log "Logfile still exists or symlink gone already? Something fishy going on"
258
259 # FIXME: Mail the log when its non-empty
260 [[ -s "${logdir}/after${PROGRAM}.log" ]] || rm "${logdir}/after${PROGRAM}.log"