From 252fbf21ef9c9ff723dd1755486ba90609410bae Mon Sep 17 00:00:00 2001 From: Digimer Date: Mon, 13 Aug 2018 02:22:46 -0400 Subject: [PATCH] * 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 --- Anvil/Tools.pm | 175 ++++++++++++++++--------------- Anvil/Tools/Log.pm | 8 +- Anvil/Tools/Storage.pm | 6 +- share/words.xml | 2 +- tools/anvil-manage-striker-peers | 107 ++++++++++++------- tools/anvil-prep-database | 92 ++++------------ 6 files changed, 184 insertions(+), 206 deletions(-) diff --git a/Anvil/Tools.pm b/Anvil/Tools.pm index 38dc9df3..a23c1fd4 100755 --- a/Anvil/Tools.pm +++ b/Anvil/Tools.pm @@ -331,16 +331,16 @@ sub environment # Pick up the passed in delimiter, if any. if ($_[0]) { - $anvil->{ENV_VALUES}{ENVIRONMENT} = shift; + $anvil->data->{ENV_VALUES}{ENVIRONMENT} = shift; # 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)); } } - return ($anvil->{ENV_VALUES}{ENVIRONMENT}); + return ($anvil->data->{ENV_VALUES}{ENVIRONMENT}); } =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 }}); # Close the log file. - if ($anvil->{HANDLE}{log_file}) + if ($anvil->data->{HANDLE}{log_file}) { - close $anvil->{HANDLE}{log_file}; - $anvil->{HANDLE}{log_file} = ""; + close $anvil->data->{HANDLE}{log_file}; + $anvil->data->{HANDLE}{log_file} = ""; } exit($exit_code); @@ -585,13 +585,13 @@ sub _anvil_version my $self = shift; my $anvil = $self; - if ($anvil->{HOST}{ANVIL_VERSION} eq "") + if ($anvil->data->{HOST}{ANVIL_VERSION} eq "") { # 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 @@ -852,100 +852,101 @@ sub _set_paths # Executables $anvil->data->{path} = { configs => { - 'anvil.conf' => "/etc/anvil/anvil.conf", - 'anvil.version' => "/etc/anvil/anvil.version", - 'firewalld.conf' => "/etc/firewalld/firewalld.conf", - 'httpd.conf' => "/etc/httpd/conf/httpd.conf", - 'journald_anvil' => "/etc/systemd/journald.conf.d/anvil.conf", - 'pg_hba.conf' => "/var/lib/pgsql/data/pg_hba.conf", - 'postgresql.conf' => "/var/lib/pgsql/data/postgresql.conf", - ssh_config => "/etc/ssh/ssh_config", + 'anvil.conf' => "/etc/anvil/anvil.conf", + 'anvil.version' => "/etc/anvil/anvil.version", + 'firewalld.conf' => "/etc/firewalld/firewalld.conf", + 'httpd.conf' => "/etc/httpd/conf/httpd.conf", + 'journald_anvil' => "/etc/systemd/journald.conf.d/anvil.conf", + 'pg_hba.conf' => "/var/lib/pgsql/data/pg_hba.conf", + 'postgresql.conf' => "/var/lib/pgsql/data/postgresql.conf", + ssh_config => "/etc/ssh/ssh_config", }, data => { - group => "/etc/group", - '.htpasswd' => "/etc/httpd/.htpasswd", - host_uuid => "/etc/anvil/host.uuid", - passwd => "/etc/passwd", + group => "/etc/group", + '.htpasswd' => "/etc/httpd/.htpasswd", + host_uuid => "/etc/anvil/host.uuid", + passwd => "/etc/passwd", }, directories => { - backups => "/root/anvil-backups", - 'cgi-bin' => "/var/www/cgi-bin", - firewalld_services => "/usr/lib/firewalld/services", - firewalld_zones => "/etc/firewalld/zones", - html => "/var/www/html", - ifcfg => "/etc/sysconfig/network-scripts", - skins => "/var/www/html/skins", - tools => "/usr/sbin", - units => "/usr/lib/systemd/system", + backups => "/root/anvil-backups", + 'cgi-bin' => "/var/www/cgi-bin", + firewalld_services => "/usr/lib/firewalld/services", + firewalld_zones => "/etc/firewalld/zones", + html => "/var/www/html", + ifcfg => "/etc/sysconfig/network-scripts", + skins => "/var/www/html/skins", + tools => "/usr/sbin", + units => "/usr/lib/systemd/system", }, exe => { - 'anvil-change-password' => "/usr/sbin/anvil-change-password", - 'anvil-prep-database' => "/usr/sbin/anvil-prep-database", - 'anvil-update-states' => "/usr/sbin/anvil-update-states", - 'anvil-report-memory' => "/usr/sbin/anvil-report-memory", - 'chmod' => "/usr/bin/chmod", - 'chown' => "/usr/bin/chown", - cp => "/usr/bin/cp", - createdb => "/usr/bin/createdb", - createuser => "/usr/bin/createuser", - dmidecode => "/usr/sbin/dmidecode", - echo => "/usr/bin/echo", - ethtool => "/usr/sbin/ethtool", - expect => "/usr/bin/expect", - 'firewall-cmd' => "/usr/bin/firewall-cmd", - gethostip => "/usr/bin/gethostip", - 'grep' => "/usr/bin/grep", - head => "/usr/bin/head", - hostname => "/usr/bin/hostname", - hostnamectl => "/usr/bin/hostnamectl", - htpasswd => "/usr/bin/htpasswd", - ifdown => "/sbin/ifdown", - ifup => "/sbin/ifup", - ip => "/usr/sbin/ip", - 'iptables-save' => "/usr/sbin/iptables-save", - journalctl => "/usr/bin/journalctl", - logger => "/usr/bin/logger", - md5sum => "/usr/bin/md5sum", - 'mkdir' => "/usr/bin/mkdir", - nmcli => "/bin/nmcli", - openssl => "/usr/bin/openssl", - passwd => "/usr/bin/passwd", - ping => "/usr/bin/ping", - pgrep => "/usr/bin/pgrep", - ps => "/usr/bin/ps", - psql => "/usr/bin/psql", - 'postgresql-setup' => "/usr/bin/postgresql-setup", - pwd => "/usr/bin/pwd", - rsync => "/usr/bin/rsync", - sed => "/usr/bin/sed", - 'shutdown' => "/usr/sbin/shutdown", - 'ssh-keyscan' => "/usr/bin/ssh-keyscan", - strings => "/usr/bin/strings", - stty => "/usr/bin/stty", - su => "/usr/bin/su", - systemctl => "/usr/bin/systemctl", - timeout => "/usr/bin/timeout", - touch => "/usr/bin/touch", - tput => "/usr/bin/tput", - 'tr' => "/usr/bin/tr", - usermod => "/usr/sbin/usermod", - uuidgen => "/usr/bin/uuidgen", - virsh => "/usr/bin/virsh", + 'anvil-change-password' => "/usr/sbin/anvil-change-password", + 'anvil-manage-striker-peers' => "/usr/sbin/anvil-manage-striker-peers", + 'anvil-prep-database' => "/usr/sbin/anvil-prep-database", + 'anvil-update-states' => "/usr/sbin/anvil-update-states", + 'anvil-report-memory' => "/usr/sbin/anvil-report-memory", + 'chmod' => "/usr/bin/chmod", + 'chown' => "/usr/bin/chown", + cp => "/usr/bin/cp", + createdb => "/usr/bin/createdb", + createuser => "/usr/bin/createuser", + dmidecode => "/usr/sbin/dmidecode", + echo => "/usr/bin/echo", + ethtool => "/usr/sbin/ethtool", + expect => "/usr/bin/expect", + 'firewall-cmd' => "/usr/bin/firewall-cmd", + gethostip => "/usr/bin/gethostip", + 'grep' => "/usr/bin/grep", + head => "/usr/bin/head", + hostname => "/usr/bin/hostname", + hostnamectl => "/usr/bin/hostnamectl", + htpasswd => "/usr/bin/htpasswd", + ifdown => "/sbin/ifdown", + ifup => "/sbin/ifup", + ip => "/usr/sbin/ip", + 'iptables-save' => "/usr/sbin/iptables-save", + journalctl => "/usr/bin/journalctl", + logger => "/usr/bin/logger", + md5sum => "/usr/bin/md5sum", + 'mkdir' => "/usr/bin/mkdir", + nmcli => "/bin/nmcli", + openssl => "/usr/bin/openssl", + passwd => "/usr/bin/passwd", + ping => "/usr/bin/ping", + pgrep => "/usr/bin/pgrep", + ps => "/usr/bin/ps", + psql => "/usr/bin/psql", + 'postgresql-setup' => "/usr/bin/postgresql-setup", + pwd => "/usr/bin/pwd", + rsync => "/usr/bin/rsync", + sed => "/usr/bin/sed", + 'shutdown' => "/usr/sbin/shutdown", + 'ssh-keyscan' => "/usr/bin/ssh-keyscan", + strings => "/usr/bin/strings", + stty => "/usr/bin/stty", + su => "/usr/bin/su", + systemctl => "/usr/bin/systemctl", + timeout => "/usr/bin/timeout", + touch => "/usr/bin/touch", + tput => "/usr/bin/tput", + 'tr' => "/usr/bin/tr", + usermod => "/usr/sbin/usermod", + uuidgen => "/usr/bin/uuidgen", + virsh => "/usr/bin/virsh", }, 'lock' => { - database => "/tmp/anvil-tools.database.lock", + database => "/tmp/anvil-tools.database.lock", }, secure => { - postgres_pgpass => "/var/lib/pgsql/.pgpass", + postgres_pgpass => "/var/lib/pgsql/.pgpass", }, sql => { - 'anvil.sql' => "/usr/share/anvil/anvil.sql", + 'anvil.sql' => "/usr/share/anvil/anvil.sql", }, urls => { - skins => "/skins", + skins => "/skins", }, words => { - 'words.xml' => "/usr/share/anvil/words.xml", + 'words.xml' => "/usr/share/anvil/words.xml", }, }; diff --git a/Anvil/Tools/Log.pm b/Anvil/Tools/Log.pm index 77138855..ef60bc02 100755 --- a/Anvil/Tools/Log.pm +++ b/Anvil/Tools/Log.pm @@ -310,7 +310,7 @@ sub entry } # 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. 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! open (my $file_handle, ">>", $shell_call) or die "Failed to open: [$shell_call] for writing. The error was: $!\n"; $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. $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. 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 - print { $anvil->{HANDLE}{log_file} } $string; + print { $anvil->data->{HANDLE}{log_file} } $string; } else { diff --git a/Anvil/Tools/Storage.pm b/Anvil/Tools/Storage.pm index bde13b23..ff258513 100755 --- a/Anvil/Tools/Storage.pm +++ b/Anvil/Tools/Storage.pm @@ -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 # 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 }}); if ($path ne "#!not_found!#") { @@ -1131,11 +1131,11 @@ sub read_config { # Read it in! 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 }}); 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 }}); $count++; diff --git a/share/words.xml b/share/words.xml index 2374146e..736d0e6c 100644 --- a/share/words.xml +++ b/share/words.xml @@ -57,7 +57,7 @@ NOTE: You must update the password of any other system using this host's Failed to write the new password to the temporary file: [#!variable!file!#]. Please check the logs for details. Beginning configuration of local system. - Peer: [#!variable!peer!#], database: [#!variable!name!#] + Peer: [#!variable!peer!#], database: [#!variable!name!#], UUID: [#!variable!uuid!#] #!data!sys::users::user_name!#]]]> diff --git a/tools/anvil-manage-striker-peers b/tools/anvil-manage-striker-peers index 1eb7244e..b6f395f4 100755 --- a/tools/anvil-manage-striker-peers +++ b/tools/anvil-manage-striker-peers @@ -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 # 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 warnings; @@ -29,6 +39,7 @@ my $anvil = Anvil::Tools->new({log_level => 2, log_secure => 1}); # Read switches $anvil->data->{switches}{list} = ""; +$anvil->data->{switches}{add} = 0; $anvil->Get->switches; # Make sure we're running as 'root' @@ -43,22 +54,10 @@ if (($< != 0) && ($> != 0)) # Paths $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? -process_entry($anvil) 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}) +if (not $anvil->data->{switches}{list}) { - $anvil->Storage->read_config({file => $anvil->data->{path}{configs}{'anvil.conf'}}); + process_entry($anvil) ; } ### 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 => { peer => $user."\@".$host.":".$port, name => $name, + uuid => $uuid, }})."\n"; $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 1, secure => 0, key => "log_0190", variables => { peer => $user."\@".$host.":".$port, @@ -115,6 +115,14 @@ sub process_entry password_file => $password_file, 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? 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->nice_exit({code => 2}); } - if (not $host) + if ((not $host) && (not $anvil->data->{switches}{remove})) { # Invalid UUID. 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}); } if (($port =~ /\D/) or ($port < 1) or ($port > 65535)) @@ -177,18 +185,44 @@ sub process_entry # Secure password lines. my $secure = (($line =~ /password/) && ($line !~ /^#/)) ? 1 : 0; - $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, secure => $secure, level => 2, list => { line => $line }}); - if ($line =~ /^(.*)(\s*)=(\s*)(.*)$/) + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, secure => $secure, level => 3, list => { line => $line }}); + + # 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 $left_space = $2; my $right_space = $3; my $value = $4; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { - variable => $variable, - left_space => $left_space, - right_space => $right_space, - value => $value, + "s1:variable" => $variable, + "s2:value" => $value, + "s3:left_space" => $left_space, + "s4:right_space" => $right_space, }}); if ($variable eq $host_variable) @@ -204,7 +238,7 @@ sub process_entry $just_deleted = 1; $anvil->data->{config}{rewrite} = 1; $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}, }}); next; @@ -238,7 +272,7 @@ sub process_entry $just_deleted = 1; $anvil->data->{config}{rewrite} = 1; $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}, }}); next; @@ -276,7 +310,7 @@ sub process_entry $just_deleted = 1; $anvil->data->{config}{rewrite} = 1; $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}, }}); next; @@ -310,7 +344,7 @@ sub process_entry $just_deleted = 1; $anvil->data->{config}{rewrite} = 1; $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}, }}); 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"; } @@ -358,7 +379,6 @@ sub process_entry # Now update! $anvil->Storage->write_file({ secure => 1, - debug => 2, file => $anvil->data->{path}{configs}{'anvil.conf'}, body => $new_body, user => "admin", @@ -366,6 +386,13 @@ sub process_entry mode => "0644", 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); diff --git a/tools/anvil-prep-database b/tools/anvil-prep-database index 8650f6eb..9b159073 100755 --- a/tools/anvil-prep-database +++ b/tools/anvil-prep-database @@ -31,7 +31,7 @@ my $anvil = Anvil::Tools->new({log_level => 2, log_secure => 0}); # Read switches $anvil->Get->switches; -# Paths +# Read in our config. $anvil->Storage->read_config({file => $anvil->data->{path}{configs}{'anvil.conf'}}); my $local_uuid = $anvil->Database->get_local_uuid(); @@ -42,13 +42,6 @@ if (not $local_uuid) { $local_uuid = add_to_local_config($anvil); $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! @@ -439,74 +432,31 @@ sub add_to_local_config mode => "0600", 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(); - my $local_uuid = ""; - my $anvil_conf_body = $anvil->Storage->read_file({file => $anvil->data->{path}{configs}{'anvil.conf'}}); - $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, secure => 1, level => 2, list => { - host_uuid => $host_uuid, - anvil_conf_body => $anvil_conf_body, - }}); + # Make the shell call, and parse the output looking for our own entry + my $host_uuid = $anvil->Get->host_uuid(); + my $output = $anvil->System->call({ + debug => 2, + 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", + 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. - my $insert = "database::".$host_uuid."::host = ".$anvil->data->{database}{$host_uuid}{host}."\n"; - $insert .= "database::".$host_uuid."::port = ".$anvil->data->{database}{$host_uuid}{port}."\n"; - $insert .= "database::".$host_uuid."::password = ".$anvil->data->{database}{$host_uuid}{password}."\n"; - $insert .= "database::".$host_uuid."::ping = ".$anvil->data->{database}{$host_uuid}{ping}."\n\n"; - $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, secure => 1, level => 2, list => { insert => $insert }}); - - # 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"; - } + # Remove the password. + unlink $password_file; + + # Re-read the config and make sure we have our own entry. + sleep 1; + $anvil->Storage->read_config({file => $anvil->data->{path}{configs}{'anvil.conf'}}); - # If we're here, we're ready to write it out. - $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { config_seen => $config_seen }}); - if (not $config_seen) + # If we still don't have a local_uuid, something went wrong. + if (not $anvil->data->{database}{$host_uuid}{host}) { - # Backup the original - my $backup_file = $anvil->Storage->backup({secure => 1, file => $anvil->data->{path}{configs}{'anvil.conf'}}); - $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 }}); + print $anvil->Words->string({key => "error_0010"})."\n"; + $anvil->nice_exit({code => 1}); } - return($local_uuid); + return($host_uuid); }