diff --git a/Anvil/Tools/Database.pm b/Anvil/Tools/Database.pm index 017f32eb..89d37fb8 100755 --- a/Anvil/Tools/Database.pm +++ b/Anvil/Tools/Database.pm @@ -2726,7 +2726,7 @@ sub insert_or_update_jobs return(""); } - # If we don't have a UUID, see if we can find one for the given job server name. + # If we don't have a UUID, see if we can find one for the given job name. if (not $job_uuid) { my $query = " @@ -2736,6 +2736,8 @@ FROM jobs WHERE job_name = ".$anvil->data->{sys}{database}{use_handle}->quote($job_name)." +AND + job_command = ".$anvil->data->{sys}{database}{use_handle}->quote($job_command)." AND job_host_uuid = ".$anvil->data->{sys}{database}{use_handle}->quote($job_host_uuid)." ;"; diff --git a/cgi-bin/striker b/cgi-bin/striker index c24c81ed..4c2e27b2 100755 --- a/cgi-bin/striker +++ b/cgi-bin/striker @@ -571,12 +571,14 @@ sub add_sync_peer # Break up the user, host and port. If anything goes wrong, we'll set an error and send it back. my $user = $anvil->data->{sys}{database}{user}; my $host = $anvil->data->{cgi}{new_peer_access}{value}; + my $password = $anvil->data->{cgi}{new_peer_password}{value}; my $name = $anvil->data->{sys}{database}{name}; - my $ping = $anvil->data->{cgi}{new_peer_ping}{value} eq "on" 1 : 0; + my $ping = $anvil->data->{cgi}{new_peer_ping}{value} eq "on" ? 1 : 0; my $port = 5432; my $ssh_tcp = 22; my $peer_uuid = ""; my $peer_host = ""; + my $use_ip = ""; # This will contain the local IP to use for the peer to setup comms with us if ($host =~ /,ssh=(\d+)$/) { $ssh_tcp = $1; @@ -637,7 +639,7 @@ sub add_sync_peer $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { shell_call => $shell_call }}); my ($error, $output) = $anvil->Remote->call({ - password => $anvil->data->{cgi}{new_peer_password}{value}, + password => $password, target => $ssh_tcp != 22 ? $host.":".$ssh_tcp : $host, shell_call => $shell_call, }); @@ -660,13 +662,28 @@ sub add_sync_peer else { my ($error, $output) = $anvil->Remote->call({ - password => $anvil->data->{cgi}{new_peer_password}{value}, + password => $password, target => $ssh_tcp != 22 ? $host.":".$ssh_tcp : $host, shell_call => $anvil->data->{path}{exe}{hostnamectl}." --static", }); $peer_host = $output->[0]; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { peer_host => $peer_host }}); + + # Lastly, if bi-directional is set, make sure we have a way to talk to it. + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { "cgi::new_peer_bidirection::value" => $anvil->data->{cgi}{new_peer_bidirection}{value} }}); + if ($anvil->data->{cgi}{new_peer_bidirection}{value} eq "on") + { + # See which of our IPs match theirs. If the peer is a hostname, first + $use_ip = $anvil->System->find_matching_ip({host => $host}); + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { use_ip => $use_ip }}); + + if ((not $use_ip) or ($use_ip eq "!!error!!")) + { + # Can't do bi-directional + $anvil->data->{form}{error_massage} = $anvil->Template->get({file => "main.html", name => "error_message", variables => { error_message => $anvil->Words->string({key => "striker_warning_0008", variables => { host => $host }}) }}); + } + } } } } @@ -675,9 +692,8 @@ sub add_sync_peer $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { "form::error_massage" => $anvil->data->{form}{error_massage} }}); if (not $anvil->data->{form}{error_massage}) { - my $pgpass_file = "/tmp/.pgpass"; - my $password = $anvil->data->{cgi}{new_peer_password}{value}; - $password =~ s/:/\:/g; + my $pgpass_file = "/tmp/.pgpass"; + $password =~ s/:/\:/g; my $body = $host.":".$port.":".$name.":".$user.":".$password; $anvil->Storage->write_file({ file => $pgpass_file, @@ -717,34 +733,46 @@ sub add_sync_peer if ($anvil->data->{cgi}{confirm}{value}) { # OK, save the job! - my $job_command = "anvil-manage-striker-peers --add "; - $job_command .= "--host-uuid ".$peer_uuid." "; - $job_command .= "--host ".$host." "; - $job_command .= "--port ".$port." "; - $job_command .= "--ping ".$ping; - $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { job_command => $job_command }}); + my $job_command = "anvil-manage-striker-peers --add --host-uuid ".$peer_uuid." --host ".$host." --port ".$port." --ping ".$ping; + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { + job_command => $job_command, + password => $anvil->Log->secure ? $password : $anvil->Words->string({key => "log_0186"}), + }}); -# my ($job_uuid) = $anvil->Database->insert_or_update_jobs({ -# file => $THIS_FILE, -# line => __LINE__, -# job_command => $job_command, -# job_data => "password=".$anvil->data->{cgi}{new_peer_password}{value}, -# job_name => "striker-peer::add", -# job_title => "job_0011", -# job_description => "job_0012", -# }); -# $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { job_uuid => $job_uuid }}); + # The job data will always contain the password for the peer, but also contain the + # command for the peer to add us if 'bidirectional' was selected. This has to be + # stored separately and not added directly as a job, because the peer is likely not + # in 'hosts' yet, and thus we can't reference it in 'job_host_uuid' at this point. + my $job_data = "password=".$password; + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, secure => 1, list => { job_data => $job_data }}); # Are we adding ourselves to the peer? - $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { "cgi::new_peer_bidirection::value" => $anvil->data->{cgi}{new_peer_bidirection}{value} }}); - if ($anvil->data->{cgi}{new_peer_bidirection}{value} eq "on") + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { use_ip => $use_ip }}); + if ($use_ip) { - my $use_ip = $anvil->System->find_matching_ip({debug => 2, host => $host}); - # See which of our IPs match theirs. If the peer is a hostname, first + # See which of our IPs match theirs. If the peer is a hostname, first + my $host_uuid = $anvil->Get->host_uuid; + my $sql_port = $anvil->data->{database}{$host_uuid}{port}; + my $job_command = "anvil-manage-striker-peers --add --host-uuid ".$host_uuid." --host ".$use_ip." --port ".$sql_port." --ping ".$ping; + $job_data .= "\npeer_job_command=".$job_command; + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, secure => 0, list => { job_command => $job_command }}); + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, secure => 1, list => { job_data => $job_data }}); } - # We don't need to store anything as hidden variables, we'll read it back from the database - # later. + # Store the job + my ($job_uuid) = $anvil->Database->insert_or_update_jobs({ + file => $THIS_FILE, + line => __LINE__, + job_command => $job_command, + job_data => $job_data, + job_name => "striker-peer::add", + job_title => "job_0011", + job_description => "job_0012", + }); + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { job_uuid => $job_uuid }}); + + # We don't need to store anything as hidden variables, we'll read it back from the + # database later. $anvil->data->{form}{body} = $anvil->Template->get({file => "striker.html", name => "system_update_recorded", variables => { title_id => "", message_id => "", @@ -752,7 +780,6 @@ sub add_sync_peer description => $ping ? "#!string!striker_0103!#" : "#!string!striker_0102!#", }}); $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => { "form::body" => $anvil->data->{form}{body} }}); - } else { diff --git a/share/words.xml b/share/words.xml index ba7987d0..5f553414 100644 --- a/share/words.xml +++ b/share/words.xml @@ -103,6 +103,17 @@ The '-y' option prevents a confirmation prompt. Rebooting the local system now. The Anvil! has restarted at: [#!variable!date_and_time!#] after powering back on. You will now be logged out and this machine will now be rebooted so that the new configuration can take effect. + Starting the job to add or update an Anvil! database peer. + Starting the job to remove an Anvil! database peer. + Sanity checkes passed. + Added the peer to the config file. + Old peer found and removed from the config file. + Existing peer found and update needed and made. + Configuration changed, existing config backed up as: [#!variable!backup!#]. + New config written to disk. + Reconnecting to the database(s) to ask the peer to add us. Will hold here until the peer is added to the 'hosts' table. Please be patient. + The peer: [#!variable!host!#] is now in the database. Proceeding. + The job the peer add us has been registered. It should add us as soon as it looks for new jobs (generally within a second or two). Starting: [#!variable!program!#]. @@ -243,7 +254,7 @@ The database connection error was: The local machine's UUID was not read properly. It should be stored in: [#!data!sys::host_uuid!#] and contain hexadecimal characters in the format: '012345-6789-abcd-ef01-23456789abcd' and usually matches the output of 'dmidecode --string system-uuid'. If this file exists and if there is a string in the file, please verify that it is structured correctly. The database with ID: [#!variable!uuid!#] for: [#!variable!file!#] is behind. Anvil! database: [#!variable!database!#] already exists. - The database with ID: [#!variable!uuid!#] is behind. A database esync will be requested. + The database with ID: [#!variable!uuid!#] is behind. A database resync will be requested. [ Warning ] - Failed to delete the temporary postgres password. insert_or_update_states() was called but the 'state_host_uuid' parameter was not passed or it is empty. Normally this is set to 'sys::data_uuid'.]]> [ Error ] - Failed to create the Anvil! database: [#!variable!database!#] @@ -349,6 +360,9 @@ The database connection error was: update_progress() called with 'progress' set to an invalid value: [#!variable!progress!#]. This must be a whole number between '0' and '100' (fractions not allowed).]]> find_matching_ip(), but it failed to resolve to an IP address.]]> + We've been asked to have the new peer add us. We will now wait for the peer to show up in the 'hosts' table and then request the job for it to add us. + The peer: [#!variable!peer_uuid!#] is not yet in 'hosts', continuing to wait. + The peer: [#!variable!peer_name!#] is now in 'hosts', proceeding. Test @@ -514,6 +528,7 @@ Here we will inject 't_0006', which injects 't_0001' which has a variable: [#!st There was a problem reading the peer's UUID. Read: [#!variable!uuid!#], which appears to be invalid. An SSH connection was established to: [#!variable!target!#], but we failed to establish a channel. The last error was: [#!variable!error!#]. The job: [#!variable!command!#] was picked up by: [#!variable!pid!#], but that process is not running and it appears to only be: [#!variable!percent!# %] complete. Restarting the job. + Unable to find a local IP on the same subnet as the IP/host: [#!variable!host!#] given for the target. Bi-directional setup not currently possible. There are not enough network interfaces on this machine. You have: [#!variable!interface_count!#] interface(s), and you need at least: [#!variable!required_interfaces_for_single!#] interfaces to connect to the requested networks (one for Back-Channel and one for each Internet-Facing network). @@ -558,6 +573,8 @@ The update appears to have not completed successfully. The output was: parse_banged_string(), an infinite loop was detected while processing: [#!variable!message!#].]]> + The TCP port: [#!variable!port!#] is not a valid. + ').]]> Yes diff --git a/tools/anvil-daemon b/tools/anvil-daemon index 8b8257ff..e5aec98f 100755 --- a/tools/anvil-daemon +++ b/tools/anvil-daemon @@ -107,7 +107,7 @@ $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list while(1) { # Connect to the database(s) - $anvil->Storage->read_config({file => "/etc/anvil/anvil.conf"}); + $anvil->Storage->read_config({file => $anvil->data->{path}{configs}{'anvil.conf'}}); $anvil->Database->connect(); $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 3, secure => 0, key => "log_0132"}); diff --git a/tools/anvil-manage-striker-peers b/tools/anvil-manage-striker-peers index 5d14f4ab..7f3c99fd 100755 --- a/tools/anvil-manage-striker-peers +++ b/tools/anvil-manage-striker-peers @@ -42,8 +42,9 @@ $| = 1; my $anvil = Anvil::Tools->new({log_level => 2, log_secure => 1}); # Read switches -$anvil->data->{switches}{list} = ""; -$anvil->data->{switches}{add} = 0; +$anvil->data->{switches}{list} = ""; +$anvil->data->{switches}{add} = 0; +$anvil->data->{switches}{remove} = 0; $anvil->Get->switches; # Make sure we're running as 'root' @@ -58,6 +59,10 @@ if (($< != 0) && ($> != 0)) # Paths $anvil->Storage->read_config({file => $anvil->data->{path}{configs}{'anvil.conf'}}); +# We'll try to connect in case we're adding additional peers. +$anvil->Database->connect(); +$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 2, secure => 0, key => "log_0132"}); + # Am I adding, editing or deleting? if (not $anvil->data->{switches}{list}) { @@ -68,6 +73,7 @@ if (not $anvil->data->{switches}{list}) # First sort by host name/ip foreach my $uuid (keys %{$anvil->data->{database}}) { + next if not $uuid; my $host = $anvil->data->{database}{$uuid}{host}; $anvil->data->{sorted}{db}{$host} = $uuid; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, secure => 0, level => 2, list => { @@ -103,22 +109,67 @@ $anvil->nice_exit({code => 0}); # Functions # ############################################################################################################# +# This updates the progress if we were called with a job UUID. +sub update_progress +{ + my ($anvil, $progress, $message) = @_; + $message = "" if not defined $message; + + # Log the progress percentage. + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { + progress => $progress, + message => $message, + "jobs::job_uuid" => $anvil->data->{jobs}{job_uuid}, + "sys::database::connections" => $anvil->data->{sys}{database}{connections}, + }}); + + if (($anvil->data->{jobs}{job_uuid}) && ($anvil->data->{sys}{database}{connections})) + { + $anvil->Job->update_progress({ + debug => 2, + progress => $progress, + message => $message, + job_uuid => $anvil->data->{jobs}{job_uuid}, + }); + } + + return(0); +} + sub process_entry { my ($anvil) = @_; + my $job_uuid = defined $anvil->data->{switches}{'job-uuid'} ? $anvil->data->{switches}{'job-uuid'} : ""; my $host_uuid = defined $anvil->data->{switches}{'host-uuid'} ? $anvil->data->{switches}{'host-uuid'} : ""; my $host = defined $anvil->data->{switches}{'host'} ? $anvil->data->{switches}{'host'} : ""; my $port = defined $anvil->data->{switches}{'port'} ? $anvil->data->{switches}{'port'} : 5432; my $password_file = defined $anvil->data->{switches}{'password-file'} ? $anvil->data->{switches}{'password-file'} : ""; my $ping = defined $anvil->data->{switches}{'ping'} ? $anvil->data->{switches}{'ping'} : 0; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, secure => 0, level => 2, list => { + job_uuid => $job_uuid, host_uuid => $host_uuid, host => $host, port => $port, password_file => $password_file, ping => $ping, }}); + + # If I have a job_uuid, store it in the data hash for 'update_progress'. + if ($job_uuid) + { + $anvil->data->{jobs}{job_uuid} = $job_uuid; + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, secure => 0, level => 2, list => { "jobs::job_uuid" => $anvil->data->{jobs}{job_uuid} }}); + + my $message = $anvil->data->{switches}{remove} ? "message_0067" : "message_0066"; + update_progress($anvil, 0, "clear"); + update_progress($anvil, 1, $message); + } + else + { + $anvil->data->{jobs}{job_uuid} = ""; + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, secure => 0, level => 2, list => { "jobs::job_uuid" => $anvil->data->{jobs}{job_uuid} }}); + } # 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'}}); @@ -133,27 +184,83 @@ sub process_entry { # Invalid UUID. print $anvil->Words->string({key => "error_0031", variables => { host_uuid => $host_uuid }})."\n"; + update_progress($anvil, 100, "error_0031,!!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}); } if ((not $host) && (not $anvil->data->{switches}{remove})) { - # Invalid UUID. + # Invalid Host name. 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 => $host }}); + update_progress($anvil, 100, "error_0032,!!switch!host!!"); + $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 2, secure => 0, key => "error_0032", variables => { switch => "host" }}); $anvil->nice_exit({code => 2}); } if (($port =~ /\D/) or ($port < 1) or ($port > 65535)) { # Invalid port. - print $anvil->Words->string({key => "error_0033", variables => { port => $port }})."\n"; - $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 2, secure => 0, key => "error_0033", variables => { port => $port }}); + print $anvil->Words->string({key => "error_0038", variables => { port => $port }})."\n"; + update_progress($anvil, 100, "error_0038,!!port!".$port."!!"); + $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 2, secure => 0, key => "error_0038", variables => { port => $port }}); $anvil->nice_exit({code => 2}); } - # Pull the password out of the file. - my $password = $anvil->Storage->read_file({file => $password_file}); - $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, secure => 1, level => 2, list => { password => $password }}); + # The password might be in 'job_data' or a password file. + my $password = ""; + my $add_peer_command = ""; + if ($password_file) + { + # Pull the password out of the file. + update_progress($anvil, 100, "message_0070,!!port!".$port."!!"); + $password = $anvil->Storage->read_file({file => $password_file}); + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, secure => 1, level => 2, list => { password => $password }}); + } + if ((not $password) && ($job_uuid)) + { + # Do we have a database connection? + if ($anvil->data->{sys}{database}{connections}) + { + # See if the password is in job_data. + my $query = "SELECT job_data FROM jobs WHERE job_uuid = ".$anvil->data->{sys}{database}{use_handle}->quote($job_uuid).";"; + my $results = $anvil->Database->query({query => $query, source => $THIS_FILE, line => __LINE__}); + my $count = @{$results}; + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { + results => $results, + count => $count, + }}); + if ($count) + { + my $job_data = $results->[0]->[0]; + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, secure => 1, list => { job_data => $job_data }}); + + foreach my $line (split/\n/, $job_data) + { + my $secure = $line =~ /password/ ? 1 : 0; + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, secure => $secure, list => { line => $line }}); + if ($line =~ /password=(.*)$/) + { + $password = $1; + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, secure => 1, list => { password => $password }}); + } + if ($line =~ /job_command=(.*)$/) + { + $add_peer_command = $1; + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, secure => 0, list => { add_peer_command => $add_peer_command }}); + } + } + } + } + } + + # If I am not removing, I need a password. + if ((not $anvil->data->{switches}{remove}) && (not $password)) + { + print $anvil->Words->string({key => "error_0039"})."\n"; + update_progress($anvil, 100, "error_0039"); + $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 2, secure => 0, key => "error_0039"}); + $anvil->nice_exit({code => 2}); + } + update_progress($anvil, 25, "message_0068"); # If the config already exists, we'll look at each of the values to see if any changed (or are not defaults). If so, we'll rewrite my $host_variable = "database::${host_uuid}::host"; @@ -174,9 +281,11 @@ sub process_entry $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, secure => 1, level => 2, list => { insert => $insert }}); # Loop through the existing file. - my $new_body = ""; - my $just_deleted = 0; - my $test_line = "database::${host_uuid}::"; + my $delete_reported = 0; + my $update_reported = 0; + my $new_body = ""; + my $just_deleted = 0; + my $test_line = "database::${host_uuid}::"; foreach my $line (split/\n/, $anvil->data->{body}{'anvil.conf'}) { # If I removed an entry, I also want to delete the white space after it. @@ -207,6 +316,7 @@ sub process_entry new_body => $new_body, "config::rewrite" => $anvil->data->{config}{rewrite}, }}); + update_progress($anvil, 50, "message_0069"); } } @@ -245,6 +355,8 @@ sub process_entry just_deleted => $just_deleted, "config::rewrite" => $anvil->data->{config}{rewrite}, }}); + update_progress($anvil, 50, "message_0070") if not $delete_reported; + $delete_reported = 1; next; } elsif ($value eq $host) @@ -279,6 +391,8 @@ sub process_entry just_deleted => $just_deleted, "config::rewrite" => $anvil->data->{config}{rewrite}, }}); + update_progress($anvil, 50, "message_0070") if not $delete_reported; + $delete_reported = 1; next; } elsif ($value eq $port) @@ -295,6 +409,8 @@ sub process_entry line => $line, "config::rewrite" => $anvil->data->{config}{rewrite}, }}); + update_progress($anvil, 50, "message_0071") if not $update_reported; + $update_reported = 1; } } elsif ($variable eq $password_variable) @@ -317,6 +433,8 @@ sub process_entry just_deleted => $just_deleted, "config::rewrite" => $anvil->data->{config}{rewrite}, }}); + update_progress($anvil, 50, "message_0070") if not $delete_reported; + $delete_reported = 1; next; } elsif ($value eq $password) @@ -333,6 +451,8 @@ sub process_entry line => $anvil->Log->secure ? $line : $anvil->Words->string({key => "log_0186"}), "config::rewrite" => $anvil->data->{config}{rewrite}, }}); + update_progress($anvil, 50, "message_0071") if not $update_reported; + $update_reported = 1; } } elsif ($variable eq $ping_variable) @@ -351,6 +471,8 @@ sub process_entry just_deleted => $just_deleted, "config::rewrite" => $anvil->data->{config}{rewrite}, }}); + update_progress($anvil, 50, "message_0070") if not $delete_reported; + $delete_reported = 1; next; } elsif ($value eq $ping) @@ -367,6 +489,8 @@ sub process_entry line => $line, "config::rewrite" => $anvil->data->{config}{rewrite}, }}); + update_progress($anvil, 50, "message_0071") if not $update_reported; + $update_reported = 1; } } } @@ -379,6 +503,7 @@ sub process_entry # 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 }}); + update_progress($anvil, 75, "message_0072,!!backup!".$backup_file."!!"); # Now update! $anvil->Storage->write_file({ @@ -390,14 +515,82 @@ sub process_entry mode => "0644", overwrite => 1, }); + update_progress($anvil, 80, "message_0073"); - # Delete any databases I new about, the we'll reload from the config. - delete $anvil->data->{database}; + # Disconnect (if no connections exist, will still clear out known databases). + $anvil->Database->disconnect; # Re-read the config. sleep 1; $anvil->Storage->read_config({file => $anvil->data->{path}{configs}{'anvil.conf'}}); } + # If I've been asked to have the peer add us, disconnect from the database, re-read the new config + # and then reconnect. This should cause a sync of 'hosts' and allow us to insert a job for the peer + # to add us. + if ($add_peer_command) + { + update_progress($anvil, 85, "message_0074"); + + # Reconnect + $anvil->Database->connect; + $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 2, secure => 0, key => "log_0132"}); + + # Now loop until we see the peer's host_uuid show up + $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 2, secure => 0, key => "log_0212"}); + my $peer_host_name = ""; + my $host_seen = 0; + until($host_seen) + { + $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 2, secure => 0, key => "log_0212"}); + my $query = "SELECT host_name FROM hosts WHERE host_uuid = ".$anvil->data->{sys}{database}{use_handle}->quote($host_uuid).";"; + my $results = $anvil->Database->query({query => $query, source => $THIS_FILE, line => __LINE__}); + my $count = @{$results}; + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { + results => $results, + count => $count, + }}); + if ($count) + { + $peer_host_name = $results->[0]->[0]; + $host_seen = 1; + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { + peer_host_name => $peer_host_name, + host_seen => $host_seen, + }}); + $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 2, secure => 0, key => "log_0214", variables => { peer_name => $peer_host_name }}); + update_progress($anvil, 90, "message_0075,!!host!".$peer_host_name."!!"); + } + else + { + $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 2, secure => 0, key => "log_0213", variables => { peer_uuid => $host_uuid }}); + sleep(1); + } + } + + # Now add the job. + my $host_uuid = $anvil->Get->host_uuid; + my $sql_password = $anvil->data->{database}{$host_uuid}{password}; + my $job_command = $add_peer_command; + my $job_data = "password=".$sql_password; + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, secure => 0, list => { job_command => $job_command }}); + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, secure => 1, list => { job_data => $job_data }}); + + # Store the job + my ($job_uuid) = $anvil->Database->insert_or_update_jobs({ + file => $THIS_FILE, + line => __LINE__, + job_host_uuid => $host_uuid, + job_command => $job_command, + job_data => $job_data, + job_name => "striker-peer::add", + job_title => "job_0011", + job_description => "job_0012", + }); + $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { job_uuid => $job_uuid }}); + update_progress($anvil, 95, "message_0076"); + } + + update_progress($anvil, 100, "message_0025"); return(0); }