cookutils view web/cooker.cgi @ rev 784

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