* Fixed a bug where '$anvil->' wasn't referencing '$anvil->data->' when it should have been.

* Got anvil-manage-striker-peers working properly (so far).
* Updated anvil-prep-database to call anvil-manage-striker-peers, but testing still needed.

Signed-off-by: Digimer <digimer@alteeve.ca>
main
Digimer 6 years ago
parent 1106d36e03
commit 252fbf21ef
  1. 175
      Anvil/Tools.pm
  2. 8
      Anvil/Tools/Log.pm
  3. 6
      Anvil/Tools/Storage.pm
  4. 2
      share/words.xml
  5. 107
      tools/anvil-manage-striker-peers
  6. 92
      tools/anvil-prep-database

@ -331,16 +331,16 @@ sub environment
# Pick up the passed in delimiter, if any. # Pick up the passed in delimiter, if any.
if ($_[0]) if ($_[0])
{ {
$anvil->{ENV_VALUES}{ENVIRONMENT} = shift; $anvil->data->{ENV_VALUES}{ENVIRONMENT} = shift;
# Load the CGI stuff if we're in a browser # Load the CGI stuff if we're in a browser
if ($anvil->{ENV_VALUES}{ENVIRONMENT} eq "html") if ($anvil->data->{ENV_VALUES}{ENVIRONMENT} eq "html")
{ {
CGI::Carp->import(qw(fatalsToBrowser)); CGI::Carp->import(qw(fatalsToBrowser));
} }
} }
return ($anvil->{ENV_VALUES}{ENVIRONMENT}); return ($anvil->data->{ENV_VALUES}{ENVIRONMENT});
} }
=head2 nice_exit =head2 nice_exit
@ -376,10 +376,10 @@ sub nice_exit
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 1, key => "log_0135", variables => { runtime => $run_time }}); $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 1, key => "log_0135", variables => { runtime => $run_time }});
# Close the log file. # Close the log file.
if ($anvil->{HANDLE}{log_file}) if ($anvil->data->{HANDLE}{log_file})
{ {
close $anvil->{HANDLE}{log_file}; close $anvil->data->{HANDLE}{log_file};
$anvil->{HANDLE}{log_file} = ""; $anvil->data->{HANDLE}{log_file} = "";
} }
exit($exit_code); exit($exit_code);
@ -585,13 +585,13 @@ sub _anvil_version
my $self = shift; my $self = shift;
my $anvil = $self; my $anvil = $self;
if ($anvil->{HOST}{ANVIL_VERSION} eq "") if ($anvil->data->{HOST}{ANVIL_VERSION} eq "")
{ {
# Try to read the local Anvil! version. # Try to read the local Anvil! version.
$anvil->{HOST}{ANVIL_VERSION} = $anvil->Get->anvil_version(); $anvil->data->{HOST}{ANVIL_VERSION} = $anvil->Get->anvil_version();
} }
return($anvil->{HOST}{ANVIL_VERSION}); return($anvil->data->{HOST}{ANVIL_VERSION});
} }
=head2 _hostname =head2 _hostname
@ -852,100 +852,101 @@ sub _set_paths
# Executables # Executables
$anvil->data->{path} = { $anvil->data->{path} = {
configs => { configs => {
'anvil.conf' => "/etc/anvil/anvil.conf", 'anvil.conf' => "/etc/anvil/anvil.conf",
'anvil.version' => "/etc/anvil/anvil.version", 'anvil.version' => "/etc/anvil/anvil.version",
'firewalld.conf' => "/etc/firewalld/firewalld.conf", 'firewalld.conf' => "/etc/firewalld/firewalld.conf",
'httpd.conf' => "/etc/httpd/conf/httpd.conf", 'httpd.conf' => "/etc/httpd/conf/httpd.conf",
'journald_anvil' => "/etc/systemd/journald.conf.d/anvil.conf", 'journald_anvil' => "/etc/systemd/journald.conf.d/anvil.conf",
'pg_hba.conf' => "/var/lib/pgsql/data/pg_hba.conf", 'pg_hba.conf' => "/var/lib/pgsql/data/pg_hba.conf",
'postgresql.conf' => "/var/lib/pgsql/data/postgresql.conf", 'postgresql.conf' => "/var/lib/pgsql/data/postgresql.conf",
ssh_config => "/etc/ssh/ssh_config", ssh_config => "/etc/ssh/ssh_config",
}, },
data => { data => {
group => "/etc/group", group => "/etc/group",
'.htpasswd' => "/etc/httpd/.htpasswd", '.htpasswd' => "/etc/httpd/.htpasswd",
host_uuid => "/etc/anvil/host.uuid", host_uuid => "/etc/anvil/host.uuid",
passwd => "/etc/passwd", passwd => "/etc/passwd",
}, },
directories => { directories => {
backups => "/root/anvil-backups", backups => "/root/anvil-backups",
'cgi-bin' => "/var/www/cgi-bin", 'cgi-bin' => "/var/www/cgi-bin",
firewalld_services => "/usr/lib/firewalld/services", firewalld_services => "/usr/lib/firewalld/services",
firewalld_zones => "/etc/firewalld/zones", firewalld_zones => "/etc/firewalld/zones",
html => "/var/www/html", html => "/var/www/html",
ifcfg => "/etc/sysconfig/network-scripts", ifcfg => "/etc/sysconfig/network-scripts",
skins => "/var/www/html/skins", skins => "/var/www/html/skins",
tools => "/usr/sbin", tools => "/usr/sbin",
units => "/usr/lib/systemd/system", units => "/usr/lib/systemd/system",
}, },
exe => { exe => {
'anvil-change-password' => "/usr/sbin/anvil-change-password", 'anvil-change-password' => "/usr/sbin/anvil-change-password",
'anvil-prep-database' => "/usr/sbin/anvil-prep-database", 'anvil-manage-striker-peers' => "/usr/sbin/anvil-manage-striker-peers",
'anvil-update-states' => "/usr/sbin/anvil-update-states", 'anvil-prep-database' => "/usr/sbin/anvil-prep-database",
'anvil-report-memory' => "/usr/sbin/anvil-report-memory", 'anvil-update-states' => "/usr/sbin/anvil-update-states",
'chmod' => "/usr/bin/chmod", 'anvil-report-memory' => "/usr/sbin/anvil-report-memory",
'chown' => "/usr/bin/chown", 'chmod' => "/usr/bin/chmod",
cp => "/usr/bin/cp", 'chown' => "/usr/bin/chown",
createdb => "/usr/bin/createdb", cp => "/usr/bin/cp",
createuser => "/usr/bin/createuser", createdb => "/usr/bin/createdb",
dmidecode => "/usr/sbin/dmidecode", createuser => "/usr/bin/createuser",
echo => "/usr/bin/echo", dmidecode => "/usr/sbin/dmidecode",
ethtool => "/usr/sbin/ethtool", echo => "/usr/bin/echo",
expect => "/usr/bin/expect", ethtool => "/usr/sbin/ethtool",
'firewall-cmd' => "/usr/bin/firewall-cmd", expect => "/usr/bin/expect",
gethostip => "/usr/bin/gethostip", 'firewall-cmd' => "/usr/bin/firewall-cmd",
'grep' => "/usr/bin/grep", gethostip => "/usr/bin/gethostip",
head => "/usr/bin/head", 'grep' => "/usr/bin/grep",
hostname => "/usr/bin/hostname", head => "/usr/bin/head",
hostnamectl => "/usr/bin/hostnamectl", hostname => "/usr/bin/hostname",
htpasswd => "/usr/bin/htpasswd", hostnamectl => "/usr/bin/hostnamectl",
ifdown => "/sbin/ifdown", htpasswd => "/usr/bin/htpasswd",
ifup => "/sbin/ifup", ifdown => "/sbin/ifdown",
ip => "/usr/sbin/ip", ifup => "/sbin/ifup",
'iptables-save' => "/usr/sbin/iptables-save", ip => "/usr/sbin/ip",
journalctl => "/usr/bin/journalctl", 'iptables-save' => "/usr/sbin/iptables-save",
logger => "/usr/bin/logger", journalctl => "/usr/bin/journalctl",
md5sum => "/usr/bin/md5sum", logger => "/usr/bin/logger",
'mkdir' => "/usr/bin/mkdir", md5sum => "/usr/bin/md5sum",
nmcli => "/bin/nmcli", 'mkdir' => "/usr/bin/mkdir",
openssl => "/usr/bin/openssl", nmcli => "/bin/nmcli",
passwd => "/usr/bin/passwd", openssl => "/usr/bin/openssl",
ping => "/usr/bin/ping", passwd => "/usr/bin/passwd",
pgrep => "/usr/bin/pgrep", ping => "/usr/bin/ping",
ps => "/usr/bin/ps", pgrep => "/usr/bin/pgrep",
psql => "/usr/bin/psql", ps => "/usr/bin/ps",
'postgresql-setup' => "/usr/bin/postgresql-setup", psql => "/usr/bin/psql",
pwd => "/usr/bin/pwd", 'postgresql-setup' => "/usr/bin/postgresql-setup",
rsync => "/usr/bin/rsync", pwd => "/usr/bin/pwd",
sed => "/usr/bin/sed", rsync => "/usr/bin/rsync",
'shutdown' => "/usr/sbin/shutdown", sed => "/usr/bin/sed",
'ssh-keyscan' => "/usr/bin/ssh-keyscan", 'shutdown' => "/usr/sbin/shutdown",
strings => "/usr/bin/strings", 'ssh-keyscan' => "/usr/bin/ssh-keyscan",
stty => "/usr/bin/stty", strings => "/usr/bin/strings",
su => "/usr/bin/su", stty => "/usr/bin/stty",
systemctl => "/usr/bin/systemctl", su => "/usr/bin/su",
timeout => "/usr/bin/timeout", systemctl => "/usr/bin/systemctl",
touch => "/usr/bin/touch", timeout => "/usr/bin/timeout",
tput => "/usr/bin/tput", touch => "/usr/bin/touch",
'tr' => "/usr/bin/tr", tput => "/usr/bin/tput",
usermod => "/usr/sbin/usermod", 'tr' => "/usr/bin/tr",
uuidgen => "/usr/bin/uuidgen", usermod => "/usr/sbin/usermod",
virsh => "/usr/bin/virsh", uuidgen => "/usr/bin/uuidgen",
virsh => "/usr/bin/virsh",
}, },
'lock' => { 'lock' => {
database => "/tmp/anvil-tools.database.lock", database => "/tmp/anvil-tools.database.lock",
}, },
secure => { secure => {
postgres_pgpass => "/var/lib/pgsql/.pgpass", postgres_pgpass => "/var/lib/pgsql/.pgpass",
}, },
sql => { sql => {
'anvil.sql' => "/usr/share/anvil/anvil.sql", 'anvil.sql' => "/usr/share/anvil/anvil.sql",
}, },
urls => { urls => {
skins => "/skins", skins => "/skins",
}, },
words => { words => {
'words.xml' => "/usr/share/anvil/words.xml", 'words.xml' => "/usr/share/anvil/words.xml",
}, },
}; };

@ -310,7 +310,7 @@ sub entry
} }
# Open the file? # Open the file?
if (not $anvil->{HANDLE}{log_file}) if (not $anvil->data->{HANDLE}{log_file})
{ {
# If the file doesn't start with a '/', we'll put it under /var/log. # If the file doesn't start with a '/', we'll put it under /var/log.
my $log_file = $anvil->data->{sys}{log_file} =~ /^\// ? $anvil->data->{sys}{log_file} : "/var/log/".$anvil->data->{sys}{log_file}; my $log_file = $anvil->data->{sys}{log_file} =~ /^\// ? $anvil->data->{sys}{log_file} : "/var/log/".$anvil->data->{sys}{log_file};
@ -326,20 +326,20 @@ sub entry
# NOTE: Don't call '$anvil->Log->entry()' here, it will cause a loop! # NOTE: Don't call '$anvil->Log->entry()' here, it will cause a loop!
open (my $file_handle, ">>", $shell_call) or die "Failed to open: [$shell_call] for writing. The error was: $!\n"; open (my $file_handle, ">>", $shell_call) or die "Failed to open: [$shell_call] for writing. The error was: $!\n";
$file_handle->autoflush(1); $file_handle->autoflush(1);
$anvil->{HANDLE}{log_file} = $file_handle; $anvil->data->{HANDLE}{log_file} = $file_handle;
# Make sure it can be written to by apache. # Make sure it can be written to by apache.
$anvil->Storage->change_mode({target => $log_file, mode => "0666"}); $anvil->Storage->change_mode({target => $log_file, mode => "0666"});
} }
if (not $anvil->{HANDLE}{log_file}) if (not $anvil->data->{HANDLE}{log_file})
{ {
# NOTE: This can't be a normal error because we can't write to the logs. # NOTE: This can't be a normal error because we can't write to the logs.
die $THIS_FILE." ".__LINE__."; log file handle doesn't exist, but it should by now.\n"; die $THIS_FILE." ".__LINE__."; log file handle doesn't exist, but it should by now.\n";
} }
# The handle has to be wrapped in a block to make 'print' happy as it doesn't like non-scalars for file handles # The handle has to be wrapped in a block to make 'print' happy as it doesn't like non-scalars for file handles
print { $anvil->{HANDLE}{log_file} } $string; print { $anvil->data->{HANDLE}{log_file} } $string;
} }
else else
{ {

@ -1099,7 +1099,7 @@ sub read_config
{ {
# Find the file, if possible. If not found, we'll not alter what the user passed in and hope # Find the file, if possible. If not found, we'll not alter what the user passed in and hope
# it is relative to where we are. # it is relative to where we are.
my $path = $anvil->Storage->find({ file => $file }); my $path = $anvil->Storage->find({debug => $debug, file => $file});
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { path => $path }}); $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { path => $path }});
if ($path ne "#!not_found!#") if ($path ne "#!not_found!#")
{ {
@ -1131,11 +1131,11 @@ sub read_config
{ {
# Read it in! # Read it in!
my $count = 0; my $count = 0;
my $body = $anvil->Storage->read_file({file => $file}); my $body = $anvil->Storage->read_file({force_read => 1, debug => $debug, file => $file});
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { body => $body }}); $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { body => $body }});
foreach my $line (split/\n/, $body) foreach my $line (split/\n/, $body)
{ {
$line = $anvil->Words->clean_spaces({ string => $line }); $line = $anvil->Words->clean_spaces({string => $line});
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { line => $line }}); $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { line => $line }});
$count++; $count++;

@ -57,7 +57,7 @@ NOTE: You must update the password of any other system using this host's
</key> </key>
<key name="message_0030">Failed to write the new password to the temporary file: [#!variable!file!#]. Please check the logs for details.</key> <key name="message_0030">Failed to write the new password to the temporary file: [#!variable!file!#]. Please check the logs for details.</key>
<key name="message_0031">Beginning configuration of local system.</key> <key name="message_0031">Beginning configuration of local system.</key>
<key name="message_0032">Peer: [#!variable!peer!#], database: [#!variable!name!#]</key> <key name="message_0032">Peer: [#!variable!peer!#], database: [#!variable!name!#], UUID: [#!variable!uuid!#]</key>
<key name="message_0033"></key> <key name="message_0033"></key>
<key name="message_0034"><![CDATA[Logged in as: [<span class="fixed_width">#!data!sys::users::user_name!#</span>]]]></key> <key name="message_0034"><![CDATA[Logged in as: [<span class="fixed_width">#!data!sys::users::user_name!#</span>]]]></key>
<key name="message_0035"></key> <key name="message_0035"></key>

@ -10,6 +10,16 @@
# #
# Calling this with --add, will insert an entry if it's not found. Calling it with no switch will update the # Calling this with --add, will insert an entry if it's not found. Calling it with no switch will update the
# entry if it exists. Calling it with --remove will delete it. # entry if it exists. Calling it with --remove will delete it.
#
### Show existing entries
# /usr/sbin/anvil-manage-striker-peers --list
### Add a new entry, or edit an existing one
# /usr/sbin/anvil-manage-striker-peers --add --host-uuid e20c3f10-c35d-4543-b5e6-8a373f27977a --host localhost --port 5432 --password-file /tmp/anvil-manage-striker-peers.2e410b43-42a0-4eaf-985c-670f92c482b8 --ping 0
### Edit an existing entry, but don't add it if it wasn't found.
# /usr/sbin/anvil-manage-striker-peers --host-uuid e20c3f10-c35d-4543-b5e6-8a373f27977a --host localhost --port 5432 --password-file /tmp/anvil-manage-striker-peers.2e410b43-42a0-4eaf-985c-670f92c482b8 --ping 0
### Remove an entry
# /usr/sbin/anvil-manage-striker-peers --remove --host-uuid e20c3f10-c35d-4543-b5e6-8a373f27977a
use strict; use strict;
use warnings; use warnings;
@ -29,6 +39,7 @@ my $anvil = Anvil::Tools->new({log_level => 2, log_secure => 1});
# Read switches # Read switches
$anvil->data->{switches}{list} = ""; $anvil->data->{switches}{list} = "";
$anvil->data->{switches}{add} = 0;
$anvil->Get->switches; $anvil->Get->switches;
# Make sure we're running as 'root' # Make sure we're running as 'root'
@ -43,22 +54,10 @@ if (($< != 0) && ($> != 0))
# Paths # Paths
$anvil->Storage->read_config({file => $anvil->data->{path}{configs}{'anvil.conf'}}); $anvil->Storage->read_config({file => $anvil->data->{path}{configs}{'anvil.conf'}});
# Read in the anvil.conf, we're going to need it in any case.
$anvil->data->{body}{'anvil.conf'} = $anvil->Storage->read_file({file => $anvil->data->{path}{configs}{'anvil.conf'}});
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, secure => 1, level => 3, list => { "body::anvil.conf" => $anvil->data->{body}{'anvil.conf'} }});
# If we don't find the entry, or if the entry exists but has changed, this will be set to '1' so we'll
# rewrite the file.
$anvil->data->{config}{rewrite} = 0;
# Am I adding, editing or deleting? # Am I adding, editing or deleting?
process_entry($anvil) if not $anvil->data->{switches}{list}; if (not $anvil->data->{switches}{list})
# Re-read the (new) config
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { "config::rewrite" => $anvil->data->{config}{rewrite} }});
if ($anvil->data->{config}{rewrite})
{ {
$anvil->Storage->read_config({file => $anvil->data->{path}{configs}{'anvil.conf'}}); process_entry($anvil) ;
} }
### Report the peers. ### Report the peers.
@ -83,6 +82,7 @@ foreach my $host (sort {$a cmp $b} keys %{$anvil->data->{sorted}{db}})
print $anvil->Words->string({key => "message_0032", variables => { print $anvil->Words->string({key => "message_0032", variables => {
peer => $user."\@".$host.":".$port, peer => $user."\@".$host.":".$port,
name => $name, name => $name,
uuid => $uuid,
}})."\n"; }})."\n";
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 1, secure => 0, key => "log_0190", variables => { $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 1, secure => 0, key => "log_0190", variables => {
peer => $user."\@".$host.":".$port, peer => $user."\@".$host.":".$port,
@ -115,6 +115,14 @@ sub process_entry
password_file => $password_file, password_file => $password_file,
ping => $ping, ping => $ping,
}}); }});
# Read in the anvil.conf, we're going to need it in any case.
$anvil->data->{body}{'anvil.conf'} = $anvil->Storage->read_file({file => $anvil->data->{path}{configs}{'anvil.conf'}});
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, secure => 1, level => 3, list => { "body::anvil.conf" => $anvil->data->{body}{'anvil.conf'} }});
# If we don't find the entry, or if the entry exists but has changed, this will be set to '1' so we'll
# rewrite the file.
$anvil->data->{config}{rewrite} = 0;
# Is anything missing? # Is anything missing?
if ((not $host_uuid) or (not $anvil->Validate->is_uuid({uuid => $host_uuid}))) if ((not $host_uuid) or (not $anvil->Validate->is_uuid({uuid => $host_uuid})))
@ -124,11 +132,11 @@ sub process_entry
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 2, secure => 0, key => "error_0031", variables => { host_uuid => $host_uuid }}); $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 2, secure => 0, key => "error_0031", variables => { host_uuid => $host_uuid }});
$anvil->nice_exit({code => 2}); $anvil->nice_exit({code => 2});
} }
if (not $host) if ((not $host) && (not $anvil->data->{switches}{remove}))
{ {
# Invalid UUID. # Invalid UUID.
print $anvil->Words->string({key => "error_0032", variables => { switch => "host" }})."\n"; print $anvil->Words->string({key => "error_0032", variables => { switch => "host" }})."\n";
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 2, secure => 0, key => "error_0032", variables => { host_uuid => $host_uuid }}); $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 2, secure => 0, key => "error_0032", variables => { host => $host }});
$anvil->nice_exit({code => 2}); $anvil->nice_exit({code => 2});
} }
if (($port =~ /\D/) or ($port < 1) or ($port > 65535)) if (($port =~ /\D/) or ($port < 1) or ($port > 65535))
@ -177,18 +185,44 @@ sub process_entry
# Secure password lines. # Secure password lines.
my $secure = (($line =~ /password/) && ($line !~ /^#/)) ? 1 : 0; my $secure = (($line =~ /password/) && ($line !~ /^#/)) ? 1 : 0;
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, secure => $secure, level => 2, list => { line => $line }}); $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, secure => $secure, level => 3, list => { line => $line }});
if ($line =~ /^(.*)(\s*)=(\s*)(.*)$/)
# If we've hit the end of the DB list, see if we need to insert a new entry.
if ($line eq "### end db list ###")
{
# If I've not seen this DB, enter it.
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, secure => 0, level => 2, list => {
peer_seen => $peer_seen,
"switches::add" => $anvil->data->{switches}{add},
}});
if ((not $peer_seen) && ($anvil->data->{switches}{add}))
{
$new_body .= $insert."\n";
$anvil->data->{config}{rewrite} = 1;
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, secure => 1, level => 2, list => {
new_body => $new_body,
"config::rewrite" => $anvil->data->{config}{rewrite},
}});
}
}
# Skip comments.
if ($line =~ /^#/)
{
$new_body .= $line."\n";
next;
}
if ($line =~ /^(.*?)(\s*)=(\s*)(.*)$/)
{ {
my $variable = $1; my $variable = $1;
my $left_space = $2; my $left_space = $2;
my $right_space = $3; my $right_space = $3;
my $value = $4; my $value = $4;
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => {
variable => $variable, "s1:variable" => $variable,
left_space => $left_space, "s2:value" => $value,
right_space => $right_space, "s3:left_space" => $left_space,
value => $value, "s4:right_space" => $right_space,
}}); }});
if ($variable eq $host_variable) if ($variable eq $host_variable)
@ -204,7 +238,7 @@ sub process_entry
$just_deleted = 1; $just_deleted = 1;
$anvil->data->{config}{rewrite} = 1; $anvil->data->{config}{rewrite} = 1;
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => {
just_deleted => $just_deleted. just_deleted => $just_deleted,
"config::rewrite" => $anvil->data->{config}{rewrite}, "config::rewrite" => $anvil->data->{config}{rewrite},
}}); }});
next; next;
@ -238,7 +272,7 @@ sub process_entry
$just_deleted = 1; $just_deleted = 1;
$anvil->data->{config}{rewrite} = 1; $anvil->data->{config}{rewrite} = 1;
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => {
just_deleted => $just_deleted. just_deleted => $just_deleted,
"config::rewrite" => $anvil->data->{config}{rewrite}, "config::rewrite" => $anvil->data->{config}{rewrite},
}}); }});
next; next;
@ -276,7 +310,7 @@ sub process_entry
$just_deleted = 1; $just_deleted = 1;
$anvil->data->{config}{rewrite} = 1; $anvil->data->{config}{rewrite} = 1;
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => {
just_deleted => $just_deleted. just_deleted => $just_deleted,
"config::rewrite" => $anvil->data->{config}{rewrite}, "config::rewrite" => $anvil->data->{config}{rewrite},
}}); }});
next; next;
@ -310,7 +344,7 @@ sub process_entry
$just_deleted = 1; $just_deleted = 1;
$anvil->data->{config}{rewrite} = 1; $anvil->data->{config}{rewrite} = 1;
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => {
just_deleted => $just_deleted. just_deleted => $just_deleted,
"config::rewrite" => $anvil->data->{config}{rewrite}, "config::rewrite" => $anvil->data->{config}{rewrite},
}}); }});
next; next;
@ -332,19 +366,6 @@ sub process_entry
} }
} }
} }
if ($line eq "### end db list ###")
{
# If I've not seen this DB, enter it.
if ((not $peer_seen) && ($anvil->data->{switches}{add}))
{
$new_body .= $insert."\n";
$anvil->data->{config}{rewrite} = 1;
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, secure => 1, level => 2, list => {
new_body => $new_body,
"config::rewrite" => $anvil->data->{config}{rewrite},
}});
}
}
$new_body .= $line."\n"; $new_body .= $line."\n";
} }
@ -358,7 +379,6 @@ sub process_entry
# Now update! # Now update!
$anvil->Storage->write_file({ $anvil->Storage->write_file({
secure => 1, secure => 1,
debug => 2,
file => $anvil->data->{path}{configs}{'anvil.conf'}, file => $anvil->data->{path}{configs}{'anvil.conf'},
body => $new_body, body => $new_body,
user => "admin", user => "admin",
@ -366,6 +386,13 @@ sub process_entry
mode => "0644", mode => "0644",
overwrite => 1, overwrite => 1,
}); });
# Delete any databases I new about, the we'll reload from the config.
delete $anvil->data->{database};
# Re-read the config.
sleep 1;
$anvil->Storage->read_config({file => $anvil->data->{path}{configs}{'anvil.conf'}});
} }
return(0); return(0);

@ -31,7 +31,7 @@ my $anvil = Anvil::Tools->new({log_level => 2, log_secure => 0});
# Read switches # Read switches
$anvil->Get->switches; $anvil->Get->switches;
# Paths # Read in our config.
$anvil->Storage->read_config({file => $anvil->data->{path}{configs}{'anvil.conf'}}); $anvil->Storage->read_config({file => $anvil->data->{path}{configs}{'anvil.conf'}});
my $local_uuid = $anvil->Database->get_local_uuid(); my $local_uuid = $anvil->Database->get_local_uuid();
@ -42,13 +42,6 @@ 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 => 2, list => { local_uuid => $local_uuid }}); $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { local_uuid => $local_uuid }});
# If we still don't have a local_uuid, something went wrong.
if (not $local_uuid)
{
print $anvil->Words->string({key => "error_0010"})."\n";
$anvil->nice_exit({code => 1});
}
} }
# Now configure! # Now configure!
@ -439,74 +432,31 @@ sub add_to_local_config
mode => "0600", mode => "0600",
overwrite => 1, overwrite => 1,
}); });
my $shell_call = $anvil->{path}{exe}{'anvil-manage-striker-peers'}." --host-uuid ".$anvil->Get->host_uuid." --host localhost --port 5432 --password-file ".$password_file." --ping 0";
my $host_uuid = $anvil->Get->host_uuid(); # Make the shell call, and parse the output looking for our own entry
my $local_uuid = ""; my $host_uuid = $anvil->Get->host_uuid();
my $anvil_conf_body = $anvil->Storage->read_file({file => $anvil->data->{path}{configs}{'anvil.conf'}}); my $output = $anvil->System->call({
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, secure => 1, level => 2, list => { debug => 2,
host_uuid => $host_uuid, shell_call => $anvil->data->{path}{exe}{'anvil-manage-striker-peers'}." --add --host-uuid ".$anvil->Get->host_uuid." --host localhost --port 5432 --password-file ".$password_file." --ping 0",
anvil_conf_body => $anvil_conf_body, source => $THIS_FILE,
}}); line => __LINE__,
});
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { output => $output }});
# Setup the data we'll insert. The password will be changed after the user does the initial config. # Remove the password.
my $insert = "database::".$host_uuid."::host = ".$anvil->data->{database}{$host_uuid}{host}."\n"; unlink $password_file;
$insert .= "database::".$host_uuid."::port = ".$anvil->data->{database}{$host_uuid}{port}."\n";
$insert .= "database::".$host_uuid."::password = ".$anvil->data->{database}{$host_uuid}{password}."\n"; # Re-read the config and make sure we have our own entry.
$insert .= "database::".$host_uuid."::ping = ".$anvil->data->{database}{$host_uuid}{ping}."\n\n"; sleep 1;
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, secure => 1, level => 2, list => { insert => $insert }}); $anvil->Storage->read_config({file => $anvil->data->{path}{configs}{'anvil.conf'}});
# Now inject the config.
my $new_body = "";
my $config_seen = 0;
my $test_line = "database::${host_uuid}::";
foreach my $line (split/\n/, $anvil_conf_body)
{
my $secure = (($line =~ /password/) && ($line !~ /^#/)) ? 1 : 0;
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, secure => $secure, level => 2, list => { line => $line }});
if ($line =~ /^$test_line/)
{
# It's already configured, abort.
$config_seen = 1;
$local_uuid = $host_uuid;
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => {
config_seen => $config_seen,
local_uuid => $local_uuid,
}});
}
if ($line eq "### end db list ###")
{
$new_body .= $insert;
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, secure => 1, level => 2, list => { new_body => $new_body }});
}
$new_body .= $line."\n";
}
# If we're here, we're ready to write it out. # If we still don't have a local_uuid, something went wrong.
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { config_seen => $config_seen }}); if (not $anvil->data->{database}{$host_uuid}{host})
if (not $config_seen)
{ {
# Backup the original print $anvil->Words->string({key => "error_0010"})."\n";
my $backup_file = $anvil->Storage->backup({secure => 1, file => $anvil->data->{path}{configs}{'anvil.conf'}}); $anvil->nice_exit({code => 1});
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { backup_file => $backup_file }});
# Now update!
$anvil->Storage->write_file({
secure => 1,
debug => 2,
file => $anvil->data->{path}{configs}{'anvil.conf'},
body => $new_body,
user => "admin",
group => "admin",
mode => "0644",
overwrite => 1,
});
# Record the local UUID for returning to the caller.
$local_uuid = $host_uuid;
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { local_uuid => $local_uuid }});
} }
return($local_uuid); return($host_uuid);
} }

Loading…
Cancel
Save