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