compiler_vm: rename compiler_server_virsh.pl to compiler_server.pl

This commit is contained in:
Pragmatic Software 2019-07-10 18:52:11 -07:00
parent 4f4b5dde24
commit 442245be74
2 changed files with 163 additions and 163 deletions

View File

@ -1,9 +1,5 @@
#!/usr/bin/perl #!/usr/bin/perl
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
use warnings; use warnings;
use strict; use strict;
@ -13,12 +9,11 @@ use IPC::Shareable;
use Time::HiRes qw/gettimeofday/; use Time::HiRes qw/gettimeofday/;
my $SERVER_PORT = 9000; my $SERVER_PORT = 9000;
my $MONITOR_PORT = 3335;
my $SERIAL_PORT = 3333; my $SERIAL_PORT = 3333;
my $HEARTBEAT_PORT = 3336; my $HEARTBEAT_PORT = 3336;
my $DOMAIN_NAME = 'compiler';
my $COMPILE_TIMEOUT = 10; my $COMPILE_TIMEOUT = 10;
my $NOGRAPHIC = 1;
sub server_listen { sub server_listen {
my $port = shift @_; my $port = shift @_;
@ -37,41 +32,18 @@ sub server_listen {
} }
sub vm_stop { sub vm_stop {
my $pid = shift @_; system("virsh shutdown $DOMAIN_NAME");
return if not defined $pid;
print "killing vm $pid\n";
kill 'INT', $pid;
waitpid($pid, 0);
} }
sub vm_start { sub vm_start {
my $pid = fork; system("virsh start $DOMAIN_NAME");
if(not defined $pid) {
die "fork failed: $!";
}
if($pid == 0) {
my $command = "qemu-system-x86_64 -M pc -net none -hda compiler-snap.qcow2 -m 512 -monitor tcp:127.0.0.1:$MONITOR_PORT,server,nowait -serial tcp:127.0.0.1:$SERIAL_PORT,server,nowait -serial tcp:127.0.0.1:$HEARTBEAT_PORT,server -boot c -enable-kvm -loadvm 1" . ($NOGRAPHIC ? " -nographic" : "");
my @command_list = split / /, $command;
exec(@command_list);
} else {
return $pid;
}
} }
sub vm_reset { sub vm_reset {
use IO::Socket; return if $ENV{NORESET};
#system("virsh detach-disk $DOMAIN_NAME vdb");
print "Resetting vm\n"; system("virsh snapshot-revert $DOMAIN_NAME 1");
my $sock = IO::Socket::INET->new(PeerAddr => '127.0.0.1', PeerPort => $MONITOR_PORT, Prot => 'tcp'); #system("virsh attach-disk $DOMAIN_NAME --source /var/lib/libvirt/images/factdata.qcow2 --target vdb");
if(not defined $sock) {
print "[vm_reset] Unable to connect to monitor: $!\n";
return;
}
print $sock "loadvm 1\n";
close $sock;
print "Reset vm\n"; print "Reset vm\n";
} }
@ -80,28 +52,41 @@ sub execute {
print "execute($cmdline)\n"; print "execute($cmdline)\n";
my @list = split / /, $cmdline;
my ($ret, $result); my ($ret, $result);
#$SIG{CHLD} = 'IGNORE';
my $child = fork; my $child = fork;
if($child == 0) { if($child == 0) {
($ret, $result) = eval { ($ret, $result) = eval {
my $result = ''; my $result = '';
my $pid = open(my $fh, '-|', "$cmdline 2>&1"); my $pid = open(my $fh, '-|', @list);
if (not defined $pid) {
print "Couldn't fork: $!\n";
return (-13, "[Fatal error]");
}
local $SIG{ALRM} = sub { print "Time out\n"; kill 9, $pid; print "sent KILL to $pid\n"; die "Timed-out: $result\n"; }; local $SIG{ALRM} = sub { print "Time out\n"; kill 9, $pid; print "sent KILL to $pid\n"; die "Timed-out: $result\n"; };
alarm($COMPILE_TIMEOUT); alarm($COMPILE_TIMEOUT);
print "Reading...\n";
while(my $line = <$fh>) { while(my $line = <$fh>) {
print "read [$line]\n";
$result .= $line; $result .= $line;
} }
close $fh; close $fh;
print "Done reading.\n";
my $ret = $? >> 8; my $ret = $? >> 8;
alarm 0; alarm 0;
#print "[$ret, $result]\n";
print "[$ret, $result]\n";
return ($ret, $result); return ($ret, $result);
}; };
@ -113,6 +98,7 @@ sub execute {
return ($ret, $result); return ($ret, $result);
} else { } else {
waitpid($child, 0); waitpid($child, 0);
print "?: $?\n";
my $result = $? >> 8; my $result = $? >> 8;
print "child exited, parent continuing [result = $result]\n"; print "child exited, parent continuing [result = $result]\n";
return (undef, $result); return (undef, $result);
@ -134,8 +120,8 @@ sub compiler_server {
$running = 1; $running = 1;
$heartbeat = 0; $heartbeat = 0;
my $vm_pid = vm_start; vm_reset;
print "vm started pid: $vm_pid\n"; print "vm started\n";
$heartbeat_pid = fork; $heartbeat_pid = fork;
die "Fork failed: $!" if not defined $heartbeat_pid; die "Fork failed: $!" if not defined $heartbeat_pid;
@ -188,7 +174,7 @@ sub compiler_server {
if ($heartbeat == -1) { if ($heartbeat == -1) {
print "fucking dead, restarting\n"; print "fucking dead, restarting\n";
waitpid $heartbeat_pid, 0; waitpid $heartbeat_pid, 0;
vm_stop $vm_pid; #vm_stop;
next; next;
} }
@ -212,11 +198,6 @@ sub compiler_server {
my $killed = 0; my $killed = 0;
eval { eval {
my $lang;
my $nick;
my $channel;
my $code = "";
local $SIG{ALRM} = sub { die 'Timed-out'; }; local $SIG{ALRM} = sub { die 'Timed-out'; };
alarm 5; alarm 5;
@ -226,58 +207,42 @@ sub compiler_server {
alarm 5; alarm 5;
print "got: [$line]\n"; print "got: [$line]\n";
if($line =~ m/^compile:end$/) { if($heartbeat <= 0) {
if($heartbeat <= 0) { print "No heartbeat yet, ignoring compile attempt.\n";
print "No heartbeat yet, ignoring compile attempt.\n"; print $client "Recovering from previous snippet, please wait.\n" if gettimeofday - $last_wait > 60;
print $client "$nick: Recovering from previous snippet, please wait.\n" if gettimeofday - $last_wait > 60; $last_wait = gettimeofday;
$last_wait = gettimeofday; last;
last;
}
print "Attempting compile...\n";
alarm 0;
my ($ret, $result) = execute("./compiler_vm_client.pl \Q$lang\E \Q$nick\E \Q$channel\E \Q$code\E");
if(not defined $ret) {
#print "parent continued\n";
print "parent continued [$result]\n";
$timed_out = 1 if $result == 243; # -13 == 243
$killed = 1 if $result == 242; # -14 = 242
last;
}
$result =~ s/\s+$//;
print "Ret: $ret; result: [$result]\n";
if($result =~ m/\[Killed\]$/) {
print "Process was killed\n";
$killed = 1;
}
if($ret == -13) {
print $client "$nick: ";
}
print $client $result . "\n";
close $client;
$ret = -14 if $killed;
# child exit
print "child exit\n";
exit $ret;
} }
if($line =~ /compile:([^:]+):([^:]+):(.*)$/) { print "Attempting compile...\n";
$nick = $1; alarm 0;
$channel = $2;
$lang = $3; my ($ret, $result) = execute("perl compiler_vm_client.pl $line");
$code = "";
next; if(not defined $ret) {
#print "parent continued\n";
print "parent continued [$result]\n";
$timed_out = 1 if $result == 243 or $result == -13; # -13 == 243
$killed = 1 if $result == 242 or $result == -14; # -14 = 242
last;
} }
$code .= $line . "\n"; $result =~ s/\s+$//;
print "Ret: $ret; result: [$result]\n";
if($result =~ m/\[Killed\]$/) {
print "Process was killed\n";
$killed = 1;
}
print $client $result . "\n";
close $client;
$ret = -14 if $killed;
# child exit
print "child exit\n";
exit $ret;
} }
alarm 0; alarm 0;
@ -287,16 +252,20 @@ sub compiler_server {
close $client; close $client;
#next unless ($timed_out or $killed); print "timed out: $timed_out; killed: $killed\n";
#next unless $timed_out; next unless ($timed_out or $killed);
print "stopping vm $vm_pid\n"; vm_reset;
vm_stop $vm_pid; next;
print "stopping vm\n";
#vm_stop;
$running = 0; $running = 0;
last; last;
} }
print "Compiler server no longer running, restarting...\n"; print "Compiler server no longer running, restarting...\n";
} }
print "waiting on heartbeat pid?\n";
waitpid($heartbeat_pid, 0); waitpid($heartbeat_pid, 0);
} }
} }

View File

@ -1,5 +1,9 @@
#!/usr/bin/perl #!/usr/bin/perl
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
use warnings; use warnings;
use strict; use strict;
@ -9,11 +13,12 @@ use IPC::Shareable;
use Time::HiRes qw/gettimeofday/; use Time::HiRes qw/gettimeofday/;
my $SERVER_PORT = 9000; my $SERVER_PORT = 9000;
my $MONITOR_PORT = 3335;
my $SERIAL_PORT = 3333; my $SERIAL_PORT = 3333;
my $HEARTBEAT_PORT = 3336; my $HEARTBEAT_PORT = 3336;
my $DOMAIN_NAME = 'compiler';
my $COMPILE_TIMEOUT = 10; my $COMPILE_TIMEOUT = 10;
my $NOGRAPHIC = 1;
sub server_listen { sub server_listen {
my $port = shift @_; my $port = shift @_;
@ -32,18 +37,41 @@ sub server_listen {
} }
sub vm_stop { sub vm_stop {
system("virsh shutdown $DOMAIN_NAME"); my $pid = shift @_;
return if not defined $pid;
print "killing vm $pid\n";
kill 'INT', $pid;
waitpid($pid, 0);
} }
sub vm_start { sub vm_start {
system("virsh start $DOMAIN_NAME"); my $pid = fork;
if(not defined $pid) {
die "fork failed: $!";
}
if($pid == 0) {
my $command = "qemu-system-x86_64 -M pc -net none -hda compiler-snap.qcow2 -m 512 -monitor tcp:127.0.0.1:$MONITOR_PORT,server,nowait -serial tcp:127.0.0.1:$SERIAL_PORT,server,nowait -serial tcp:127.0.0.1:$HEARTBEAT_PORT,server -boot c -enable-kvm -loadvm 1" . ($NOGRAPHIC ? " -nographic" : "");
my @command_list = split / /, $command;
exec(@command_list);
} else {
return $pid;
}
} }
sub vm_reset { sub vm_reset {
return if $ENV{NORESET}; use IO::Socket;
#system("virsh detach-disk $DOMAIN_NAME vdb");
system("virsh snapshot-revert $DOMAIN_NAME 1"); print "Resetting vm\n";
#system("virsh attach-disk $DOMAIN_NAME --source /var/lib/libvirt/images/factdata.qcow2 --target vdb"); my $sock = IO::Socket::INET->new(PeerAddr => '127.0.0.1', PeerPort => $MONITOR_PORT, Prot => 'tcp');
if(not defined $sock) {
print "[vm_reset] Unable to connect to monitor: $!\n";
return;
}
print $sock "loadvm 1\n";
close $sock;
print "Reset vm\n"; print "Reset vm\n";
} }
@ -52,41 +80,28 @@ sub execute {
print "execute($cmdline)\n"; print "execute($cmdline)\n";
my @list = split / /, $cmdline;
my ($ret, $result); my ($ret, $result);
#$SIG{CHLD} = 'IGNORE';
my $child = fork; my $child = fork;
if($child == 0) { if($child == 0) {
($ret, $result) = eval { ($ret, $result) = eval {
my $result = ''; my $result = '';
my $pid = open(my $fh, '-|', @list); my $pid = open(my $fh, '-|', "$cmdline 2>&1");
if (not defined $pid) {
print "Couldn't fork: $!\n";
return (-13, "[Fatal error]");
}
local $SIG{ALRM} = sub { print "Time out\n"; kill 9, $pid; print "sent KILL to $pid\n"; die "Timed-out: $result\n"; }; local $SIG{ALRM} = sub { print "Time out\n"; kill 9, $pid; print "sent KILL to $pid\n"; die "Timed-out: $result\n"; };
alarm($COMPILE_TIMEOUT); alarm($COMPILE_TIMEOUT);
print "Reading...\n";
while(my $line = <$fh>) { while(my $line = <$fh>) {
print "read [$line]\n";
$result .= $line; $result .= $line;
} }
close $fh; close $fh;
print "Done reading.\n";
my $ret = $? >> 8; my $ret = $? >> 8;
alarm 0; alarm 0;
#print "[$ret, $result]\n";
print "[$ret, $result]\n";
return ($ret, $result); return ($ret, $result);
}; };
@ -98,7 +113,6 @@ sub execute {
return ($ret, $result); return ($ret, $result);
} else { } else {
waitpid($child, 0); waitpid($child, 0);
print "?: $?\n";
my $result = $? >> 8; my $result = $? >> 8;
print "child exited, parent continuing [result = $result]\n"; print "child exited, parent continuing [result = $result]\n";
return (undef, $result); return (undef, $result);
@ -120,8 +134,8 @@ sub compiler_server {
$running = 1; $running = 1;
$heartbeat = 0; $heartbeat = 0;
vm_reset; my $vm_pid = vm_start;
print "vm started\n"; print "vm started pid: $vm_pid\n";
$heartbeat_pid = fork; $heartbeat_pid = fork;
die "Fork failed: $!" if not defined $heartbeat_pid; die "Fork failed: $!" if not defined $heartbeat_pid;
@ -174,7 +188,7 @@ sub compiler_server {
if ($heartbeat == -1) { if ($heartbeat == -1) {
print "fucking dead, restarting\n"; print "fucking dead, restarting\n";
waitpid $heartbeat_pid, 0; waitpid $heartbeat_pid, 0;
#vm_stop; vm_stop $vm_pid;
next; next;
} }
@ -198,6 +212,11 @@ sub compiler_server {
my $killed = 0; my $killed = 0;
eval { eval {
my $lang;
my $nick;
my $channel;
my $code = "";
local $SIG{ALRM} = sub { die 'Timed-out'; }; local $SIG{ALRM} = sub { die 'Timed-out'; };
alarm 5; alarm 5;
@ -207,42 +226,58 @@ sub compiler_server {
alarm 5; alarm 5;
print "got: [$line]\n"; print "got: [$line]\n";
if($heartbeat <= 0) { if($line =~ m/^compile:end$/) {
print "No heartbeat yet, ignoring compile attempt.\n"; if($heartbeat <= 0) {
print $client "Recovering from previous snippet, please wait.\n" if gettimeofday - $last_wait > 60; print "No heartbeat yet, ignoring compile attempt.\n";
$last_wait = gettimeofday; print $client "$nick: Recovering from previous snippet, please wait.\n" if gettimeofday - $last_wait > 60;
last; $last_wait = gettimeofday;
last;
}
print "Attempting compile...\n";
alarm 0;
my ($ret, $result) = execute("./compiler_vm_client.pl \Q$lang\E \Q$nick\E \Q$channel\E \Q$code\E");
if(not defined $ret) {
#print "parent continued\n";
print "parent continued [$result]\n";
$timed_out = 1 if $result == 243; # -13 == 243
$killed = 1 if $result == 242; # -14 = 242
last;
}
$result =~ s/\s+$//;
print "Ret: $ret; result: [$result]\n";
if($result =~ m/\[Killed\]$/) {
print "Process was killed\n";
$killed = 1;
}
if($ret == -13) {
print $client "$nick: ";
}
print $client $result . "\n";
close $client;
$ret = -14 if $killed;
# child exit
print "child exit\n";
exit $ret;
} }
print "Attempting compile...\n"; if($line =~ /compile:([^:]+):([^:]+):(.*)$/) {
alarm 0; $nick = $1;
$channel = $2;
my ($ret, $result) = execute("perl compiler_vm_client.pl $line"); $lang = $3;
$code = "";
if(not defined $ret) { next;
#print "parent continued\n";
print "parent continued [$result]\n";
$timed_out = 1 if $result == 243 or $result == -13; # -13 == 243
$killed = 1 if $result == 242 or $result == -14; # -14 = 242
last;
} }
$result =~ s/\s+$//; $code .= $line . "\n";
print "Ret: $ret; result: [$result]\n";
if($result =~ m/\[Killed\]$/) {
print "Process was killed\n";
$killed = 1;
}
print $client $result . "\n";
close $client;
$ret = -14 if $killed;
# child exit
print "child exit\n";
exit $ret;
} }
alarm 0; alarm 0;
@ -252,20 +287,16 @@ sub compiler_server {
close $client; close $client;
print "timed out: $timed_out; killed: $killed\n"; #next unless ($timed_out or $killed);
next unless ($timed_out or $killed); #next unless $timed_out;
vm_reset; print "stopping vm $vm_pid\n";
next; vm_stop $vm_pid;
print "stopping vm\n";
#vm_stop;
$running = 0; $running = 0;
last; last;
} }
print "Compiler server no longer running, restarting...\n"; print "Compiler server no longer running, restarting...\n";
} }
print "waiting on heartbeat pid?\n";
waitpid($heartbeat_pid, 0); waitpid($heartbeat_pid, 0);
} }
} }