Working synchronization

This commit is contained in:
nemunaire 2014-01-19 17:49:07 +01:00
parent db77d8b697
commit 3c04523ca8
6 changed files with 135 additions and 44 deletions

View File

@ -132,7 +132,8 @@ for my $f (readdir $dh)
$sth = query($dbh, "SELECT S.id FROM solved S WHERE S.id_exercice = ".$dbh->quote($exercice)); $sth = query($dbh, "SELECT S.id FROM solved S WHERE S.id_exercice = ".$dbh->quote($exercice));
if (! $sth->rows) if (! $sth->rows)
{ {
say $team; say "TEAM$team,$theme:SYNCSYN:TEAM$team:SYNC:HOME:SYNC:all:DS";
say STDERR localtime().": Team $team solve exercice $exercice in $theme"; say STDERR localtime().": Team $team solve exercice $exercice in $theme";
query($dbh, "INSERT INTO solved (id_team, id_exercice, time) VALUES ($team, ".$dbh->quote($exercice).", CURRENT_TIMESTAMP);"); query($dbh, "INSERT INTO solved (id_team, id_exercice, time) VALUES ($team, ".$dbh->quote($exercice).", CURRENT_TIMESTAMP);");
$exit++; $exit++;
@ -150,6 +151,8 @@ for my $f (readdir $dh)
} }
} }
else { else {
say "TEAM$team,$theme:SYNCSYN";
say STDERR localtime().": Team $team didn't give the correct answer for exercice $exercice."; say STDERR localtime().": Team $team didn't give the correct answer for exercice $exercice.";
} }

120
gen_site.pl Normal file → Executable file
View File

@ -6,10 +6,19 @@ use warnings;
use threads; use threads;
use threads::shared; use threads::shared;
use Cwd 'abs_path';
use File::Basename;
use File::Copy;
use File::Find;
use File::Path qw/make_path remove_tree/;
use File::Temp "tempdir";
use Getopt::Long; use Getopt::Long;
use Thread::Queue; use Thread::Queue;
our $outdir = "outest"; our $outdir = "outest";
our $outteams = "/teams/";
our $outhome = "/htdocs/";
our $tmpdir = tempdir();
our $baseurl = "http://localhost"; our $baseurl = "http://localhost";
our $baseadmin = "/admin/"; our $baseadmin = "/admin/";
@ -21,6 +30,7 @@ our $threads = 6;
sub genHome(;$) sub genHome(;$)
{ {
my $m = shift // Mirror->new(); my $m = shift // Mirror->new();
$m->add_url($basehome . "index.html");
$m->add_url($basehome); $m->add_url($basehome);
return $m; return $m;
@ -104,9 +114,19 @@ sub manage
elsif (/^(reset)*r(e(s(e(t)?)?)?)?/) elsif (/^(reset)*r(e(s(e(t)?)?)?)?/)
{ {
say "Performing RESET ..."; say "Performing RESET ...";
remove_tree($main::tmpdir);
mkdir($main::tmpdir);
$m->reset(); $m->reset();
} }
elsif (/^J$/) elsif (/^(D)?(SYNC)*S(Y(N(C)?)?)?/)
{
sync($1);
}
elsif (/^LS$/)
{
system("ls '$main::tmpdir'");
}
elsif (/^J(O(I(N)?)?)?$/)
{ {
say "JOIN receive, stopping all threads..."; say "JOIN receive, stopping all threads...";
$m->stop(); $m->stop();
@ -144,6 +164,77 @@ sub manage
} }
} }
sub sync
{
if (shift)
{
say "Full synchronization to $main::outdir";
my $tmpcopy = tempdir();
find(
sub
{
if (-f)
{
my $todir = $File::Find::dir."/";
if ($todir =~ /^\Q$main::tmpdir\E\/?(\Q$main::baseadmin\E|\Q$main::baseteams\E|\Q$main::basehome\E)(.*)$/)
{
$todir = $tmpcopy;
return if ($1 eq $main::baseadmin);
$todir .= $main::outteams if ($1 eq $main::baseteams);
$todir .= $main::outhome if ($1 eq $main::basehome);
$todir .= $2;
}
make_path($todir, { mode => 0711 }) if (! -d $todir );
copy($File::Find::name, $todir) or warn(q{copy failed:} . $!);
}
},
$tmpdir
);
abs_path($main::outdir);
abs_path($tmpcopy);
remove_tree($main::outdir);
system("mv '$tmpcopy' '$main::outdir'");
}
else
{
say "Incremental synchronization to $main::outdir";
find(
sub
{
if (-f)
{
my $todir = $File::Find::dir."/";
if ($todir =~ /^\Q$main::tmpdir\E\/?(\Q$main::baseadmin\E|\Q$main::baseteams\E|\Q$main::basehome\E)(.*)$/)
{
$todir = $main::outdir;
return if ($1 eq $main::baseadmin);
$todir .= $main::outteams if ($1 eq $main::baseteams);
$todir .= $main::outhome if ($1 eq $main::basehome);
$todir .= $2;
}
make_path($todir, { mode => 0711 }) if (! -d $todir );
say "$File::Find::name -> $todir";
copy($File::Find::name, $todir) or warn(q{copy failed:} . $!);
}
},
$tmpdir
);
}
}
sub parse($$) sub parse($$)
{ {
my $m = shift; my $m = shift;
@ -189,6 +280,8 @@ GetOptions ("threads|thread|t=i" => \$threads,
"deamon|d" => \$deamon, "deamon|d" => \$deamon,
"help|h|?" => \$help); "help|h|?" => \$help);
$outdir = abs_path($outdir);
if ($deamon) if ($deamon)
{ {
my $m :shared = Mirror->new(); my $m :shared = Mirror->new();
@ -222,8 +315,12 @@ else
genHome($m); genHome($m);
$m->join(); $m->join();
sync(1);
} }
remove_tree($main::tmpdir);
package Mirror; package Mirror;
@ -461,21 +558,30 @@ sub getLinks($)
{ {
my $self = shift; my $self = shift;
return $self->{content} =~ /(?:src|href)="([^"]+)"/g; return $self->{content} =~ /(?:src|href|action)="([^"]+)"/g;
} }
sub getNearLinks($) sub getNearLinks($)
{ {
my $self = shift; my $self = shift;
return $self->{content} =~ /(?:src|href)="(?:\Q$main::baseurl\E|(?:\/?\.\.)+)?([^:"]+)"/g; my @links = $self->{content} =~ /(?:src|href)="(?:\Q$main::baseurl\E|(?:\/?\.\.)+)?([^:"]+)"/g;
for my $action ($self->{content} =~ /action="(?:\Q$main::baseurl\E|(?:\/?\.\.)+)?([^:"]+)"/g)
{
push @links, $action;
push @links, "$action/gerr";
push @links, "$action/serr";
}
return @links;
} }
sub treatLinks($) sub treatLinks($)
{ {
my $self = shift; my $self = shift;
$self->{content} =~ s!(src|href)="( \Q$baseteams\E[^/]+/ | \Q$baseadmin\E | \Q$basehome\E)([^"]*)"!$1="/$3"!gx; $self->{content} =~ s!(src|href|action)="( \Q$baseteams\E[^/]+/ | \Q$baseadmin\E | \Q$basehome\E)([^"]*)"!$1="/$3"!gx;
} }
sub fetch($) sub fetch($)
@ -499,7 +605,7 @@ sub alreadySaved($;$)
sub getSavePath($;$) sub getSavePath($;$)
{ {
my $self = shift; my $self = shift;
my $basedir = shift // $main::outdir; my $basedir = shift // $main::tmpdir;
# Convert URL to real directory path # Convert URL to real directory path
my $path = $self->{url}; my $path = $self->{url};
@ -520,7 +626,7 @@ sub save($;$)
if ($path =~ /\.[a-z0-9]{2,4}$/) if ($path =~ /\.[a-z0-9]{2,4}$/)
{ {
eval { eval {
make_path( dirname("$path") ) if (! -d dirname("$path") ); make_path( dirname("$path") , { mode => 0711 }) if (! -d dirname("$path") );
}; };
open my $fd, ">", $path or die "$path: $!"; open my $fd, ">", $path or die "$path: $!";
@ -530,7 +636,7 @@ sub save($;$)
else else
{ {
eval { eval {
make_path "$path" if (! -d "$path"); make_path "$path", { mode => 0711 } if (! -d "$path");
}; };
open my $fd, ">", "$path/index.html" or die "$path: $!"; open my $fd, ">", "$path/index.html" or die "$path: $!";

View File

@ -1,6 +1,6 @@
#!/bin/sh #!/bin/sh
rm /tmp/stop rm -f /tmp/stop
cd `dirname "$0"` cd `dirname "$0"`
if [ "$UID" = "0" ] if [ "$UID" = "0" ]
@ -13,42 +13,21 @@ fi
touch ./logs/checks.log touch ./logs/checks.log
tail -f ./logs/checks.log & tail -f ./logs/checks.log &
FULLREGEN=0 TMPF=`mktemp`
tail -f "$TMPF" | ./gen_site.pl -d -o ./out/ &
while ! [ -f /tmp/stop ]; while ! [ -f /tmp/stop ];
do do
if [ "$FULLREGEN" != "0" ] ./synchro.sh delete > /dev/null
then
./synchro.sh
else
./synchro.sh delete
fi
if [ `ls submission | wc -l` -gt 1 ] if [ `ls submission | wc -l` -gt 1 ]
then then
TMPF=`mktemp` ./check.pl 2>> ./logs/checks.log >> "$TMPF"
if ! ./check.pl 2>> ./logs/checks.log > "$TMPF"
then
FULLREGEN=1
fi
if [ `cat "$TMPF" | wc -l` -gt 0 ]
then
while ! cat "$TMPF" | xargs ./gen_site.sh
do
echo "FAIL regeneration, retry..." 1>&2
done
fi
rm "$TMPF"
elif [ "$FULLREGEN" != "0" ]
then
while ! ./gen_site.sh; do
echo "FAIL regeneration, retry..." 1>&2
done
FULLREGEN=0
else else
sleep 1 sleep 1
fi fi
done done
rm -rf "$TMPF"

View File

@ -12,7 +12,7 @@ server {
} }
server { server {
listen [::1]:80 ipv6only=on; listen 80;
include /var/www/fic2014-server/nginx-server-common.conf; include /var/www/fic2014-server/nginx-server-common.conf;
} }

View File

@ -37,7 +37,7 @@ server {
if ($team) { if ($team) {
root /var/www/fic2014-server/teams/$team$1; 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; rewrite ^/([0-9]+-?[a-zA-Z0-9_-]*)/([a-zA-Z0-9_]+)/submission$ /submission.php?team=$team&theme=$1&exercice=$2 last;
} }
if ($team = 0) { if ($team = 0) {
root /var/www/fic2014-server/htdocs/; root /var/www/fic2014-server/htdocs/;
@ -55,12 +55,14 @@ server {
} }
location ~* \favicon.ico$ { location ~* \favicon.ico$ {
root /var/www/fic2014-server/htdocs/;
access_log off; access_log off;
expires 1d; expires 1d;
add_header Cache-Control public; add_header Cache-Control public;
} }
location ~ ^/(img|js|css|fonts)/ { location ~ ^/(assets|img|js|css|fonts)/ {
root /var/www/fic2014-server/htdocs/;
access_log off; access_log off;
expires 7d; expires 7d;
add_header Cache-Control public; add_header Cache-Control public;

View File

@ -5,20 +5,21 @@ function show($file)
if (file_exists($file)) if (file_exists($file))
print file_get_contents($file); print file_get_contents($file);
else else
header("Location: ".$_SERVER["HTTP_REFERER"]); header("HTTP/1.1 403 Forbidden");
} }
$file = __DIR__."/submission/".intval($_GET["team"])."-".intval($_GET["theme"])."-".urlencode($_GET["exercice"]); $file = __DIR__."/submission/".intval($_GET["team"])."-".intval($_GET["theme"])."-".urlencode($_GET["exercice"]);
if (file_exists($file)) if (file_exists($file))
show(__DIR__."/teams/".intval($_GET["team"])."/submission-".intval($_GET["theme"])."-".urlencode($_GET["exercice"])."-already.html"); show(__DIR__."/teams/".intval($_GET["team"])."/".urlencode($_GET["theme"])."/".urlencode($_GET["exercice"])."/submission/serr/index.html");
else if (!empty($_POST["solution"]) && !empty($_GET["team"]) && !empty($_GET["theme"]) && !empty($_GET["exercice"])) else if (!empty($_POST["solution"]) && !empty($_GET["team"]) && !empty($_GET["theme"]) && !empty($_GET["exercice"]))
{ {
file_put_contents($file, $_POST['solution'], LOCK_EX); file_put_contents($file, $_POST['solution'], LOCK_EX);
show(__DIR__."/teams/".intval($_GET["team"])."/submission-".intval($_GET["theme"])."-".urlencode($_GET["exercice"]).".html"); show(__DIR__."/teams/".intval($_GET["team"])."/".urlencode($_GET["theme"])."/".urlencode($_GET["exercice"])."/submission/index.html");
} }
else else
show(__DIR__."/teams/".intval($_GET["team"])."/submission-".intval($_GET["theme"])."-".urlencode($_GET["exercice"])."-bad.html"); show(__DIR__."/teams/".intval($_GET["team"])."/".urlencode($_GET["theme"])."/".urlencode($_GET["exercice"])."/submission/gerr/index.html");