]> git.decadent.org.uk Git - dak.git/blob - config/debian/common
Don't generate the cruft report in-place
[dak.git] / config / debian / common
1 # -*- mode:sh -*-
2 # log something (basically echo it together with a timestamp)
3 #
4 # Set $PROGRAM to a string to have it added to the output.
5 function log () {
6     local prefix=${PROGRAM:-}
7     echo "$(date +"%b %d %H:%M:%S") ${HOSTNAME} ${prefix}[$$]: $*"
8 }
9
10 # log the message using log() but then also send a mail
11 # to the address configured in MAILTO (if non-empty)
12 function log_error () {
13     log "$@"
14     if [[ -z ${MAILTO} ]]; then
15         echo "$*" | mail -a "X-Debian: DAK" -e -s "[$PROGRAM@${HOSTNAME}] ERROR [$$]" -a "From: Debian FTP Masters <ftpmaster@ftp-master.debian.org>" ${MAILTO}
16     fi
17 }
18
19 # debug log, only output when DEBUG=1
20 function debug () {
21     if [[ $DEBUG -eq 1 ]]; then
22         log "$*"
23     fi
24 }
25
26 # Get a tempfile, add it to the right variable to get rid of it,
27 # and return it
28 function gettempfile() {
29     local MAKEDIR=${1:-false}
30     local TMPARGS=""
31     if [[ ${MAKEDIR} == true ]]; then
32         TMPARGS="--directory"
33     fi
34     local TMPFILE=$( mktemp -p ${TMPDIR} ${TMPARGS} )
35     TMPFILES="${TEMPFILES} ${TMPFILE}"
36     echo "${TMPFILE}"
37 }
38
39 # Function that only cleans tempfiles, but does not exit or otherwise
40 # care about any exit status
41 function cleantempfiles() {
42     resolvetmpfiles
43     for TEMPFILE in $TMPFILES; do
44         if [[ -n ${TEMPFILE} ]] && [[ -f ${TEMPFILE} ]]; then
45             rm -f "${TEMPFILE}"
46         elif [[ -n ${TEMPFILE} ]] && [[ -d ${TEMPFILE} ]]; then
47             if [[ ${TEMPFILE} != / ]] && [[ ${TEMPFILE} != /* ]]; then
48                 rm -rf "${TEMPFILE}"
49             fi
50         fi
51     done
52     TMPFILES=""
53 }
54
55 function resolvetmpfiles() {
56     # If you don't understand this better not touch the script
57     for TEMPFILE in $TEMPFILES; do
58         TMPFILES="${TMPFILES} ${!TEMPFILE:-""}"
59     done
60     TEMPFILES=""
61 }
62
63 # Function cleanup
64 # No arguments
65 # Cleans up any known tempfile.
66 # Just ensure your script sets the variable
67 # TEMPFILES to the names of variables of tempfiles
68 # Or TMPFILES to the pathes of tempfiles
69 function cleanup() {
70     ERRVAL=$?
71     trap - ERR EXIT TERM HUP INT QUIT
72
73     cleantempfiles
74
75     return $ERRVAL
76 }
77 TEMPFILES=${TEMPFILES:-""}
78 TMPFILES=${TMPFILES:-""}
79
80 # Timestamp. Used for dinstall stat graphs
81 function ts() {
82     echo "Archive maintenance timestamp ($1): $(date +%H:%M:%S)"
83 }
84
85 ########################################################################
86 ########################################################################
87
88 function wbtrigger() {
89     SSHOPT="-n -o BatchMode=yes -o ConnectTimeout=30 -o SetupTimeout=240"
90     if lockfile -r 3 -l 3600 "${LOCK_BUILDD}"; then
91         ssh -q -q ${SSHOPT} wbadm@buildd /srv/wanna-build/trigger.often
92     fi
93     rm -f "${LOCK_BUILDD}"
94 }
95
96 # used by cron.dinstall *and* cron.unchecked.
97 function make_buildd_dir () {
98     # We generate straight into the static mirror location for incoming
99     log "Preparing buildd area"
100     dak manage-build-queues -a
101     dak generate-packages-sources2 -a build-queues
102     dak generate-releases -a build-queues >/dev/null
103
104     # Stick a last modified date in the page footer
105     echo "<p>Last updated: $(date -u)</p>" > ${incoming}/web/README.html
106
107     # Tell the mirrors that we've updated
108     log "Pushing static for incoming.d.o"
109     chronic /usr/local/bin/static-update-component incoming.debian.org < /dev/null
110 }
111
112 # Process (oldstable)-proposed-updates "NEW" queue
113 function punew_do() {
114     local queue="$1"
115     local qdir="$2"
116     local to="${3}"
117
118     date -u -R >> REPORT
119     dak process-policy "${queue}" | tee -a REPORT | mail -a "X-Debian: DAK" -e -s "NEW changes in ${queue}" -a "From: Debian FTP Masters <ftpmaster@ftp-master.debian.org>" "${to}"
120     echo >> REPORT
121
122     dak generate-packages-sources2 -s "${queue}"
123
124     STAMP=${STAMP:-$(date "+%Y%m%d%H%M")}
125
126     local exportdir="${qdir}/tree/${STAMP}"
127     local targetdir="${qdir}/export"
128     mkdir -p -- ${exportdir}
129     dak export -q "${queue}" -d "${exportdir}" --all
130     ln -sfT ${exportdir} ${targetdir}
131     find "${qdir}/tree" -mindepth 1 -maxdepth 1 -not -name "${STAMP}" -type d -print0 | xargs --no-run-if-empty -0 rm -rf
132 }
133
134 # These versions used in dinstall
135 function punew() {
136     log "Doing automated p-u-new processing"
137     cd "${queuedir}/p-u-new"
138     punew_do "$1" "${queuedir}/p-u-new" "debian-release@lists.debian.org"
139 }
140
141 function opunew() {
142     log "Doing automated o-p-u-new processing"
143     cd "${queuedir}/o-p-u-new"
144     punew_do "$1" "${queuedir}/o-p-u-new" "debian-release@lists.debian.org"
145 }
146
147 function backports_policy() {
148     local queue="backports-policy"
149     local qdir="/srv/backports-master.debian.org/queue/policy"
150     local to="backports-team@debian.org"
151
152     log "Doing automated ${queue} processing"
153
154     cd "${qdir}"
155     punew_do "${queue}" "${qdir}" "${to}"
156 }
157
158 # Do the unchecked processing, in case we have files.
159 function do_unchecked () {
160     cd $unchecked
161
162     changes=$(find . -maxdepth 1 -mindepth 1 -type f \( -name \*.changes -o -name \*.dak-commands \) | sed -e "s,./,," | xargs)
163     report=${queuedir}/REPORT
164     timestamp=$(date "+%Y-%m-%d %H:%M")
165
166     if [[ ! -z ${changes} ]]; then
167         log "Processing files ${changes}"
168         {
169             echo "${timestamp}: ${changes}"
170             dak process-upload -a -d "$unchecked"
171             dak process-commands -d "$unchecked"
172         } >> ${report}
173
174         sync_debbugs
175         do_buildd
176     else
177         log "Nothing to do"
178         echo "Nothing to do" >> ${report}
179     fi
180 }
181
182 # process NEW policy queue
183 function do_new () {
184     log "Doing NEW processing"
185     (dak process-policy new; dak process-policy byhand) | mail -a "X-Debian: DAK" -e -s "NEW and BYHAND processing" -a "From: Debian FTP Masters <ftpmaster@ftp-master.debian.org>" ftpmaster@ftp-master.debian.org
186
187     log "Processing Backports NEW"
188     dak process-policy backports-new | mail -a "X-Debian: DAK" -e -s "NEW processing for backports-new" -a "From: Debian FTP Masters <ftpmaster@ftp-master.debian.org>" backports-team@debian.org
189
190     log "Cleanup NEW/Backports NEW"
191     dak clean-suites -a new,backports-new
192 }
193
194 function sync_debbugs () {
195     # sync with debbugs
196     log "Sync debbugs version tracking information"
197     echo "--" >> $report
198     timestamp=$(date "+%Y-%m-%d-%H:%M")
199     mkdir -p $queuedir/bts_version_track_archive/${timestamp}
200     rsync -aq $queuedir/bts_version_track/ $queuedir/bts_version_track_archive/${timestamp}
201     rmdir --ignore-fail-on-non-empty $queuedir/bts_version_track_archive/${timestamp} # remove if empty.
202     rsync -aq -e "ssh -o Batchmode=yes -o ConnectTimeout=30 -o SetupTimeout=30" --remove-source-files  $queuedir/bts_version_track/ bugs-sync:/srv/bugs.debian.org/versions/queue/ftp-master/ 2>/dev/null && touch $lockdir/synced_bts_version || true
203     NOW=$(date +%s)
204     TSTAMP=$(stat -c %Y $lockdir/synced_bts_version)
205     DIFF=$(( NOW - TSTAMP ))
206     if [[ $DIFF -ge 259200 ]]; then
207         log_error "Kids, you tried your best and you failed miserably. The lesson is, never try. (Homer Simpson)"
208     fi
209 }
210
211 function clean_debbugs () {
212     log "Cleanup debbugs"
213     # Delete files older than 60 days
214     find $queuedir/bts_version_track_archive/ -mtime +60 -type f -delete
215     # Delete empty directories
216     find $queuedir/bts_version_track_archive/ -empty -type d -delete
217 }
218
219 function reports() {
220     # Send a report on NEW/BYHAND packages
221     log "Nagging ftpteam about NEW/BYHAND packages"
222     dak queue-report | mail -a "X-Debian: DAK" -e -s "NEW and BYHAND on $(date +%D)" -a "From: Debian FTP Masters <ftpmaster@ftp-master.debian.org>" ftpmaster@ftp-master.debian.org
223     dak queue-report -d backports-new,backports-policy | mail -a "X-Debian: DAK" -e -s "NEW and POLICY on $(date +%D)" -a "From: Debian FTP Masters <ftpmaster@ftp-master.debian.org>" backports-team@debian.org
224     # and one on crufty packages
225     log "Sending information about crufty packages"
226     dak cruft-report -R > $webdir/cruft-report-daily.txt.new
227     dak cruft-report -R -s experimental >> $webdir/cruft-report-daily.txt.new
228     mv $webdir/cruft-report-daily.txt.new $webdir/cruft-report-daily.txt
229     mail -a "X-Debian: DAK" -e -s "Debian archive cruft report for $(date +%D)" -a "From: Debian FTP Masters <ftpmaster@ftp-master.debian.org>" ftpmaster@ftp-master.debian.org < $webdir/cruft-report-daily.txt
230 }
231
232 function pg_timestamp() {
233     tsname=${1:-"unknown"}
234     log "Saving postgres transaction id for ${tsname}"
235     psql -tAc 'select txid_current();' > $base/backup/txid_${tsname}_$(date +%Y.%m.%d-%H:%M:%S)
236 }
237
238 function get_archiveroot() {
239     local archivename="$1"
240     local query="SELECT path FROM archive WHERE name='${archivename}'"
241     local archiveroot="$(psql -tAc "${query}")"
242     if [[ -z ${archiveroot} ]]; then
243         echo "get_archiveroot: couldn't get archiveroot for '${archivename}'" >&2
244         return 1
245     fi
246     echo "${archiveroot}"
247 }
248
249 # Prepare the trees for buildds, then push wanna-build
250 function do_buildd() {
251     if lockfile -r3 ${LOCK_DAILY}; then
252         TMPFILES="${TMPFILES} ${LOCK_DAILY}"
253         make_buildd_dir
254         wbtrigger
255     fi
256 }
257
258 # Cleanup policy queues
259 function cleanpolicy() {
260     dak clean-suites -a backports-policy,policy
261 }
262
263 # Scan new packages for contents
264 function scancontents() {
265     dak contents -l 10000 scan-binary
266     dak contents -l 1000 scan-source
267 }
268
269 function ddaccess() {
270     # Tell our dd accessible mirror to sync itself up.
271     log "Trigger dd accessible parts sync"
272     ${scriptsdir}/sync-dd dd-sync dd-sync1 dd-sync2 sync
273 }
274
275
276
277 ########################################################################
278 ########################################################################
279 ########################################################################
280 ########################################################################
281
282 # Function to save which stage we are in, so we can restart an interrupted
283 # dinstall. Or even run actions in parallel, if we dare to, by simply
284 # backgrounding the call to this function. But that should only really be
285 # done for things we don't care much about.
286 #
287 # This should be called with the first argument being an array, with the
288 # members
289 #  - FUNC - the function name to call
290 #  - ARGS - Possible arguments to hand to the function. Can be the empty string
291 #  - TIME - The timestamp name. Can be the empty string
292 #  - ERR  - if this is the string false, then the call will be surrounded by
293 #           set +e ... set -e calls, so errors in the function do not exit
294 #           dinstall. Can be the empty string, meaning true.
295 #
296 # MAKE SURE TO KEEP THIS THE LAST FUNCTION, AFTER ALL THE VARIOUS ONES
297 # ADDED FOR DINSTALL FEATURES!
298 function stage() {
299     ARGS='GO[@]'
300     local "${!ARGS}"
301
302     local error=${ERR:-"true"}
303
304     ARGS=${ARGS:-""}
305
306     log "########## ${PROGRAM} BEGIN: ${FUNC} ${ARGS} ##########"
307     local STAGEFILE="${stagedir}/${FUNC}${ARGS:+_}${ARGS}"
308     STAGEFILE=${STAGEFILE// /_}
309     if [[ -f ${STAGEFILE} ]]; then
310         local stamptime=$(/usr/bin/stat -c %Z "${STAGEFILE}")
311         local unixtime=$(date +%s)
312         local difference=$(( unixtime - stamptime ))
313         if [[ ${difference} -ge 14400 ]]; then
314             log_error "Did already run ${FUNC}, stagefile exists, but that was ${difference} seconds ago. Please check."
315         else
316             log "Did already run ${FUNC}, not calling again..."
317         fi
318         return
319     fi
320
321     debug "Now calling function ${FUNC}. Arguments: ${ARGS}. Timestamp: ${TIME}"
322
323     # Make sure we are always at the same place. If a function wants
324     # to be elsewhere, it has to cd first!
325     cd ${configdir}
326
327     # Now redirect the output into $STAGEFILE.log. In case it errors
328     # out somewhere our errorhandler trap can then mail the contents
329     # of $STAGEFILE.log only, instead of a whole ${PROGRAM} logfile.
330     # Short error mails ftw!
331     exec >> "${STAGEFILE}.log" 2>&1
332
333     if [[ -f ${LOCK_STOP} ]]; then
334         log "${LOCK_STOP} exists, exiting immediately"
335         exit 42
336     fi
337
338     # Do we care about trouble in the function we call?
339     if [[ ${error} == false ]]; then
340         set +e
341     fi
342     ${FUNC} ${ARGS}
343
344     # No matter what happened in the function, we make sure we have
345     # set -e default state back
346     set -e
347
348     # Make sure we are always at the same place.
349     cd ${configdir}
350
351     # We always use the same umask. If a function wants to do
352     # different, fine, but we reset.
353     umask 022
354
355     touch "${STAGEFILE}"
356
357     if [[ -n ${TIME} ]]; then
358         ts "${TIME}"
359     fi
360
361     # And the output goes back to the normal logfile
362     exec >> "${LOGFILE}" 2>&1
363
364     # Now we should make sure that we have a usable ${PROGRAM}.log, so
365     # append the $STAGEFILE.log to it.
366     if [[ ${TIMESTAMP} == true ]]; then
367         /usr/bin/ts "%b %d %H:%M:%S ${HOSTNAME} ${PROGRAM}[$$]: ${FUNC} " < "${STAGEFILE}.log"
368     else
369         cat "${STAGEFILE}.log"
370     fi
371     rm -f "${STAGEFILE}.log"
372
373     log "########## ${PROGRAM} END: ${FUNC} ##########"
374
375     if [[ -f ${LOCK_STOP} ]]; then
376         log "${LOCK_STOP} exists, exiting immediately"
377         exit 42
378     fi
379 }