2008-06-24 16:22:55 +00:00
#!/usr/bin/env perl -w
2007-12-12 19:16:31 +00:00
# IBM(c) 2007 EPL license http://www.eclipse.org/legal/epl-v10.html
#####################################################
#
# xCAT plugin package to handle the xcat2nim command.
#
#####################################################
package xCAT_plugin::xcat2nim ;
2008-09-24 18:14:11 +00:00
use Sys::Hostname ;
2007-12-12 19:16:31 +00:00
use xCAT::NodeRange ;
use xCAT::Schema ;
use xCAT::Utils ;
use xCAT::DBobjUtils ;
use Data::Dumper ;
use Getopt::Long ;
use xCAT::MsgUtils ;
2008-06-24 16:22:55 +00:00
use strict ;
2007-12-12 19:16:31 +00:00
# options can be bundled up like -vV
Getopt::Long:: Configure ( "bundling" ) ;
$ Getopt:: Long:: ignorecase = 0 ;
#
# Globals
#
@ ::noderange ; # list of nodes derived from command line
#------------------------------------------------------------------------------
= head1 xcat2nim
This program module file supports the xcat2nim command .
= cut
#------------------------------------------------------------------------------
= head2 xCAT xcat2nim command
= cut
#------------------------------------------------------------------------------
#----------------------------------------------------------------------------
= head3 handled_commands
Return a list of commands handled by this plugin
= cut
#-----------------------------------------------------------------------------
sub handled_commands
{
return {
xcat2nim = > "xcat2nim"
} ;
}
2008-09-10 15:51:29 +00:00
#-------------------------------------------------------
= head3 preprocess_request
Check and setup for hierarchy
= cut
#-------------------------------------------------------
sub preprocess_request
{
my $ req = shift ;
my $ cb = shift ;
my % sn ;
if ( $ req - > { _xcatdest } ) { return [ $ req ] ; } #exit if preprocessed
2008-09-24 18:14:11 +00:00
my $ nodes = $ req - > { node } ; # this may not be the list of nodes we need!
my $ command = $ req - > { command } - > [ 0 ] ;
$ ::args = $ req - > { arg } ;
2008-09-10 15:51:29 +00:00
my $ service = "xcat" ;
my @ requests ;
2008-09-24 18:14:11 +00:00
if ( $ command =~ /xcat2nim/ ) {
# handle -h etc.
# list of nodes could be derived multiple ways!!
my ( $ ret , $ mynodes ) = & prexcat2nim ( $ cb ) ;
if ( $ ret ) { # either error or -h was processed etc.
my $ rsp ;
if ( $ ret eq "1" ) {
$ rsp - > { errorcode } - > [ 0 ] = $ ret ;
push @ { $ rsp - > { data } } , "Return=$ret." ;
xCAT::MsgUtils - > message ( "E" , $ rsp , $ cb , $ ret ) ;
}
return undef ;
} else {
if ( $ mynodes ) {
# set up the requests to go to the service nodes
# all get the same request
# get the hash of service nodes - for the nodes that were provided
my $ sn ;
$ sn = xCAT::Utils - > get_ServiceNode ( $ mynodes , $ service , "MN" ) ;
foreach my $ snkey ( keys %$ sn ) {
my $ reqcopy = { %$ req } ;
$ reqcopy - > { node } = $ sn - > { $ snkey } ;
$ reqcopy - > { '_xcatdest' } = $ snkey ;
push @ requests , $ reqcopy ;
}
return \ @ requests ;
}
}
}
return undef ;
2008-09-10 15:51:29 +00:00
}
2007-12-12 19:16:31 +00:00
#----------------------------------------------------------------------------
= head3 process_request
Check for xCAT command and call the appropriate subroutine .
Arguments:
Returns:
0 - OK
1 - error
Globals:
Error:
Example:
Comments:
= cut
#-----------------------------------------------------------------------------
sub process_request
{
$ ::request = shift ;
2008-09-24 18:14:11 +00:00
my $ callback = shift ;
2007-12-12 19:16:31 +00:00
my $ ret ;
my $ msg ;
2008-06-24 16:22:55 +00:00
my $ rsp ;
2007-12-12 19:16:31 +00:00
# globals used by all subroutines.
$ ::command = $ ::request - > { command } - > [ 0 ] ;
$ ::args = $ ::request - > { arg } ;
$ ::filedata = $ ::request - > { stdin } - > [ 0 ] ;
2008-09-24 18:14:11 +00:00
( $ ret , $ msg ) = & x2n ( $ callback ) ;
if ( $ ret > 0 ) {
my $ rsp ;
$ rsp - > { errorcode } - > [ 0 ] = $ ret ;
push @ { $ rsp - > { data } } , "Return=$ret." ;
xCAT::MsgUtils - > message ( "E" , $ rsp , $ callback , $ ret ) ;
}
return 0 ;
}
#----------------------------------------------------------------------------
= head3 prexcat2nim
Preprocessing for the xcat2nim command .
Arguments:
Returns:
0 - OK - needs further processing
1 - error - done processing this cmd
2 - help or version - done processing this cmd
Comments:
= cut
#-----------------------------------------------------------------------------
sub prexcat2nim
{
my $ callback = shift ;
$ ::callback = $ callback ;
2007-12-12 19:16:31 +00:00
2008-09-24 18:14:11 +00:00
my @ nodelist ; # pass back list of nodes
$ ::msgstr = "" ;
if ( defined ( @ { $ ::args } ) ) {
@ ARGV = @ { $ ::args } ;
} else {
& xcat2nim_usage ;
return 1 ;
}
Getopt::Long:: Configure ( "no_pass_through" ) ;
if (
! GetOptions (
'all|a' = > \ $ ::opt_a ,
'f|force' = > \ $ ::FORCE ,
'help|h|?' = > \ $ ::opt_h ,
'list|l' = > \ $ ::opt_l ,
'update|u' = > \ $ ::opt_u ,
'remove|r' = > \ $ ::opt_r ,
'o=s' = > \ $ ::opt_o ,
't=s' = > \ $ ::opt_t ,
'verbose|V' = > \ $ ::opt_V ,
'version|v' = > \ $ ::opt_v ,
)
)
{
& xcat2nim_usage ;
return 1 ;
}
# Option -h for Help
if ( $ ::opt_h )
2007-12-12 19:16:31 +00:00
{
2008-09-24 18:14:11 +00:00
& xcat2nim_usage ;
return 2 ;
2007-12-12 19:16:31 +00:00
}
2008-09-24 18:14:11 +00:00
# Option -v for version - do we need this???
if ( $ ::opt_v )
{
my $ rsp ;
my $ version = xCAT::Utils - > Version ( ) ;
$ rsp - > { data } - > [ 0 ] = "xcat2nim - $version" ;
xCAT::MsgUtils - > message ( "I" , $ rsp , $ callback ) ;
return 2 ; # - just exit
}
######## TODO ##################
# this needs to be cleaned up!!!!!
# too much duplicate code being called ...
#################################
# process the command line
my $ rc = & processArgs ;
my % objhash = xCAT::DBobjUtils - > getobjdefs ( \ % ::objtype ) ;
# need to figure out what nodes are involved so
# we can create the node/group defs on the
# correct service nodes
foreach my $ o ( @ ::clobjnames ) {
if ( $ ::objtype { $ o } eq 'node' ) {
push ( @ nodelist , $ o ) ;
}
if ( $ ::objtype { $ o } eq 'group' ) {
my $ memberlist = xCAT::DBobjUtils - > getGroupMembers ( $ o , \ % objhash ) ;
my @ members = split ( ',' , $ memberlist ) ;
if ( @ members ) {
foreach my $ n ( @ members ) {
push ( @ nodelist , $ n ) ;
}
}
}
2008-03-07 20:37:33 +00:00
}
2008-09-24 18:14:11 +00:00
# make sure the nodes are resolvable
# - if not then exit
foreach my $ n ( @ nodelist ) {
my $ packed_ip = gethostbyname ( $ n ) ;
if ( ! $ packed_ip ) {
my $ rsp ;
$ rsp - > { data } - > [ 0 ] = "Could not resolve node $n.\n" ;
xCAT::MsgUtils - > message ( "E" , $ rsp , $ callback ) ;
return 1 ;
}
}
return ( 0 , \ @ nodelist ) ;
2007-12-12 19:16:31 +00:00
}
#----------------------------------------------------------------------------
= head3 processArgs
Process the command line .
Also - Process any input files provided on cmd line .
Arguments:
Returns:
0 - OK
1 - just print usage
2 - error
Globals:
Error:
Example:
Comments:
= cut
#-----------------------------------------------------------------------------
sub processArgs
{
my $ gotattrs = 0 ;
2008-06-25 15:37:19 +00:00
if ( defined ( @ { $ ::args } ) ) {
@ ARGV = @ { $ ::args } ;
} else {
return 3 ;
}
2007-12-12 19:16:31 +00:00
2008-06-24 16:22:55 +00:00
my % ObjTypeHash ;
2007-12-12 19:16:31 +00:00
# parse the options
2008-03-07 20:37:33 +00:00
Getopt::Long:: Configure ( "no_pass_through" ) ;
2007-12-12 19:16:31 +00:00
if (
! GetOptions (
'all|a' = > \ $ ::opt_a ,
2008-06-25 14:42:23 +00:00
'f|force' = > \ $ ::FORCE ,
2008-02-21 19:17:18 +00:00
'help|h|?' = > \ $ ::opt_h ,
2008-02-18 20:12:15 +00:00
'list|l' = > \ $ ::opt_l ,
'update|u' = > \ $ ::opt_u ,
'remove|r' = > \ $ ::opt_r ,
2007-12-12 19:16:31 +00:00
'o=s' = > \ $ ::opt_o ,
't=s' = > \ $ ::opt_t ,
'verbose|V' = > \ $ ::opt_V ,
'version|v' = > \ $ ::opt_v ,
)
)
{
# return 2;
}
# can get object names in many ways - easier to keep track
$ ::objectsfrom_args = 0 ;
$ ::objectsfrom_opto = 0 ;
$ ::objectsfrom_optt = 0 ;
$ ::objectsfrom_opta = 0 ;
#
# process @ARGV
#
# - put attr=val operands in ATTRS hash
while ( my $ a = shift ( @ ARGV ) )
{
if ( ! ( $ a =~ /=/ ) )
{
# the first arg could be a noderange or a list of args
if ( ( $ ::opt_t ) && ( $ ::opt_t ne 'node' ) )
{
# if we know the type isn't "node" then set the object list
@ ::clobjnames = split ( ',' , $ a ) ;
$ ::objectsfrom_args = 1 ;
}
elsif ( ! $ ::opt_t || ( $ ::opt_t eq 'node' ) )
{
# if the type was not provided or it is "node"
# then set noderange
@ ::noderange = & noderange ( $ a , 0 ) ;
@ ::clobjnames = @ ::noderange ;
$ ::objectsfrom_args = 1 ;
}
}
else
{
# if it has an "=" sign its an attr=val - we hope
# - this will handle "attr= "
my ( $ attr , $ value ) = $ a =~ /^\s*(\S+?)\s*=\s*(\S*.*)$/ ;
if ( ! defined ( $ attr ) || ! defined ( $ value ) )
{
my $ rsp ;
$ rsp - > { data } - > [ 0 ] = "Incorrect \'attr=val\' pair - $a\n" ;
xCAT::MsgUtils - > message ( "E" , $ rsp , $ ::callback ) ;
return 3 ;
}
$ gotattrs = 1 ;
# put attr=val in hash
$ ::ATTRS { $ attr } = $ value ;
}
}
# Option -V for verbose output
if ( defined ( $ ::opt_V ) )
{
$ ::verbose = 1 ;
$ ::VERBOSE = 1 ;
}
#
# determine the object types
#
# could have comma seperated list of types
if ( $ ::opt_t )
{
my @ tmptypes ;
if ( $ ::opt_t =~ /,/ )
{
# can't have mult types when using attr=val
if ( $ gotattrs )
{
my $ rsp ;
$ rsp - > { data } - > [ 0 ] =
2008-09-24 18:14:11 +00:00
"$::msgstr Cannot combine multiple types with \'att=val\' pairs on the command line.\n" ;
2007-12-12 19:16:31 +00:00
xCAT::MsgUtils - > message ( "E" , $ rsp , $ ::callback ) ;
return 3 ;
}
else
{
@ tmptypes = split ( ',' , $ ::opt_t ) ;
}
}
else
{
push ( @ tmptypes , $ ::opt_t ) ;
}
# check for valid types
my @ xdeftypes ;
foreach my $ k ( keys % { xCAT::Schema:: defspec } )
{
push ( @ xdeftypes , $ k ) ;
}
foreach my $ t ( @ tmptypes )
{
if ( ! grep ( /$t/ , @ xdeftypes ) )
{
my $ rsp ;
$ rsp - > { data } - > [ 0 ] =
2008-09-24 18:14:11 +00:00
"$::msgstr Type \'$t\' is not a valid xCAT object type.\n" ;
2007-12-12 19:16:31 +00:00
$ rsp - > { data } - > [ 1 ] = "Skipping to the next type.\n" ;
xCAT::MsgUtils - > message ( "I" , $ rsp , $ ::callback ) ;
}
else
{
chomp $ t ;
push ( @ ::clobjtypes , $ t ) ;
}
}
}
# must have object type(s) - default if not provided
if ( ! @ ::clobjtypes && ! $ ::opt_a )
{
# make the default type = 'node' if not specified
push ( @ ::clobjtypes , 'node' ) ;
my $ rsp ;
2008-09-24 18:14:11 +00:00
$ rsp - > { data } - > [ 0 ] = "$::msgstr Assuming an object type of \'node\'.\n" ;
2007-12-12 19:16:31 +00:00
xCAT::MsgUtils - > message ( "I" , $ rsp , $ ::callback ) ;
}
#
# determine the object names
#
# - get object names from the -o option or the noderange
# - this assumes the ronderange was not provided as an arg!!!
if ( $ ::opt_o )
{
$ ::objectsfrom_opto = 1 ;
# make a list
if ( $ ::opt_o =~ /,/ )
{
@ ::clobjnames = split ( ',' , $ ::opt_o ) ;
}
else
{
push ( @ ::clobjnames , $ ::opt_o ) ;
}
}
# if there is no opt_o & no noderange then try to find all the objects
# of the given types.
if ( $ ::opt_t
&& ! ( $ ::opt_o
|| $ ::opt_a
|| @ ::noderange
|| @ ::clobjnames ) )
{
my @ tmplist ;
$ ::objectsfrom_optt = 1 ;
# could have multiple type
foreach my $ t ( @ ::clobjtypes )
{
# look up all objects of this type in the DB ???
@ tmplist = xCAT::DBobjUtils - > getObjectsOfType ( $ t ) ;
unless ( @ tmplist )
{
my $ rsp ;
$ rsp - > { data } - > [ 0 ] =
2008-09-24 18:14:11 +00:00
"$::msgstr Could not get objects of type \'$t\'.\n" ;
2007-12-12 19:16:31 +00:00
$ rsp - > { data } - > [ 1 ] = "Skipping to the next type.\n" ;
xCAT::MsgUtils - > message ( "I" , $ rsp , $ ::callback ) ;
next ;
}
# add objname and type to hash and global list
foreach my $ o ( @ tmplist )
{
push ( @ ::clobjnames , $ o ) ;
$ ObjTypeHash { $ o } = $ t ;
}
}
}
# can't have -a with other obj sources
if ( $ ::opt_a
&& ( $ ::opt_o || @ ::noderange ) )
{
my $ rsp ;
$ rsp - > { data } - > [ 0 ] =
2008-09-24 18:14:11 +00:00
"$::msgstr Cannot use \'-a\' with \'-o\' or a noderange.\n" ;
2007-12-12 19:16:31 +00:00
xCAT::MsgUtils - > message ( "E" , $ rsp , $ ::callback ) ;
return 3 ;
}
# if -a then get a list of all DB objects
2008-06-24 16:22:55 +00:00
my % AllObjTypeHash ;
2007-12-12 19:16:31 +00:00
if ( $ ::opt_a )
{
my @ tmplist ;
$ ::objectsfrom_opta = 1 ;
# for every type of data object get the list of defined objects
foreach my $ t ( keys % { xCAT::Schema:: defspec } )
{
# just for node and group for now!!!!!!!
if ( ( $ t eq 'node' ) || ( $ t eq 'group' ) ) {
my @ tmplist ;
@ tmplist = xCAT::DBobjUtils - > getObjectsOfType ( $ t ) ;
# add objname and type to hash and global list
if ( scalar ( @ tmplist ) > 0 )
{
foreach my $ o ( @ tmplist )
{
push ( @ ::clobjnames , $ o ) ;
$ AllObjTypeHash { $ o } = $ t ;
}
}
}
}
}
# must have object name(s) -
if ( ! @ ::clobjnames )
{
return 3 ;
}
# create hash with object names and types
foreach my $ o ( @ ::clobjnames )
{
if ( $ ::objectsfrom_opta ) {
# if the object names came from the "-a" option then
% ::objtype = % AllObjTypeHash ;
} elsif ( $ ::objectsfrom_optt ) {
# if the names came from the opt_t option
% ::objtype = % ObjTypeHash ;
} elsif ( $ ::objectsfrom_args || $ ::objectsfrom_opto ) {
# from the opt_o or as an argument
# - there can only be one type
$ ::objtype { $ o } = @ ::clobjtypes [ 0 ] ;
}
}
return 0 ;
}
#----------------------------------------------------------------------------
= head3 x2n
Support for the xcat2nim command .
Arguments:
Returns:
0 - OK
1 - error
Globals:
Error:
Example:
Comments:
= cut
#-----------------------------------------------------------------------------
sub x2n
{
2008-06-25 14:42:23 +00:00
my $ callback = shift ;
2007-12-12 19:16:31 +00:00
my $ rc = 0 ;
my $ error = 0 ;
2008-09-24 18:14:11 +00:00
# get the local short host name
( $ ::local_host = hostname ( ) ) =~ s/\..*$// ;
chomp $ ::local_host ;
if ( xCAT::Utils - > isMN ( ) ) {
$ ::msgstr = "" ;
} else {
$ ::msgstr = "$::local_host: " ;
}
2007-12-12 19:16:31 +00:00
# process the command line
$ rc = & processArgs ;
if ( $ rc != 0 )
{
# rc: 0 - ok, 1 - return, 2 - help, 3 - error
if ( $ rc != 1 )
{
& xcat2nim_usage ;
}
return ( $ rc - 1 ) ;
}
# get all the attrs for these definitions
% ::objhash = xCAT::DBobjUtils - > getobjdefs ( \ % ::objtype ) ;
if ( ! defined ( % ::objhash ) )
{
my $ rsp ;
2008-09-24 18:14:11 +00:00
$ rsp - > { data } - > [ 0 ] = "$::msgstr Could not get xCAT object definitions.\n" ;
2007-12-12 19:16:31 +00:00
xCAT::MsgUtils - > message ( "E" , $ rsp , $ ::callback ) ;
return 1 ;
}
# create NIM defs for each xCAT def
foreach my $ objname ( keys % ::objhash )
{
# list the NIM object definition - if there is one
2008-02-18 20:12:15 +00:00
if ( $ ::opt_l || $ ::opt_r ) {
if ( & rm_or_list_nim_object ( $ objname , $ ::objtype { $ objname } ) ) {
2007-12-12 19:16:31 +00:00
# the routine failed
2008-02-14 14:25:44 +00:00
$ error + + ;
2007-12-12 19:16:31 +00:00
next ;
}
2008-02-18 20:12:15 +00:00
} else {
2007-12-12 19:16:31 +00:00
2008-09-24 18:14:11 +00:00
# create a NIM machine definition
if ( $ ::objtype { $ objname } eq 'node' ) {
# need to set group type to either static or dynamic
if ( mkclientdef ( $ objname , $ callback ) ) {
# could not create client definition
$ error + + ;
}
next ;
}
2007-12-12 19:16:31 +00:00
2008-09-24 18:14:11 +00:00
# create a NIM group definition
if ( $ ::objtype { $ objname } eq 'group' ) {
2008-10-10 15:54:06 +00:00
$ ::objhash { $ objname } { 'grouptype' } = 'static' ;
2008-09-24 18:14:11 +00:00
if ( mkgrpdef ( $ objname , $ callback ) ) {
# could not create group definition
$ error + + ;
}
next ;
2007-12-12 19:16:31 +00:00
}
2008-02-18 20:12:15 +00:00
}
2007-12-12 19:16:31 +00:00
}
if ( $ error )
{
my $ rsp ;
$ rsp - > { data } - > [ 0 ] =
2008-09-24 18:14:11 +00:00
"$::msgstr One or more errors occured.\n" ;
2007-12-12 19:16:31 +00:00
xCAT::MsgUtils - > message ( "E" , $ rsp , $ ::callback ) ;
return 1 ;
}
else
{
if ( $ ::verbose )
{
# give results
2008-02-18 20:12:15 +00:00
my $ rsp ;
if ( $ ::opt_r ) {
2008-09-24 18:14:11 +00:00
$ rsp - > { data } - > [ 0 ] = "$::msgstr The following definitions were removed:" ;
2008-02-18 20:12:15 +00:00
} elsif ( $ ::opt_u ) {
2008-09-24 18:14:11 +00:00
$ rsp - > { data } - > [ 0 ] = "$::msgstr The following definitions were updated:" ;
2008-02-18 20:12:15 +00:00
} elsif ( ! $ ::opt_l ) {
2008-09-24 18:14:11 +00:00
$ rsp - > { data } - > [ 0 ] = "$::msgstr The following definitions were created:" ;
2008-02-18 20:12:15 +00:00
}
2007-12-12 19:16:31 +00:00
xCAT::MsgUtils - > message ( "I" , $ rsp , $ ::callback ) ;
my $ n = 1 ;
2008-02-20 20:50:44 +00:00
foreach my $ o ( sort ( keys % ::objhash ) )
2007-12-12 19:16:31 +00:00
{
$ rsp - > { data } - > [ $ n ] = "$o\n" ;
$ n + + ;
}
xCAT::MsgUtils - > message ( "I" , $ rsp , $ ::callback ) ;
}
else
{
2008-02-18 20:12:15 +00:00
if ( ! $ ::opt_l ) {
my $ rsp ;
$ rsp - > { data } - > [ 0 ] =
2008-09-24 18:14:11 +00:00
"$::local_host: NIM operations have completed successfully.\n" ;
2008-02-18 20:12:15 +00:00
xCAT::MsgUtils - > message ( "I" , $ rsp , $ ::callback ) ;
}
2007-12-12 19:16:31 +00:00
}
return 0 ;
}
}
#----------------------------------------------------------------------------
= head3 mkclientdef
Create a NIM client definition .
Arguments:
Returns:
0 - OK
1 - error
Globals:
Error:
Example:
Comments:
= cut
#-----------------------------------------------------------------------------
sub mkclientdef
{
2008-06-25 14:42:23 +00:00
my $ node = shift ;
my $ callback = shift ;
2007-12-12 19:16:31 +00:00
my $ cabletype = undef ;
my $ ifattr = undef ;
2008-06-24 16:22:55 +00:00
my % finalattrs ;
my $ shorthost ;
my $ net_name ;
my $ adaptertype ;
my $ nim_args ;
my $ nim_type ;
2008-09-24 18:14:11 +00:00
# this code runs on service nodes - which are NIM masters
2007-12-12 19:16:31 +00:00
# check to see if the client is already defined
# - sets $::client_exists
2008-09-24 18:14:11 +00:00
if ( & check_nim_client ( $ node , $ ::local_host ) ) {
2007-12-12 19:16:31 +00:00
# the routine failed
return 1 ;
}
2008-06-25 14:42:23 +00:00
# need short host name for NIM client defs
( $ shorthost = $ node ) =~ s/\..*$// ;
2007-12-12 19:16:31 +00:00
# don't update an existing def unless they say so!
if ( $ ::client_exists && ! $ ::opt_u ) {
2008-06-25 14:42:23 +00:00
if ( $ ::FORCE ) {
# get rid of the old definition
my $ rmcmd = "/usr/sbin/nim -Fo reset $shorthost;/usr/sbin/nim -Fo deallocate -a subclass=all $shorthost;/usr/sbin/nim -Fo remove $shorthost" ;
my $ output = xCAT::Utils - > runcmd ( "$rmcmd" , - 1 ) ;
if ( $ ::RUNCMD_RC != 0 ) {
my $ rsp ;
2008-09-24 18:14:11 +00:00
push @ { $ rsp - > { data } } , "$::msgstr Could not remove the existing NIM object named \'$shorthost\'.\n" ;
2008-06-25 14:42:23 +00:00
if ( $ ::VERBOSE ) {
push @ { $ rsp - > { data } } , "$output" ;
}
xCAT::MsgUtils - > message ( "E" , $ rsp , $ callback ) ;
return 1 ;
}
2007-12-12 19:16:31 +00:00
2008-06-25 14:42:23 +00:00
} else { # no force
my $ rsp ;
2008-09-24 18:14:11 +00:00
$ rsp - > { data } - > [ 0 ] = "$::msgstr The NIM client machine \'$shorthost\' already exists. Use the \'-f\' option to remove and recreate or the \'-u\' option to update an existing definition.\n" ;
2008-06-25 14:42:23 +00:00
xCAT::MsgUtils - > message ( "I" , $ rsp , $ ::callback ) ;
return 1 ;
}
}
# either create or update the def
2007-12-12 19:16:31 +00:00
2008-06-25 14:42:23 +00:00
# process the args and add defaults etc.
foreach my $ attr ( keys % ::ATTRS ) {
2007-12-12 19:16:31 +00:00
2008-06-25 14:42:23 +00:00
if ( $ attr =~ /^if/ ) {
2007-12-12 19:16:31 +00:00
2008-06-25 14:42:23 +00:00
$ ifattr = "-a $attr=\'$::ATTRS{$attr}\'" ;
2007-12-12 19:16:31 +00:00
2008-06-25 14:42:23 +00:00
} elsif ( $ attr =~ /^cable_type/ ) {
$ cabletype = "-a cable_type1=\'$::ATTRS{$attr}\'" ;
2007-12-12 19:16:31 +00:00
2008-06-25 14:42:23 +00:00
} else {
# add to
$ finalattrs { $ attr } = $ ::ATTRS { $ attr } ;
2007-12-12 19:16:31 +00:00
}
2008-06-25 14:42:23 +00:00
}
2007-12-12 19:16:31 +00:00
2008-06-25 14:42:23 +00:00
# req a value for cable_type
2007-12-12 19:16:31 +00:00
2008-06-25 14:42:23 +00:00
if ( ! $ cabletype ) {
$ cabletype = "-a cable_type1=N/A " ;
}
2007-12-12 19:16:31 +00:00
2008-06-25 14:42:23 +00:00
# req a value for "if1" interface def
if ( ! $ ifattr ) {
# then try to create the attr - required
if ( $ ::objhash { $ node } { 'netname' } ) {
$ net_name = $ ::objhash { $ node } { 'netname' } ;
} else {
$ net_name = "find_net" ;
}
2007-12-12 19:16:31 +00:00
2008-06-25 14:42:23 +00:00
# only support Ethernet for management interfaces
$ adaptertype = "ent" ;
2008-02-14 14:25:44 +00:00
2008-06-25 14:42:23 +00:00
if ( ! $ ::objhash { $ node } { 'mac' } )
{
my $ rsp ;
2008-09-24 18:14:11 +00:00
$ rsp - > { data } - > [ 0 ] = "$::msgstr Missing the MAC for node \'$node\'.\n" ;
2008-06-25 14:42:23 +00:00
xCAT::MsgUtils - > message ( "E" , $ rsp , $ ::callback ) ;
return 1 ;
2007-12-12 19:16:31 +00:00
}
2008-06-25 14:42:23 +00:00
$ ifattr = "-a if1=\'$net_name $shorthost $::objhash{$node}{'mac'} $adaptertype\'" ;
}
2007-12-12 19:16:31 +00:00
2008-06-25 14:42:23 +00:00
# only support standalone for now - will get this from node def in future
$ nim_type = "-t standalone" ;
2007-12-12 19:16:31 +00:00
2008-06-25 14:42:23 +00:00
$ nim_args = "$ifattr " ;
$ nim_args . = "$cabletype" ;
2007-12-12 19:16:31 +00:00
2008-06-25 14:42:23 +00:00
# add the rest of the attr=val to the command line
foreach my $ a ( keys % finalattrs ) {
$ nim_args . = " -a $a=\'$finalattrs{$a}\'" ;
}
2008-02-14 14:25:44 +00:00
2008-06-25 14:42:23 +00:00
# put together the correct NIM command
my $ cmd ;
2008-02-14 14:25:44 +00:00
2008-06-25 14:42:23 +00:00
if ( $ ::client_exists && $ ::opt_u ) {
$ cmd = "nim -F -o change $nim_args $shorthost" ;
} else {
$ cmd = "nim -o define $nim_type $nim_args $shorthost" ;
}
2007-12-12 19:16:31 +00:00
2008-06-25 14:42:23 +00:00
# may need to use dsh if it is a remote server
my $ nimcmd ;
2008-09-24 18:14:11 +00:00
$ nimcmd = qq~$cmd 2>&1~ ;
2007-12-12 19:16:31 +00:00
2008-06-25 14:42:23 +00:00
# run the cmd
my $ output = xCAT::Utils - > runcmd ( "$nimcmd" , - 1 ) ;
if ( $ ::RUNCMD_RC != 0 )
{
my $ rsp ;
2008-09-24 18:14:11 +00:00
$ rsp - > { data } - > [ 0 ] = "$::msgstr Could not create a NIM definition for \'$node\'.\n" ;
2008-06-25 14:42:23 +00:00
if ( $ ::verbose )
{
$ rsp - > { data } - > [ 1 ] = "$output" ;
2007-12-12 19:16:31 +00:00
}
2008-06-25 14:42:23 +00:00
xCAT::MsgUtils - > message ( "E" , $ rsp , $ ::callback ) ;
return 1 ;
}
2007-12-12 19:16:31 +00:00
return 0 ;
}
#----------------------------------------------------------------------------
= head3 mkgrpdef
Create a NIM group definition .
Arguments:
Returns:
0 - OK
1 - error
Globals:
Error:
Example:
Comments:
= cut
#-----------------------------------------------------------------------------
sub mkgrpdef
{
2008-06-25 14:42:23 +00:00
my $ group = shift ;
my $ callback = shift ;
2007-12-12 19:16:31 +00:00
my $ cmd = undef ;
# get members and determine all the different masters
2008-10-10 15:54:06 +00:00
# For example, the xCAT group "all" will have nodes that are managed
# by multiple NIM masters - so we will create a local group "all"
# on each of those masters
2008-06-24 16:22:55 +00:00
my % ServerList = & getMasterGroupLists ( $ group ) ;
2007-12-12 19:16:31 +00:00
2008-10-10 15:54:06 +00:00
# get the members for the group def on this system
my @ members = @ { $ ServerList { $ ::local_host } } ;
2007-12-12 19:16:31 +00:00
2008-10-10 15:54:06 +00:00
# check to see if the group is already defined - sets $::grp_exists
if ( & check_nim_group ( $ group , $ ::local_host ) ) {
# the routine failed
return 1 ;
}
2007-12-12 19:16:31 +00:00
2008-10-10 15:54:06 +00:00
# don't update an existing def unless we're told
if ( $ ::grp_exists && ! $ ::opt_u ) {
if ( $ ::FORCE ) {
# get rid of the old definition
2007-12-12 19:16:31 +00:00
2008-10-10 15:54:06 +00:00
# ???? - does remove alone do the deallocate??
my $ rmcmd = "/usr/sbin/nim -Fo remove $group" ;
my $ output = xCAT::Utils - > runcmd ( "$rmcmd" , - 1 ) ;
if ( $ ::RUNCMD_RC != 0 ) {
2008-06-25 14:42:23 +00:00
my $ rsp ;
2008-10-10 15:54:06 +00:00
push @ { $ rsp - > { data } } , "$::msgstr Could not remove the existing NIM group named \'$group\'.\n" ;
if ( $ ::VERBOSE ) {
push @ { $ rsp - > { data } } , "$output" ;
}
xCAT::MsgUtils - > message ( "E" , $ rsp , $ callback ) ;
2008-06-25 14:42:23 +00:00
return 1 ;
}
2007-12-12 19:16:31 +00:00
2008-10-10 15:54:06 +00:00
} else { # no force
my $ rsp ;
$ rsp - > { data } - > [ 0 ] = "$::msgstr The NIM group \'$group\' already exists. Use the \'-f\' option to remove and recreate or the \'-u\' option to update an existing definition.\n" ;
xCAT::MsgUtils - > message ( "I" , $ rsp , $ ::callback ) ;
return 1 ;
}
}
2007-12-12 19:16:31 +00:00
2008-10-10 15:54:06 +00:00
# either create or update the group def on this master
2008-06-25 14:42:23 +00:00
2008-10-10 15:54:06 +00:00
# any list with more than 1024 members is an error
# - NIM can't handle that
if ( $# members > 1024 ) {
my $ rsp ;
$ rsp - > { data } - > [ 0 ] =
"$::msgstr Cannot create a NIM group definition with more than 1024 members - on \'$::local_host\'." ;
xCAT::MsgUtils - > message ( "I" , $ rsp , $ ::callback ) ;
next ;
}
2008-06-25 14:42:23 +00:00
2008-10-10 15:54:06 +00:00
#
# The list may become quite long and not fit on one cmd line
# so we do it one at a time for now - need to revisit this
# (like do blocks at a time) - TODO
#
my $ justadd = 0 ; # after the first define we just need to add
foreach my $ memb ( @ members ) {
my $ shorthost ;
( $ shorthost = $ memb ) =~ s/\..*$// ;
# do we change or create
my $ cmd ;
if ( ( $ ::grp_exists && $ ::opt_u ) || $ justadd ) {
$ cmd = "nim -o change -a add_member=$shorthost $group 2>&1" ;
} else {
$ cmd = "nim -o define -t mac_group -a add_member=$shorthost $group 2>&1" ;
$ justadd + + ;
}
2008-06-25 14:42:23 +00:00
2008-10-10 15:54:06 +00:00
my $ output = xCAT::Utils - > runcmd ( "$cmd" , - 1 ) ;
if ( $ ::RUNCMD_RC != 0 )
{
my $ rsp ;
$ rsp - > { data } - > [ 0 ] = "$::msgstr Could not create a NIM definition for \'$group\'.\n" ;
if ( $ ::verbose )
{
$ rsp - > { data } - > [ 1 ] = "$output" ;
2007-12-12 19:16:31 +00:00
}
2008-10-10 15:54:06 +00:00
xCAT::MsgUtils - > message ( "E" , $ rsp , $ ::callback ) ;
return 1 ;
2008-06-25 14:42:23 +00:00
}
2007-12-12 19:16:31 +00:00
}
return 0 ;
}
#----------------------------------------------------------------------------
= head3 rm_or_list_nim_object
List a NIM object definition .
Arguments:
Returns:
0 - OK
1 - error
Globals:
Error:
Example:
Comments:
= cut
#-----------------------------------------------------------------------------
sub rm_or_list_nim_object
{
my ( $ object , $ type ) = @ _ ;
if ( $ type eq 'node' ) {
if ( $ ::opt_l ) {
2008-06-24 16:22:55 +00:00
my $ cmd ;
2008-09-24 18:14:11 +00:00
$ cmd = qq~lsnim -l $object 2>/dev/null~ ;
2007-12-12 19:16:31 +00:00
2008-02-20 20:50:44 +00:00
my $ outref = xCAT::Utils - > runcmd ( "$cmd" , - 1 ) ;
2007-12-12 19:16:31 +00:00
if ( $ ::RUNCMD_RC != 0 )
{
my $ rsp ;
2008-09-24 18:14:11 +00:00
$ rsp - > { data } - > [ 0 ] = "$::msgstr Could not get the NIM definition for $object.\n" ;
2008-02-20 20:50:44 +00:00
if ( $ ::verbose )
{
$ rsp - > { data } - > [ 1 ] = "$outref" ;
}
2007-12-12 19:16:31 +00:00
xCAT::MsgUtils - > message ( "E" , $ rsp , $ ::callback ) ;
return 1 ;
} else {
# display to NIM output
my $ rsp ;
2008-02-18 20:12:15 +00:00
# $rsp->{data}->[0] = "NIM master: $nim_master";
# $rsp->{data}->[1] = "Client name: $object";
$ rsp - > { data } - > [ 0 ] = "$outref" ;
2007-12-12 19:16:31 +00:00
xCAT::MsgUtils - > message ( "I" , $ rsp , $ ::callback ) ;
return 0 ;
}
} elsif ( $ ::opt_r ) {
# remove the object
2008-06-24 16:22:55 +00:00
my $ cmd ;
2008-10-10 15:54:06 +00:00
if ( $ ::FORCE ) {
$ cmd = qq~nim -Fo reset $object;nim -Fo deallocate -a subclass=all $object;nim -Fo remove $object 2>/dev/null~ ;
} else {
$ cmd = qq~nim -Fo remove $object 2>/dev/null~ ;
}
2007-12-12 19:16:31 +00:00
2008-06-24 16:22:55 +00:00
my $ outref = xCAT::Utils - > runcmd ( "$cmd" , - 1 ) ;
2007-12-12 19:16:31 +00:00
if ( $ ::RUNCMD_RC != 0 )
{
my $ rsp ;
2008-09-24 18:14:11 +00:00
$ rsp - > { data } - > [ 0 ] = "$::msgstr Could not remove the NIM definition for \'$object\'.\n" ;
2008-02-20 20:50:44 +00:00
if ( $ ::verbose )
{
$ rsp - > { data } - > [ 1 ] = "$outref" ;
}
2007-12-12 19:16:31 +00:00
xCAT::MsgUtils - > message ( "E" , $ rsp , $ ::callback ) ;
return 1 ;
}
}
}
if ( $ type eq 'group' ) {
2008-10-10 15:54:06 +00:00
my $ cmd ;
if ( $ ::opt_l ) {
$ cmd = qq~lsnim -l $object 2>/dev/null~ ;
my $ outref = xCAT::Utils - > runcmd ( "$cmd" , - 1 ) ;
if ( $ ::RUNCMD_RC != 0 )
{
my $ rsp ;
$ rsp - > { data } - > [ 0 ] = "$::msgstr Could not list the NIM definition for \'$object\'.\n" ;
if ( $ ::verbose )
{
$ rsp - > { data } - > [ 1 ] = "$outref" ;
}
xCAT::MsgUtils - > message ( "E" , $ rsp , $ ::callback ) ;
return 1 ;
} else {
2008-09-24 18:14:11 +00:00
2008-10-10 15:54:06 +00:00
# display NIM output
my $ rsp ;
$ rsp - > { data } - > [ 0 ] = "$outref" ;
xCAT::MsgUtils - > message ( "I" , $ rsp , $ ::callback ) ;
return 0 ;
}
} elsif ( $ ::opt_r ) {
$ cmd = qq~nim -Fo remove $object 2>/dev/null~ ;
2007-12-12 19:16:31 +00:00
2008-10-10 15:54:06 +00:00
my $ outref = xCAT::Utils - > runcmd ( "$cmd" , - 1 ) ;
if ( $ ::RUNCMD_RC != 0 )
{
my $ rsp ;
$ rsp - > { data } - > [ 0 ] = "$::msgstr Could not remove the NIM definition for \'$object\'.\n" ;
if ( $ ::verbose )
2007-12-12 19:16:31 +00:00
{
2008-10-10 15:54:06 +00:00
$ rsp - > { data } - > [ 1 ] = "$outref" ;
}
xCAT::MsgUtils - > message ( "E" , $ rsp , $ ::callback ) ;
return 1 ;
}
2007-12-12 19:16:31 +00:00
}
2008-10-10 15:54:06 +00:00
}
2007-12-12 19:16:31 +00:00
return 0 ;
}
#----------------------------------------------------------------------------
= head3 getNIMmaster
Get the name of the NIM master for a node .
Arguments:
Returns:
name
undef - error
Globals:
Error:
Example:
Comments:
= cut
#-----------------------------------------------------------------------------
sub getNIMmaster
{
my ( $ node ) = @ _ ;
my $ NimMaster ;
my $ master = undef ;
# get the server name
if ( $ ::objhash { $ node } { nimmaster } ) {
$ NimMaster = $ ::objhash { $ node } { nimmaster } ;
} elsif ( $ ::objhash { $ node } { servicenode } ) {
# the servicenode attr is set for this node
$ NimMaster = $ ::objhash { $ node } { servicenode } ;
} elsif ( $ ::objhash { $ node } { xcatmaster } ) {
$ NimMaster = $ ::objhash { $ node } { xcatmaster } ;
} else {
$ NimMaster = $ ::local_host ;
}
# assume short hostnames for now???
if ( $ NimMaster ) {
( $ master = $ NimMaster ) =~ s/\..*$// ;
}
return $ master ;
}
#----------------------------------------------------------------------------
= head3 getMasterGroupLists
Get a hash of all the masters that have a certain group defined
and a list of members for each masters definition .
Arguments:
Returns:
server group list hash
undef - error
Globals:
Error:
Example:
Comments:
= cut
#-----------------------------------------------------------------------------
sub getMasterGroupLists
{
my ( $ group ) = @ _ ;
my $ NimMaster ;
my $ thismaster ;
2008-06-24 16:22:55 +00:00
my % membhash ;
my % memberhash ;
my % ServerList ;
2007-12-12 19:16:31 +00:00
# get the members list
my $ memberlist = xCAT::DBobjUtils - > getGroupMembers ( $ group , \ % ::objhash ) ;
my @ members = split ( ',' , $ memberlist ) ;
# get the node(member) definitions
if ( @ members ) {
foreach my $ n ( @ members ) {
$ membhash { $ n } = 'node' ;
}
% memberhash = xCAT::DBobjUtils - > getobjdefs ( \ % membhash ) ;
} else {
my $ rsp ;
2008-09-24 18:14:11 +00:00
$ rsp - > { data } - > [ 0 ] = "$::msgstr Could not get members of the xCAT group \'$group\'.\n" ;
2007-12-12 19:16:31 +00:00
xCAT::MsgUtils - > message ( "E" , $ rsp , $ ::callback ) ;
return undef ;
}
if ( defined ( % memberhash ) ) {
# sort the list by server node - one list per server
foreach my $ m ( @ members ) {
if ( $ memberhash { $ m } { nimmaster } ) {
$ NimMaster = $ memberhash { $ m } { nimmaster } ;
} elsif ( $ memberhash { $ m } { servicenode } ) {
# the servicenode attr is set for this node
$ NimMaster = $ memberhash { $ m } { servicenode } ;
} elsif ( $ memberhash { $ m } { xcatmaster } ) {
$ NimMaster = $ memberhash { $ m } { xcatmaster } ;
} else {
2008-09-24 18:14:11 +00:00
$ NimMaster = hostname ( ) ;
2007-12-12 19:16:31 +00:00
}
# assume short hostnames for now???
( $ thismaster = $ NimMaster ) =~ s/\..*$// ;
push ( @ { $ ServerList { $ thismaster } } , $ m ) ;
}
} else {
# could not get node def
my $ rsp ;
2008-09-24 18:14:11 +00:00
$ rsp - > { data } - > [ 0 ] = "$::msgstr Could not get xCAT node definition for all members of the xCAT group \'$group\'.\n" ;
2007-12-12 19:16:31 +00:00
xCAT::MsgUtils - > message ( "E" , $ rsp , $ ::callback ) ;
return undef ;
}
return % ServerList ;
}
#----------------------------------------------------------------------------
= head3 check_nim_group
See if an xCAT group has already been defined as a NIM machine group .
Arguments:
Returns:
0 - OK
1 - error
Globals:
Error:
Example:
Comments:
= cut
#-----------------------------------------------------------------------------
sub check_nim_group
{
my ( $ group , $ servnode ) = @ _ ;
my ( $ cmd , @ output ) ;
2008-06-24 16:22:55 +00:00
my @ GroupList ;
2008-04-22 13:17:31 +00:00
chomp $ ::local_host ;
chomp $ servnode ;
2007-12-12 19:16:31 +00:00
if ( $ ::NIMGroupList { $ servnode } ) {
@ GroupList = @ { $ ::NIMGroupList { $ servnode } } ;
} else {
2008-09-24 18:14:11 +00:00
$ cmd = qq~lsnim -c groups | cut -f1 -d' ' 2>/dev/null~ ;
2007-12-12 19:16:31 +00:00
2008-02-18 20:12:15 +00:00
@ GroupList = xCAT::Utils - > runcmd ( "$cmd" , - 1 ) ;
2007-12-12 19:16:31 +00:00
if ( $ ::RUNCMD_RC != 0 )
{
my $ rsp ;
2008-09-24 18:14:11 +00:00
$ rsp - > { data } - > [ 0 ] = "$::msgstr Could not get a list of NIM group definitions.\n" ;
2007-12-12 19:16:31 +00:00
xCAT::MsgUtils - > message ( "E" , $ rsp , $ ::callback ) ;
return 1 ;
}
# save member list for each server
@ { $ ::NIMGroupList { $ servnode } } = @ GroupList ;
}
$ ::grp_exists = grep ( /^$group$/ , @ GroupList ) ? 1 : 0 ;
return 0 ;
}
#----------------------------------------------------------------------------
= head3 check_nim_client
See if an xCAT node has already been defined as a NIM client .
Arguments:
Returns:
0 - OK
1 - error
Globals:
Error:
Example:
Comments:
= cut
#-----------------------------------------------------------------------------
sub check_nim_client
{
my ( $ node , $ servnode ) = @ _ ;
my ( $ cmd , @ ClientList ) ;
if ( $ ::NIMclientList { $ servnode } ) {
@ ClientList = @ { $ ::NIMclientList { $ servnode } } ;
} else {
2008-09-24 18:14:11 +00:00
$ cmd = qq~lsnim -c machines | cut -f1 -d' ' 2>/dev/null~ ;
2007-12-12 19:16:31 +00:00
2008-02-18 20:12:15 +00:00
@ ClientList = xCAT::Utils - > runcmd ( "$cmd" , - 1 ) ;
2007-12-12 19:16:31 +00:00
if ( $ ::RUNCMD_RC != 0 )
{
my $ rsp ;
2008-09-24 18:14:11 +00:00
$ rsp - > { data } - > [ 0 ] = "$::msgstr Could not get a list of NIM client definitions from \'$servnode\'.\n" ;
2007-12-12 19:16:31 +00:00
xCAT::MsgUtils - > message ( "E" , $ rsp , $ ::callback ) ;
return 1 ;
}
# save member list for each server
@ { $ ::NIMclientList { $ servnode } } = @ ClientList ;
}
$ ::client_exists = grep ( /^$node$/ , @ ClientList ) ? 1 : 0 ;
return 0 ;
}
#----------------------------------------------------------------------------
= head3 xcat2nim_usage
Arguments:
Returns:
Globals:
Error:
Example:
Comments:
= cut
#-----------------------------------------------------------------------------
sub xcat2nim_usage
{
my $ rsp ;
$ rsp - > { data } - > [ 0 ] =
"\nUsage: xcat2nim - Use this command to create and manage AIX NIM definitions based on xCAT object definitions.\n" ;
2008-06-25 14:42:23 +00:00
$ rsp - > { data } - > [ 1 ] = " xcat2nim [-h|--help ]\n" ;
2007-12-12 19:16:31 +00:00
$ rsp - > { data } - > [ 2 ] =
2008-06-25 14:42:23 +00:00
" xcat2nim [-V|--verbose] [-a|--all] [-l|--list] [-u|--update] " ;
2007-12-12 19:16:31 +00:00
2008-06-25 14:42:23 +00:00
$ rsp - > { data } - > [ 3 ] = " [-f|--force] [-r|--remove] [-t object-types] [-o object-names]" ;
2007-12-12 19:16:31 +00:00
$ rsp - > { data } - > [ 4 ] =
" [noderange] [attr=val [attr=val...]]\n" ;
xCAT::MsgUtils - > message ( "I" , $ rsp , $ ::callback ) ;
return 0 ;
}
1 ;