@ -27,26 +27,28 @@ if (($running_directory =~ /^\./) && ($ENV{PWD}))
$| = 1;
$| = 1;
my $anvil = Anvil::Tools->new();
my $anvil = Anvil::Tools->new();
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 3, secure => 0, key => "log_0115", variables => { program => $THIS_FILE }});
$anvil->Log->level({set => 2});
$anvil->Log->secure({set => 1});
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 2, secure => 0, key => "log_0115", variables => { program => $THIS_FILE }});
my $local_uuid = $anvil->Database->get_local_uuid();
my $local_uuid = $anvil->Database->get_local_uuid();
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3 , list => { local_uuid => $local_uuid }});
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2 , list => { local_uuid => $local_uuid }});
# If we didn't get the $local_uuid, then there is no entry for this system in anvil.conf yet, so we'll add it.
# If we didn't get the $local_uuid, then there is no entry for this system in anvil.conf yet, so we'll add it.
if (not $local_uuid)
if (not $local_uuid)
{
{
$local_uuid = add_to_local_config($anvil);
$local_uuid = add_to_local_config($anvil);
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3 , list => { local_uuid => $local_uuid }});
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2 , list => { local_uuid => $local_uuid }});
}
}
# Now configure!
# Now configure!
if ($local_uuid)
if ($local_uuid)
{
{
# Start checks
# Start checks
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3 , list => { "sys::service::postgresql" => $anvil->data->{sys}{daemon}{postgresql} }});
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2 , list => { "sys::service::postgresql" => $anvil->data->{sys}{daemon}{postgresql} }});
my $running = $anvil->System->check_daemon({daemon => $anvil->data->{sys}{daemon}{postgresql}});
my $running = $anvil->System->check_daemon({debug => 2, d aemon => $anvil->data->{sys}{daemon}{postgresql}});
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3 , list => { running => $running }});
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2 , list => { running => $running }});
if ($running eq "2")
if ($running eq "2")
{
{
# Not installed.
# Not installed.
@ -56,11 +58,11 @@ if ($local_uuid)
elsif (not $running)
elsif (not $running)
{
{
# Do we need to initialize the databae?
# Do we need to initialize the databae?
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3 , list => { "path::configs::pg_hba.conf" => $anvil->data->{path}{configs}{'pg_hba.conf'} }});
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2 , list => { "path::configs::pg_hba.conf" => $anvil->data->{path}{configs}{'pg_hba.conf'} }});
if (-e $anvil->data->{path}{configs}{'pg_hba.conf'})
if (-e $anvil->data->{path}{configs}{'pg_hba.conf'})
{
{
# It already exists.
# It already exists.
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3 , list => { "path::configs::pg_hba.conf" => $anvil->data->{path}{configs}{'pg_hba.conf'} }});
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2 , list => { "path::configs::pg_hba.conf" => $anvil->data->{path}{configs}{'pg_hba.conf'} }});
}
}
else
else
{
{
@ -96,7 +98,7 @@ if ($local_uuid)
# Setup postgresql.conf
# Setup postgresql.conf
my $postgresql_backup = $anvil->data->{path}{directories}{backups}."/pgsql/postgresql.conf";
my $postgresql_backup = $anvil->data->{path}{directories}{backups}."/pgsql/postgresql.conf";
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3 , list => { postgresql_backup => $postgresql_backup }});
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2 , list => { postgresql_backup => $postgresql_backup }});
$anvil->Storage->copy_file({
$anvil->Storage->copy_file({
source_file => $anvil->data->{path}{configs}{'postgresql.conf'},
source_file => $anvil->data->{path}{configs}{'postgresql.conf'},
target_file => $postgresql_backup,
target_file => $postgresql_backup,
@ -149,12 +151,12 @@ if ($local_uuid)
my $new_pg_hba_conf = "";
my $new_pg_hba_conf = "";
foreach my $line (split/\n/, $pg_hba_conf)
foreach my $line (split/\n/, $pg_hba_conf)
{
{
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3 , list => { line => $line }});
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2 , list => { line => $line }});
if ($line =~ /^host\s+all\s+all\s+\all\s+md5$/)
if ($line =~ /^host\s+all\s+all\s+\all\s+md5$/)
{
{
# No need to update.
# No need to update.
$update_file = 0;
$update_file = 0;
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3 , list => { update_file => $update_file }});
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2 , list => { update_file => $update_file }});
last;
last;
}
}
elsif ($line =~ /^# TYPE\s+DATABASE/)
elsif ($line =~ /^# TYPE\s+DATABASE/)
@ -185,7 +187,7 @@ if ($local_uuid)
}
}
# Start the daemon. '0' = started, anything else is a problem.
# Start the daemon. '0' = started, anything else is a problem.
my $return_code = $anvil->System->start_daemon({daemon => $anvil->data->{sys}{daemon}{postgresql}});
my $return_code = $anvil->System->start_daemon({debug => 2, d aemon => $anvil->data->{sys}{daemon}{postgresql}});
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { return_code => $return_code }});
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { return_code => $return_code }});
if ($return_code eq "0")
if ($return_code eq "0")
{
{
@ -193,7 +195,7 @@ if ($local_uuid)
$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"});
# Make sure it is enabled on boot.
# Make sure it is enabled on boot.
my $return_code = $anvil->System->enable_daemon({daemon => $anvil->data->{sys}{daemon}{postgresql}});
my $return_code = $anvil->System->enable_daemon({debug => 2, d aemon => $anvil->data->{sys}{daemon}{postgresql}});
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { return_code => $return_code }});
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { return_code => $return_code }});
}
}
else
else
@ -226,14 +228,14 @@ if ($local_uuid)
if (-e $anvil->data->{path}{secure}{postgres_pgpass})
if (-e $anvil->data->{path}{secure}{postgres_pgpass})
{
{
$created_pgpass = 1;
$created_pgpass = 1;
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3 , list => { created_pgpass => $created_pgpass }});
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2 , list => { created_pgpass => $created_pgpass }});
}
}
}
}
# Does the database user exist?
# Does the database user exist?
my $create_user = 1;
my $create_user = 1;
my $database_user = $anvil->data->{database}{$local_uuid}{user} ? $anvil->data->{database}{$local_uuid}{user} : $anvil->data->{sys}{database}{user};
my $database_user = $anvil->data->{database}{$local_uuid}{user} ? $anvil->data->{database}{$local_uuid}{user} : $anvil->data->{sys}{database}{user};
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3 , list => { database_user => $database_user }});
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2 , list => { database_user => $database_user }});
if (not $database_user)
if (not $database_user)
{
{
# No database user defined
# No database user defined
@ -241,19 +243,19 @@ if ($local_uuid)
$anvil->nice_exit({code => 3});
$anvil->nice_exit({code => 3});
}
}
my ($user_list, $return_code) = $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, $return_code) = $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__});
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => { output => $outpu t, return_code => $return_code }});
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { user_list => $user_lis t, return_code => $return_code }});
foreach my $line (split/\n/, $user_list)
foreach my $line (split/\n/, $user_list)
{
{
if ($line =~ /^ $database_user\s+\|\s+(\d+)/)
if ($line =~ /^ $database_user\s+\|\s+(\d+)/)
{
{
# User exists already
# User exists already
my $id = $1;
my $id = $1;
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 3 , key => "log_0060", variables => { user => $database_user, id => $id }});
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 2 , key => "log_0060", variables => { user => $database_user, id => $id }});
$create_user = 0;
$create_user = 0;
last;
last;
}
}
}
}
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3 , list => { create_user => $create_user }});
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2 , list => { create_user => $create_user }});
if ($create_user)
if ($create_user)
{
{
# Create the user
# Create the user
@ -279,13 +281,13 @@ if ($local_uuid)
}
}
# Update/set the passwords.
# Update/set the passwords.
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3 , secure => 1, list => { "database::${local_uuid}::password" => $anvil->data->{database}{$local_uuid}{password} }});
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2 , secure => 1, list => { "database::${local_uuid}::password" => $anvil->data->{database}{$local_uuid}{password} }});
if ($anvil->data->{database}{$local_uuid}{password})
if ($anvil->data->{database}{$local_uuid}{password})
{
{
foreach my $user ("postgres", $database_user)
foreach my $user ("postgres", $database_user)
{
{
my ($update_output, $return_code) = $anvil->System->call({secure => 1, shell_call => $anvil->data->{path}{exe}{su}." - postgres -c \"".$anvil->data->{path}{exe}{psql}." template1 -c \\\"ALTER ROLE $user WITH PASSWORD '".$anvil->data->{database}{$local_uuid}{password}."';\\\"\"", source => $THIS_FILE, line => __LINE__});
my ($update_output, $return_code) = $anvil->System->call({secure => 1, shell_call => $anvil->data->{path}{exe}{su}." - postgres -c \"".$anvil->data->{path}{exe}{psql}." template1 -c \\\"ALTER ROLE $user WITH PASSWORD '".$anvil->data->{database}{$local_uuid}{password}."';\\\"\"", source => $THIS_FILE, line => __LINE__});
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3 , secure => 1, list => { update_output => $update_output, return_code => $return_code }});
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2 , secure => 1, list => { update_output => $update_output, return_code => $return_code }});
foreach my $line (split/\n/, $user_list)
foreach my $line (split/\n/, $user_list)
{
{
if ($line =~ /ALTER ROLE/)
if ($line =~ /ALTER ROLE/)
@ -301,29 +303,31 @@ if ($local_uuid)
# Create the database, if needed.
# Create the database, if needed.
my $create_database = 1;
my $create_database = 1;
my $database_name = $anvil->data->{database}{$local_uuid}{name} ? $anvil->data->{database}{$local_uuid}{name} : $anvil->data->{sys}{database}{name};
my $database_name = $anvil->data->{database}{$local_uuid}{name} ? $anvil->data->{database}{$local_uuid}{name} : $anvil->data->{sys}{database}{name};
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3 , list => { database_name => $database_name }});
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2 , list => { database_name => $database_name }});
my ($database_list, $return_code) = $anvil->System->call({shell_call => $anvil->data->{path}{exe}{su}." - postgres -c \"".$anvil->data->{path}{exe}{psql}." template1 -c 'SELECT datname FROM pg_catalog.pg_database;'\"", source => $THIS_FILE, line => __LINE__});
undef $return_code;
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => { database_list => $database_list, return_code => $return_code }});
(my $database_list, $return_code) = $anvil->System->call({shell_call => $anvil->data->{path}{exe}{su}." - postgres -c \"".$anvil->data->{path}{exe}{psql}." template1 -c 'SELECT datname FROM pg_catalog.pg_database;'\"", source => $THIS_FILE, line => __LINE__});
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { database_list => $database_list, return_code => $return_code }});
foreach my $line (split/\n/, $database_list)
foreach my $line (split/\n/, $database_list)
{
{
if ($line =~ /^ $database_name$/)
if ($line =~ /^ $database_name$/)
{
{
# Database already exists.
# Database already exists.
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 3 , key => "log_0105", variables => { database => $database_name }});
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 2 , key => "log_0105", variables => { database => $database_name }});
$create_database = 0;
$create_database = 0;
last;
last;
}
}
}
}
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3 , list => { create_database => $create_database }});
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2 , list => { create_database => $create_database }});
if ($create_database)
if ($create_database)
{
{
my ($create_output, $return_code) = $anvil->System->call({shell_call => $anvil->data->{path}{exe}{su}." - postgres -c \"".$anvil->data->{path}{exe}{createdb}." --owner ".$database_user." ".$database_name."\"", source => $THIS_FILE, line => __LINE__});
my ($create_output, $return_code) = $anvil->System->call({shell_call => $anvil->data->{path}{exe}{su}." - postgres -c \"".$anvil->data->{path}{exe}{createdb}." --owner ".$database_user." ".$database_name."\"", source => $THIS_FILE, line => __LINE__});
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { create_output => $create_output, return_code => $return_code }});
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { create_output => $create_output, return_code => $return_code }});
undef $return_code;
my $database_exists = 0;
my $database_exists = 0;
my ( $database_list, $return_code) = $anvil->System->call({shell_call => $anvil->data->{path}{exe}{su}." - postgres -c \"".$anvil->data->{path}{exe}{psql}." template1 -c 'SELECT datname FROM pg_catalog.pg_database;'\"", source => $THIS_FILE, line => __LINE__});
( my $database_list, $return_code) = $anvil->System->call({shell_call => $anvil->data->{path}{exe}{su}." - postgres -c \"".$anvil->data->{path}{exe}{psql}." template1 -c 'SELECT datname FROM pg_catalog.pg_database;'\"", source => $THIS_FILE, line => __LINE__});
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3 , list => { database_list => $database_list, return_code => $return_code }});
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2 , list => { database_list => $database_list, return_code => $return_code }});
foreach my $line (split/\n/, $database_list)
foreach my $line (split/\n/, $database_list)
{
{
if ($line =~ /^ $database_name$/)
if ($line =~ /^ $database_name$/)