cookutils view web/cooker.cgi @ rev 805

cooker.cgi: move buttons to the right
author Pascal Bellard <pascal.bellard@slitaz.org>
date Sun Jul 03 11:30:30 2016 +0200 (2016-07-03)
parents 5658598bf22a
children 05a7f4f61409
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 case "$HTTP_USER_AGENT" in
30 *SliTaz*)
31 grep -qs "^${QUERY_STRING#recook=}$" $CACHE/recook-packages ||
32 echo ${QUERY_STRING#recook=} >> $CACHE/recook-packages
33 esac
34 cat <<EOT
35 Location: ${HTTP_REFERER:-${REQUEST_URI%\?*}}
37 EOT
38 exit ;;
39 poke)
40 touch $CACHE/cooker-request
41 cat <<EOT
42 Location: ${HTTP_REFERER:-${REQUEST_URI%\?*}}
44 EOT
45 exit ;;
46 download*)
47 file=$(busybox httpd -d "$PKGS/${QUERY_STRING#*=}")
48 cat <<EOT
49 Content-Type: application/octet-stream
50 Content-Length: $(stat -c %s "$file")
51 Content-Disposition: attachment; filename="$(basename "$file")"
53 EOT
54 cat "$file"
55 exit ;;
56 rss)
57 cat <<EOT
58 Content-Type: application/rss+xml
60 EOT
61 ;;
62 *)
63 cat <<EOT
64 Content-Type: text/html; charset=utf-8
66 EOT
67 ;;
68 esac
71 # RSS feed generator
72 if [ "$QUERY_STRING" == 'rss' ]; then
73 pubdate=$(date -R)
74 cat <<EOT
75 <?xml version="1.0" encoding="utf-8" ?>
76 <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
77 <channel>
78 <title>SliTaz Cooker</title>
79 <description>The SliTaz packages cooker feed</description>
80 <link>$COOKER_URL</link>
81 <lastBuildDate>$pubdate</lastBuildDate>
82 <pubDate>$pubdate</pubDate>
83 <atom:link href="http://cook.slitaz.org/cooker.cgi?rss" rel="self" type="application/rss+xml" />
84 EOT
85 for rss in $(ls -lt $FEEDS/*.xml | head -n 12); do
86 cat $rss | sed 's|<guid|& isPermaLink="false"|g;s|</pubDate| GMT&|g'
87 done
88 cat <<EOT
89 </channel>
90 </rss>
91 EOT
92 exit 0
93 fi
96 #
97 # Functions
98 #
101 # Put some colors in log and DB files.
103 syntax_highlighter() {
104 case $1 in
105 log)
106 # If variables not defined - define them with some rare values
107 : ${_src=#_#_#}
108 : ${_install=#_#_#}
109 : ${_fs=#_#_#}
110 : ${_stuff=#_#_#}
111 sed -e 's/&/\&amp;/g;s/</\&lt;/g;s/>/\&gt;/g' \
112 -e 's#OK$#<span class="span-ok">OK</span>#g' \
113 -e 's#Done$#<span class="span-ok">Done</span>#g' \
114 -e 's#yes$#<span class="span-ok">yes</span>#g' \
115 -e 's#no$#<span class="span-no">no</span>#g' \
116 -e 's#error$#<span class="span-red">error</span>#g' \
117 -e 's#ERROR:#<span class="span-red">ERROR:</span>#g' \
118 -e 's#WARNING:#<span class="span-red">WARNING:</span>#g' \
119 -e s"#^Executing:\([^']*\).#<span class='sh-val'>\0</span>#"g \
120 -e s"#^====\([^']*\).#<span class='span-line'>\0</span>#"g \
121 -e s"#^[a-zA-Z0-9]\([^']*\) :: #<span class='span-sky'>\0</span>#"g \
122 -e s"#ftp://[^ '\"]*#<a href='\0'>\0</a>#"g \
123 -e s"#http://[^ '\"]*#<a href='\0'>\0</a>#"g | \
124 sed "s|$_src|<span class='var'>\${src}</span>|g;
125 s|$_install|<span class='var'>\${install}</span>|g;
126 s|$_fs|<span class='var'>\${fs}</span>|g;
127 s|$_stuff|<span class='var'>\${stuff}</span>|g"
128 ;;
130 receipt)
131 sed -e s'|&|\&amp;|g' -e 's|<|\&lt;|g' -e 's|>|\&gt;|'g \
132 -e s"#^\#\([^']*\)#<span class='sh-comment'>\0</span>#"g \
133 -e s"#\"\([^']*\)\"#<span class='sh-val'>\0</span>#"g ;;
135 diff)
136 sed -e 's|&|\&amp;|g' -e 's|<|\&lt;|g' -e 's|>|\&gt;|g' \
137 -e s"#^-\([^']*\).#<span class='span-red'>\0</span>#"g \
138 -e s"#^+\([^']*\).#<span class='span-ok'>\0</span>#"g \
139 -e s"#@@\([^']*\)@@#<span class='span-sky'>@@\1@@</span>#"g ;;
141 activity)
142 sed s"#^\([^']* : \)#<span class='log-date'>\0</span>#"g ;;
143 esac
144 }
147 # Latest build pkgs.
149 list_packages() {
150 cd $PKGS
151 ls -1t *.tazpkg | head -20 | \
152 while read file; do
153 echo -n $(TZ=UTC stat -c '%y' $PKGS/$file | cut -d . -f 1 | sed s/:[0-9]*$//)
154 echo " : $file"
155 done
156 }
159 # Optional full list button
161 more_button() {
162 [ $(wc -l < ${3:-$CACHE/$1}) -gt ${4:-12} ] && cat <<EOT
163 <div style="float: right;">
164 <a class="button" href="cooker.cgi?file=$1">$2</a>
165 </div>
166 EOT
167 }
170 # Show the running command and its progression
172 running_command()
173 {
174 local state="Not running"
175 if [ -s "$command" ]; then
176 state="$(cat $command)"
177 if grep -q "^$state" $cooktime ; then
178 set -- $(cat $cooktime)
179 state="$state $((($(date +%s)-$3)*100/$2))%"
180 [ $2 -gt 300 ] && state="$state (should end $(date -u -d @$(($2+$3))))"
181 fi
182 fi
183 echo $state
184 }
187 # xHTML header. Pages can be customized with a separated html.header file.
189 if [ -f "header.html" ]; then
190 cat header.html
191 else
192 cat <<EOT
193 <!DOCTYPE html>
194 <html lang="en">
195 <head>
196 <meta charset="utf-8"/>
197 <title>SliTaz Cooker</title>
198 <link rel="shortcut icon" href="favicon.ico"/>
199 <link rel="stylesheet" type="text/css" href="style.css"/>
200 <meta name="robots" content="nofollow">
201 </head>
202 <body>
204 <div id="header">
205 <div id="logo"></div>
206 <h1><a href="cooker.cgi">SliTaz Cooker</a></h1>
207 </div>
209 <!-- Content -->
210 <div id="content">
211 EOT
212 fi
215 #
216 # Load requested page
217 #
219 case "${QUERY_STRING}" in
220 pkg=*)
221 pkg=${QUERY_STRING#pkg=}
222 log=$LOGS/$pkg.log
223 echo "<h2>Package: $pkg</h2>"
225 # Define cook variables for syntax highlighter
226 . "$WOK/$pkg/receipt"
227 _wok='/home/slitaz/wok'
228 _src="$_wok/$pkg/source/$PACKAGE-$VERSION"
229 _install="$_wok/$pkg/install"
230 _fs="$_wok/$pkg/taz/$PACKAGE-$VERSION/fs"
231 _stuff="$_wok/$pkg/stuff"
233 # Package info.
234 echo '<div id="info">'
235 if [ -f "$wok/$pkg/receipt" ]; then
236 echo "<a href='cooker.cgi?receipt=$pkg'>receipt</a>"
237 unset WEB_SITE
238 . $wok/$pkg/receipt
240 [ -n "$WEB_SITE" ] && # busybox wget -s $WEB_SITE &&
241 echo "<a href='$WEB_SITE'>home</a>"
243 if [ -f "$wok/$pkg/taz/$PACKAGE-$VERSION/receipt" ]; then
244 echo "<a href='cooker.cgi?files=$pkg'>files</a>"
245 unset EXTRAVERSION
246 . $wok/$pkg/taz/$PACKAGE-$VERSION/receipt
247 if [ -f $wok/$pkg/taz/$PACKAGE-$VERSION/description.txt ]; then
248 echo "<a href='cooker.cgi?description=$pkg'>description</a>"
249 fi
250 if [ -f $PKGS/$PACKAGE-$VERSION$EXTRAVERSION.tazpkg ]; then
251 echo "<a href='cooker.cgi?download=$PACKAGE-$VERSION$EXTRAVERSION.tazpkg'>download</a>"
252 fi
253 if [ -f $PKGS/$PACKAGE-$VERSION$EXTRAVERSION-$ARCH.tazpkg ]; then
254 echo "<a href='cooker.cgi?download=$PACKAGE-$VERSION$EXTRAVERSION-$ARCH.tazpkg'>download</a>"
255 fi
256 fi
257 echo "<a href='ftp://${HTTP_HOST%:*}/$pkg/'>browse</a>"
258 else
259 if [ $(ls $wok/*$pkg*/receipt 2> /dev/null | wc -l) -eq 0 ]; then
260 echo "No package named: $pkg"
261 else
262 ls $wok/$pkg/receipt >/dev/null 2>&1 || pkg="*$pkg*"
263 echo '<table style="width:100%">'
264 for i in $(cd $wok ; ls $pkg/receipt); do
265 pkg=$(dirname $i)
266 unset SHORT_DESC CATEGORY
267 . $wok/$pkg/receipt
268 cat <<EOT
269 <tr>
270 <td><a href="cooker.cgi?pkg=$pkg">$pkg</a></td>
271 <td>$SHORT_DESC</td>
272 <td>$CATEGORY</td>
273 </tr>
274 EOT
275 done
276 echo '</table>'
277 unset pkg
278 fi
279 fi
280 echo '</div>'
282 # Check for a log file and display summary if it exists.
283 if [ -f "$log" ]; then
284 if grep -q "cook:$pkg$" $command; then
285 echo "<pre>The Cooker is currently building: $pkg</pre>"
286 fi
287 if fgrep -q "Summary for:" $LOGS/$pkg.log; then
288 echo '<h3>Cook summary</h3>'
289 echo '<pre>'
290 grep -A 12 "^Summary for:" $LOGS/$pkg.log | sed /^$/d | \
291 syntax_highlighter log
292 echo '</pre>'
293 fi
294 if fgrep -q "Debug information" $LOGS/$pkg.log; then
295 echo '<h3>Cook failed</h3>'
296 echo '<pre>'
297 grep -A 8 "^Debug information" $LOGS/$pkg.log | sed /^$/d | \
298 syntax_highlighter log
299 echo '</pre>'
300 fi
301 echo '<h3>Cook log</h3>'
302 echo '<pre>'
303 cat $log | syntax_highlighter log
304 echo '</pre>'
305 case "$HTTP_USER_AGENT" in
306 *SliTaz*)
307 [ -f $CACHE/cooker-request ] && [ -n "$HTTP_REFERER" ] &&
308 echo "<a class=\"button\" href=\"cooker.cgi?recook=$pkg\">Recook $pkg</a>"
309 esac
310 else
311 [ "$pkg" ] && echo "<pre>No log: $pkg</pre>"
312 fi ;;
314 file=*)
315 # Don't allow all files on the system for security reasons.
316 file=${QUERY_STRING#file=}
317 case "$file" in
318 activity|cooknotes|cooklist)
319 [ "$file" == "cooklist" ] && \
320 nb="- Packages: $(cat $cooklist | wc -l)"
321 echo "<h2>DB: $file $nb</h2>"
322 echo '<pre>'
323 tac $CACHE/$file | syntax_highlighter activity
324 echo '</pre>' ;;
326 broken)
327 nb=$(cat $broken | wc -l)
328 echo "<h2>DB: broken - Packages: $nb</h2>"
329 echo '<pre>'
330 cat $CACHE/$file | sort | \
331 sed s"#^[^']*#<a href='cooker.cgi?pkg=\0'>\0</a>#"g
332 echo '</pre>' ;;
334 *.diff)
335 diff=$CACHE/$file
336 echo "<h2>Diff for: ${file%.diff}</h2>"
337 [ "$file" == "installed.diff" ] && echo \
338 "<p>This is the latest diff between installed packages \
339 and installed build dependencies to cook.</p>"
340 echo '<pre>'
341 cat $diff | syntax_highlighter diff
342 echo '</pre>' ;;
344 *.log)
345 log=$LOGS/$file
346 name=$(basename $log)
347 echo "<h2>Log for: ${name%.log}</h2>"
348 if [ -f "$log" ]; then
349 if fgrep -q "Summary" $log; then
350 echo '<pre>'
351 grep -A 20 "^Summary" $log | sed /^$/d | \
352 syntax_highlighter log
353 echo '</pre>'
354 fi
355 echo '<pre>'
356 cat $log | syntax_highlighter log
357 echo '</pre>'
358 else
359 echo "<pre>No log file: $log</pre>"
360 fi ;;
361 esac ;;
363 stuff=*)
364 file=${QUERY_STRING#stuff=}
365 echo "<h2>$file</h2>"
366 echo '<pre>'
367 cat $wok/$file | sed 's/&/\&amp;/g;s/</\&lt;/g;s/>/\&gt;/g'
368 echo '</pre>' ;;
370 receipt=*)
371 pkg=${QUERY_STRING#receipt=}
372 echo "<h2>Receipt for: $pkg</h2>"
373 if [ -f "$wok/$pkg/receipt" ]; then
374 ( cd $wok/$pkg ; find stuff -type f 2> /dev/null ) | \
375 while read file ; do
376 echo "<a href=\"?stuff=$pkg/$file\">$file</a>"
377 done
378 echo '<pre>'
379 cat $wok/$pkg/receipt | \
380 syntax_highlighter receipt
381 echo '</pre>'
382 else
383 echo "<pre>No receipt for: $pkg</pre>"
384 fi ;;
386 files=*)
387 pkg=${QUERY_STRING#files=}
388 dir=$(ls -d $WOK/$pkg/taz/$pkg-*)
389 if [ -d "$dir/fs" ]; then
390 echo "<h2>Installed files by: $pkg ($(du -hs $dir/fs | awk '{ print $1 }'))</h2>"
391 echo '<pre>'
392 find $dir/fs -not -type d -print0 | xargs -0 ls -ld | \
393 sed "s|\(.*\) /.*\(${dir#*wok}/fs\)\(.*\)|\1 <a href=\"?download=../wok\2\3\">\3</a>|;s|^\([^-].*\)\(<a.*\)\">\(.*\)</a>|\1\3|"
394 echo '</pre>'
395 else
396 echo "<pre>No files list for: $pkg</pre>"
397 fi ;;
399 description=*)
400 pkg=${QUERY_STRING#description=}
401 echo "<h2>Description of $pkg</h2>"
402 dir=$(ls -d $WOK/$pkg/taz/$pkg-*)
403 if [ -s "$dir/description.txt" ]; then
404 echo '<pre>'
405 cat $dir/description.txt | \
406 sed 's/&/\&amp;/g;s/</\&lt;/g;s/>/\&gt;/g'
407 echo '</pre>'
408 else
409 echo "<pre>No description for: $pkg</pre>"
410 fi ;;
412 *)
413 # We may have a toolchain.cgi script for cross cooker's
414 if [ -f "toolchain.cgi" ]; then
415 toolchain='toolchain.cgi'
416 else
417 toolchain='cooker.cgi?pkg=slitaz-toolchain'
418 fi
419 # Main page with summary. Count only package include in ARCH,
420 # use 'cooker arch-db' to manually create arch.$ARCH files.
421 inwok=$(ls $WOK/*/arch.$ARCH | wc -l)
422 cooked=$(ls $PKGS/*.tazpkg | wc -l)
423 unbuilt=$(($inwok - $cooked))
424 pct=0
425 [ $inwok -gt 0 ] && pct=$(( ($cooked * 100) / $inwok ))
426 cat <<EOT
427 <div style="float: right;">
428 <form method="get" action="$SCRIPT_NAME">
429 Package:
430 <input type="text" name="pkg" />
431 </form>
432 </div>
434 <h2>Summary</h2>
436 <pre>
437 Running command : $(running_command)
438 Wok revision : <a href="$WOK_URL">$(cat $wokrev)</a>
439 Commits to cook : $(cat $commits | wc -l)
440 Current cooklist : $(cat $cooklist | wc -l)
441 Broken packages : $(cat $broken | wc -l)
442 Blocked packages : $(cat $blocked | wc -l)
443 </pre>
444 EOT
445 [ -e $CACHE/cooker-request ] &&
446 [ $CACHE/activity -nt $CACHE/cooker-request ] && cat <<EOT
447 <div style="float: right;">
448 <a class="button" href="cooker.cgi?poke">Poke cooker</a>
449 </div>
450 EOT
451 cat <<EOT
452 <p class="info">
453 Packages: $inwok in the wok | $cooked cooked | $unbuilt unbuilt |
454 Server date: $(date -u '+%F %R %Z')
455 </p>
456 <div class="pctbar">
457 <div class="pct" style="width: ${pct}%;">${pct}%</div>
458 </div>
460 <p>
461 Latest:
462 <a href="cooker.cgi?file=cookorder.log">cookorder.log</a>
463 <a href="cooker.cgi?file=commits.log">commits.log</a>
464 <a href="cooker.cgi?file=pkgdb.log">pkgdb.log</a>
465 <a href="cooker.cgi?file=installed.diff">installed.diff</a>
466 - Architecture $ARCH:
467 <a href="$toolchain">toolchain</a>
468 </p>
470 $(more_button activity "More activity" $CACHE/activity 12)
471 <h2 id="activity">Activity</h2>
472 <pre>
473 $(tac $CACHE/activity | head -n 12 | syntax_highlighter activity)
474 </pre>
475 EOT
477 [ -s $cooknotes ] && cat <<EOT
478 $(more_button cooknotes "More notes" $cooknotes 12)
479 <h2 id="cooknotes">Cooknotes</h2>
480 <pre>
481 $(tac $cooknotes | head -n 12 | syntax_highlighter activity)
482 </pre>
483 EOT
485 [ -s $commits ] && cat <<EOT
486 <h2 id="commits">Commits</h2>
487 <pre>
488 $(cat $commits)
489 </pre>
490 EOT
492 [ -s $cooklist ] && cat <<EOT
493 $(more_button cooklist "Full cooklist" $cooklist 20)
494 <h2 id="cooklist">Cooklist</h2>
495 <pre>
496 $(cat $cooklist | head -n 20)
497 </pre>
498 EOT
500 [ -s $broken ] && cat <<EOT
501 $(more_button broken "All broken packages" $broken 20)
502 <h2 id="broken">Broken</h2>
503 <pre>
504 $(cat $broken | head -n 20 | sed s"#^[^']*#<a href='cooker.cgi?pkg=\0'>\0</a>#"g)
505 </pre>
506 EOT
508 [ -s $blocked ] && cat <<EOT
509 <h2 id="blocked">Blocked</h2>
510 <pre>
511 $(cat $blocked | sed s"#^[^']*#<a href='cooker.cgi?pkg=\0'>\0</a>#"g)
512 </pre>
513 EOT
515 cat <<EOT
516 <h2 id="lastcook">Latest cook</h2>
517 <pre>
518 $(list_packages | sed s"#^\([^']*\).* : #<span class='log-date'>\0</span>#"g)
519 </pre>
520 EOT
521 ;;
522 esac
525 # Close xHTML page
527 cat <<EOT
528 </div>
530 <div id="footer">
531 <a href="http://www.slitaz.org/">SliTaz Website</a>
532 <a href="cooker.cgi">Cooker</a>
533 <a href="http://hg.slitaz.org/cookutils/raw-file/tip/doc/cookutils.en.html">
534 Documentation</a>
535 </div>
537 </body>
538 </html>
539 EOT
541 exit 0