]> git.decadent.org.uk Git - dak.git/blob - config/backports/cron.dinstall
include backports archive in (pool) sync
[dak.git] / config / backports / 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, 2010 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/backports-master.debian.org/dak/config/backports/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     STAGEFILE="${stagedir}/${FUNC}"
77     if [ -f "${STAGEFILE}" ]; then
78         stamptime=$(/usr/bin/stat -c %Z "${STAGEFILE}")
79         unixtime=$(date +%s)
80         difference=$(( $unixtime - $stamptime ))
81         if [ ${difference} -ge 14400 ]; then
82             log_error "Did already run ${FUNC}, stagefile exists, but that was ${difference} seconds ago. Please check."
83         else
84             log "Did already run ${FUNC}, not calling again..."
85         fi
86         return
87     fi
88
89     debug "Now calling function ${FUNC}. Arguments: ${ARGS}. Timestamp: ${TIME}"
90
91     # Make sure we are always at the same place. If a function wants to be elsewhere,
92     # it has to cd first!
93     cd ${configdir}
94
95     # Now redirect the output into $STAGEFILE.log. In case it errors out somewhere our
96     # errorhandler trap can then mail the contents of $STAGEFILE.log only, instead of a whole
97     # dinstall logfile. Short error mails ftw!
98     exec >> "${STAGEFILE}.log" 2>&1
99
100     if [ -f "${LOCK_STOP}" ]; then
101         log "${LOCK_STOP} exists, exiting immediately"
102         exit 42
103     fi
104
105     if [ "${error}" = "false" ]; then
106         set +e
107     fi
108     ${FUNC} ${ARGS}
109
110     # No matter what happened in the function, we make sure we have set -e default state back
111     set -e
112
113     # Make sure we are always at the same place.
114     cd ${configdir}
115
116     # We always use the same umask. If a function wants to do different, fine, but we reset.
117     umask 022
118
119     touch "${STAGEFILE}"
120
121     if [ -n "${TIME}" ]; then
122         ts "${TIME}"
123     fi
124
125     # And the output goes back to the normal logfile
126     exec >> "$LOGFILE" 2>&1
127
128     # Now we should make sure that we have a usable dinstall.log, so append the $STAGEFILE.log
129     # to it.
130     cat "${STAGEFILE}.log" >> "${LOGFILE}"
131     rm -f "${STAGEFILE}.log"
132
133     if [ -f "${LOCK_STOP}" ]; then
134         log "${LOCK_STOP} exists, exiting immediately"
135         exit 42
136     fi
137 }
138
139 ########################################################################
140
141 # We need logs.
142 LOGFILE="$logdir/dinstall.log"
143
144 exec >> "$LOGFILE" 2>&1
145
146 # And now source our default config
147 . "${configdir}/dinstall.variables"
148
149 # Make sure we start out with a sane umask setting
150 umask 022
151
152 # And use one locale, no matter what the caller has set
153 export LANG=C
154 export LC_ALL=C
155
156 # If we did not install new packages, we dont want to run.
157 if ! [ -f "${DINSTALLPACKAGES}" ]; then
158     log "nothing to do"
159     exit 0
160 fi
161 rm -f "${DINSTALLPACKAGES}"
162
163 touch "${DINSTALLSTART}"
164 ts "startup"
165 DINSTALLBEGIN="$(date -u +"%a %b %d %T %Z %Y (%s)")"
166 state "Startup"
167
168 lockfile -l 3600 "${LOCK_DAILY}"
169 trap onerror ERR
170 trap cleanup EXIT TERM HUP INT QUIT
171
172 touch "${LOCK_BRITNEY}"
173
174 GO=(
175     FUNC="savetimestamp"
176     TIME=""
177     ARGS=""
178     ERR="false"
179 )
180 stage $GO
181
182 GO=(
183     FUNC="pg_timestamp"
184     TIME="pg_dump1"
185     ARGS="predinstall"
186     ERR=""
187 )
188 stage $GO
189
190 lockfile "$LOCK_ACCEPTED"
191 lockfile "$LOCK_NEW"
192
193 GO=(
194     FUNC="punew"
195     TIME="p-u-new"
196     ARGS="proposedupdates"
197     ERR="false"
198 )
199 stage $GO
200
201 GO=(
202     FUNC="newstage"
203     TIME="newstage"
204     ARGS=""
205     ERR=""
206 )
207 stage $GO
208
209 GO=(
210     FUNC="cruft"
211     TIME="cruft"
212     ARGS=""
213     ERR=""
214 )
215 stage $GO
216
217 state "indices"
218
219 GO=(
220     FUNC="dominate"
221     TIME="dominate"
222     ARGS=""
223     ERR=""
224 )
225 stage $GO
226
227 GO=(
228     FUNC="filelist"
229     TIME="generate-filelist"
230     ARGS=""
231     ERR=""
232 )
233 stage $GO
234
235 GO=(
236     FUNC="fingerprints"
237     TIME="import-keyring"
238     ARGS=""
239     ERR="false"
240 )
241 stage $GO
242
243 GO=(
244     FUNC="overrides"
245     TIME="overrides"
246     ARGS=""
247     ERR=""
248 )
249 stage $GO
250
251 GO=(
252     FUNC="mpfm"
253     TIME="pkg-file-mapping"
254     ARGS=""
255     ERR="false"
256 )
257 stage $GO
258
259 state "packages/contents"
260 GO=(
261     FUNC="packages"
262     TIME="apt-ftparchive"
263     ARGS=""
264     ERR=""
265 )
266 # Careful: When we ever go and remove this monster-long thing, we have to check the backgrounded
267 # functions before it. We no longer have a 1.5hour sync point then.
268 stage $GO
269
270 state "dists/"
271 GO=(
272     FUNC="pdiff"
273     TIME="pdiff"
274     ARGS=""
275     ERR=""
276 )
277 stage $GO
278
279 GO=(
280     FUNC="release"
281     TIME="release files"
282     ARGS=""
283     ERR=""
284 )
285 stage $GO
286
287 GO=(
288     FUNC="dakcleanup"
289     TIME="cleanup"
290     ARGS=""
291     ERR=""
292 )
293 stage $GO
294
295 GO=(
296     FUNC="buildd_dir"
297     TIME="buildd_dir"
298     ARGS=""
299     ERR=""
300 )
301 stage $GO
302
303 state "scripts"
304 GO=(
305     FUNC="mkmaintainers"
306     TIME="mkmaintainers"
307     ARGS=""
308     ERR=""
309 )
310 stage $GO
311
312 GO=(
313     FUNC="copyoverrides"
314     TIME="copyoverrides"
315     ARGS=""
316     ERR=""
317 )
318 stage $GO
319
320 GO=(
321     FUNC="mklslar"
322     TIME="mklslar"
323     ARGS=""
324     ERR=""
325 )
326 stage $GO
327
328 GO=(
329     FUNC="mkchecksums"
330     TIME="mkchecksums"
331     ARGS=""
332     ERR=""
333 )
334 stage $GO
335
336 GO=(
337     FUNC="mirror"
338     TIME="mirror hardlinks"
339     ARGS=""
340     ERR=""
341 )
342 stage $GO
343
344 rm -f "$LOCK_ACCEPTED"
345 rm -f "$LOCK_NEW"
346 rm -f "${LOCK_DAILY}"
347
348 ts "locked part finished"
349 state "postlock"
350
351 GO=(
352     FUNC="changelogs"
353     TIME="changelogs"
354     ARGS=""
355     ERR="false"
356 )
357 stage $GO &
358
359 GO=(
360     FUNC="pg_timestamp"
361     TIME="pg_dump2"
362     ARGS="postdinstall"
363     ERR=""
364 )
365 stage $GO &
366
367 GO=(
368     FUNC="expire"
369     TIME="expire_dumps"
370     ARGS=""
371     ERR=""
372 )
373 #stage $GO &
374
375 # GO=(
376 #     FUNC="dm"
377 #     TIME=""
378 #     ARGS=""
379 #     ERR=""
380 # )
381 # stage $GO &
382
383 GO=(
384     FUNC="mirrorpush"
385     TIME="mirrorpush"
386     ARGS=""
387     ERR="false"
388 )
389 stage $GO &
390
391 GO=(
392     FUNC="stats"
393     TIME="stats"
394     ARGS=""
395     ERR="false"
396 )
397 stage $GO &
398
399 rm -f "${LOCK_BRITNEY}"
400
401 GO=(
402     FUNC="cleantransactions"
403     TIME=""
404     ARGS=""
405     ERR=""
406 )
407 stage $GO &
408
409 # GO=(
410 #     FUNC="aptftpcleanup"
411 #     TIME="apt-ftparchive cleanup"
412 #     ARGS=""
413 #     ERR="false"
414 # )
415 # stage $GO
416
417 # we need to wait for the background processes before the end of dinstall
418 wait
419
420 log "Daily cron scripts successful, all done"
421
422 exec > "$logdir/afterdinstall.log" 2>&1
423
424 GO=(
425     FUNC="renamelogfile"
426     TIME=""
427     ARGS=""
428     ERR="false"
429 )
430 stage $GO
431 state "all done"
432
433
434 # Now, at the very (successful) end of dinstall, make sure we remove
435 # our stage files, so the next dinstall run will do it all again.
436 rm -f ${stagedir}/*
437 touch "${DINSTALLEND}"