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