From f1bd4b99ca4e7e670ed674b9557e7c2f3ee5ed9a Mon Sep 17 00:00:00 2001 From: Digimer Date: Wed, 27 Dec 2017 00:45:59 -0400 Subject: [PATCH] * Fixed more issues with anvil-prep-database. * Added the 'debug' parameter to System->call(). Also added a check to make sure that the called executable exists. * Added the 'debug' parameter to System->start_daemon(). Signed-off-by: Digimer --- Anvil/Tools/Database.pm | 18 ++++++++++ Anvil/Tools/System.pm | 70 +++++++++++++++++++++++++++------------ share/words.xml | 6 ++++ tools/anvil-prep-database | 53 +++++++++++++++++++---------- 4 files changed, 108 insertions(+), 39 deletions(-) diff --git a/Anvil/Tools/Database.pm b/Anvil/Tools/Database.pm index 81540473..345cc58c 100755 --- a/Anvil/Tools/Database.pm +++ b/Anvil/Tools/Database.pm @@ -1192,33 +1192,51 @@ sub get_local_id my $anvil = $self->parent; $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 3, key => "log_0125", variables => { method => "Database->get_local_id()" }}); + my $debug = 3; my $local_id = ""; my $network_details = $anvil->Get->network_details; foreach my $id (sort {$a cmp $b} keys %{$anvil->data->{database}}) { + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { + "network_details->hostname" => $network_details->{hostname}, + "database::${id}::host" => $anvil->data->{database}{$id}{host}, + }}); if ($network_details->{hostname} eq $anvil->data->{database}{$id}{host}) { $local_id = $id; + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { local_id => $local_id }}); last; } } + + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { local_id => $local_id }}); if (not $local_id) { foreach my $interface (sort {$a cmp $b} keys %{$network_details->{interface}}) { my $ip_address = $network_details->{interface}{$interface}{ip}; my $subnet_mask = $network_details->{interface}{$interface}{netmask}; + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { + ip_address => $ip_address, + subnet_mask => $subnet_mask, + }}); foreach my $id (sort {$a cmp $b} keys %{$anvil->data->{database}}) { + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { + ip_address => $ip_address, + "database::${id}::host" => $anvil->data->{database}{$id}{host}, + }}); if ($ip_address eq $anvil->data->{database}{$id}{host}) { $local_id = $id; + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { local_id => $local_id }}); last; } } } } + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { local_id => $local_id }}); return($local_id); } diff --git a/Anvil/Tools/System.pm b/Anvil/Tools/System.pm index 9b8b90f9..baddad11 100755 --- a/Anvil/Tools/System.pm +++ b/Anvil/Tools/System.pm @@ -120,7 +120,7 @@ sub call my $parameter = shift; my $anvil = $self->parent; - my $debug = 3; + my $debug = defined $parameter->{debug} ? $parameter->{debug} : 3; my $line = defined $parameter->{line} ? $parameter->{line} : __LINE__; my $shell_call = defined $parameter->{shell_call} ? $parameter->{shell_call} : ""; my $secure = defined $parameter->{secure} ? $parameter->{secure} : 0; @@ -135,22 +135,49 @@ sub call } else { - # Make the system call - $output = ""; - $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => $debug, secure => $secure, key => "log_0011", variables => { shell_call => $shell_call }}); - open (my $file_handle, $shell_call." 2>&1 |") or $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, secure => $secure, priority => "err", key => "log_0014", variables => { shell_call => $shell_call, error => $! }}); - while(<$file_handle>) + # If this is an executable, make sure the program exists. + my $found = 1; + if (($shell_call =~ /^(\/.*?) /) or ($shell_call =~ /^(\/.*)/)) { - chomp; - my $line = $_; - $line =~ s/\n$//; - $line =~ s/\r$//; - $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => $debug, secure => $secure, key => "log_0017", variables => { line => $line }}); - $output .= $line."\n"; + my $program = $1; + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, secure => $secure, list => { program => $program }}); + if (not -e $program) + { + $found = 0; + $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 1, priority => "alert", key => "log_0141", variable => { + program => $program, + shell_call => $shell_call, + }}); + } + elsif (not -x $program) + { + $found = 0; + $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 1, priority => "alert", key => "log_0142", variable => { + program => $program, + shell_call => $shell_call, + }}); + } + } + + if ($found) + { + # Make the system call + $output = ""; + $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => $debug, secure => $secure, key => "log_0011", variables => { shell_call => $shell_call }}); + open (my $file_handle, $shell_call." 2>&1 |") or $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, secure => $secure, priority => "err", key => "log_0014", variables => { shell_call => $shell_call, error => $! }}); + while(<$file_handle>) + { + chomp; + my $line = $_; + $line =~ s/\n$//; + $line =~ s/\r$//; + $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => $debug, secure => $secure, key => "log_0017", variables => { line => $line }}); + $output .= $line."\n"; + } + close $file_handle; + chomp($output); + $output =~ s/\n$//s; } - close $file_handle; - chomp($output); - $output =~ s/\n$//s; } $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, secure => $secure, list => { output => $output }}); @@ -1460,23 +1487,24 @@ sub start_daemon my $anvil = $self->parent; my $return = undef; + my $debug = defined $parameter->{debug} ? $parameter->{debug} : 3; my $daemon = defined $parameter->{daemon} ? $parameter->{daemon} : ""; my $say_daemon = $daemon =~ /\.service$/ ? $daemon : $daemon.".service"; - $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => { daemon => $daemon, say_daemon => $say_daemon }}); + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { daemon => $daemon, say_daemon => $say_daemon }}); - my $output = $anvil->System->call({shell_call => $anvil->data->{path}{exe}{systemctl}." start ".$say_daemon."; ".$anvil->data->{path}{exe}{'echo'}." return_code:\$?"}); - $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => { output => $output }}); + my $output = $anvil->System->call({shell_call => $anvil->data->{path}{exe}{systemctl}." start ".$say_daemon."; ".$anvil->data->{path}{exe}{'echo'}." return_code:\$?", debug => $debug}); + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { output => $output }}); foreach my $line (split/\n/, $output) { - $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => { line => $line }}); + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { line => $line }}); if ($line =~ /return_code:(\d+)/) { $return = $1; - $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => { 'return' => $return }}); + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { 'return' => $return }}); } } - $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => { 'return' => $return }}); + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { 'return' => $return }}); return($return); } diff --git a/share/words.xml b/share/words.xml index f177b353..f0e6a941 100644 --- a/share/words.xml +++ b/share/words.xml @@ -209,6 +209,12 @@ The database connection error was: [#!variable!variable_value!#]. See 'perldoc Anvil::Tools::#!variable!module!#' for valid options.]]> + + + + + Failed to find a local ID, no databases are stored on this machine. + PostgreSQL server is not installed, unable to proceed. Test diff --git a/tools/anvil-prep-database b/tools/anvil-prep-database index 0f06c620..a2417cee 100755 --- a/tools/anvil-prep-database +++ b/tools/anvil-prep-database @@ -9,7 +9,7 @@ # 2 = Failed to start postgres # 3 = ScanCore user not set in the local ID in striker.conf # 4 = Failed to create the database user. -# 5 = +# 5 = PostgreSQL not installed. use strict; use warnings; @@ -33,8 +33,6 @@ $anvil->Log->secure({set => 1}); # Read switches $anvil->Get->switches; -die $THIS_FILE." ".__LINE__."; log level: [".$anvil->Log->level."]\n"; - # Paths $anvil->data->{path}{config}{'striker.conf'} = "/etc/anvil/anvil.conf"; $anvil->Storage->read_config({file => $anvil->data->{path}{config}{'striker.conf'}}); @@ -46,20 +44,39 @@ if ($local_id) # Start checks my $running = $anvil->System->check_daemon({daemon => "postgresql"}); $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => { running => $running }}); - if (not $running) + if ($running eq "2") + { + # Not installed. + $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0144"}); + exit(5); + } + elsif (not $running) { # Do we need to initialize the databae? if (not -e $anvil->data->{path}{configs}{'pg_hba.conf'}) { # Initialize. my $output = $anvil->System->call({shell_call => $anvil->data->{path}{exe}{'postgresql-setup'}." initdb", source => $THIS_FILE, line => __LINE__}); - $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => { output => $output }}); + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { output => $output }}); # Did it succeed? if (not -e $anvil->data->{path}{configs}{'pg_hba.conf'}) { # Failed... - $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0050"}); + if ($output =~ /cannot create directory ‘(.*?)’: File exists/s) + { + my $file = $1; + $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 1, key => "log_0139", variables => { file => $file }}); + } + elsif ($output =~ /Initializing database ... failed, see (\/var\/.*?\.log)/s) + { + my $file = $1; + $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 1, key => "log_0140", variables => { file => $file }}); + } + else + { + $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0050"}); + } exit(1); } else @@ -149,10 +166,10 @@ if ($local_id) } } - # Start the daemon. It might fail if it has never been initialized. - my $started = $anvil->System->start_daemon({daemon => "postgresql"}); - $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => { started => $started }}); - if ($started) + # Start the daemon. '0' = started, anything else is a problem. + my $return_code = $anvil->System->start_daemon({daemon => "postgresql"}); + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { return_code => $return_code }}); + if ($return_code eq "0") { # Started the daemon. $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 1, key => "log_0059"}); @@ -160,7 +177,7 @@ if ($local_id) else { # Failed to start - $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "error_0002"}); + $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0094"}); exit(2); } } @@ -198,7 +215,7 @@ if ($local_id) if (not $scancore_user) { # No database user defined - $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "error_0003", variables => { id => $local_id }}); + $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0099", variables => { id => $local_id }}); exit(3); } my $user_list = $anvil->System->call({shell_call => $anvil->data->{path}{exe}{su}." - postgres -c \"".$anvil->data->{path}{exe}{psql}." template1 -c 'SELECT usename, usesysid FROM pg_catalog.pg_user;'\"", source => $THIS_FILE, line => __LINE__}); @@ -270,7 +287,7 @@ if ($local_id) if ($line =~ /^ $scancore_database$/) { # Database already exists. - $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 3, key => "message_0008", variables => { database => $scancore_database }}); + $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 3, key => "log_0105", variables => { database => $scancore_database }}); $create_database = 0; last; } @@ -289,14 +306,14 @@ if ($local_id) if ($line =~ /^ $scancore_database$/) { # Database created - $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 3, key => "message_0008", variables => { database => $scancore_database }}); + $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 3, key => "log_0110", variables => { database => $scancore_database }}); $database_exists = 1; last; } } if (not $database_exists) { - $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "error_0005", variables => { database => $scancore_database }}); + $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0109", variables => { database => $scancore_database }}); exit(5); } } @@ -308,7 +325,7 @@ if ($local_id) if (-e $anvil->data->{path}{secure}{postgres_pgpass}) { # Failed to unlink the file. - $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "alert", key => "warning_0001"}); + $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "alert", key => "log_0107"}); } } @@ -363,8 +380,8 @@ RateLimitBurst=0 } else { - # Didn't find an entry for this machine. - $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 3, key => "message_0010"}); + # Didn't find an entry for this machine. This is normal on nodes. + $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 3, key => "log_0143"}); } exit(0);