cookutils view web/cooker.cgi @ rev 796

cooker.cgi: allow recook from tazweb only
author Pascal Bellard <pascal.bellard@slitaz.org>
date Tue Apr 05 09:58:36 2016 +0200 (2016-04-05)
parents bddeccba5e51
children 166a91b87ac8
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 sed -e 's/&/\&amp;/g;s/</\&lt;/g;s/>/\&gt;/g' \
107 -e 's#OK$#<span class="span-ok">OK</span>#g' \
108 -e 's#Done$#<span class="span-ok">Done</span>#g' \
109 -e 's#yes$#<span class="span-ok">yes</span>#g' \
110 -e 's#no$#<span class="span-no">no</span>#g' \
111 -e 's#error$#<span class="span-red">error</span>#g' \
112 -e 's#ERROR:#<span class="span-red">ERROR:</span>#g' \
113 -e 's#WARNING:#<span class="span-red">WARNING:</span>#g' \
114 -e s"#^Executing:\([^']*\).#<span class='sh-val'>\0</span>#"g \
115 -e s"#^====\([^']*\).#<span class='span-line'>\0</span>#"g \
116 -e s"#^[a-zA-Z0-9]\([^']*\) :: #<span class='span-sky'>\0</span>#"g \
117 -e s"#ftp://[^ '\"]*#<a href='\0'>\0</a>#"g \
118 -e s"#http://[^ '\"]*#<a href='\0'>\0</a>#"g ;;
120 receipt)
121 sed -e s'|&|\&amp;|g' -e 's|<|\&lt;|g' -e 's|>|\&gt;|'g \
122 -e s"#^\#\([^']*\)#<span class='sh-comment'>\0</span>#"g \
123 -e s"#\"\([^']*\)\"#<span class='sh-val'>\0</span>#"g ;;
125 diff)
126 sed -e 's|&|\&amp;|g' -e 's|<|\&lt;|g' -e 's|>|\&gt;|g' \
127 -e s"#^-\([^']*\).#<span class='span-red'>\0</span>#"g \
128 -e s"#^+\([^']*\).#<span class='span-ok'>\0</span>#"g \
129 -e s"#@@\([^']*\)@@#<span class='span-sky'>@@\1@@</span>#"g ;;
131 activity)
132 sed s"#^\([^']* : \)#<span class='log-date'>\0</span>#"g ;;
133 esac
134 }
137 # Latest build pkgs.
139 list_packages() {
140 cd $PKGS
141 ls -1t *.tazpkg | head -20 | \
142 while read file; do
143 echo -n $(TZ=UTC stat -c '%y' $PKGS/$file | cut -d . -f 1 | sed s/:[0-9]*$//)
144 echo " : $file"
145 done
146 }
149 # Optional full list button
151 more_button() {
152 [ $(wc -l < ${3:-$CACHE/$1}) -gt ${4:-12} ] &&
153 echo "<a class=\"button\" href=\"cooker.cgi?file=$1\">$2</a>"
154 }
157 # Show the running command and its progression
159 running_command()
160 {
161 local state="Not running"
162 if [ -s "$command" ]; then
163 state="$(cat $command)"
164 if grep -q "^$state" $cooktime ; then
165 set -- $(cat $cooktime)
166 state="$state $((($(date +%s)-$3)*100/$2))%"
167 [ $2 -gt 300 ] && state="$state (should end $(date -u -d @$(($2+$3))))"
168 fi
169 fi
170 echo $state
171 }
174 # xHTML header. Pages can be customized with a separated html.header file.
176 if [ -f "header.html" ]; then
177 cat header.html
178 else
179 cat <<EOT
180 <!DOCTYPE html>
181 <html lang="en">
182 <head>
183 <meta charset="utf-8"/>
184 <title>SliTaz Cooker</title>
185 <link rel="shortcut icon" href="favicon.ico"/>
186 <link rel="stylesheet" type="text/css" href="style.css"/>
187 <meta name="robots" content="nofollow">
188 </head>
189 <body>
191 <div id="header">
192 <div id="logo"></div>
193 <h1><a href="cooker.cgi">SliTaz Cooker</a></h1>
194 </div>
196 <!-- Content -->
197 <div id="content">
198 EOT
199 fi
202 #
203 # Load requested page
204 #
206 case "${QUERY_STRING}" in
207 pkg=*)
208 pkg=${QUERY_STRING#pkg=}
209 log=$LOGS/$pkg.log
210 echo "<h2>Package: $pkg</h2>"
212 # Package info.
213 echo '<div id="info">'
214 if [ -f "$wok/$pkg/receipt" ]; then
215 echo "<a href='cooker.cgi?receipt=$pkg'>receipt</a>"
216 unset WEB_SITE
217 . $wok/$pkg/receipt
219 [ -n "$WEB_SITE" ] && # busybox wget -s $WEB_SITE &&
220 echo "<a href='$WEB_SITE'>home</a>"
222 if [ -f "$wok/$pkg/taz/$PACKAGE-$VERSION/receipt" ]; then
223 echo "<a href='cooker.cgi?files=$pkg'>files</a>"
224 unset EXTRAVERSION
225 . $wok/$pkg/taz/$PACKAGE-$VERSION/receipt
226 if [ -f $wok/$pkg/taz/$PACKAGE-$VERSION/description.txt ]; then
227 echo "<a href='cooker.cgi?description=$pkg'>description</a>"
228 fi
229 if [ -f $PKGS/$PACKAGE-$VERSION$EXTRAVERSION.tazpkg ]; then
230 echo "<a href='cooker.cgi?download=$PACKAGE-$VERSION$EXTRAVERSION.tazpkg'>download</a>"
231 fi
232 if [ -f $PKGS/$PACKAGE-$VERSION$EXTRAVERSION-$ARCH.tazpkg ]; then
233 echo "<a href='cooker.cgi?download=$PACKAGE-$VERSION$EXTRAVERSION-$ARCH.tazpkg'>download</a>"
234 fi
235 fi
236 echo "<a href='ftp://${HTTP_HOST%:*}/$pkg/'>browse</a>"
237 else
238 if [ $(ls $wok/*$pkg*/receipt 2> /dev/null | wc -l) -eq 0 ]; then
239 echo "No package named: $pkg"
240 else
241 ls $wok/$pkg/receipt >/dev/null 2>&1 || pkg="*$pkg*"
242 echo '<table style="width:100%">'
243 for i in $(cd $wok ; ls $pkg/receipt); do
244 pkg=$(dirname $i)
245 unset SHORT_DESC CATEGORY
246 . $wok/$pkg/receipt
247 cat <<EOT
248 <tr>
249 <td><a href="cooker.cgi?pkg=$pkg">$pkg</a></td>
250 <td>$SHORT_DESC</td>
251 <td>$CATEGORY</td>
252 </tr>
253 EOT
254 done
255 echo '</table>'
256 unset pkg
257 fi
258 fi
259 echo '</div>'
261 # Check for a log file and display summary if it exists.
262 if [ -f "$log" ]; then
263 if grep -q "cook:$pkg$" $command; then
264 echo "<pre>The Cooker is currently building: $pkg</pre>"
265 fi
266 if fgrep -q "Summary for:" $LOGS/$pkg.log; then
267 echo '<h3>Cook summary</h3>'
268 echo '<pre>'
269 grep -A 12 "^Summary for:" $LOGS/$pkg.log | sed /^$/d | \
270 syntax_highlighter log
271 echo '</pre>'
272 fi
273 if fgrep -q "Debug information" $LOGS/$pkg.log; then
274 echo '<h3>Cook failed</h3>'
275 echo '<pre>'
276 grep -A 8 "^Debug information" $LOGS/$pkg.log | sed /^$/d | \
277 syntax_highlighter log
278 echo '</pre>'
279 fi
280 echo '<h3>Cook log</h3>'
281 echo '<pre>'
282 cat $log | syntax_highlighter log
283 echo '</pre>'
284 case "$HTTP_USER_AGENT" in
285 *SliTaz*)
286 [ -f $CACHE/cooker-request ] && [ -n "$HTTP_REFERER" ] &&
287 echo "<a class=\"button\" href=\"cooker.cgi?recook=$pkg\">Recook $pkg</a>"
288 esac
289 else
290 [ "$pkg" ] && echo "<pre>No log: $pkg</pre>"
291 fi ;;
293 file=*)
294 # Don't allow all files on the system for security reasons.
295 file=${QUERY_STRING#file=}
296 case "$file" in
297 activity|cooknotes|cooklist)
298 [ "$file" == "cooklist" ] && \
299 nb="- Packages: $(cat $cooklist | wc -l)"
300 echo "<h2>DB: $file $nb</h2>"
301 echo '<pre>'
302 tac $CACHE/$file | syntax_highlighter activity
303 echo '</pre>' ;;
305 broken)
306 nb=$(cat $broken | wc -l)
307 echo "<h2>DB: broken - Packages: $nb</h2>"
308 echo '<pre>'
309 cat $CACHE/$file | sort | \
310 sed s"#^[^']*#<a href='cooker.cgi?pkg=\0'>\0</a>#"g
311 echo '</pre>' ;;
313 *.diff)
314 diff=$CACHE/$file
315 echo "<h2>Diff for: ${file%.diff}</h2>"
316 [ "$file" == "installed.diff" ] && echo \
317 "<p>This is the latest diff between installed packages \
318 and installed build dependencies to cook.</p>"
319 echo '<pre>'
320 cat $diff | syntax_highlighter diff
321 echo '</pre>' ;;
323 *.log)
324 log=$LOGS/$file
325 name=$(basename $log)
326 echo "<h2>Log for: ${name%.log}</h2>"
327 if [ -f "$log" ]; then
328 if fgrep -q "Summary" $log; then
329 echo '<pre>'
330 grep -A 20 "^Summary" $log | sed /^$/d | \
331 syntax_highlighter log
332 echo '</pre>'
333 fi
334 echo '<pre>'
335 cat $log | syntax_highlighter log
336 echo '</pre>'
337 else
338 echo "<pre>No log file: $log</pre>"
339 fi ;;
340 esac ;;
342 stuff=*)
343 file=${QUERY_STRING#stuff=}
344 echo "<h2>$file</h2>"
345 echo '<pre>'
346 cat $wok/$file | sed 's/&/\&amp;/g;s/</\&lt;/g;s/>/\&gt;/g'
347 echo '</pre>' ;;
349 receipt=*)
350 pkg=${QUERY_STRING#receipt=}
351 echo "<h2>Receipt for: $pkg</h2>"
352 if [ -f "$wok/$pkg/receipt" ]; then
353 ( cd $wok/$pkg ; find stuff -type f 2> /dev/null ) | \
354 while read file ; do
355 echo "<a href=\"?stuff=$pkg/$file\">$file</a>"
356 done
357 echo '<pre>'
358 cat $wok/$pkg/receipt | \
359 syntax_highlighter receipt
360 echo '</pre>'
361 else
362 echo "<pre>No receipt for: $pkg</pre>"
363 fi ;;
365 files=*)
366 pkg=${QUERY_STRING#files=}
367 dir=$(ls -d $WOK/$pkg/taz/$pkg-*)
368 if [ -d "$dir/fs" ]; then
369 echo "<h2>Installed files by: $pkg ($(du -hs $dir/fs | awk '{ print $1 }'))</h2>"
370 echo '<pre>'
371 find $dir/fs -not -type d -print0 | xargs -0 ls -ld | \
372 sed "s|\(.*\) /.*\(${dir#*wok}/fs\)\(.*\)|\1 <a href=\"?download=../wok\2\3\">\3</a>|;s|^\([^-].*\)\(<a.*\)\">\(.*\)</a>|\1\3|"
373 echo '</pre>'
374 else
375 echo "<pre>No files list for: $pkg</pre>"
376 fi ;;
378 description=*)
379 pkg=${QUERY_STRING#description=}
380 echo "<h2>Description of $pkg</h2>"
381 dir=$(ls -d $WOK/$pkg/taz/$pkg-*)
382 if [ -s "$dir/description.txt" ]; then
383 echo '<pre>'
384 cat $dir/description.txt | \
385 sed 's/&/\&amp;/g;s/</\&lt;/g;s/>/\&gt;/g'
386 echo '</pre>'
387 else
388 echo "<pre>No description for: $pkg</pre>"
389 fi ;;
391 *)
392 # We may have a toolchain.cgi script for cross cooker's
393 if [ -f "toolchain.cgi" ]; then
394 toolchain='toolchain.cgi'
395 else
396 toolchain='cooker.cgi?pkg=slitaz-toolchain'
397 fi
398 # Main page with summary. Count only package include in ARCH,
399 # use 'cooker arch-db' to manually create arch.$ARCH files.
400 inwok=$(ls $WOK/*/arch.$ARCH | wc -l)
401 cooked=$(ls $PKGS/*.tazpkg | wc -l)
402 unbuilt=$(($inwok - $cooked))
403 pct=0
404 [ $inwok -gt 0 ] && pct=$(( ($cooked * 100) / $inwok ))
405 cat <<EOT
406 <div style="float: right;">
407 <form method="get" action="$SCRIPT_NAME">
408 Package:
409 <input type="text" name="pkg" />
410 </form>
411 </div>
413 <h2>Summary</h2>
415 <pre>
416 Running command : $(running_command)
417 Wok revision : <a href="$WOK_URL">$(cat $wokrev)</a>
418 Commits to cook : $(cat $commits | wc -l)
419 Current cooklist : $(cat $cooklist | wc -l)
420 Broken packages : $(cat $broken | wc -l)
421 Blocked packages : $(cat $blocked | wc -l)
422 </pre>
424 <p class="info">
425 Packages: $inwok in the wok | $cooked cooked | $unbuilt unbuilt |
426 Server date: $(date -u '+%F %R %Z')
427 </p>
428 <div class="pctbar">
429 <div class="pct" style="width: ${pct}%;">${pct}%</div>
430 </div>
432 <p>
433 Latest:
434 <a href="cooker.cgi?file=cookorder.log">cookorder.log</a>
435 <a href="cooker.cgi?file=commits.log">commits.log</a>
436 <a href="cooker.cgi?file=pkgdb.log">pkgdb.log</a>
437 <a href="cooker.cgi?file=installed.diff">installed.diff</a>
438 - Architecture $ARCH:
439 <a href="$toolchain">toolchain</a>
440 </p>
441 EOT
442 [ -e $CACHE/cooker-request ] &&
443 [ $CACHE/activity -nt $CACHE/cooker-request ] && cat <<EOT
445 <a class="button" href="cooker.cgi?poke">Poke cooker</a>
446 EOT
447 cat <<EOT
449 <h2 id="activity">Activity</h2>
450 <pre>
451 $(tac $CACHE/activity | head -n 12 | syntax_highlighter activity)
452 </pre>
453 $(more_button activity "More activity" $CACHE/activity 12)
456 <h2 id="cooknotes">Cooknotes</h2>
457 <pre>
458 $(tac $cooknotes | head -n 12 | syntax_highlighter activity)
459 </pre>
460 $(more_button cooknotes "More notes" $cooknotes 12)
463 <h2 id="commits">Commits</h2>
464 <pre>
465 $(cat $commits)
466 </pre>
469 <h2 id="cooklist">Cooklist</h2>
470 <pre>
471 $(cat $cooklist | head -n 20)
472 </pre>
473 $(more_button cooklist "Full cooklist" $cooklist 20)
476 <h2 id="broken">Broken</h2>
477 <pre>
478 $(cat $broken | head -n 20 | sed s"#^[^']*#<a href='cooker.cgi?pkg=\0'>\0</a>#"g)
479 </pre>
480 $(more_button broken "All broken packages" $broken 20)
483 <h2 id="blocked">Blocked</h2>
484 <pre>
485 $(cat $blocked | sed s"#^[^']*#<a href='cooker.cgi?pkg=\0'>\0</a>#"g)
486 </pre>
489 <h2 id="lastcook">Latest cook</h2>
490 <pre>
491 $(list_packages | sed s"#^\([^']*\).* : #<span class='log-date'>\0</span>#"g)
492 </pre>
493 EOT
494 ;;
495 esac
498 # Close xHTML page
500 cat <<EOT
501 </div>
503 <div id="footer">
504 <a href="http://www.slitaz.org/">SliTaz Website</a>
505 <a href="cooker.cgi">Cooker</a>
506 <a href="http://hg.slitaz.org/cookutils/raw-file/tip/doc/cookutils.en.html">
507 Documentation</a>
508 </div>
510 </body>
511 </html>
512 EOT
514 exit 0