From b77d03fdeea4cd1dd244f067de5d3ab199f2743a Mon Sep 17 00:00:00 2001 From: Srdjan Date: Wed, 10 Feb 2016 15:47:25 +1300 Subject: [PATCH] bug_15562: Use do() rather than system() to execute updatedatabase.pl from installer.pl That way: * no external process is spawned * code executes in the same perl process, which is required for plack multi-site I have a dream. A dream that one day all code from .pl's will be in some .pm's. Signed-off-by: Kyle M Hall --- C4/Installer/PerlDependencies.pm | 5 ++++ installer/install.pl | 54 +++++++++++++++++++++------------------- 2 files changed, 33 insertions(+), 26 deletions(-) diff --git a/C4/Installer/PerlDependencies.pm b/C4/Installer/PerlDependencies.pm index c4f5409..d2e3254 100644 --- a/C4/Installer/PerlDependencies.pm +++ b/C4/Installer/PerlDependencies.pm @@ -362,6 +362,11 @@ our $PERL_DEPS = { 'required' => '1', 'min_ver' => '0.46' }, + 'Capture::Tiny' => { + 'usage' => 'Core', + 'required' => '1', + 'min_ver' => '0.08' + }, 'HTTP::Cookies' => { 'usage' => 'Core', 'required' => '1', diff --git a/installer/install.pl b/installer/install.pl index ac43214..b89abb6 100755 --- a/installer/install.pl +++ b/installer/install.pl @@ -24,6 +24,7 @@ use diagnostics; use C4::InstallAuth; use CGI qw ( -utf8 ); use POSIX qw(strftime); +use Capture::Tiny qw(capture); use C4::Context; use C4::Output; @@ -388,40 +389,41 @@ elsif ( $step && $step == 3 ) { chk_log( $logdir, "updatedatabase-error_$filename_suffix" ) ); - my $cmd = C4::Context->config("intranetdir") - . "/installer/data/$info{dbms}/updatedatabase.pl >> $logfilepath 2>> $logfilepath_errors"; + my $script = C4::Context->config("intranetdir") . "/installer/data/$info{dbms}/updatedatabase.pl"; + my ($stdout, $stderr, $exit) = capture { do( $script ) }; + warn "Cannot execute $script: ".($@ || $!) if $@ || $!; - system($cmd ); - - my $fh; - open( $fh, "<", $logfilepath ) - or die "Cannot open log file $logfilepath: $!"; - my @report = <$fh>; - close $fh; - if (@report) { - $template->param( update_report => - [ map { { line => $_ } } split( /\n/, join( '', @report ) ) ] - ); + if ($stdout) { + if ( open( my $fh, ">>", $logfilepath ) ) { + print $fh $stdout; + close $fh; + } + else { + warn "Cannot open log file $logfilepath: $!"; + } + $template->param( update_report => [ map { line => $_ }, split( /\n/, $stdout ) ] ); $template->param( has_update_succeeds => 1 ); } else { - eval { `rm $logfilepath` }; +# eval { `rm $logfilepath` }; } - open( $fh, "<", $logfilepath_errors ) - or die "Cannot open log file $logfilepath_errors: $!"; - @report = <$fh>; - close $fh; - if (@report) { - $template->param( update_errors => - [ map { { line => $_ } } split( /\n/, join( '', @report ) ) ] - ); + + if ($stderr) { + if ( open( my $fh, ">>", $logfilepath_errors ) ) { + print $fh $stderr; + close $fh; + } + else { + warn "Cannot open log file $logfilepath_errors: $!"; + } + my @errors = split( /\n/, $stderr ); + $template->param( update_errors => [ map { line => $_ }, @errors ] ); $template->param( has_update_errors => 1 ); - warn -"The following errors were returned while attempting to run the updatedatabase.pl script:\n"; - foreach my $line (@report) { warn "$line\n"; } + warn "The following errors were returned while attempting to run the updatedatabase.pl script:\n"; + warn $_ foreach @errors; } else { - eval { `rm $logfilepath_errors` }; +# eval { `rm $logfilepath_errors` }; } $template->param( $op => 1 ); } -- 2.7.4