From 807bfbf235e8b80c1cd79d7828747930fa3b3131 Mon Sep 17 00:00:00 2001 From: Pragmatic Software Date: Mon, 18 Sep 2017 21:36:40 -0700 Subject: [PATCH] Persist-key now works on VM-based code-factoids --- PBot/FactoidModuleLauncher.pm | 4 +- PBot/Factoids.pm | 5 +++ modules/compiler_vm/compiler_server_virsh.pl | 2 + modules/compiler_vm/compiler_vm_server.pl | 39 +++++++++++++++---- modules/compiler_vm/languages/_default.pm | 5 +++ .../compiler_vm/languages/server/_default.pm | 1 + 6 files changed, 47 insertions(+), 9 deletions(-) diff --git a/PBot/FactoidModuleLauncher.pm b/PBot/FactoidModuleLauncher.pm index 9c0d39f0..bbd8d145 100644 --- a/PBot/FactoidModuleLauncher.pm +++ b/PBot/FactoidModuleLauncher.pm @@ -60,7 +60,7 @@ sub execute_module { my $module = $self->{pbot}->{factoids}->{factoids}->hash->{$channel}->{$trigger}->{action}; my $module_dir = $self->{pbot}->{registry}->get_value('general', 'module_dir'); - $self->{pbot}->{logger}->log("(" . (defined $from ? $from : "(undef)") . "): $nick!$user\@$host: Executing module $module $arguments\n"); + $self->{pbot}->{logger}->log("(" . (defined $from ? $from : "(undef)") . "): $nick!$user\@$host: Executing module [$command] $module $arguments\n"); $arguments =~ s/\$nick/$nick/g; $arguments =~ s/\$channel/$from/g; @@ -160,6 +160,8 @@ sub execute_module { } if ($command eq 'code-factoid') { + $text =~ s/\s+$//g; + $self->{pbot}->{logger}->log("No text result from code-factoid.\n") and exit 0 if not length $text; $text = $self->{pbot}->{factoids}->handle_action($nick, $user, $host, $from, $root_channel, $root_keyword, $root_keyword, $arguments, $text, $tonick, 0, $referenced, undef, $root_keyword); } diff --git a/PBot/Factoids.pm b/PBot/Factoids.pm index 2a290901..11c7efc5 100644 --- a/PBot/Factoids.pm +++ b/PBot/Factoids.pm @@ -663,6 +663,11 @@ sub execute_code_factoid_using_vm { } my %h = (nick => $nick, channel => $from, lang => $lang, code => $code, arguments => $arguments, factoid => "$chan:$keyword"); + + if (exists $self->{factoids}->hash->{$chan}->{$keyword}->{'persist-key'}) { + $h{'persist-key'} = $self->{factoids}->hash->{$chan}->{$keyword}->{'persist-key'}; + } + my $json = encode_json \%h; $self->{pbot}->{factoids}->{factoidmodulelauncher}->execute_module($from, $tonick, $nick, $user, $host, 'code-factoid', $chan, $root_keyword, "compiler", $json, 0); return ""; diff --git a/modules/compiler_vm/compiler_server_virsh.pl b/modules/compiler_vm/compiler_server_virsh.pl index 37680037..ce3be0c5 100755 --- a/modules/compiler_vm/compiler_server_virsh.pl +++ b/modules/compiler_vm/compiler_server_virsh.pl @@ -40,7 +40,9 @@ sub vm_start { } sub vm_reset { + system("virsh detach-disk $DOMAIN_NAME vdb"); system("virsh snapshot-revert $DOMAIN_NAME 1"); + system("virsh attach-disk $DOMAIN_NAME --source /var/lib/libvirt/images/factdata.qcow2 --target vdb"); print "Reset vm\n"; } diff --git a/modules/compiler_vm/compiler_vm_server.pl b/modules/compiler_vm/compiler_vm_server.pl index 2fcdc085..23e961bc 100755 --- a/modules/compiler_vm/compiler_vm_server.pl +++ b/modules/compiler_vm/compiler_vm_server.pl @@ -8,7 +8,6 @@ use warnings; use strict; use File::Basename; -use POSIX; use JSON; my $USERNAME = 'compiler'; @@ -84,8 +83,19 @@ sub run_server { exit; } - POSIX::setgid($gid); - POSIX::setuid($uid); + if ($compile_in->{'persist-key'}) { + system("mount /dev/vdb1 /root/factdata"); + system("mkdir /root/factdata/$compile_in->{'persist-key'}"); + system("cp -R -p /root/factdata/$compile_in->{'persist-key'}/* /home/compiler/"); + } + + system("chown -R compiler /home/compiler/*"); + system("chgrp -R compiler /home/compiler/*"); + system("chmod -R 755 /home/compiler"); + system("rm -rf /home/compiler/prog*"); + + $( = $gid; + $< = $uid; my $result = interpret(%$compile_in); @@ -95,6 +105,16 @@ sub run_server { print "Done compiling; result: [$result] [$json]\n"; print $output "result:$json\n"; print $output "result:end\n"; + + $( = 0; + $< = 0; + + if ($compile_in->{'persist-key'}) { + system("id"); + system("cp -R -p /home/compiler/* /root/factdata/$compile_in->{'persist-key'}/"); + system("umount /root/factdata"); + } + exit; } else { waitpid $pid, 0; @@ -126,8 +146,7 @@ sub interpret { $h{lang} = '_default' if not exists $languages{$h{lang}}; - system("chmod -R 755 /home/compiler"); - system("rm -rf /home/compiler/prog*"); + chdir("/home/compiler"); my $mod = $h{lang}->new(%h); @@ -136,9 +155,13 @@ sub interpret { $mod->postprocess if not $mod->{error} and not $mod->{done}; if (exists $mod->{no_output} or not length $mod->{output}) { - $mod->{output} .= "\n" if length $mod->{output}; - $mod->{output} .= "Success (no output).\n" if not $mod->{error}; - $mod->{output} .= "Success (exit code $mod->{error}).\n" if $mod->{error}; + if ($h{factoid}) { + $mod->{output} = ""; + } else { + $mod->{output} .= "\n" if length $mod->{output}; + $mod->{output} .= "Success (no output).\n" if not $mod->{error}; + $mod->{output} .= "Success (exit code $mod->{error}).\n" if $mod->{error}; + } } return $mod->{output}; diff --git a/modules/compiler_vm/languages/_default.pm b/modules/compiler_vm/languages/_default.pm index d191f980..592f3dc3 100755 --- a/modules/compiler_vm/languages/_default.pm +++ b/modules/compiler_vm/languages/_default.pm @@ -33,6 +33,7 @@ sub new { $self->{max_history} = $conf{max_history} // 10000; $self->{arguments} = $conf{arguments}; $self->{factoid} = $conf{factoid}; + $self->{'persist-key'} = $conf{'persist-key'}; $self->{default_options} = ''; $self->{cmdline} = 'echo Hello, world!'; @@ -374,6 +375,10 @@ sub execute { my $compile_in = { lang => $self->{lang}, sourcefile => $self->{sourcefile}, execfile => $self->{execfile}, cmdline => $cmdline, input => $input, date => $date, arguments => $self->{arguments}, code => $pretty_code }; + + $compile_in->{'factoid'} = $self->{'factoid'} if length $self->{'factoid'}; + $compile_in->{'persist-key'} = $self->{'persist-key'} if length $self->{'persist-key'}; + my $compile_json = encode_json($compile_in); #print STDERR "Sending [$compile_json] to vm_server\n"; diff --git a/modules/compiler_vm/languages/server/_default.pm b/modules/compiler_vm/languages/server/_default.pm index 33baee8b..97fb4a8c 100755 --- a/modules/compiler_vm/languages/server/_default.pm +++ b/modules/compiler_vm/languages/server/_default.pm @@ -25,6 +25,7 @@ sub new { $self->{date} = $conf{date}; $self->{arguments} = $conf{arguments}; $self->{factoid} = $conf{factoid}; + $self->{'persist-key'} = $conf{'persist-key'}; $self->initialize(%conf);