cookutils view web/cooker.cgi @ rev 697

cookutils: catch control-c in aufs jail (again)
author Pascal Bellard <pascal.bellard@slitaz.org>
date Sat Oct 25 11:27:23 2014 +0200 (2014-10-25)
parents 5bcc3be62d13
children d81d9cd03f7b
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 wokrev="$CACHE/wokrev"
23 # We're not logged and want time zone to display correct server date.
24 export TZ=$(cat /etc/TZ)
26 if [ "${QUERY_STRING%%=*}" == "download" ]; then
27 file=$PKGS/${QUERY_STRING#*=}
28 cat <<EOT
29 Content-Type: application/octet-stream
30 Content-Length: $(stat -c %s $file)
31 Content-Disposition: attachment; filename=$(basename $file)
33 EOT
34 cat $file
35 exit
36 fi
38 echo -n "Content-Type: "
39 if [ "$QUERY_STRING" == "rss" ]; then
40 echo "application/rss+xml"
41 else
42 echo "text/html; charset=utf-8"
43 fi
44 echo ""
46 # RSS feed generator
47 if [ "$QUERY_STRING" == "rss" ]; then
48 pubdate=$(date -R)
49 cat << EOT
50 <?xml version="1.0" encoding="utf-8" ?>
51 <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
52 <channel>
53 <title>SliTaz Cooker</title>
54 <description>The SliTaz packages cooker feed</description>
55 <link>$COOKER_URL</link>
56 <lastBuildDate>$pubdate</lastBuildDate>
57 <pubDate>$pubdate</pubDate>
58 <atom:link href="http://cook.slitaz.org/cooker.cgi?rss" rel="self" type="application/rss+xml" />
59 EOT
60 for rss in $(ls -lt $FEEDS/*.xml | head -n 12)
61 do
62 cat $rss | sed 's|<guid|& isPermaLink="false"|g;s|</pubDate| GMT&|g'
63 done
64 cat << EOT
65 </channel>
66 </rss>
67 EOT
68 exit 0
69 fi
71 #
72 # Functions
73 #
75 # Put some colors in log and DB files.
76 syntax_highlighter() {
77 case $1 in
78 log)
79 sed -e 's/&/\&amp;/g;s/</\&lt;/g;s/>/\&gt;/g' \
80 -e 's#OK$#<span class="span-ok">OK</span>#g' \
81 -e 's#Done$#<span class="span-ok">Done</span>#g' \
82 -e 's#yes$#<span class="span-ok">yes</span>#g' \
83 -e 's#no$#<span class="span-no">no</span>#g' \
84 -e 's#error$#<span class="span-red">error</span>#g' \
85 -e 's#ERROR:#<span class="span-red">ERROR:</span>#g' \
86 -e 's#WARNING:#<span class="span-red">WARNING:</span>#g' \
87 -e s"#^Executing:\([^']*\).#<span class='sh-val'>\0</span>#"g \
88 -e s"#^====\([^']*\).#<span class='span-line'>\0</span>#"g \
89 -e s"#^[a-zA-Z0-9]\([^']*\) :: #<span class='span-sky'>\0</span>#"g \
90 -e s"#ftp://[^ '\"]*#<a href='\0'>\0</a>#"g \
91 -e s"#http://[^ '\"]*#<a href='\0'>\0</a>#"g ;;
92 receipt)
93 sed -e s'|&|\&amp;|g' -e 's|<|\&lt;|g' -e 's|>|\&gt;|'g \
94 -e s"#^\#\([^']*\)#<span class='sh-comment'>\0</span>#"g \
95 -e s"#\"\([^']*\)\"#<span class='sh-val'>\0</span>#"g ;;
96 diff)
97 sed -e 's|&|\&amp;|g' -e 's|<|\&lt;|g' -e 's|>|\&gt;|g' \
98 -e s"#^-\([^']*\).#<span class='span-red'>\0</span>#"g \
99 -e s"#^+\([^']*\).#<span class='span-ok'>\0</span>#"g \
100 -e s"#@@\([^']*\)@@#<span class='span-sky'>@@\1@@</span>#"g ;;
101 activity)
102 sed s"#^\([^']* : \)#<span class='log-date'>\0</span>#"g ;;
103 esac
104 }
106 # Latest build pkgs.
107 list_packages() {
108 cd $PKGS
109 ls -1t *.tazpkg | head -20 | \
110 while read file
111 do
112 echo -n $(stat -c '%y' $PKGS/$file | cut -d . -f 1 | sed s/:[0-9]*$//)
113 echo " : $file"
114 done
115 }
117 # Optional full list button
118 more_button() {
119 [ $(wc -l < ${3:-$CACHE/$1}) -gt ${4:-12} ] &&
120 echo "<a class=\"button\" href=\"cooker.cgi?file=$1\">$2</a>"
121 }
123 # xHTML header. Pages can be customized with a separated html.header file.
124 if [ -f "header.html" ]; then
125 cat header.html
126 else
127 cat << EOT
128 <!DOCTYPE html>
129 <html xmlns="http://www.w3.org/1999/xhtml">
130 <head>
131 <title>SliTaz Cooker</title>
132 <meta charset="utf-8" />
133 <link rel="shortcut icon" href="favicon.ico" />
134 <link rel="stylesheet" type="text/css" href="style.css" />
135 </head>
136 <body>
138 <div id="header">
139 <div id="logo"></div>
140 <h1><a href="cooker.cgi">SliTaz Cooker</a></h1>
141 </div>
143 <!-- Content -->
144 <div id="content">
145 EOT
146 fi
148 #
149 # Load requested page
150 #
152 case "${QUERY_STRING}" in
153 pkg=*)
154 pkg=${QUERY_STRING#pkg=}
155 log=$LOGS/$pkg.log
156 echo "<h2>Package: $pkg</h2>"
158 # Package info.
159 echo '<div id="info">'
160 if [ -f "$wok/$pkg/receipt" ]; then
161 echo "<a href='cooker.cgi?receipt=$pkg'>receipt</a>"
162 unset WEB_SITE
163 . $wok/$pkg/receipt
164 [ -n "$WEB_SITE" ] && # busybox wget -s $WEB_SITE &&
165 echo "<a href='$WEB_SITE'>home</a>"
166 if [ -f "$wok/$pkg/taz/$PACKAGE-$VERSION/receipt" ]; then
167 echo "<a href='cooker.cgi?files=$pkg'>files</a>"
168 unset EXTRAVERSION
169 . $wok/$pkg/taz/$PACKAGE-$VERSION/receipt
170 if [ -f $wok/$pkg/taz/$PACKAGE-$VERSION/description.txt ]; then
171 echo "<a href='cooker.cgi?description=$pkg'>description</a>"
172 fi
173 if [ -f $PKGS/$PACKAGE-$VERSION$EXTRAVERSION.tazpkg ]; then
174 echo "<a href='cooker.cgi?download=$PACKAGE-$VERSION$EXTRAVERSION.tazpkg'>download</a>"
175 fi
176 if [ -f $PKGS/$PACKAGE-$VERSION$EXTRAVERSION-$ARCH.tazpkg ]; then
177 echo "<a href='cooker.cgi?download=$PACKAGE-$VERSION$EXTRAVERSION-$ARCH.tazpkg'>download</a>"
178 fi
179 echo "<a href='ftp://${HTTP_HOST%:*}/$pkg/'>browse</a>"
180 fi
181 else
182 echo "No package named: $pkg"
183 fi
184 echo '</div>'
186 # Check for a log file and display summary if it exists.
187 if [ -f "$log" ]; then
188 if grep -q "cook:$pkg$" $command; then
189 echo "<pre>The Cooker is currently building: $pkg</pre>"
190 fi
191 if fgrep -q "Summary for:" $LOGS/$pkg.log; then
192 echo "<h3>Cook summary</h3>"
193 echo '<pre>'
194 grep -A 12 "^Summary for:" $LOGS/$pkg.log | sed /^$/d | \
195 syntax_highlighter log
196 echo '</pre>'
197 fi
198 if fgrep -q "Debug information" $LOGS/$pkg.log; then
199 echo "<h3>Cook failed</h3>"
200 echo '<pre>'
201 grep -A 8 "^Debug information" $LOGS/$pkg.log | sed /^$/d | \
202 syntax_highlighter log
203 echo '</pre>'
204 fi
205 echo "<h3>Cook log</h3>"
206 echo '<pre>'
207 cat $log | syntax_highlighter log
208 echo '</pre>'
209 else
210 echo "<pre>No log: $pkg</pre>"
211 fi ;;
212 file=*)
213 # Dont allow all files on the system for security reasons.
214 file=${QUERY_STRING#file=}
215 case "$file" in
216 activity|cooknotes|cooklist)
217 [ "$file" == "cooklist" ] && \
218 nb="- Packages: $(cat $cooklist | wc -l)"
219 echo "<h2>DB: $file $nb</h2>"
220 echo '<pre>'
221 tac $CACHE/$file | syntax_highlighter activity
222 echo '</pre>' ;;
223 broken)
224 nb=$(cat $broken | wc -l)
225 echo "<h2>DB: broken - Packages: $nb</h2>"
226 echo '<pre>'
227 cat $CACHE/$file | sort | \
228 sed s"#^[^']*#<a href='cooker.cgi?pkg=\0'>\0</a>#"g
229 echo '</pre>' ;;
230 *.diff)
231 diff=$CACHE/$file
232 echo "<h2>Diff for: ${file%.diff}</h2>"
233 [ "$file" == "installed.diff" ] && echo \
234 "<p>This is the latest diff between installed packages \
235 and installed build dependencies to cook.</p>"
236 echo '<pre>'
237 cat $diff | syntax_highlighter diff
238 echo '</pre>' ;;
239 *.log)
240 log=$LOGS/$file
241 name=$(basename $log)
242 echo "<h2>Log for: ${name%.log}</h2>"
243 if [ -f "$log" ]; then
244 if fgrep -q "Summary" $log; then
245 echo '<pre>'
246 grep -A 20 "^Summary" $log | sed /^$/d | \
247 syntax_highlighter log
248 echo '</pre>'
249 fi
250 echo '<pre>'
251 cat $log | syntax_highlighter log
252 echo '</pre>'
253 else
254 echo "<pre>No log file: $log</pre>"
255 fi ;;
256 esac ;;
257 stuff=*)
258 file=${QUERY_STRING#stuff=}
259 echo "<h2>$file</h2>"
260 echo '<pre>'
261 cat $wok/$file | sed 's/&/\&amp;/g;s/</\&lt;/g;s/>/\&gt;/g'
262 echo '</pre>' ;;
263 receipt=*)
264 pkg=${QUERY_STRING#receipt=}
265 echo "<h2>Receipt for: $pkg</h2>"
266 if [ -f "$wok/$pkg/receipt" ]; then
267 ( cd $wok/$pkg ; find stuff -type f 2> /dev/null ) | \
268 while read file ; do
269 echo "<a href=\"?stuff=$pkg/$file\">$file</a>"
270 done
271 echo '<pre>'
272 cat $wok/$pkg/receipt | \
273 syntax_highlighter receipt
274 echo '</pre>'
275 else
276 echo "<pre>No receipt for: $pkg</pre>"
277 fi ;;
278 files=*)
279 pkg=${QUERY_STRING#files=}
280 echo "<h2>Installed files by: $pkg</h2>"
281 dir=$(ls -d $WOK/$pkg/taz/$pkg-*)
282 if [ -d "$dir/fs" ]; then
283 echo '<pre>'
284 find $dir/fs -not -type d | xargs ls -ld | \
285 sed "s|\(.*\) /.*\(${dir#*wok}/fs\)\(.*\)|\1 <a href=\"?download=../wok\2\3\">\3</a>|;s|^\([^-].*\)\(<a.*\)\">\(.*\)</a>|\1\3|"
286 echo '</pre>'
287 else
288 echo "<pre>No files list for: $pkg</pre>"
289 fi ;;
290 description=*)
291 pkg=${QUERY_STRING#description=}
292 echo "<h2>Description of $pkg</h2>"
293 dir=$(ls -d $WOK/$pkg/taz/$pkg-*)
294 if [ -s "$dir/description.txt" ]; then
295 echo '<pre>'
296 cat $dir/description.txt | \
297 sed 's/&/\&amp;/g;s/</\&lt;/g;s/>/\&gt;/g'
298 echo '</pre>'
299 else
300 echo "<pre>No description for: $pkg</pre>"
301 fi ;;
302 *)
303 # We may have a toolchain.cgi script for cross cooker's
304 if [ -f "toolchain.cgi" ]; then
305 toolchain='toolchain.cgi'
306 else
307 toolchain='cooker.cgi?pkg=slitaz-toolchain'
308 fi
309 # Main page with summary. Count only package include in ARCH,
310 # use 'cooker arch-db' to manually create arch.$ARCH files.
311 inwok=$(ls $WOK/*/arch.$ARCH | wc -l)
312 cooked=$(ls $PKGS/*.tazpkg | wc -l)
313 unbuilt=$(($inwok - $cooked))
314 pct=0
315 [ $inwok -gt 0 ] && pct=$(( ($cooked * 100) / $inwok ))
316 cat << EOT
317 <div style="float: right;">
318 <form method="get" action="$SCRIPT_NAME">
319 Package:
320 <input type="text" name="pkg" />
321 </form>
322 </div>
324 <h2>Summary</h2>
326 <pre>
327 Running command : $([ -s "$command" ] && cat $command || echo "Not running")
328 Wok revision : <a href="$WOK_URL">$(cat $wokrev)</a>
329 Commits to cook : $(cat $commits | wc -l)
330 Current cooklist : $(cat $cooklist | wc -l)
331 Broken packages : $(cat $broken | wc -l)
332 Blocked packages : $(cat $blocked | wc -l)
333 </pre>
335 <p class="info">
336 Packages: $inwok in the wok - $cooked cooked - $unbuilt unbuilt -
337 Server date: $(date -u '+%Y-%m-%d %H:%M %Z')
338 </p>
339 <div class="pctbar">
340 <div class="pct" style="width: ${pct}%;">${pct}%</div>
341 </div>
343 <p>
344 Latest:
345 <a href="cooker.cgi?file=cookorder.log">cookorder.log</a>
346 <a href="cooker.cgi?file=commits.log">commits.log</a>
347 <a href="cooker.cgi?file=installed.diff">installed.diff</a>
348 - Architecture $ARCH:
349 <a href="$toolchain">toolchain</a>
350 </p>
352 <a name="activity"></a>
353 <h2>Activity</h2>
354 <pre>
355 $(tac $CACHE/activity | head -n 12 | syntax_highlighter activity)
356 </pre>
357 $(more_button activity "More activity" $CACHE/activity 12)
359 <a name="cooknotes"></a>
360 <h2>Cooknotes</h2>
361 <pre>
362 $(tac $cooknotes | head -n 12 | syntax_highlighter activity)
363 </pre>
364 $(more_button cooknotes "More notes" $cooknotes 12)
366 <a name="commits"></a>
367 <h2>Commits</h2>
368 <pre>
369 $(cat $commits)
370 </pre>
372 <a name="cooklist"></a>
373 <h2>Cooklist</h2>
374 <pre>
375 $(cat $cooklist | head -n 20)
376 </pre>
377 $(more_button cooklist "Full cooklist" $cooklist 20)
379 <a name="broken"></a>
380 <h2>Broken</h2>
381 <pre>
382 $(cat $broken | head -n 20 | sed s"#^[^']*#<a href='cooker.cgi?pkg=\0'>\0</a>#"g)
383 </pre>
384 $(more_button broken "All broken packages" $broken 20)
386 <a name="blocked"></a>
387 <h2>Blocked</h2>
388 <pre>
389 $(cat $blocked | sed s"#^[^']*#<a href='cooker.cgi?pkg=\0'>\0</a>#"g)
390 </pre>
392 <a name="lastcook"></a>
393 <h2>Latest cook</h2>
394 <pre>
395 $(list_packages | sed s"#^\([^']*\).* : #<span class='log-date'>\0</span>#"g)
396 </pre>
397 EOT
398 ;;
399 esac
401 # Close xHTML page
402 cat << EOT
403 </div>
405 <div id="footer">
406 <a href="http://www.slitaz.org/">SliTaz Website</a>
407 <a href="cooker.cgi">Cooker</a>
408 <a href="http://hg.slitaz.org/cookutils/raw-file/tip/doc/cookutils.en.html">
409 Documentation</a>
410 </div>
412 </body>
413 </html>
414 EOT
416 exit 0