Server synchronisation

This commit is contained in:
nemunaire 2013-12-13 18:45:25 +01:00
parent 4b101ef4b2
commit bbec08ac4f
6 changed files with 218 additions and 18 deletions

View File

@ -4,14 +4,20 @@ use v5.10.1;
use strict;
use warnings;
use DBI;
use File::Basename;
use Data::Dumper;
#Return number of good solutions
my $exit = 0;
my $root = dirname(__FILE__);
chdir($root);
# First, read PHP configuration to extract some settings
my $profile;
my $submission_dir;
open my $conf, "<", "onyx/config/root.xml";
open my $conf, "<", "$root/onyx/config/root.xml";
for my $p (<$conf>)
{
if ($p =~ /<(?:option|var) name="(.*)">(.*)<\/(?:option|var)>/)
@ -27,7 +33,7 @@ die("submission_dir is not a directory") if ! $submission_dir || ! -d $submissio
# Read db settings
my %db_settings;
open my $dbprof, "<", "onyx/db/$profile.profile.php";
open my $dbprof, "<", "$root/onyx/db/$profile.profile.php";
while (<$dbprof>)
{
if (/\$___profile\[['"](.+)['"]\] = ['"](.+)['"]/)
@ -42,8 +48,10 @@ my $dbh;
opendir(my $dh, $submission_dir) || die "Can't opendir submission_dir: $!";
for my $f (readdir $dh)
{
if ($f =~ /^([0-9]+)-([0-9]+)-([0-9]+)$/)
if ($f =~ /^([0-9]+)-([0-9]+)-([a-zA-Z0-9_]+)$/)
{
my $good = -1;
my $team = $1;
my $theme = $2;
my $exercice = $3;
@ -57,18 +65,51 @@ for my $f (readdir $dh)
{'RaiseError' => 1, 'PrintError' => 1})
or die $DBI::errstr if !$dbh;
my $sth = query($dbh, "SELECT format, value FROM exercice_keys WHERE id_exercice = ".int($exercice));
my $sth = query($dbh, "SELECT format, value FROM exercice_keys WHERE id_exercice = '$exercice';");
# Check solutions
while (my $row = get_row($sth))
{
say Dumper($row);
$good = 1 if ($good == -1);
my $type = @$row[0];
my $sol = @$row[1];
if ($type eq "raw" && $sol ne $solution) {
$good = 0;
last;
}
elsif ($type ne "raw") {
$good = 0;
warn "$type not implemented";
last;
}
}
# Register solve
if ($good == -1) {
say "Exercice $exercice doesn't exist ; given by team $team in theme $theme.";
}
elsif ($good == 1)
{
say "Team $team solve exercice $exercice in $theme at ".localtime();
query($dbh, "INSERT INTO solved (id_team, id_exercice, time) VALUES ($team, '$exercice', CURRENT_TIMESTAMP);");
$exit++;
}
else {
say "Team $team didn't give the correct answer for exercice $exercice.";
}
# Remove the file
unlink("$submission_dir/$f");
}
}
closedir $dh;
$dbh->disconnect() if $dbh;
exit( $exit > 126 ? 126 : $exit );
sub query
{
my $sth = $_[0]->prepare($_[1]);
@ -78,3 +119,8 @@ sub query
return $sth;
}
sub get_row
{
return $_[0]->fetchrow_arrayref();
}

View File

@ -1,10 +1,91 @@
#!/bin/sh
BASEURL="localhost"
SALT_TEAM="connected"
OUT_TEAM="./teams"
MAX_PARAL=9
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
mkdir -p out
cd out
wget -c -m https://$BASEURL/ https://$BASEURL/connected/
# First, remove existing version if any
rm -rf "$BASEURL" "$OUT_TEAM"
wget $WGET_OPT -m -b "http://$BASEURL/" -o /dev/null
mkdir -p "$BASEURL"
ln -sf "`pwd`/../files/" "$BASEURL/files"
NB=0
PIDLIST=
# Get list of teams and fetch them in parallel
for l in $(curl -k "http://$BASEURL/$SALT_TEAM/" 2> /dev/null | grep -oE "/[^/]+/[0-9]+/")
do
(
wget $WGET_OPT -m "http://$BASEURL/$l"
for m in $(grep -R "<form " "$BASEURL/$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/$m" -O "$BASEURL/$OUT.html"
done
# Remove /connected/XY
sed -Ei "s#/[^/]+/([0-9]+)/#/#" "$BASEURL/$l/"*
sed -Ei "s#/([0-9]+)-[^/]*/([a-zA-Z0-9_]+)/submission#/submission-\1-\2.html#" "$BASEURL/$l/"*
) &
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"
wait $PIDLIST
rm /tmp/generate_site
# Move connected/ at root
mv "$BASEURL/$SALT_TEAM/" "$OUT_TEAM"
# Remove all robots.txt
find . -name robots.txt -exec rm {} \;
# Remove useless symlink
rm "$BASEURL/files"

View File

@ -17,6 +17,11 @@
rewrite ^/(.*)$ /index.php?p=$1 last;
}
location /files
{
root /var/www/fic2014-server/;
}
location ~* \favicon.ico$ {
access_log off;
expires 1d;
@ -37,7 +42,7 @@
{
if (!-e $document_root$document_uri) { return 404; }
include /etc/nginx/fastcgi.conf;
fastcgi_pass unix:/var/run/fastcgi/php-fpm.sock;
fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_index index.php;
break;
}

View File

@ -13,23 +13,44 @@ server {
ssl_prefer_server_ciphers on;
ssl_ciphers ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:!ADH:!AECDH:!MD5:!DSS;
ssl_client_certificate /var/www/fic2014-server/misc/pki/cacert.crt;
ssl_verify_client on;
add_header Strict-Transport-Security "max-age=2592000; includeSubdomains";
ssl_verify_client optional;
ssl_crl /var/www/fic2014-server/misc/pki/crl.pem;
add_header Strict-Transport-Security "max-age=2592000; includeSubdomains";
location /
{
default_type text/html;
if ($ssl_client_s_dn !~ "/C=FR/ST=France/O=Epita/OU=SRS/")
{
return 401;
}
if ($ssl_client_s_dn ~ "/C=FR/ST=France/O=Epita/OU=SRS/CN=Team1/") {
root /var/www/fic2014-server/htdocs/connected/166$1;
set $team 0;
rewrite ^/submission-([0-9]+)-([0-9]+).html$ /submission.php?team=166&theme=$1&exercice=$2 last;
if ($ssl_client_s_dn ~ "/C=FR/ST=France/O=Epita/OU=SRS/CN=bombal_s/") { set $team 161; }
if ($ssl_client_s_dn ~ "/C=FR/ST=France/O=Epita/OU=SRS/CN=Groupe_1/") { set $team 166; }
if ($ssl_client_s_dn ~ "/C=FR/ST=France/O=Epita/OU=SRS/CN=Groupe_2/") { set $team 167; }
if ($ssl_client_s_dn ~ "/C=FR/ST=France/O=Epita/OU=SRS/CN=Groupe_3/") { set $team 168; }
if ($ssl_client_s_dn ~ "/C=FR/ST=France/O=Epita/OU=SRS/CN=Groupe_4/") { set $team 169; }
if ($ssl_client_s_dn ~ "/C=FR/ST=France/O=Epita/OU=SRS/CN=Groupe_5/") { set $team 170; }
if ($ssl_client_s_dn ~ "/C=FR/ST=France/O=Epita/OU=SRS/CN=Groupe_6/") { set $team 171; }
if ($ssl_client_s_dn ~ "/C=FR/ST=France/O=Epita/OU=SRS/CN=Groupe_7/") { set $team 172; }
if ($ssl_client_s_dn ~ "/C=FR/ST=France/O=Epita/OU=SRS/CN=Groupe_8/") { set $team 173; }
if ($team) {
root /var/www/fic2014-server/teams/$team$1;
rewrite ^/submission-([0-9]+)-([a-zA-Z0-9_]+).html$ /submission.php?team=$team&theme=$1&exercice=$2 last;
}
if ($team = 0) {
root /var/www/fic2014-server/htdocs/;
}
}
location /connected
{
return 403;
}
location /files
{
root /var/www/fic2014-server/;
}
location ~* \favicon.ico$ {
@ -38,7 +59,7 @@ server {
add_header Cache-Control public;
}
location ~ ^/(img|js|css)/ {
location ~ ^/(img|js|css|fonts)/ {
access_log off;
expires 7d;
add_header Cache-Control public;

24
submission.php Normal file
View File

@ -0,0 +1,24 @@
<?php
function show($file)
{
if (file_exists($file))
print file_get_contents($file);
else
header("Location: ".$_SERVER["HTTP_REFERER"]);
}
$file = __DIR__."/submission/".intval($_GET["team"])."-".intval($_GET["theme"])."-".urlencode($_GET["exercice"]);
if (file_exists($file))
show(__DIR__."/teams/".intval($_GET["team"])."/alreadysubmited-".intval($_GET["theme"])."-".urlencode($_GET["exercice"]).".html");
else if (!empty($_POST["solution"]) && !empty($_GET["team"]) && !empty($_GET["theme"]) && !empty($_GET["exercice"]))
{
file_put_contents($file, $_POST['solution'], LOCK_EX);
show(__DIR__."/teams/".intval($_GET["team"])."/submission-".intval($_GET["theme"])."-".urlencode($_GET["exercice"]).".html");
}
else
show(__DIR__."/teams/".intval($_GET["team"])."/badsubmission-".intval($_GET["theme"])."-".urlencode($_GET["exercice"]).".html");

23
synchro.sh Executable file
View File

@ -0,0 +1,23 @@
#!/bin/sh
cd `dirname "$0"`
if [ "$UID" = "0" ]
then
SCRIPT=`pwd`/`basename "$0"`
su -c "sh $SCRIPT" synchro
exit $?
fi
rsync -e ssh -av --delete out/localhost/* phobos:~/htdocs/
rsync -e ssh -av --delete out/teams phobos:~/
rsync -e ssh -av --delete files phobos:~/
rsync -e ssh -av --delete misc phobos:~/
scp nginx.conf submission.php phobos:~/
rsync -e ssh -av --delete out/localhost/* phobos:~/htdocs/
rsync -e ssh -av phobos:~/submission/ submission/
ssh phobos "rm ~/submission/*"
exit $?