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