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;
|
2012-08-09 04:07:40 +00:00
|
|
|
use xCAT::TableUtils;
|
2010-05-27 07:42:49 +00:00
|
|
|
use xCAT::NetworkUtils;
|
2007-10-26 22:44:33 +00:00
|
|
|
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
|
|
|
|
2012-12-03 20:23: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;
|
2010-05-27 07:42:49 +00:00
|
|
|
my $MACTOLINKLOCAL;
|
2007-10-26 22:44:33 +00:00
|
|
|
|
2012-12-07 16:18:23 +00:00
|
|
|
|
|
|
|
############# TODO - add return code checking !!!!!
|
|
|
|
|
2012-12-03 20:23:33 +00:00
|
|
|
my %usage =
|
|
|
|
(makehosts =>
|
|
|
|
"Usage: makehosts <noderange> [-d] [-n] [-l] [-a] [-o] [-m]\n makehosts -h",
|
|
|
|
);
|
2007-10-26 22:44:33 +00:00
|
|
|
|
2012-12-03 20:23:33 +00:00
|
|
|
sub handled_commands
|
|
|
|
{
|
|
|
|
return {makehosts => "hosts",};
|
2007-10-26 22:44:33 +00:00
|
|
|
}
|
2012-12-03 20:23:33 +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++;
|
2010-02-11 21:01:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-03 20:23:33 +00:00
|
|
|
sub addnode
|
|
|
|
{
|
2013-04-01 17:24:37 +00:00
|
|
|
my $callback = shift;
|
2012-12-03 20:23:33 +00:00
|
|
|
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;
|
2012-12-07 16:18:23 +00:00
|
|
|
my $nics = shift;
|
2012-12-03 20:23:33 +00:00
|
|
|
my $idx = 0;
|
|
|
|
my $foundone = 0;
|
|
|
|
|
2013-01-24 14:54:25 +00:00
|
|
|
# if this ip was already added then just update the entry
|
2012-12-03 20:23:33 +00:00
|
|
|
while ($idx <= $#hosts)
|
|
|
|
{
|
2012-12-07 16:18:23 +00:00
|
|
|
|
2012-12-03 20:23:33 +00:00
|
|
|
if ( $hosts[$idx] =~ /^${ip}\s/
|
|
|
|
or $hosts[$idx] =~ /^\d+\.\d+\.\d+\.\d+\s+${node}[\s\.r]/)
|
|
|
|
{
|
|
|
|
if ($foundone)
|
|
|
|
{
|
|
|
|
$hosts[$idx] = "";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-12-07 16:18:23 +00:00
|
|
|
# we found a matching entry in the hosts list
|
|
|
|
if ($nics) {
|
|
|
|
# we're processing the nics table and we found an
|
|
|
|
# existing entry for this ip so just add this
|
|
|
|
# node name as an alias for the existing entry
|
|
|
|
my ($hip, $hnode, $hdom, $hother)= split(/ /, $hosts[$idx]);
|
|
|
|
|
|
|
|
# at this point "othernames", if any is just a space
|
|
|
|
# delimited list - so just add the node name to the list
|
|
|
|
$othernames .= " $node";
|
2013-04-01 17:24:37 +00:00
|
|
|
$hosts[$idx] = build_line($callback, $ip, $hnode, $domain, $othernames);
|
2012-12-07 16:18:23 +00:00
|
|
|
} else {
|
|
|
|
# otherwise just try to completely update the existing
|
|
|
|
# entry
|
2013-04-01 17:24:37 +00:00
|
|
|
$hosts[$idx] = build_line($callback, $ip, $node, $domain, $othernames);
|
2012-12-07 16:18:23 +00:00
|
|
|
}
|
2012-12-03 20:23:33 +00:00
|
|
|
}
|
|
|
|
$foundone = 1;
|
|
|
|
}
|
|
|
|
$idx++;
|
2007-10-26 22:44:33 +00:00
|
|
|
}
|
2012-12-03 20:23:33 +00:00
|
|
|
if ($foundone) { return; }
|
2009-04-02 21:38:45 +00:00
|
|
|
|
2013-04-01 17:24:37 +00:00
|
|
|
my $line = build_line($callback, $ip, $node, $domain, $othernames);
|
|
|
|
if ($line) {
|
|
|
|
push @hosts, $line;
|
|
|
|
}
|
2009-04-02 21:38:45 +00:00
|
|
|
}
|
|
|
|
|
2012-12-03 20:23:33 +00:00
|
|
|
sub build_line
|
|
|
|
{
|
2013-04-01 17:24:37 +00:00
|
|
|
my $callback = shift;
|
2012-12-03 20:23:33 +00:00
|
|
|
my $ip = shift;
|
|
|
|
my $node = shift;
|
|
|
|
my $domain = shift;
|
|
|
|
my $othernames = shift;
|
|
|
|
my @o_names = ();
|
|
|
|
my @n_names = ();
|
|
|
|
if (defined $othernames)
|
|
|
|
{
|
2012-12-07 16:18:23 +00:00
|
|
|
# the "hostnames" attribute can be a list delimited by
|
|
|
|
# either a comma or a space
|
2012-12-03 20:23:33 +00:00
|
|
|
@o_names = split(/,| /, $othernames);
|
2009-04-27 03:06:42 +00:00
|
|
|
}
|
2009-04-02 21:38:45 +00:00
|
|
|
my $longname;
|
2012-12-03 20:23:33 +00:00
|
|
|
foreach (@o_names)
|
|
|
|
{
|
|
|
|
if (($_ eq $node) || ($domain && ($_ eq "$node.$domain")))
|
|
|
|
{
|
|
|
|
$longname = "$node.$domain";
|
|
|
|
$_ = "";
|
|
|
|
}
|
|
|
|
elsif ($_ =~ /\./)
|
|
|
|
{
|
|
|
|
if (!$longname)
|
|
|
|
{
|
|
|
|
$longname = $_;
|
|
|
|
$_ = "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
elsif ($ADDNAMES)
|
|
|
|
{
|
|
|
|
unshift(@n_names, "$_.$domain");
|
|
|
|
}
|
2010-01-11 18:43:18 +00:00
|
|
|
}
|
2012-12-03 20:23:33 +00:00
|
|
|
unshift(@o_names, @n_names);
|
2009-04-02 21:38:45 +00:00
|
|
|
|
2013-04-01 17:24:37 +00:00
|
|
|
my $shortname;
|
2012-12-03 20:23:33 +00:00
|
|
|
if ($node =~ m/\.$domain$/i)
|
|
|
|
{
|
2009-07-14 15:55:31 +00:00
|
|
|
$longname = $node;
|
2013-04-01 17:24:37 +00:00
|
|
|
($shortname = $node) =~ s/\.$domain$//;
|
2012-12-03 20:23:33 +00:00
|
|
|
}
|
|
|
|
elsif ($domain && !$longname)
|
|
|
|
{
|
2013-04-01 17:24:37 +00:00
|
|
|
$shortname = $node;
|
2012-12-03 20:23:33 +00:00
|
|
|
$longname = "$node.$domain";
|
|
|
|
}
|
2009-04-02 21:38:45 +00:00
|
|
|
|
2013-04-01 17:24:37 +00:00
|
|
|
# if shortname contains a dot then we have a bad syntax for name
|
|
|
|
if ($shortname =~ /\./) {
|
|
|
|
my $rsp;
|
|
|
|
push @{$rsp->{data}}, "Invalid short hostname \'$shortname\'\n";
|
|
|
|
xCAT::MsgUtils->message("E", $rsp, $callback);
|
|
|
|
return undef;
|
|
|
|
}
|
|
|
|
|
2012-12-03 20:23:33 +00:00
|
|
|
$othernames = join(' ', @o_names);
|
2013-04-01 17:24:37 +00:00
|
|
|
if ($LONGNAME) { return "$ip $longname $shortname $othernames\n"; }
|
|
|
|
elsif ($OTHERNAMESFIRST) { return "$ip $othernames $longname $shortname\n"; }
|
|
|
|
else { return "$ip $shortname $longname $othernames\n"; }
|
2007-10-26 22:44:33 +00:00
|
|
|
}
|
2009-04-02 21:38:45 +00:00
|
|
|
|
2012-12-03 20:23:33 +00:00
|
|
|
sub addotherinterfaces
|
|
|
|
{
|
2013-04-01 17:24:37 +00:00
|
|
|
my $callback = shift;
|
2012-12-03 20:23:33 +00:00
|
|
|
my $node = shift;
|
|
|
|
my $otherinterfaces = shift;
|
|
|
|
my $domain = shift;
|
2009-04-02 21:38:45 +00:00
|
|
|
|
2012-12-03 20:23:33 +00:00
|
|
|
my @itf_pairs = split(/,/, $otherinterfaces);
|
|
|
|
foreach (@itf_pairs)
|
|
|
|
{
|
2013-01-10 20:04:21 +00:00
|
|
|
my ($itf, $ip);
|
|
|
|
if ($_ =~ /!/) {
|
|
|
|
($itf, $ip) = split(/!/, $_);
|
|
|
|
} else {
|
|
|
|
($itf, $ip) = split(/:/, $_);
|
|
|
|
}
|
2012-12-03 20:23:33 +00:00
|
|
|
if ($ip && xCAT::NetworkUtils->isIpaddr($ip))
|
|
|
|
{
|
|
|
|
if ($itf =~ /^-/)
|
|
|
|
{
|
|
|
|
$itf = $node . $itf;
|
|
|
|
}
|
2013-04-01 17:24:37 +00:00
|
|
|
addnode $callback, $itf, $ip, '', $domain;
|
2012-12-03 20:23:33 +00:00
|
|
|
}
|
2009-07-06 20:39:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-03 20:23:33 +00:00
|
|
|
sub process_request
|
|
|
|
{
|
|
|
|
Getopt::Long::Configure("bundling");
|
|
|
|
$Getopt::Long::ignorecase = 0;
|
|
|
|
Getopt::Long::Configure("no_pass_through");
|
|
|
|
|
|
|
|
my $req = shift;
|
|
|
|
my $callback = shift;
|
2012-12-07 16:18:23 +00:00
|
|
|
|
2012-12-03 20:23:33 +00:00
|
|
|
my $HELP;
|
|
|
|
my $REMOVE;
|
2009-07-06 20:39:39 +00:00
|
|
|
|
2012-12-03 20:23:33 +00:00
|
|
|
# parse the options
|
|
|
|
if ($req && $req->{arg}) { @ARGV = @{$req->{arg}}; }
|
|
|
|
else { @ARGV = (); }
|
|
|
|
|
|
|
|
# print "argv=@ARGV\n";
|
|
|
|
if (
|
|
|
|
!GetOptions(
|
|
|
|
'h|help' => \$HELP,
|
|
|
|
'n' => \$REMOVE,
|
|
|
|
'd' => \$::DELNODE,
|
|
|
|
'o|othernamesfirst' => \$OTHERNAMESFIRST,
|
|
|
|
'a|adddomaintohostnames' => \$ADDNAMES,
|
|
|
|
'm|mactolinklocal' => \$MACTOLINKLOCAL,
|
|
|
|
'l|longnamefirst' => \$LONGNAME,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
{
|
|
|
|
$callback->({data => $usage{makehosts}});
|
|
|
|
return;
|
2009-03-15 18:54:05 +00:00
|
|
|
}
|
2012-12-03 20:23:33 +00:00
|
|
|
|
|
|
|
# display the usage if -h
|
|
|
|
if ($HELP)
|
|
|
|
{
|
|
|
|
$callback->({data => $usage{makehosts}});
|
|
|
|
return;
|
2007-10-26 22:44:33 +00:00
|
|
|
}
|
2012-12-03 20:23:33 +00:00
|
|
|
|
2013-03-21 18:21:32 +00:00
|
|
|
# get site domain for backward compatibility
|
|
|
|
my $sitetab = xCAT::Table->new('site');
|
|
|
|
if ($sitetab) {
|
|
|
|
my $dom = $sitetab->getAttribs({key=>'domain'},'value');
|
|
|
|
if ($dom and $dom->{value}) {
|
|
|
|
$::sitedomain=$dom->{value};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-03 20:23:33 +00:00
|
|
|
my $hoststab = xCAT::Table->new('hosts');
|
|
|
|
my $domain;
|
|
|
|
my $lockh;
|
|
|
|
|
|
|
|
@hosts = ();
|
|
|
|
if ($REMOVE)
|
|
|
|
{
|
|
|
|
if (-e "/etc/hosts")
|
|
|
|
{
|
|
|
|
my $bakname = "/etc/hosts.xcatbak";
|
|
|
|
rename("/etc/hosts", $bakname);
|
|
|
|
|
|
|
|
# add the localhost entry if trying to create the /etc/hosts from scratch
|
|
|
|
if ($^O =~ /^aix/i)
|
|
|
|
{
|
|
|
|
push @hosts, "127.0.0.1 loopback localhost\n";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
push @hosts, "127.0.0.1 localhost\n";
|
|
|
|
}
|
|
|
|
}
|
2008-06-03 20:28:01 +00:00
|
|
|
}
|
2012-12-03 20:23:33 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (-e "/etc/hosts")
|
|
|
|
{
|
|
|
|
my $bakname = "/etc/hosts.xcatbak";
|
|
|
|
copy("/etc/hosts", $bakname);
|
|
|
|
}
|
2012-12-07 16:18:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
# the contents of the /etc/hosts file is saved in the @hosts array
|
|
|
|
# the @hosts elements are updated and used to re-create the
|
|
|
|
# /etc/hosts file at the end by the writeout subroutine.
|
2012-12-03 20:23:33 +00:00
|
|
|
open($lockh, ">", "/tmp/xcat/hostsfile.lock");
|
|
|
|
flock($lockh, LOCK_EX);
|
|
|
|
my $rconf;
|
|
|
|
open($rconf, "/etc/hosts"); # Read file into memory
|
|
|
|
if ($rconf)
|
|
|
|
{
|
|
|
|
while (<$rconf>)
|
|
|
|
{
|
|
|
|
push @hosts, $_;
|
|
|
|
}
|
|
|
|
close($rconf);
|
|
|
|
}
|
2007-10-26 22:44:33 +00:00
|
|
|
}
|
|
|
|
|
2012-12-03 20:23:33 +00:00
|
|
|
if ($req->{node})
|
|
|
|
{
|
|
|
|
if ($MACTOLINKLOCAL)
|
2010-05-27 07:42:49 +00:00
|
|
|
{
|
2012-12-03 20:23:33 +00:00
|
|
|
my $mactab = xCAT::Table->new("mac");
|
|
|
|
my $machash = $mactab->getNodesAttribs($req->{node}, ['mac']);
|
|
|
|
foreach my $node (keys %{$machash})
|
|
|
|
{
|
|
|
|
|
|
|
|
my $mac = $machash->{$node}->[0]->{mac};
|
|
|
|
if (!$mac)
|
|
|
|
{
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
my $linklocal = xCAT::NetworkUtils->linklocaladdr($mac);
|
|
|
|
|
2012-12-07 16:18:23 +00:00
|
|
|
my $netn;
|
|
|
|
($domain, $netn) = &getIPdomain($linklocal, $callback);
|
2013-03-21 18:21:32 +00:00
|
|
|
if (!$domain) {
|
|
|
|
$domain=$::sitedomain;
|
|
|
|
}
|
2012-12-03 20:23:33 +00:00
|
|
|
|
|
|
|
if ($::DELNODE)
|
|
|
|
{
|
|
|
|
delnode $node, $linklocal, $node, $domain;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-04-01 17:24:37 +00:00
|
|
|
addnode $callback, $node, $linklocal, $node, $domain;
|
2012-12-03 20:23:33 +00:00
|
|
|
}
|
2010-05-27 07:42:49 +00:00
|
|
|
}
|
2010-02-11 21:01:24 +00:00
|
|
|
}
|
2012-12-03 20:23:33 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
my $hostscache =
|
|
|
|
$hoststab->getNodesAttribs($req->{node},
|
|
|
|
[qw(ip node hostnames otherinterfaces)]);
|
|
|
|
foreach (@{$req->{node}})
|
|
|
|
{
|
|
|
|
|
|
|
|
my $ref = $hostscache->{$_}->[0];
|
2012-12-07 16:18:23 +00:00
|
|
|
|
|
|
|
my $netn;
|
|
|
|
($domain, $netn) = &getIPdomain($ref->{ip}, $callback);
|
2013-03-21 18:21:32 +00:00
|
|
|
if (!$domain) {
|
|
|
|
$domain=$::sitedomain;
|
|
|
|
}
|
2012-12-03 20:23:33 +00:00
|
|
|
|
|
|
|
if ($::DELNODE)
|
|
|
|
{
|
|
|
|
delnode $ref->{node}, $ref->{ip}, $ref->{hostnames}, $domain;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (xCAT::NetworkUtils->isIpaddr($ref->{ip}))
|
|
|
|
{
|
2013-04-01 17:24:37 +00:00
|
|
|
addnode $callback, $ref->{node}, $ref->{ip}, $ref->{hostnames}, $domain;
|
2012-12-03 20:23:33 +00:00
|
|
|
}
|
|
|
|
if (defined($ref->{otherinterfaces}))
|
|
|
|
{
|
2013-04-01 17:24:37 +00:00
|
|
|
addotherinterfaces $callback, $ref->{node}, $ref->{otherinterfaces}, $domain;
|
2012-12-03 20:23:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} #end foreach
|
|
|
|
} # end else
|
|
|
|
|
|
|
|
# do the other node nics - if any
|
|
|
|
&donics($req->{node}, $callback);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ($::DELNODE)
|
|
|
|
{
|
|
|
|
return;
|
2011-09-06 14:14:14 +00:00
|
|
|
}
|
2012-12-03 20:23:33 +00:00
|
|
|
my @hostents =
|
|
|
|
$hoststab->getAllNodeAttribs(
|
|
|
|
['ip', 'node', 'hostnames', 'otherinterfaces']);
|
|
|
|
|
|
|
|
my @allnodes;
|
|
|
|
foreach (@hostents)
|
|
|
|
{
|
|
|
|
|
|
|
|
push @allnodes, $_->{node};
|
|
|
|
|
2012-12-07 16:18:23 +00:00
|
|
|
my $netn;
|
|
|
|
($domain, $netn) = &getIPdomain($_->{ip});
|
2013-03-21 18:21:32 +00:00
|
|
|
if (!$domain) {
|
|
|
|
$domain=$::sitedomain;
|
|
|
|
}
|
2012-12-03 20:23:33 +00:00
|
|
|
|
|
|
|
if (xCAT::NetworkUtils->isIpaddr($_->{ip}))
|
|
|
|
{
|
2013-04-01 17:24:37 +00:00
|
|
|
addnode $callback, $_->{node}, $_->{ip}, $_->{hostnames}, $domain;
|
2012-12-03 20:23:33 +00:00
|
|
|
}
|
|
|
|
if (defined($_->{otherinterfaces}))
|
|
|
|
{
|
2013-04-01 17:24:37 +00:00
|
|
|
addotherinterfaces $callback, $_->{node}, $_->{otherinterfaces}, $domain;
|
2012-12-03 20:23:33 +00:00
|
|
|
}
|
2011-05-18 01:31:14 +00:00
|
|
|
}
|
2012-12-03 20:23:33 +00:00
|
|
|
|
|
|
|
# also do nics table
|
|
|
|
&donics(\@allnodes, $callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
writeout();
|
|
|
|
|
|
|
|
if ($lockh)
|
|
|
|
{
|
|
|
|
flock($lockh, LOCK_UN);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub writeout
|
|
|
|
{
|
|
|
|
my $targ;
|
|
|
|
open($targ, '>', "/etc/hosts");
|
|
|
|
foreach (@hosts)
|
|
|
|
{
|
|
|
|
print $targ $_;
|
2007-10-26 22:44:33 +00:00
|
|
|
}
|
2012-12-03 20:23:33 +00:00
|
|
|
close($targ);
|
2007-10-26 22:44:33 +00:00
|
|
|
}
|
|
|
|
|
2012-12-03 20:23:33 +00:00
|
|
|
#-------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
=head3 donics
|
|
|
|
|
|
|
|
Add the additional network interfaces for a list of nodes as
|
|
|
|
indicated in the nics table
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
node name
|
|
|
|
Returns:
|
|
|
|
0 - ok
|
|
|
|
1 - error
|
|
|
|
|
|
|
|
Globals:
|
|
|
|
|
|
|
|
Example:
|
2013-01-24 14:54:25 +00:00
|
|
|
my $rc = &donics(\@nodes, $callback);
|
2012-12-03 20:23:33 +00:00
|
|
|
|
|
|
|
Comments:
|
|
|
|
none
|
|
|
|
=cut
|
|
|
|
|
|
|
|
#-------------------------------------------------------------------------------
|
|
|
|
sub donics
|
|
|
|
{
|
|
|
|
my $nodes = shift;
|
|
|
|
my $callback = shift;
|
|
|
|
|
|
|
|
my @nodelist = @{$nodes};
|
|
|
|
|
|
|
|
my $nicstab = xCAT::Table->new('nics');
|
|
|
|
my $nettab = xCAT::Table->new('networks');
|
|
|
|
|
|
|
|
foreach my $node (@nodelist)
|
|
|
|
{
|
2013-01-24 14:54:25 +00:00
|
|
|
my $nich;
|
|
|
|
my %nicindex;
|
2012-12-03 20:23:33 +00:00
|
|
|
|
|
|
|
# get the nic info
|
|
|
|
my $et =
|
|
|
|
$nicstab->getNodeAttribs(
|
|
|
|
$node,
|
|
|
|
[
|
|
|
|
'nicips', 'nichostnamesuffixes',
|
2013-03-22 14:25:53 +00:00
|
|
|
'nicnetworks', 'nicaliases'
|
2012-12-03 20:23:33 +00:00
|
|
|
]
|
|
|
|
);
|
|
|
|
|
2013-03-29 13:19:19 +00:00
|
|
|
# only require IP for nic
|
|
|
|
if ( !($et->{nicips}) ) {
|
2012-12-03 20:23:33 +00:00
|
|
|
next;
|
|
|
|
}
|
|
|
|
|
2013-01-24 14:54:25 +00:00
|
|
|
# gather nics info
|
2013-01-10 20:04:21 +00:00
|
|
|
# delimiter could be ":" or "!"
|
2013-01-24 14:54:25 +00:00
|
|
|
# new $et->{nicips} looks like
|
|
|
|
# "eth0!11.10.1.1,eth1!60.0.0.5|60.0.0.250..."
|
2012-12-03 20:23:33 +00:00
|
|
|
my @nicandiplist = split(',', $et->{'nicips'});
|
2013-03-29 13:19:19 +00:00
|
|
|
|
2012-12-03 20:23:33 +00:00
|
|
|
foreach (@nicandiplist)
|
|
|
|
{
|
2013-01-10 20:04:21 +00:00
|
|
|
my ($nicname, $nicip);
|
|
|
|
|
|
|
|
# if it contains a "!" then split on "!"
|
|
|
|
if ($_ =~ /!/) {
|
|
|
|
($nicname, $nicip) = split('!', $_);
|
|
|
|
} else {
|
|
|
|
($nicname, $nicip) = split(':', $_);
|
|
|
|
}
|
2013-01-24 14:54:25 +00:00
|
|
|
|
|
|
|
$nicindex{$nicname}=0;
|
|
|
|
|
|
|
|
if (!$nicip) {
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $nicip =~ /\|/) {
|
|
|
|
my @ips = split( /\|/, $nicip);
|
|
|
|
foreach my $ip (@ips) {
|
|
|
|
$nich->{$nicname}->{nicip}->[$nicindex{$nicname}] = $ip;
|
|
|
|
$nicindex{$nicname}++;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$nich->{$nicname}->{nicip}->[$nicindex{$nicname}] = $nicip;
|
|
|
|
$nicindex{$nicname}++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-03 20:23:33 +00:00
|
|
|
my @nicandsufx = split(',', $et->{'nichostnamesuffixes'});
|
2013-03-29 13:19:19 +00:00
|
|
|
|
2012-12-03 20:23:33 +00:00
|
|
|
foreach (@nicandsufx)
|
|
|
|
{
|
2013-01-10 20:04:21 +00:00
|
|
|
my ($nicname, $nicsufx);
|
|
|
|
if ($_ =~ /!/) {
|
|
|
|
($nicname, $nicsufx) = split('!', $_);
|
|
|
|
} else {
|
|
|
|
($nicname, $nicsufx) = split(':', $_);
|
|
|
|
}
|
2013-01-24 14:54:25 +00:00
|
|
|
|
|
|
|
if ( $nicsufx =~ /\|/) {
|
|
|
|
my @sufs = split( /\|/, $nicsufx);
|
|
|
|
my $index=0;
|
|
|
|
foreach my $suf (@sufs) {
|
|
|
|
$nich->{$nicname}->{nicsufx}->[$index] = $suf;
|
|
|
|
$index++;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$nich->{$nicname}->{nicsufx}->[0] = $nicsufx;
|
|
|
|
}
|
2012-12-03 20:23:33 +00:00
|
|
|
}
|
2013-01-24 14:54:25 +00:00
|
|
|
|
2013-03-29 13:19:19 +00:00
|
|
|
# see if we need to fill in a default suffix
|
|
|
|
# nich has all the valid nics - ie. that have IPs provided!
|
|
|
|
foreach my $nic (keys %{$nich}) {
|
|
|
|
for (my $i = 0; $i < $nicindex{$nic}; $i++ ){
|
|
|
|
if (!$nich->{$nic}->{nicsufx}->[$i]) {
|
|
|
|
# then we have no suffix at all for this
|
|
|
|
# so set a default
|
|
|
|
$nich->{$nic}->{nicsufx}->[$i] = "-$nic";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-03 20:23:33 +00:00
|
|
|
my @nicandnetwrk = split(',', $et->{'nicnetworks'});
|
|
|
|
foreach (@nicandnetwrk)
|
|
|
|
{
|
2013-01-10 20:04:21 +00:00
|
|
|
my ($nicname, $netwrk);
|
|
|
|
if ($_ =~ /!/) {
|
|
|
|
($nicname, $netwrk) = split('!', $_);
|
|
|
|
} else {
|
|
|
|
($nicname, $netwrk) = split(':', $_);
|
|
|
|
}
|
2013-03-29 13:19:19 +00:00
|
|
|
|
2013-01-24 14:54:25 +00:00
|
|
|
if (!$netwrk) {
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $netwrk =~ /\|/) {
|
|
|
|
my @nets = split( /\|/, $netwrk);
|
|
|
|
my $index=0;
|
|
|
|
foreach my $net (@nets) {
|
|
|
|
$nich->{$nicname}->{netwrk}->[$index] = $net;
|
|
|
|
$index++;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$nich->{$nicname}->{netwrk}->[0] = $netwrk;
|
|
|
|
}
|
2012-12-03 20:23:33 +00:00
|
|
|
}
|
2013-03-29 13:19:19 +00:00
|
|
|
|
2013-03-22 14:25:53 +00:00
|
|
|
my @nicandnicalias = split(',', $et->{'nicaliases'});
|
|
|
|
foreach (@nicandnicalias)
|
|
|
|
{
|
|
|
|
my ($nicname, $aliases);
|
|
|
|
if ($_ =~ /!/) {
|
|
|
|
($nicname, $aliases) = split('!', $_);
|
|
|
|
} else {
|
|
|
|
($nicname, $aliases) = split(':', $_);
|
|
|
|
}
|
|
|
|
if (!$aliases) {
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $aliases =~ /\|/) {
|
|
|
|
my @names = split( /\|/, $aliases);
|
|
|
|
my $index=0;
|
|
|
|
foreach my $alias (@names) {
|
|
|
|
$nich->{$nicname}->{nicaliases}->[$index] = $alias;
|
|
|
|
$index++;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$nich->{$nicname}->{nicaliases}->[0] = $aliases;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-24 14:54:25 +00:00
|
|
|
# end gather nics info
|
2012-12-03 20:23:33 +00:00
|
|
|
|
2013-01-24 14:54:25 +00:00
|
|
|
# add or delete nic entries in the hosts file
|
|
|
|
foreach my $nic (keys %{$nich}) {
|
2012-12-03 20:23:33 +00:00
|
|
|
# make sure we have the short hostname
|
|
|
|
my $shorthost;
|
|
|
|
($shorthost = $node) =~ s/\..*$//;
|
|
|
|
|
2013-01-24 14:54:25 +00:00
|
|
|
for (my $i = 0; $i < $nicindex{$nic}; $i++ ){
|
2012-12-03 20:23:33 +00:00
|
|
|
|
2013-01-24 14:54:25 +00:00
|
|
|
my $nicip = $nich->{$nic}->{nicip}->[$i];
|
|
|
|
my $nicsuffix = $nich->{$nic}->{nicsufx}->[$i];
|
|
|
|
my $nicnetworks = $nich->{$nic}->{netwrk}->[$i];
|
2013-03-22 14:25:53 +00:00
|
|
|
my $nicaliases = $nich->{$nic}->{nicaliases}->[$i];
|
2012-12-03 20:23:33 +00:00
|
|
|
|
2013-01-24 14:54:25 +00:00
|
|
|
if (!$nicip) {
|
|
|
|
next;
|
2012-12-07 16:18:23 +00:00
|
|
|
}
|
|
|
|
|
2013-01-24 14:54:25 +00:00
|
|
|
# construct hostname for nic
|
|
|
|
my $nichostname = "$shorthost$nicsuffix";
|
|
|
|
|
2013-03-21 18:21:32 +00:00
|
|
|
# get domain from network def provided by nic attr
|
|
|
|
my $nt = $nettab->getAttribs({ netname => "$nicnetworks"}, 'domain');
|
2013-01-24 14:54:25 +00:00
|
|
|
# look up the domain as a check or if it's not provided
|
2013-03-21 18:21:32 +00:00
|
|
|
my ($ndomain, $netn) = &getIPdomain($nicip, $callback);
|
|
|
|
|
|
|
|
if ( $nt->{domain} && $ndomain ) {
|
|
|
|
# if they don't match we may have a problem.
|
|
|
|
if($nicnetworks ne $netn) {
|
2013-01-24 14:54:25 +00:00
|
|
|
my $rsp;
|
2013-03-21 18:21:32 +00:00
|
|
|
push @{$rsp->{data}}, "The xCAT network name listed for
|
|
|
|
\'$nichostname\' is \'$nicnetworks\' however the nic IP address \'$nicip\' seems to be in the \'$netn\' network.\nIf there is an error then makes corrections to the database definitions and re-run this command.\n";
|
2013-01-24 14:54:25 +00:00
|
|
|
xCAT::MsgUtils->message("W", $rsp, $callback);
|
|
|
|
}
|
2013-03-21 18:21:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# choose a domain
|
|
|
|
my $nicdomain;
|
|
|
|
if ( $ndomain ) {
|
|
|
|
# try the one based on the ip address first
|
|
|
|
$nicdomain=$ndomain;
|
|
|
|
} elsif ( $nt->{domain} ) {
|
|
|
|
# then try the one provides in the nics entry
|
|
|
|
$nicdomain=$nt->{domain};
|
|
|
|
} else {
|
|
|
|
# try the site domain for backward compatibility
|
|
|
|
$nicdomain=$::sitedomain;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!$nicdomain) {
|
|
|
|
my $rsp;
|
|
|
|
push @{$rsp->{data}}, "Could not find a domain name for $nic/$nicip.\n";
|
|
|
|
xCAT::MsgUtils->message("W", $rsp, $callback);
|
|
|
|
next;
|
2013-01-24 14:54:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($::DELNODE)
|
|
|
|
{
|
|
|
|
delnode $nichostname, $nicip, '', $nicdomain;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-04-01 17:24:37 +00:00
|
|
|
addnode $callback, $nichostname, $nicip, $nicaliases, $nicdomain, 1;
|
2013-01-24 14:54:25 +00:00
|
|
|
}
|
|
|
|
} # end for each index
|
2012-12-03 20:23:33 +00:00
|
|
|
} # end for each nic
|
|
|
|
} # end for each node
|
|
|
|
|
|
|
|
$nettab->close;
|
|
|
|
$nicstab->close;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#-------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
=head3 getIPdomain
|
|
|
|
|
|
|
|
Find the xCAT network definition match the IP and then return the
|
|
|
|
domain value from that network def.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
node IP
|
|
|
|
callback
|
|
|
|
Returns:
|
2012-12-07 16:18:23 +00:00
|
|
|
domain and netname - ok
|
2013-03-21 18:21:32 +00:00
|
|
|
undef - error
|
2012-12-03 20:23:33 +00:00
|
|
|
|
|
|
|
Globals:
|
|
|
|
|
|
|
|
Example:
|
|
|
|
my $rc = &getIPdomain($nodeIP, $callback);
|
|
|
|
|
|
|
|
Comments:
|
|
|
|
none
|
|
|
|
=cut
|
|
|
|
|
|
|
|
#-------------------------------------------------------------------------------
|
|
|
|
sub getIPdomain
|
|
|
|
{
|
|
|
|
my $nodeIP = shift;
|
|
|
|
my $callback = shift;
|
|
|
|
|
|
|
|
# get the network defs
|
|
|
|
my $nettab = xCAT::Table->new('networks');
|
|
|
|
my @nets = $nettab->getAllAttribs('netname', 'net', 'mask', 'domain');
|
|
|
|
|
|
|
|
# foreach network def
|
|
|
|
foreach my $enet (@nets)
|
|
|
|
{
|
|
|
|
my $NM = $enet->{'mask'};
|
|
|
|
my $net = $enet->{'net'};
|
|
|
|
if (xCAT::NetworkUtils->ishostinsubnet($nodeIP, $NM, $net))
|
|
|
|
{
|
2012-12-07 16:18:23 +00:00
|
|
|
return ($enet->{'domain'}, $enet->{'netname'});
|
2012-12-03 20:23:33 +00:00
|
|
|
last;
|
|
|
|
}
|
|
|
|
}
|
2007-10-26 22:44:33 +00:00
|
|
|
|
2012-12-03 20:23:33 +00:00
|
|
|
# could not find the network domain for this IP address
|
2013-03-21 18:21:32 +00:00
|
|
|
return undef;
|
2007-10-26 22:44:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
1;
|