Replaced hosts management in anvil-join-anvil with System->update_hosts.

Signed-off-by: digimer <mkelly@alteeve.ca>
main
digimer 10 months ago
parent 3fd2a172a4
commit b8c73fd3f2
  1. 2
      Anvil/Tools/System.pm
  2. 2
      tools/anvil-daemon
  3. 105
      tools/anvil-join-anvil

@ -5484,7 +5484,7 @@ sub update_hosts
# Push the IP into the array so that we print them in the order be first saw them. # Push the IP into the array so that we print them in the order be first saw them.
push @{$ip_order}, $ip_address; push @{$ip_order}, $ip_address;
} }
$lines->{$ip_address} .= $host_name." "; $lines->{$ip_address} .= $host_name;
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => {
"lines->${ip_address}" => $lines->{$ip_address}, "lines->${ip_address}" => $lines->{$ip_address},
}}); }});

@ -1901,7 +1901,7 @@ sub run_jobs
# Record that we've tried to start this job, so that we don't try to restart it for any reason for at least a minute. # Record that we've tried to start this job, so that we don't try to restart it for any reason for at least a minute.
$anvil->data->{jobs}{$job_uuid}{started} = time; $anvil->data->{jobs}{$job_uuid}{started} = time;
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { 'jobs::$job_uuid::started' => $anvil->data->{jobs}{$job_uuid}{started} }}); $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { "jobs::${job_uuid}::started" => $anvil->data->{jobs}{$job_uuid}{started} }});
# Record that a job with this command has started # Record that a job with this command has started
$anvil->data->{jobs_started}{$short_command} = $job_uuid; $anvil->data->{jobs_started}{$short_command} = $job_uuid;

@ -263,7 +263,7 @@ sub wait_for_etc_hosts
# Add the IP to be added to /etc/hosts. # Add the IP to be added to /etc/hosts.
$anvil->data->{to_add}{$host_name} = $ip_address; $anvil->data->{to_add}{$host_name} = $ip_address;
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { "to_add::${ip_address}" => $anvil->data->{to_add}{$host_name} }}); $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { "to_add::${host_name}" => $anvil->data->{to_add}{$host_name} }});
} }
} }
@ -279,107 +279,10 @@ sub wait_for_etc_hosts
{ {
# Not ready, wait a bit. # Not ready, wait a bit.
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, 'print' => 1, level => 1, key => "log_0834"}); $anvil->Log->entry({source => $THIS_FILE, line => __LINE__, 'print' => 1, level => 1, key => "log_0834"});
sleep 5;
# Add the values to /etc/hosts. # Try to update the /etc/hosts file
my $new_hosts_body = ""; $anvil->System->update_hosts({debug => 2});
foreach my $line (split/\n/, $hosts_file)
{
# See if this line needs to be modified.
if ($line =~ /^(\d.*?)\s+(.*)$/)
{
my $this_ip = $1;
my $hosts = $2;
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => {
this_ip => $this_ip,
hosts => $hosts,
}});
if ($anvil->Validate->ip({ip => $this_ip}))
{
my $changes = 0;
my $new_hosts = "";
my $comment = ($hosts =~ /(#.*)$/)[0];
$comment = "" if not defined $comment;
$hosts =~ s/#.*?//;
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => {
comment => $comment,
hosts => $hosts,
}});
foreach my $host_name (split/\s+/, $hosts)
{
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { host_name => $host_name }});
if ((exists $anvil->data->{to_add}{$host_name}) &&
($anvil->data->{to_add}{$host_name}) &&
($this_ip ne $anvil->data->{to_add}{$host_name}))
{
# Remove this host.
$changes = 1;
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { changes => $changes }});
delete $anvil->data->{to_add}{$host_name};
next;
}
$new_hosts .= $host_name." ";
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { new_hosts => $new_hosts }});
}
# Do we have any names to add to this IP?
foreach my $host_name (sort {$a cmp $b} keys %{$anvil->data->{to_add}})
{
if ($this_ip eq $anvil->data->{to_add}{$host_name})
{
# Add it.
$new_hosts .= $host_name." ";
$changes = 1;
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { changes => $changes }});
delete $anvil->data->{to_add}{$host_name};
}
}
if ($changes)
{
$new_hosts_body .= $this_ip."\t".$new_hosts.$comment."\n";
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { changes => $changes }});
next;
}
}
}
# If we're alive here, just add the old line.
$new_hosts_body .= $line."\n";
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { new_hosts_body => $new_hosts_body }});
}
# Add any hosts still not processed.
foreach my $host_name (sort {$a cmp $b} keys %{$anvil->data->{to_add}})
{
$new_hosts_body .= $anvil->data->{to_add}{$host_name}."\t".$host_name."\n";
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { new_hosts_body => $new_hosts_body }});
delete $anvil->data->{to_add}{$host_name};
}
# If there's a difference, write the new hosts file.
my $difference = diff \$new_hosts_body, \$hosts_file, { STYLE => 'Unified' };
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { difference => $difference }});
if ($difference)
{
update_progress($anvil, ($anvil->data->{job}{progress} += 2), "job_0112");
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, 'print' => 1, level => 1, key => "log_0835"});
my $failed = $anvil->Storage->write_file({
debug => 2,
overwrite => 1,
file => $anvil->data->{path}{configs}{hosts},
body => $new_hosts_body,
user => "root",
group => "root",
mode => "0644",
});
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { failed => $failed }});
}
sleep 1; sleep 1;
} }
} }

Loading…
Cancel
Save