* 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. 42
      Anvil/Tools/System.pm
  3. 6
      share/words.xml
  4. 51
      tools/anvil-prep-database

@ -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);
}

@ -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;
@ -134,6 +134,32 @@ sub call
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0043"});
}
else
{
# If this is an executable, make sure the program exists.
my $found = 1;
if (($shell_call =~ /^(\/.*?) /) or ($shell_call =~ /^(\/.*)/))
{
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 = "";
@ -152,6 +178,7 @@ sub call
chomp($output);
$output =~ s/\n$//s;
}
}
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, secure => $secure, list => { output => $output }});
return($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);
}

@ -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_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_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. -->
<key name="t_0000">Test</key>

@ -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...
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);

Loading…
Cancel
Save