@ -10,6 +10,7 @@ use strict;
use warnings;
use warnings;
use Anvil::Tools;
use Anvil::Tools;
use Data::Dumper;
use Data::Dumper;
use NetAddr::IP;
$| = 1;
$| = 1;
@ -22,7 +23,6 @@ if (($running_directory =~ /^\./) && ($ENV{PWD}))
=cut
=cut
Striker initialization;
# At this point, the Striker should be coming up at the IP. Once both/all Strikers are up, update their anvil.conf to add each other's UUID database entry.
# At this point, the Striker should be coming up at the IP. Once both/all Strikers are up, update their anvil.conf to add each other's UUID database entry.
# FROM ONE STRIKER;
# FROM ONE STRIKER;
job_uuid | job_host_uuid | job_command | job_data | job_picked_up_by | job_picked_up_at | job_updated | job_name | job_progress | job_title | job_description | job_status | modified_date
job_uuid | job_host_uuid | job_command | job_data | job_picked_up_by | job_picked_up_at | job_updated | job_name | job_progress | job_title | job_description | job_status | modified_date
@ -167,10 +167,12 @@ sub merge_peer_striker
foreach my $network_number (sort {$a cmp $b} keys %{$anvil->data->{striker}{$striker_number}{network}{$network}})
foreach my $network_number (sort {$a cmp $b} keys %{$anvil->data->{striker}{$striker_number}{network}{$network}})
{
{
my $ip = $anvil->data->{striker}{$striker_number}{network}{$network}{$network_number}{ip};
my $ip = $anvil->data->{striker}{$striker_number}{network}{$network}{$network_number}{ip};
push @{$peer_ips}, $ip;
my $subnet_mask = $anvil->data->{striker}{$striker_number}{network}{$network}{$network_number}{subnet_mask};
push @{$peer_ips}, $ip."/".$subnet_mask;
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => {
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => {
network_number => $network_number,
network_number => $network_number,
ip => $ip,
ip => $ip,
subnet_mask => $subnet_mask,
}});
}});
foreach my $link_number (sort {$a cmp $b} keys %{$anvil->data->{striker}{$striker_number}{network}{$network}{$network_number}{'link'}})
foreach my $link_number (sort {$a cmp $b} keys %{$anvil->data->{striker}{$striker_number}{network}{$network}{$network_number}{'link'}})
{
{
@ -212,26 +214,111 @@ AND
### Add the peer.
### Add the peer.
# First, wait for access.
# First, wait for access.
my $waiting = 1;
my $waiting = 1;
my $joining = 0;
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, 'print' => 1, level => 1, key => "job_0227", variables => { number => $striker_number }});
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, 'print' => 1, level => 1, key => "job_0227", variables => { number => $striker_number }});
while ($waiting)
while ($waiting)
{
{
foreach my $ip (@{$peer_ips})
foreach my $network (@{$peer_ips})
{
{
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { ip => $ip }});
next if not $waiting;
my $access = $anvil->Remote->test_access({
my ($ip, $subnet_mask) = ($network =~ /^(.*?)\/(.*)$/);
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => {
ip => $ip,
subnet_mask => $subnet_mask,
}});
# Try to read the host uuid.
my $file = $anvil->data->{path}{data}{host_uuid};
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { file => $file }});
my $shell_call = "
if [ -e ".$file." ];
then
cat ".$file.";
else
echo 0;
fi;
";
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 2, key => "log_0166", variables => {
shell_call => $shell_call,
target => $ip,
remote_user => "root",
}});
my ($host_uuid, $error, $return_code) = $anvil->Remote->call({
shell_call => $shell_call,
target => $ip,
target => $ip,
password => $anvil->data->{base}{password}{desired},
password => $anvil->data->{base}{password}{desired},
});
});
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { access => $access }});
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => {
if ($access)
host_uuid => $host_uuid,
error => $error,
return_code => $return_code,
}});
if (($host_uuid) && ($anvil->Validate->uuid({uuid => $host_uuid})))
{
{
# Success!
# Success!
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, 'print' => 1, level => 1, key => "job_0228", variables => {
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, 'print' => 1, level => 1, key => "job_0228", variables => {
number => $striker_number,
number => $striker_number,
ip => $ip,
ip => $ip,
}});
}});
exit(0);
$peer_host_uuid = $host_uuid;
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { peer_host_uuid => $peer_host_uuid }});
# Find the IP we used to reach the peer.
my $our_ip = "";
$anvil->Network->load_ips({
host => "local",
host_uuid => $anvil->Get->host_uuid(),
});
foreach my $interface_name (sort {$a cmp $b} keys %{$anvil->data->{network}{'local'}{interface}})
{
my $local_ip = $anvil->data->{network}{'local'}{interface}{$interface_name}{ip};
my $local_subnet_mask = $anvil->data->{network}{'local'}{interface}{$interface_name}{subnet_mask};
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => {
interface_name => $interface_name,
local_ip => $local_ip,
local_subnet_mask => $local_subnet_mask,
}});
next if $local_subnet_mask ne $subnet_mask;
# See if this IP is in the same subnet.
my $first = NetAddr::IP->new($local_ip."/".$local_subnet_mask);
my $second = NetAddr::IP->new($ip."/".$subnet_mask);
if ($second->within($first))
{
# Found the IP to tell the peer to use
$our_ip = $local_ip;
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { our_ip => $our_ip }});
last;
}
}
if (not $our_ip)
{
# wtf;
die;
}
# Register a job and then wait for it to show up in our database.
my $job_data = "password=".$anvil->data->{base}{password}{desired}."\n";
$job_data .= "peer_job_command=".$anvil->data->{path}{exe}{'striker-manage-peers'}." --add --host-uuid ".$anvil->Get->host_uuid." --host ".$our_ip." --port 5432 --ping 1";
my $job_command = $anvil->data->{path}{exe}{'striker-manage-peers'}." --add --host-uuid ".$host_uuid." --host ".$ip." --port 5432 --ping 1";
$waiting = 0;
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => {
job_command => $job_command,
job_data => $anvil->Log->is_secure($job_data),
waiting => $waiting,
}});
my ($job_uuid) = $anvil->Database->insert_or_update_jobs({
job_command => $job_command,
job_data => $job_data,
job_name => "striker-peer::remove",
job_title => "job_0013",
job_description => "job_0014",
job_progress => 0,
});
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { job_uuid => $job_uuid }});
}
}
else
else
{
{
@ -250,8 +337,46 @@ AND
}
}
}
}
}
}
# Now wait until the peer is in our database.
my $peer_host_name = "";
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, 'print' => 1, level => 1, key => "job_0231", variables => {
number => $striker_number,
peer_host_uuid => $peer_host_uuid,
}});
until ($peer_host_name)
{
my $query = "SELECT host_name FROM hosts WHERE host_uuid = ".$anvil->Database->quote($peer_host_uuid).";";
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { query => $query }});
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];
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, 'print' => 1, level => 1, key => "job_0232", variables => {
number => $striker_number,
peer_host_name => $peer_host_name,
}});
}
else
{
# Sleep for a bit.
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, 'print' => 1, level => 1, key => "job_0233", variables => {
number => $striker_number,
peer_host_uuid => $peer_host_uuid,
}});
sleep 5;
}
}
}
}
# By here, the peer(s) are joined.
return(0);
return(0);
}
}