Debian Codegraph @

contrib/debmirror-marshal-deltas

raw
1 #!/bin/bash
2
3 set -e
4
5 # A complex script that creates a repository of deltas, that can be
6 # used by debdelta-upgrade for upgrading packages.
7 # See also --help below.
8
9 # Copyright (C) 2006-11 Andrea Mennucci.
10 # License: GNU Library General Public License, version 2 or later
11
12 #since the output is automatically parsed, do it in English
13 unset LANG
14 unset LC_ALL
15 unset LC_NUMERIC
16
17 startgpgagent () {
18 gpg-agent --homedir "${GNUPGHOME}" --daemon --write-env-file "$GNUPGAGENTINFO" ;
19 }
20
21
22 DEBUG=''
23 VERBOSE=''
24 RM='rm'
25 MV='mv'
26 DO_MIRROR=true
27 DO_CLEAN_DELTAS=''
28 DO_CLEAN_DEBS=''
29 CONF=''
30 STARTAGENT=''
31 while [ "$1" ] ; do
32 case "$1" in
33 -h|--help)
34 cat <<EOF
35 Usage: debmirror-marshal-deltas [options] -c CONFFILE
36
37 This script keeps a full mirror of the Debian repository (using
38 'debmirror'), and it uses it to build a repository of deltas (using
39 'debdeltas').
40
41 Note that this script assumes that the current Debian
42 distributions are $DISTc .
43 It will generate deltas for the upgrades in those,
44 on arches $ARCHc , and sections $SECTIONc .
45
46 Options:
47
48 -v verbose (passed to debmirror and debdeltas)
49 -d debug (passed to debmirror and debdeltas),
50 also, do not 'rm' stuff, just tell
51 -c CONF configuration file
52 --no-mirror do not download debs to the local mirror
53 --clean-deltas clean useless deltas (see debdeltas man page)
54 --clean-debs clean older snapshots in deb mirror
55 --start-gpg-agent start gpg agent, feed the
56 passwd for the secret gpg key in it, then exit
57 EOF
58 exit ;;
59 -v) VERBOSE="$VERBOSE -v" ;;
60 -d) DEBUG=--debug ; RM='echo -- would rm ' ; MV='echo -- would mv ' ;;
61 -c) shift; CONF="$1" ;;
62 --no-mirror) DO_MIRROR='' ;;
63 --clean-deltas) DO_CLEAN_DELTAS=true ;;
64 --clean-debs) DO_CLEAN_DEBS=true ;;
65 --snapshot) echo $0 does not use its own snapshots any more ; exit ;;
66 --start-gpg-agent) STARTAGENT=true ;;
67 *) echo "$0: Unknown option $1 , try --help" ; exit 1 ;;
68 esac
69 shift
70 done
71
72 ################## read configuration
73
74 if test "$CONF" = "" ; then
75 echo "please specify a configuration file"
76 exit 1
77 fi
78 if test ! -r "$CONF" ; then
79 echo "cannot read configuration file: $CONF"
80 exit 1
81 fi
82
83 . "$CONF"
84
85 #################
86
87 if test "$STARTAGENT" ; then
88 echo ------ start agent
89 startgpgagent
90 . "$GNUPGAGENTINFO"
91 export GPG_AGENT_INFO
92 #force loading of the private key (I know no better way)
93 echo ------ sign dummy file, to load the key
94 t=`tempfile`
95 echo pippo > $t
96 gpg2 --quiet --homedir "${GNUPGHOME}" -o /dev/null --sign $t
97 rm $t
98 exit 0
99 fi
100
101 #################### set gpg stuff, test it
102
103
104
105 gpgagentcmd="$0 -c $CONF --start-gpg-agent"
106
107 # set gpg-agent variables, test it
108 if test "$GNUPGAGENTINFO" ; then
109 if test ! -r "$GNUPGAGENTINFO" ; then
110 echo ERROR no agent info, please start the agent with
111 echo $gpgagentcmd
112 exit 1
113 else
114 . "$GNUPGAGENTINFO"
115 export GPG_AGENT_INFO
116 if test ! "${GPG_AGENT_INFO}" -o ! -e "${GPG_AGENT_INFO/:*/}" -o ! -O "${GPG_AGENT_INFO/:*/}" ; then
117 echo ERROR agent info is not OK, please run the command
118 echo $gpgagentcmd
119 exit 1
120 elif ! echo | gpg-connect-agent --homedir ${GNUPGHOME} ; then
121 echo ERROR agent is not responding, please run the command
122 echo $gpgagentcmd
123 exit 1
124 fi
125 fi
126 fi
127
128
129 #test that we can sign, possibly loading the password in the agent
130 if test "$GNUPGSEC" ; then
131 t=`tempfile`
132 echo pippo > $t
133 if ! gpg2 --quiet --batch --homedir "${GNUPGHOME}" -o /dev/null --default-key $GNUPGSEC --sign $t ;
134 then
135 echo signature test FAILED
136 rm $t
137 exit 1
138 fi
139 rm $t
140 fi
141
142 ###################### some useful variables
143
144 #the lock used by debmirror
145 debmirlock=$debmir/Archive-Update-in-Progress-`hostname -f`
146
147 b=`basename $0`
148
149 mylockfile=${TMPDIR:-/tmp}/$b.lock
150 lockfile -r 10 $mylockfile || exit 1
151 trap "rm $VERBOSE -f $mylockfile" 0
152
153 log=/nonexistant
154
155 ## profile debdeltas
156 #debdeltas="python -m cProfile -o /tmp/debdelta-profile-$(dirname $p | tr / _ ) $debdeltas"
157
158 today=`date +'%F'`
159 yyyymm=`date +'%Y-%m'`
160
161 ################## routines
162
163
164
165 clean_old_deltas () {
166 find $deltamir/pool \
167 \( -name '*debdelta-fails' -or -name '*debdelta-too-big' \
168 -or -name '*debdelta' \) -mtime +${old_delete_days} -type f |\
169 xargs -r $RM $VERBOSE || true
170
171 find $deltamir/pool/ -type d -empty | xargs -r rmdir $VERBOSE || true
172 }
173
174
175
176 run_debmirror () {
177 if test -e $debmirlock ; then
178 echo Archive $debmir is locked
179 exit 1
180 fi
181
182 echo -n "---start mirroring at " >> $log ; date --utc >> $log
183
184 tm=`tempfile`
185 tme=`tempfile`
186
187 $debmirror $debmir --nosource \
188 $debmirror_opt --arch=$ARCHc \
189 --section=$SECTIONc \
190 -v $DEBUG -h $host -d $DISTc \
191 > $tm 2> $tme ; dme=$?
192
193 if test "$dme" != 0 ; then
194 echo debmirror failed , exit code $dme , stdout $tm
195 awk '{ print "> " $0 }' $tm
196 echo debmirror failed stderr $tme
197 awk '{ print "> " $0 }' $tme
198 $RM -f $debmirlock || true
199 #sometimes the temporary directory of debmirror is messed up
200 #rm -r $debmir/.temp
201 clean_old_deltas
202 rm $log
203 exit 1
204 else
205 $RM $VERBOSE $tm $tme
206 fi
207 echo -n "---end mirroring at " >> $log ; date --utc >> $log
208 }
209
210
211
212 findmelog () {
213 date=`date +'%F-%H'`
214 log=$deltamir/log/$yyyymm/${date}.log
215 if test -r $log -o -r $log.gz ; then
216 for i in 0 1 2 3 4 5 6 7 8 9 a b c d e f g h i j k l m n o p q r s t u v w x y z ; do
217 if test -r $log -o -r $log.gz ; then
218 log=$deltamir/log/$yyyymm/${date}.$i.log
219 fi
220 done
221 fi
222 }
223
224 created_deltas=0
225
226 run_debdeltas () {
227 debdeltas_errors=0
228 echo -n "--------- running debdeltas ------" >> $log ; date --utc >> $log ;
229 echo "---- options ---- $debdelta_opt " >> $log
230 for dist in $DISTs ; do
231 dist_created_deltas=0
232 distlog=`tempfile`
233 #find latest
234 pushd $debmir/dists/$dist/ > /dev/null
235 latest=$(echo [0-9] [0-9][0-9] [0-9][0-9][0-9] [0-9][0-9][0-9][0-9] \
236 | tr ' ' '\n' | sort -n | tail -n 1)
237 let prev=latest || true
238 while test "$prev" -gt 1 && cmp -s Release $prev/Release ; do
239 let prev=prev-1 || true
240 done
241 popd > /dev/null
242 echo -------------- $dist ---- >> $distlog
243 echo -n "---Release " >> $distlog ; grep ^Date $debmir/dists/$dist/Release >> $distlog
244 echo -n "---Start at " >> $distlog ; date --utc >> $distlog
245 #lenny contains debdelta 0.27, that does not understand lzma
246 if test "$dist" = lenny ; then x="--disable-feature lzma,xz" ;
247 #squeeze contains debdelta 0.39trl, that does not understand xz
248 elif test "$dist" = squeeze ; then x="--disable-feature xz" ; else x="" ; fi
249 for sec in $SECTIONs ; do for arch in $ARCHs ; do
250 sec_arch_dist_log=`tempfile`
251 sec_arch_dist_err=`tempfile`
252 wo="--old $debmir/dists/$dist/$prev/$sec/binary-$arch/Packages.gz"
253 if test "$dist" = squeeze-updates ; then
254 wo="$wo --old $debmir/dists/squeeze/$sec/binary-$arch/Packages.gz"
255 fi
256 OV=0 ; while test $OV -lt $prev ; do
257 if test -r $debmir/dists/$dist/$OV/$sec/binary-$arch/Packages.gz ; then
258 wo="$wo --old $debmir/dists/$dist/$OV/$sec/binary-$arch/Packages.gz"
259 fi
260 let OV=OV+10
261 done
262 echo "------ $sec $arch -- (debdelta options += $x $wo )" >> $sec_arch_dist_log
263 if $debdeltas $VERBOSE $debdelta_opt $x $wo --dir $deltamir// \
264 $debmir/dists/$dist/$sec/binary-$arch/Packages.gz >> $sec_arch_dist_log 2>> $sec_arch_dist_err ; then
265 #todo, only write if something happened
266 cat $sec_arch_dist_log >> $distlog
267 else
268 debdeltas_errors=$?
269 cat $sec_arch_dist_log >> $distlog
270 echo "--- debdeltas failed above, ret = ${debdeltas_errors}" >> $distlog
271 echo "--- debdeltas failed on $dist $sec $arch"
272 fi
273 if test -s "$sec_arch_dist_err" ; then
274 debdeltas_errors=11111 #make sure we keep this log
275 echo "--- debdeltas stderr " >> $distlog
276 cat $sec_arch_dist_err >> $distlog
277 echo "--- end of debdeltas stderr " >> $distlog
278 echo "--- debdeltas produced stderr on $dist $sec $arch"
279 fi
280 rm $sec_arch_dist_log $sec_arch_dist_err
281 done ; done
282
283 if grep -v '^ Total running time' $distlog | grep -v '^---' | grep -q '.' ; then
284 let created_deltas=created_deltas+1
285 dist_created_deltas=1
286 fi
287 if test "${debdeltas_errors}" -gt 0 -o "${dist_created_deltas}" -gt 0 ; then
288 #only log distributions stanzas when some deltas were generated, or errors occourred
289 cat $distlog >> $log
290 if test "${publish_deltas}" ; then
291 echo -n "--- publishing starts at " >> $log ; date --utc >> $log
292 if ${publish_deltas} ; then
293 echo -n "--- publishing ends at " >> $log ; date --utc >> $log
294 else
295 echo -n "--- publishing FAILS, at " >> $log ; date --utc >> $log
296 echo "--- publishing FAILS"
297 fi
298 fi
299 #else
300 # echo SKIPPED THIS STANZA ; cat $distlog
301 fi
302 rm $distlog
303 done
304 gzip -9 $log
305 if test "$debdeltas_errors" -gt 0 ; then
306 #some error occurred
307 echo "-----ERRORS--- full log is in $log.gz "
308 else
309 #no error occurred, clean up
310 if test "$created_deltas" = 0 ; then
311 $RM $log.gz
312 fi
313 fi
314 }
315
316
317 run_debdeltas_clean () {
318 echo -------------- cleanup delta pool
319 $debdeltas --clean-deltas -n 0 \
320 --dir $deltamir// \
321 $( for arch in $ARCHs ; do
322 for sec in $SECTIONs ; do
323 for dist in $DISTs ; do
324 echo $debmir/dists/$dist/$sec/binary-$arch/Packages.gz
325 done ; done ; done )
326 }
327
328
329 debmarshal_trim_snapshots ()
330 {
331 rectcode=1
332 for dist in $DISTs ; do
333 pushd $debmir/dists/$dist/ > /dev/null
334 #act only if there are at least 3 snapshots
335 if test -d 0 -a -d 1 -a -d 3 ; then
336 latest=$(echo [0-9] [0-9][0-9] [0-9][0-9][0-9] [0-9][0-9][0-9][0-9] \
337 | tr ' ' '\n' | sort -n | tail -n 1)
338 #check sanity
339 sane=1 ; t=0 ; f=1 ;
340 while test "$f" -le "$latest" ; do
341 if test ! -d "$f" ; then
342 echo "Warning, ignoring $debmir/dists/$dist , the snapshot $f does not exist"
343 sane=0
344 elif test $t -nt $f ; then
345 echo "Warning, ignoring $debmir/dists/$dist , the snapshot $f is older than the $t "
346 sane=0
347 fi
348 let f++ ; let t++ ;
349 done
350 # if sane and too many snapshots, move all down a step
351 if test "$sane" = 1 -a "$keep_marshal_snapshots" -a "$latest" -gt "$keep_marshal_snapshots" ; then
352 retcode=0
353 let d=latest-keep_marshal_snapshots
354 t=0
355 #remove older
356 while test "$t" -le "$d" -a -d "$d" ; do $RM -r $t ; let t++ ; done
357 #move down others
358 t=0
359 let f=d+1
360 while test "$f" -le "$latest" ; do
361 $MV $f $t
362 let t++
363 let f++
364 done
365 let t--
366 rm latest
367 ln -s $t latest
368 echo "------------ deleted revisions from 0 to $d in $dist (latest now points to $t)"
369 fi # end of " if sane and too many snapshots"
370 fi #end of "if at least 3 snapshots"
371 popd > /dev/null
372 done
373 return $retcode
374 }
375
376 clean_old_debs () {
377 if test -e $debmirlock ; then
378 echo Archive $debmir is locked
379 exit 1
380 fi
381 echo -------------- cleanup mirror
382 if debmarshal_trim_snapshots ; then
383 z=`tempfile`
384 ${debmarshal_list_useless_debs} $debmir > $z
385 ! grep -vx '.*\.deb' $z #(just in case)
386 n=$(wc -l < $z)
387 if test $n -lt $max_delete ; then
388 echo "--- delete $n debs"
389 tr '\n' '\000' < $z | xargs -r0 $RM
390 rm $z
391 else
392 echo "--- will not delete $n debs (too many!), the list is in $z"
393 fi
394 fi
395 }
396
397
398 ########################################### code
399
400
401 if test `stat -f -c "%a" "$deltamir"` -le 512 ; then
402 echo "------- emergency delta pool cleanup , very low disk space in delta mirror"
403 run_debdeltas_clean
404 clean_old_deltas
405 elif test "${DO_CLEAN_DELTAS}" ; then
406 run_debdeltas_clean
407 clean_old_deltas
408 fi
409
410 if test `stat -f -c "%a" "$debmir"` -le 512 ; then
411 echo "------- emergency debs pool cleanup , very low disk space in debs mirror"
412 clean_old_debs
413 elif test "${DO_CLEAN_DEBS}" ; then
414 clean_old_debs
415 fi
416
417
418 test -d $deltamir/log/$yyyymm || mkdir $deltamir/log/$yyyymm
419
420 findmelog
421
422 if [ "${DO_MIRROR}" ] ; then
423 run_debmirror
424 else
425 echo ---------- skipped mirror step
426 fi
427
428 #the mirror was updated, so we check the difference
429 run_debdeltas