cookutils view cooker @ rev 773

cooker: recook button support
author Pascal Bellard <pascal.bellard@slitaz.org>
date Fri Nov 13 14:57:30 2015 +0100 (2015-11-13)
parents a537875d79f9
children 2185b741fba9
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 #
10 . /usr/lib/slitaz/libcook.sh
12 # Set pkg name and use same wok as cook.
13 pkg="$2"
14 wok="$WOK"
16 # PID file.
17 pidfile='/var/run/cooker.pid'
19 #
20 # Functions
21 #
23 usage() {
24 cat <<EOT
26 Usage: cooker [command] [pkg|list|note|hours]
28 Options:
29 usage|-u Display this short usage.
30 setup|-s Setup the Cooker environment.
31 setup-cron Setup a cron job for the Cooker.
32 arch-db Create host arch packages DB.
33 note|-n Add a note to the cooknotes.
34 notes|-ns Display all the cooknotes.
35 block|-b Block a package so cook will skip it.
36 unblock|-ub Unblock a blocked package.
37 pkg|-p Same as 'cook pkg' but with cooker log.
38 flavor|-f Cook all packages of a flavor.
39 list|-l Cook all packages in the given list.
40 cat|-c Cook all packages of a category.
41 rev|-r Cook packages of a specific revision.
42 all|-a Find and cook all unbuilt packages.
44 EOT
45 exit 0
46 }
49 # Some messages occur in activity but log verbose output when checking for commits
50 # into a log file.
52 log_commits() {
53 sed '/^.\//'d | sed '/^.hg/'d | tee -a $LOGS/commits.log
54 }
57 # Clean up before exit when check and cook commits finish.
59 clean_exit() {
60 rm -f $command; touch $command
61 [ "$previous_command" ] && ps | grep -q "${previous_command/:/ }" &&
62 echo -n "$previous_command" > $command
63 rm -f $pidfile
64 }
67 # Summary for commits.
69 commits_summary() {
70 msg="from revision $cur to $new"
71 [ "$new" == "$cur" ] && msg="revision $new"
72 echo "Will cook $msg"
73 separator
74 echo -e "\nSummary for commits"
75 separator
76 echo "Hg wok revision : $cur"
77 echo "Pulled revision : $new"
78 echo "Check date : $(date '+%F %T')"
79 }
82 # Scan packages build deps and fill up cookorder list.
84 cook_order_scan() {
85 rm -f $cookorder
86 touch $cookorder
87 for pkg in $(cat $cooklist); do
88 unset WANTED BUILD_DEPENDS
89 . $wok/$pkg/receipt
90 # The :: is for web interface color.
91 [ "$WANTED$BUILD_DEPENDS" ] && echo "$pkg :: $WANTED $BUILD_DEPENDS"
92 for dep in $WANTED $BUILD_DEPENDS; do
93 if grep -q "^$dep$" $cooklist; then
94 if ! grep -q "^$dep$" $cookorder; then
95 echo "$dep" >> $cookorder
96 fi
97 fi
98 done
99 done
101 # Append unordered packages to cookorder.
102 for pkg in $(cat $cooklist); do
103 if ! grep -q "^$pkg$" $cookorder; then
104 echo "$pkg" >> $cookorder
105 fi
106 done
107 }
110 # Scan and rescan until the cooklist is ordered then handle WANTED.
112 cook_order() {
113 time=$(date +%s)
114 scan=0
116 # Keep an original cooklist so we do a diff when ordering is finished.
117 cp -f $cooklist $cooklist.0
118 echo 'cookorder' > $command
119 echo -e '\nInitial Cooker order scan'
120 separator
121 cook_order_scan
123 # Diff between the cooklist and new ordered list ? So copy the last
124 # cookorder to cooklist and rescan it.
125 while /bin/true; do
126 diff $cooklist $cookorder > $cookorder.diff
127 if [ -s "$cookorder.diff" ]; then
128 scan=$(($scan + 1))
129 echo -e "\nDiff scan: $scan"
130 separator
131 mv -f $cookorder $cooklist
132 cook_order_scan
133 else
134 break
135 fi
136 done
138 # Keep a diff between submitted cooklist and the ordered.
139 diff $cooklist.0 $cooklist > $cooklist.diff
140 rm -f $cookorder $cookorder.diff $cooklist.0
142 # Scan finished: append pkg to WANTED or leave it in the ordered cooklist.
143 # TODO: grep the line number to get pkg position and keep it higher.
144 echo -e '\nHandle WANTED package'
145 separator
146 for pkg in $(cat $cooklist); do
147 unset WANTED
148 . $wok/$pkg/receipt
149 for wanted in $WANTED; do
150 echo "$pkg :: $wanted"
151 if grep -q ^${wanted}$ $cooklist; then
152 sed -i -e "/^$pkg$/"d \
153 -e "/^$wanted$/ a $pkg" $cooklist
154 fi
155 done
156 done
158 # Show ordered cooklist
159 echo -e '\nCooklist order'
160 separator
161 cat $cooklist
162 separator
164 time=$(($(date +%s) - $time))
165 pkgs=$(cat $cooklist | wc -l)
166 echo -e '\nSummary for cookorder'
167 separator
168 cat <<EOT
169 Ordered packages : $pkgs
170 Scans executed : $scan
171 Scan duration : ${time}s
172 EOT
173 separator
175 rm -f $command
176 }
179 # Remove blocked (faster this way than grepping before).
181 strip_blocked() {
182 for pkg in $(cat $blocked); do
183 sed -i /^${pkg}$/d $cooklist
184 done
185 sed -i /^$/d $cooklist
186 }
189 # Use in default mode and with all cmd.
191 cook_commits() {
192 if [ -s "$commits" ]; then
193 for pkg in $(cat $commits); do
194 ps | grep -q "cook $pkg$" && continue
195 echo "cook:$pkg" > $command
196 cook $pkg || broken
197 sed -i /^${pkg}$/d $commits
198 done
199 fi
200 }
203 # Cook all packages in a cooklist.
205 cook_list() {
206 for pkg in $(cat $cooklist); do
207 ps | grep -q "cook $pkg$" && continue
208 cook $pkg || broken
209 sed -i /^${pkg}$/d $cooklist
210 done
211 }
214 # Create a arch.$ARCH file for each package cooked for the target host
215 # architecture
216 #
217 # The deal: we dont want all packages handled by cooker commands and stats,
218 # since we dont cross compile all packages for each arch but only a set of
219 # packages to provide one full featured desktop, servers and goodies useful
220 # for the host system.
221 #
223 arch_db() {
224 count=0
225 echo "Cleaning packages DB : arch.$ARCH"
226 rm -f $wok/*/arch.$ARCH && cd $wok
227 echo "Creating $ARCH packages DB..."
228 for pkg in *; do
229 HOST_ARCH=
230 . $wok/$pkg/receipt
231 if [ -n "$HOST_ARCH" ]; then
232 if $(echo "$HOST_ARCH" | egrep -q "$ARCH|any"); then
233 count=$(($count + 1))
234 echo "Adding: $pkg"
235 touch $pkg/arch.$ARCH
236 fi
237 unset HOST_ARCH
238 else
239 # HOST_ARCH not set --> in i486
240 if [ "$ARCH" == 'i486' ]; then
241 count=$(($count + 1))
242 echo "Adding: $pkg"
243 touch $pkg/arch.$ARCH
244 fi
245 fi
246 done
247 echo "Packages for $ARCH : $count"
248 }
251 #
252 # Commands
253 #
255 previous_command="$(cat $command)"
256 case "$1" in
257 usage|help|-u|-h)
258 usage ;;
260 setup|-s)
261 # Setup the Cooker environment.
262 echo -e '\nSetting up the Cooker'
263 mkdir -p $CACHE
264 echo "Cooker setup using: $SLITAZ" | log
265 separator
266 for pkg in $SETUP_PKGS mercurial rsync tazlito; do
267 [ ! -d "$INSTALLED/$pkg" ] && tazpkg get-install $pkg
268 done
269 mkdir -p $SLITAZ && cd $SLITAZ
270 if [ -d "${wok}-hg" ]; then
271 echo -e 'Hg wok already exists.\n'
272 exit 1
273 fi
274 if [ -d "$wok" ]; then
275 echo -e 'Build wok already exists.\n'
276 exit 1
277 fi
279 # Directories and files
280 echo "mkdir's and touch files in: $SLITAZ"
281 mkdir -p $PKGS $LOGS $FEEDS $CACHE $SRC
282 for f in $activity $blocked $broken $commits $cooklist $command; do
283 touch $f
284 done
285 hg clone $WOK_URL ${wok}-hg || exit 1
286 [ -d "$flavors" ] || hg clone $FLAVORS_URL flavors
287 cp -a ${wok}-hg $wok
288 separator; newline ;;
290 arch-db)
291 # Manually create arch packages DB.
292 arch_db ;;
294 setup-cron)
295 # Create cron job for the cooker.
296 [ "$2" ] || hours=2
297 if [ ! -f "$crontabs" ]; then
298 mkdir -p /var/spool/cron/crontabs
299 fi
300 if ! fgrep -q /usr/bin/cooker $crontabs; then
301 cat > $crontabs << EOT
302 # Run SliTaz Cooker every $hours hours
303 0 */$hours * * * /usr/bin/cooker --output=html
304 */5 * * * * [ $CACHE/cooker-request -nt $CACHE/activity ] && /usr/bin/cooker --output=html
305 */5 * * * * [ -s $CACHE/recook-packages ] && /usr/bin/cooker list $CACHE/recook-packages
306 EOT
307 touch $CACHE/cooker-request $CACHE/recook-packages
308 chmod 666 $CACHE/cooker-request $CACHE/recook-packages
309 killall crond 2>/dev/null && /etc/init.d/crond start
310 fi ;;
312 check-cron)
313 if [ ! -f "$crontabs" ]; then
314 echo "There is no $crontabs here. Use setup-cron option."
315 exit 1
316 fi
317 fgrep /usr/bin/cooker $crontabs ;;
319 note|-n)
320 # Blocked a pkg and want others to know why? Post a note!
321 [ -n "$2" ] && echo "$(date '+%F %R') : $2" >> $cooknotes ;;
323 notes|-ns)
324 # View cooknotes.
325 echo -e '\nCooknotes'
326 separator
327 cat $cooknotes
328 separator; newline ;;
330 block|-b)
331 # Block a package.
332 [ "$pkg" ] && cook $pkg --block ;;
334 unblock|-ub)
335 # Unblock a package.
336 [ "$pkg" ] && cook $pkg --unblock ;;
338 reverse|-r)
339 # Cook all reverse dependencies for a package. This command lets us
340 # control the Cooker manually for commits that will cook a lot of packages.
341 #
342 # Use hg commit? Ex: hg commit -m "Message bla bla | cooker:reverse"
343 #
344 if [ ! -d "$wok/$pkg" ]; then
345 echo -e "\nNo package $2 found.\n"
346 exit 0
347 fi
348 rm -f $cooklist; touch $cooklist
349 echo -e "\nReverse cooklist for: $pkg"
350 separator
352 cd $wok
353 for rev in *; do
354 unset WANTED DEPENDS BUILD_DEPENDS; . $wok/$rev/receipt
355 if echo "$WANTED $DEPENDS $BUILD_DEPENDS" | fgrep -q $pkg; then
356 echo "$rev" | tee -a $cooklist
357 fi
358 done
359 separator
360 echo -e "Reverse dependencies found: $(cat $cooklist | wc -l)\n"
361 strip_blocked
362 cook_order | tee $LOGS/cookorder.log
363 cook_list ;;
365 pkg|-p)
366 # Same as 'cook pkg' but with log for web interface.
367 ps | grep -q "cook $pkg$" && echo 'Already running' && continue
368 cook $pkg || broken
369 clean_exit ;;
371 cat|-c)
372 # Cook all packages of a category.
373 cat="$2"
374 rm -f $cooklist; touch $cooklist
376 cd $wok
377 for pkg in *; do
378 unset CATEGORY; . $pkg/receipt
379 [ "$CATEGORY" == "$cat" ] && echo $pkg >> $cooklist
380 done
381 strip_blocked
382 cook_order | tee $LOGS/cookorder.log
383 cook_list ;;
385 flavor|-f)
386 # Cook all packages of a flavor.
387 name="$2"
388 if [ ! -d "$flavors/$name" ]; then
389 echo -e "\nSpecified flavor does not exist: $name\n"
390 exit 1
391 fi
392 if [ -d "$flavors/.hg" ]; then
393 cd $flavors; hg pull -u
394 fi
395 list="$flavors/$name/packages.list"
396 cp -a $list $cooklist
397 strip_blocked
398 cook_order | tee $LOGS/cookorder.log
399 cook_list ;;
401 list|-l)
402 # Cook a list of packages given in argument.
403 list="$2"
404 if [ ! -f "$list" ]; then
405 echo -e "\nSpecified list does not exist: $list\n"
406 exit 1
407 fi
408 cp -a $list $cooklist
409 strip_blocked
410 cook_order | tee $LOGS/cookorder.log
411 cook_list ;;
413 rev|-r)
414 # Cook or recook a specific Hg revision.
415 rev="$2"
416 [ "$rev" ] || exit 0
417 rm -f $cooklist; touch $cooklist
419 cd $wok
420 for pkg in $(hg log --rev=$rev --template "{files}"); do
421 echo "$pkg" | cut -d/ -f1 >> $cooklist
422 done
423 strip_blocked
424 cook_order | tee $LOGS/cookorder.log
425 cook_list ;;
427 all|-a)
428 # Try to build all unbuilt packages except blocked's.
429 echo 'cooker:all' > $command
430 rm -f $cooklist; touch $cooklist
431 newline
432 echo 'Cooker cooklist'
433 separator
435 # Find all unbuilt packages. Get EXTRAVERSION from packed receipt
436 # if it exists since extra version is added when packing the package.
437 echo 'Searching for all unbuilt packages' | log
439 cd $wok
440 for pkg in *; do
441 unset EXTRAVERSION
442 . $pkg/receipt
443 [ -f "$pkg/taz/$PACKAGE-$VERSION/receipt" ] && \
444 . $pkg/taz/$PACKAGE-$VERSION/receipt
445 if [ ! -f "$PKGS/$PACKAGE-$VERSION$EXTRAVERSION.tazpkg" ]; then
446 echo $pkg; echo $pkg >> $cooklist
447 fi
448 done
449 strip_blocked
450 cook_order | tee $LOGS/cookorder.log
451 echo "Packages to cook: $(cat $cooklist | wc -l)" | log
452 cook_list ;;
454 *)
455 # Default is to cook all commits if not yet running.
456 [ -n "$1" ] && usage
457 cooklist=$commits
458 if [ -f "$pidfile" ]; then
459 pid=$(cat $pidfile)
460 if [ -s /proc/$pid/status ]; then
461 echo -e "\nStill cooking latest commits with pid:"
462 echo -e " $pid\n"
463 exit 0
464 fi
465 rm -f "$pidfile"
466 fi
468 # Start and get a PID file.
469 rm -f $LOGS/commits.log
470 newline
471 echo 'Checking for commits' | log_commits
472 separator | tee -a $LOGS/commits.log
474 echo $$ > $pidfile
475 trap 'echo -e "\nCooker stopped: PID $$\n" && \
476 rm -f $pidfile $command && exit 1' INT TERM
478 echo "Cooker PID : $$" | log_commits
479 echo "Cooker date : $(date '+%F %T')" | log_commits
481 # Get revisions. Here we have 2 echoes since we want a msg on screen,
482 # in commits log and activity DB without a space before.
483 cd $wok || exit 1
484 cur=$(hg head --template '{rev}\n')
485 echo "Updating wok : ${wok}-hg (rev $cur)" | log_commits
486 echo "Updating wok: ${wok}-hg" | log
487 echo 'hg:pull' > $command
488 cd $wok-hg; hg pull -u | log_commits
489 new=$(hg head --template '{rev}\n')
490 # Store last rev to be used by CGI so it doesn't need to call hg head
491 # on each load.
492 echo "$new" > $wokrev
494 # Sync build wok with rsync so we don't take care about removing old
495 # files as before.
496 if [ "$new" -gt "$cur" ]; then
497 echo "Changes found from: $cur to $new" | log
498 echo 'Syncing build wok with Hg wok...' | log_commits
499 rsync -r -t -c -l -u -v -D -E $wok-hg/ $wok/ | \
500 sed '/^$/'d | log_commits
501 else
502 echo "No revision changes: $cur vs $new" | log
503 separator | log_commits
504 clean_exit; newline
505 exit 0
506 fi
508 # Get and display modifications.
509 cd $wok-hg
510 commits_summary | log_commits
511 cur=$(($cur + 1))
512 rm -f $commits.tmp; touch $commits.tmp
513 for rev in $(seq $cur $new); do
514 for file in $(hg log --rev=$rev --template "{files}"); do
515 pkg=$(echo $file | cut -d/ -f1)
516 desc=$(hg log --rev=$rev --template "{desc}" $file)
517 echo "Committed package : $pkg - $desc" | log_commits
518 echo $pkg >> $commits.tmp
519 done
520 done
522 # We may have deleted packages and files in stuff/. Remove it and
523 # clean DB as well as log file.
524 cd $wok
525 for pkg in *; do
526 if [ ! -d "${wok}-hg/$pkg" ]; then
527 echo "Removing package: $pkg" | log_commits
528 . $wok/$pkg/receipt
529 rm -rf $PKGS/$PACKAGE-$VERSION* $wok/$pkg $LOGS/$pkg.log
530 sed -i "/^${pkg}$/"d $blocked $broken $commits.tmp
531 fi
532 done
534 # Keep previous commit and discard duplicate lines
535 cat $commits $commits.tmp | sed /"^$"/d > $commits.new
536 uniq $commits.new > $commits; rm $commits.*
538 # Handle cross compilation. Create arch packages DB and remove pkgs
539 # not cooked for this arch from the commits list.
540 arch_db
541 for pkg in $(cat $commits); do
542 if [ ! -f "$wok/$pkg/arch.$ARCH" ]; then
543 echo "Cooker arch : skip $pkg (not included in: $ARCH)" | \
544 log_commits
545 sed -i "/^${pkg}$/"d $commits
546 else
547 echo "Cooker arch : $ARCH" | log_commits
548 fi
549 done
551 # Stats
552 pkgs=$(cat $commits | wc -l)
553 echo "Packages to cook: $pkgs" | log
554 echo "Packages to cook : $pkgs" | log_commits
555 separator | log_commits
556 newline
557 strip_blocked
558 cook_order | tee $LOGS/cookorder.log
559 cook_commits
560 clean_exit ;;
561 esac
563 exit 0