autodist/autodist-cgi

323 lines
15 KiB
Bash
Executable File

#!/bin/bash
. /etc/sysconfig/autoport
NUM=`echo "$QUERY_STRING" | sed -n 's/^.*NUM=\([^&]*\).*$/\1/p' | sed "s/%20/ /g"`
HOST=`echo "$QUERY_STRING" | sed -n 's/^.*HOST=\([^&]*\).*$/\1/p' | sed "s/%20/ /g"`
AS_HOST=`echo "$QUERY_STRING" | sed -n 's/^.*AS_HOST=\([^&]*\).*$/\1/p' | sed "s/%20/ /g"`
SHOWLOG=`echo "$QUERY_STRING" | sed -n 's/^.*SHOWLOG=\([^&]*\).*$/\1/p' | sed "s/%20/ /g"`
SHOWINDEX=`echo "$QUERY_STRING" | sed -n 's/^.*SHOWINDEX=\([^&]*\).*$/\1/p' | sed "s/%20/ /g"`
LINES=`echo "$QUERY_STRING" | sed -n 's/^.*LINES=\([^&]*\).*$/\1/p' | sed "s/%20/ /g"`
FORMAT=`echo "$QUERY_STRING" | sed -n 's/^.*FORMAT=\([^&]*\).*$/\1/p' | sed "s/%20/ /g"`
[ ! "$FORMAT" ] && FORMAT=html
[ "$LINES" ] || LINES=5
[ "$HOST" ] || HOST=0
[ "$AS_HOST" ] || AS_HOST=0
[ "$AS_HOST" -a "$HOST" != "0" ] && HOST_ADD="&HOST=$HOST"
if [ "$AS_HOST" = "0" -a "$FORMAT" = "xml" ]; then
echo -e "Content-type: text/xml\nPragma: no-cache\n"
echo -n "<autodist><output><![CDATA["
RELOADTIME=3600000
else
echo -e "Content-type: text/html; charset=UTF-8\n"
fi
if [ "$SHOWINDEX" ]; then
echo "Host $AS_HOST:"
for i in `seq 0 ${#AUTOPORT_ARCH[*]}`; do
[ "${AUTOPORT_ARCH[$i]}" ] || continue
[ "${AUTOPORT_DISABLE[$i]}" -a "${AUTOPORT_DISABLE[$i]}" != "0" ] && continue
if [ "${AUTOPORT_UPDATE[$i]}" ]; then
echo "<a href=\"?NUM=$i$HOST_ADD\">${AUTOPORT_REPOSITORIES[$i]}(${AUTOPORT_ARCH[$i]})</a>&nbsp;"
elif [ "${AUTOPORT_CHROOT[$i]}" ]; then
echo "<a href=\"?NUM=$i$HOST_ADD\">${AUTOPORT_REPOSITORIES[$i]}(${AUTOPORT_ARCH[$i]})</a>&nbsp;"
elif [ "${AUTOPORT_NATIVE[$i]}" ]; then
echo "<a href=\"?NUM=$i$HOST_ADD\">${AUTOPORT_REPOSITORIES[$i]}(${AUTOPORT_ARCH[$i]})</a>&nbsp;"
elif [ "${AUTOPORT_CROSS[$i]}" ]; then
echo "<a href=\"?NUM=$i$HOST_ADD\">${AUTOPORT_REPOSITORIES[$i]}(${AUTOPORT_CROSS[$i]};${AUTOPORT_ARCH[$i]})</a>&nbsp;"
fi
done
echo "<br>"
exit 0
elif [ "$AS_HOST" = "0" ]; then
echo "Show: [<a href=\"?NUM=\">All hosts</a>] [<a href=\"?SHOWLOG=monitor\">Build hosts monitor</a>]<br>"
for h in `seq 0 ${#AUTOPORT_CGI_HOST[*]}`; do
curl "${AUTOPORT_CGI_HOST[$h]}?AS_HOST=$h&SHOWINDEX=1" 2>/dev/null
done
echo "<hr>"
fi
[ ! "$SHOWLOG" -a "$HOST" = "$AS_HOST" ] && echo "<h2>Host: `hostname -s` (`uname -m`, kernel `uname -r`)</h2>"
if [ "$SHOWLOG" -a "$HOST" != "$AS_HOST" ]; then
curl "${AUTOPORT_CGI_HOST[$HOST]}?AS_HOST=$HOST&SHOWLOG=$SHOWLOG&NUM=$NUM&LOG=$LOG" 2>/dev/null
else
for i in `seq 0 ${#AUTOPORT_ARCH[*]}`; do
[ "${AUTOPORT_ARCH[$i]}" ] || continue
[ "${AUTOPORT_DISABLE[$i]}" -a "${AUTOPORT_DISABLE[$i]}" != "0" ] && continue
if [ "${AUTOPORT_UPDATE[$i]}" ]; then
LOGFILE="/var/autodist/log/autodist.log"
LASTLOGFILE="/var/autodist/log/autodist-last.log"
elif [ "${AUTOPORT_CHROOT[$i]}" ]; then
LOGFILE="/var/autodist/log/autoport-chroot-${AUTOPORT_CHROOT[$i]}.log"
LASTLOGFILE="/var/autodist/log/autoport-chroot-${AUTOPORT_CHROOT[$i]}-last.log"
AUTODISTLOGFILE="/var/autoport/${AUTOPORT_CHROOT[$i]}/home/${AUTOPORT_CHROOT_USER[$i]}/.autoport/${AUTOPORT_ARCH[$i]}/${AUTOPORT_REPOSITORIES[$i]}.log"
elif [ "${AUTOPORT_NATIVE[$i]}" ]; then
LOGFILE="/var/autodist/log/autoport-native-${AUTOPORT_REPOSITORIES[$i]}-${AUTOPORT_ARCH[$i]}.log"
LASTLOGFILE="/var/autodist/log/autoport-native-${AUTOPORT_REPOSITORIES[$i]}-${AUTOPORT_ARCH[$i]}-last.log"
AUTODISTLOGFILE="/var/autodist/.autoport/${AUTOPORT_ARCH[$i]}/${AUTOPORT_REPOSITORIES[$i]}.log"
elif [ "${AUTOPORT_CROSS[$i]}" ]; then
LOGFILE="/var/autodist/log/autoport-cross-${AUTOPORT_CROSS[$i]}.log"
LASTLOGFILE="/var/autodist/log/autoport-cross-${AUTOPORT_CROSS[$i]}-last.log"
else
continue
fi
if [ "$SHOWLOG" = "monitor" ]; then
echo "<h2>Build hosts monitor</h2>"
icecream-monitor localhost listcs | \
while read line; do
[ "${line:0:8}" = "200 done" ] && printon=
if [ "$printon" ]; then
set -- $line
case $2 in
\(*) echo "<b>$line</b><br>" ;;
*) echo "$line<br>" ;;
esac
fi
[ "${line:0:6}" = "listcs" ] && printon=1
done
echo "<br>"
RELOADTIME=15000
break
elif [ "$SHOWLOG" ]; then
if [ "$HOST" = "$AS_HOST" -a "$NUM" = "$i" ]; then
echo "<h2>$SHOWLOG log in ${AUTOPORT_REPOSITORIES[$i]}(${AUTOPORT_ARCH[$i]}):</h2>"
cat << _EOF
<div class=downloadbox align=center>
<div align=left style="background-color: white; height: 550px; width: 850px; overflow: auto; font-size: 8pt; border:1px solid #2b6600; margin: 4px; padding:4; background-color: #e0f2d0;">
_EOF
echo -n "<pre><code>"
if [ "$SHOWLOG" = "current" ]; then
RELOADTIME=600000
cat $LOGFILE | sed "s|<|\&lt;|g"
elif [ "$SHOWLOG" = "last" ]; then
cat $LASTLOGFILE | sed "s|<|\&lt;|g"
else
# Security check
[ "${SHOWLOG/\/\.}" != "${SHOWLOG}" ] && continue
if [ "${AUTOPORT_CHROOT[$i]}" ]; then
BUILDLOGDIR="/var/autoport/${AUTOPORT_CHROOT[$i]}/home/${AUTOPORT_CHROOT_USER[$i]}/.autodist/log/"
elif [ "${AUTOPORT_NATIVE[$i]}" -o "${AUTOPORT_UPDATE[$i]}" ]; then
BUILDLOGDIR="/var/autodist/log/"
fi
if [ "${SHOWLOG/\/ok\/}" != "${SHOWLOG}" -o "${SHOWLOG/\failed\/}" != "${SHOWLOG}" ]; then
SHOWLOGPKG=${SHOWLOG/*\/}
[ "${SHOWLOGPKG}" ] || continue
echo "<table><tr><td colspan=3>Last ${SHOWLOGPKG} logs:</td>"
for t in prepare update build install send; do
echo -n "<tr><td>$t:</td><td>"
[ -e ${BUILDLOGDIR}${t}/ok/${SHOWLOGPKG} ] && echo -n "<td><a href=\"?NUM=$i$HOST_ADD&SHOWLOG=${t}/ok/${SHOWLOGPKG}\">OK</a></td>" || echo "<td></td>"
[ -e ${BUILDLOGDIR}${t}/failed/${SHOWLOGPKG} ] && echo -n "<td><a href=\"?NUM=$i$HOST_ADD&SHOWLOG=${t}/failed/${SHOWLOGPKG}\">FAILED</a></td>" || echo "<td></td>"
echo "</tr>"
done
echo "</table>"
fi
[ -e ${BUILDLOGDIR}${SHOWLOG} ] && cat ${BUILDLOGDIR}${SHOWLOG} | sed "s|<|\&lt;|g"
fi
echo "</code></pre></div></div>"
fi
elif [ ! "$SHOWLOG" ]; then
RELOADTIME=60000
[[ "$NUM" && "$NUM" != "$i" ]] && continue
[[ "$HOST" = "$AS_HOST" ]] || continue
if [ "${AUTOPORT_UPDATE[$i]}" ]; then
echo "<b>* <a href=\"?NUM=$i&HOST=$AS_HOST\">${AUTOPORT_REPOSITORIES[$i]}</a>(${AUTOPORT_ARCH[$i]}) (type:autodist update)"
echo " [ <a href=\"?SHOWLOG=current&NUM=$i$HOST_ADD\">log</a> <a href=\"?SHOWLOG=last&NUM=$i$HOST_ADD\">last</a> ]</b>"
fi
if [ "${AUTOPORT_CHROOT[$i]}" ]; then
echo "<b>* <a href=\"?NUM=$i&HOST=$AS_HOST\">${AUTOPORT_REPOSITORIES[$i]}</a>(${AUTOPORT_ARCH[$i]}) (type:chroot autoport</a> chroot:${AUTOPORT_CHROOT[$i]})"
echo " [ <a href=\"?SHOWLOG=current&NUM=$i$HOST_ADD\">log</a> <a href=\"?SHOWLOG=last&NUM=$i$HOST_ADD\">last</a> ]</b>"
fi
if [ "${AUTOPORT_NATIVE[$i]}" ]; then
echo "<b>* <a href=\"?NUM=$i&HOST=$AS_HOST\">${AUTOPORT_REPOSITORIES[$i]}</a>(${AUTOPORT_ARCH[$i]}) (type:native autoport)"
echo " [ <a href=\"?SHOWLOG=current&NUM=$i$HOST_ADD\">log</a> <a href=\"?SHOWLOG=last&NUM=$i$HOST_ADD\">last</a> ]</b>"
fi
if [ "${AUTOPORT_CROSS[$i]}" ]; then
echo "<b>* <a href=\"?NUM=$i&HOST=$AS_HOST\">cross-platform</a> autoport (arch:${AUTOPORT_CROSS[$i]})</b>"
echo " [ <a href=\"?SHOWLOG=current&NUM=$i$HOST_ADD\">last</a> ]</b>"
fi
cat << _EOF
<div class=downloadbox align=center>
<div align=left style="background-color: white; height: 250px; width: 850px; overflow: auto; font-size: 8pt; border:1px solid #2b6600; margin: 4px; padding:4; background-color: #e0f2d0;">
_EOF
echo "<pre>"
if [ "${AUTOPORT_UPDATE[$i]}" ]; then
unset STARTTIME ENDTIME JOBON BUILDNOW BUILDLOG SCHEDULED
BUILDNOWIDX=0
while read line; do
set -- $line
if [[ "${line:0:1}" = $'\r' ]]; then
PREF=`echo $1 | tr -d "\r"`
else
PREF=$1
fi
case $PREF in
"%%" ) if [ "$2 $3" == "Autodist started" ]; then
STARTTIME=`echo $line | sed "s|.* @ ||"`
AUTODIST_PID=`echo $line | sed "s|.*PID \([0-9]*\) .*|\1|"`
elif [ "$2 $3 $4" == "All jobs done" ]; then
ENDTIME=`echo $line | sed "s|.* @ ||"`
elif [ "$2 $3" == "autoupdate jobs:" ]; then
SCHEDULED=`echo $line | sed "s|.*autoupdate jobs: ||"`
elif [ "$2" == "Doing" ]; then
BUILDNOW[$BUILDNOWIDX]="$3"
BUILDLOG[$BUILDNOWIDX]=
BUILDNOWSTATUS[$BUILDNOWIDX]=
BUILDNOWIDX=`expr $BUILDNOWIDX + 1`
# elif [ "$2" == "Starting" ]; then
# case $3 in
# "PREPARE"|"UPDATE"|"BUILD"|"SEND") BUILDNOW[$BUILDNOWIDX-1]="${BUILDNOW[$BUILDNOWIDX-1]} $3" ;;
# esac
#elif [ "$2" != "Starting" -a "$2" != "Updating" ]; then
# [ "$JOBON" ] || echo $line
fi ;;
"?=") if [ "$2" == "See" ]; then
BUILDLOG[$BUILDNOWIDX-1]="${BUILDLOG[$BUILDNOWIDX-1]} `echo $line | sed 's|.*autodist/log/||'`"
fi ;;
"=>") JOBON=1 ;;
"%!"|"!!") BUILDNOWSTATUS[$BUILDNOWIDX-1]="$line"; JOBON= ;;
"==") if [ "${2:0:2}" == "==" ]; then
JOBON=
# else
# if [ "$2" != "${BUILDNOW[$BUILDNOWIDX-1]}" ]; then
# BUILDNOW[$BUILDNOWIDX]="$2"
# BUILDNOWIDX=`expr $BUILDNOWIDX + 1`
# fi
fi ;;
"Hunk"|"") ;;
*) [ "$JOBON" ] || {
line=`echo $line | sed "s|.*\r||"`
echo $line
} ;;
esac
done < $LOGFILE
else
unset STARTTIME ENDTIME BUILDNOW BUILDLOG AUTODISTON LASTLINE SCHEDULED
while read line; do
line=`echo $line | sed "s|.*\r||"`
set -- $line
case $1 in
"%%" ) AUTODISTON=1 ;;
"^"|"="*|""|"!"|"|"|"#"|"->") ;; #comment
"+") [ "$STARTTIME" ] || STARTTIME=`echo $line | sed "s|.*start at ||"` ;;
"-") ENDTIME=`echo $line | sed "s|.*end at ||"` ;;
[A-Za-z0-9]*) ;;
">") [ "$SCHEDULED" ] || SCHEDULED=`echo $line | sed "s|.*Scheduled jobs: ||"` ;;
*) [ ! "$AUTODISTON" ] && echo $line ;;
esac
done < $LOGFILE
BUILDNOWIDX=1
while read line; do
line=`echo $line | sed "s|.*\r||"`
set -- $line
case $1 in
"%%" ) if [ "$2" == "Doing" ]; then
if [ "$3" != "${BUILDNOW[$BUILDNOWIDX-1]}" ]; then
BUILDNOW[$BUILDNOWIDX]="$3"
BUILDLOG[$BUILDNOWIDX]=
BUILDNOWSTATUS[$BUILDNOWIDX]=
BUILDNOWIDX=`expr $BUILDNOWIDX + 1`
fi
JOBON=1
fi ;;
"?=") if [ "$2" == "See" ]; then
BUILDLOG[$BUILDNOWIDX-1]="${BUILDLOG[$BUILDNOWIDX-1]} `echo $line | sed 's|.*autodist/log/||'`"
# BUILDLOG[$BUILDNOWIDX-1]=`echo $line | sed "s|.*autodist/log/||"`
fi ;;
"=>") JOBON=1 ;;
"%!"|"!!") BUILDNOWSTATUS[$BUILDNOWIDX-1]="$line"; JOBON= ;;
"==") [ "${2:0:2}" == "==" ] && JOBON= ;;
"Hunk"|"") ;;
*) [ "$JOBON" ] || echo $line ;;
esac
done < $AUTODISTLOGFILE
LASTLINE=$line
fi
echo "<b>Start:</b> $STARTTIME"
echo "<b>Scheduled jobs:</b> $SCHEDULED"
for b in `seq 0 ${#BUILDNOW[*]}`; do
# set -- ${BUILDNOW[$b]}
JOBNAME=${BUILDNOW[$b]}
[ "$JOBNAME" ] || continue
echo -n "<a href=\"/distribution/webbuild.php?ENVIRONMENT=$i&PACKAGE=$JOBNAME\" target=\"webbuild\"><b>$JOBNAME</b></a>: "
rowcnt=0
if [ "${BUILDLOG[$b]}" ]; then
for l in ${BUILDLOG[$b]}; do
SUBJOBNAME=`echo $l | sed "s|.*/${JOBNAME}_\(.*\)\..*|\1|"`
[ "${SUBJOBNAME:0:1}" = "_" ] && SUBJOBNAME=$l
[ "$SUBJOBNAME" = "$l" ] && SUBJOBNAME=`echo $l | sed "s|.*/\(.*\)\..*|\1|"`
SUBJOBNAME=${SUBJOBNAME/__*}
OPERATIONNAME=`echo $l | sed "s|\(.*\)/.*/.*|\1|"`
OPERATIONSTATUS=`echo $l | sed "s|.*/\(.*\)/.*|\1|"`
if [ $rowcnt -ge 4 ]; then
echo
rowcnt=0
fi
if [ "$OPERATIONNAME" = "send" -a "$OPERATIONSTATUS" = "ok" ]; then
echo -n "<a href=\"?SHOWLOG=$l&NUM=$i$HOST_ADD\">$OPERATIONNAME:$OPERATIONSTATUS</a>"
elif [ "$OPERATIONNAME" = "install" -a "$OPERATIONSTATUS" = "ok" ]; then
echo -n "<a href=\"?SHOWLOG=$l&NUM=$i$HOST_ADD\">$OPERATIONNAME:$OPERATIONSTATUS</a>"
elif [ "$OPERATIONSTATUS" = "ok" ]; then
echo -n "<a href=\"?SHOWLOG=$l&NUM=$i$HOST_ADD\"><font color=black>$OPERATIONNAME:$OPERATIONSTATUS</font></a>"
else
echo -n "<a href=\"?SHOWLOG=$l&NUM=$i$HOST_ADD\"><font color=red>$OPERATIONNAME:$OPERATIONSTATUS</font></a>"
fi
[ "$SUBJOBNAME" != "$JOBNAME" ] && echo -en "(<a href=\"/distribution/webbuild.php?ENVIRONMENT=$i&PACKAGE=$SUBJOBNAME\" target=\"webbuild\">$SUBJOBNAME</a>)"
echo -n " "
rowcnt=`expr $rowcnt + 1`
done
fi
[ "${BUILDNOWSTATUS[$b]}" ] && echo "<i>(${BUILDNOWSTATUS[$b]})</i>" || echo
done
if [ "$LASTLINE" ]; then
set -- $LASTLINE
[ "$1" = "^" ] && echo "<font color=red><b>${2}</b> ${3} ${4} ${5} ${6} ${7}</font>"
fi
[ "$ENDTIME" ] && echo "<b>End:</b> $ENDTIME" || echo "<b>End: <a href=\"?SHOWLOG=current.$AUTODIST_PID&NUM=$i$HOST_ADD\">in progress</a></b>"
echo
# grep -v "^=" $LOGFILE | tail -n "$LINES" | sed "s|.*\r||"
# echo
echo "</pre></div></div>"
fi
done
# only root host (0) calls other hosts
if [ "$AS_HOST" = "0" -a ! "$SHOWLOG" ]; then
for h in `seq 1 ${#AUTOPORT_CGI_HOST[*]}`; do
if [ "$NUM" -a "$HOST" = "$h" ]; then
curl "${AUTOPORT_CGI_HOST[$h]}?AS_HOST=$h&LINES=$LINES&NUM=$NUM&HOST=$HOST" 2>/dev/null
elif [ ! "$NUM" ]; then
curl "${AUTOPORT_CGI_HOST[$h]}?AS_HOST=$h&LINES=$LINES" 2>/dev/null
fi
done
fi
fi
if [ "$AS_HOST" = "0" ]; then
echo "<p><i>Generated by autodist CGI interface. Last update: `date`</i>"
if [ "$FORMAT" = "xml" ]; then
echo -n "]]></output>"
[ "$RELOADTIME" ] && echo -n "<reloadtime>$RELOADTIME</reloadtime>"
echo "</autodist>"
fi
fi