server/gen_site.sh

156 lines
2.8 KiB
Bash
Executable File

#!/bin/sh
BASEURL="localhost"
SALT_TEAM="connected"
OUT_TEAM="./teams"
OUT_HTDOCS="./htdocs"
MAX_PARAL=10
DEBUG=0
cd `dirname "$0"`
if [ "$UID" = "0" ]
then
SCRIPT=`pwd`/`basename "$0"`
su -c "sh -c '$SCRIPT $@'" synchro
exit $?
fi
if [ -f "/tmp/generate_site" ]
then
echo "This script is already running" 1>&2
echo "Remove the file /tmp/generate_site if you are sure this is not true" 1>&2
exit 1
fi
touch /tmp/generate_site
WGET_OPT="--no-check-certificate -c"
if [ $DEBUG -ne 1 ]
then
WGET_OPT="-q"
fi
./clear_cache.sh top
mkdir -p out
ORIG_DIR=`pwd`
MYTMPDIR=`mktemp -d`
cd "$MYTMPDIR"
# First, remove existing version if any
rm -rf "$BASEURL" "$OUT_TEAM"
wget $WGET_OPT -m -b "http://$BASEURL:8080/" -o /dev/null
mkdir -p "$BASEURL"
ln -sf "$ORIG_DIR/files/" "$BASEURL/files"
# Get list of teams
TEAMS=
if [ $# -gt 0 ]
then
while [ $# -gt 0 ]
do
TEAMS="$TEAMS /$SALT_TEAM/$1/"
shift
done
FULLSYNC=0
else
for l in $(curl -k "http://$BASEURL:8080/$SALT_TEAM/" 2> /dev/null | grep -oE "/[^/]+/[0-9]+/")
do
TEAMS="$TEAMS $l"
done
FULLSYNC=1
fi
echo "Team list to generate: $TEAMS"
NB=0
PIDLIST=
# Fetch them in parallel
for l in $TEAMS
do
(
if ! wget $WGET_OPT -m "http://$BASEURL:8080/$l"
then
exit 1
fi
for m in $(grep -R "<form " "$BASEURL:8080/$l" | grep -oE "[^/]+/([^/]+)/([0-9]+)-[^/]+/([a-zA-Z0-9_]+)/submission")
do
OUT=`echo "$m" | sed -E 's#([^/]+)/([^/]+)/([0-9]+)-[^/]+/([a-zA-Z0-9_]+)/submission#\1/\2/submission-\3-\4#'`
wget $WGET_OPT "http://$BASEURL:8080/$m" -O "$BASEURL/$OUT.html"
wget $WGET_OPT "http://$BASEURL:8080/$m/gerr" -O "$BASEURL/$OUT-bad.html"
wget $WGET_OPT "http://$BASEURL:8080/$m/serr" -O "$BASEURL/$OUT-already.html"
done
# Remove /connected/XY
for f in `find "$BASEURL:8080/$l" -type f`
do
sed -Ei "s#([0-9]+)-[^/]*/([a-zA-Z0-9_]+)/submission#submission-\1-\2.html#;s#[^/\"]+/([0-9]+)/##" "$f"
done
) &
PIDLIST="$PIDLIST $!"
NB=$(($NB + 1))
if [ $NB -ge $MAX_PARAL ]
then
echo "Generating teams ...$PIDLIST"
wait $PIDLIST
PIDLIST=
NB=0
fi
done
echo "Generating teams ...$PIDLIST"
ERR=0
for i in $PIDLIST
do
if ! wait $i
then
ERR=$(($ERR + 1))
fi
done
# Move connected/ at root
mv "$BASEURL/$SALT_TEAM/" "$OUT_TEAM"
mv "$BASEURL/" "$OUT_HTDOCS"
# Remove all robots.txt
find . -name robots.txt -exec rm {} \;
# Remove useless symlink
rm "$BASEURL/files"
# Ready to launch another gen_site
rm /tmp/generate_site
if [ $ERR -gt 0 ]
then
cd "$ORIG_DIR"
rm -rf "$MYTMPDIR"
echo "Some errors occurs" 1>&2
exit $ERR
else
MOREOPT=
if [ "$FULL" = "1" ]
then
MOREOPT="--delete"
fi
# Ok, now, sync files with prod
rsync -av $MOREOPT * "$ORIG_DIR/out"
cd "$ORIG_DIR"
rm -rf "$MYTMPDIR"
fi