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