]> git.decadent.org.uk Git - dak.git/blob - config/debian/cron.dinstall
Use log() not echo() to get timestamps
[dak.git] / config / debian / cron.dinstall
1 #!/bin/bash
2 # No way I try to deal with a crippled sh just for POSIX foo.
3
4 # Copyright (C) 2009-2012 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 set -o pipefail
31 # make sure to only use defined variables
32 set -u
33 # ERR traps should be inherited from functions too. (And command
34 # substitutions and subshells and whatnot, but for us the functions is
35 # the important part here)
36 set -E
37
38 # import the general variable set.
39 export SCRIPTVARS=/srv/ftp-master.debian.org/dak/config/debian/vars
40 . $SCRIPTVARS
41
42 ########################################################################
43 # Functions                                                            #
44 ########################################################################
45 # common functions are "outsourced"
46 . "${configdir}/common"
47
48 # source the dinstall functions
49 . "${configdir}/dinstall.functions"
50
51 ########################################################################
52 ########################################################################
53
54 # Function to save which stage we are in, so we can restart an interrupted
55 # dinstall. Or even run actions in parallel, if we dare to, by simply
56 # backgrounding the call to this function. But that should only really be
57 # done for things we don't care much about.
58 #
59 # This should be called with the first argument being an array, with the
60 # members
61 #  - FUNC - the function name to call
62 #  - ARGS - Possible arguments to hand to the function. Can be the empty string
63 #  - TIME - The timestamp name. Can be the empty string
64 #  - ERR  - if this is the string false, then the call will be surrounded by
65 #           set +e ... set -e calls, so errors in the function do not exit
66 #           dinstall. Can be the empty string, meaning true.
67 #
68 # MAKE SURE TO KEEP THIS THE LAST FUNCTION, AFTER ALL THE VARIOUS ONES
69 # ADDED FOR DINSTALL FEATURES!
70 function stage() {
71     ARGS='GO[@]'
72     local "${!ARGS}"
73
74     error=${ERR:-"true"}
75
76     ARGS=${ARGS:-""}
77
78     log "########## DINSTALL BEGIN: ${FUNC} ${ARGS} ##########"
79     STAGEFILE="${stagedir}/${FUNC}_${ARGS}"
80     STAGEFILE=${STAGEFILE// /_}
81     if [ -f "${STAGEFILE}" ]; then
82         stamptime=$(/usr/bin/stat -c %Z "${STAGEFILE}")
83         unixtime=$(date +%s)
84         difference=$(( $unixtime - $stamptime ))
85         if [ ${difference} -ge 14400 ]; then
86             log_error "Did already run ${FUNC}, stagefile exists, but that was ${difference} seconds ago. Please check."
87         else
88             log "Did already run ${FUNC}, not calling again..."
89         fi
90         return
91     fi
92
93     debug "Now calling function ${FUNC}. Arguments: ${ARGS}. Timestamp: ${TIME}"
94
95     # Make sure we are always at the same place. If a function wants to be elsewhere,
96     # it has to cd first!
97     cd ${configdir}
98
99     # Now redirect the output into $STAGEFILE.log. In case it errors out somewhere our
100     # errorhandler trap can then mail the contents of $STAGEFILE.log only, instead of a whole
101     # dinstall logfile. Short error mails ftw!
102     exec >> "${STAGEFILE}.log" 2>&1
103
104     if [ -f "${LOCK_STOP}" ]; then
105         log "${LOCK_STOP} exists, exiting immediately"
106         exit 42
107     fi
108
109     if [ "${error}" = "false" ]; then
110         set +e
111     fi
112     ${FUNC} ${ARGS}
113
114     # No matter what happened in the function, we make sure we have set -e default state back
115     set -e
116
117     # Make sure we are always at the same place.
118     cd ${configdir}
119
120     # We always use the same umask. If a function wants to do different, fine, but we reset.
121     umask 022
122
123     touch "${STAGEFILE}"
124
125     if [ -n "${TIME}" ]; then
126         ts "${TIME}"
127     fi
128
129     # And the output goes back to the normal logfile
130     exec >> "$LOGFILE" 2>&1
131
132     # Now we should make sure that we have a usable dinstall.log, so append the $STAGEFILE.log
133     # to it.
134     cat "${STAGEFILE}.log" >> "${LOGFILE}"
135     rm -f "${STAGEFILE}.log"
136
137     log "########## DINSTALL END: ${FUNC} ##########"
138
139     if [ -f "${LOCK_STOP}" ]; then
140         log "${LOCK_STOP} exists, exiting immediately"
141         exit 42
142     fi
143 }
144
145 ########################################################################
146
147 # We need logs.
148 LOGFILE="$logdir/dinstall.log"
149
150 exec >> "$LOGFILE" 2>&1
151
152 # And now source our default config
153 . "${configdir}/dinstall.variables"
154
155 # Make sure we start out with a sane umask setting
156 umask 022
157
158 # And use one locale, no matter what the caller has set
159 export LANG=C
160 export LC_ALL=C
161
162 touch "${DINSTALLSTART}"
163 ts "startup"
164 DINSTALLBEGIN="$(date -u +"%a %b %d %T %Z %Y (%s)")"
165 state "Startup"
166
167 lockfile -l 3600 "${LOCK_DAILY}"
168 trap onerror ERR
169 trap remove_daily_lock EXIT TERM HUP INT QUIT
170
171 touch "${LOCK_BRITNEY}"
172
173 GO=(
174     FUNC="savetimestamp"
175     TIME=""
176     ARGS=""
177     ERR="false"
178 )
179 stage $GO
180
181 GO=(
182     FUNC="qa1"
183     TIME="init"
184     ARGS=""
185     ERR="false"
186 )
187 stage $GO &
188
189 GO=(
190     FUNC="pg_timestamp"
191     TIME="pg_dump1"
192     ARGS="predinstall"
193     ERR=""
194 )
195 stage $GO
196
197 GO=(
198     FUNC="updates"
199     TIME="External Updates"
200     ARGS=""
201     ERR="false"
202 )
203 stage $GO
204
205 GO=(
206     FUNC="i18n1"
207     TIME="i18n 1"
208     ARGS=""
209     ERR="false"
210 )
211 stage $GO
212
213 GO=(
214     FUNC="dep11"
215     TIME="dep11 1"
216     ARGS=""
217     ERR="false"
218 )
219 stage $GO
220
221 lockfile "$LOCK_ACCEPTED"
222 trap remove_all_locks EXIT TERM HUP INT QUIT
223
224 GO=(
225     FUNC="punew"
226     TIME="p-u-new"
227     ARGS="stable-new"
228     ERR="false"
229 )
230 stage $GO
231
232 GO=(
233     FUNC="opunew"
234     TIME="o-p-u-new"
235     ARGS="oldstable-new"
236     ERR="false"
237 )
238 stage $GO
239
240 GO=(
241     FUNC="backports_policy"
242     TIME="backports-policy"
243     ARGS=""
244     ERR="false"
245 )
246 stage $GO
247
248 GO=(
249     FUNC="cruft"
250     TIME="cruft"
251     ARGS=""
252     ERR=""
253 )
254 stage $GO
255
256 state "indices"
257
258 GO=(
259     FUNC="dominate"
260     TIME="dominate"
261     ARGS=""
262     ERR=""
263 )
264 stage $GO
265
266 GO=(
267     FUNC="autocruft"
268     TIME="autocruft"
269     ARGS="unstable experimental"
270     ERR=""
271 )
272 stage $GO
273
274 GO=(
275     FUNC="fingerprints"
276     TIME="import-keyring"
277     ARGS=""
278     ERR="false"
279 )
280 stage $GO
281
282 GO=(
283     FUNC="overrides"
284     TIME="overrides"
285     ARGS=""
286     ERR=""
287 )
288 stage $GO
289
290 GO=(
291     FUNC="mpfm"
292     TIME="pkg-file-mapping"
293     ARGS=""
294     ERR="false"
295 )
296 stage $GO
297
298 state "packages/contents"
299 GO=(
300     FUNC="packages"
301     TIME="apt-ftparchive"
302     ARGS=""
303     ERR=""
304 )
305 stage $GO
306
307 state "dists/"
308 GO=(
309     FUNC="pdiff"
310     TIME="pdiff"
311     ARGS=""
312     ERR=""
313 )
314 stage $GO
315
316 GO=(
317     FUNC="gitpdiff"
318     TIME="gitpdiff"
319     ARGS=""
320     ERR=""
321 )
322 #stage $GO
323
324 GO=(
325     FUNC="release"
326     TIME="release files"
327     ARGS=""
328     ERR=""
329 )
330 stage $GO
331
332 GO=(
333     FUNC="dakcleanup"
334     TIME="cleanup"
335     ARGS=""
336     ERR=""
337 )
338 stage $GO
339
340 state "scripts"
341 GO=(
342     FUNC="mkmaintainers"
343     TIME="mkmaintainers"
344     ARGS=""
345     ERR=""
346 )
347 stage $GO
348
349 GO=(
350     FUNC="copyoverrides"
351     TIME="copyoverrides"
352     ARGS=""
353     ERR=""
354 )
355 stage $GO
356
357 GO=(
358     FUNC="mklslar"
359     TIME="mklslar"
360     ARGS=""
361     ERR=""
362 )
363 stage $GO
364
365 GO=(
366     FUNC="mkfilesindices"
367     TIME="mkfilesindices"
368     ARGS=""
369     ERR=""
370 )
371 stage $GO
372
373 GO=(
374     FUNC="mkchecksums"
375     TIME="mkchecksums"
376     ARGS=""
377     ERR=""
378 )
379 stage $GO
380
381 GO=(
382     FUNC="mirror"
383     TIME="mirror hardlinks"
384     ARGS=""
385     ERR=""
386 )
387 stage $GO
388
389 GO=(
390     FUNC="ddaccess"
391     TIME="ddaccessible sync"
392     ARGS=""
393     ERR="false"
394 )
395 stage $GO
396
397 remove_all_locks
398 trap - EXIT TERM HUP INT QUIT
399
400 ts "locked part finished"
401 state "postlock"
402
403 GO=(
404     FUNC="changelogs"
405     TIME="changelogs"
406     ARGS=""
407     ERR="false"
408 )
409 stage $GO &
410
411 GO=(
412     FUNC="pg_timestamp"
413     TIME="pg_dump2"
414     ARGS="postdinstall"
415     ERR=""
416 )
417 stage $GO
418
419 GO=(
420     FUNC="expire"
421     TIME="expire_dumps"
422     ARGS=""
423     ERR=""
424 )
425 stage $GO &
426
427 GO=(
428     FUNC="transitionsclean"
429     TIME="transitionsclean"
430     ARGS=""
431     ERR=""
432 )
433 stage $GO &
434
435 GO=(
436     FUNC="dm"
437     TIME=""
438     ARGS=""
439     ERR=""
440 )
441 stage $GO &
442
443 GO=(
444     FUNC="bts"
445     TIME=""
446     ARGS=""
447     ERR="false"
448 )
449 stage $GO &
450
451 GO=(
452     FUNC="mirrorpush"
453     TIME="mirrorpush"
454     ARGS=""
455     ERR="false"
456 )
457 stage $GO &
458
459 GO=(
460     FUNC="mirrorpush-backports"
461     TIME="mirrorpush-backports"
462     ARGS=""
463     ERR="false"
464 )
465 stage $GO &
466
467 GO=(
468     FUNC="i18n2"
469     TIME="i18n 2"
470     ARGS=""
471     ERR="false"
472 )
473 stage $GO &
474
475 GO=(
476     FUNC="stats"
477     TIME="stats"
478     ARGS=""
479     ERR="false"
480 )
481 stage $GO &
482
483 GO=(
484     FUNC="testingsourcelist"
485     TIME=""
486     ARGS=""
487     ERR="false"
488 )
489 stage $GO &
490
491 rm -f "${LOCK_BRITNEY}"
492
493 GO=(
494     FUNC="cleantransactions"
495     TIME=""
496     ARGS=""
497     ERR=""
498 )
499 stage $GO
500
501 # we need to wait for the background processes before the end of dinstall
502 wait
503
504 log "Daily cron scripts successful, all done"
505
506 exec > "$logdir/afterdinstall.log" 2>&1
507
508 GO=(
509     FUNC="renamelogfile"
510     TIME=""
511     ARGS=""
512     ERR="false"
513 )
514 stage $GO
515 state "all done"
516
517
518 # Now, at the very (successful) end of dinstall, make sure we remove
519 # our stage files, so the next dinstall run will do it all again.
520 rm -f ${stagedir}/*
521 touch "${DINSTALLEND}"