adjust-apache: Better handling for /etc/hosts

Signed-off-by: Ben Greear <greearb@candelatech.com>
This commit is contained in:
Ben Greear
2019-11-21 14:20:10 -08:00
parent e9ffab48d3
commit 756b1ceb09

View File

@@ -48,34 +48,34 @@ die ("No ip found for mgt_dev; your config.values file is out of date: $!")
unless ((defined $ip) && ($ip ne "")); unless ((defined $ip) && ($ip ne ""));
print "ip: $ip\n"; print "ip: $ip\n";
my @host_lines = `cat /etc/hosts`;
chomp (@host_lines); # This must be kept in sync with similar code in lf_kinstall.
@host_lines = ("127.0.0.1 localhost", @host_lines) my $fname = "/etc/hosts";
if (@host_lines < 1); if (-f "$fname") {
my $removed = 0; my @lines = `cat $fname`;
for (my $i =$#host_lines-1; $i>=0; $i--) { open(FILE, ">$fname") or die "Couldn't open file: $fname for writing: $!\n\n";
my $line = $host_lines[$i]; my $foundit = 0;
if ($line =~ /$MgrHostname/) { my $i;
splice(@host_lines, $i, 1);
$removed++; for ($i = 0; $i<@lines; $i++) {
my $ln =$lines[$i];
chomp($ln);
if ($ln =~ /^###-LF-HOSTAME-NEXT-###/) {
print FILE "$ln\n";
print FILE "$ip $MgrHostname\n";
$i++;
$foundit = 1;
} }
if (($removed < 2) && ( $line eq "" || $line eq "\n")) { else {
splice(@host_lines, $i, 1); print FILE "$ln\n";
$removed++;
} }
}
if (!$foundit) {
print FILE "###-LF-HOSTAME-NEXT-###\n";
print FILE "$ip $MgrHostname";
}
close FILE;
} }
@host_lines = (@host_lines, ("$ip $MgrHostname", "\n"));
my $dt = `date +%Y%m%d-%H%M%s`;
chomp $dt;
#print "dt[$dt]\n";
#print "cp /etc/hosts /etc/.hosts.$dt\n";
#print "=======================================\n";
#print join("\n", @host_lines);
#print "=======================================\n";
die ("Unable to write to /etc/hosts: $!")
unless open(my $fh, ">", "/etc/hosts");
print $fh join("\n", @host_lines);
close $fh;
my $local_crt =""; my $local_crt ="";
my $local_key =""; my $local_key ="";
@@ -141,6 +141,8 @@ foreach my $file (@places_to_check) {
$edited++ if ($confline =~ /# modified by lanforge/); $edited++ if ($confline =~ /# modified by lanforge/);
} }
push(@newlines, "# modified by lanforge\n") if ($edited == 0); push(@newlines, "# modified by lanforge\n") if ($edited == 0);
my $fh;
die ($!) unless open($fh, ">", $file); die ($!) unless open($fh, ">", $file);
print $fh join("\n", @newlines); print $fh join("\n", @newlines);
close $fh; close $fh;