2007-10-26 22:44:33 +00:00
|
|
|
#!/usr/bin/env perl
|
|
|
|
# IBM(c) 2007 EPL license http://www.eclipse.org/legal/epl-v10.html
|
|
|
|
package xCAT::Utils;
|
2008-04-05 14:53:35 +00:00
|
|
|
require xCAT::Table;
|
2008-02-25 14:18:18 +00:00
|
|
|
use POSIX qw(ceil);
|
2008-02-02 19:39:33 +00:00
|
|
|
use Socket;
|
2008-04-05 14:53:35 +00:00
|
|
|
require xCAT::Schema;
|
|
|
|
require Data::Dumper;
|
|
|
|
require xCAT::NodeRange;
|
|
|
|
require DBI;
|
2008-05-07 17:35:14 +00:00
|
|
|
|
2008-05-13 12:36:13 +00:00
|
|
|
our @ISA = qw(Exporter);
|
2008-05-07 14:51:12 +00:00
|
|
|
our @EXPORT_OK = qw(genpassword);
|
|
|
|
|
2008-05-07 17:35:14 +00:00
|
|
|
#--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
=head1 xCAT::Utils
|
|
|
|
|
|
|
|
=head2 Package Description
|
|
|
|
|
|
|
|
This program module file, is a set of utilities used by xCAT commands.
|
|
|
|
|
|
|
|
=cut
|
|
|
|
|
2008-05-07 14:51:12 +00:00
|
|
|
#--------------------------------------------------------------------------------
|
2008-05-13 12:36:13 +00:00
|
|
|
|
2008-05-07 14:51:12 +00:00
|
|
|
=head3 genpassword
|
|
|
|
returns a random string of specified length or 8 if none given
|
|
|
|
Arguments:
|
|
|
|
length of string requested
|
|
|
|
Returns:
|
|
|
|
string of requested length or 8
|
|
|
|
Globals:
|
|
|
|
none
|
|
|
|
Error:
|
|
|
|
none
|
|
|
|
Example:
|
|
|
|
my $salt = genpassword(8);
|
|
|
|
Comments:
|
|
|
|
none
|
|
|
|
=cut
|
|
|
|
|
|
|
|
#--------------------------------------------------------------------------------
|
2008-05-13 12:36:13 +00:00
|
|
|
sub genpassword
|
|
|
|
{
|
|
|
|
|
|
|
|
#Generate a pseudo-random password of specified length
|
|
|
|
my $length = shift;
|
|
|
|
unless ($length) { $length = 8; }
|
|
|
|
my $password = '';
|
|
|
|
my $characters =
|
|
|
|
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890';
|
|
|
|
srand; #have to reseed, rand is not rand otherwise
|
|
|
|
while (length($password) < $length)
|
|
|
|
{
|
|
|
|
$password .= substr($characters, int(rand 63), 1);
|
2008-05-07 14:51:12 +00:00
|
|
|
}
|
|
|
|
return $password;
|
|
|
|
}
|
|
|
|
|
2007-10-26 22:44:33 +00:00
|
|
|
#--------------------------------------------------------------------------------
|
2007-12-20 19:02:45 +00:00
|
|
|
|
2007-10-26 22:44:33 +00:00
|
|
|
=head3 quote
|
|
|
|
|
|
|
|
Quote a string, taking into account embedded quotes. This function is most
|
|
|
|
useful when passing string through the shell to another cmd. It handles one
|
|
|
|
level of embedded double quotes, single quotes, and dollar signs.
|
|
|
|
Arguments:
|
|
|
|
string to quote
|
|
|
|
Returns:
|
|
|
|
quoted string
|
|
|
|
Globals:
|
|
|
|
none
|
|
|
|
Error:
|
|
|
|
none
|
|
|
|
Example:
|
|
|
|
if (defined($$opthashref{'WhereStr'})) {
|
|
|
|
$where = xCAT::Utils->quote($$opthashref{'WhereStr'});
|
|
|
|
}
|
|
|
|
Comments:
|
|
|
|
none
|
|
|
|
=cut
|
2007-12-20 19:02:45 +00:00
|
|
|
|
2007-10-26 22:44:33 +00:00
|
|
|
#--------------------------------------------------------------------------------
|
|
|
|
sub quote
|
|
|
|
{
|
|
|
|
my ($class, $str) = @_;
|
|
|
|
|
|
|
|
# if the value has imbedded double quotes, use single quotes. If it also has
|
|
|
|
# single quotes, escape the double quotes.
|
|
|
|
if (!($str =~ /\"/)) # no embedded double quotes
|
|
|
|
{
|
|
|
|
$str =~ s/\$/\\\$/sg; # escape the dollar signs
|
|
|
|
$str =~ s/\`/\\\`/sg;
|
|
|
|
$str = qq("$str");
|
|
|
|
}
|
|
|
|
elsif (!($str =~ /\'/))
|
|
|
|
{
|
|
|
|
$str = qq('$str');
|
|
|
|
} # no embedded single quotes
|
|
|
|
else # has both embedded double and single quotes
|
|
|
|
{
|
|
|
|
|
|
|
|
# Escape the double quotes. (Escaping single quotes does not seem to work
|
|
|
|
# in the shells.)
|
|
|
|
$str =~ s/\"/\\\"/sg; #" this comment helps formating
|
|
|
|
$str =~ s/\$/\\\$/sg; # escape the dollar signs
|
|
|
|
$str =~ s/\`/\\\`/sg;
|
|
|
|
$str = qq("$str");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#-------------------------------------------------------------------------------
|
2007-12-20 19:02:45 +00:00
|
|
|
|
2007-10-26 22:44:33 +00:00
|
|
|
=head3 isAIX
|
|
|
|
returns 1 if localHost is AIX
|
|
|
|
Arguments:
|
|
|
|
none
|
|
|
|
Returns:
|
|
|
|
1 - localHost is AIX
|
|
|
|
0 - localHost is some other platform
|
|
|
|
Globals:
|
|
|
|
none
|
|
|
|
Error:
|
|
|
|
none
|
|
|
|
Example:
|
|
|
|
if (xCAT::Utils->isAIX()) { blah; }
|
|
|
|
Comments:
|
|
|
|
none
|
|
|
|
=cut
|
|
|
|
|
|
|
|
#-------------------------------------------------------------------------------
|
|
|
|
sub isAIX
|
|
|
|
{
|
|
|
|
if ($^O =~ /^aix/i) { return 1; }
|
|
|
|
else { return 0; }
|
|
|
|
}
|
|
|
|
|
2008-01-21 19:39:09 +00:00
|
|
|
#-------------------------------------------------------------------------------
|
|
|
|
|
2008-01-21 19:49:59 +00:00
|
|
|
=head3 xfork
|
2008-01-21 19:39:09 +00:00
|
|
|
forks, safely coping with open database handles
|
|
|
|
Argumens:
|
|
|
|
none
|
|
|
|
Returns:
|
|
|
|
same as fork
|
|
|
|
=cut
|
2008-02-04 20:02:15 +00:00
|
|
|
|
2008-03-26 18:20:05 +00:00
|
|
|
#-------------------------------------------------------------------------------
|
|
|
|
|
2008-02-04 20:02:15 +00:00
|
|
|
sub xfork
|
|
|
|
{
|
|
|
|
my $rc = fork;
|
|
|
|
unless (defined($rc))
|
|
|
|
{
|
|
|
|
return $rc;
|
|
|
|
}
|
|
|
|
unless ($rc)
|
|
|
|
{
|
|
|
|
|
|
|
|
#my %drivers = DBI->installed_drivers;
|
|
|
|
foreach (values %{$::XCAT_DBHS})
|
|
|
|
{ #@{$drh->{ChildHandles}}) {
|
|
|
|
$_->{InactiveDestroy} = 1;
|
|
|
|
undef $_;
|
2008-01-21 19:39:09 +00:00
|
|
|
}
|
2008-02-04 20:02:15 +00:00
|
|
|
}
|
|
|
|
return $rc;
|
2008-01-21 19:39:09 +00:00
|
|
|
}
|
|
|
|
|
2008-03-26 18:20:05 +00:00
|
|
|
sub close_all_dbhs
|
|
|
|
{
|
|
|
|
foreach (values %{$::XCAT_DBHS})
|
|
|
|
{ #@{$drh->{ChildHandles}}) {
|
|
|
|
$_->disconnect;
|
|
|
|
undef $_;
|
|
|
|
}
|
2008-03-11 20:27:46 +00:00
|
|
|
}
|
2008-03-26 18:20:05 +00:00
|
|
|
|
2007-10-26 22:44:33 +00:00
|
|
|
#-------------------------------------------------------------------------------
|
2007-12-20 19:02:45 +00:00
|
|
|
|
2007-10-26 22:44:33 +00:00
|
|
|
=head3 isLinux
|
|
|
|
returns 1 if localHost is Linux
|
|
|
|
Arguments:
|
|
|
|
none
|
|
|
|
Returns:
|
|
|
|
1 - localHost is Linux
|
|
|
|
0 - localHost is some other platform
|
|
|
|
Globals:
|
|
|
|
none
|
|
|
|
Error:
|
|
|
|
none
|
|
|
|
Example:
|
|
|
|
if (xCAT::Utils->isLinux()) { blah; }
|
|
|
|
Comments:
|
|
|
|
none
|
|
|
|
=cut
|
2007-12-20 19:02:45 +00:00
|
|
|
|
2007-10-26 22:44:33 +00:00
|
|
|
#-------------------------------------------------------------------------------
|
|
|
|
sub isLinux
|
|
|
|
{
|
|
|
|
if ($^O =~ /^linux/i) { return 1; }
|
|
|
|
else { return 0; }
|
|
|
|
}
|
|
|
|
|
|
|
|
#-------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
=head3 make_node_list_file
|
|
|
|
|
2008-04-05 14:53:35 +00:00
|
|
|
Makes a node list file.
|
2007-10-26 22:44:33 +00:00
|
|
|
|
|
|
|
Arguments:
|
|
|
|
(\@list_of_nodes) - reference to an arrary of nodes.
|
|
|
|
Returns:
|
|
|
|
$file_name and sets the global var: $::NODE_LIST_FILE
|
|
|
|
Globals:
|
|
|
|
the ENV vars: DSH_LIST, RPOWER_LIST, RCONSOLE_LIST
|
|
|
|
Error:
|
|
|
|
None documented
|
|
|
|
Example:
|
2008-04-05 14:53:35 +00:00
|
|
|
xCAT::Utils->make_node_list_file(\@nodelist);
|
2007-10-26 22:44:33 +00:00
|
|
|
|
|
|
|
Comments:
|
|
|
|
IMPORTANT:
|
|
|
|
Make sure to cleanup afterwards with:
|
|
|
|
|
|
|
|
xCAT::Utils->close_delete_file($file_handle, $file_name)
|
|
|
|
|
|
|
|
=cut
|
|
|
|
|
|
|
|
#--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
sub make_node_list_file
|
|
|
|
{
|
|
|
|
my ($class, $ref_node_list) = @_;
|
|
|
|
my @node_list = @$ref_node_list;
|
|
|
|
srand(time | $$); #random number generator start
|
|
|
|
|
|
|
|
my $file = "/tmp/csm_$$";
|
|
|
|
while (-e $file)
|
|
|
|
{
|
|
|
|
$file = xCAT::Utils->CreateRandomName($file);
|
|
|
|
}
|
|
|
|
|
|
|
|
open($::NODE_LIST_FILE, ">$file")
|
|
|
|
or MsgUtils->message("E", "Cannot write to file: $file\n");
|
|
|
|
foreach my $node (@node_list)
|
|
|
|
{
|
|
|
|
print $::NODE_LIST_FILE "$node\n";
|
|
|
|
}
|
|
|
|
return $file;
|
|
|
|
}
|
|
|
|
|
|
|
|
#--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
=head3 CreateRandomName
|
|
|
|
|
|
|
|
Create a randome file name.
|
|
|
|
Arguments:
|
|
|
|
Prefix of name
|
|
|
|
Returns:
|
|
|
|
Prefix with 8 random letters appended
|
|
|
|
Error:
|
|
|
|
none
|
|
|
|
Example:
|
|
|
|
$file = xCAT::Utils->CreateRandomName($namePrefix);
|
|
|
|
Comments:
|
|
|
|
None
|
|
|
|
=cut
|
|
|
|
|
|
|
|
#-------------------------------------------------------------------------------
|
|
|
|
sub CreateRandomName
|
|
|
|
{
|
|
|
|
my ($class, $name) = @_;
|
|
|
|
|
|
|
|
my $nI;
|
|
|
|
for ($nI = 0 ; $nI < 8 ; $nI++)
|
|
|
|
{
|
|
|
|
my $char = ('a' .. 'z', 'A' .. 'Z')[int(rand(52)) + 1];
|
|
|
|
$name .= $char;
|
|
|
|
}
|
|
|
|
$name;
|
|
|
|
}
|
|
|
|
|
|
|
|
#-----------------------------------------------------------------------
|
|
|
|
|
2008-04-05 14:53:35 +00:00
|
|
|
=head3
|
2007-10-26 22:44:33 +00:00
|
|
|
close_delete_file.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
file handle,filename
|
|
|
|
Returns:
|
2008-04-05 14:53:35 +00:00
|
|
|
none
|
2007-10-26 22:44:33 +00:00
|
|
|
Globals:
|
|
|
|
none
|
|
|
|
Error:
|
|
|
|
undef
|
|
|
|
Example:
|
|
|
|
xCAT::Utils->close_delete_file($file_handle, $file_name);
|
|
|
|
Comments:
|
|
|
|
none
|
|
|
|
|
|
|
|
=cut
|
|
|
|
|
|
|
|
#------------------------------------------------------------------------
|
|
|
|
sub close_delete_file
|
|
|
|
{
|
|
|
|
my ($class, $file_handle, $file_name) = @_;
|
|
|
|
close $file_handle;
|
|
|
|
|
|
|
|
unlink($file_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
#-----------------------------------------------------------------------
|
|
|
|
|
2008-04-05 14:53:35 +00:00
|
|
|
=head3
|
2007-10-26 22:44:33 +00:00
|
|
|
list_all_nodes
|
|
|
|
|
|
|
|
Arguments:
|
2008-04-05 14:53:35 +00:00
|
|
|
|
2007-10-26 22:44:33 +00:00
|
|
|
Returns:
|
2008-04-05 14:53:35 +00:00
|
|
|
an array of all define nodes from the nodelist table
|
2007-10-26 22:44:33 +00:00
|
|
|
Globals:
|
|
|
|
none
|
|
|
|
Error:
|
|
|
|
undef
|
|
|
|
Example:
|
|
|
|
@nodes=xCAT::Utils->list_all_nodes;
|
|
|
|
Comments:
|
|
|
|
none
|
|
|
|
|
|
|
|
=cut
|
|
|
|
|
|
|
|
#------------------------------------------------------------------------
|
|
|
|
sub list_all_nodes
|
|
|
|
{
|
|
|
|
my @nodes;
|
|
|
|
my @nodelist;
|
|
|
|
my $nodelisttab;
|
|
|
|
if ($nodelisttab = xCAT::Table->new("nodelist"))
|
|
|
|
{
|
|
|
|
my @attribs = ("node");
|
|
|
|
@nodes = $nodelisttab->getAllAttribs(@attribs);
|
|
|
|
foreach my $node (@nodes)
|
|
|
|
{
|
|
|
|
push @nodelist, $node->{node};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
xCAT::MsgUtils->message("E", " Could not read the nodelist table\n");
|
|
|
|
}
|
|
|
|
return @nodelist;
|
|
|
|
}
|
|
|
|
|
|
|
|
#-----------------------------------------------------------------------
|
|
|
|
|
2008-04-05 14:53:35 +00:00
|
|
|
=head3
|
2007-10-26 22:44:33 +00:00
|
|
|
list_all_nodegroups
|
|
|
|
|
|
|
|
Arguments:
|
2008-04-05 14:53:35 +00:00
|
|
|
|
2007-10-26 22:44:33 +00:00
|
|
|
Returns:
|
2008-04-05 14:53:35 +00:00
|
|
|
an array of all define node groups from the nodelist table
|
2007-10-26 22:44:33 +00:00
|
|
|
Globals:
|
|
|
|
none
|
|
|
|
Error:
|
|
|
|
undef
|
|
|
|
Example:
|
|
|
|
@nodegrps=xCAT::Utils->list_all_nodegroups;
|
|
|
|
Comments:
|
|
|
|
none
|
|
|
|
|
|
|
|
=cut
|
|
|
|
|
|
|
|
#------------------------------------------------------------------------
|
|
|
|
sub list_all_node_groups
|
|
|
|
{
|
|
|
|
my @grouplist;
|
|
|
|
my @grouplist2;
|
|
|
|
my @distinctgroups;
|
|
|
|
my $nodelisttab;
|
|
|
|
if ($nodelisttab = xCAT::Table->new("nodelist"))
|
|
|
|
{
|
|
|
|
my @attribs = ("groups");
|
|
|
|
@grouplist = $nodelisttab->getAllAttribs(@attribs);
|
|
|
|
|
|
|
|
# build a distinct list of unique group names
|
|
|
|
foreach my $group (@grouplist)
|
|
|
|
{
|
|
|
|
my $gnames = $group->{groups};
|
|
|
|
my @groupnames = split ",", $gnames;
|
|
|
|
foreach my $groupname (@groupnames)
|
|
|
|
{
|
|
|
|
if (!grep(/$groupname/, @distinctgroups))
|
|
|
|
{ # not already in list
|
|
|
|
push @distinctgroups, $groupname;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
xCAT::MsgUtils->message("E", " Could not read the nodelist table\n");
|
|
|
|
}
|
2008-04-22 13:24:38 +00:00
|
|
|
$nodelisttab->close;
|
2007-10-26 22:44:33 +00:00
|
|
|
return @distinctgroups;
|
|
|
|
}
|
|
|
|
|
|
|
|
#-----------------------------------------------------------------------
|
|
|
|
|
2008-04-05 14:53:35 +00:00
|
|
|
=head3
|
2007-10-26 22:44:33 +00:00
|
|
|
list_nodes_in_nodegroup
|
|
|
|
|
|
|
|
Arguments: nodegroup
|
2008-04-05 14:53:35 +00:00
|
|
|
|
2007-10-26 22:44:33 +00:00
|
|
|
Returns:
|
2008-04-05 14:53:35 +00:00
|
|
|
an array of all define nodes in the node group
|
2007-10-26 22:44:33 +00:00
|
|
|
|
|
|
|
Globals:
|
|
|
|
none
|
|
|
|
Error:
|
|
|
|
undef
|
|
|
|
Example:
|
|
|
|
@nodes=xCAT::Utils->list_nodes_in_nodegroup($group);
|
|
|
|
Comments:
|
|
|
|
none
|
|
|
|
|
|
|
|
=cut
|
|
|
|
|
|
|
|
#------------------------------------------------------------------------
|
|
|
|
sub list_nodes_in_nodegroups
|
|
|
|
{
|
|
|
|
my ($class, $group) = @_;
|
|
|
|
$req->{noderange}->[0] = $group;
|
2008-04-05 14:53:35 +00:00
|
|
|
my @nodes = xCAT::NodeRange::noderange($req->{noderange}->[0]);
|
2007-10-26 22:44:33 +00:00
|
|
|
return @nodes;
|
|
|
|
}
|
|
|
|
|
|
|
|
#-----------------------------------------------------------------------
|
|
|
|
|
2008-04-05 14:53:35 +00:00
|
|
|
=head3
|
|
|
|
get_site_attribute
|
2007-10-26 22:44:33 +00:00
|
|
|
|
|
|
|
Arguments:
|
2008-04-05 14:53:35 +00:00
|
|
|
|
2007-10-26 22:44:33 +00:00
|
|
|
Returns:
|
2008-04-05 14:53:35 +00:00
|
|
|
The value of the attribute requested from the site table
|
2007-10-26 22:44:33 +00:00
|
|
|
Globals:
|
|
|
|
none
|
|
|
|
Error:
|
|
|
|
undef
|
|
|
|
Example:
|
|
|
|
@attr=xCAT::Utils->get_site_attribute($attribute);
|
|
|
|
Comments:
|
|
|
|
none
|
|
|
|
|
|
|
|
=cut
|
|
|
|
|
|
|
|
#------------------------------------------------------------------------
|
|
|
|
sub get_site_attribute
|
|
|
|
{
|
|
|
|
my ($class, $attr) = @_;
|
|
|
|
my $values;
|
|
|
|
|
|
|
|
my $sitetab = xCAT::Table->new('site');
|
|
|
|
if ($sitetab)
|
|
|
|
{
|
2008-04-14 16:58:54 +00:00
|
|
|
(my $ref) = $sitetab->getAttribs({key => $attr}, value);
|
2007-10-26 22:44:33 +00:00
|
|
|
if ($ref and $ref->{value})
|
|
|
|
{
|
|
|
|
$values = $ref->{value};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
xCAT::MsgUtils->message("E", " Could not read the site table\n");
|
|
|
|
|
|
|
|
}
|
2008-04-22 13:24:38 +00:00
|
|
|
$sitetab->close;
|
2007-10-26 22:44:33 +00:00
|
|
|
return $values;
|
|
|
|
}
|
2007-11-27 14:22:04 +00:00
|
|
|
|
2007-11-28 19:44:47 +00:00
|
|
|
#-----------------------------------------------------------------------
|
2007-12-20 19:02:45 +00:00
|
|
|
|
2008-04-05 14:53:35 +00:00
|
|
|
=head3
|
2007-11-28 19:44:47 +00:00
|
|
|
add_cron_job
|
|
|
|
This function adds a new cron job.
|
|
|
|
Arguments:
|
2008-04-05 14:53:35 +00:00
|
|
|
job--- string in the crontab job format.
|
2007-11-28 19:44:47 +00:00
|
|
|
Returns:
|
|
|
|
(code, message)
|
|
|
|
Globals:
|
|
|
|
none
|
|
|
|
Error:
|
|
|
|
undef
|
|
|
|
Example:
|
|
|
|
xCAT::Utils->add_cron_job("*/5 * * * * /usr/bin/myjob");
|
|
|
|
Comments:
|
|
|
|
none
|
|
|
|
|
|
|
|
=cut
|
2007-12-20 19:02:45 +00:00
|
|
|
|
2007-11-28 19:44:47 +00:00
|
|
|
#------------------------------------------------------------------------
|
2007-12-20 19:02:45 +00:00
|
|
|
sub add_cron_job
|
|
|
|
{
|
|
|
|
$newentry = shift;
|
|
|
|
if ($newentry =~ /xCAT::Utils/)
|
|
|
|
{
|
|
|
|
$newentry = shift;
|
|
|
|
}
|
|
|
|
|
|
|
|
#read the cron tab entries
|
|
|
|
my @tabs = `/usr/bin/crontab -l 2>/dev/null`;
|
|
|
|
my @newtabs = ();
|
|
|
|
foreach (@tabs)
|
|
|
|
{
|
|
|
|
chomp($_);
|
|
|
|
|
|
|
|
# stop adding if it's already there
|
|
|
|
if ($_ eq $newentry) { return (0, "started"); }
|
|
|
|
|
|
|
|
#skip headers for Linux
|
|
|
|
next
|
|
|
|
if $_ =~
|
|
|
|
m/^\#.+(DO NOT EDIT THIS FILE|\(.+ installed on |Cron version )/;
|
|
|
|
push(@newtabs, $_);
|
|
|
|
}
|
|
|
|
|
|
|
|
#add new entries to the cron tab
|
|
|
|
push(@newtabs, $newentry);
|
|
|
|
my $tabname = "";
|
|
|
|
if (xCAT::Utils::isLinux) { $tabname = "-"; }
|
|
|
|
open(CRONTAB, "|/usr/bin/crontab $tabname")
|
|
|
|
or return (1, "cannot open crontab.");
|
|
|
|
foreach (@newtabs) { print CRONTAB $_ . "\n"; }
|
|
|
|
close(CRONTAB);
|
2007-11-28 19:44:47 +00:00
|
|
|
|
2007-12-20 19:02:45 +00:00
|
|
|
return (0, "");
|
|
|
|
}
|
2007-11-28 19:44:47 +00:00
|
|
|
|
|
|
|
#-----------------------------------------------------------------------
|
2007-12-20 19:02:45 +00:00
|
|
|
|
2008-04-05 14:53:35 +00:00
|
|
|
=head3
|
2007-11-28 19:44:47 +00:00
|
|
|
remove_cron_job
|
|
|
|
This function removes a new cron job.
|
|
|
|
Arguments:
|
2008-04-05 14:53:35 +00:00
|
|
|
job--- a substring that is contained in a crontab entry.
|
|
|
|
(use crontab -l to see all the job entries.)
|
2007-11-28 19:44:47 +00:00
|
|
|
Returns:
|
|
|
|
(code, message)
|
|
|
|
Globals:
|
|
|
|
none
|
|
|
|
Error:
|
|
|
|
undef
|
|
|
|
Example:
|
|
|
|
xCAT::Utils->remove_cron_job("/usr/bin/myjob");
|
|
|
|
This will remove any cron job that contains this string.
|
|
|
|
Comments:
|
|
|
|
none
|
|
|
|
|
|
|
|
=cut
|
2007-12-20 19:02:45 +00:00
|
|
|
|
2007-11-28 19:44:47 +00:00
|
|
|
#------------------------------------------------------------------------
|
2007-12-20 19:02:45 +00:00
|
|
|
sub remove_cron_job
|
|
|
|
{
|
|
|
|
$job = shift;
|
|
|
|
if ($job =~ /xCAT::Utils/)
|
|
|
|
{
|
|
|
|
$job = shift;
|
|
|
|
}
|
|
|
|
|
|
|
|
#read the cron tab entries and remove the one that contains $job
|
|
|
|
my @tabs = `/usr/bin/crontab -l 2>/dev/null`;
|
|
|
|
my @newtabs = ();
|
|
|
|
foreach (@tabs)
|
|
|
|
{
|
|
|
|
chomp($_);
|
|
|
|
|
|
|
|
# stop adding if it's already there
|
|
|
|
next if index($_, $job, 0) >= 0;
|
|
|
|
|
|
|
|
#skip headers for Linux
|
|
|
|
next
|
|
|
|
if $_ =~
|
|
|
|
m/^\#.+(DO NOT EDIT THIS FILE|\(.+ installed on |Cron version )/;
|
|
|
|
push(@newtabs, $_);
|
|
|
|
}
|
|
|
|
|
|
|
|
#refresh the cron
|
|
|
|
my $tabname = "";
|
|
|
|
if (xCAT::Utils::isLinux) { $tabname = "-"; }
|
|
|
|
open(CRONTAB, "|/usr/bin/crontab $tabname")
|
|
|
|
or return (1, "cannot open crontab.");
|
|
|
|
foreach (@newtabs) { print CRONTAB $_ . "\n"; }
|
|
|
|
close(CRONTAB);
|
2007-11-28 19:44:47 +00:00
|
|
|
|
2007-12-20 19:02:45 +00:00
|
|
|
return (0, "");
|
|
|
|
}
|
2007-11-28 19:44:47 +00:00
|
|
|
|
2007-11-27 14:22:04 +00:00
|
|
|
#-------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
=head3 runcmd
|
|
|
|
Run the given cmd and return the output in an array (already chopped).
|
2008-04-05 14:53:35 +00:00
|
|
|
Alternately, if this function is used in a scalar context, the output
|
2007-11-27 14:22:04 +00:00
|
|
|
is joined into a single string with the newlines separating the lines.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
command, exitcode and reference to output
|
|
|
|
Returns:
|
|
|
|
see below
|
|
|
|
Globals:
|
2008-04-05 14:53:35 +00:00
|
|
|
$::RUNCMD_RC , $::CALLBACK
|
2007-11-27 14:22:04 +00:00
|
|
|
Error:
|
|
|
|
Normally, if there is an error running the cmd,it will display the
|
|
|
|
error and exit with the cmds exit code, unless exitcode
|
|
|
|
is given one of the following values:
|
|
|
|
0: display error msg, DO NOT exit on error, but set
|
|
|
|
$::RUNCMD_RC to the exit code.
|
|
|
|
-1: DO NOT display error msg and DO NOT exit on error, but set
|
|
|
|
$::RUNCMD_RC to the exit code.
|
|
|
|
-2: DO the default behavior (display error msg and exit with cmds
|
|
|
|
exit code.
|
|
|
|
number > 0: Display error msg and exit with the given code
|
|
|
|
|
|
|
|
Example:
|
|
|
|
my $outref = xCAT::Utils->runcmd($cmd, -2, 1);
|
|
|
|
|
|
|
|
Comments:
|
|
|
|
If refoutput is true, then the output will be returned as a
|
|
|
|
reference to an array for efficiency.
|
|
|
|
|
|
|
|
|
|
|
|
=cut
|
|
|
|
|
|
|
|
#-------------------------------------------------------------------------------
|
|
|
|
sub runcmd
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
my ($class, $cmd, $exitcode, $refoutput) = @_;
|
|
|
|
$::RUNCMD_RC = 0;
|
|
|
|
if (!$xCAT::Utils::NO_STDERR_REDIRECT)
|
|
|
|
{
|
|
|
|
if (!($cmd =~ /2>&1$/)) { $cmd .= ' 2>&1'; }
|
|
|
|
|
2008-02-04 20:02:15 +00:00
|
|
|
}
|
|
|
|
if ($::VERBOSE)
|
|
|
|
{
|
|
|
|
xCAT::MsgUtils->message("I", "Running Command: $cmd\n");
|
2007-11-27 14:22:04 +00:00
|
|
|
}
|
|
|
|
my $outref = [];
|
|
|
|
@$outref = `$cmd`;
|
|
|
|
if ($?)
|
|
|
|
{
|
|
|
|
$::RUNCMD_RC = $? >> 8;
|
|
|
|
my $displayerror = 1;
|
|
|
|
my $rc;
|
|
|
|
if (defined($exitcode) && length($exitcode) && $exitcode != -2)
|
|
|
|
{
|
|
|
|
if ($exitcode > 0)
|
|
|
|
{
|
|
|
|
$rc = $exitcode;
|
|
|
|
} # if not zero, exit with specified code
|
|
|
|
elsif ($exitcode <= 0)
|
|
|
|
{
|
|
|
|
$rc = ''; # if zero or negative, do not exit
|
|
|
|
if ($exitcode < 0) { $displayerror = 0; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$rc = $::RUNCMD_RC;
|
|
|
|
} # if exitcode not specified, use cmd exit code
|
|
|
|
if ($displayerror)
|
|
|
|
{
|
|
|
|
my $errmsg = '';
|
|
|
|
if (xCAT::Utils->isLinux() && $::RUNCMD_RC == 139)
|
|
|
|
{
|
|
|
|
$errmsg = "Segmentation fault $errmsg";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$errmsg = join('', @$outref);
|
|
|
|
chomp $errmsg;
|
|
|
|
|
|
|
|
}
|
2007-12-20 19:02:45 +00:00
|
|
|
if ($::CALLBACK)
|
|
|
|
{
|
|
|
|
my %rsp;
|
|
|
|
$rsp->{data}->[0] =
|
|
|
|
"Command failed: $cmd. Error message: $errmsg.\n";
|
|
|
|
xCAT::MsgUtils->message("E", $rsp, $::CALLBACK);
|
|
|
|
|
|
|
|
}
|
|
|
|
else
|
2007-11-27 14:22:04 +00:00
|
|
|
{
|
|
|
|
xCAT::MsgUtils->message("E",
|
|
|
|
"Command failed: $cmd. Error message: $errmsg.\n");
|
|
|
|
}
|
|
|
|
$xCAT::Utils::errno = 29;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($refoutput)
|
|
|
|
{
|
|
|
|
chomp(@$outref);
|
|
|
|
return $outref;
|
|
|
|
}
|
|
|
|
elsif (wantarray)
|
|
|
|
{
|
|
|
|
chomp(@$outref);
|
|
|
|
return @$outref;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
my $line = join('', @$outref);
|
|
|
|
chomp $line;
|
|
|
|
return $line;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2007-11-28 19:44:47 +00:00
|
|
|
|
2007-12-20 19:02:45 +00:00
|
|
|
#--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
=head3 getHomeDir
|
|
|
|
|
|
|
|
Get the path the user home directory from /etc/passwd.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
none
|
|
|
|
Returns:
|
|
|
|
path to user home directory.
|
|
|
|
Globals:
|
|
|
|
none
|
|
|
|
Error:
|
|
|
|
none
|
|
|
|
Example:
|
|
|
|
$myHome = xCAT::Utils->getHomeDir();
|
|
|
|
Comments:
|
|
|
|
none
|
|
|
|
|
|
|
|
=cut
|
|
|
|
|
|
|
|
#--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
sub getHomeDir
|
|
|
|
{
|
|
|
|
my ($class, $username) = @_;
|
|
|
|
my @user = split ':', (`/bin/grep ^$username /etc/passwd 2>&1`);
|
|
|
|
my $home = $user[5];
|
|
|
|
return $home;
|
|
|
|
}
|
|
|
|
|
|
|
|
#--------------------------------------------------------------------------------
|
|
|
|
|
2008-04-05 14:53:35 +00:00
|
|
|
=head3 setupSSH
|
2007-12-20 19:02:45 +00:00
|
|
|
|
|
|
|
Transfers the ssh keys to setup ssh to the input nodes.
|
|
|
|
|
|
|
|
Arguments:
|
2008-04-05 14:53:35 +00:00
|
|
|
Array of nodes
|
2007-12-20 19:02:45 +00:00
|
|
|
Returns:
|
2008-04-05 14:53:35 +00:00
|
|
|
|
2007-12-20 19:02:45 +00:00
|
|
|
Globals:
|
2008-04-05 14:53:35 +00:00
|
|
|
$::XCATROOT , $::CALLBACK
|
2007-12-20 19:02:45 +00:00
|
|
|
Error:
|
2008-04-05 14:53:35 +00:00
|
|
|
0=good, 1=error
|
2007-12-20 19:02:45 +00:00
|
|
|
Example:
|
|
|
|
xCAT::Utils->setupSSH(@target_nodes);
|
|
|
|
Comments:
|
|
|
|
Does not setup known_hosts. Assumes automatically
|
|
|
|
setup by SSH ( ssh config option StrictHostKeyChecking no should
|
|
|
|
be set in the ssh config file).
|
|
|
|
|
|
|
|
=cut
|
|
|
|
|
|
|
|
#--------------------------------------------------------------------------------
|
|
|
|
sub setupSSH
|
|
|
|
{
|
|
|
|
my ($class, $ref_nodes) = @_;
|
|
|
|
my @nodes = $ref_nodes;
|
|
|
|
my @badnodes = ();
|
|
|
|
my $n_str = join ',', @nodes;
|
|
|
|
my $SSHdir = "/install/postscripts/.ssh";
|
|
|
|
if ($::XCATROOT)
|
|
|
|
{
|
|
|
|
$::REMOTESHELL_EXPECT = "$::XCATROOT/sbin/remoteshell.expect";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$::REMOTESHELL_EXPECT = "/opt/xcat/sbin/remoteshell.expect";
|
|
|
|
}
|
2008-01-30 18:49:33 +00:00
|
|
|
$::REMOTE_SHELL = "/usr/bin/ssh";
|
2007-12-20 19:02:45 +00:00
|
|
|
|
|
|
|
# make the directory to hold keys to transfer to the nodes
|
|
|
|
if (!-d $SSHdir)
|
|
|
|
{
|
|
|
|
mkdir("/install", 0755);
|
|
|
|
mkdir("/install/postscripts", 0755);
|
|
|
|
mkdir("/install/postscripts/.ssh", 0755);
|
|
|
|
}
|
|
|
|
|
|
|
|
# Generate the keys
|
|
|
|
xCAT::Utils->runcmd("$::REMOTESHELL_EXPECT -k", 0);
|
|
|
|
if ($::RUNCMD_RC != 0)
|
|
|
|
{ # error
|
|
|
|
my %rsp;
|
2008-04-05 14:53:35 +00:00
|
|
|
$rsp->{data}->[0] = "remoteshell.expect failed generating keys.";
|
2007-12-20 19:02:45 +00:00
|
|
|
xCAT::MsgUtils->message("E", $rsp, $::CALLBACK);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
# Copy the keys to the directory
|
|
|
|
my $rc = xCAT::Utils->cpSSHFiles($SSHdir);
|
|
|
|
if ($rc != 0)
|
|
|
|
{ # error
|
|
|
|
my %rsp;
|
|
|
|
$rsp->{data}->[0] = "Error running cpSSHFiles.\n";
|
|
|
|
xCAT::MsgUtils->message("E", $rsp, $::CALLBACK);
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
open(FILE, ">$SSHdir/copy.perl")
|
|
|
|
or die "cannot open file $SSHdir/copy.perl\n";
|
|
|
|
|
|
|
|
# build the perl copy script in $SSHdir/copy.perl
|
|
|
|
print FILE "#!/usr/bin/perl
|
|
|
|
my (\$name,\$passwd,\$uid,\$gid,\$quota,\$comment,\$gcos,\$dir,\$shell,\$expire) = getpwnam(\"root\");
|
|
|
|
my \$home = \$dir;
|
|
|
|
umask(0077);
|
|
|
|
\$dest_dir = \"\$home/.ssh/\";
|
|
|
|
if (! -d \"\$dest_dir\" ) {
|
|
|
|
# create a local directory
|
|
|
|
\$cmd = \"mkdir -p \$dest_dir\";
|
2008-04-05 14:53:35 +00:00
|
|
|
system(\"\$cmd\");
|
2007-12-20 19:02:45 +00:00
|
|
|
chmod 0700, \$dest_dir;
|
|
|
|
}
|
|
|
|
`cat /tmp/.ssh/authorized_keys >> \$home/.ssh/authorized_keys 2>&1`;
|
|
|
|
`cat /tmp/.ssh/authorized_keys2 >> \$home/.ssh/authorized_keys2 2>&1`;
|
|
|
|
`rm -f /tmp/.ssh/authorized_keys 2>&1`;
|
|
|
|
`rm -f /tmp/.ssh/authorized_keys2 2>&1`;
|
|
|
|
`rm -f /tmp/.ssh/copy.perl 2>&1`;
|
|
|
|
rmdir(\"/tmp/.ssh\");";
|
|
|
|
close FILE;
|
|
|
|
chmod 0744, "$SSHdir/copy.perl";
|
|
|
|
|
|
|
|
# end build Perl code
|
|
|
|
|
|
|
|
#set an ENV var if more than 10 nodes for remoteshell.expect
|
|
|
|
my $num_nodes = scalar(@nodes);
|
|
|
|
if ($num_nodes > 10)
|
|
|
|
{
|
|
|
|
$ENV{'XCAT_UPD_MULTNODES'} = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
# send the keys to the nodes
|
|
|
|
#
|
|
|
|
my $cmd = "$::REMOTESHELL_EXPECT -s $n_str";
|
2008-04-14 16:58:54 +00:00
|
|
|
my $rc = system("$cmd") >> 8;
|
2007-12-20 19:02:45 +00:00
|
|
|
if ($rc)
|
|
|
|
{
|
|
|
|
my %rsp;
|
2008-04-05 14:53:35 +00:00
|
|
|
$rsp->{data}->[0] = "remoteshell.expect failed sending keys.";
|
2007-12-20 19:02:45 +00:00
|
|
|
xCAT::MsgUtils->message("E", $rsp, $::CALLBACK);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
# must always check to see if worked, run test
|
|
|
|
foreach my $n (@nodes)
|
|
|
|
{
|
|
|
|
my $cmd = "$::REMOTESHELL_EXPECT -t $::REMOTE_SHELL $n ";
|
|
|
|
my @cmdout = `$cmd 2>&1`;
|
|
|
|
chomp(@cmdout); # take the newline off
|
|
|
|
my $rc = $? >> 8;
|
|
|
|
if ($rc)
|
|
|
|
{
|
|
|
|
push @badnodes, $n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
xCAT::Utils->runcmd("/bin/stty echo", 0);
|
|
|
|
delete $ENV{'XCAT_UPD_MULTNODES'};
|
2007-11-28 19:44:47 +00:00
|
|
|
|
2007-12-20 19:02:45 +00:00
|
|
|
if (@badnodes)
|
|
|
|
{
|
|
|
|
my $nstring = join ',', @badnodes;
|
|
|
|
my %rsp;
|
|
|
|
$rsp->{data}->[0] =
|
2008-04-05 14:53:35 +00:00
|
|
|
"SSH setup failed for the following nodes: $nstring.";
|
2007-12-20 19:02:45 +00:00
|
|
|
xCAT::MsgUtils->message("E", $rsp, $::CALLBACK);
|
|
|
|
return @badnodes;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
my %rsp;
|
2008-04-05 14:53:35 +00:00
|
|
|
$rsp->{data}->[0] = "$::REMOTE_SHELL setup is complete.";
|
2007-12-20 19:02:45 +00:00
|
|
|
xCAT::MsgUtils->message("I", $rsp, $::CALLBACK);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2007-11-28 19:44:47 +00:00
|
|
|
|
2007-12-20 19:02:45 +00:00
|
|
|
#--------------------------------------------------------------------------------
|
2007-11-28 19:44:47 +00:00
|
|
|
|
2007-12-20 19:02:45 +00:00
|
|
|
=head3 cpSSHFiles
|
2007-11-28 19:44:47 +00:00
|
|
|
|
2007-12-20 19:02:45 +00:00
|
|
|
Copies the ssh keyfiles and the copy perl script into
|
|
|
|
/install/postscripts/.ssh.
|
2007-11-28 19:44:47 +00:00
|
|
|
|
2007-12-20 19:02:45 +00:00
|
|
|
Arguments:
|
2008-04-05 14:53:35 +00:00
|
|
|
directory path
|
2007-12-20 19:02:45 +00:00
|
|
|
Returns:
|
2008-04-05 14:53:35 +00:00
|
|
|
|
2007-12-20 19:02:45 +00:00
|
|
|
Globals:
|
2008-04-05 14:53:35 +00:00
|
|
|
$::CALLBACK
|
2007-12-20 19:02:45 +00:00
|
|
|
Error:
|
2007-11-28 19:44:47 +00:00
|
|
|
|
2007-12-20 19:02:45 +00:00
|
|
|
Example:
|
|
|
|
xCAT::Utils->cpSSHFiles;
|
2007-11-28 19:44:47 +00:00
|
|
|
|
2007-12-20 19:02:45 +00:00
|
|
|
Comments:
|
|
|
|
none
|
2007-11-28 19:44:47 +00:00
|
|
|
|
2007-12-20 19:02:45 +00:00
|
|
|
=cut
|
2007-11-28 19:44:47 +00:00
|
|
|
|
2007-12-20 19:02:45 +00:00
|
|
|
#--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
sub cpSSHFiles
|
|
|
|
{
|
|
|
|
my ($class, $SSHdir) = @_;
|
|
|
|
my ($cmd, $rc);
|
|
|
|
if ($::VERBOSE)
|
|
|
|
{
|
|
|
|
my %rsp;
|
|
|
|
$rsp->{data}->[0] = "Copying SSH Keys";
|
|
|
|
xCAT::MsgUtils->message("I", $rsp, $::CALLBACK);
|
|
|
|
}
|
|
|
|
my $home = xCAT::Utils->getHomeDir("root");
|
|
|
|
|
|
|
|
my $authorized_keys = "$SSHdir/authorized_keys";
|
|
|
|
my $authorized_keys2 = "$SSHdir/authorized_keys2";
|
|
|
|
if ( !(-e "$home/.ssh/identity.pub")
|
|
|
|
|| !(-e "$home/.ssh/id_rsa.pub")
|
|
|
|
|| !(-e "$home/.ssh/id_dsa.pub"))
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
$cmd = " cp $home/.ssh/identity.pub $authorized_keys";
|
|
|
|
xCAT::Utils->runcmd($cmd, 0);
|
|
|
|
if ($::RUNCMD_RC != 0)
|
|
|
|
{
|
|
|
|
my %rsp;
|
|
|
|
$rsp->{data}->[0] = "$cmd failed.\n";
|
|
|
|
xCAT::MsgUtils->message("E", $rsp, $::CALLBACK);
|
|
|
|
return (1);
|
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ($::VERBOSE)
|
|
|
|
{
|
|
|
|
my %rsp;
|
|
|
|
$rsp->{data}->[0] = "$cmd succeeded.\n";
|
|
|
|
xCAT::MsgUtils->message("I", $rsp, $::CALLBACK);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$cmd = "cp $home/.ssh/id_rsa.pub $authorized_keys2";
|
|
|
|
xCAT::Utils->runcmd($cmd, 0);
|
|
|
|
if ($::RUNCMD_RC != 0)
|
|
|
|
{
|
|
|
|
my %rsp;
|
|
|
|
$rsp->{data}->[0] = "$cmd failed.\n";
|
|
|
|
xCAT::MsgUtils->message("E", $rsp, $::CALLBACK);
|
|
|
|
return (1);
|
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ($::VERBOSE)
|
|
|
|
{
|
|
|
|
my %rsp;
|
|
|
|
$rsp->{data}->[0] = "$cmd succeeded.\n";
|
|
|
|
xCAT::MsgUtils->message("I", $rsp, $::CALLBACK);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$cmd = "cat $home/.ssh/id_dsa.pub >> $authorized_keys2";
|
|
|
|
xCAT::Utils->runcmd($cmd, 0);
|
|
|
|
if ($::RUNCMD_RC != 0)
|
|
|
|
{
|
|
|
|
my %rsp;
|
|
|
|
$rsp->{data}->[0] = "$cmd failed.\n";
|
|
|
|
xCAT::MsgUtils->message("E", $rsp, $::CALLBACK);
|
|
|
|
return (1);
|
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ($::VERBOSE)
|
|
|
|
{
|
|
|
|
my %rsp;
|
|
|
|
$rsp->{data}->[0] = "$cmd succeeded.\n";
|
|
|
|
xCAT::MsgUtils->message("I", $rsp, $::CALLBACK);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(-e "$authorized_keys") || !(-e "$authorized_keys2"))
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
2008-02-04 20:02:15 +00:00
|
|
|
|
2008-01-22 14:59:14 +00:00
|
|
|
#-------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
=head3 isServiceNode
|
2008-04-05 14:53:35 +00:00
|
|
|
checks for the /etc/xCATSN file
|
|
|
|
|
2008-01-22 14:59:14 +00:00
|
|
|
Arguments:
|
|
|
|
none
|
|
|
|
Returns:
|
2008-04-05 14:53:35 +00:00
|
|
|
1 - localHost is ServiceNode
|
|
|
|
0 - localHost is not ServiceNode
|
2008-01-22 14:59:14 +00:00
|
|
|
Globals:
|
|
|
|
none
|
|
|
|
Error:
|
|
|
|
none
|
|
|
|
Example:
|
|
|
|
%::XCATMasterPort defined in the caller.
|
2008-04-05 14:53:35 +00:00
|
|
|
$return=(xCAT::Utils->isServiceNode())
|
2008-01-22 14:59:14 +00:00
|
|
|
Comments:
|
|
|
|
none
|
|
|
|
=cut
|
|
|
|
|
|
|
|
#-------------------------------------------------------------------------------
|
|
|
|
sub isServiceNode
|
|
|
|
{
|
|
|
|
my $value;
|
2008-02-18 15:10:31 +00:00
|
|
|
if (-e "/etc/xCATSN")
|
2008-01-22 14:59:14 +00:00
|
|
|
{
|
2008-02-06 17:19:13 +00:00
|
|
|
return 1;
|
2008-02-18 15:10:31 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-02-06 17:19:13 +00:00
|
|
|
return 0;
|
2008-02-18 15:10:31 +00:00
|
|
|
}
|
2008-02-07 20:11:52 +00:00
|
|
|
}
|
2008-03-26 18:20:05 +00:00
|
|
|
|
2008-03-13 12:08:34 +00:00
|
|
|
#-------------------------------------------------------------------------------
|
|
|
|
|
2008-03-31 15:52:36 +00:00
|
|
|
=head3 isMN
|
|
|
|
checks for the /etc/xCATMN file , if it exists it is a Management Server
|
2008-04-05 14:53:35 +00:00
|
|
|
|
2008-03-13 12:08:34 +00:00
|
|
|
Arguments:
|
|
|
|
none
|
|
|
|
Returns:
|
2008-04-05 14:53:35 +00:00
|
|
|
1 - localHost is ServiceNode
|
|
|
|
0 - localHost is not ServiceNode
|
2008-03-13 12:08:34 +00:00
|
|
|
Globals:
|
|
|
|
none
|
|
|
|
Error:
|
|
|
|
none
|
|
|
|
Example:
|
2008-04-05 14:53:35 +00:00
|
|
|
$return=(xCAT::Utils->isMN())
|
2008-03-13 12:08:34 +00:00
|
|
|
Comments:
|
|
|
|
none
|
|
|
|
=cut
|
|
|
|
|
|
|
|
#-------------------------------------------------------------------------------
|
2008-03-31 15:52:36 +00:00
|
|
|
sub isMN
|
2008-03-13 12:08:34 +00:00
|
|
|
{
|
|
|
|
my $value;
|
2008-03-31 15:52:36 +00:00
|
|
|
if (-e "/etc/xCATMN")
|
2008-03-13 12:08:34 +00:00
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#-------------------------------------------------------------------------------
|
|
|
|
|
2008-03-19 15:22:03 +00:00
|
|
|
=head3 classful_networks_for_net_and_mask
|
2008-04-05 14:53:35 +00:00
|
|
|
|
2008-03-19 15:22:03 +00:00
|
|
|
Arguments:
|
|
|
|
network and mask
|
|
|
|
Returns:
|
|
|
|
a list of classful subnets that constitute the entire potentially classless arguments
|
|
|
|
Globals:
|
|
|
|
none
|
|
|
|
Error:
|
|
|
|
none
|
|
|
|
Example:
|
|
|
|
Comments:
|
|
|
|
none
|
|
|
|
=cut
|
|
|
|
|
|
|
|
#-------------------------------------------------------------------------------
|
2008-03-26 18:20:05 +00:00
|
|
|
sub classful_networks_for_net_and_mask
|
2008-03-19 15:22:03 +00:00
|
|
|
{
|
2008-03-26 18:20:05 +00:00
|
|
|
my $network = shift;
|
|
|
|
my $mask = shift;
|
2008-03-19 15:22:03 +00:00
|
|
|
my $given_mask = 0;
|
2008-03-26 18:20:05 +00:00
|
|
|
if ($mask =~ /\./)
|
|
|
|
{
|
|
|
|
$given_mask = 1;
|
|
|
|
my $masknumber = unpack("N", inet_aton($mask));
|
|
|
|
$mask = 32;
|
|
|
|
until ($masknumber % 2)
|
|
|
|
{
|
|
|
|
$masknumber = $masknumber >> 1;
|
|
|
|
$mask--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-19 15:22:03 +00:00
|
|
|
my @results;
|
|
|
|
my $bitstoeven = (8 - ($mask % 8));
|
|
|
|
if ($bitstoeven eq 8) { $bitstoeven = 0; }
|
|
|
|
my $resultmask = $mask + $bitstoeven;
|
2008-03-26 18:20:05 +00:00
|
|
|
if ($given_mask)
|
|
|
|
{
|
|
|
|
$resultmask =
|
|
|
|
inet_ntoa(pack("N", (2**$resultmask - 1) << (32 - $resultmask)));
|
2008-03-19 15:22:03 +00:00
|
|
|
}
|
2008-03-26 18:20:05 +00:00
|
|
|
push @results, $resultmask;
|
2008-03-19 15:22:03 +00:00
|
|
|
|
|
|
|
my $padbits = (32 - ($bitstoeven + $mask));
|
|
|
|
my $numchars = int(($mask + $bitstoeven) / 4);
|
|
|
|
my $curmask = 2**$mask - 1 << (32 - $mask);
|
|
|
|
my $nown = unpack("N", inet_aton($network));
|
|
|
|
$nown = $nown & $curmask;
|
|
|
|
my $highn = $nown + ((2**$bitstoeven - 1) << (32 - $mask - $bitstoeven));
|
|
|
|
|
|
|
|
while ($nown <= $highn)
|
|
|
|
{
|
2008-03-26 18:20:05 +00:00
|
|
|
push @results, inet_ntoa(pack("N", $nown));
|
|
|
|
|
|
|
|
#$rethash->{substr($nowhex, 0, $numchars)} = $network;
|
|
|
|
$nown += 1 << (32 - $mask - $bitstoeven);
|
2008-03-19 15:22:03 +00:00
|
|
|
}
|
|
|
|
return @results;
|
|
|
|
}
|
|
|
|
|
|
|
|
#-------------------------------------------------------------------------------
|
|
|
|
|
2008-04-05 14:53:35 +00:00
|
|
|
=head3 my_hexnets
|
|
|
|
|
2008-03-13 12:08:34 +00:00
|
|
|
Arguments:
|
|
|
|
none
|
|
|
|
Returns:
|
|
|
|
Globals:
|
|
|
|
none
|
|
|
|
Error:
|
|
|
|
none
|
|
|
|
Example:
|
|
|
|
Comments:
|
|
|
|
none
|
|
|
|
=cut
|
2008-02-07 20:11:52 +00:00
|
|
|
|
2008-03-13 12:08:34 +00:00
|
|
|
#-------------------------------------------------------------------------------
|
2008-02-25 14:18:18 +00:00
|
|
|
sub my_hexnets
|
|
|
|
{
|
2008-02-26 19:57:10 +00:00
|
|
|
my $rethash;
|
2008-02-25 14:18:18 +00:00
|
|
|
my @nets = split /\n/, `/sbin/ip addr`;
|
|
|
|
foreach (@nets)
|
|
|
|
{
|
|
|
|
my @elems = split /\s+/;
|
|
|
|
unless (/^\s*inet\s/)
|
|
|
|
{
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
(my $curnet, my $maskbits) = split /\//, $elems[2];
|
2008-02-26 19:57:10 +00:00
|
|
|
my $bitstoeven = (4 - ($maskbits % 4));
|
2008-02-25 14:18:18 +00:00
|
|
|
if ($bitstoeven eq 4) { $bitstoeven = 0; }
|
2008-02-26 19:57:10 +00:00
|
|
|
my $padbits = (32 - ($bitstoeven + $maskbits));
|
|
|
|
my $numchars = int(($maskbits + $bitstoeven) / 4);
|
|
|
|
my $curmask = 2**$maskbits - 1 << (32 - $maskbits);
|
|
|
|
my $nown = unpack("N", inet_aton($curnet));
|
2008-02-25 14:18:18 +00:00
|
|
|
$nown = $nown & $curmask;
|
2008-02-26 19:57:10 +00:00
|
|
|
my $highn =
|
|
|
|
$nown + ((2**$bitstoeven - 1) << (32 - $maskbits - $bitstoeven));
|
|
|
|
|
|
|
|
while ($nown <= $highn)
|
|
|
|
{
|
|
|
|
my $nowhex = sprintf("%08x", $nown);
|
|
|
|
$rethash->{substr($nowhex, 0, $numchars)} = $curnet;
|
|
|
|
$nown += 1 << (32 - $maskbits - $bitstoeven);
|
2008-02-25 14:18:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $rethash;
|
|
|
|
}
|
2008-02-26 19:57:10 +00:00
|
|
|
|
2008-03-21 20:00:07 +00:00
|
|
|
#-------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
=head3 my_if_netmap
|
|
|
|
Arguments:
|
|
|
|
none
|
|
|
|
Returns:
|
|
|
|
hash of networks to interface names
|
|
|
|
Globals:
|
|
|
|
none
|
|
|
|
Error:
|
|
|
|
none
|
|
|
|
Comments:
|
|
|
|
none
|
|
|
|
=cut
|
2008-03-26 18:20:05 +00:00
|
|
|
|
2008-03-21 20:00:07 +00:00
|
|
|
#-------------------------------------------------------------------------------
|
2008-03-26 18:20:05 +00:00
|
|
|
sub my_if_netmap
|
|
|
|
{
|
|
|
|
if (scalar(@_))
|
|
|
|
{ #called with the other syntax
|
|
|
|
$net = shift;
|
|
|
|
}
|
|
|
|
my @rtable = split /\n/, `netstat -rn`;
|
|
|
|
if ($?)
|
|
|
|
{
|
|
|
|
return "Unable to run netstat, $?";
|
|
|
|
}
|
|
|
|
my %retmap;
|
|
|
|
foreach (@rtable)
|
|
|
|
{
|
|
|
|
if (/^\D/) { next; } #skip headers
|
|
|
|
if (/^\S+\s+\S+\s+\S+\s+\S*G/)
|
|
|
|
{
|
|
|
|
next;
|
|
|
|
} #Skip networks that require gateways to get to
|
|
|
|
/^(\S+)\s.*\s(\S+)$/;
|
|
|
|
$retmap{$1} = $2;
|
|
|
|
}
|
|
|
|
return \%retmap;
|
2008-03-21 20:00:07 +00:00
|
|
|
}
|
|
|
|
|
2008-02-04 20:02:15 +00:00
|
|
|
#-------------------------------------------------------------------------------
|
|
|
|
|
2008-04-05 14:53:35 +00:00
|
|
|
=head3 my_ip_facing
|
|
|
|
|
2008-02-04 20:02:15 +00:00
|
|
|
Arguments:
|
|
|
|
none
|
|
|
|
Returns:
|
|
|
|
Globals:
|
|
|
|
none
|
|
|
|
Error:
|
|
|
|
none
|
|
|
|
Example:
|
|
|
|
Comments:
|
|
|
|
none
|
|
|
|
=cut
|
|
|
|
|
2008-02-18 15:10:31 +00:00
|
|
|
#-------------------------------------------------------------------------------
|
|
|
|
sub my_ip_facing
|
|
|
|
{
|
|
|
|
my $peer = shift;
|
|
|
|
if (@_)
|
|
|
|
{
|
|
|
|
$peer = shift;
|
|
|
|
}
|
|
|
|
my $noden = unpack("N", inet_aton($peer));
|
|
|
|
my @nets = split /\n/, `/sbin/ip addr`;
|
|
|
|
foreach (@nets)
|
|
|
|
{
|
|
|
|
my @elems = split /\s+/;
|
|
|
|
unless (/^\s*inet\s/)
|
|
|
|
{
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
(my $curnet, my $maskbits) = split /\//, $elems[2];
|
|
|
|
my $curmask = 2**$maskbits - 1 << (32 - $maskbits);
|
|
|
|
my $curn = unpack("N", inet_aton($curnet));
|
|
|
|
if (($noden & $curmask) == ($curn & $curmask))
|
|
|
|
{
|
|
|
|
return $curnet;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return undef;
|
|
|
|
}
|
|
|
|
|
|
|
|
#-------------------------------------------------------------------------------
|
|
|
|
|
2008-04-05 14:53:35 +00:00
|
|
|
=head3 nodeonmynet - checks to see if node is on the network
|
2008-02-18 15:10:31 +00:00
|
|
|
Arguments:
|
2008-04-05 14:53:35 +00:00
|
|
|
Node name
|
2008-02-18 15:10:31 +00:00
|
|
|
Returns: 1 if node is on the network
|
|
|
|
Globals:
|
|
|
|
none
|
|
|
|
Error:
|
|
|
|
none
|
|
|
|
Example:
|
|
|
|
Comments:
|
|
|
|
none
|
|
|
|
=cut
|
|
|
|
|
2008-02-04 20:02:15 +00:00
|
|
|
#-------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
sub nodeonmynet
|
|
|
|
{
|
|
|
|
my $nodetocheck = shift;
|
|
|
|
if (scalar(@_))
|
|
|
|
{
|
|
|
|
$nodetocheck = shift;
|
|
|
|
}
|
2008-03-26 18:20:05 +00:00
|
|
|
unless (inet_aton($nodetocheck))
|
|
|
|
{
|
|
|
|
return 0;
|
2008-03-20 21:36:49 +00:00
|
|
|
}
|
2008-02-04 20:02:15 +00:00
|
|
|
my $nodeip = inet_ntoa(inet_aton($nodetocheck));
|
|
|
|
unless ($nodeip =~ /\d+\.\d+\.\d+\.\d+/)
|
|
|
|
{
|
2008-02-18 15:10:31 +00:00
|
|
|
return 0; #Not supporting IPv6 here IPV6TODO
|
2008-02-04 20:02:15 +00:00
|
|
|
}
|
|
|
|
my $noden = unpack("N", inet_aton($nodeip));
|
|
|
|
my @nets = split /\n/, `/sbin/ip route`;
|
|
|
|
foreach (@nets)
|
|
|
|
{
|
|
|
|
my @elems = split /\s+/;
|
|
|
|
unless ($elems[1] =~ /dev/)
|
|
|
|
{
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
(my $curnet, my $maskbits) = split /\//, $elems[0];
|
|
|
|
my $curmask = 2**$maskbits - 1 << (32 - $maskbits);
|
|
|
|
my $curn = unpack("N", inet_aton($curnet));
|
|
|
|
if (($noden & $curmask) == $curn)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#-------------------------------------------------------------------------------
|
|
|
|
|
2008-04-05 14:53:35 +00:00
|
|
|
=head3 thishostisnot
|
2008-02-18 15:10:31 +00:00
|
|
|
returns 0 if host is not the same
|
2008-02-04 20:02:15 +00:00
|
|
|
Arguments:
|
2008-04-05 14:53:35 +00:00
|
|
|
hostname
|
2008-02-04 20:02:15 +00:00
|
|
|
Returns:
|
|
|
|
Globals:
|
|
|
|
none
|
|
|
|
Error:
|
|
|
|
none
|
|
|
|
Example:
|
|
|
|
Comments:
|
|
|
|
none
|
|
|
|
=cut
|
|
|
|
|
|
|
|
#-------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
sub thishostisnot
|
|
|
|
{
|
|
|
|
my $comparison = shift;
|
|
|
|
if (scalar(@_))
|
|
|
|
{
|
|
|
|
$comparison = shift;
|
|
|
|
}
|
|
|
|
|
|
|
|
my @ips = split /\n/, `/sbin/ip addr`;
|
|
|
|
my $comp = inet_aton($comparison);
|
|
|
|
foreach (@ips)
|
|
|
|
{
|
|
|
|
if (/^\s*inet/)
|
|
|
|
{
|
|
|
|
my @ents = split(/\s+/);
|
|
|
|
my $ip = $ents[2];
|
|
|
|
$ip =~ s/\/.*//;
|
|
|
|
if (inet_aton($ip) eq $comp)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#print Dumper(inet_aton($ip));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#-------------------------------------------------------------------------------
|
|
|
|
|
2008-04-05 14:53:35 +00:00
|
|
|
=head3 GetMasterNodeName
|
2008-02-04 20:02:15 +00:00
|
|
|
Reads the database for the Master node name for the input node
|
|
|
|
Arguments:
|
|
|
|
Node
|
|
|
|
Returns:
|
2008-04-05 14:53:35 +00:00
|
|
|
MasterHostName
|
2008-02-04 20:02:15 +00:00
|
|
|
Globals:
|
|
|
|
none
|
|
|
|
Error:
|
|
|
|
none
|
|
|
|
Example:
|
2008-04-05 14:53:35 +00:00
|
|
|
$master=(xCAT::Utils->GetMasterNodeName($node))
|
2008-02-04 20:02:15 +00:00
|
|
|
Comments:
|
|
|
|
none
|
|
|
|
=cut
|
|
|
|
|
|
|
|
#-------------------------------------------------------------------------------
|
|
|
|
sub GetMasterNodeName
|
|
|
|
{
|
|
|
|
my ($class, $node) = @_;
|
|
|
|
my $master;
|
|
|
|
my $noderestab = xCAT::Table->new('noderes');
|
|
|
|
my $typetab = xCAT::Table->new('nodetype');
|
|
|
|
unless ($noderestab and $typetab)
|
|
|
|
{
|
|
|
|
xCAT::MsgUtils->message('S',
|
|
|
|
"Unable to open noderes or nodetype table.\n");
|
2008-04-22 13:24:38 +00:00
|
|
|
$noderestab->close;
|
|
|
|
$typetab->close;
|
2008-02-04 20:02:15 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
my $sitetab = xCAT::Table->new('site');
|
|
|
|
(my $et) = $sitetab->getAttribs({key => "master"}, 'value');
|
|
|
|
if ($et and $et->{value})
|
|
|
|
{
|
|
|
|
$master = $et->{value};
|
|
|
|
}
|
|
|
|
$et = $noderestab->getNodeAttribs($node, ['xcatmaster']);
|
|
|
|
if ($et and $et->{'xcatmaster'})
|
|
|
|
{
|
|
|
|
$master = $et->{'xcatmaster'};
|
|
|
|
}
|
|
|
|
unless ($master)
|
|
|
|
{
|
|
|
|
xCAT::MsgUtils->message('S', "Unable to identify master for $node.\n");
|
2008-04-22 13:24:38 +00:00
|
|
|
$sitetab->close;
|
|
|
|
$noderestab->close;
|
|
|
|
$typetab->close;
|
2008-02-04 20:02:15 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2008-04-22 13:24:38 +00:00
|
|
|
$sitetab->close;
|
|
|
|
$noderestab->close;
|
|
|
|
$typetab->close;
|
2008-02-04 20:02:15 +00:00
|
|
|
return $master;
|
2008-02-02 19:39:33 +00:00
|
|
|
}
|
2008-02-04 20:02:15 +00:00
|
|
|
|
|
|
|
#-------------------------------------------------------------------------------
|
|
|
|
|
2008-04-05 14:53:35 +00:00
|
|
|
=head3 GetNodeOSARCH
|
2008-02-04 20:02:15 +00:00
|
|
|
Reads the database for the OS and Arch of the input Node
|
|
|
|
Arguments:
|
|
|
|
Node
|
|
|
|
Returns:
|
|
|
|
$et->{'os'}
|
|
|
|
$et->{'arch'}
|
|
|
|
Globals:
|
|
|
|
none
|
|
|
|
Error:
|
|
|
|
none
|
|
|
|
Example:
|
2008-04-05 14:53:35 +00:00
|
|
|
$master=(xCAT::Utils->GetNodeOSARCH($node))
|
2008-02-04 20:02:15 +00:00
|
|
|
Comments:
|
|
|
|
none
|
|
|
|
=cut
|
|
|
|
|
|
|
|
#-------------------------------------------------------------------------------
|
|
|
|
sub GetNodeOSARCH
|
|
|
|
{
|
|
|
|
my ($class, $node) = @_;
|
|
|
|
my $noderestab = xCAT::Table->new('noderes');
|
|
|
|
my $typetab = xCAT::Table->new('nodetype');
|
|
|
|
unless ($noderestab and $typetab)
|
|
|
|
{
|
|
|
|
xCAT::MsgUtils->message('S',
|
|
|
|
"Unable to open noderes or nodetype table.\n");
|
|
|
|
return 1;
|
2008-02-02 19:39:33 +00:00
|
|
|
}
|
2008-02-04 20:02:15 +00:00
|
|
|
my $et = $typetab->getNodeAttribs($node, ['os', 'arch']);
|
|
|
|
unless ($et and $et->{'os'} and $et->{'arch'})
|
|
|
|
{
|
|
|
|
xCAT::MsgUtils->message('S',
|
|
|
|
"No os/arch setting in nodetype table for $node.\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $et;
|
|
|
|
|
2008-02-18 15:10:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#-----------------------------------------------------------------------------
|
|
|
|
|
2008-04-05 14:53:35 +00:00
|
|
|
=head3 exportDBConfig
|
|
|
|
|
2008-04-22 13:24:38 +00:00
|
|
|
Reads the /etc/xcat/cfgloc file for the DB configuration and exports it
|
2008-02-18 15:10:31 +00:00
|
|
|
in $XCATCFG
|
|
|
|
=cut
|
|
|
|
|
|
|
|
#-----------------------------------------------------------------------------
|
|
|
|
sub exportDBConfig
|
|
|
|
{
|
|
|
|
|
|
|
|
# export the xcat database configuration
|
2008-04-22 13:24:38 +00:00
|
|
|
my $configfile = "/etc/xcat/cfgloc";
|
2008-02-18 15:10:31 +00:00
|
|
|
if (!($ENV{'XCATCFG'}))
|
|
|
|
{
|
|
|
|
if (-e ($configfile))
|
|
|
|
{
|
|
|
|
open(CFGFILE, "<$configfile")
|
|
|
|
or xCAT::MsgUtils->message('S',
|
|
|
|
"Cannot open $configfile for DB access. \n");
|
|
|
|
foreach my $line (<CFGFILE>)
|
|
|
|
{
|
2008-04-22 13:24:38 +00:00
|
|
|
chop $line;
|
|
|
|
$exp .= $line;
|
|
|
|
|
|
|
|
$ENV{'XCATCFG'} = $exp;
|
|
|
|
close CFGFILE;
|
|
|
|
last;
|
2008-02-18 15:10:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#-----------------------------------------------------------------------------
|
|
|
|
|
2008-04-05 14:53:35 +00:00
|
|
|
=head3 readSNInfo
|
|
|
|
|
2008-02-18 15:10:31 +00:00
|
|
|
Read resource, NFS server, Master node, OS an ARCH from the database
|
|
|
|
for the service node
|
|
|
|
|
|
|
|
Input: service nodename
|
|
|
|
Output: Masternode, OS and ARCH
|
|
|
|
=cut
|
|
|
|
|
|
|
|
#-----------------------------------------------------------------------------
|
|
|
|
sub readSNInfo
|
|
|
|
{
|
|
|
|
my ($class, $nodename) = @_;
|
|
|
|
my $rc = 0;
|
|
|
|
my $et;
|
|
|
|
my $masternode;
|
|
|
|
my $os;
|
|
|
|
my $arch;
|
|
|
|
$rc = xCAT::Utils->exportDBConfig();
|
|
|
|
if ($rc == 0)
|
|
|
|
{
|
|
|
|
|
|
|
|
if ($nodename)
|
|
|
|
{
|
|
|
|
$masternode = xCAT::Utils->GetMasterNodeName($nodename);
|
|
|
|
if (!($masternode))
|
|
|
|
{
|
|
|
|
xCAT::MsgUtils->message('S',
|
2008-02-25 13:03:17 +00:00
|
|
|
"Could not get Master for node $nodename\n");
|
2008-02-18 15:10:31 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
$et = xCAT::Utils->GetNodeOSARCH($nodename);
|
|
|
|
if (!($et->{'os'} || $et->{'arch'}))
|
|
|
|
{
|
|
|
|
xCAT::MsgUtils->message('S',
|
2008-02-25 13:03:17 +00:00
|
|
|
"Could not get OS/ARCH for node $nodename\n");
|
2008-02-18 15:10:31 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$et->{'master'} = $masternode;
|
|
|
|
return $et;
|
|
|
|
}
|
|
|
|
return $rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
#-----------------------------------------------------------------------------
|
|
|
|
|
2008-04-05 14:53:35 +00:00
|
|
|
=head3 isServiceReq
|
2008-02-18 15:10:31 +00:00
|
|
|
|
2008-02-28 19:46:39 +00:00
|
|
|
|
2008-05-06 15:41:58 +00:00
|
|
|
Checks the service node table in the database to see
|
|
|
|
if the input Service should be setup on the
|
2008-02-18 15:10:31 +00:00
|
|
|
input service node
|
2008-04-05 14:53:35 +00:00
|
|
|
|
2008-05-06 15:41:58 +00:00
|
|
|
Input: service nodename, service,ipaddres(s) and hostnames of service node
|
2008-04-05 14:53:35 +00:00
|
|
|
Output:
|
|
|
|
0 - no service required
|
|
|
|
1 - setup service
|
|
|
|
2 - service is setup, just start the daemon
|
2008-02-18 19:00:29 +00:00
|
|
|
-1 - error
|
2008-02-18 15:10:31 +00:00
|
|
|
Globals:
|
|
|
|
none
|
|
|
|
Error:
|
|
|
|
none
|
|
|
|
Example:
|
2008-03-06 18:42:32 +00:00
|
|
|
if (xCAT::Utils->isServiceReq($servicenodename, $service, $serviceip) { blah; }
|
2008-02-18 15:10:31 +00:00
|
|
|
|
|
|
|
=cut
|
|
|
|
|
|
|
|
#-----------------------------------------------------------------------------
|
|
|
|
sub isServiceReq
|
|
|
|
{
|
2008-02-21 18:21:41 +00:00
|
|
|
my ($class, $servicenodename, $service, $serviceip) = @_;
|
2008-04-22 13:24:38 +00:00
|
|
|
my @ips = @$serviceip; # list of service node ip addresses and names
|
2008-03-26 18:20:05 +00:00
|
|
|
my $rc = 0;
|
|
|
|
|
2008-02-18 15:10:31 +00:00
|
|
|
# check if service is already setup
|
2008-05-06 15:41:58 +00:00
|
|
|
#`grep $service /etc/xCATSN`;
|
|
|
|
#if ($? == 0)
|
|
|
|
#{ # service is already setup, just start daemon
|
|
|
|
# return 2;
|
|
|
|
#}
|
2008-03-06 18:42:32 +00:00
|
|
|
|
|
|
|
$rc = xCAT::Utils->exportDBConfig(); # export DB env
|
|
|
|
if ($rc != 0)
|
2008-02-28 19:46:39 +00:00
|
|
|
{
|
2008-03-06 18:42:32 +00:00
|
|
|
xCAT::MsgUtils->message('S', "Unable export DB environment.\n");
|
|
|
|
return -1;
|
|
|
|
|
2008-02-29 14:06:01 +00:00
|
|
|
}
|
2008-02-28 19:46:39 +00:00
|
|
|
|
2008-05-06 15:41:58 +00:00
|
|
|
# get handle to servicenode table
|
|
|
|
my $servicenodetab = xCAT::Table->new('servicenode');
|
|
|
|
unless ($servicenodetab)
|
2008-02-28 19:46:39 +00:00
|
|
|
{
|
2008-05-06 15:41:58 +00:00
|
|
|
xCAT::MsgUtils->message('S', "Unable to open servicenode table.\n");
|
|
|
|
return 0; # do not setup anything
|
2008-02-28 19:46:39 +00:00
|
|
|
}
|
2008-05-07 17:35:14 +00:00
|
|
|
|
|
|
|
# read all the nodes from the table
|
|
|
|
my @snodelist = $servicenodetab->getAllNodeAttribs([$service]);
|
|
|
|
$servicenodetab->close;
|
|
|
|
foreach $serviceip (@ips) # check the table for this servicenode
|
2008-02-28 19:46:39 +00:00
|
|
|
{
|
2008-05-07 17:35:14 +00:00
|
|
|
foreach my $node (@snodelist)
|
|
|
|
|
2008-02-18 19:00:29 +00:00
|
|
|
{
|
2008-05-13 12:36:13 +00:00
|
|
|
if ($serviceip eq $node->{'node'})
|
|
|
|
{ # match table entry
|
|
|
|
if ($node->{$service})
|
|
|
|
{ # returns service, only if set
|
|
|
|
my $value = $node->{$service};
|
|
|
|
$value =~ tr/a-z/A-Z/; # convert to upper
|
|
|
|
# value 1 or yes then we setup the service
|
|
|
|
if (($value == 1) || ($value eq "YES"))
|
|
|
|
{
|
|
|
|
return 1; # found service required for the node
|
|
|
|
}
|
2008-05-07 17:35:14 +00:00
|
|
|
}
|
2008-02-18 19:00:29 +00:00
|
|
|
}
|
2008-02-26 19:57:10 +00:00
|
|
|
}
|
2008-02-18 15:10:31 +00:00
|
|
|
}
|
|
|
|
|
2008-05-07 17:35:14 +00:00
|
|
|
return 0; # servicenode is not required to setup this service
|
2008-05-06 15:41:58 +00:00
|
|
|
|
2008-02-02 19:39:33 +00:00
|
|
|
}
|
2008-02-18 19:00:29 +00:00
|
|
|
|
2008-02-21 18:21:41 +00:00
|
|
|
#-----------------------------------------------------------------------------
|
|
|
|
|
2008-03-06 18:42:32 +00:00
|
|
|
=head3 determinehostname and ip address(s)
|
2008-04-05 14:53:35 +00:00
|
|
|
|
2008-03-06 18:42:32 +00:00
|
|
|
Used on the service node to figure out what hostname and ip address(s)
|
2008-04-22 13:24:38 +00:00
|
|
|
are valid names and addresses
|
2008-04-05 14:53:35 +00:00
|
|
|
Input: None
|
|
|
|
Output: ipaddress(s),nodename
|
2008-02-21 18:21:41 +00:00
|
|
|
=cut
|
|
|
|
|
|
|
|
#-----------------------------------------------------------------------------
|
|
|
|
sub determinehostname
|
|
|
|
{
|
|
|
|
my $hostname;
|
|
|
|
my $hostnamecmd = "/bin/hostname";
|
2008-03-06 18:42:32 +00:00
|
|
|
my @thostname = xCAT::Utils->runcmd($hostnamecmd, 0);
|
|
|
|
if ($::RUNCMD_RC != 0)
|
2008-02-21 18:21:41 +00:00
|
|
|
{ # could not get hostname
|
2008-03-06 18:42:32 +00:00
|
|
|
xCAT::MsgUtils->message("S",
|
|
|
|
"Error $::RUNCMD_RC from $hostnamecmd command\n");
|
|
|
|
exit $::RUNCMD_RC;
|
2008-02-21 18:21:41 +00:00
|
|
|
}
|
|
|
|
$hostname = $thostname[0];
|
2008-03-06 18:42:32 +00:00
|
|
|
|
2008-02-29 14:06:01 +00:00
|
|
|
# strip off domain, if there
|
2008-03-06 18:42:32 +00:00
|
|
|
my @shorthost = split(/\./, $hostname);
|
|
|
|
my @ips = xCAT::Utils->gethost_ips;
|
|
|
|
my @hostinfo = (@ips, $shorthost[0]);
|
|
|
|
|
2008-02-21 18:21:41 +00:00
|
|
|
return @hostinfo;
|
|
|
|
}
|
|
|
|
|
2008-02-25 13:03:17 +00:00
|
|
|
#-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
=head3 update_xCATSN
|
|
|
|
Will add the input service string to /etc/xCATSN to indicate that
|
|
|
|
the service has been setup by the service node
|
2008-04-05 14:53:35 +00:00
|
|
|
Input: service (e.g. tftp, nfs,etc)
|
2008-02-25 13:03:17 +00:00
|
|
|
Output: 0 = added, 1= already there
|
|
|
|
|
|
|
|
=cut
|
|
|
|
|
|
|
|
#-----------------------------------------------------------------------------
|
|
|
|
sub update_xCATSN
|
|
|
|
{
|
|
|
|
my ($class, $service) = @_;
|
|
|
|
my $file = "/etc/xCATSN";
|
|
|
|
my $rc = 0;
|
|
|
|
my $cmd = " grep $service $file";
|
|
|
|
xCAT::Utils->runcmd($cmd, -1);
|
|
|
|
if ($::RUNCMD_RC != 0)
|
|
|
|
{ # need to add
|
|
|
|
`echo $service >> /etc/xCATSN`;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$rc = 1;
|
|
|
|
}
|
|
|
|
return $rc;
|
|
|
|
}
|
|
|
|
|
2008-03-06 18:42:32 +00:00
|
|
|
#-----------------------------------------------------------------------------
|
|
|
|
|
2008-04-05 14:53:35 +00:00
|
|
|
=head3 gethost_ips
|
|
|
|
Will use ifconfig to determine all possible ip addresses for the
|
2008-04-22 13:24:38 +00:00
|
|
|
host it is running on and then gethostbyaddr to get all possible hostnames
|
2008-03-06 18:42:32 +00:00
|
|
|
|
2008-04-05 14:53:35 +00:00
|
|
|
input:
|
2008-04-22 13:24:38 +00:00
|
|
|
output: array of ipaddress(s) and hostnames
|
2008-03-06 18:42:32 +00:00
|
|
|
example: @ips=xCAT::gethost_ips();
|
|
|
|
|
|
|
|
=cut
|
|
|
|
|
|
|
|
#-----------------------------------------------------------------------------
|
|
|
|
sub gethost_ips
|
|
|
|
{
|
|
|
|
my ($class) = @_;
|
|
|
|
my $cmd;
|
|
|
|
my @ipaddress;
|
|
|
|
$cmd = "ifconfig" . " -a";
|
|
|
|
$cmd = $cmd . "| grep \"inet \"";
|
|
|
|
my @result = xCAT::Utils->runcmd($cmd, 0);
|
|
|
|
if ($::RUNCMD_RC != 0)
|
|
|
|
{
|
|
|
|
xCAT::MsgUtils->message("S", "Error from $cmd\n");
|
|
|
|
exit $::RUNCMD_RC;
|
|
|
|
}
|
|
|
|
foreach my $addr (@result)
|
|
|
|
{
|
|
|
|
my ($inet, $addr1, $Bcast, $Mask) = split(" ", $addr);
|
|
|
|
my @ip = split(":", $addr1);
|
|
|
|
push @ipaddress, $ip[1];
|
|
|
|
}
|
2008-04-22 13:24:38 +00:00
|
|
|
my @names = @ipaddress;
|
|
|
|
foreach my $ipaddr (@names)
|
|
|
|
{
|
|
|
|
my $packedaddr = inet_aton($ipaddr);
|
|
|
|
my $hostname = gethostbyaddr($packedaddr, AF_INET);
|
|
|
|
if ($hostname)
|
|
|
|
{
|
|
|
|
my @shorthost = split(/\./, $hostname);
|
|
|
|
push @ipaddress, $shorthost[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-06 18:42:32 +00:00
|
|
|
return @ipaddress;
|
|
|
|
}
|
2008-03-26 18:20:05 +00:00
|
|
|
|
2008-03-12 19:16:44 +00:00
|
|
|
#-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
=head3 create_postscripts_tar
|
2008-04-05 14:53:35 +00:00
|
|
|
|
2008-03-12 19:16:44 +00:00
|
|
|
This routine will tar and compress the /install/postscripts directory
|
|
|
|
and place in /install/autoinst/xcat_postscripts.Z
|
|
|
|
|
2008-04-05 14:53:35 +00:00
|
|
|
input: none
|
|
|
|
output:
|
2008-03-12 19:16:44 +00:00
|
|
|
example: $rc=xCAT::create_postscripts_tar();
|
|
|
|
|
|
|
|
=cut
|
|
|
|
|
|
|
|
#-----------------------------------------------------------------------------
|
2008-03-26 18:20:05 +00:00
|
|
|
sub create_postscripts_tar
|
2008-03-12 19:16:44 +00:00
|
|
|
{
|
|
|
|
my ($class) = @_;
|
|
|
|
my $cmd;
|
2008-03-26 18:20:05 +00:00
|
|
|
if (!(-e "/install/autoinst"))
|
|
|
|
{
|
|
|
|
mkdir("/install/autoinst");
|
2008-03-19 21:09:38 +00:00
|
|
|
}
|
|
|
|
|
2008-04-22 13:24:38 +00:00
|
|
|
$cmd =
|
2008-05-01 13:10:57 +00:00
|
|
|
"cd /install/postscripts; tar -cf /install/autoinst/xcatpost.tar * .ssh/* _xcat/*; gzip -f /install/autoinst/xcatpost.tar";
|
2008-03-12 19:16:44 +00:00
|
|
|
my @result = xCAT::Utils->runcmd($cmd, 0);
|
|
|
|
if ($::RUNCMD_RC != 0)
|
|
|
|
{
|
|
|
|
xCAT::MsgUtils->message("S", "Error from $cmd\n");
|
2008-03-26 18:20:05 +00:00
|
|
|
return $::RUNCMD_RC;
|
2008-03-12 19:16:44 +00:00
|
|
|
}
|
2008-03-06 18:42:32 +00:00
|
|
|
|
2008-05-06 17:22:15 +00:00
|
|
|
# for AIX add an entry to the /etc/tftpaccess.ctrl file so
|
|
|
|
# we can tftp the tar file from the node
|
|
|
|
if (xCAT::Utils->isAIX())
|
|
|
|
{
|
|
|
|
my $tftpctlfile = "/etc/tftpaccess.ctl";
|
|
|
|
my $entry = "allow:/install/autoinst/xcatpost.tar.gz";
|
2008-05-03 13:01:21 +00:00
|
|
|
|
2008-05-06 17:22:15 +00:00
|
|
|
# see if there is already an entry
|
|
|
|
my $cmd = "cat $tftpctlfile | grep xcatpost";
|
|
|
|
my @result = xCAT::Utils->runcmd("$cmd", -1);
|
|
|
|
if ($::RUNCMD_RC != 0)
|
|
|
|
{
|
2008-05-03 13:01:21 +00:00
|
|
|
|
2008-05-06 17:22:15 +00:00
|
|
|
# not found so add it
|
|
|
|
unless (open(TFTPFILE, ">>$tftpctlfile"))
|
|
|
|
{
|
|
|
|
xCAT::MsgUtils->message("S", "Could not open $tftpctlfile.\n");
|
|
|
|
return $::RUNCMD_RC;
|
|
|
|
}
|
2008-05-03 13:01:21 +00:00
|
|
|
|
2008-05-06 17:22:15 +00:00
|
|
|
print TFTPFILE $entry;
|
2008-05-03 13:01:21 +00:00
|
|
|
|
2008-05-06 17:22:15 +00:00
|
|
|
close(TFTPFILE);
|
|
|
|
}
|
|
|
|
}
|
2008-05-03 13:01:21 +00:00
|
|
|
return 0;
|
2008-03-12 19:16:44 +00:00
|
|
|
}
|
2008-03-26 18:20:05 +00:00
|
|
|
|
|
|
|
#-----------------------------------------------------------------------------
|
|
|
|
|
2008-04-05 14:53:35 +00:00
|
|
|
=head3 get_site_Master
|
2008-03-26 18:20:05 +00:00
|
|
|
|
|
|
|
Reads the site table for the Master attribute and returns it.
|
|
|
|
input: none
|
|
|
|
output : value of site.Master attribute , blank is an error
|
|
|
|
example: $Master =xCAT::get_site_Master();
|
|
|
|
|
|
|
|
=cut
|
|
|
|
|
|
|
|
#-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
sub get_site_Master
|
|
|
|
{
|
|
|
|
my $Master;
|
|
|
|
my $sitetab = xCAT::Table->new('site');
|
|
|
|
(my $et) = $sitetab->getAttribs({key => "master"}, 'value');
|
|
|
|
if ($et and $et->{value})
|
|
|
|
{
|
|
|
|
$Master = $et->{value};
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
xCAT::MsgUtils->message('E',
|
|
|
|
"Unable to read site table for Master attribute.\n");
|
|
|
|
}
|
|
|
|
return $Master;
|
|
|
|
}
|
|
|
|
|
|
|
|
#-----------------------------------------------------------------------------
|
|
|
|
|
2008-04-05 14:53:35 +00:00
|
|
|
=head3 get_ServiceNode
|
2008-03-26 18:20:05 +00:00
|
|
|
|
|
|
|
Will get the Service node ( name or ipaddress) as known by the Management
|
2008-04-23 18:14:57 +00:00
|
|
|
Server or Node for the input nodename or ipadress of the node
|
2008-03-26 18:20:05 +00:00
|
|
|
|
2008-04-15 14:37:26 +00:00
|
|
|
input: list of nodenames and/or node ipaddresses (array ref)
|
2008-04-05 14:53:35 +00:00
|
|
|
service name
|
2008-03-31 15:52:36 +00:00
|
|
|
"MN" or "Node" determines if you want the Service node as known
|
|
|
|
by the Management Node or by the node.
|
2008-03-26 18:20:05 +00:00
|
|
|
|
|
|
|
recognized service names: xcat,tftpserver,
|
|
|
|
nfsserver,conserver,monserver
|
|
|
|
|
|
|
|
service "xcat" is used by command like xdsh that need to know the
|
2008-04-05 14:53:35 +00:00
|
|
|
service node that will process the command but are not tied to a
|
2008-03-26 18:20:05 +00:00
|
|
|
specific service like tftp
|
|
|
|
|
|
|
|
Todo: Handle dhcpserver and nameserver from the networks table
|
|
|
|
|
2008-04-24 17:28:35 +00:00
|
|
|
output: A hash ref of arrays, the key is the service node pointing to
|
2008-04-05 14:53:35 +00:00
|
|
|
an array of nodes that are serviced by that service node
|
2008-03-26 18:20:05 +00:00
|
|
|
|
|
|
|
Globals:
|
2008-04-05 14:53:35 +00:00
|
|
|
$::ERROR_RC
|
2008-03-26 18:20:05 +00:00
|
|
|
Error:
|
2008-04-05 14:53:35 +00:00
|
|
|
$::ERROR_RC=0 no error $::ERROR_RC=1 error
|
2008-03-26 18:20:05 +00:00
|
|
|
|
2008-04-15 14:37:26 +00:00
|
|
|
example: $sn =xCAT::Utils->get_ServiceNode(\@nodes,$service,"MN");
|
2008-03-26 18:20:05 +00:00
|
|
|
|
|
|
|
=cut
|
|
|
|
|
|
|
|
#-----------------------------------------------------------------------------
|
2008-03-31 15:19:42 +00:00
|
|
|
sub get_ServiceNode
|
2008-03-26 18:20:05 +00:00
|
|
|
{
|
|
|
|
my ($class, $node, $service, $request) = @_;
|
|
|
|
my @node_list = @$node;
|
|
|
|
my $cmd;
|
|
|
|
my %snhash;
|
|
|
|
my $sn;
|
|
|
|
my $nodehmtab;
|
|
|
|
my $noderestab;
|
|
|
|
my $snattribute;
|
|
|
|
$::ERROR_RC = 0;
|
|
|
|
|
2008-03-31 15:52:36 +00:00
|
|
|
# determine if the request is for the service node as known by the MN
|
2008-03-27 12:35:01 +00:00
|
|
|
# or the node
|
2008-03-26 18:20:05 +00:00
|
|
|
|
2008-03-31 15:52:36 +00:00
|
|
|
if ($request eq "MN")
|
2008-03-27 12:35:01 +00:00
|
|
|
{
|
2008-03-26 18:20:05 +00:00
|
|
|
$snattribute = "servicenode";
|
|
|
|
}
|
2008-03-27 12:35:01 +00:00
|
|
|
else # Node
|
|
|
|
{
|
2008-03-26 18:20:05 +00:00
|
|
|
$snattribute = "xcatmaster";
|
|
|
|
}
|
|
|
|
|
|
|
|
my $master =
|
|
|
|
xCAT::Utils->get_site_Master(); # read the site table, master attrib
|
|
|
|
|
|
|
|
$noderestab = xCAT::Table->new('noderes');
|
|
|
|
unless ($noderestab) # no noderes table, use default site.master
|
|
|
|
{
|
|
|
|
xCAT::MsgUtils->message('I',
|
|
|
|
"Unable to open noderes table. Using site->Master.\n");
|
|
|
|
if ($master) # use site Master value
|
|
|
|
{
|
|
|
|
foreach my $node (@node_list)
|
|
|
|
{ # no noderes table, all use site Master
|
|
|
|
push @{$snhash{$master}}, $node;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
xCAT::MsgUtils->message('E', "Unable to read site Master value.\n");
|
|
|
|
$::ERROR_RC = 1;
|
|
|
|
}
|
|
|
|
return \%snhash;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($service eq "xcat")
|
2008-03-27 12:35:01 +00:00
|
|
|
{ # find all service nodes for the nodes in the list
|
2008-03-26 18:20:05 +00:00
|
|
|
foreach my $node (@node_list)
|
|
|
|
{
|
|
|
|
$sn = $noderestab->getNodeAttribs($node, [$snattribute]);
|
|
|
|
if ($sn and $sn->{$snattribute})
|
2008-03-27 12:35:01 +00:00
|
|
|
{ # if service node defined
|
2008-03-26 18:20:05 +00:00
|
|
|
my $key = $sn->{$snattribute};
|
|
|
|
push @{$snhash{$key}}, $node;
|
|
|
|
}
|
|
|
|
else
|
2008-03-27 12:35:01 +00:00
|
|
|
{ # use site.master
|
2008-03-26 18:20:05 +00:00
|
|
|
push @{$snhash{$master}}, $node;
|
|
|
|
}
|
|
|
|
}
|
2008-04-22 13:24:38 +00:00
|
|
|
$noderestab->close;
|
2008-03-26 18:20:05 +00:00
|
|
|
return \%snhash;
|
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (
|
|
|
|
($service eq "tftpserver") # all from noderes table
|
|
|
|
|| ($service eq "nfsserver") || ($service eq "monserver")
|
|
|
|
)
|
|
|
|
{
|
|
|
|
foreach my $node (@node_list)
|
|
|
|
{
|
|
|
|
$sn =
|
|
|
|
$noderestab->getNodeAttribs($node, [$service, $snattribute]);
|
|
|
|
if ($sn and $sn->{$service})
|
|
|
|
{
|
2008-03-27 12:35:01 +00:00
|
|
|
|
2008-03-31 15:52:36 +00:00
|
|
|
# see if both MN and Node address in attribute
|
2008-03-27 12:35:01 +00:00
|
|
|
my ($msattr, $nodeattr) = split ',', $sn->{$service};
|
|
|
|
my $key = $msattr;
|
|
|
|
if ($request eq "Node")
|
|
|
|
{
|
|
|
|
if ($nodeattr) # override with Node, if it exists
|
|
|
|
{
|
|
|
|
$key = $nodeattr;
|
|
|
|
}
|
|
|
|
}
|
2008-03-26 18:20:05 +00:00
|
|
|
push @{$snhash{$key}}, $node;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ($sn and $sn->{$snattribute}) # if it exists
|
|
|
|
{
|
|
|
|
my $key = $sn->{$snattribute};
|
|
|
|
push @{$snhash{$key}}, $node;
|
|
|
|
}
|
|
|
|
else
|
2008-03-27 12:35:01 +00:00
|
|
|
{ # use site.master
|
2008-03-26 18:20:05 +00:00
|
|
|
push @{$snhash{$master}}, $node;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-04-22 13:24:38 +00:00
|
|
|
$noderestab->close;
|
2008-03-26 18:20:05 +00:00
|
|
|
return \%snhash;
|
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ($service eq "conserver")
|
|
|
|
{
|
|
|
|
|
|
|
|
$nodehmtab = xCAT::Table->new('nodehm');
|
|
|
|
unless ($nodehmtab) # no nodehm table default to site->master
|
|
|
|
{
|
|
|
|
xCAT::MsgUtils->message('I',
|
|
|
|
"Unable to open nodehm table.\n");
|
|
|
|
|
|
|
|
# use service node
|
|
|
|
foreach my $node (@node_list)
|
|
|
|
{
|
|
|
|
$sn =
|
|
|
|
$noderestab->getNodeAttribs($node, [$snattribute]);
|
|
|
|
if ($sn)
|
|
|
|
{
|
|
|
|
my $key = $sn->{$snattribute};
|
|
|
|
push @{$snhash{$key}}, $node;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{ # no service node use master
|
|
|
|
push @{$snhash{$master}}, $node;
|
|
|
|
}
|
|
|
|
}
|
2008-04-22 13:24:38 +00:00
|
|
|
$nodehmtab->close;
|
2008-03-26 18:20:05 +00:00
|
|
|
return \%snhash;
|
|
|
|
}
|
|
|
|
|
2008-03-27 12:35:01 +00:00
|
|
|
# can read the nodehm table
|
2008-03-26 18:20:05 +00:00
|
|
|
foreach my $node (@node_list)
|
|
|
|
{
|
|
|
|
$sn = $nodehmtab->getNodeAttribs($node, ['conserver']);
|
2008-03-27 12:35:01 +00:00
|
|
|
if ($sn and $sn->{'conserver'})
|
2008-03-26 18:20:05 +00:00
|
|
|
{
|
2008-03-27 12:35:01 +00:00
|
|
|
|
2008-03-31 15:52:36 +00:00
|
|
|
# see if both MN and Node address in attribute
|
2008-03-27 12:35:01 +00:00
|
|
|
my ($msattr, $nodeattr) = split ',', $sn->{'conserver'};
|
|
|
|
my $key = $msattr;
|
|
|
|
if ($request eq "Node")
|
|
|
|
{
|
|
|
|
if ($nodeattr) # override with Node, if it exists
|
|
|
|
{
|
|
|
|
$key = $nodeattr;
|
|
|
|
}
|
|
|
|
}
|
2008-03-26 18:20:05 +00:00
|
|
|
push @{$snhash{$key}}, $node;
|
|
|
|
}
|
|
|
|
else
|
2008-03-27 12:35:01 +00:00
|
|
|
{ # use service node
|
2008-03-26 18:20:05 +00:00
|
|
|
$sn =
|
|
|
|
$noderestab->getNodeAttribs($node, [$snattribute]);
|
2008-03-27 12:35:01 +00:00
|
|
|
if ($sn and $sn->{$snattribute})
|
2008-03-26 18:20:05 +00:00
|
|
|
{
|
|
|
|
my $key = $sn->{$snattribute};
|
|
|
|
push @{$snhash{$key}}, $node;
|
|
|
|
}
|
|
|
|
else
|
2008-03-27 12:35:01 +00:00
|
|
|
{ # no service node use master
|
2008-03-26 18:20:05 +00:00
|
|
|
push @{$snhash{$master}}, $node;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-04-22 13:24:38 +00:00
|
|
|
$noderestab->close;
|
|
|
|
$nodehmtab->close;
|
2008-03-26 18:20:05 +00:00
|
|
|
return \%snhash;
|
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
xCAT::MsgUtils->message('E',
|
|
|
|
"Invalid service=$service input.\n");
|
|
|
|
$::ERROR_RC = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return \%snhash;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2008-04-23 18:14:57 +00:00
|
|
|
#-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
=head3 toIP
|
|
|
|
|
|
|
|
IPv4 function to convert hostname to IP address
|
|
|
|
|
|
|
|
=cut
|
|
|
|
|
|
|
|
#-----------------------------------------------------------------------------
|
2008-04-22 13:24:38 +00:00
|
|
|
sub toIP
|
|
|
|
{
|
2008-04-02 19:39:39 +00:00
|
|
|
|
2008-04-22 13:24:38 +00:00
|
|
|
if ($_[0] =~ /^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$/)
|
|
|
|
{
|
|
|
|
return ([0, $_[0]]);
|
|
|
|
}
|
|
|
|
my $packed_ip = gethostbyname($_[0]);
|
|
|
|
if (!$packed_ip or $!)
|
|
|
|
{
|
|
|
|
return ([1, "Cannot Resolve: $_[0]\n"]);
|
|
|
|
}
|
|
|
|
return ([0, inet_ntoa($packed_ip)]);
|
|
|
|
}
|
2008-04-02 19:39:39 +00:00
|
|
|
|
2008-04-23 18:14:57 +00:00
|
|
|
#-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
=head3 isSN
|
|
|
|
|
|
|
|
Determines if the input node name is a service node
|
2008-05-06 15:41:58 +00:00
|
|
|
Reads the servicenode table. nodename must be service node name as
|
|
|
|
known by the Management node.
|
2008-04-23 18:14:57 +00:00
|
|
|
|
|
|
|
returns 1 if input host is a service node
|
|
|
|
Arguments:
|
|
|
|
hostname
|
|
|
|
Returns:
|
|
|
|
1 - is Service Node
|
|
|
|
0 - is not a Service Node
|
|
|
|
Globals:
|
|
|
|
none
|
|
|
|
Error:
|
|
|
|
none
|
|
|
|
Example:
|
|
|
|
if (xCAT::Utils->isSN($nodename)) { blah; }
|
|
|
|
Comments:
|
|
|
|
none
|
|
|
|
|
|
|
|
=cut
|
|
|
|
|
|
|
|
#-----------------------------------------------------------------------------
|
|
|
|
sub isSN
|
|
|
|
{
|
|
|
|
my ($class, $node) = @_;
|
|
|
|
|
2008-05-06 15:41:58 +00:00
|
|
|
# reads all nodes from the service node table
|
|
|
|
my @servicenodes;
|
|
|
|
my $servicenodetab = xCAT::Table->new('servicenode');
|
|
|
|
unless ($servicenodetab) # no servicenode table
|
|
|
|
{
|
|
|
|
xCAT::MsgUtils->message('I', "Unable to open servicenode table.\n");
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
2008-05-07 17:35:14 +00:00
|
|
|
@nodes = $servicenodetab->getAllNodeAttribs(['tftpserver']);
|
2008-05-06 15:41:58 +00:00
|
|
|
$servicenodetab->close;
|
|
|
|
foreach my $nodes (@nodes)
|
2008-04-23 18:14:57 +00:00
|
|
|
{
|
2008-05-06 15:41:58 +00:00
|
|
|
if ($node eq $nodes->{node})
|
2008-04-23 18:14:57 +00:00
|
|
|
{
|
2008-05-07 17:35:14 +00:00
|
|
|
return 1; # match
|
2008-04-23 18:14:57 +00:00
|
|
|
}
|
|
|
|
}
|
2008-05-06 15:41:58 +00:00
|
|
|
|
2008-04-23 18:14:57 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2008-04-23 19:11:16 +00:00
|
|
|
|
|
|
|
#-----------------------------------------------------------------------------
|
|
|
|
|
2008-04-24 17:28:35 +00:00
|
|
|
=head3 getAllSN
|
2008-04-23 19:11:16 +00:00
|
|
|
|
2008-05-06 15:41:58 +00:00
|
|
|
Returns an array of all service nodes from service node table
|
2008-04-23 19:11:16 +00:00
|
|
|
|
|
|
|
Arguments:
|
|
|
|
none
|
|
|
|
Returns:
|
|
|
|
array of Service Nodes or empty array, if none
|
|
|
|
Globals:
|
|
|
|
none
|
|
|
|
Error:
|
|
|
|
1 - error
|
|
|
|
Example:
|
|
|
|
@allSN=xCAT::Utils->get_AllSN
|
|
|
|
Comments:
|
|
|
|
none
|
|
|
|
|
|
|
|
=cut
|
|
|
|
|
|
|
|
#-----------------------------------------------------------------------------
|
2008-04-24 17:28:35 +00:00
|
|
|
sub getAllSN
|
2008-04-23 19:11:16 +00:00
|
|
|
{
|
|
|
|
|
2008-05-06 15:41:58 +00:00
|
|
|
# reads all nodes from the service node table
|
2008-04-23 19:11:16 +00:00
|
|
|
my @servicenodes;
|
2008-05-06 15:41:58 +00:00
|
|
|
my $servicenodetab = xCAT::Table->new('servicenode');
|
2008-05-07 17:35:14 +00:00
|
|
|
unless ($servicenodetab) # no servicenode table
|
2008-04-23 19:11:16 +00:00
|
|
|
{
|
2008-05-07 17:35:14 +00:00
|
|
|
xCAT::MsgUtils->message('I', "Unable to open servicenode table.\n");
|
|
|
|
$servicenodetab->close;
|
|
|
|
return @servicenodes;
|
|
|
|
|
|
|
|
}
|
|
|
|
my @nodes = $servicenodetab->getAllNodeAttribs(['tftpserver']);
|
|
|
|
foreach my $nodes (@nodes)
|
|
|
|
{
|
|
|
|
push @servicenodes, $nodes->{node};
|
2008-04-23 19:11:16 +00:00
|
|
|
}
|
2008-05-06 15:41:58 +00:00
|
|
|
$servicenodetab->close;
|
2008-04-23 19:11:16 +00:00
|
|
|
return @servicenodes;
|
|
|
|
}
|
2008-04-24 17:28:35 +00:00
|
|
|
|
|
|
|
#-----------------------------------------------------------------------------
|
|
|
|
|
2008-05-06 15:41:58 +00:00
|
|
|
=head3 getSNandNodes
|
|
|
|
|
|
|
|
Returns an hash-array of all service nodes and the nodes they service
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
none
|
|
|
|
#-----------------------------------------------------------------------------
|
|
|
|
|
2008-04-24 17:28:35 +00:00
|
|
|
=head3 getSNandNodes
|
|
|
|
|
|
|
|
Returns an hash-array of all service nodes and the nodes they service
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
none
|
|
|
|
Returns:
|
|
|
|
Service Nodes and the nodes they service or empty , if none
|
|
|
|
Globals:
|
|
|
|
none
|
|
|
|
Error:
|
|
|
|
1 - error
|
|
|
|
Example:
|
|
|
|
$sn=xCAT::Utils->getSNandNodes()
|
|
|
|
Comments:
|
|
|
|
none
|
|
|
|
|
|
|
|
=cut
|
|
|
|
|
|
|
|
#-----------------------------------------------------------------------------
|
|
|
|
sub getSNandNodes
|
|
|
|
{
|
|
|
|
|
|
|
|
# read all the nodes from the nodelist table
|
|
|
|
# call get_ServiceNode to find which Service Node
|
|
|
|
# the node belongs to.
|
|
|
|
my %sn;
|
|
|
|
my @nodes;
|
|
|
|
my $nodelisttab = xCAT::Table->new('nodelist');
|
|
|
|
my $recs = $nodelisttab->getAllEntries();
|
|
|
|
foreach (@$recs)
|
|
|
|
{
|
|
|
|
push @nodes, $_->{node};
|
|
|
|
}
|
|
|
|
$nodelisttab->close;
|
|
|
|
$sn = xCAT::Utils->get_ServiceNode(\@nodes, "xcat", "MN");
|
|
|
|
return $sn;
|
|
|
|
}
|
2008-05-06 17:22:15 +00:00
|
|
|
|
|
|
|
#-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
=head3 getSNList
|
|
|
|
|
2008-05-07 17:35:14 +00:00
|
|
|
Reads the servicenode table. Will return all the enabled Service Nodes
|
2008-05-06 17:22:15 +00:00
|
|
|
that will setup the input Service ( e.g tftpserver,nameserver,etc)
|
|
|
|
If service is blank, then will return the list of all enabled Service
|
|
|
|
Nodes.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
Servicename ( xcat,tftpserver,dhcpserver,conserver,etc)
|
|
|
|
Returns:
|
|
|
|
Array of service node names
|
|
|
|
Globals:
|
|
|
|
none
|
|
|
|
Error:
|
|
|
|
1 - error
|
|
|
|
Example:
|
|
|
|
$sn= xCAT::Utils->getSNList($servicename) { blah; }
|
|
|
|
$sn= xCAT::Utils->getSNList() { blah; }
|
|
|
|
Comments:
|
|
|
|
none
|
|
|
|
|
|
|
|
=cut
|
|
|
|
|
|
|
|
#-----------------------------------------------------------------------------
|
|
|
|
sub getSNList
|
|
|
|
{
|
|
|
|
my ($class, $service) = @_;
|
|
|
|
|
|
|
|
# reads all nodes from the service node table
|
|
|
|
my @servicenodes;
|
2008-05-20 17:56:53 +00:00
|
|
|
my $servicenodetab = xCAT::Table->new('servicenode',-create=>1);
|
2008-05-06 17:22:15 +00:00
|
|
|
unless ($servicenodetab) # no servicenode table
|
|
|
|
{
|
|
|
|
xCAT::MsgUtils->message('I', "Unable to open servicenode table.\n");
|
2008-05-20 17:56:53 +00:00
|
|
|
return ();
|
2008-05-06 17:22:15 +00:00
|
|
|
}
|
2008-05-07 17:35:14 +00:00
|
|
|
my @nodes = $servicenodetab->getAllNodeAttribs([$service]);
|
2008-05-06 17:22:15 +00:00
|
|
|
$servicenodetab->close;
|
|
|
|
foreach my $node (@nodes)
|
|
|
|
{
|
2008-05-07 17:35:14 +00:00
|
|
|
if ($service eq "") # want all the service nodes
|
2008-05-06 17:22:15 +00:00
|
|
|
{
|
|
|
|
push @servicenodes, $node->{node};
|
|
|
|
}
|
|
|
|
else
|
2008-05-07 17:35:14 +00:00
|
|
|
{ # looking for a particular service
|
|
|
|
if ($node->{$service})
|
|
|
|
{ # if null then do not add node
|
|
|
|
my $value = $node->{$service};
|
|
|
|
$value =~ tr/a-z/A-Z/; # convert to upper
|
|
|
|
# value 1 or yes or blank then we setup the service
|
|
|
|
if (($value == 1) || ($value eq "YES"))
|
|
|
|
{
|
|
|
|
push @servicenodes, $node->{node};
|
2008-05-06 17:22:15 +00:00
|
|
|
|
2008-05-07 17:35:14 +00:00
|
|
|
}
|
2008-05-06 17:22:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return @servicenodes;
|
|
|
|
}
|
2008-05-13 12:36:13 +00:00
|
|
|
|
2008-05-12 19:49:41 +00:00
|
|
|
#-------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
=head3 isMounted
|
|
|
|
Checks if the input directory is already mounted
|
|
|
|
Arguments:
|
|
|
|
directory
|
|
|
|
Returns:
|
|
|
|
1 - directory is mounted
|
|
|
|
0 - directory is not mounted
|
|
|
|
Globals:
|
|
|
|
none
|
|
|
|
Error:
|
|
|
|
-1 error
|
|
|
|
Example:
|
|
|
|
if (xCAT::Utils->isMounted($directory)) { blah; }
|
|
|
|
Comments:
|
|
|
|
none
|
|
|
|
=cut
|
|
|
|
|
|
|
|
#-------------------------------------------------------------------------------
|
|
|
|
sub isMounted
|
|
|
|
{
|
|
|
|
my ($class, $directory) = @_;
|
|
|
|
$cmd = "df -P $directory";
|
|
|
|
my @output = xCAT::Utils->runcmd($cmd, -1);
|
|
|
|
foreach my $line (@output)
|
|
|
|
{
|
|
|
|
my ($file_sys, $blocks, $used, $avail, $cap, $mount_point) =
|
|
|
|
split(' ', $line);
|
|
|
|
if ($mount_point eq $directory)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2008-05-06 17:22:15 +00:00
|
|
|
|
2007-12-20 19:02:45 +00:00
|
|
|
1;
|