server/gen_site.sh

156 lines
2.8 KiB
Bash
Raw Permalink Normal View History

#!/bin/sh
BASEURL="localhost"
2013-12-13 17:45:25 +00:00
SALT_TEAM="connected"
OUT_TEAM="./teams"
2014-01-14 15:14:31 +00:00
OUT_HTDOCS="./htdocs"
2013-12-13 17:45:25 +00:00
2013-12-14 05:11:14 +00:00
MAX_PARAL=10
2013-12-13 17:45:25 +00:00
DEBUG=0
cd `dirname "$0"`
2013-12-13 17:45:25 +00:00
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
2013-12-14 05:11:14 +00:00
./clear_cache.sh top
mkdir -p out
2013-12-14 05:11:14 +00:00
ORIG_DIR=`pwd`
MYTMPDIR=`mktemp -d`
cd "$MYTMPDIR"
2013-12-13 17:45:25 +00:00
# First, remove existing version if any
rm -rf "$BASEURL" "$OUT_TEAM"
2021-10-06 09:33:24 +00:00
wget $WGET_OPT -m -b "http://$BASEURL:8080/" -o /dev/null
2013-12-13 17:45:25 +00:00
mkdir -p "$BASEURL"
2013-12-14 05:11:14 +00:00
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
2021-10-06 09:33:24 +00:00
for l in $(curl -k "http://$BASEURL:8080/$SALT_TEAM/" 2> /dev/null | grep -oE "/[^/]+/[0-9]+/")
2013-12-14 05:11:14 +00:00
do
TEAMS="$TEAMS $l"
done
FULLSYNC=1
fi
echo "Team list to generate: $TEAMS"
2013-12-13 17:45:25 +00:00
NB=0
PIDLIST=
2013-12-14 05:11:14 +00:00
# Fetch them in parallel
for l in $TEAMS
2013-12-13 17:45:25 +00:00
do
(
2021-10-06 09:33:24 +00:00
if ! wget $WGET_OPT -m "http://$BASEURL:8080/$l"
2013-12-14 05:11:14 +00:00
then
exit 1
fi
2013-12-13 17:45:25 +00:00
2021-10-06 09:33:24 +00:00
for m in $(grep -R "<form " "$BASEURL:8080/$l" | grep -oE "[^/]+/([^/]+)/([0-9]+)-[^/]+/([a-zA-Z0-9_]+)/submission")
2013-12-13 17:45:25 +00:00
do
2021-10-06 09:33:24 +00:00
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"
2013-12-13 17:45:25 +00:00
done
# Remove /connected/XY
2021-10-06 09:33:24 +00:00
for f in `find "$BASEURL:8080/$l" -type f`
2014-01-14 15:14:31 +00:00
do
2021-10-06 09:33:24 +00:00
sed -Ei "s#([0-9]+)-[^/]*/([a-zA-Z0-9_]+)/submission#submission-\1-\2.html#;s#[^/\"]+/([0-9]+)/##" "$f"
2014-01-14 15:14:31 +00:00
done
2013-12-13 17:45:25 +00:00
) &
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"
2013-12-14 05:11:14 +00:00
ERR=0
for i in $PIDLIST
do
if ! wait $i
then
ERR=$(($ERR + 1))
fi
done
2013-12-13 17:45:25 +00:00
# Move connected/ at root
mv "$BASEURL/$SALT_TEAM/" "$OUT_TEAM"
2014-01-14 15:14:31 +00:00
mv "$BASEURL/" "$OUT_HTDOCS"
2013-12-13 17:45:25 +00:00
# Remove all robots.txt
find . -name robots.txt -exec rm {} \;
# Remove useless symlink
rm "$BASEURL/files"
2013-12-14 05:11:14 +00:00
# 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=
2021-10-06 09:33:24 +00:00
if [ "$FULL" = "1" ]
2013-12-14 05:11:14 +00:00
then
MOREOPT="--delete"
fi
# Ok, now, sync files with prod
rsync -av $MOREOPT * "$ORIG_DIR/out"
cd "$ORIG_DIR"
rm -rf "$MYTMPDIR"
fi