* 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 <digimer@alteeve.ca>
main
Digimer 7 years ago
parent d864bd6dac
commit f1bd4b99ca
  1. 18
      Anvil/Tools/Database.pm
  2. 70
      Anvil/Tools/System.pm
  3. 6
      share/words.xml
  4. 53
      tools/anvil-prep-database

@ -1192,33 +1192,51 @@ sub get_local_id
my $anvil = $self->parent; my $anvil = $self->parent;
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 3, key => "log_0125", variables => { method => "Database->get_local_id()" }}); $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 $local_id = "";
my $network_details = $anvil->Get->network_details; my $network_details = $anvil->Get->network_details;
foreach my $id (sort {$a cmp $b} keys %{$anvil->data->{database}}) 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}) if ($network_details->{hostname} eq $anvil->data->{database}{$id}{host})
{ {
$local_id = $id; $local_id = $id;
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { local_id => $local_id }});
last; last;
} }
} }
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { local_id => $local_id }});
if (not $local_id) if (not $local_id)
{ {
foreach my $interface (sort {$a cmp $b} keys %{$network_details->{interface}}) foreach my $interface (sort {$a cmp $b} keys %{$network_details->{interface}})
{ {
my $ip_address = $network_details->{interface}{$interface}{ip}; my $ip_address = $network_details->{interface}{$interface}{ip};
my $subnet_mask = $network_details->{interface}{$interface}{netmask}; 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}}) 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}) if ($ip_address eq $anvil->data->{database}{$id}{host})
{ {
$local_id = $id; $local_id = $id;
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { local_id => $local_id }});
last; last;
} }
} }
} }
} }
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { local_id => $local_id }});
return($local_id); return($local_id);
} }

@ -120,7 +120,7 @@ sub call
my $parameter = shift; my $parameter = shift;
my $anvil = $self->parent; my $anvil = $self->parent;
my $debug = 3; my $debug = defined $parameter->{debug} ? $parameter->{debug} : 3;
my $line = defined $parameter->{line} ? $parameter->{line} : __LINE__; my $line = defined $parameter->{line} ? $parameter->{line} : __LINE__;
my $shell_call = defined $parameter->{shell_call} ? $parameter->{shell_call} : ""; my $shell_call = defined $parameter->{shell_call} ? $parameter->{shell_call} : "";
my $secure = defined $parameter->{secure} ? $parameter->{secure} : 0; my $secure = defined $parameter->{secure} ? $parameter->{secure} : 0;
@ -135,22 +135,49 @@ sub call
} }
else else
{ {
# Make the system call # If this is an executable, make sure the program exists.
$output = ""; my $found = 1;
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => $debug, secure => $secure, key => "log_0011", variables => { shell_call => $shell_call }}); if (($shell_call =~ /^(\/.*?) /) or ($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 $program = $1;
my $line = $_; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, secure => $secure, list => { program => $program }});
$line =~ s/\n$//; if (not -e $program)
$line =~ s/\r$//; {
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => $debug, secure => $secure, key => "log_0017", variables => { line => $line }}); $found = 0;
$output .= $line."\n"; $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 }}); $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 $anvil = $self->parent;
my $return = undef; my $return = undef;
my $debug = defined $parameter->{debug} ? $parameter->{debug} : 3;
my $daemon = defined $parameter->{daemon} ? $parameter->{daemon} : ""; my $daemon = defined $parameter->{daemon} ? $parameter->{daemon} : "";
my $say_daemon = $daemon =~ /\.service$/ ? $daemon : $daemon.".service"; 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:\$?"}); 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 => 3, list => { output => $output }}); $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { output => $output }});
foreach my $line (split/\n/, $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+)/) if ($line =~ /return_code:(\d+)/)
{ {
$return = $1; $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); return($return);
} }

@ -209,6 +209,12 @@ The database connection error was:
<key name="log_0136"><![CDATA[[ Error ] - The method: [#!variable!method!#] was called with either 'job_uuid': [#!variable!job_uuid!#] not being passed (or was not a valid UUID), or 'job_name': [#!variable!job_name!#] not being passed.]]></key> <key name="log_0136"><![CDATA[[ Error ] - The method: [#!variable!method!#] was called with either 'job_uuid': [#!variable!job_uuid!#] not being passed (or was not a valid UUID), or 'job_name': [#!variable!job_name!#] not being passed.]]></key>
<key name="log_0137"><![CDATA[[ Error ] - The method: [#!variable!method!#] was called with an invalid value for: [#!variable!variable_name!#]: -> [#!variable!variable_value!#]. See 'perldoc Anvil::Tools::#!variable!module!#' for valid options.]]></key> <key name="log_0137"><![CDATA[[ Error ] - The method: [#!variable!method!#] was called with an invalid value for: [#!variable!variable_name!#]: -> [#!variable!variable_value!#]. See 'perldoc Anvil::Tools::#!variable!module!#' for valid options.]]></key>
<key name="log_0138"><![CDATA[[ Error ] - The method: [#!variable!method!#] was with an invalid 'job_progress': [#!variable!job_progress!#]. It needs to be a whole-number value between 0 and 100, inclusive.]]></key> <key name="log_0138"><![CDATA[[ Error ] - The method: [#!variable!method!#] was with an invalid 'job_progress': [#!variable!job_progress!#]. It needs to be a whole-number value between 0 and 100, inclusive.]]></key>
<key name="log_0139"><![CDATA[[ Error ] - Failed to initialized the database! The file: [#!variable!file!#] already exists, which should not be the case when initializing the database.]]></key>
<key name="log_0140"><![CDATA[[ Error ] - Failed to initialized the database! Information on why this failed might be found in: [#!variable!file!#].]]></key>
<key name="log_0141"><![CDATA[[ Error ] - The system call: [#!variable!system_call!#] will fail because the program: [#!variable!program!#] doesn't exist.]]></key>
<key name="log_0142"><![CDATA[[ Error ] - The system call: [#!variable!system_call!#] will fail because the program: [#!variable!program!#] isn't executable.]]></key>
<key name="log_0143">Failed to find a local ID, no databases are stored on this machine.</key>
<key name="log_0144">PostgreSQL server is not installed, unable to proceed.</key>
<!-- Test words. Do NOT change unless you update 't/Words.t' or tests will needlessly fail. --> <!-- Test words. Do NOT change unless you update 't/Words.t' or tests will needlessly fail. -->
<key name="t_0000">Test</key> <key name="t_0000">Test</key>

@ -9,7 +9,7 @@
# 2 = Failed to start postgres # 2 = Failed to start postgres
# 3 = ScanCore user not set in the local ID in striker.conf # 3 = ScanCore user not set in the local ID in striker.conf
# 4 = Failed to create the database user. # 4 = Failed to create the database user.
# 5 = # 5 = PostgreSQL not installed.
use strict; use strict;
use warnings; use warnings;
@ -33,8 +33,6 @@ $anvil->Log->secure({set => 1});
# Read switches # Read switches
$anvil->Get->switches; $anvil->Get->switches;
die $THIS_FILE." ".__LINE__."; log level: [".$anvil->Log->level."]\n";
# Paths # Paths
$anvil->data->{path}{config}{'striker.conf'} = "/etc/anvil/anvil.conf"; $anvil->data->{path}{config}{'striker.conf'} = "/etc/anvil/anvil.conf";
$anvil->Storage->read_config({file => $anvil->data->{path}{config}{'striker.conf'}}); $anvil->Storage->read_config({file => $anvil->data->{path}{config}{'striker.conf'}});
@ -46,20 +44,39 @@ if ($local_id)
# Start checks # Start checks
my $running = $anvil->System->check_daemon({daemon => "postgresql"}); my $running = $anvil->System->check_daemon({daemon => "postgresql"});
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => { running => $running }}); $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? # Do we need to initialize the databae?
if (not -e $anvil->data->{path}{configs}{'pg_hba.conf'}) if (not -e $anvil->data->{path}{configs}{'pg_hba.conf'})
{ {
# Initialize. # Initialize.
my $output = $anvil->System->call({shell_call => $anvil->data->{path}{exe}{'postgresql-setup'}." initdb", source => $THIS_FILE, line => __LINE__}); 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? # Did it succeed?
if (not -e $anvil->data->{path}{configs}{'pg_hba.conf'}) if (not -e $anvil->data->{path}{configs}{'pg_hba.conf'})
{ {
# Failed... # 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); exit(1);
} }
else else
@ -149,10 +166,10 @@ if ($local_id)
} }
} }
# Start the daemon. It might fail if it has never been initialized. # Start the daemon. '0' = started, anything else is a problem.
my $started = $anvil->System->start_daemon({daemon => "postgresql"}); my $return_code = $anvil->System->start_daemon({daemon => "postgresql"});
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => { started => $started }}); $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { return_code => $return_code }});
if ($started) if ($return_code eq "0")
{ {
# Started the daemon. # Started the daemon.
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 1, key => "log_0059"}); $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 1, key => "log_0059"});
@ -160,7 +177,7 @@ if ($local_id)
else else
{ {
# Failed to start # 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); exit(2);
} }
} }
@ -198,7 +215,7 @@ if ($local_id)
if (not $scancore_user) if (not $scancore_user)
{ {
# No database user defined # 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); 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__}); 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$/) if ($line =~ /^ $scancore_database$/)
{ {
# Database already exists. # 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; $create_database = 0;
last; last;
} }
@ -289,14 +306,14 @@ if ($local_id)
if ($line =~ /^ $scancore_database$/) if ($line =~ /^ $scancore_database$/)
{ {
# Database created # 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; $database_exists = 1;
last; last;
} }
} }
if (not $database_exists) 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); exit(5);
} }
} }
@ -308,7 +325,7 @@ if ($local_id)
if (-e $anvil->data->{path}{secure}{postgres_pgpass}) if (-e $anvil->data->{path}{secure}{postgres_pgpass})
{ {
# Failed to unlink the file. # 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 else
{ {
# Didn't find an entry for this machine. # Didn't find an entry for this machine. This is normal on nodes.
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 3, key => "message_0010"}); $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 3, key => "log_0143"});
} }
exit(0); exit(0);

Loading…
Cancel
Save