cookutils view cooker @ rev 697

cookutils: catch control-c in aufs jail (again)
author Pascal Bellard <pascal.bellard@slitaz.org>
date Sat Oct 25 11:27:23 2014 +0200 (2014-10-25)
parents 00c89537b9d4
children da66e6be1add
line source
1 #!/bin/sh
2 #
3 # SliTaz Build Bot. The Cooker is a tool to automate and test SliTaz package
4 # building. Please read the Cookbook documentation for more information
5 # and discuss with the AUTHORS before adding anything here. PS: no translations
6 # here since it's not an end user tool and it's not useful. All devs should
7 # at least understand basic English.
8 #
9 . /usr/lib/slitaz/libcook.sh
11 # Set pkg name and use same wok as cook.
12 pkg="$2"
13 wok="$WOK"
15 # PID file.
16 pidfile='/var/run/cooker.pid'
18 #
19 # Functions
20 #
22 usage() {
23 cat << EOT
25 Usage: cooker [command] [pkg|list|note|hours]
27 Options:
28 usage|-u Display this short usage.
29 setup|-s Setup the Cooker environment.
30 setup-cron Setup a cron job for the Cooker.
31 arch-db Create host arch packages DB.
32 note|-n Add a note to the cooknotes.
33 notes|-ns Display all the cooknotes.
34 block|-b Block a package so cook will skip it.
35 unblock|-ub Unblock a blocked package.
36 pkg|-p Same as 'cook pkg' but with cooker log.
37 flavor|-f Cook all packages of a flavor.
38 list|-l Cook all packages in the given list.
39 cat|-c Cook all packages of a category.
40 rev|-r Cook packages of a specific revision.
41 all|-a Find and cook all unbuilt packages.
43 EOT
44 exit 0
45 }
47 # Some messages occur in activity but log verbose output when checking for commits
48 # into a log file.
49 log_commits() {
50 sed '/^.\//'d | sed '/^.hg/'d | tee -a $LOGS/commits.log
51 }
53 # Clean up before exit when check and cook commits finish.
54 clean_exit() {
55 rm -f $command && touch $command
56 rm -f $pidfile
57 }
59 # Summary for commits.
60 commits_summary() {
61 msg="from revision $cur to $new"
62 [ "$new" == "$cur" ] && msg="revision $new"
63 echo "Will cook $msg"
64 separator
65 echo -e "\nSummary for commits"
66 separator
67 echo "Hg wok revision : $cur"
68 echo "Pulled revision : $new"
69 echo "Check date : $(date '+%Y-%m-%d %H:%M:%S')"
70 }
72 # Scan packages build deps and fill up cookorder list.
73 cook_order_scan() {
74 rm -f $cookorder
75 touch $cookorder
76 for pkg in $(cat $cooklist)
77 do
78 unset WANTED BUILD_DEPENDS
79 . $wok/$pkg/receipt
80 # The :: is for web interface color.
81 [ "$WANTED$BUILD_DEPENDS" ] && echo "$pkg :: $WANTED $BUILD_DEPENDS"
82 for dep in $WANTED $BUILD_DEPENDS
83 do
84 if grep -q "^$dep$" $cooklist; then
85 if ! grep -q "^$dep$" $cookorder; then
86 echo "$dep" >> $cookorder
87 fi
88 fi
89 done
90 done
92 # Append unordered packages to cookorder.
93 for pkg in $(cat $cooklist)
94 do
95 if ! grep -q "^$pkg$" $cookorder; then
96 echo "$pkg" >> $cookorder
97 fi
98 done
99 }
101 # Scan and rescan until the cooklist is ordered then handle WANTED.
102 cook_order() {
103 time=$(date +%s)
104 scan=0
106 # Keep an original cooklist so we do a diff when ordering is finished.
107 cp -f $cooklist $cooklist.0
108 echo "cookorder" > $command
109 echo -e "\nInitial Cooker order scan"
110 separator
111 cook_order_scan
113 # Diff between the cooklist and new ordered list ? So copy the last
114 # cookorder to cooklist and rescan it.
115 while /bin/true
116 do
117 diff $cooklist $cookorder > $cookorder.diff
118 if [ -s "$cookorder.diff" ]; then
119 scan=$(($scan + 1))
120 echo -e "\nDiff scan: $scan"
121 separator
122 mv -f $cookorder $cooklist
123 cook_order_scan
124 else
125 break
126 fi
127 done
129 # Keep a diff between submitted cooklist and the ordered.
130 diff $cooklist.0 $cooklist > $cooklist.diff
131 rm -f $cookorder $cookorder.diff $cooklist.0
133 # Scan finished: append pkg to WANTED or leave it in the ordered cooklist.
134 # TODO: grep the line number to get pkg position and keep it higher.
135 echo -e "\nHandle WANTED package"
136 separator
137 for pkg in $(cat $cooklist)
138 do
139 unset WANTED
140 . $wok/$pkg/receipt
141 for wanted in $WANTED
142 do
143 echo "$pkg :: $wanted"
144 if grep -q ^${wanted}$ $cooklist; then
145 sed -i -e "/^$pkg$/"d \
146 -e "/^$wanted$/ a $pkg" $cooklist
147 fi
148 done
149 done
151 # Show ordered cooklist
152 echo -e "\nCooklist order"
153 separator
154 cat $cooklist
155 separator
156 time=$(($(date +%s) - $time))
157 pkgs=$(cat $cooklist | wc -l)
158 echo -e "\nSummary for cookorder"
159 separator
160 cat << EOT
161 Ordered packages : $pkgs
162 Scans executed : $scan
163 Scan duration : ${time}s
164 EOT
165 separator && rm -f $command
166 }
168 # Remove blocked (faster this way than grepping before).
169 strip_blocked() {
170 for pkg in $(cat $blocked)
171 do
172 sed -i /^${pkg}$/d $cooklist
173 done && sed -i /^$/d $cooklist
174 }
176 # Use in default mode and with all cmd.
177 cook_commits() {
178 if [ -s "$commits" ]; then
179 for pkg in $(cat $commits)
180 do
181 echo "cook:$pkg" > $command
182 cook $pkg || broken
183 sed -i /^${pkg}$/d $commits
184 done
185 fi
186 }
188 # Cook all packages in a cooklist.
189 cook_list() {
190 for pkg in $(cat $cooklist)
191 do
192 cook $pkg || broken
193 sed -i /^${pkg}$/d $cooklist
194 done
195 }
197 # Create a arch.$ARCH file for each package cooked for the target host
198 # architecture
199 #
200 # The deal: we dont want all packages handled by cooker commands and stats,
201 # since we dont cross compile all packages for each arch but only a set of
202 # packages to provide one full featured desktop, servers and goodies useful
203 # for the host system.
204 #
205 arch_db() {
206 count=0
207 echo "Cleaning packages DB : arch.$ARCH"
208 rm -f $wok/*/arch.$ARCH && cd $wok
209 echo "Creating $ARCH packages DB..."
210 for pkg in *
211 do
212 HOST_ARCH=
213 . $wok/$pkg/receipt
214 if [ -n "$HOST_ARCH" ]; then
215 if $(echo "$HOST_ARCH" | egrep -q "$ARCH|any"); then
216 count=$(($count + 1))
217 echo "Adding: $pkg"
218 touch $pkg/arch.$ARCH
219 fi
220 unset HOST_ARCH
221 else
222 # HOST_ARCH not set --> in i486
223 if [ "$ARCH" == "i486" ]; then
224 count=$(($count + 1))
225 echo "Adding: $pkg"
226 touch $pkg/arch.$ARCH
227 fi
228 fi
229 done
230 echo "Packages for $ARCH : $count"
231 }
233 #
234 # Commands
235 #
236 case "$1" in
237 usage|help|-u|-h)
238 usage ;;
239 setup|-s)
240 # Setup the Cooker environment.
241 echo -e "\nSetting up the Cooker"
242 mkdir -p $CACHE
243 echo "Cooker setup using: $SLITAZ" | log
244 separator
245 for pkg in $SETUP_PKGS mercurial rsync tazlito
246 do
247 [ ! -d "$INSTALLED/$pkg" ] && tazpkg get-install $pkg
248 done
249 mkdir -p $SLITAZ && cd $SLITAZ
250 [ -d "${wok}-hg" ] && echo -e "Hg wok already exists.\n" && exit 1
251 [ -d "$wok" ] && echo -e "Build wok already exists.\n" && exit 1
253 # Directories and files
254 echo "mkdir's and touch files in: $SLITAZ"
255 mkdir -p $PKGS $LOGS $FEEDS $CACHE $SRC
256 for f in $activity $blocked $broken $commits $cooklist $command
257 do
258 touch $f
259 done
260 hg clone $WOK_URL ${wok}-hg || exit 1
261 [ -d "$flavors" ] || hg clone $FLAVORS_URL flavors
262 cp -a ${wok}-hg $wok
263 separator && newline ;;
264 arch-db)
265 # Manually create arch packages DB.
266 arch_db ;;
267 setup-cron)
268 # Create cron job for the cooker.
269 [ "$2" ] || hours=2
270 if [ ! -f "$crontabs" ]; then
271 mkdir -p /var/spool/cron/crontabs
272 fi
273 if ! fgrep -q /usr/bin/cooker $crontabs; then
274 echo "# Run SliTaz Cooker every $hours hours" > $crontabs
275 echo "0 */$hours * * * /usr/bin/cooker --output=html" >> $crontabs
276 killall crond 2>/dev/null && /etc/init.d/crond start
277 fi ;;
278 check-cron)
279 [ -f "$crontabs" ] || \
280 echo "There is no $crontabs here. Use setup-cron option." && exit 1
281 fgrep /usr/bin/cooker $crontabs ;;
282 note|-n)
283 # Blocked a pkg and want others to know why ? Post a note!
284 note="$2"
285 date=$(date "+%Y-%m-%d %H:%M")
286 [ "$note" ] && echo "$date : $note" >> $cooknotes ;;
287 notes|-ns)
288 # View cooknotes.
289 echo -e "\nCooknotes"
290 separator
291 cat $cooknotes
292 separator && newline ;;
293 block|-b)
294 # Block a package.
295 [ "$pkg" ] && cook $pkg --block ;;
296 unblock|-ub)
297 # Unblock a package.
298 [ "$pkg" ] && cook $pkg --unblock ;;
299 reverse|-r)
300 # Cook all reverse dependencies for a package. This command lets us
301 # control the Cooker manually for commits that will cook a lot of packages.
302 #
303 # Use hg commit ? Ex: hg commit -m "Message bla bla | cooker:reverse"
304 #
305 [ ! -d "$wok/$pkg" ] && echo -e "\nNo package $2 found.\n" && exit 0
306 rm -f $cooklist && touch $cooklist && cd $wok
307 echo -e "\nReverse cooklist for: $pkg"
308 separator && cd $wok
309 for rev in *
310 do
311 unset WANTED DEPENDS BUILD_DEPENDS && . $wok/$rev/receipt
312 if echo "$WANTED $DEPENDS $BUILD_DEPENDS" | fgrep -q $pkg; then
313 echo "$rev" | tee -a $cooklist
314 fi
315 done && separator
316 echo -e "Reverse dependencies found: $(cat $cooklist | wc -l)\n"
317 strip_blocked
318 cook_order | tee $LOGS/cookorder.log
319 cook_list ;;
320 pkg|-p)
321 # Same as 'cook pkg' but with log for web interface.
322 cook $pkg || broken
323 clean_exit ;;
324 cat|-c)
325 # Cook all packages of a category.
326 cat="$2"
327 rm -f $cooklist && touch $cooklist && cd $wok
328 for pkg in *
329 do
330 unset CATEGORY && . $pkg/receipt
331 [ "$CATEGORY" == "$cat" ] && echo $pkg >> $cooklist
332 done
333 strip_blocked
334 cook_order | tee $LOGS/cookorder.log
335 cook_list ;;
336 flavor|-f)
337 # Cook all packages of a flavor.
338 name="$2"
339 [ ! -d "$flavors/$name" ] && \
340 echo -e "\nSpecified flavor does not exist: $name\n" && exit 1
341 [ -d "$flavors/.hg" ] && cd $flavors && hg pull -u
342 list=$flavors/$name/packages.list
343 cp -a $list $cooklist
344 strip_blocked
345 cook_order | tee $LOGS/cookorder.log
346 cook_list ;;
347 list|-l)
348 # Cook a list of packages given in argument.
349 list="$2"
350 [ ! -f "$list" ] && \
351 echo -e "\nSpecified list does not exist: $list\n" && exit 1
352 cp -a $list $cooklist
353 strip_blocked
354 cook_order | tee $LOGS/cookorder.log
355 cook_list ;;
356 rev|-r)
357 # Cook or recook a specific Hg revision.
358 rev="$2"
359 [ "$rev" ] || exit 0
360 cd $wok
361 rm -f $cooklist && touch $cooklist
362 for pkg in $(hg log --rev=$rev --template "{files}")
363 do
364 echo "$pkg" | cut -d "/" -f 1 >> $cooklist
365 done
366 strip_blocked
367 cook_order | tee $LOGS/cookorder.log
368 cook_list ;;
369 all|-a)
370 # Try to build all unbuilt packages except blocked's.
371 echo "cooker:all" > $command
372 rm -f $cooklist && touch $cooklist
373 newline && cd $wok
374 echo "Cooker cooklist"
375 separator
377 # Find all unbuilt packages. Get EXTRAVERSION from packed receipt
378 # if it exists since extra version is added when packing the package.
379 echo "Searching for all unbuilt packages" | log
380 for pkg in *
381 do
382 unset EXTRAVERSION
383 . $pkg/receipt
384 [ -f "$pkg/taz/$PACKAGE-$VERSION/receipt" ] && \
385 . $pkg/taz/$PACKAGE-$VERSION/receipt
386 if [ ! -f "$PKGS/$PACKAGE-${VERSION}${EXTRAVERSION}.tazpkg" ]
387 then
388 echo $pkg && echo $pkg >> $cooklist
389 fi
390 done
391 strip_blocked
392 cook_order | tee $LOGS/cookorder.log
393 echo "Packages to cook: $(cat $cooklist | wc -l)" | log
394 cook_list ;;
395 *)
396 # Default is to cook all commits if not yet running.
397 [ "$1" ] && usage
398 cooklist=$commits
399 if [ -f "$pidfile" ]; then
400 pid=$(cat $pidfile)
401 if [ -s /proc/$pid/status ]; then
402 gettext -e "\nStill cooking latest commits with pid:"
403 echo -e " $pid\n" && exit 0
404 fi
405 rm -f "$pidfile"
406 fi
408 # Start and get a PID file.
409 rm -f $LOGS/commits.log
410 newline
411 echo "Checking for commits" | log_commits
412 separator | tee -a $LOGS/commits.log
414 echo $$ > $pidfile
415 trap 'echo -e "\nCooker stopped: PID $$\n" && \
416 rm -f $pidfile $command && exit 1' INT TERM
418 echo "Cooker PID : $$" | log_commits
419 echo "Cooker date : $(date '+%Y-%m-%d %H:%M:%S')" | log_commits
421 # Get revisions. Here we have 2 echoes since we want a msg on screen,
422 # in commits log and activity DB without a space before.
423 cd $wok || exit 1
424 cur=$(hg head --template '{rev}\n')
425 echo "Updating wok : ${wok}-hg (rev $cur)" | log_commits
426 echo "Updating wok: ${wok}-hg" | log
427 echo "hg:pull" > $command
428 cd ${wok}-hg && hg pull -u | log_commits
429 new=$(hg head --template '{rev}\n')
430 # Store last rev to be used by CGI so it doesn't need to call hg head
431 # on each load.
432 echo "$new" > $wokrev
434 # Sync build wok with rsync so we don't take care about removing old
435 # files as before.
436 if [ "$new" -gt "$cur" ]; then
437 echo "Changes found from: $cur to $new" | log
438 echo "Syncing build wok with Hg wok..." | log_commits
439 rsync -r -t -c -l -u -v -D -E ${wok}-hg/ $wok/ | \
440 sed '/^$/'d | log_commits
441 else
442 echo "No revision changes: $cur vs $new" | log
443 separator | log_commits
444 clean_exit && newline && exit 0
445 fi
447 # Get and display modifications.
448 cd ${wok}-hg
449 commits_summary | log_commits
450 cur=$(($cur + 1))
451 rm -f $commits.tmp && touch $commits.tmp
452 for rev in $(seq $cur $new)
453 do
454 for file in $(hg log --rev=$rev --template "{files}")
455 do
456 pkg=$(echo $file | cut -d "/" -f 1)
457 desc=$(hg log --rev=$rev --template "{desc}" $file)
458 echo "Committed package : $pkg - $desc" | log_commits
459 echo $pkg >> $commits.tmp
460 done
461 done
463 # We may have deleted packages and files in stuff/. Remove it and
464 # clean DB as well as log file.
465 cd $wok
466 for pkg in *
467 do
468 if [ ! -d "${wok}-hg/$pkg" ]; then
469 echo "Removing package: $pkg" | log_commits
470 . $wok/$pkg/receipt
471 rm -rf $PKGS/$PACKAGE-$VERSION* $wok/$pkg $LOGS/$pkg.log
472 sed -i "/^${pkg}$/"d $blocked $broken $commits.tmp
473 fi
474 done
476 # Keep previous commit and discard duplicate lines
477 cat $commits $commits.tmp | sed /"^$"/d > $commits.new
478 uniq $commits.new > $commits && rm $commits.*
480 # Handle cross compilation. Create arch packages DB and remove pkgs
481 # not cooked for this arch from the commits list.
482 arch_db
483 for pkg in $(cat $commits)
484 do
485 if [ ! -f "$wok/$pkg/arch.$ARCH" ]; then
486 echo "Cooker arch : skip $pkg (not included in: $ARCH)" | \
487 log_commits
488 sed -i "/^${pkg}$/"d $commits
489 else
490 echo "Cooker arch : $ARCH" | log_commits
491 fi
492 done
494 # Stats
495 pkgs=$(cat $commits | wc -l)
496 echo "Packages to cook: $pkgs" | log
497 echo "Packages to cook : $pkgs" | log_commits
498 separator | log_commits
499 newline
500 strip_blocked
501 cook_order | tee $LOGS/cookorder.log
502 cook_commits
503 clean_exit ;;
504 esac
506 exit 0