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