2007-10-26 22:44:33 +00:00
|
|
|
# IBM(c) 2007 EPL license http://www.eclipse.org/legal/epl-v10.html
|
|
|
|
package xCAT_plugin::hosts;
|
2008-07-18 20:22:45 +00:00
|
|
|
use strict;
|
|
|
|
use warnings;
|
2007-10-26 22:44:33 +00:00
|
|
|
use xCAT::Table;
|
|
|
|
use Data::Dumper;
|
2008-07-02 15:22:30 +00:00
|
|
|
use File::Copy;
|
2009-04-02 21:38:45 +00:00
|
|
|
use Getopt::Long;
|
2010-02-19 15:00:51 +00:00
|
|
|
use Fcntl ':flock';
|
2009-04-02 21:38:45 +00:00
|
|
|
|
2007-10-26 22:44:33 +00:00
|
|
|
|
|
|
|
my @hosts; #Hold /etc/hosts data to be written back
|
2009-04-02 21:38:45 +00:00
|
|
|
my $LONGNAME;
|
2010-01-07 19:33:58 +00:00
|
|
|
my $OTHERNAMESFIRST;
|
2010-01-11 18:36:24 +00:00
|
|
|
my $ADDNAMES;
|
2007-10-26 22:44:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
my %usage=(
|
2010-02-11 21:01:24 +00:00
|
|
|
makehosts => "Usage: makehosts <noderange> [-d] [-n] [-l] [-a] [-o]\n makehosts -h",
|
2007-10-26 22:44:33 +00:00
|
|
|
);
|
|
|
|
sub handled_commands {
|
|
|
|
return {
|
|
|
|
makehosts => "hosts",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-02-11 21:01:24 +00:00
|
|
|
sub delnode {
|
|
|
|
my $node = shift;
|
|
|
|
my $ip = shift;
|
|
|
|
unless ($node and $ip) { return; } #bail if requested to do something that could zap /etc/hosts badly
|
|
|
|
my $othernames = shift;
|
|
|
|
my $domain = shift;
|
|
|
|
my $idx=0;
|
|
|
|
|
|
|
|
while ($idx <= $#hosts) {
|
|
|
|
if (($ip and $hosts[$idx] =~ /^${ip}\s/) or $hosts[$idx] =~ /^\d+\.\d+\.\d+\.\d+\s+${node}[\s\.r]/) {
|
|
|
|
$hosts[$idx]="";
|
|
|
|
}
|
|
|
|
$idx++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-26 22:44:33 +00:00
|
|
|
sub addnode {
|
|
|
|
my $node = shift;
|
|
|
|
my $ip = shift;
|
2009-02-17 20:40:37 +00:00
|
|
|
unless ($node and $ip) { return; } #bail if requested to do something that could zap /etc/hosts badly
|
2007-10-26 22:44:33 +00:00
|
|
|
my $othernames = shift;
|
2009-03-15 18:54:05 +00:00
|
|
|
my $domain = shift;
|
2007-10-26 22:44:33 +00:00
|
|
|
my $idx=0;
|
|
|
|
my $foundone=0;
|
|
|
|
|
|
|
|
while ($idx <= $#hosts) {
|
2009-07-06 20:39:39 +00:00
|
|
|
if ($hosts[$idx] =~ /^${ip}\s/ or $hosts[$idx] =~ /^\d+\.\d+\.\d+\.\d+\s+${node}[\s\.r]/) {
|
2009-06-04 19:11:04 +00:00
|
|
|
if ($foundone) {
|
|
|
|
$hosts[$idx]="";
|
|
|
|
} else {
|
|
|
|
$hosts[$idx]=build_line($ip, $node, $domain, $othernames);
|
|
|
|
}
|
2007-10-26 22:44:33 +00:00
|
|
|
$foundone=1;
|
|
|
|
}
|
|
|
|
$idx++;
|
|
|
|
}
|
2009-06-04 19:11:04 +00:00
|
|
|
if ($foundone) { return;}
|
2009-04-02 21:38:45 +00:00
|
|
|
|
|
|
|
my $line=build_line($ip, $node, $domain, $othernames);
|
|
|
|
push @hosts, $line;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub build_line {
|
|
|
|
my $ip=shift;
|
|
|
|
my $node=shift;
|
|
|
|
my $domain=shift;
|
|
|
|
my $othernames=shift;
|
2009-04-27 03:06:42 +00:00
|
|
|
my @o_names=();
|
2010-01-11 19:06:16 +00:00
|
|
|
my @n_names=();
|
2009-04-27 03:06:42 +00:00
|
|
|
if (defined $othernames) {
|
|
|
|
@o_names=split(/,| /, $othernames);
|
|
|
|
}
|
2009-04-02 21:38:45 +00:00
|
|
|
my $longname;
|
|
|
|
foreach (@o_names) {
|
|
|
|
if (($_ eq $node) || ( $domain && ($_ eq "$node.$domain"))) {
|
|
|
|
$longname="$node.$domain";
|
|
|
|
$_="";
|
|
|
|
} elsif ( $_ =~ /\./) {
|
|
|
|
if (!$longname) {
|
|
|
|
$longname=$_;
|
|
|
|
$_="";
|
|
|
|
}
|
2010-01-11 18:36:24 +00:00
|
|
|
} elsif ($ADDNAMES) {
|
2010-01-11 19:06:16 +00:00
|
|
|
unshift(@n_names,"$_.$domain");
|
2009-04-02 21:38:45 +00:00
|
|
|
}
|
2010-01-11 18:43:18 +00:00
|
|
|
}
|
2010-01-11 19:06:16 +00:00
|
|
|
unshift(@o_names,@n_names);
|
2009-04-02 21:38:45 +00:00
|
|
|
|
2009-07-14 15:55:31 +00:00
|
|
|
if ($node =~ m/\.$domain$/i) {
|
|
|
|
$longname = $node;
|
|
|
|
$node =~ s/\.$domain$//;
|
|
|
|
} elsif ($domain && !$longname) {
|
|
|
|
$longname="$node.$domain";
|
2009-04-02 21:38:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$othernames=join(' ', @o_names);
|
|
|
|
if ($LONGNAME) { return "$ip $longname $node $othernames\n"; }
|
2010-01-11 19:06:16 +00:00
|
|
|
elsif ($OTHERNAMESFIRST) { return "$ip $othernames $longname $node\n"; }
|
2009-04-02 21:38:45 +00:00
|
|
|
else { return "$ip $node $longname $othernames\n"; }
|
2007-10-26 22:44:33 +00:00
|
|
|
}
|
2009-04-02 21:38:45 +00:00
|
|
|
|
|
|
|
|
2009-07-06 20:39:39 +00:00
|
|
|
sub addotherinterfaces {
|
|
|
|
my $node = shift;
|
|
|
|
my $otherinterfaces = shift;
|
|
|
|
my $domain = shift;
|
|
|
|
|
|
|
|
my @itf_pairs=split(/,/, $otherinterfaces);
|
|
|
|
foreach (@itf_pairs) {
|
|
|
|
my ($itf,$ip)=split(/:/, $_);
|
|
|
|
if ($itf =~ /^-/ ) {
|
|
|
|
$itf = $node.$itf };
|
|
|
|
addnode $itf,$ip,'',$domain;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-10-26 22:44:33 +00:00
|
|
|
sub process_request {
|
2009-04-02 21:38:45 +00:00
|
|
|
Getopt::Long::Configure("bundling") ;
|
|
|
|
$Getopt::Long::ignorecase=0;
|
|
|
|
Getopt::Long::Configure("no_pass_through");
|
|
|
|
|
2007-10-26 22:44:33 +00:00
|
|
|
my $req = shift;
|
|
|
|
my $callback = shift;
|
2009-04-02 21:38:45 +00:00
|
|
|
my $HELP;
|
|
|
|
my $REMOVE;
|
2010-02-11 21:01:24 +00:00
|
|
|
my $DELNODE;
|
2009-04-02 21:38:45 +00:00
|
|
|
|
|
|
|
# parse the options
|
|
|
|
if ($req && $req->{arg}) {@ARGV = @{$req->{arg}};}
|
|
|
|
else { @ARGV = (); }
|
2009-04-27 03:06:42 +00:00
|
|
|
|
|
|
|
# print "argv=@ARGV\n";
|
2009-04-02 21:38:45 +00:00
|
|
|
if(!GetOptions(
|
|
|
|
'h|help' => \$HELP,
|
|
|
|
'n' => \$REMOVE,
|
2010-02-11 21:01:24 +00:00
|
|
|
'd' => \$DELNODE,
|
2010-01-11 18:36:24 +00:00
|
|
|
'o|othernamesfirst' => \$OTHERNAMESFIRST,
|
|
|
|
'a|adddomaintohostnames' => \$ADDNAMES,
|
2009-04-02 21:38:45 +00:00
|
|
|
'l|longnamefirst' => \$LONGNAME,))
|
|
|
|
{
|
|
|
|
$callback->({data=>$usage{makehosts}});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
# display the usage if -h
|
|
|
|
if ($HELP) {
|
|
|
|
$callback->({data=>$usage{makehosts}});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-10-26 22:44:33 +00:00
|
|
|
my $hoststab = xCAT::Table->new('hosts');
|
2009-03-15 18:54:05 +00:00
|
|
|
my $sitetab = xCAT::Table->new('site');
|
|
|
|
my $domain;
|
2010-02-19 15:00:51 +00:00
|
|
|
my $lockh;
|
2009-03-15 18:54:05 +00:00
|
|
|
if ($sitetab) {
|
|
|
|
my $dent = $sitetab->getAttribs({key=>'domain'},'value');
|
|
|
|
if ($dent and $dent->{value}) {
|
|
|
|
$domain=$dent->{value};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-26 22:44:33 +00:00
|
|
|
@hosts = ();
|
2009-04-02 21:38:45 +00:00
|
|
|
if ($REMOVE) {
|
2007-10-26 22:44:33 +00:00
|
|
|
if (-e "/etc/hosts") {
|
|
|
|
my $bakname = "/etc/hosts.xcatbak";
|
|
|
|
rename("/etc/hosts",$bakname);
|
|
|
|
}
|
|
|
|
} else {
|
2008-06-03 20:28:01 +00:00
|
|
|
if (-e "/etc/hosts") {
|
|
|
|
my $bakname = "/etc/hosts.xcatbak";
|
|
|
|
copy("/etc/hosts",$bakname);
|
|
|
|
}
|
2010-02-19 15:00:51 +00:00
|
|
|
open($lockh,">","/tmp/xcat/hostsfile.lock");
|
|
|
|
flock($lockh,LOCK_EX);
|
2008-07-18 20:22:45 +00:00
|
|
|
my $rconf;
|
2007-10-26 22:44:33 +00:00
|
|
|
open($rconf,"/etc/hosts"); # Read file into memory
|
|
|
|
if ($rconf) {
|
|
|
|
while (<$rconf>) {
|
|
|
|
push @hosts,$_;
|
|
|
|
}
|
|
|
|
close($rconf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($req->{node}) {
|
2009-07-06 20:39:39 +00:00
|
|
|
my $hostscache = $hoststab->getNodesAttribs($req->{node},[qw(ip node hostnames otherinterfaces)]);
|
2007-10-26 22:44:33 +00:00
|
|
|
foreach(@{$req->{node}}) {
|
2009-07-06 20:39:39 +00:00
|
|
|
my $ref = $hostscache->{$_}->[0]; #$hoststab->getNodeAttribs($_,[qw(ip node hostnames otherinterfaces)]);
|
2010-02-11 21:01:24 +00:00
|
|
|
if ($DELNODE) {
|
|
|
|
delnode $ref->{node},$ref->{ip},$ref->{hostnames},$domain;
|
|
|
|
} else {
|
|
|
|
addnode $ref->{node},$ref->{ip},$ref->{hostnames},$domain;
|
|
|
|
if (defined($ref->{otherinterfaces})){
|
|
|
|
addotherinterfaces $ref->{node},$ref->{otherinterfaces},$domain;
|
|
|
|
}
|
2009-07-06 20:39:39 +00:00
|
|
|
}
|
2007-10-26 22:44:33 +00:00
|
|
|
}
|
|
|
|
} else {
|
2010-02-11 21:01:24 +00:00
|
|
|
if ($DELNODE) {
|
|
|
|
return;
|
|
|
|
}
|
2009-07-06 20:39:39 +00:00
|
|
|
my @hostents = $hoststab->getAllNodeAttribs(['ip','node','hostnames','otherinterfaces']);
|
2007-10-26 22:44:33 +00:00
|
|
|
foreach (@hostents) {
|
2009-03-15 18:54:05 +00:00
|
|
|
addnode $_->{node},$_->{ip},$_->{hostnames},$domain;
|
2009-07-06 20:39:39 +00:00
|
|
|
if (defined($_->{otherinterfaces})){
|
|
|
|
addotherinterfaces $_->{node},$_->{otherinterfaces},$domain;
|
|
|
|
}
|
2007-10-26 22:44:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
writeout();
|
2010-02-19 15:00:51 +00:00
|
|
|
flock($lockh,LOCK_UN);
|
2007-10-26 22:44:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
sub writeout {
|
|
|
|
my $targ;
|
|
|
|
open($targ,'>',"/etc/hosts");
|
|
|
|
foreach (@hosts) {
|
|
|
|
print $targ $_;
|
|
|
|
}
|
|
|
|
close($targ)
|
|
|
|
}
|
|
|
|
|
|
|
|
1;
|