cookutils view web/cooker.cgi @ rev 785

cooker.cgi: avoid multiple recooks
author Pascal Bellard <pascal.bellard@slitaz.org>
date Sun Dec 06 17:56:47 2015 +0100 (2015-12-06)
parents ccdce778cbc0
children 36e267c88f80
line source
1 #!/bin/sh
2 #
3 # SliTaz Cooker CGI/web interface.
4 #
6 [ -f "/etc/slitaz/cook.conf" ] && . /etc/slitaz/cook.conf
7 [ -f "cook.conf" ] && . ./cook.conf
9 # The same wok as cook.
10 wok="$WOK"
12 # Cooker DB files.
13 activity="$CACHE/activity"
14 commits="$CACHE/commits"
15 cooklist="$CACHE/cooklist"
16 cookorder="$CACHE/cookorder"
17 command="$CACHE/command"
18 blocked="$CACHE/blocked"
19 broken="$CACHE/broken"
20 cooknotes="$CACHE/cooknotes"
21 cooktime="$CACHE/cooktime"
22 wokrev="$CACHE/wokrev"
24 # We're not logged and want time zone to display correct server date.
25 export TZ=$(cat /etc/TZ)
27 case "$QUERY_STRING" in
28 recook=*)
29 grep -qs "^${QUERY_STRING#recook=}$" $CACHE/recook-packages ||
30 echo ${QUERY_STRING#recook=} >> $CACHE/recook-packages
31 cat <<EOT
32 Location: $HTTP_REFERER
34 EOT
35 exit ;;
36 poke)
37 touch $CACHE/cooker-request
38 cat <<EOT
39 Location: $HTTP_REFERER
41 EOT
42 exit ;;
43 download*)
44 file=$(busybox httpd -d "$PKGS/${QUERY_STRING#*=}")
45 cat <<EOT
46 Content-Type: application/octet-stream
47 Content-Length: $(stat -c %s "$file")
48 Content-Disposition: attachment; filename="$(basename "$file")"
50 EOT
51 cat "$file"
52 exit ;;
53 rss)
54 cat <<EOT
55 Content-Type: application/rss+xml
57 EOT
58 ;;
59 *)
60 cat <<EOT
61 Content-Type: text/html; charset=utf-8
63 EOT
64 ;;
65 esac
68 # RSS feed generator
69 if [ "$QUERY_STRING" == 'rss' ]; then
70 pubdate=$(date -R)
71 cat <<EOT
72 <?xml version="1.0" encoding="utf-8" ?>
73 <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
74 <channel>
75 <title>SliTaz Cooker</title>
76 <description>The SliTaz packages cooker feed</description>
77 <link>$COOKER_URL</link>
78 <lastBuildDate>$pubdate</lastBuildDate>
79 <pubDate>$pubdate</pubDate>
80 <atom:link href="http://cook.slitaz.org/cooker.cgi?rss" rel="self" type="application/rss+xml" />
81 EOT
82 for rss in $(ls -lt $FEEDS/*.xml | head -n 12); do
83 cat $rss | sed 's|<guid|& isPermaLink="false"|g;s|</pubDate| GMT&|g'
84 done
85 cat <<EOT
86 </channel>
87 </rss>
88 EOT
89 exit 0
90 fi
93 #
94 # Functions
95 #
98 # Put some colors in log and DB files.
100 syntax_highlighter() {
101 case $1 in
102 log)
103 sed -e 's/&/\&amp;/g;s/</\&lt;/g;s/>/\&gt;/g' \
104 -e 's#OK$#<span class="span-ok">OK</span>#g' \
105 -e 's#Done$#<span class="span-ok">Done</span>#g' \
106 -e 's#yes$#<span class="span-ok">yes</span>#g' \
107 -e 's#no$#<span class="span-no">no</span>#g' \
108 -e 's#error$#<span class="span-red">error</span>#g' \
109 -e 's#ERROR:#<span class="span-red">ERROR:</span>#g' \
110 -e 's#WARNING:#<span class="span-red">WARNING:</span>#g' \
111 -e s"#^Executing:\([^']*\).#<span class='sh-val'>\0</span>#"g \
112 -e s"#^====\([^']*\).#<span class='span-line'>\0</span>#"g \
113 -e s"#^[a-zA-Z0-9]\([^']*\) :: #<span class='span-sky'>\0</span>#"g \
114 -e s"#ftp://[^ '\"]*#<a href='\0'>\0</a>#"g \
115 -e s"#http://[^ '\"]*#<a href='\0'>\0</a>#"g ;;
117 receipt)
118 sed -e s'|&|\&amp;|g' -e 's|<|\&lt;|g' -e 's|>|\&gt;|'g \
119 -e s"#^\#\([^']*\)#<span class='sh-comment'>\0</span>#"g \
120 -e s"#\"\([^']*\)\"#<span class='sh-val'>\0</span>#"g ;;
122 diff)
123 sed -e 's|&|\&amp;|g' -e 's|<|\&lt;|g' -e 's|>|\&gt;|g' \
124 -e s"#^-\([^']*\).#<span class='span-red'>\0</span>#"g \
125 -e s"#^+\([^']*\).#<span class='span-ok'>\0</span>#"g \
126 -e s"#@@\([^']*\)@@#<span class='span-sky'>@@\1@@</span>#"g ;;
128 activity)
129 sed s"#^\([^']* : \)#<span class='log-date'>\0</span>#"g ;;
130 esac
131 }
134 # Latest build pkgs.
136 list_packages() {
137 cd $PKGS
138 ls -1t *.tazpkg | head -20 | \
139 while read file; do
140 echo -n $(stat -c '%y' $PKGS/$file | cut -d . -f 1 | sed s/:[0-9]*$//)
141 echo " : $file"
142 done
143 }
146 # Optional full list button
148 more_button() {
149 [ $(wc -l < ${3:-$CACHE/$1}) -gt ${4:-12} ] &&
150 echo "<a class=\"button\" href=\"cooker.cgi?file=$1\">$2</a>"
151 }
154 # Show the running command and its progression
156 running_command()
157 {
158 local state="Not running"
159 if [ -s "$command" ]; then
160 state="$(cat $command)"
161 if grep -q "^$state" $cooktime ; then
162 set -- $(cat $cooktime)
163 state="$state $((($(date +%s)-$3)*100/$2))%"
164 fi
165 fi
166 echo $state
167 }
170 # xHTML header. Pages can be customized with a separated html.header file.
172 if [ -f "header.html" ]; then
173 cat header.html
174 else
175 cat <<EOT
176 <!DOCTYPE html>
177 <html lang="en">
178 <head>
179 <meta charset="utf-8"/>
180 <title>SliTaz Cooker</title>
181 <link rel="shortcut icon" href="favicon.ico"/>
182 <link rel="stylesheet" type="text/css" href="style.css"/>
183 <meta name="robots" content="nofollow">
184 </head>
185 <body>
187 <div id="header">
188 <div id="logo"></div>
189 <h1><a href="cooker.cgi">SliTaz Cooker</a></h1>
190 </div>
192 <!-- Content -->
193 <div id="content">
194 EOT
195 fi
198 #
199 # Load requested page
200 #
202 case "${QUERY_STRING}" in
203 pkg=*)
204 pkg=${QUERY_STRING#pkg=}
205 log=$LOGS/$pkg.log
206 echo "<h2>Package: $pkg</h2>"
208 # Package info.
209 echo '<div id="info">'
210 if [ -f "$wok/$pkg/receipt" ]; then
211 echo "<a href='cooker.cgi?receipt=$pkg'>receipt</a>"
212 unset WEB_SITE
213 . $wok/$pkg/receipt
215 [ -n "$WEB_SITE" ] && # busybox wget -s $WEB_SITE &&
216 echo "<a href='$WEB_SITE'>home</a>"
218 if [ -f "$wok/$pkg/taz/$PACKAGE-$VERSION/receipt" ]; then
219 echo "<a href='cooker.cgi?files=$pkg'>files</a>"
220 unset EXTRAVERSION
221 . $wok/$pkg/taz/$PACKAGE-$VERSION/receipt
222 if [ -f $wok/$pkg/taz/$PACKAGE-$VERSION/description.txt ]; then
223 echo "<a href='cooker.cgi?description=$pkg'>description</a>"
224 fi
225 if [ -f $PKGS/$PACKAGE-$VERSION$EXTRAVERSION.tazpkg ]; then
226 echo "<a href='cooker.cgi?download=$PACKAGE-$VERSION$EXTRAVERSION.tazpkg'>download</a>"
227 fi
228 if [ -f $PKGS/$PACKAGE-$VERSION$EXTRAVERSION-$ARCH.tazpkg ]; then
229 echo "<a href='cooker.cgi?download=$PACKAGE-$VERSION$EXTRAVERSION-$ARCH.tazpkg'>download</a>"
230 fi
231 fi
232 echo "<a href='ftp://${HTTP_HOST%:*}/$pkg/'>browse</a>"
233 else
234 if [ $(ls $wok/*$pkg*/receipt 2> /dev/null | wc -l) -eq 0 ]; then
235 echo "No package named: $pkg"
236 else
237 ls $wok/$pkg/receipt >/dev/null 2>&1 || pkg="*$pkg*"
238 echo '<table style="width:100%">'
239 for i in $(cd $wok ; ls $pkg/receipt); do
240 pkg=$(dirname $i)
241 unset SHORT_DESC CATEGORY
242 . $wok/$pkg/receipt
243 cat <<EOT
244 <tr>
245 <td><a href="cooker.cgi?pkg=$pkg">$pkg</a></td>
246 <td>$SHORT_DESC</td>
247 <td>$CATEGORY</td>
248 </tr>
249 EOT
250 done
251 echo '</table>'
252 unset pkg
253 fi
254 fi
255 echo '</div>'
257 # Check for a log file and display summary if it exists.
258 if [ -f "$log" ]; then
259 if grep -q "cook:$pkg$" $command; then
260 echo "<pre>The Cooker is currently building: $pkg</pre>"
261 fi
262 if fgrep -q "Summary for:" $LOGS/$pkg.log; then
263 echo '<h3>Cook summary</h3>'
264 echo '<pre>'
265 grep -A 12 "^Summary for:" $LOGS/$pkg.log | sed /^$/d | \
266 syntax_highlighter log
267 echo '</pre>'
268 fi
269 if fgrep -q "Debug information" $LOGS/$pkg.log; then
270 echo '<h3>Cook failed</h3>'
271 echo '<pre>'
272 grep -A 8 "^Debug information" $LOGS/$pkg.log | sed /^$/d | \
273 syntax_highlighter log
274 echo '</pre>'
275 fi
276 echo '<h3>Cook log</h3>'
277 echo '<pre>'
278 cat $log | syntax_highlighter log
279 echo '</pre>'
280 echo "<a class=\"button\" href=\"cooker.cgi?recook=$pkg\">Recook $pkg</a>"
281 else
282 [ "$pkg" ] && echo "<pre>No log: $pkg</pre>"
283 fi ;;
285 file=*)
286 # Don't allow all files on the system for security reasons.
287 file=${QUERY_STRING#file=}
288 case "$file" in
289 activity|cooknotes|cooklist)
290 [ "$file" == "cooklist" ] && \
291 nb="- Packages: $(cat $cooklist | wc -l)"
292 echo "<h2>DB: $file $nb</h2>"
293 echo '<pre>'
294 tac $CACHE/$file | syntax_highlighter activity
295 echo '</pre>' ;;
297 broken)
298 nb=$(cat $broken | wc -l)
299 echo "<h2>DB: broken - Packages: $nb</h2>"
300 echo '<pre>'
301 cat $CACHE/$file | sort | \
302 sed s"#^[^']*#<a href='cooker.cgi?pkg=\0'>\0</a>#"g
303 echo '</pre>' ;;
305 *.diff)
306 diff=$CACHE/$file
307 echo "<h2>Diff for: ${file%.diff}</h2>"
308 [ "$file" == "installed.diff" ] && echo \
309 "<p>This is the latest diff between installed packages \
310 and installed build dependencies to cook.</p>"
311 echo '<pre>'
312 cat $diff | syntax_highlighter diff
313 echo '</pre>' ;;
315 *.log)
316 log=$LOGS/$file
317 name=$(basename $log)
318 echo "<h2>Log for: ${name%.log}</h2>"
319 if [ -f "$log" ]; then
320 if fgrep -q "Summary" $log; then
321 echo '<pre>'
322 grep -A 20 "^Summary" $log | sed /^$/d | \
323 syntax_highlighter log
324 echo '</pre>'
325 fi
326 echo '<pre>'
327 cat $log | syntax_highlighter log
328 echo '</pre>'
329 else
330 echo "<pre>No log file: $log</pre>"
331 fi ;;
332 esac ;;
334 stuff=*)
335 file=${QUERY_STRING#stuff=}
336 echo "<h2>$file</h2>"
337 echo '<pre>'
338 cat $wok/$file | sed 's/&/\&amp;/g;s/</\&lt;/g;s/>/\&gt;/g'
339 echo '</pre>' ;;
341 receipt=*)
342 pkg=${QUERY_STRING#receipt=}
343 echo "<h2>Receipt for: $pkg</h2>"
344 if [ -f "$wok/$pkg/receipt" ]; then
345 ( cd $wok/$pkg ; find stuff -type f 2> /dev/null ) | \
346 while read file ; do
347 echo "<a href=\"?stuff=$pkg/$file\">$file</a>"
348 done
349 echo '<pre>'
350 cat $wok/$pkg/receipt | \
351 syntax_highlighter receipt
352 echo '</pre>'
353 else
354 echo "<pre>No receipt for: $pkg</pre>"
355 fi ;;
357 files=*)
358 pkg=${QUERY_STRING#files=}
359 dir=$(ls -d $WOK/$pkg/taz/$pkg-*)
360 if [ -d "$dir/fs" ]; then
361 echo "<h2>Installed files by: $pkg ($(du -hs $dir/fs | awk '{ print $1 }'))</h2>"
362 echo '<pre>'
363 find $dir/fs -not -type d -print0 | xargs -0 ls -ld | \
364 sed "s|\(.*\) /.*\(${dir#*wok}/fs\)\(.*\)|\1 <a href=\"?download=../wok\2\3\">\3</a>|;s|^\([^-].*\)\(<a.*\)\">\(.*\)</a>|\1\3|"
365 echo '</pre>'
366 else
367 echo "<pre>No files list for: $pkg</pre>"
368 fi ;;
370 description=*)
371 pkg=${QUERY_STRING#description=}
372 echo "<h2>Description of $pkg</h2>"
373 dir=$(ls -d $WOK/$pkg/taz/$pkg-*)
374 if [ -s "$dir/description.txt" ]; then
375 echo '<pre>'
376 cat $dir/description.txt | \
377 sed 's/&/\&amp;/g;s/</\&lt;/g;s/>/\&gt;/g'
378 echo '</pre>'
379 else
380 echo "<pre>No description for: $pkg</pre>"
381 fi ;;
383 *)
384 # We may have a toolchain.cgi script for cross cooker's
385 if [ -f "toolchain.cgi" ]; then
386 toolchain='toolchain.cgi'
387 else
388 toolchain='cooker.cgi?pkg=slitaz-toolchain'
389 fi
390 # Main page with summary. Count only package include in ARCH,
391 # use 'cooker arch-db' to manually create arch.$ARCH files.
392 inwok=$(ls $WOK/*/arch.$ARCH | wc -l)
393 cooked=$(ls $PKGS/*.tazpkg | wc -l)
394 unbuilt=$(($inwok - $cooked))
395 pct=0
396 [ $inwok -gt 0 ] && pct=$(( ($cooked * 100) / $inwok ))
397 cat <<EOT
398 <div style="float: right;">
399 <form method="get" action="$SCRIPT_NAME">
400 Package:
401 <input type="text" name="pkg" />
402 </form>
403 </div>
405 <h2>Summary</h2>
407 <pre>
408 Running command : $(running_command)
409 Wok revision : <a href="$WOK_URL">$(cat $wokrev)</a>
410 Commits to cook : $(cat $commits | wc -l)
411 Current cooklist : $(cat $cooklist | wc -l)
412 Broken packages : $(cat $broken | wc -l)
413 Blocked packages : $(cat $blocked | wc -l)
414 </pre>
416 <p class="info">
417 Packages: $inwok in the wok | $cooked cooked | $unbuilt unbuilt |
418 Server date: $(date -u '+%F %R %Z')
419 </p>
420 <div class="pctbar">
421 <div class="pct" style="width: ${pct}%;">${pct}%</div>
422 </div>
424 <p>
425 Latest:
426 <a href="cooker.cgi?file=cookorder.log">cookorder.log</a>
427 <a href="cooker.cgi?file=commits.log">commits.log</a>
428 <a href="cooker.cgi?file=pkgdb.log">pkgdb.log</a>
429 <a href="cooker.cgi?file=installed.diff">installed.diff</a>
430 - Architecture $ARCH:
431 <a href="$toolchain">toolchain</a>
432 </p>
433 EOT
434 [ -e $CACHE/cooker-request ] &&
435 [ $CACHE/activity -nt $CACHE/cooker-request ] && cat <<EOT
437 <a class="button" href="cooker.cgi?poke">Poke cooker</a>
438 EOT
439 cat <<EOT
441 <h2 id="activity">Activity</h2>
442 <pre>
443 $(tac $CACHE/activity | head -n 12 | syntax_highlighter activity)
444 </pre>
445 $(more_button activity "More activity" $CACHE/activity 12)
448 <h2 id="cooknotes">Cooknotes</h2>
449 <pre>
450 $(tac $cooknotes | head -n 12 | syntax_highlighter activity)
451 </pre>
452 $(more_button cooknotes "More notes" $cooknotes 12)
455 <h2 id="commits">Commits</h2>
456 <pre>
457 $(cat $commits)
458 </pre>
461 <h2 id="cooklist">Cooklist</h2>
462 <pre>
463 $(cat $cooklist | head -n 20)
464 </pre>
465 $(more_button cooklist "Full cooklist" $cooklist 20)
468 <h2 id="broken">Broken</h2>
469 <pre>
470 $(cat $broken | head -n 20 | sed s"#^[^']*#<a href='cooker.cgi?pkg=\0'>\0</a>#"g)
471 </pre>
472 $(more_button broken "All broken packages" $broken 20)
475 <h2 id="blocked">Blocked</h2>
476 <pre>
477 $(cat $blocked | sed s"#^[^']*#<a href='cooker.cgi?pkg=\0'>\0</a>#"g)
478 </pre>
481 <h2 id="lastcook">Latest cook</h2>
482 <pre>
483 $(list_packages | sed s"#^\([^']*\).* : #<span class='log-date'>\0</span>#"g)
484 </pre>
485 EOT
486 ;;
487 esac
490 # Close xHTML page
492 cat <<EOT
493 </div>
495 <div id="footer">
496 <a href="http://www.slitaz.org/">SliTaz Website</a>
497 <a href="cooker.cgi">Cooker</a>
498 <a href="http://hg.slitaz.org/cookutils/raw-file/tip/doc/cookutils.en.html">
499 Documentation</a>
500 </div>
502 </body>
503 </html>
504 EOT
506 exit 0