2008-07-09 16:10:10 +00:00
#!/usr/bin/env perl -w
2007-10-26 22:44:33 +00:00
# IBM(c) 2007 EPL license http://www.eclipse.org/legal/epl-v10.html
#####################################################
#
# xCAT plugin package to handle commands that manage the xCAT object
# definitions
#
#####################################################
2007-11-26 20:09:18 +00:00
2007-10-26 22:44:33 +00:00
package xCAT_plugin::DBobjectdefs ;
2012-12-29 08:33:14 +00:00
BEGIN
{
$ ::XCATROOT = $ ENV { 'XCATROOT' } ? $ ENV { 'XCATROOT' } : '/opt/xcat' ;
}
2007-10-26 22:44:33 +00:00
use xCAT::NodeRange ;
use xCAT::Schema ;
2007-11-26 20:09:18 +00:00
use xCAT::DBobjUtils ;
2007-10-26 22:44:33 +00:00
use Data::Dumper ;
use Getopt::Long ;
2007-11-26 20:09:18 +00:00
use xCAT::MsgUtils ;
2009-06-30 09:47:48 +00:00
use xCAT::Utils ;
2012-12-29 08:33:14 +00:00
use xCAT::SvrUtils ;
2008-07-09 16:10:10 +00:00
use strict ;
2007-10-26 22:44:33 +00:00
# options can be bundled up like -vV
Getopt::Long:: Configure ( "bundling" ) ;
2007-11-26 20:09:18 +00:00
$ Getopt:: Long:: ignorecase = 0 ;
#
# Globals
#
2007-10-26 22:44:33 +00:00
% ::CLIATTRS ; # attr=values provided on the command line
2007-11-26 20:09:18 +00:00
% ::FILEATTRS ; # attr=values provided in an input file
% ::FINALATTRS ; # final set of attr=values that are used to set
2011-03-20 05:56:47 +00:00
# the object
2007-11-26 20:09:18 +00:00
% ::objfilehash ; # hash of objects/types based of "-f" option
2011-03-20 05:56:47 +00:00
# (list in file)
2007-11-26 20:09:18 +00:00
% ::WhereHash ; # hash of attr=val from "-w" option
@ ::AttrList ; # list of attrs from "-i" option
2013-03-20 06:56:32 +00:00
% ::NicsAttrHash ; # hash of nics attributes specified with "-i" option
# e.g. $::NicsAttrHash{'nicips'} = ("eth0","eth1");
2007-11-26 20:09:18 +00:00
# object type lists
@ ::clobjtypes ; # list of object types derived from the command line.
@ ::fileobjtypes ; # list of object types from input file ("-x" or "-z")
2007-10-26 22:44:33 +00:00
2007-11-26 20:09:18 +00:00
# object name lists
@ ::clobjnames ; # list of object names derived from the command line
@ ::fileobjnames ; # list of object names from an input file
@ ::objfilelist ; # list of object names from the "-f" option
@ ::allobjnames ; # combined list
@ ::noderange ; # list of nodes derived from command line
2007-10-26 22:44:33 +00:00
#------------------------------------------------------------------------------
= head1 DBobjectdefs
2008-02-21 21:10:35 +00:00
This program module file supports the management of the xCAT data object
2007-10-26 22:44:33 +00:00
definitions .
2007-11-26 20:09:18 +00:00
Supported xCAT data object commands:
mkdef - create xCAT data object definitions .
lsdef - list xCAT data object definitions .
chdef - change xCAT data object definitions .
rmdef - remove xCAT data object definitions .
2007-10-26 22:44:33 +00:00
If adding to this file , please take a moment to ensure that:
1 . Your contrib has a readable pod header describing the purpose and use of
the subroutine .
2 . Your contrib is under the correct heading and is in alphabetical order
under that heading .
3 . You have run tidypod on your this file and saved the html file
= cut
#------------------------------------------------------------------------------
= head2 xCAT data object definition support
= cut
#------------------------------------------------------------------------------
#----------------------------------------------------------------------------
= head3 handled_commands
Return a list of commands handled by this plugin
= cut
#-----------------------------------------------------------------------------
sub handled_commands
{
return {
2007-11-26 20:09:18 +00:00
mkdef = > "DBobjectdefs" ,
lsdef = > "DBobjectdefs" ,
chdef = > "DBobjectdefs" ,
rmdef = > "DBobjectdefs"
2007-10-26 22:44:33 +00:00
} ;
}
#----------------------------------------------------------------------------
= head3 process_request
Check for xCAT command and call the appropriate subroutine .
Arguments:
2008-02-21 21:10:35 +00:00
2007-10-26 22:44:33 +00:00
Returns:
0 - OK
1 - error
Globals:
2008-02-21 21:10:35 +00:00
2007-10-26 22:44:33 +00:00
Error:
Example:
Comments:
= cut
#-----------------------------------------------------------------------------
sub process_request
{
$ ::request = shift ;
$ ::callback = shift ;
my $ ret ;
my $ msg ;
2011-03-02 06:36:24 +00:00
& initialize_variables ( ) ;
2007-10-26 22:44:33 +00:00
# globals used by all subroutines.
2007-11-26 20:09:18 +00:00
$ ::command = $ ::request - > { command } - > [ 0 ] ;
$ ::args = $ ::request - > { arg } ;
$ ::filedata = $ ::request - > { stdin } - > [ 0 ] ;
2007-10-26 22:44:33 +00:00
# figure out which cmd and call the subroutine to process
2007-11-26 20:09:18 +00:00
if ( $ ::command eq "mkdef" )
2007-10-26 22:44:33 +00:00
{
( $ ret , $ msg ) = & defmk ;
}
2007-11-26 20:09:18 +00:00
elsif ( $ ::command eq "lsdef" )
2007-10-26 22:44:33 +00:00
{
( $ ret , $ msg ) = & defls ;
}
2007-11-26 20:09:18 +00:00
elsif ( $ ::command eq "chdef" )
2007-10-26 22:44:33 +00:00
{
( $ ret , $ msg ) = & defch ;
}
2007-11-26 20:09:18 +00:00
elsif ( $ ::command eq "rmdef" )
2007-10-26 22:44:33 +00:00
{
( $ ret , $ msg ) = & defrm ;
}
2010-09-14 13:03:54 +00:00
my $ rsp ;
2007-10-26 22:44:33 +00:00
if ( $ msg )
{
$ rsp - > { data } - > [ 0 ] = $ msg ;
}
2010-09-14 13:03:54 +00:00
if ( $ ret > 0 ) {
$ rsp - > { errorcode } - > [ 0 ] = $ ret ;
}
$ ::callback - > ( $ rsp ) ;
2007-10-26 22:44:33 +00:00
}
2013-04-03 11:43:18 +00:00
sub parse_attr_for_osimage {
my $ command = shift ;
my $ attr_hash = shift ;
if ( ! exists ( $ attr_hash - > { profile } ) or ! exists ( $ attr_hash - > { provmethod } ) ) {
my $ rsp ;
$ rsp - > { data } - > [ 0 ] = "The profile and provmethod are all need to be specified." ;
xCAT::MsgUtils - > message ( "E" , $ rsp , $ ::callback ) ;
return - 1 ;
} else {
my $ tmp_profile = $ attr_hash - > { profile } ;
my $ tmp_provmethod = $ attr_hash - > { provmethod } ;
if ( $ tmp_provmethod !~ /install|netboot|statelite/ ) {
my $ rsp ;
$ rsp - > { data } - > [ 0 ] = "The provmethod: $tmp_provmethod is incorrect." ;
xCAT::MsgUtils - > message ( "E" , $ rsp , $ ::callback ) ;
return - 1 ;
}
my ( $ tmp_imagetype , $ tmp_arch , $ tmp_osname , $ tmp_ostype , $ tmp_osvers ) ;
if ( ! exists ( $ attr_hash - > { osarch } ) ) {
$ tmp_arch = `uname -m` ;
chomp ( $ tmp_arch ) ;
$ tmp_arch = "x86" if ( $ tmp_arch =~ /i.86$/ ) ;
$ attr_hash - > { osarch } = $ tmp_arch ;
} else {
$ tmp_arch = $ attr_hash - > { osarch } ;
}
if ( ! exists ( $ attr_hash - > { osvers } ) ) {
$ tmp_osvers = xCAT::Utils - > osver ( "all" ) ;
$ tmp_osvers =~ s/,// ;
$ attr_hash - > { osvers } = $ tmp_osvers ;
} else {
$ tmp_osvers = $ attr_hash - > { osvers } ;
}
$ tmp_osname = $ tmp_osvers ;
$ tmp_ostype = "Linux" ; #like Linux, Windows
$ tmp_imagetype = "linux" ;
my $ prov_dir = ( $ tmp_provmethod eq "install" ) ? "install" : "netboot" ;
if ( ( $ tmp_osvers =~ /^win/ ) || ( $ tmp_osvers =~ /^imagex/ ) ) {
$ tmp_osname = "windows" ;
$ tmp_ostype = "Windows" ;
$ tmp_imagetype = "windows" ;
} elsif ( $ tmp_osvers =~ /^hyperv/ ) {
$ tmp_osname = "hyperv" ;
$ tmp_ostype = "Windows" ;
$ tmp_imagetype = "windows" ;
} else {
until ( - r "$::XCATROOT/share/xcat/$prov_dir/$tmp_osname/" or not $ tmp_osname ) {
chop ( $ tmp_osname ) ;
}
unless ( $ tmp_osname ) {
my $ rsp ;
$ rsp - > { data } - > [ 0 ] = "Unable to find $::XCATROOT/share/xcat/$prov_dir directory for $tmp_osvers." ;
xCAT::MsgUtils - > message ( "E" , $ rsp , $ ::callback ) ;
return - 1 ;
}
}
#for rhels5.1 genos=rhel5
my $ tmp_genos = $ tmp_osvers ;
$ tmp_genos =~ s/\..*// ;
if ( $ tmp_genos =~ /rh.*s(\d*)/ ) {
$ tmp_genos = "rhel$1" ;
}
if ( exists ( $ attr_hash - > { imagetype } ) && ( $ attr_hash - > { imagetype } !~ /^$tmp_imagetype/i ) ) {
my $ rsp ;
$ rsp - > { data } - > [ 0 ] = "The input imagetype:$attr_hash->{imagetype} not match $tmp_imagetype." ;
xCAT::MsgUtils - > message ( "E" , $ rsp , $ ::callback ) ;
return - 1 ;
} elsif ( ! exists ( $ attr_hash - > { imagetype } ) ) {
$ attr_hash - > { imagetype } = $ tmp_imagetype ;
}
if ( exists ( $ attr_hash - > { osname } ) && ( $ attr_hash - > { osname } !~ /^$tmp_ostype/i ) ) {
my $ rsp ;
$ rsp - > { data } - > [ 0 ] = "The input osname:$attr_hash->{osname} not match $tmp_ostype." ;
xCAT::MsgUtils - > message ( "E" , $ rsp , $ ::callback ) ;
return - 1 ;
} elsif ( ! exists ( $ attr_hash - > { osname } ) ) {
$ attr_hash - > { osname } = $ tmp_ostype ;
}
2013-04-08 07:03:24 +00:00
#if (!exists($attr_hash->{osdistroname})) {
2013-04-03 11:43:18 +00:00
$ attr_hash - > { osdistroname } = "$tmp_osvers-$tmp_arch" ;
2013-04-08 07:03:24 +00:00
#}
2013-04-03 11:43:18 +00:00
if ( ! exists ( $ attr_hash - > { synclists } ) || $ command eq "chdef" ) {
my $ tmp_synclist = xCAT::SvrUtils - > getsynclistfile ( undef , $ tmp_osvers , $ tmp_arch , $ tmp_profile , "netboot" ) ;
if ( $ tmp_synclist ) {
$ attr_hash - > { synclists } = $ tmp_synclist ;
}
}
my @ non_win_attr = qw( pkglist pkgdir otherpkglist otherpkgdir exlist postinstall rootimgdir template ) ;
if ( $ tmp_osname =~ /^win/ ) {
my @ invalid_attr = ( ) ;
foreach ( @ non_win_attr ) {
if ( exists ( $ attr_hash - > { $ _ } ) ) {
push @ invalid_attr , $ _ ;
}
}
if ( $# invalid_attr ) {
my $ rsp ;
$ rsp - > { data } - > [ 0 ] = "$tmp_osvers can not work with @invalid_attr." ;
xCAT::MsgUtils - > message ( "E" , $ rsp , $ ::callback ) ;
return - 1 ;
}
} else {
my $ installroot = xCAT::TableUtils - > getInstallDir ( ) ;
my @ installdirs = xCAT::TableUtils - > get_site_attribute ( "installdir" ) ;
my $ tmp = $ installdirs [ 0 ] ;
if ( defined ( $ tmp ) ) {
$ installroot = $ tmp ;
}
my $ cuspath = "$installroot/custom/$prov_dir/$tmp_osname" ;
my $ defpath = "$::XCATROOT/share/xcat/$prov_dir/$tmp_osname" ;
if ( $ tmp_provmethod eq "install" ) {
$ attr_hash - > { exlist } = '' ;
$ attr_hash - > { postinstall } = '' ;
$ attr_hash - > { rootimgdir } = '' ;
if ( ( ! exists ( $ attr_hash - > { template } ) ) || ( $ command eq "chdef" ) ) {
my $ tmp_tmplfile = xCAT::SvrUtils - > get_tmpl_file_name ( $ cuspath , $ tmp_profile , $ tmp_osvers , $ tmp_arch , $ tmp_genos ) ;
if ( ! $ tmp_tmplfile ) {
$ tmp_tmplfile = xCAT::SvrUtils - > get_tmpl_file_name ( $ defpath , $ tmp_profile , $ tmp_osvers , $ tmp_arch , $ tmp_genos ) ;
}
if ( $ tmp_tmplfile ) {
$ attr_hash - > { template } = $ tmp_tmplfile ;
}
}
}
if ( $ tmp_provmethod ne "install" ) {
$ attr_hash - > { template } = '' ;
if ( ! exists ( $ attr_hash - > { exlist } ) ) {
my $ tmp_exlist = xCAT::SvrUtils - > get_exlist_file_name ( $ cuspath , $ tmp_profile , $ tmp_osvers , $ tmp_arch ) ;
if ( ! $ tmp_exlist ) {
$ tmp_exlist = xCAT::SvrUtils - > get_exlist_file_name ( $ defpath , $ tmp_profile , $ tmp_osvers , $ tmp_arch ) ;
}
if ( $ tmp_exlist ) {
$ attr_hash - > { exlist } = $ tmp_exlist ;
}
}
if ( ! exists ( $ attr_hash - > { postinstall } ) ) {
my $ tmp_post = xCAT::SvrUtils - > get_postinstall_file_name ( $ cuspath , $ tmp_profile , $ tmp_osvers , $ tmp_arch ) ;
if ( ! $ tmp_post ) {
$ tmp_post = xCAT::SvrUtils - > get_postinstall_file_name ( $ defpath , $ tmp_profile , $ tmp_osvers , $ tmp_arch ) ;
}
if ( $ tmp_post ) {
$ attr_hash - > { postinstall } = $ tmp_post ;
}
}
if ( ! exists ( $ attr_hash - > { rootimgdir } ) ) {
$ attr_hash - > { rootimgdir } = "$installroot/netboot/$tmp_osvers/$tmp_arch/$tmp_profile" ;
}
}
if ( ! exists ( $ attr_hash - > { pkglist } ) ) {
my $ tmp_pkglist = xCAT::SvrUtils - > get_pkglist_file_name ( $ cuspath , $ tmp_profile , $ tmp_osvers , $ tmp_arch ) ;
if ( ! $ tmp_pkglist ) {
$ tmp_pkglist = xCAT::SvrUtils - > get_pkglist_file_name ( $ defpath , $ tmp_profile , $ tmp_osvers , $ tmp_arch ) ;
}
if ( $ tmp_pkglist ) {
$ attr_hash - > { pkglist } = $ tmp_pkglist ;
}
}
if ( ! exists ( $ attr_hash - > { otherpkglist } ) ) {
my $ tmp_othpkglist = xCAT::SvrUtils - > get_otherpkgs_pkglist_file_name ( $ cuspath , $ tmp_profile , $ tmp_osvers , $ tmp_arch ) ;
if ( ! $ tmp_othpkglist ) {
$ tmp_othpkglist = xCAT::SvrUtils - > get_otherpkgs_pkglist_file_name ( $ defpath , $ tmp_profile , $ tmp_osvers , $ tmp_arch ) ;
}
if ( $ tmp_othpkglist ) {
$ attr_hash - > { otherpkglist } = $ tmp_othpkglist ;
}
}
if ( ! exists ( $ attr_hash - > { otherpkgdir } ) ) {
$ attr_hash - > { otherpkgdir } = "$installroot/post/otherpkgs/$tmp_osvers/$tmp_arch" ;
}
if ( ! exists ( $ attr_hash - > { pkgdir } ) ) {
$ attr_hash - > { pkgdir } = "$installroot/$tmp_osvers/$tmp_arch" ;
}
}
}
return 0 ;
}
2007-10-26 22:44:33 +00:00
#----------------------------------------------------------------------------
= head3 processArgs
Process the command line . Covers all four commands .
2011-03-20 05:56:47 +00:00
Also - Process any input files provided on cmd line .
2007-10-26 22:44:33 +00:00
Arguments:
Returns:
0 - OK
2011-03-20 05:56:47 +00:00
1 - just return
2011-03-18 14:58:45 +00:00
2 - just print usage
2011-03-20 05:56:47 +00:00
3 - error
2007-10-26 22:44:33 +00:00
Globals:
2008-02-21 21:10:35 +00:00
2007-10-26 22:44:33 +00:00
Error:
Example:
Comments:
= cut
#-----------------------------------------------------------------------------
sub processArgs
{
2007-11-26 20:09:18 +00:00
my $ gotattrs = 0 ;
2007-10-26 22:44:33 +00:00
2010-01-07 14:19:57 +00:00
if ( defined ( @ { $ ::args } ) ) {
2008-06-25 15:37:19 +00:00
@ ARGV = @ { $ ::args } ;
} else {
2010-01-07 14:19:57 +00:00
if ( $ ::command eq "lsdef" ) {
2010-03-26 11:45:16 +00:00
push @ ARGV , "-t" ;
push @ ARGV , "node" ;
2010-01-07 14:19:57 +00:00
push @ ARGV , "-s" ;
} else {
return 2 ;
}
}
2011-02-20 13:25:32 +00:00
if ( scalar ( @ { $ ::args } ) eq 1 and $ ::args - > [ 0 ] eq '-S' )
{
if ( $ ::command eq "lsdef" ) {
push @ ARGV , "-t" ;
push @ ARGV , "node" ;
push @ ARGV , "-s" ;
} else {
return 2 ;
}
}
2010-01-07 14:19:57 +00:00
if ( $ ::command eq "lsdef" ) {
2011-04-20 06:51:35 +00:00
if ( scalar ( @ ARGV ) == 2 && ( ( $ ARGV [ 0 ] eq "-l" && $ ARGV [ 1 ] eq "-S" ) || ( $ ARGV [ 0 ] eq "-S" && $ ARGV [ 1 ] eq "-l" ) ) ) {
push @ ARGV , "-t" ;
push @ ARGV , "node" ;
}
}
if ( $ ::command eq "lsdef" ) {
2010-01-07 14:19:57 +00:00
if ( scalar ( @ ARGV ) == 1 && $ ARGV [ 0 ] eq "-l" ) {
2010-03-26 11:45:16 +00:00
push @ ARGV , "-t" ;
push @ ARGV , "node" ;
2010-01-07 14:19:57 +00:00
}
2008-06-25 15:37:19 +00:00
}
2008-02-21 21:10:35 +00:00
2008-01-22 13:41:44 +00:00
if ( scalar ( @ ARGV ) <= 0 ) {
return 2 ;
}
2007-10-26 22:44:33 +00:00
# parse the options - include any option from all 4 cmds
2011-03-20 05:56:47 +00:00
Getopt::Long:: Configure ( "no_pass_through" ) ;
2007-10-26 22:44:33 +00:00
if (
! GetOptions (
2007-11-26 20:09:18 +00:00
'all|a' = > \ $ ::opt_a ,
2011-05-18 06:19:48 +00:00
'compress|c' = > \ $ ::opt_c ,
2007-10-26 22:44:33 +00:00
'dynamic|d' = > \ $ ::opt_d ,
2007-11-26 20:09:18 +00:00
'f|force' = > \ $ ::opt_f ,
2007-10-26 22:44:33 +00:00
'i=s' = > \ $ ::opt_i ,
2008-02-21 19:16:33 +00:00
'help|h|?' = > \ $ ::opt_h ,
2007-10-26 22:44:33 +00:00
'long|l' = > \ $ ::opt_l ,
2010-01-07 14:19:57 +00:00
'short|s' = > \ $ ::opt_s ,
2007-10-26 22:44:33 +00:00
'm|minus' = > \ $ ::opt_m ,
2010-09-08 10:38:12 +00:00
'n=s' = > \ $ ::opt_n ,
2007-10-26 22:44:33 +00:00
'o=s' = > \ $ ::opt_o ,
2007-11-26 20:09:18 +00:00
'p|plus' = > \ $ ::opt_p ,
2007-10-26 22:44:33 +00:00
't=s' = > \ $ ::opt_t ,
'verbose|V' = > \ $ ::opt_V ,
'version|v' = > \ $ ::opt_v ,
2009-06-30 09:47:48 +00:00
'w=s@' = > \ $ ::opt_w ,
2007-11-26 20:09:18 +00:00
'x|xml' = > \ $ ::opt_x ,
2010-12-15 01:47:06 +00:00
'z|stanza' = > \ $ ::opt_z ,
2011-05-26 08:36:06 +00:00
'nocache' = > \ $ ::opt_nc ,
2011-01-10 09:26:46 +00:00
'S' = > \ $ ::opt_S ,
2011-06-07 06:51:28 +00:00
'osimage' = > \ $ ::opt_osimg ,
2013-03-20 06:56:32 +00:00
'nics' = > \ $ ::opt_nics ,
2013-04-08 07:03:24 +00:00
'u' = > \ $ ::opt_setattr ,
2007-10-26 22:44:33 +00:00
)
)
{
2007-11-26 20:09:18 +00:00
2011-03-20 05:56:47 +00:00
my $ rsp ;
$ rsp - > { data } - > [ 0 ] = "Invalid option.." ;
xCAT::MsgUtils - > message ( "E" , $ rsp , $ ::callback ) ;
2011-03-18 14:58:45 +00:00
return 2 ;
2007-10-26 22:44:33 +00:00
}
2013-04-08 07:03:24 +00:00
if ( defined ( $ ::opt_setattr ) && ( $ ::command ne "chdef" ) && ( $ ::command ne "mkdef" ) ) {
my $ rsp ;
$ rsp - > { data } - > [ 0 ] = "Option \'-u\' can not work with $::command." ;
xCAT::MsgUtils - > message ( "E" , $ rsp , $ ::callback ) ;
return 2 ;
}
if ( defined ( $ ::opt_setattr ) ) {
if ( ! $ ::opt_t && ! $ ::filedata ) {
$ ::opt_t = 'osimage' ;
} elsif ( $ ::opt_t ne "osimage" ) {
my $ rsp ;
$ rsp - > { data } - > [ 0 ] = "Option \'-u\' only work for objtype \'osimage\'." ;
xCAT::MsgUtils - > message ( "E" , $ rsp , $ ::callback ) ;
return 2 ;
}
2013-04-03 11:43:18 +00:00
}
2011-06-16 00:41:05 +00:00
# -t node is the default value
2013-03-04 16:46:28 +00:00
if ( ! $ ::opt_t && ! $ ::opt_a && ! $ ::opt_h && ( $ ::command eq "lsdef" ) )
2011-06-16 00:41:05 +00:00
{
$ ::opt_t = 'node' ;
}
2011-03-20 05:56:47 +00:00
# Initialize some global arrays in case this is being called twice in the same process.
# Currently only doing this when --nocache is specified, but i think it should be done all of the time.
2011-05-26 08:36:06 +00:00
if ( $ ::opt_nc ) {
2011-03-02 06:36:24 +00:00
& initialize_variables ( ) ;
2011-03-20 05:56:47 +00:00
}
2007-10-26 22:44:33 +00:00
2007-11-26 20:09:18 +00:00
# opt_x not yet supported
if ( $ ::opt_x )
2007-10-26 22:44:33 +00:00
{
2007-11-26 20:09:18 +00:00
my $ rsp ;
$ rsp - > { data } - > [ 0 ] =
"The \'-x\' (XML format) option is not yet implemented." ;
2009-06-09 08:30:49 +00:00
xCAT::MsgUtils - > message ( "E" , $ rsp , $ ::callback ) ;
2007-11-26 20:09:18 +00:00
return 2 ;
}
2011-06-07 06:51:28 +00:00
# -i and --osimage cannot be used together
if ( $ ::opt_i && $ ::opt_osimg ) {
my $ rsp ;
$ rsp - > { data } - > [ 0 ] = "The flags \'-i'\ and \'--osimage'\ cannot be used together." ;
xCAT::MsgUtils - > message ( "E" , $ rsp , $ ::callback ) ;
return 2 ;
}
# -z and --osimage cannot be used together
if ( $ ::opt_z && $ ::opt_osimg ) {
my $ rsp ;
$ rsp - > { data } - > [ 0 ] = "The flags \'-z'\ and \'--osimage'\ cannot be used together." ;
xCAT::MsgUtils - > message ( "E" , $ rsp , $ ::callback ) ;
return 2 ;
}
2012-12-27 05:50:35 +00:00
# -a and -t cannot be used together
if ( $ ::opt_a && $ ::opt_t ) {
my $ rsp ;
$ rsp - > { data } - > [ 0 ] = "The flags \'-a'\ and \'-t'\ cannot be used together." ;
xCAT::MsgUtils - > message ( "E" , $ rsp , $ ::callback ) ;
return 2 ;
}
2010-01-07 14:19:57 +00:00
# -l and -s cannot be used together
if ( $ ::opt_l && $ ::opt_s ) {
my $ rsp ;
$ rsp - > { data } - > [ 0 ] = "The flags \'-l'\ and \'-s'\ cannot be used together." ;
xCAT::MsgUtils - > message ( "E" , $ rsp , $ ::callback ) ;
return 2 ;
}
2013-03-20 06:56:32 +00:00
# -i and --nics cannot be used together
if ( $ ::opt_nics && $ ::opt_i ) {
my $ rsp ;
$ rsp - > { data } - > [ 0 ] = "The flags \'-i'\ and \'--nics'\ cannot be used together." ;
xCAT::MsgUtils - > message ( "E" , $ rsp , $ ::callback ) ;
return 2 ;
}
# --nics is the equivalent of -i nicips,nichostnamesuffixes...
if ( $ ::opt_nics ) {
2014-01-13 17:55:15 +00:00
$ ::opt_i = "nicips,nichostnamesuffixes,nichostnameprefixes,nictypes,niccustomscripts,nicnetworks,nicaliases" ;
2013-03-20 06:56:32 +00:00
}
2010-01-07 14:19:57 +00:00
# -i and -s cannot be used together
if ( $ ::opt_i && $ ::opt_s ) {
my $ rsp ;
$ rsp - > { data } - > [ 0 ] = "The flags \'-i'\ and \'-s'\ cannot be used together." ;
xCAT::MsgUtils - > message ( "E" , $ rsp , $ ::callback ) ;
return 2 ;
}
2011-05-18 06:19:48 +00:00
# -c must be used together with -i
if ( $ ::opt_c && ! $ ::opt_i ) {
my $ rsp ;
$ rsp - > { data } - > [ 0 ] = "The flags \'-c'\ and \'-i'\ must be used together." ;
xCAT::MsgUtils - > message ( "E" , $ rsp , $ ::callback ) ;
return 2 ;
}
2007-11-26 20:09:18 +00:00
# can get object names in many ways - easier to keep track
$ ::objectsfrom_args = 0 ;
$ ::objectsfrom_opto = 0 ;
$ ::objectsfrom_optt = 0 ;
$ ::objectsfrom_opta = 0 ;
$ ::objectsfrom_nr = 0 ;
$ ::objectsfrom_file = 0 ;
#
# process @ARGV
#
# - put attr=val operands in ATTRS hash
2010-03-23 03:25:57 +00:00
my $ noderangespace = 0 ;
2007-11-26 20:09:18 +00:00
while ( my $ a = shift ( @ ARGV ) )
{
2007-10-26 22:44:33 +00:00
if ( ! ( $ a =~ /=/ ) )
{
2007-11-26 20:09:18 +00:00
2010-03-23 03:25:57 +00:00
# can not have spaces in the noderange
if ( $ noderangespace )
{
my $ rsp ;
$ rsp - > { data } - > [ 0 ] = "noderange can not contain spaces." ;
xCAT::MsgUtils - > message ( "E" , $ rsp , $ ::callback ) ;
return 2 ;
}
$ noderangespace + + ;
2007-11-26 20:09:18 +00:00
# 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 ) ;
2010-03-01 10:11:13 +00:00
@ ::noderange = @ ::clobjnames ;
2007-11-26 20:09:18 +00:00
$ ::objectsfrom_args = 1 ;
}
elsif ( ! $ ::opt_t || ( $ ::opt_t eq 'node' ) )
{
# if the type was not provided or it is "node"
2011-03-20 05:56:47 +00:00
# then set noderange
2009-06-15 09:29:56 +00:00
if ( ( $ ::command ne 'mkdef' ) && ( $ a =~ m/^\// ) )
{
@ ::noderange = & noderange ( $ a , 1 ) ; # Use the "verify" option to support regular expression
}
else
{
@ ::noderange = & noderange ( $ a , 0 ) ; # mkdef could not spport regular expression
}
2009-11-10 03:29:08 +00:00
if ( scalar ( @ ::noderange ) == 0 )
{
my $ rsp ;
$ rsp - > { data } - > [ 0 ] = "No node in \'$a\', check the noderange syntax." ;
xCAT::MsgUtils - > message ( "E" , $ rsp , $ ::callback ) ;
2011-03-18 14:58:45 +00:00
return 3 ;
2009-11-10 03:29:08 +00:00
}
2007-11-26 20:09:18 +00:00
}
2007-10-26 22:44:33 +00:00
}
else
{
# if it has an "=" sign its an attr=val - we hope
2007-11-26 20:09:18 +00:00
# - this will handle "attr= "
my ( $ attr , $ value ) = $ a =~ /^\s*(\S+?)\s*=\s*(\S*.*)$/ ;
2007-10-26 22:44:33 +00:00
if ( ! defined ( $ attr ) || ! defined ( $ value ) )
{
2007-11-26 20:09:18 +00:00
my $ rsp ;
2009-10-12 07:41:52 +00:00
$ rsp - > { data } - > [ 0 ] = "Incorrect \'attr=val\' pair - $a." ;
2007-11-26 20:09:18 +00:00
xCAT::MsgUtils - > message ( "E" , $ rsp , $ ::callback ) ;
return 3 ;
2007-10-26 22:44:33 +00:00
}
$ gotattrs = 1 ;
# put attr=val in hash
$ ::ATTRS { $ attr } = $ value ;
}
}
2013-03-21 06:41:01 +00:00
if ( ( ! $ ::opt_t || $ ::opt_t eq 'node' ) && ( $ ::command eq 'chdef' ) && ( $ ::opt_m || $ ::opt_p ) )
{
my $ nicattrs = 0 ;
foreach my $ kattr ( keys % ::ATTRS )
{
if ( $ kattr =~ /^nic\w+\.\w+$/ )
{
$ nicattrs = 1 ;
}
last ;
}
if ( $ nicattrs )
{
my $ rsp ;
$ rsp - > { data } - > [ 0 ] = "chdef does not support to change the nic related attributes with -m or -p flag." ;
xCAT::MsgUtils - > message ( "E" , $ rsp , $ ::callback ) ;
return 3 ;
}
}
2009-10-10 07:05:40 +00:00
# Check arguments for rmdef command
# rmdef is very dangerous if wrong flag is specified
# it may cause all the objects to be deleted, check the flags
# for example: rmdef -t node -d, the user want to delete the node named "-d",
# but it will delete all the nodes!
# use -o instead
if ( $ ::command eq 'rmdef' )
{
if ( defined ( $ ::opt_d ) || defined ( $ ::opt_i ) || defined ( $ ::opt_l )
|| defined ( $ ::opt_m ) || defined ( $ ::opt_p ) || defined ( $ ::opt_w )
2010-01-07 14:19:57 +00:00
|| defined ( $ ::opt_x ) || defined ( $ ::opt_z ) || defined ( $ ::opt_s ) )
2009-10-10 07:05:40 +00:00
{
my $ rsp ;
$ rsp - > { data } - > [ 0 ] = "Invalid flag specified, see rmdef manpage for details." ;
xCAT::MsgUtils - > message ( "E" , $ rsp , $ ::callback ) ;
2011-03-18 14:58:45 +00:00
return 2 ;
2009-10-10 07:05:40 +00:00
}
}
2007-10-26 22:44:33 +00:00
# Option -h for Help
2007-11-26 20:09:18 +00:00
# if user specifies "-t" & "-h" they want a list of valid attrs
if ( defined ( $ ::opt_h ) && ! defined ( $ ::opt_t ) )
2007-10-26 22:44:33 +00:00
{
2007-11-26 20:09:18 +00:00
return 2 ;
2007-10-26 22:44:33 +00:00
}
# Option -v for version - do we need this???
if ( defined ( $ ::opt_v ) )
{
2007-11-26 20:09:18 +00:00
my $ rsp ;
2011-03-20 05:56:47 +00:00
my $ version = xCAT::Utils - > Version ( ) ;
2009-10-12 07:41:52 +00:00
push @ { $ rsp - > { data } } , "$::command - $version" ;
2007-11-26 20:09:18 +00:00
xCAT::MsgUtils - > message ( "I" , $ rsp , $ ::callback ) ;
return 1 ; # no usage - just exit
2007-10-26 22:44:33 +00:00
}
# Option -V for verbose output
if ( defined ( $ ::opt_V ) )
{
$ ::verbose = 1 ;
2007-11-26 20:09:18 +00:00
$ ::VERBOSE = 1 ;
2009-03-22 19:15:19 +00:00
} else {
2011-03-20 05:56:47 +00:00
$ ::verbose = 0 ;
2009-03-22 19:15:19 +00:00
$ ::VERBOSE = 0 ;
2011-03-20 05:56:47 +00:00
}
2007-11-26 20:09:18 +00:00
#
# process the input file - if provided
#
if ( $ ::filedata )
{
my $ rc = xCAT::DBobjUtils - > readFileInput ( $ ::filedata ) ;
if ( $ rc )
{
my $ rsp ;
2009-10-12 07:41:52 +00:00
$ rsp - > { data } - > [ 0 ] = "Could not process file input data." ;
2007-11-26 20:09:18 +00:00
xCAT::MsgUtils - > message ( "I" , $ rsp , $ ::callback ) ;
return 1 ;
}
# - %::FILEATTRS{fileobjname}{attr}=val
# set @::fileobjtypes, @::fileobjnames, %::FILEATTRS
$ ::objectsfrom_file = 1 ;
2007-10-26 22:44:33 +00:00
}
2007-11-26 20:09:18 +00:00
#
# determine the object types
#
2007-10-26 22:44:33 +00:00
# could have comma seperated list of types
if ( $ ::opt_t )
{
2007-11-26 20:09:18 +00:00
my @ tmptypes ;
2007-10-26 22:44:33 +00:00
if ( $ ::opt_t =~ /,/ )
{
# can't have mult types when using attr=val
if ( $ gotattrs )
{
2007-11-26 20:09:18 +00:00
my $ rsp ;
$ rsp - > { data } - > [ 0 ] =
2009-10-12 07:41:52 +00:00
"Cannot combine multiple types with \'att=val\' pairs on the command line." ;
2007-11-26 20:09:18 +00:00
xCAT::MsgUtils - > message ( "E" , $ rsp , $ ::callback ) ;
return 3 ;
2007-10-26 22:44:33 +00:00
}
else
{
2007-11-26 20:09:18 +00:00
@ tmptypes = split ( ',' , $ ::opt_t ) ;
2007-10-26 22:44:33 +00:00
}
}
else
{
2007-11-26 20:09:18 +00:00
push ( @ tmptypes , $ ::opt_t ) ;
}
# check for valid types
my @ xdeftypes ;
foreach my $ k ( keys % { xCAT::Schema:: defspec } )
{
push ( @ xdeftypes , $ k ) ;
}
foreach my $ t ( @ tmptypes )
{
2009-07-21 19:36:14 +00:00
if ( ! grep ( /^$t$/ , @ xdeftypes ) )
2007-11-26 20:09:18 +00:00
{
my $ rsp ;
$ rsp - > { data } - > [ 0 ] =
2009-05-07 13:58:00 +00:00
"\nType \'$t\' is not a valid xCAT object type." ;
2009-06-09 08:30:49 +00:00
xCAT::MsgUtils - > message ( "E" , $ rsp , $ ::callback ) ;
2011-03-20 05:56:47 +00:00
return 3 ;
2007-11-26 20:09:18 +00:00
}
else
{
chomp $ t ;
push ( @ ::clobjtypes , $ t ) ;
}
2007-10-26 22:44:33 +00:00
}
}
2007-11-26 20:09:18 +00:00
# must have object type(s) - default if not provided
if ( ! @ ::clobjtypes && ! @ ::fileobjtypes && ! $ ::opt_a && ! $ ::opt_t )
{
# make the default type = 'node' if not specified
push ( @ ::clobjtypes , 'node' ) ;
my $ rsp ;
2011-03-20 05:56:47 +00:00
if ( ! $ ::opt_z && ! $ ::opt_x ) {
# don't want this msg in stanza or xml output
#$rsp->{data}->[0] = "Assuming an object type of \'node\'.";
#xCAT::MsgUtils->message("I", $rsp, $::callback);
}
2007-11-26 20:09:18 +00:00
}
# if user specifies "-t" & "-h" they want valid type or attrs info
if ( $ ::opt_h && $ ::opt_t )
2007-10-26 22:44:33 +00:00
{
2007-11-26 20:09:18 +00:00
# give the list of attr names for each type specified
2007-10-26 22:44:33 +00:00
foreach my $ t ( @ ::clobjtypes )
{
2007-11-26 20:09:18 +00:00
my $ rsp ;
2011-03-20 05:56:47 +00:00
if ( $ t eq 'site' ) {
2012-02-21 08:03:15 +00:00
if ( $ ::opt_i )
{
my $ tmprsp ;
$ tmprsp - > { data } - > [ 0 ] = "It is not supported to list the description of some specific site attributes, displaying the description for all site attributes instead." ;
xCAT::MsgUtils - > message ( "W" , $ tmprsp , $ ::callback ) ;
}
2011-03-20 05:56:47 +00:00
my $ schema = xCAT::Table - > getTableSchema ( 'site' ) ;
my $ desc ;
2009-03-20 18:46:11 +00:00
2011-03-20 05:56:47 +00:00
$ rsp - > { data } - > [ 0 ] = "\nThere can only be one xCAT site definition. This definition consists \nof an unlimited list of user-defined attributes and values that represent \nglobal settings for the whole cluster. The following is a list \nof the attributes currently supported by xCAT." ;
2009-03-20 18:46:11 +00:00
2011-03-20 05:56:47 +00:00
$ desc = $ schema - > { descriptions } - > { 'key' } ;
$ rsp - > { data } - > [ 1 ] = $ desc ;
2009-03-20 18:46:11 +00:00
2011-03-20 05:56:47 +00:00
xCAT::MsgUtils - > message ( "I" , $ rsp , $ ::callback ) ;
next ;
}
2009-03-20 18:46:11 +00:00
2011-03-20 05:56:47 +00:00
# get the data type definition from Schema.pm
2007-11-26 20:09:18 +00:00
my $ datatype = $ xCAT:: Schema:: defspec { $ t } ;
2012-02-21 08:03:15 +00:00
if ( ! $ ::opt_i )
{
$ rsp - > { data } - > [ 0 ] = "The valid attribute names for object type '$t' are:" ;
}
2009-03-20 18:46:11 +00:00
2007-11-26 20:09:18 +00:00
# get the objkey for this type object (ex. objkey = 'node')
my $ objkey = $ datatype - > { 'objkey' } ;
$ rsp - > { data } - > [ 1 ] = "Attribute Description\n" ;
my @ alreadydone ; # the same attr may appear more then once
2011-03-20 05:56:47 +00:00
my @ attrlist ;
2008-01-21 20:35:16 +00:00
my $ outstr = "" ;
2012-02-21 08:03:15 +00:00
my @ dispattrs = ( ) ;
my % dispattrhash = ( ) ;
if ( $ ::opt_i )
{
@ dispattrs = split ( /,/ , $ ::opt_i ) ;
foreach my $ dattr ( @ dispattrs )
{
2013-03-20 06:56:32 +00:00
# lsdef -t node -h -i nicips.eth0
if ( $ dattr =~ /^(nic\w+)\.\w+$/ )
{
$ dattr = $ 1 ;
}
2012-02-21 08:03:15 +00:00
$ dispattrhash { $ dattr } = 1 ;
}
}
2008-07-09 16:10:10 +00:00
foreach my $ this_attr ( @ { $ datatype - > { 'attrs' } } )
2007-11-26 20:09:18 +00:00
{
my $ attr = $ this_attr - > { attr_name } ;
2012-02-21 08:03:15 +00:00
# Only display the specified attributes
if ( $ ::opt_i )
{
if ( ! defined ( $ dispattrhash { $ attr } ) || ! $ dispattrhash { $ attr } )
{
next ;
}
}
2007-11-26 20:09:18 +00:00
my $ desc = $ this_attr - > { description } ;
2008-07-29 17:43:47 +00:00
if ( ! defined ( $ desc ) ) {
2011-03-20 05:56:47 +00:00
# description key not there, so go to the corresponding
# entry in tabspec to get the description
my ( $ tab , $ at ) = split ( /\./ , $ this_attr - > { tabentry } ) ;
my $ schema = xCAT::Table - > getTableSchema ( $ tab ) ;
$ desc = $ schema - > { descriptions } - > { $ at } ;
2008-02-21 21:10:35 +00:00
}
2007-11-26 20:09:18 +00:00
2011-03-20 05:56:47 +00:00
# could display the table that the attr is in
# however some attrs are in more than one table!!!
#my ($tab, $junk) = split('\.', $this_attr->{tabentry});
2008-02-12 14:00:20 +00:00
2007-11-26 20:09:18 +00:00
if ( ! grep ( /^$attr$/ , @ alreadydone ) )
2008-02-21 21:10:35 +00:00
{
2011-03-20 05:56:47 +00:00
my $ space = ( length ( $ attr ) < 7 ? "\t\t" : "\t" ) ;
push ( @ attrlist , "$attr:$space$desc\n\n" ) ;
2007-11-26 20:09:18 +00:00
}
push ( @ alreadydone , $ attr ) ;
}
2008-02-21 21:10:35 +00:00
2011-03-20 05:56:47 +00:00
# print the output in alphabetical order
2008-07-29 17:43:47 +00:00
foreach my $ a ( sort @ attrlist ) {
$ outstr . = "$a" ;
}
chop ( $ outstr ) ; chop ( $ outstr ) ;
2008-01-21 20:35:16 +00:00
$ rsp - > { data } - > [ 2 ] = $ outstr ;
2008-06-24 16:22:55 +00:00
2011-03-20 05:56:47 +00:00
# the monitoring table is special
if ( $ t eq 'monitoring' ) {
$ rsp - > { data } - > [ 3 ] = "\nYou can also include additional monitoring plug-in specific settings. These settings will be used by the monitoring plug-in to customize the behavior such as event filter, sample interval, responses etc." ;
}
2007-11-26 20:09:18 +00:00
xCAT::MsgUtils - > message ( "I" , $ rsp , $ ::callback ) ;
2007-10-26 22:44:33 +00:00
}
2007-11-26 20:09:18 +00:00
return 1 ;
2007-10-26 22:44:33 +00:00
}
2007-11-26 20:09:18 +00:00
#
# determine the object names
#
2007-10-26 22:44:33 +00:00
# - get object names from the -o option or the noderange
if ( $ ::opt_o )
{
2007-11-26 20:09:18 +00:00
$ ::objectsfrom_opto = 1 ;
# special handling for site table !!!!!
if ( ( $ ::opt_t eq 'site' ) && ( $ ::opt_o ne 'clustersite' ) )
{
2011-03-02 08:37:08 +00:00
push ( @ ::clobjnames , $ ::opt_o ) ;
2007-11-26 20:09:18 +00:00
push ( @ ::clobjnames , 'clustersite' ) ;
}
elsif ( $ ::opt_t eq 'node' )
2007-10-26 22:44:33 +00:00
{
2009-08-13 09:36:04 +00:00
if ( ( $ ::command ne 'mkdef' ) && ( $ ::opt_o =~ m/^\// ) )
2009-06-15 09:29:56 +00:00
{
@ ::clobjnames = & noderange ( $ ::opt_o , 1 ) ; #Use the "verify" option to support regular expression
}
else
{
@ ::clobjnames = & noderange ( $ ::opt_o , 0 ) ; #mkdef can not support regular expression
}
2007-10-26 22:44:33 +00:00
}
else
{
2007-11-26 20:09:18 +00:00
# make a list
if ( $ ::opt_o =~ /,/ )
{
@ ::clobjnames = split ( ',' , $ ::opt_o ) ;
}
else
{
push ( @ ::clobjnames , $ ::opt_o ) ;
}
2007-10-26 22:44:33 +00:00
}
}
2007-11-26 20:09:18 +00:00
elsif ( @ ::noderange && ( @ ::clobjtypes [ 0 ] eq 'node' ) )
2007-10-26 22:44:33 +00:00
{
# if there's no object list and the type is node then the
2007-11-26 20:09:18 +00:00
# noderange list is assumed to be the object names list
@ ::clobjnames = @ ::noderange ;
$ ::objectsfrom_nr = 1 ;
2007-10-26 22:44:33 +00:00
}
2007-11-26 20:09:18 +00:00
# special case for site table!!!!!!!!!!!!!!
if ( ( $ ::opt_t eq 'site' ) && ! $ ::opt_o )
{
push ( @ ::clobjnames , 'clustersite' ) ;
$ ::objectsfrom_opto = 1 ;
}
2007-10-26 22:44:33 +00:00
2007-11-26 20:09:18 +00:00
# if there is no other input for object names then we need to
2011-03-20 05:56:47 +00:00
# find all the object names for the specified types
2010-12-27 08:17:12 +00:00
# Do NOT do this for rmdef
2007-11-26 20:09:18 +00:00
if ( $ ::opt_t
&& ! ( $ ::opt_o
|| $ ::filedata
|| $ ::opt_a
|| @ ::noderange
|| @ ::clobjnames ) )
2007-10-26 22:44:33 +00:00
{
2007-11-26 20:09:18 +00:00
my @ tmplist ;
2007-10-26 22:44:33 +00:00
2012-05-02 02:53:42 +00:00
if ( ( $ ::command eq 'rmdef' ) || ( $ ::command eq 'chdef' ) )
2010-12-27 08:17:12 +00:00
{
my $ rsp ;
$ rsp - > { data } - > [ 0 ] =
"No object names were provided." ;
xCAT::MsgUtils - > message ( "E" , $ rsp , $ ::callback ) ;
return 2 ;
}
2007-11-26 20:09:18 +00:00
# also ne chdef ????????
if ( $ ::command ne 'mkdef' )
2007-10-26 22:44:33 +00:00
{
2007-11-26 20:09:18 +00:00
$ ::objectsfrom_optt = 1 ;
2007-10-26 22:44:33 +00:00
2007-11-26 20:09:18 +00:00
# could have multiple type
foreach my $ t ( @ ::clobjtypes )
{
# special case for site table !!!!
if ( $ t eq 'site' )
{
push ( @ tmplist , 'clustersite' ) ;
}
else
{
2007-10-26 22:44:33 +00:00
2007-11-26 20:09:18 +00:00
# look up all objects of this type in the DB ???
@ tmplist = xCAT::DBobjUtils - > getObjectsOfType ( $ t ) ;
unless ( @ tmplist )
{
my $ rsp ;
$ rsp - > { data } - > [ 0 ] =
2009-10-12 07:41:52 +00:00
"Could not get objects of type \'$t\'." ;
2009-05-07 13:58:00 +00:00
#$rsp->{data}->[1] = "Skipping to the next type.\n";
2009-06-09 08:30:49 +00:00
xCAT::MsgUtils - > message ( "E" , $ rsp , $ ::callback ) ;
2009-05-07 13:58:00 +00:00
return 3 ;
2007-11-26 20:09:18 +00:00
}
}
2007-10-26 22:44:33 +00:00
2007-11-26 20:09:18 +00:00
# add objname and type to hash and global list
foreach my $ o ( @ tmplist )
{
push ( @ ::clobjnames , $ o ) ;
$ ::ObjTypeHash { $ o } = $ t ;
}
}
}
2007-10-26 22:44:33 +00:00
}
2007-11-26 20:09:18 +00:00
2007-10-26 22:44:33 +00:00
# can't have -a with other obj sources
if ( $ ::opt_a
2007-11-26 20:09:18 +00:00
&& ( $ ::opt_o || $ ::filedata || @ ::noderange ) )
2007-10-26 22:44:33 +00:00
{
2007-11-26 20:09:18 +00:00
my $ rsp ;
$ rsp - > { data } - > [ 0 ] =
2009-10-12 07:41:52 +00:00
"Cannot use \'-a\' with \'-o\', a noderange or file input." ;
2007-11-26 20:09:18 +00:00
xCAT::MsgUtils - > message ( "E" , $ rsp , $ ::callback ) ;
return 3 ;
2007-10-26 22:44:33 +00:00
}
# if -a then get a list of all DB objects
if ( $ ::opt_a )
{
2007-11-26 20:09:18 +00:00
my @ tmplist ;
2007-10-26 22:44:33 +00:00
2007-11-26 20:09:18 +00:00
# for every type of data object get the list of defined objects
foreach my $ t ( keys % { xCAT::Schema:: defspec } )
{
2012-12-04 05:52:20 +00:00
# exclude the auditlog and eventlog,
# the auditlog and eventlog tables might be very big
# use lsdef -t auditlog or lsdef -t eventlog instead
if ( ( $ t eq 'auditlog' ) || ( $ t eq 'eventlog' ) ) { next ; }
2007-10-26 22:44:33 +00:00
2007-11-26 20:09:18 +00:00
$ ::objectsfrom_opta = 1 ;
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 ;
}
}
}
2007-10-26 22:44:33 +00:00
}
# must have object name(s) -
2011-03-20 05:56:47 +00:00
if ( ( scalar ( @ ::clobjnames ) == 0 ) && ( scalar ( @ ::fileobjnames ) == 0 ) )
2007-10-26 22:44:33 +00:00
{
2011-06-16 00:50:26 +00:00
my $ rsp ;
$ rsp - > { data } - > [ 0 ] =
"No object names were provided." ;
xCAT::MsgUtils - > message ( "E" , $ rsp , $ ::callback ) ;
2007-11-26 20:09:18 +00:00
return 3 ;
2007-10-26 22:44:33 +00:00
}
2007-11-26 20:09:18 +00:00
# combine object name all object names provided
2007-10-26 22:44:33 +00:00
@ ::allobjnames = @ ::clobjnames ;
2011-03-20 05:56:47 +00:00
if ( scalar ( @ ::fileobjnames ) > 0 )
2007-10-26 22:44:33 +00:00
{
2007-11-26 20:09:18 +00:00
# add list from stanza or xml file
2007-10-26 22:44:33 +00:00
push @ ::allobjnames , @ ::fileobjnames ;
}
2011-03-20 05:56:47 +00:00
elsif ( scalar ( @ ::objfilelist ) > 0 )
2007-11-26 20:09:18 +00:00
{
# add list from "-f" file option
push @ ::allobjnames , @ ::objfilelist ;
}
# check for the -w option
if ( $ ::opt_w )
{
2009-06-30 09:47:48 +00:00
my $ rc = xCAT::Utils - > parse_selection_string ( $ ::opt_w , \ % ::WhereHash ) ;
if ( $ rc != 0 )
2007-11-26 20:09:18 +00:00
{
2009-06-30 09:47:48 +00:00
my $ rsp ;
2009-10-12 07:41:52 +00:00
$ rsp - > { data } - > [ 0 ] = "Incorrect selection string specified with -w flag." ;
2009-06-30 09:47:48 +00:00
xCAT::MsgUtils - > message ( "E" , $ rsp , $ ::callback ) ;
return 3 ;
2007-11-26 20:09:18 +00:00
}
2009-10-10 07:05:40 +00:00
# For dynamic node groups, check the selection string
if ( ( $ ::opt_t eq 'group' ) && ( $ ::opt_d ) )
{
my $ datatype = $ xCAT:: Schema:: defspec { 'node' } ;
my @ nodeattrs = ( ) ;
foreach my $ this_attr ( @ { $ datatype - > { 'attrs' } } )
{
push @ nodeattrs , $ this_attr - > { attr_name } ;
}
foreach my $ whereattr ( keys % ::WhereHash )
{
if ( ! grep ( /^$whereattr$/ , @ nodeattrs ) )
{
my $ rsp ;
$ rsp - > { data } - > [ 0 ] = "Incorrect attribute \'$whereattr\' in the selection string specified with -w flag." ;
xCAT::MsgUtils - > message ( "E" , $ rsp , $ ::callback ) ;
return 1 ;
}
}
}
2007-11-26 20:09:18 +00:00
}
# check for the -i option
if ( $ ::opt_i && ( $ ::command ne 'lsdef' ) )
{
my $ rsp ;
$ rsp - > { data } - > [ 0 ] =
2009-10-12 07:41:52 +00:00
"The \'-i\' option is only valid for the lsdef command." ;
2007-11-26 20:09:18 +00:00
xCAT::MsgUtils - > message ( "E" , $ rsp , $ ::callback ) ;
return 3 ;
}
# just make a global list of the attr names provided
if ( $ ::opt_i )
{
@ ::AttrList = split ( ',' , $ ::opt_i ) ;
2013-03-20 06:56:32 +00:00
# nicips.<nic> should be changed to nicips
my $ i = 0 ;
for ( $ i = 0 ; $ i < ( scalar @ ::AttrList ) ; $ i + + )
{
if ( $ ::AttrList [ $ i ] =~ /^(nic\w+)\.(\w+)$/ )
{
$ ::AttrList [ $ i ] = $ 1 ;
push @ { $ ::NicsAttrHash { $ ::AttrList [ $ i ] } } , $ 2 ;
}
}
2007-11-26 20:09:18 +00:00
}
2007-10-26 22:44:33 +00:00
return 0 ;
}
#----------------------------------------------------------------------------
= head3 defmk
2007-11-26 20:09:18 +00:00
Support for the xCAT mkdef command .
2007-10-26 22:44:33 +00:00
Arguments:
Returns:
0 - OK
1 - error
Globals:
Error:
Example:
Comments:
2011-03-20 05:56:47 +00:00
Object names to create are derived from
- o , - t , w , - z , - x , or noderange !
Attr = val pairs come from cmd line args or - z / - x files
2007-10-26 22:44:33 +00:00
= cut
#-----------------------------------------------------------------------------
sub defmk
{
2007-11-26 20:09:18 +00:00
@ ::allobjnames = [] ;
my $ rc = 0 ;
my $ error = 0 ;
2007-10-26 22:44:33 +00:00
2011-03-20 05:56:47 +00:00
my % objTypeLists ;
2008-07-09 16:10:10 +00:00
2007-10-26 22:44:33 +00:00
# process the command line
2007-11-26 20:09:18 +00:00
$ rc = & processArgs ;
2011-03-18 14:58:45 +00:00
2007-10-26 22:44:33 +00:00
if ( $ rc != 0 )
{
2007-11-26 20:09:18 +00:00
# rc: 0 - ok, 1 - return, 2 - help, 3 - error
2011-03-20 05:56:47 +00:00
# 0 - continue
# 1 - return (like for version option)
# 2 - return with usage
# 3 - return error
if ( $ rc == 1 ) {
return 0 ;
} elsif ( $ rc == 2 ) {
& defmk_usage ;
return 0 ;
} elsif ( $ rc == 3 ) {
return 1 ;
}
}
2007-10-26 22:44:33 +00:00
2007-11-26 20:09:18 +00:00
# check options unique to these commands
if ( $ ::opt_p || $ ::opt_m )
{
# error
my $ rsp ;
$ rsp - > { data } - > [ 0 ] =
"The \'-p\' and \'-m\' options are not valid for the mkdef command." ;
xCAT::MsgUtils - > message ( "E" , $ rsp , $ ::callback ) ;
& defmk_usage ;
return 1 ;
}
if ( $ ::opt_t && ( $ ::opt_a || $ ::opt_z || $ ::opt_x ) )
{
my $ rsp ;
$ rsp - > { data } - > [ 0 ] =
2009-10-12 07:41:52 +00:00
"Cannot combine \'-t\' and \'-a\', \'-z\', or \'-x\' options." ;
2007-11-26 20:09:18 +00:00
xCAT::MsgUtils - > message ( "E" , $ rsp , $ ::callback ) ;
& defmk_usage ;
return 1 ;
}
2011-03-20 05:56:47 +00:00
# can't have -z with other obj sources
if ( $ ::opt_z && ( $ ::opt_o || @ ::noderange ) )
{
my $ rsp ;
$ rsp - > { data } - > [ 0 ] = "Cannot use \'-z\' with \'-o\' or a noderange." ;
$ rsp - > { data } - > [ 1 ] = "Example of -z usage:\n\t\'cat stanzafile | mkdef -z\'" ;
xCAT::MsgUtils - > message ( "E" , $ rsp , $ ::callback ) ;
& defmk_usage ;
return 1 ;
}
2008-02-28 19:28:41 +00:00
2007-11-26 20:09:18 +00:00
# check to make sure we have a list of objects to work with
if ( ! @ ::allobjnames )
{
my $ rsp ;
2009-10-12 07:41:52 +00:00
$ rsp - > { data } - > [ 0 ] = "No object names were provided." ;
2009-06-09 08:30:49 +00:00
xCAT::MsgUtils - > message ( "E" , $ rsp , $ ::callback ) ;
2007-11-26 20:09:18 +00:00
& defmk_usage ;
return 1 ;
2010-03-24 14:51:26 +00:00
} else {
my $ invalidnodename = ( ) ;
foreach my $ node ( @ ::allobjnames ) {
2010-05-28 09:50:15 +00:00
if ( ( $ node =~ /[A-Z]/ ) && ( ( ! $ ::opt_t ) || ( $ ::opt_t eq "node" ) ) ) {
2010-03-24 14:51:26 +00:00
$ invalidnodename . = ",$node" ;
}
}
if ( $ invalidnodename ) {
$ invalidnodename =~ s/,// ;
my $ rsp ;
2010-03-30 04:13:18 +00:00
$ rsp - > { data } - > [ 0 ] = "The node name \'$invalidnodename\' contains capital letters which may not be resolved correctly by the dns server." ;
2010-03-24 14:51:26 +00:00
xCAT::MsgUtils - > message ( "W" , $ rsp , $ ::callback ) ;
}
2007-11-26 20:09:18 +00:00
}
2007-10-26 22:44:33 +00:00
2007-11-26 20:09:18 +00:00
# set $objtype & fill in cmd line hash
if ( % ::ATTRS || ( $ ::opt_t eq "group" ) )
2007-10-26 22:44:33 +00:00
{
# if attr=val on cmd line then could only have one type
2007-11-26 20:09:18 +00:00
$ ::objtype = @ ::clobjtypes [ 0 ] ;
2007-10-26 22:44:33 +00:00
#
# set cli attrs for each object definition
#
2007-11-26 20:09:18 +00:00
foreach my $ objname ( @ ::clobjnames )
2007-10-26 22:44:33 +00:00
{
2007-11-26 20:09:18 +00:00
# set the objtype attr - if provided
if ( $ ::objtype )
{
$ ::CLIATTRS { $ objname } { objtype } = $ ::objtype ;
}
# get the data type definition from Schema.pm
my $ datatype = $ xCAT:: Schema:: defspec { $ ::objtype } ;
my @ list ;
foreach my $ this_attr ( sort @ { $ datatype - > { 'attrs' } } )
{
my $ a = $ this_attr - > { attr_name } ;
push ( @ list , $ a ) ;
}
# set the attrs from the attr=val pairs
foreach my $ attr ( keys % ::ATTRS )
{
2013-03-21 06:41:01 +00:00
my $ attrorig = $ attr ;
# nicips.eth0 => nicips
if ( $ attr =~ /^(nic\w+)\.\w+$/ )
{
$ attr = $ 1 ;
}
2011-03-20 05:56:47 +00:00
if ( ! grep ( /^$attr$/ , @ list ) && ( $ ::objtype ne 'site' ) && ( $ ::objtype ne 'monitoring' ) )
2007-11-26 20:09:18 +00:00
{
my $ rsp ;
$ rsp - > { data } - > [ 0 ] =
2009-10-12 07:41:52 +00:00
"\'$attr\' is not a valid attribute name for an object type of \'$::objtype\'." ;
$ rsp - > { data } - > [ 1 ] = "Skipping to the next attribute." ;
2007-11-26 20:09:18 +00:00
xCAT::MsgUtils - > message ( "E" , $ rsp , $ ::callback ) ;
$ error = 1 ;
next ;
}
else
{
2013-03-21 06:41:01 +00:00
$ ::CLIATTRS { $ objname } { $ attrorig } = $ ::ATTRS { $ attrorig } ;
2008-11-10 03:11:12 +00:00
if ( $ ::verbose )
{
my $ rsp ;
$ rsp - > { data } - > [ 0 ] = "\nFunction: defmk-->set the attrs for each object definition" ;
2013-03-21 06:41:01 +00:00
$ rsp - > { data } - > [ 1 ] = "defmk: objname=$objname, attr=$attrorig, value=$::ATTRS{$attrorig}" ;
2008-11-10 03:11:12 +00:00
xCAT::MsgUtils - > message ( "I" , $ rsp , $ ::callback ) ;
}
2007-11-26 20:09:18 +00:00
}
} # end - foreach attr
2007-10-26 22:44:33 +00:00
}
}
#
# Pull all the pieces together for the final hash
2011-03-20 05:56:47 +00:00
# - combines the command line attrs and input file attrs if provided
2007-10-26 22:44:33 +00:00
#
if ( & setFINALattrs != 0 )
{
2007-11-26 20:09:18 +00:00
$ error = 1 ;
2007-10-26 22:44:33 +00:00
}
2007-11-26 20:09:18 +00:00
# we need a list of objects that are
2011-03-20 05:56:47 +00:00
# already defined for each type.
2007-11-26 20:09:18 +00:00
foreach my $ t ( @ ::finalTypeList )
2007-10-26 22:44:33 +00:00
{
2007-11-26 20:09:18 +00:00
# special case for site table !!!!!!!!!!!!!!!!!!!!
if ( $ t eq 'site' )
{
@ { $ objTypeLists { $ t } } = 'clustersite' ;
}
else
2007-10-26 22:44:33 +00:00
{
2007-11-26 20:09:18 +00:00
@ { $ objTypeLists { $ t } } = xCAT::DBobjUtils - > getObjectsOfType ( $ t ) ;
2007-10-26 22:44:33 +00:00
}
2008-11-10 03:11:12 +00:00
if ( $ ::verbose )
{
my $ rsp ;
$ rsp - > { data } - > [ 0 ] = "\ndefmk: list objects that are defined for each type" ;
2009-10-12 07:41:52 +00:00
$ rsp - > { data } - > [ 1 ] = "@{$objTypeLists{$t}}" ;
2008-11-10 03:11:12 +00:00
xCAT::MsgUtils - > message ( "I" , $ rsp , $ ::callback ) ;
}
2007-11-26 20:09:18 +00:00
}
2007-10-26 22:44:33 +00:00
2011-12-20 12:10:55 +00:00
# Build up a hash for the array in objTypeLists
# for performance consideration, grep the array is not effective
my % objTypeListsHash ;
foreach my $ objk ( keys % objTypeLists )
{
foreach my $ obj ( @ { $ objTypeLists { $ objk } } ) {
$ objTypeListsHash { $ objk } { $ obj } = 1 ;
}
}
2009-05-07 13:58:00 +00:00
OBJ: foreach my $ obj ( keys % ::FINALATTRS )
2007-11-26 20:09:18 +00:00
{
2007-10-26 22:44:33 +00:00
2007-11-26 20:09:18 +00:00
my $ type = $ ::FINALATTRS { $ obj } { objtype } ;
2007-10-26 22:44:33 +00:00
2007-11-26 20:09:18 +00:00
# check to make sure we have type
if ( ! $ type )
{
my $ rsp ;
2009-10-12 07:41:52 +00:00
$ rsp - > { data } - > [ 0 ] = "No type was provided for object \'$obj\'." ;
$ rsp - > { data } - > [ 1 ] = "Skipping to the next object." ;
2007-11-26 20:09:18 +00:00
xCAT::MsgUtils - > message ( "I" , $ rsp , $ ::callback ) ;
2009-08-07 01:18:17 +00:00
$ error = 1 ;
2007-10-26 22:44:33 +00:00
next ;
}
2011-03-20 05:56:47 +00:00
# we don't want to overwrite any existing table row. This could
# happen if there are multiple table keys. (ex. networks table -
# where the object name is not either of the table keys - net
# & mask)
# just handle network objects for now -
if ( $ type eq 'network' ) {
my @ nets = xCAT::DBobjUtils - > getObjectsOfType ( 'network' ) ;
my % objhash ;
foreach my $ n ( @ nets ) {
$ objhash { $ n } = $ type ;
}
my % nethash = xCAT::DBobjUtils - > getobjdefs ( \ % objhash ) ;
foreach my $ o ( keys % nethash ) {
if ( ( $ nethash { $ o } { net } eq $ ::FINALATTRS { $ obj } { net } ) && ( $ nethash { $ o } { mask } eq $ ::FINALATTRS { $ obj } { mask } ) ) {
my $ rsp ;
$ rsp - > { data } - > [ 0 ] = "A network definition called \'$o\' already exists that contains the same net and mask values. Cannot create a definition for \'$obj\'." ;
xCAT::MsgUtils - > message ( "E" , $ rsp , $ ::callback ) ;
$ error = 1 ;
delete $ ::FINALATTRS { $ obj } ;
next OBJ ;
}
}
}
2009-05-07 13:58:00 +00:00
2007-11-26 20:09:18 +00:00
# if object already exists
2011-12-20 12:10:55 +00:00
if ( defined ( $ objTypeListsHash { $ type } { $ obj } ) && ( $ objTypeListsHash { $ type } { $ obj } == 1 ) )
2007-10-26 22:44:33 +00:00
{
2007-11-26 20:09:18 +00:00
if ( $ ::opt_f )
2007-10-26 22:44:33 +00:00
{
2007-11-26 20:09:18 +00:00
# remove the old object
2011-03-20 05:56:47 +00:00
my % objhash ;
2007-11-26 20:09:18 +00:00
$ objhash { $ obj } = $ type ;
if ( xCAT::DBobjUtils - > rmobjdefs ( \ % objhash ) != 0 )
{
$ error = 1 ;
my $ rsp ;
$ rsp - > { data } - > [ 0 ] =
2009-10-12 07:41:52 +00:00
"Could not remove the definition for \'$obj\'." ;
2007-11-26 20:09:18 +00:00
xCAT::MsgUtils - > message ( "E" , $ rsp , $ ::callback ) ;
}
2007-10-26 22:44:33 +00:00
}
2007-11-26 20:09:18 +00:00
else
2007-10-26 22:44:33 +00:00
{
2007-11-26 20:09:18 +00:00
# won't remove the old one unless the force option is used
my $ rsp ;
$ rsp - > { data } - > [ 0 ] =
2009-10-12 07:41:52 +00:00
"\nA definition for \'$obj\' already exists." ;
2007-11-26 20:09:18 +00:00
$ rsp - > { data } - > [ 1 ] =
2009-10-12 07:41:52 +00:00
"To remove the old definition and replace it with \na new definition use the force \'-f\' option." ;
2007-11-26 20:09:18 +00:00
$ rsp - > { data } - > [ 2 ] =
2009-10-12 07:41:52 +00:00
"To change the existing definition use the \'chdef\' command." ;
2007-11-26 20:09:18 +00:00
xCAT::MsgUtils - > message ( "E" , $ rsp , $ ::callback ) ;
$ error = 1 ;
2011-08-30 07:55:06 +00:00
delete $ ::FINALATTRS { $ obj } ;
2007-10-26 22:44:33 +00:00
next ;
2007-11-26 20:09:18 +00:00
2007-10-26 22:44:33 +00:00
}
2007-11-26 20:09:18 +00:00
}
2007-10-26 22:44:33 +00:00
2007-11-26 20:09:18 +00:00
# need to handle group definitions - special!
if ( $ type eq 'group' )
{
2007-10-26 22:44:33 +00:00
2007-11-26 20:09:18 +00:00
my @ memberlist ;
2007-10-26 22:44:33 +00:00
2007-11-26 20:09:18 +00:00
# if the group type was not set then set it
if ( ! $ ::FINALATTRS { $ obj } { grouptype } )
{
if ( $ ::opt_d )
{
2009-08-07 02:09:33 +00:00
# For dynamic node group,
# can not assign attributes for inherit
# only the 'objtype' in %::FINALATTRS
if ( scalar ( keys % { $ ::FINALATTRS { $ obj } } ) > 1 )
{
my $ rsp ;
2009-10-12 07:41:52 +00:00
$ rsp - > { data } - > [ 0 ] = "Can not assign attributes to dynamic node group \'$obj\'." ;
2009-08-07 02:09:33 +00:00
xCAT::MsgUtils - > message ( "E" , $ rsp , $ ::callback ) ;
$ error = 1 ;
2009-11-06 06:50:51 +00:00
delete ( $ ::FINALATTRS { $ obj } ) ;
2009-08-07 02:09:33 +00:00
next ;
}
2007-11-26 20:09:18 +00:00
$ ::FINALATTRS { $ obj } { grouptype } = 'dynamic' ;
$ ::FINALATTRS { $ obj } { members } = 'dynamic' ;
}
else
2007-10-26 22:44:33 +00:00
{
2007-11-26 20:09:18 +00:00
$ ::FINALATTRS { $ obj } { grouptype } = 'static' ;
2007-10-26 22:44:33 +00:00
}
2007-11-26 20:09:18 +00:00
}
2007-10-26 22:44:33 +00:00
2007-11-26 20:09:18 +00:00
# if dynamic and wherevals not set then set to opt_w
if ( $ ::FINALATTRS { $ obj } { grouptype } eq 'dynamic' )
{
if ( ! $ ::FINALATTRS { $ obj } { wherevals } )
{
if ( $ ::opt_w )
{
2009-06-30 09:47:48 +00:00
$ ::FINALATTRS { $ obj } { wherevals } = join ( '::' , @ { $ ::opt_w } ) ;
#$::FINALATTRS{$obj}{wherevals} = $::opt_w;
2007-11-26 20:09:18 +00:00
}
else
{
my $ rsp ;
$ rsp - > { data } - > [ 0 ] =
2009-10-12 07:41:52 +00:00
"The \'where\' attributes and values were not provided for dynamic group \'$obj\'." ;
$ rsp - > { data } - > [ 1 ] = "Skipping to the next group." ;
2007-11-26 20:09:18 +00:00
xCAT::MsgUtils - > message ( "E" , $ rsp , $ ::callback ) ;
next ;
}
}
}
2007-10-26 22:44:33 +00:00
2007-11-26 20:09:18 +00:00
# if static group then figure out memberlist
if ( $ ::FINALATTRS { $ obj } { grouptype } eq 'static' )
{
if ( $ ::opt_w && $ ::FINALATTRS { $ obj } { members } )
{
my $ rsp ;
$ rsp - > { data } - > [ 0 ] =
2009-10-12 07:41:52 +00:00
"Cannot use a list of members together with the \'-w\' option." ;
2007-11-26 20:09:18 +00:00
xCAT::MsgUtils - > message ( "E" , $ rsp , $ ::callback ) ;
return 1 ;
}
2007-10-26 22:44:33 +00:00
2007-11-26 20:09:18 +00:00
if ( $ ::FINALATTRS { $ obj } { members } )
{
@ memberlist = & noderange ( $ ::FINALATTRS { $ obj } { members } , 0 ) ;
2007-10-26 22:44:33 +00:00
2007-11-26 20:09:18 +00:00
# don't list all the nodes in the group table
2011-03-20 05:56:47 +00:00
# set the value to static and we'll figure out the list
# by looking in the nodelist table
2007-11-26 20:09:18 +00:00
$ ::FINALATTRS { $ obj } { members } = 'static' ;
2007-10-26 22:44:33 +00:00
2007-11-26 20:09:18 +00:00
}
else
{
if ( $ ::opt_w )
{
$ ::FINALATTRS { $ obj } { members } = 'static' ;
# get a list of nodes whose attr values match the
# "where" values and make that the memberlist of
# the group.
# get a list of all node nodes
my @ tmplist =
xCAT::DBobjUtils - > getObjectsOfType ( 'node' ) ;
# create a hash of obj names and types
2011-03-20 05:56:47 +00:00
my % objhash ;
2007-11-26 20:09:18 +00:00
foreach my $ n ( @ tmplist )
{
$ objhash { $ n } = 'node' ;
}
# get all the attrs for these nodes
2009-12-30 07:50:28 +00:00
my @ whereattrs = keys % ::WhereHash ;
my % myhash = xCAT::DBobjUtils - > getobjdefs ( \ % objhash , 0 , \ @ whereattrs ) ;
2007-11-26 20:09:18 +00:00
# see which ones match the where values
foreach my $ objname ( keys % myhash )
{
2009-06-30 09:47:48 +00:00
if ( xCAT::Utils - > selection_string_match ( \ % myhash , $ objname , \ % ::WhereHash ) ) {
2007-11-26 20:09:18 +00:00
push ( @ memberlist , $ objname ) ;
}
}
}
else
{
my $ rsp ;
$ rsp - > { data } - > [ 0 ] =
2009-10-12 07:41:52 +00:00
"Cannot determine a member list for group \'$obj\'." ;
2012-06-27 07:05:01 +00:00
xCAT::MsgUtils - > message ( "W" , $ rsp , $ ::callback ) ;
2007-11-26 20:09:18 +00:00
}
}
2007-10-26 22:44:33 +00:00
2009-10-12 07:41:52 +00:00
# mkdef -t group should not create new nodes
my @ tmpmemlist = ( ) ;
my @ allnodes = xCAT::DBobjUtils - > getObjectsOfType ( 'node' ) ;
foreach my $ tmpnode ( @ memberlist )
{
if ( ! grep ( /^$tmpnode$/ , @ allnodes ) )
{
my $ rsp ;
$ rsp - > { data } - > [ 0 ] = "Could not find a node named \'$tmpnode\', skipping to the next node." ;
xCAT::MsgUtils - > message ( "E" , $ rsp , $ ::callback ) ;
}
else
{
push @ tmpmemlist , $ tmpnode ;
}
}
@ memberlist = @ tmpmemlist ;
2007-11-26 20:09:18 +00:00
# need to add group name to all members in nodelist table
my $ tab =
xCAT::Table - > new ( 'nodelist' , - create = > 1 , - autocommit = > 0 ) ;
2007-10-26 22:44:33 +00:00
2007-11-26 20:09:18 +00:00
my $ newgroups ;
2009-10-28 20:03:35 +00:00
my $ changed = 0 ;
2007-11-26 20:09:18 +00:00
foreach my $ n ( @ memberlist )
{
2008-11-10 03:11:12 +00:00
if ( $ ::verbose )
{
my $ rsp ;
$ rsp - > { data } - > [ 0 ] = "defmk: add group name [$n] to nodelist table" ;
xCAT::MsgUtils - > message ( "I" , $ rsp , $ ::callback ) ;
}
2007-10-26 22:44:33 +00:00
2007-11-26 20:09:18 +00:00
# add this group name to the node entry in
2011-03-20 05:56:47 +00:00
# the nodelist table
2008-07-09 16:10:10 +00:00
#$nodehash{$n}{groups} = $obj;
2007-10-26 22:44:33 +00:00
2007-11-26 20:09:18 +00:00
# get the current value
my $ grps = $ tab - > getNodeAttribs ( $ n , [ 'groups' ] ) ;
2007-10-26 22:44:33 +00:00
2007-11-26 20:09:18 +00:00
# if it's not already in the "groups" list then add it
my @ tmpgrps = split ( /,/ , $ grps - > { 'groups' } ) ;
2007-10-26 22:44:33 +00:00
2007-11-26 20:09:18 +00:00
if ( ! grep ( /^$obj$/ , @ tmpgrps ) )
{
if ( $ grps and $ grps - > { 'groups' } )
{
$ newgroups = "$grps->{'groups'},$obj" ;
2007-10-26 22:44:33 +00:00
2007-11-26 20:09:18 +00:00
}
else
{
$ newgroups = $ obj ;
}
}
2007-10-26 22:44:33 +00:00
2007-11-26 20:09:18 +00:00
# add this group name to the node entry in
# the nodelist table
if ( $ newgroups )
{
$ tab - > setNodeAttribs ( $ n , { groups = > $ newgroups } ) ;
2011-03-20 05:56:47 +00:00
$ changed = 1 ;
2007-11-26 20:09:18 +00:00
}
2007-10-26 22:44:33 +00:00
2007-11-26 20:09:18 +00:00
}
2011-03-20 05:56:47 +00:00
if ( $ changed ) {
$ tab - > commit ;
}
2007-11-26 20:09:18 +00:00
}
2008-02-28 19:05:31 +00:00
} # end - if group type
2007-11-26 20:09:18 +00:00
2009-12-01 03:30:18 +00:00
# If none of the attributes in nodelist is defined: groups,status,appstatus,primarysn,comments,disable
# the nodelist table will not be updated, caused mkdef failed.
# We give a restriction that the "groups" must be specified with mkdef.
if ( ( $ type eq "node" ) && ! defined ( $ ::FINALATTRS { $ obj } { groups } ) )
{
my $ rsp ;
$ rsp - > { data } - > [ 0 ] =
"Attribute \'groups\' is not specified for node \'$obj\', skipping to the next node." ;
xCAT::MsgUtils - > message ( "E" , $ rsp , $ ::callback ) ;
$ error = 1 ;
next ;
}
2011-12-20 12:10:55 +00:00
# Removed the code to handle the nodegroup table with mkdef -t node groups=xxx
# Only dynamic groups should be in nodegroup table
# Do not try to add static group into the nodegroup table
# performance!!!!
2013-04-08 07:03:24 +00:00
if ( defined ( $ ::opt_setattr ) && $ type eq "osimage" ) {
my $ rc = & parse_attr_for_osimage ( $ ::command , $ ::FINALATTRS { $ obj } ) ;
if ( $ rc ) {
$ error = $ rc ;
next ;
}
}
2008-10-12 17:43:32 +00:00
} # end of each obj
2007-11-26 20:09:18 +00:00
#
# write each object into the tables in the xCAT database
#
if ( xCAT::DBobjUtils - > setobjdefs ( \ % ::FINALATTRS ) != 0 )
{
my $ rsp ;
2009-10-12 07:41:52 +00:00
$ rsp - > { data } - > [ 0 ] = "Could not write data to the xCAT database." ;
2007-11-26 20:09:18 +00:00
2011-03-20 05:56:47 +00:00
# xCAT::MsgUtils->message("E", $rsp, $::callback);
2007-11-26 20:09:18 +00:00
$ error = 1 ;
}
2007-10-26 22:44:33 +00:00
2007-11-26 20:09:18 +00:00
if ( $ error )
{
my $ rsp ;
$ rsp - > { data } - > [ 0 ] =
2009-10-12 07:41:52 +00:00
"One or more errors occured when attempting to create or modify xCAT \nobject definitions." ;
2007-11-26 20:09:18 +00:00
xCAT::MsgUtils - > message ( "E" , $ rsp , $ ::callback ) ;
return 1 ;
}
else
{
if ( $ ::verbose )
{
# give results
my $ rsp ;
$ rsp - > { data } - > [ 0 ] =
2009-10-12 07:41:52 +00:00
"The database was updated for the following objects:" ;
2007-11-26 20:09:18 +00:00
xCAT::MsgUtils - > message ( "I" , $ rsp , $ ::callback ) ;
my $ n = 1 ;
foreach my $ o ( sort ( keys % ::FINALATTRS ) )
{
2009-10-12 07:41:52 +00:00
$ rsp - > { data } - > [ $ n ] = "$o" ;
2007-11-26 20:09:18 +00:00
$ n + + ;
}
xCAT::MsgUtils - > message ( "I" , $ rsp , $ ::callback ) ;
}
else
{
my $ rsp ;
2011-10-09 01:39:37 +00:00
my $ nodenum = scalar ( keys % ::FINALATTRS ) ;
2007-11-26 20:09:18 +00:00
$ rsp - > { data } - > [ 0 ] =
2011-10-09 01:39:37 +00:00
"$nodenum object definitions have been created or modified." ;
2007-11-26 20:09:18 +00:00
xCAT::MsgUtils - > message ( "I" , $ rsp , $ ::callback ) ;
}
return 0 ;
}
}
#----------------------------------------------------------------------------
= head3 defch
Support for the xCAT chdef command .
Arguments:
2007-10-26 22:44:33 +00:00
Returns:
0 - OK
1 - error
Globals:
Error:
Example:
Comments:
2011-03-20 05:56:47 +00:00
Object names to create are derived from
- o , - t , w , - z , - x , or noderange !
Attr = val pairs come from cmd line args or - z / - x files
2007-10-26 22:44:33 +00:00
= cut
#-----------------------------------------------------------------------------
2007-11-26 20:09:18 +00:00
sub defch
2007-10-26 22:44:33 +00:00
{
2007-11-26 20:09:18 +00:00
@ ::allobjnames = [] ;
my $ rc = 0 ;
my $ error = 0 ;
2009-10-12 07:41:52 +00:00
my $ firsttime = 1 ;
2007-11-26 20:09:18 +00:00
2009-10-12 07:41:52 +00:00
my % objTypeLists ;
# hash that contains all the new objects that are being created
my % newobjects ;
2008-07-09 16:10:10 +00:00
2007-11-26 20:09:18 +00:00
# process the command line
$ rc = & processArgs ;
2011-03-18 14:58:45 +00:00
2007-11-26 20:09:18 +00:00
if ( $ rc != 0 )
2007-10-26 22:44:33 +00:00
{
2007-11-26 20:09:18 +00:00
# rc: 0 - ok, 1 - return, 2 - help, 3 - error
2011-03-20 05:56:47 +00:00
# 0 - continue
# 1 - return (like for version option)
# 2 - return with usage
# 3 - return error
if ( $ rc == 1 ) {
return 0 ;
} elsif ( $ rc == 2 ) {
& defch_usage ;
return 0 ;
} elsif ( $ rc == 3 ) {
return 1 ;
}
}
2011-03-18 14:58:45 +00:00
2007-11-26 20:09:18 +00:00
#
# check options unique to this command
#
2010-09-08 10:38:12 +00:00
if ( $ ::opt_n ) {
# check the option for changing object name
if ( $ ::opt_n && ( $ ::opt_d || $ ::opt_p || $ ::opt_m || $ ::opt_z || $ ::opt_w ) ) {
my $ rsp ;
$ rsp - > { data } - > [ 0 ] = "Cannot combine \'-n\' and \'-d\',\'-p\',\'-m\',\'-z\',\'-w\' options." ;
xCAT::MsgUtils - > message ( "E" , $ rsp , $ ::callback ) ;
& defch_usage ;
return 1 ;
}
if ( scalar ( @ ::clobjnames ) > 1 ) {
my $ rsp ;
$ rsp - > { data } - > [ 0 ] =
"The \'-n\' option (changing object name) can only work on one object." ;
xCAT::MsgUtils - > message ( "E" , $ rsp , $ ::callback ) ;
& defch_usage ;
return 1 ;
}
my % objhash = ( $ ::clobjnames [ 0 ] = > $ ::clobjtypes [ 0 ] ) ;
my @ validnode = xCAT::DBobjUtils - > getObjectsOfType ( $ ::clobjtypes [ 0 ] ) ;
if ( ! grep /^$::clobjnames[0]$/ , @ validnode ) {
my $ rsp ;
$ rsp - > { data } - > [ 0 ] =
"The $::clobjnames[0] is not a valid object." ;
xCAT::MsgUtils - > message ( "E" , $ rsp , $ ::callback ) ;
return 1 ;
}
# Use the getobjdefs function to get a hash which including
# all the records in the table that should be changed
my % chnamehash = ( ) ;
xCAT::DBobjUtils - > getobjdefs ( \ % objhash , $ ::VERBOSE , undef , \ % chnamehash ) ;
foreach my $ tb ( keys % chnamehash ) {
my $ tab = xCAT::Table - > new ( $ tb ) ;
unless ( $ tab ) {
my $ rsp ;
push @ { $ rsp - > { data } } , "Unable to open $tb table." ;
xCAT::MsgUtils - > message ( "E" , $ rsp , $ ::callback ) ;
return 1 ;
}
my $ index = 0 ;
my @ taben = @ { $ chnamehash { $ tb } } ;
# In the @taben, there are several pair of value for
# changing the key value of a table entry
my @ keystrs = ( ) ;
while ( $ taben [ $ index ] ) {
# Make a key word string to avoid that changing
# one table record multiple times
my $ keystr ;
foreach my $ key ( sort ( keys % { $ taben [ $ index ] } ) ) {
$ keystr . = "$key:$taben[$index]{$key}:" ;
}
if ( grep /^$keystr$/ , @ keystrs ) {
$ index += 2 ;
next ;
}
push @ keystrs , $ keystr ;
my % chname = ( $ taben [ $ index + 1 ] = > $ ::opt_n ) ;
$ tab - > setAttribs ( $ taben [ $ index ] , \ % chname ) ;
$ index += 2 ;
}
}
my $ rsp ;
push @ { $ rsp - > { data } } , "Changed the object name from $::clobjnames[0] to $::opt_n." ;
xCAT::MsgUtils - > message ( "I" , $ rsp , $ ::callback ) ;
return 0 ;
}
2007-11-26 20:09:18 +00:00
if ( $ ::opt_f )
{
# error
my $ rsp ;
$ rsp - > { data } - > [ 0 ] =
"The \'-f\' option is not valid for the chdef command." ;
xCAT::MsgUtils - > message ( "E" , $ rsp , $ ::callback ) ;
& defch_usage ;
return 1 ;
}
2007-10-26 22:44:33 +00:00
2007-11-26 20:09:18 +00:00
if ( $ ::opt_t && ( $ ::opt_a || $ ::opt_z || $ ::opt_x ) )
{
my $ rsp ;
$ rsp - > { data } - > [ 0 ] =
2009-10-12 07:41:52 +00:00
"Cannot combine \'-t\' and \'-a\', \'-z\', or \'-x\' options." ;
2007-11-26 20:09:18 +00:00
xCAT::MsgUtils - > message ( "E" , $ rsp , $ ::callback ) ;
& defch_usage ;
return 1 ;
}
2011-03-20 05:56:47 +00:00
# can't have -z with other obj sources
if ( $ ::opt_z && ( $ ::opt_o || @ ::noderange ) )
{
my $ rsp ;
$ rsp - > { data } - > [ 0 ] = "Cannot use \'-z\' with \'-o\' or a noderange." ;
$ rsp - > { data } - > [ 1 ] = "Example of -z usage:\n\t\'cat stanzafile | chdef -z\'" ;
xCAT::MsgUtils - > message ( "E" , $ rsp , $ ::callback ) ;
& defch_usage ;
return 1 ;
}
2013-04-08 07:03:24 +00:00
if ( $ ::opt_t eq "osimage" && $ ::opt_setattr && ( $ ::opt_p || $ ::opt_m ) ) {
my $ rsp ;
$ rsp - > { data } - > [ 0 ] = "Cannot use \'-u\' with \'-p\' or \'-m\'." ;
xCAT::MsgUtils - > message ( "E" , $ rsp , $ ::callback ) ;
return 1 ;
}
2008-02-28 19:28:41 +00:00
2007-11-26 20:09:18 +00:00
# check to make sure we have a list of objects to work with
if ( ! @ ::allobjnames )
{
my $ rsp ;
2009-10-12 07:41:52 +00:00
$ rsp - > { data } - > [ 0 ] = "No object names were provided." ;
2009-06-09 08:30:49 +00:00
xCAT::MsgUtils - > message ( "E" , $ rsp , $ ::callback ) ;
2007-11-26 20:09:18 +00:00
& defch_usage ;
return 1 ;
}
# set $objtype & fill in cmd line hash
2013-04-08 07:03:24 +00:00
if ( % ::ATTRS || ( $ ::opt_t eq "group" ) )
2007-11-26 20:09:18 +00:00
{
# if attr=val on cmd line then could only have one type
$ ::objtype = @ ::clobjtypes [ 0 ] ;
#
# set cli attrs for each object definition
#
foreach my $ objname ( @ ::clobjnames )
2007-10-26 22:44:33 +00:00
{
2007-11-26 20:09:18 +00:00
# set the objtype attr - if provided
if ( $ ::objtype )
{
chomp $ ::objtype ;
$ ::CLIATTRS { $ objname } { objtype } = $ ::objtype ;
}
# get the data type definition from Schema.pm
my $ datatype = $ xCAT:: Schema:: defspec { $ ::objtype } ;
2011-12-27 06:12:19 +00:00
my % list ;
2007-11-26 20:09:18 +00:00
foreach my $ this_attr ( sort @ { $ datatype - > { 'attrs' } } )
{
my $ a = $ this_attr - > { attr_name } ;
2011-12-27 06:12:19 +00:00
$ list { $ a } = 1 ;
2007-11-26 20:09:18 +00:00
}
# set the attrs from the attr=val pairs
foreach my $ attr ( keys % ::ATTRS )
{
2013-03-21 06:41:01 +00:00
my $ attrorig = $ attr ;
# nicips.eth0 => nicips
if ( $ attr =~ /^(nic\w+)\.\w+$/ )
{
$ attr = $ 1 ;
}
2011-12-27 06:12:19 +00:00
if ( ! defined ( $ list { $ attr } ) && ( $ ::objtype ne 'site' ) && ( $ ::objtype ne 'monitoring' ) )
2007-11-26 20:09:18 +00:00
{
my $ rsp ;
$ rsp - > { data } - > [ 0 ] =
2012-12-06 13:30:55 +00:00
"\'$attr\' is not a valid attribute name for an object type of \'$::objtype\'." ;
2009-10-12 07:41:52 +00:00
$ rsp - > { data } - > [ 1 ] = "Skipping to the next attribute." ;
2007-11-26 20:09:18 +00:00
xCAT::MsgUtils - > message ( "E" , $ rsp , $ ::callback ) ;
$ error = 1 ;
next ;
}
else
{
2013-03-21 06:41:01 +00:00
$ ::CLIATTRS { $ objname } { $ attrorig } = $ ::ATTRS { $ attrorig } ;
2007-11-26 20:09:18 +00:00
}
}
2007-10-26 22:44:33 +00:00
}
}
2007-11-26 20:09:18 +00:00
#
# Pull all the pieces together for the final hash
2011-03-20 05:56:47 +00:00
# - combines the command line attrs and input file attrs if provided
2007-11-26 20:09:18 +00:00
#
if ( & setFINALattrs != 0 )
{
$ error = 1 ;
}
# we need a list of objects that are
# already defined for each type.
foreach my $ t ( @ ::finalTypeList )
{
# special case for site table !!!!!!!!!!!!!!!!!!!!
if ( $ t eq 'site' )
{
@ { $ objTypeLists { $ t } } = 'clustersite' ;
}
else
{
@ { $ objTypeLists { $ t } } = xCAT::DBobjUtils - > getObjectsOfType ( $ t ) ;
}
2008-11-10 03:11:12 +00:00
if ( $ ::verbose )
{
my $ rsp ;
$ rsp - > { data } - > [ 0 ] = "\ndefch: list objects that are defined for each type" ;
2009-10-12 07:41:52 +00:00
$ rsp - > { data } - > [ 1 ] = "@{$objTypeLists{$t}}" ;
2008-11-10 03:11:12 +00:00
xCAT::MsgUtils - > message ( "I" , $ rsp , $ ::callback ) ;
}
2007-11-26 20:09:18 +00:00
}
2011-12-27 06:12:19 +00:00
# Build up a hash for the array in objTypeLists
# for performance consideration, grep the array is not effective
my % objTypeListsHash ;
foreach my $ objk ( keys % objTypeLists )
{
foreach my $ obj ( @ { $ objTypeLists { $ objk } } ) {
$ objTypeListsHash { $ objk } { $ obj } = 1 ;
}
}
2007-11-26 20:09:18 +00:00
foreach my $ obj ( keys % ::FINALATTRS )
{
my $ isDefined = 0 ;
my $ type = $ ::FINALATTRS { $ obj } { objtype } ;
2013-04-08 07:03:24 +00:00
my % attrhash ;
my @ img_attrs = qw( imagetype provmethod profile osname osvers osarch ) ;
if ( $ ::opt_setattr && $ type eq "osimage" ) {
my % tmp_objhash = ( ) ;
% attrhash = xCAT::DBobjUtils - > getobjdefs ( { $ obj = > $ type } , $ ::VERBOSE , \ @ img_attrs ) ;
}
2007-11-26 20:09:18 +00:00
# check to make sure we have type
if ( ! $ type )
{
my $ rsp ;
2009-10-12 07:41:52 +00:00
$ rsp - > { data } - > [ 0 ] = "No type was provided for object \'$obj\'." ;
$ rsp - > { data } - > [ 1 ] = "Skipping to the next object." ;
2007-11-26 20:09:18 +00:00
xCAT::MsgUtils - > message ( "E" , $ rsp , $ ::callback ) ;
$ error = 1 ;
next ;
}
2011-12-27 06:12:19 +00:00
if ( defined ( $ objTypeListsHash { $ type } { $ obj } ) && ( $ objTypeListsHash { $ type } { $ obj } == 1 ) )
2007-11-26 20:09:18 +00:00
{
$ isDefined = 1 ;
}
2009-10-12 07:41:52 +00:00
if ( ! $ isDefined )
{
$ newobjects { $ obj } = $ type ;
2010-03-01 10:11:13 +00:00
if ( ! grep ( /^groups$/ , keys % { $ ::FINALATTRS { $ obj } } ) ) {
$ ::FINALATTRS { $ obj } { 'groups' } = "all" ;
}
2009-10-12 07:41:52 +00:00
}
2008-02-28 19:05:31 +00:00
2008-07-09 16:10:10 +00:00
if ( ! $ isDefined && $ ::opt_m )
2007-11-26 20:09:18 +00:00
{
#error - cannot remove items from an object that does not exist.
my $ rsp ;
$ rsp - > { data } - > [ 0 ] =
2009-10-12 07:41:52 +00:00
"The \'-m\' option is not valid since the \'$obj\' definition does not exist." ;
2007-11-26 20:09:18 +00:00
xCAT::MsgUtils - > message ( "E" , $ rsp , $ ::callback ) ;
$ error = 1 ;
next ;
}
#
# need to handle group definitions - special!
2011-03-20 05:56:47 +00:00
# - may need to update the node definitions for the group members
2007-11-26 20:09:18 +00:00
#
if ( $ type eq 'group' )
{
my % grphash ;
my @ memberlist ;
# what kind of group is this? - static or dynamic
my $ grptype ;
2011-03-20 05:56:47 +00:00
my % objhash ;
2009-08-07 02:09:33 +00:00
if ( $ ::opt_d )
{
# For dynamic node group,
# can not assign attributes for inherit
# only the 'objtype' in %::FINALATTRS
if ( scalar ( keys % { $ ::FINALATTRS { $ obj } } ) > 1 )
{
my $ rsp ;
2009-10-12 07:41:52 +00:00
$ rsp - > { data } - > [ 0 ] = "Can not assign attributes to dynamic node group \'$obj\'." ;
2009-08-07 02:09:33 +00:00
xCAT::MsgUtils - > message ( "E" , $ rsp , $ ::callback ) ;
$ error = 1 ;
2009-11-06 06:50:51 +00:00
delete ( $ ::FINALATTRS { $ obj } ) ;
2009-08-07 02:09:33 +00:00
next ;
}
}
2007-11-26 20:09:18 +00:00
if ( $ isDefined )
{
$ objhash { $ obj } = $ type ;
2009-12-30 07:50:28 +00:00
my @ finalattrs = keys % { $ ::FINALATTRS { $ obj } } ;
push @ finalattrs , 'grouptype' ;
% grphash = xCAT::DBobjUtils - > getobjdefs ( \ % objhash , 0 , \ @ finalattrs ) ;
2012-11-30 09:08:47 +00:00
if ( ! ( % grphash ) )
2007-11-26 20:09:18 +00:00
{
my $ rsp ;
$ rsp - > { data } - > [ 0 ] =
2009-10-12 07:41:52 +00:00
"Could not get xCAT object definitions." ;
2007-11-26 20:09:18 +00:00
xCAT::MsgUtils - > message ( "E" , $ rsp , $ ::callback ) ;
return 1 ;
}
2009-06-23 05:51:28 +00:00
$ grptype = $ grphash { $ obj } { grouptype } ;
2009-11-06 06:50:51 +00:00
if ( ( $ grptype eq "dynamic" ) && ( scalar ( keys % { $ ::FINALATTRS { $ obj } } ) > 1 ) )
{
my $ rsp ;
$ rsp - > { data } - > [ 0 ] = "Can not assign attributes to dynamic node group \'$obj\'." ;
xCAT::MsgUtils - > message ( "E" , $ rsp , $ ::callback ) ;
$ error = 1 ;
delete ( $ ::FINALATTRS { $ obj } ) ;
next ;
}
2011-03-20 05:56:47 +00:00
# for now all groups are static
#$grptype = 'static';
2007-11-26 20:09:18 +00:00
}
else
{ #not defined
if ( $ ::FINALATTRS { $ obj } { grouptype } )
{
$ grptype = $ ::FINALATTRS { $ obj } { grouptype } ;
}
elsif ( $ ::opt_d )
{
$ grptype = 'dynamic' ;
}
else
{
$ grptype = 'static' ;
}
}
# make sure wherevals was set - if info provided
if ( ! $ ::FINALATTRS { $ obj } { wherevals } )
{
if ( $ ::opt_w )
{
2009-06-30 09:47:48 +00:00
$ ::FINALATTRS { $ obj } { wherevals } = join ( '::' , @ { $ ::opt_w } ) ;
#$::FINALATTRS{$obj}{wherevals} = $::opt_w;
2007-11-26 20:09:18 +00:00
}
}
# get the @memberlist for static group
2011-03-20 05:56:47 +00:00
# - if provided - to use below
2013-05-14 07:17:21 +00:00
# if the static group is not defined in the nodegroup table
# the grptype will be undef
if ( ! $ grptype || ( $ grptype eq 'static' ) )
2007-11-26 20:09:18 +00:00
{
# check for bad cmd line options
if ( $ ::opt_w && $ ::FINALATTRS { $ obj } { members } )
{
my $ rsp ;
$ rsp - > { data } - > [ 0 ] =
2009-10-12 07:41:52 +00:00
"Cannot use a list of members together with the \'-w\' option." ;
2007-11-26 20:09:18 +00:00
xCAT::MsgUtils - > message ( "E" , $ rsp , $ ::callback ) ;
$ error = 1 ;
next ;
}
if ( $ ::FINALATTRS { $ obj } { members } )
{
@ memberlist = & noderange ( $ ::FINALATTRS { $ obj } { members } , 0 ) ;
# don't list all the nodes in the group table
# set the value to static and we figure out the list
# by looking in the nodelist table
$ ::FINALATTRS { $ obj } { members } = 'static' ;
}
elsif ( $ ::FINALATTRS { $ obj } { wherevals } )
{
$ ::FINALATTRS { $ obj } { members } = 'static' ;
# get a list of nodes whose attr values match the
# "where" values and make that the memberlist of
# the group.
# get a list of all node nodes
my @ tmplist = xCAT::DBobjUtils - > getObjectsOfType ( 'node' ) ;
# create a hash of obj names and types
2011-03-20 05:56:47 +00:00
my % objhash ;
2007-11-26 20:09:18 +00:00
foreach my $ n ( @ tmplist )
{
$ objhash { $ n } = 'node' ;
}
2009-06-30 09:47:48 +00:00
# get a list of attr=val pairs, is it really necessary??
my @ wherevals = split ( /::/ , $ ::FINALATTRS { $ obj } { wherevals } ) ;
my $ rc = xCAT::Utils - > parse_selection_string ( \ @ wherevals , \ % ::WhereHash ) ;
if ( $ rc != 0 )
2007-11-26 20:09:18 +00:00
{
2009-06-30 09:47:48 +00:00
my $ rsp ;
2009-07-06 09:45:22 +00:00
$ rsp - > { data } - > [ 0 ] = "Incorrect selection string" ;
2009-06-30 09:47:48 +00:00
xCAT::MsgUtils - > message ( "E" , $ rsp , $ ::callback ) ;
return 3 ;
2007-11-26 20:09:18 +00:00
}
2009-12-30 07:50:28 +00:00
# get the attrs for these nodes
my @ whereattrs = keys % ::WhereHash ;
my % myhash = xCAT::DBobjUtils - > getobjdefs ( \ % objhash , 0 , \ @ whereattrs ) ;
2007-11-26 20:09:18 +00:00
# see which ones match the where values
foreach my $ objname ( keys % myhash )
{
2009-06-30 09:47:48 +00:00
if ( xCAT::Utils - > selection_string_match ( \ % myhash , $ objname , \ % ::WhereHash ) ) {
2007-11-26 20:09:18 +00:00
push ( @ memberlist , $ objname ) ;
}
2009-06-30 09:47:48 +00:00
2007-11-26 20:09:18 +00:00
}
}
} # end - get memberlist for static group
2009-10-12 07:41:52 +00:00
# chdef -t group should not create new nodes
my @ tmpmemlist = ( ) ;
my @ allnodes = xCAT::DBobjUtils - > getObjectsOfType ( 'node' ) ;
foreach my $ tmpnode ( @ memberlist )
{
if ( ! grep ( /^$tmpnode$/ , @ allnodes ) )
{
my $ rsp ;
$ rsp - > { data } - > [ 0 ] = "Could not find a node named \'$tmpnode\', skipping to the next node." ;
xCAT::MsgUtils - > message ( "E" , $ rsp , $ ::callback ) ;
}
else
{
push @ tmpmemlist , $ tmpnode ;
}
}
@ memberlist = @ tmpmemlist ;
2007-11-26 20:09:18 +00:00
if ( ! $ isDefined )
{
# if the group type was not set then set it
if ( ! $ ::FINALATTRS { $ obj } { grouptype } )
{
if ( $ ::opt_d )
{
$ ::FINALATTRS { $ obj } { grouptype } = 'dynamic' ;
$ ::FINALATTRS { $ obj } { members } = 'dynamic' ;
if ( ! $ ::FINALATTRS { $ obj } { wherevals } )
{
my $ rsp ;
$ rsp - > { data } - > [ 0 ] =
2009-10-12 07:41:52 +00:00
"The \'where\' attributes and values were not provided for dynamic group \'$obj\'." ;
$ rsp - > { data } - > [ 1 ] = "Skipping to the next group." ;
2007-11-26 20:09:18 +00:00
xCAT::MsgUtils - > message ( "E" , $ rsp , $ ::callback ) ;
$ error = 1 ;
next ;
}
}
else
{
$ ::FINALATTRS { $ obj } { grouptype } = 'static' ;
}
}
# if this is a static group
2011-03-20 05:56:47 +00:00
# then update the "groups" attr of each member node
2007-11-26 20:09:18 +00:00
if ( $ ::FINALATTRS { $ obj } { grouptype } eq 'static' )
{
# for each node in memberlist add this group
# name to the groups attr of the node
my % membhash ;
2008-07-09 16:10:10 +00:00
foreach my $ n ( @ memberlist )
2007-11-26 20:09:18 +00:00
{
2008-02-28 19:05:31 +00:00
2007-11-26 20:09:18 +00:00
$ membhash { $ n } { groups } = $ obj ;
2009-10-10 03:20:33 +00:00
$ membhash { $ n } { objtype } = 'node' ;
2007-11-26 20:09:18 +00:00
}
$ ::plus_option = 1 ;
$ ::minus_option = 0 ;
if ( xCAT::DBobjUtils - > setobjdefs ( \ % membhash ) != 0 )
{
$ error = 1 ;
}
$ ::plus_option = 0 ;
}
}
else
{ # group is defined
# if a list of members is provided then update the node entries
# note: the members attr of the group def will be set
2011-03-20 05:56:47 +00:00
# to static
2007-11-26 20:09:18 +00:00
if ( @ memberlist )
{
# options supported
if ( $ ::opt_m )
{ # removing these members
# for each node in memberlist - remove this group
# from the groups attr
my % membhash ;
2008-07-09 16:10:10 +00:00
foreach my $ n ( @ memberlist )
2007-11-26 20:09:18 +00:00
{
$ membhash { $ n } { groups } = $ obj ;
$ membhash { $ n } { objtype } = 'node' ;
}
$ ::plus_option = 0 ;
$ ::minus_option = 1 ;
if ( xCAT::DBobjUtils - > setobjdefs ( \ % membhash ) != 0 )
{
$ error = 1 ;
}
$ ::minus_option = 0 ;
}
elsif ( $ ::opt_p )
{ #adding these new members
# for each node in memberlist add this group
# name to the groups attr
my % membhash ;
2008-07-09 16:10:10 +00:00
foreach my $ n ( @ memberlist )
2007-11-26 20:09:18 +00:00
{
$ membhash { $ n } { groups } = $ obj ;
$ membhash { $ n } { objtype } = 'node' ;
}
$ ::plus_option = 1 ;
$ ::minus_option = 0 ;
if ( xCAT::DBobjUtils - > setobjdefs ( \ % membhash ) != 0 )
{
$ error = 1 ;
}
$ ::plus_option = 0 ;
}
else
{ # replace the members list altogether
# this is the default for the chdef command
2011-03-20 05:56:47 +00:00
if ( $ firsttime ) {
2007-11-26 20:09:18 +00:00
# get the current members list
2011-03-20 05:56:47 +00:00
$ grphash { $ obj } { 'grouptype' } = "static" ;
2007-11-26 20:09:18 +00:00
my $ list =
xCAT::DBobjUtils - > getGroupMembers ( $ obj , \ % grphash ) ;
my @ currentlist = split ( ',' , $ list ) ;
# for each node in currentlist - remove group name
2011-03-20 05:56:47 +00:00
# from groups attr
2007-11-26 20:09:18 +00:00
my % membhash ;
2008-07-09 16:10:10 +00:00
foreach my $ n ( @ currentlist )
2007-11-26 20:09:18 +00:00
{
$ membhash { $ n } { groups } = $ obj ;
$ membhash { $ n } { objtype } = 'node' ;
}
$ ::plus_option = 0 ;
$ ::minus_option = 1 ;
if ( xCAT::DBobjUtils - > setobjdefs ( \ % membhash ) != 0 )
{
$ error = 1 ;
}
2011-03-20 05:56:47 +00:00
$ firsttime = 0 ;
} # end - first time
2007-11-26 20:09:18 +00:00
$ ::minus_option = 0 ;
# for each node in memberlist add this group
# name to the groups attr
my % membhash ;
2008-07-09 16:10:10 +00:00
foreach my $ n ( @ memberlist )
2007-11-26 20:09:18 +00:00
{
$ membhash { $ n } { groups } = $ obj ;
$ membhash { $ n } { objtype } = 'node' ;
}
$ ::plus_option = 1 ;
$ ::minus_option = 0 ;
if ( xCAT::DBobjUtils - > setobjdefs ( \ % membhash ) != 0 )
{
$ error = 1 ;
}
$ ::plus_option = 0 ;
}
} # end - if memberlist
} # end - if group is defined
} # end - if group type
2013-04-08 07:03:24 +00:00
if ( $ type eq 'osimage' && ! $ ::opt_m && ! $ ::opt_p && $ ::opt_setattr && exists ( $ attrhash { $ obj } ) ) {
foreach my $ tmp_attr ( @ img_attrs ) {
if ( ! exists ( $ ::FINALATTRS { $ obj } { $ tmp_attr } ) && exists ( $ attrhash { $ obj } { $ tmp_attr } ) &&
defined ( $ attrhash { $ obj } { $ tmp_attr } ) ) {
$ ::FINALATTRS { $ obj } { $ tmp_attr } = $ attrhash { $ obj } { $ tmp_attr } ;
}
}
my $ rc = & parse_attr_for_osimage ( $ ::command , $ ::FINALATTRS { $ obj } ) ;
if ( $ rc ) {
next ;
}
}
2007-11-26 20:09:18 +00:00
2011-12-27 06:12:19 +00:00
# Removed the code to handle the nodegroup table with chdef -t node groups=xxx
# Only dynamic groups should be in nodegroup table
# Do not try to add static group into the nodegroup table
# performance!!!!
2007-11-26 20:09:18 +00:00
2009-08-06 01:33:14 +00:00
#special case for osimage, if the osimage was not defined,
#chdef can not create it correctly if no attribute in osimage table is defined
#set the default imagetype 'NIM' if it is not specified
if ( ( ! $ isDefined ) && ( $ type eq 'osimage' ) && ( ! defined ( $ ::FINALATTRS { $ obj } { imagetype } ) ) )
{
$ ::FINALATTRS { $ obj } { imagetype } = 'NIM' ;
}
2008-10-12 17:43:32 +00:00
2007-11-26 20:09:18 +00:00
} # end - for each object to update
#
# write each object into the tables in the xCAT database
#
# set update option
$ ::plus_option = 0 ;
$ ::minus_option = 0 ;
if ( $ ::opt_p )
{
$ ::plus_option = 1 ;
}
elsif ( $ ::opt_m )
{
$ ::minus_option = 1 ;
}
if ( xCAT::DBobjUtils - > setobjdefs ( \ % ::FINALATTRS ) != 0 )
{
my $ rsp ;
2009-10-12 07:41:52 +00:00
$ rsp - > { data } - > [ 0 ] = "Could not write data to the xCAT database." ;
2007-11-26 20:09:18 +00:00
2011-03-20 05:56:47 +00:00
# xCAT::MsgUtils->message("E", $rsp, $::callback);
2007-11-26 20:09:18 +00:00
$ error = 1 ;
}
2010-03-01 10:11:13 +00:00
2007-11-26 20:09:18 +00:00
if ( $ error )
{
my $ rsp ;
$ rsp - > { data } - > [ 0 ] =
2009-10-12 07:41:52 +00:00
"One or more errors occured when attempting to create or modify xCAT \nobject definitions." ;
2007-11-26 20:09:18 +00:00
xCAT::MsgUtils - > message ( "E" , $ rsp , $ ::callback ) ;
return 1 ;
}
else
{
if ( $ ::verbose )
{
# give results
my $ rsp ;
$ rsp - > { data } - > [ 0 ] =
2009-10-12 07:41:52 +00:00
"The database was updated for the following objects:" ;
2007-11-26 20:09:18 +00:00
xCAT::MsgUtils - > message ( "I" , $ rsp , $ ::callback ) ;
my $ n = 1 ;
foreach my $ o ( sort ( keys % ::FINALATTRS ) )
{
$ rsp - > { data } - > [ $ n ] = "$o\n" ;
$ n + + ;
}
xCAT::MsgUtils - > message ( "I" , $ rsp , $ ::callback ) ;
}
else
{
my $ rsp ;
2011-10-09 01:39:37 +00:00
my $ nodenum = scalar ( keys % ::FINALATTRS ) ;
2007-11-26 20:09:18 +00:00
$ rsp - > { data } - > [ 0 ] =
2011-10-09 01:39:37 +00:00
"$nodenum object definitions have been created or modified." ;
2007-11-26 20:09:18 +00:00
xCAT::MsgUtils - > message ( "I" , $ rsp , $ ::callback ) ;
2009-10-12 07:41:52 +00:00
if ( scalar ( keys % newobjects ) > 0 )
{
2010-03-24 14:51:26 +00:00
my $ newobj = ( ) ;
my $ invalidnodename = ( ) ;
foreach my $ node ( keys % newobjects ) {
2010-05-28 09:50:15 +00:00
if ( ( $ node =~ /[A-Z]/ ) && ( ( ! $ ::opt_t ) || ( $ ::opt_t eq "node" ) ) ) {
2010-03-24 14:51:26 +00:00
$ invalidnodename . = ",$node" ;
}
$ newobj . = ",$node" ;
}
if ( $ newobj ) {
$ newobj =~ s/,// ;
my $ rsp ;
$ rsp - > { data } - > [ 0 ] = "New object definitions \'$newobj\' have been created." ;
xCAT::MsgUtils - > message ( "I" , $ rsp , $ ::callback ) ;
}
if ( $ invalidnodename ) {
$ invalidnodename =~ s/,// ;
my $ rsp ;
2010-03-30 04:13:18 +00:00
$ rsp - > { data } - > [ 0 ] = "The node name \'$invalidnodename\' contains capital letters which may not be resolved correctly by the dns server." ;
2010-03-24 14:51:26 +00:00
xCAT::MsgUtils - > message ( "W" , $ rsp , $ ::callback ) ;
}
2009-10-12 07:41:52 +00:00
}
2010-03-24 14:51:26 +00:00
}
2007-11-26 20:09:18 +00:00
return 0 ;
}
2007-10-26 22:44:33 +00:00
}
#----------------------------------------------------------------------------
= head3 setFINALattrs
2011-03-20 05:56:47 +00:00
create % ::FINALATTRS { objname } { attr } = val hash
conbines % ::FILEATTRS , and % ::CLIATTR
2007-10-26 22:44:33 +00:00
Arguments:
Returns:
0 - OK
1 - error
Globals:
Error:
Example:
Comments:
= cut
#-----------------------------------------------------------------------------
sub setFINALattrs
{
2007-11-26 20:09:18 +00:00
my $ error = 0 ;
2013-04-16 02:48:38 +00:00
# set the final hash based on the info from the file hash and cmd line hash
@ ::finalTypeList = ( ) ;
2007-10-26 22:44:33 +00:00
# set the final hash based on the info from the input file
if ( @ ::fileobjnames )
{
foreach my $ objname ( @ ::fileobjnames )
{
# check if this object is one of the type specified
2007-11-26 20:09:18 +00:00
if ( @ ::clobtypes )
2007-10-26 22:44:33 +00:00
{
2009-07-21 19:36:14 +00:00
if ( ! grep ( /^$::FILEATTRS{$objname}{objtype}$/ , @ ::clobtypes ) )
2007-11-26 20:09:18 +00:00
{
next ;
}
}
# get the data type definition from Schema.pm
2009-01-29 18:42:59 +00:00
2011-03-20 05:56:47 +00:00
if ( ! $ ::FILEATTRS { $ objname } { objtype } ) {
my $ rsp ;
$ rsp - > { data } - > [ 0 ] = "\nNo objtype value was specified for \'$objname\'. Cannot create object definition." ;
xCAT::MsgUtils - > message ( "E" , $ rsp , $ ::callback ) ;
$ error = 1 ;
next ;
}
2009-01-29 18:42:59 +00:00
2013-03-21 06:41:01 +00:00
# special case for the nic* attributes
# merge nic*.eth0, nic*.eth1
if ( $ ::FILEATTRS { $ objname } { objtype } eq 'node' )
{
xCAT::DBobjUtils - > collapsenicsattr ( $ ::FILEATTRS { $ objname } , $ objname ) ;
}
2007-11-26 20:09:18 +00:00
my $ datatype =
$ xCAT:: Schema:: defspec { $ ::FILEATTRS { $ objname } { objtype } } ;
my @ list ;
foreach my $ this_attr ( sort @ { $ datatype - > { 'attrs' } } )
{
my $ a = $ this_attr - > { attr_name } ;
push ( @ list , $ a ) ;
}
push ( @ list , "objtype" ) ;
2007-10-26 22:44:33 +00:00
2007-11-26 20:09:18 +00:00
# if so then add it to the final hash
foreach my $ attr ( keys % { $ ::FILEATTRS { $ objname } } )
{
# see if valid attr
2011-03-20 05:56:47 +00:00
if ( ! grep ( /^$attr$/ , @ list ) && ( $ ::FILEATTRS { $ objname } { objtype } ne 'site' ) && ( $ ::FILEATTRS { $ objname } { objtype } ne 'monitoring' ) )
2007-11-26 20:09:18 +00:00
{
my $ rsp ;
$ rsp - > { data } - > [ 0 ] =
2012-12-06 13:30:55 +00:00
"\'$attr\' is not a valid attribute name for an object type of \'$::objtype\'." ;
2007-11-26 20:09:18 +00:00
xCAT::MsgUtils - > message ( "E" , $ rsp , $ ::callback ) ;
$ error = 1 ;
next ;
}
else
2007-10-26 22:44:33 +00:00
{
$ ::FINALATTRS { $ objname } { $ attr } =
$ ::FILEATTRS { $ objname } { $ attr } ;
2013-04-16 02:48:38 +00:00
if ( $ attr eq 'objtype' )
{
if ( ! grep ( /^$::FINALATTRS{$objname}{objtype}$/ , @ ::finalTypeList ) )
{
my $ type = $ ::FINALATTRS { $ objname } { objtype } ;
chomp $ type ;
push @ ::finalTypeList , $ type ;
}
}
2007-10-26 22:44:33 +00:00
}
2007-11-26 20:09:18 +00:00
}
2011-03-20 05:56:47 +00:00
# need to make sure the node attr is set otherwise nothing
# gets set in the nodelist table
if ( $ ::FINALATTRS { $ objname } { objtype } eq "node" ) {
$ ::FINALATTRS { $ objname } { node } = $ objname ;
}
2007-10-26 22:44:33 +00:00
}
}
foreach my $ objname ( @ ::clobjnames )
{
2013-03-21 06:41:01 +00:00
# special case for the nic* attributes
# merge nic*.eth0, nic*.eth1
if ( $ ::CLIATTRS { $ objname } { objtype } eq 'node' )
{
# Even if only the nicips.eth0 is specified with CLI,
# need to read the whole nicips attribute from the nics table,
# then merge the nicips.eth0 into the nicips attribute,
my % tmphash = ( ) ;
foreach my $ nodeattr ( keys % { $ ::CLIATTRS { $ objname } } )
{
if ( $ nodeattr =~ /^(nic\w+)\.\w+$/ )
{
my $ tmpnicattr = $ 1 ;
if ( ! defined ( $ tmphash { $ tmpnicattr } ) )
{
my $ nicstable = xCAT::Table - > new ( "nics" , - create = > 1 , - autocommit = > 0 ) ;
if ( ! $ nicstable ) {
my $ rsp ;
$ rsp - > { data } - > [ 0 ] = "Could not open the \'nics\' table." ;
xCAT::MsgUtils - > message ( "E" , $ rsp , $ ::callback ) ;
return 1 ;
}
my $ nichash = $ nicstable - > getNodeAttribs ( $ objname , [ $ tmpnicattr ] ) ;
if ( $ nichash && $ nichash - > { $ tmpnicattr } )
{
$ tmphash { $ tmpnicattr } = $ nichash - > { $ tmpnicattr } ;
}
$ nicstable - > close ( ) ;
}
}
}
# $tmphash{nicips} = "eth0!1.1.1.1|1.2.1.1,eth1!2.1.1.1|2.2.1.1"
foreach my $ nicattr ( keys % tmphash )
{
# eth0!1.1.1.1|1.2.1.1,eth1!2.1.1.1|2.2.1.1
my $ nicval = $ tmphash { $ nicattr } ;
my @ nicarray = split ( /,/ , $ nicval ) ;
foreach my $ nicv ( @ nicarray )
{
my @ nica = split ( /!/ , $ nicv ) ;
# put the additional nicips.eth1, nicips.eth2 into %::CLIATTRS
if ( ! defined $ ::CLIATTRS { $ objname } { "$nicattr.$nica[0]" } )
{
$ ::CLIATTRS { $ objname } { "$nicattr.$nica[0]" } = $ nica [ 1 ] ;
}
}
}
xCAT::DBobjUtils - > collapsenicsattr ( $ ::CLIATTRS { $ objname } , $ objname ) ;
}
2007-10-26 22:44:33 +00:00
foreach my $ attr ( keys % { $ ::CLIATTRS { $ objname } } )
{
2007-11-26 20:09:18 +00:00
2007-10-26 22:44:33 +00:00
$ ::FINALATTRS { $ objname } { $ attr } = $ ::CLIATTRS { $ objname } { $ attr } ;
2007-11-26 20:09:18 +00:00
if ( $ attr eq 'objtype' )
{
if (
2009-07-21 19:36:14 +00:00
! grep ( /^$::FINALATTRS{$objname}{objtype}$/ , @ ::finalTypeList )
2007-11-26 20:09:18 +00:00
)
{
my $ type = $ ::FINALATTRS { $ objname } { objtype } ;
chomp $ type ;
push @ ::finalTypeList , $ type ;
}
}
2008-06-07 14:12:13 +00:00
}
2011-03-20 05:56:47 +00:00
# need to make sure the node attr is set otherwise nothing
# gets set in the nodelist table
if ( $ ::FINALATTRS { $ objname } { objtype } eq "node" ) {
2008-06-07 14:12:13 +00:00
$ ::FINALATTRS { $ objname } { node } = $ objname ;
2007-10-26 22:44:33 +00:00
}
}
2007-11-26 20:09:18 +00:00
if ( $ error )
{
return 1 ;
}
else
{
return 0 ;
}
2007-10-26 22:44:33 +00:00
}
#----------------------------------------------------------------------------
2007-11-26 20:09:18 +00:00
= head3 defls
2007-10-26 22:44:33 +00:00
2007-11-26 20:09:18 +00:00
Support for the xCAT defls command .
2007-10-26 22:44:33 +00:00
Arguments:
Returns:
0 - OK
1 - error
Globals:
2007-11-26 20:09:18 +00:00
2007-10-26 22:44:33 +00:00
Error:
Example:
Comments:
2011-03-20 05:56:47 +00:00
Object names derived from - o , - t , w , - a or noderange !
2007-11-26 20:09:18 +00:00
List of attrs to display is given by - i .
Output goes to standard out or a stanza / xml file ( - z or - x )
2008-02-21 21:10:35 +00:00
2007-10-26 22:44:33 +00:00
= cut
#-----------------------------------------------------------------------------
2007-11-26 20:09:18 +00:00
sub defls
2007-10-26 22:44:33 +00:00
{
2007-11-26 20:09:18 +00:00
my $ long = 0 ;
my % myhash ;
my % objhash ;
my @ objectlist ;
@ ::allobjnames ;
2010-01-07 14:19:57 +00:00
my @ displayObjList ;
2007-11-26 20:09:18 +00:00
my $ numtypes = 0 ;
2010-01-07 14:19:57 +00:00
my $ rsp_info ;
2007-10-26 22:44:33 +00:00
# process the command line
my $ rc = & processArgs ;
2011-03-18 14:58:45 +00:00
2007-10-26 22:44:33 +00:00
if ( $ rc != 0 )
{
2011-03-18 14:58:45 +00:00
2007-11-26 20:09:18 +00:00
# rc: 0 - ok, 1 - return, 2 - help, 3 - error
2011-03-20 05:56:47 +00:00
# 0 - continue
# 1 - return (like for version option)
# 2 - return with usage
# 3 - return error
if ( $ rc == 1 ) {
return 0 ;
} elsif ( $ rc == 2 ) {
& defls_usage ;
return 0 ;
} elsif ( $ rc == 3 ) {
return 1 ;
}
}
2011-03-18 14:58:45 +00:00
2007-10-26 22:44:33 +00:00
2007-11-26 20:09:18 +00:00
# do we want just the object names or all the attr=val
2010-01-07 14:19:57 +00:00
if ( $ ::opt_l || @ ::noderange || $ ::opt_o || $ ::opt_i )
2007-11-26 20:09:18 +00:00
{
# assume we want the the details - not just the names
2011-03-20 05:56:47 +00:00
# - if provided object names or noderange
2007-11-26 20:09:18 +00:00
$ long + + ;
}
2010-01-07 14:19:57 +00:00
if ( $ ::opt_s ) {
$ long = 0 ;
}
2008-04-15 16:48:53 +00:00
# which attrs do we want?
# this is a temp hack to help scaling when you only
# want a list of nodes - needs to be fully implemented
if ( $ ::opt_l || $ ::opt_w ) {
# if long or -w then get all the attrs
$ ::ATTRLIST = "all" ;
} elsif ( $ ::opt_i ) {
# is -i then just get the ones in the list
$ ::ATTRLIST = $ ::opt_i ;
} elsif ( @ ::noderange || $ ::opt_o ) {
# if they gave a list of objects then they must want more
2010-01-07 14:19:57 +00:00
# than the object names!
if ( $ ::opt_s ) {
$ ::ATTRLIST = "none" ;
} else {
$ ::ATTRLIST = "all" ;
}
2008-04-15 16:48:53 +00:00
} else {
# otherwise just get a list of object names
$ ::ATTRLIST = "none" ;
}
2007-11-26 20:09:18 +00:00
2007-10-26 22:44:33 +00:00
#
2011-03-20 05:56:47 +00:00
# put together a hash with the list of objects and the associated types
# - need to figure out which objects to look up
2007-10-26 22:44:33 +00:00
#
2007-11-26 20:09:18 +00:00
# if a set of objects was provided on the cmd line then there can
2011-03-20 05:56:47 +00:00
# be only one type value
2010-01-07 14:19:57 +00:00
# Figure out the attributes that needed in the def operation
my @ neededattrs = ( ) ;
if ( $ ::opt_i ) {
@ neededattrs = ( @ neededattrs , @ ::AttrList ) ;
if ( $ ::opt_w ) {
my @ whereattrs = keys % ::WhereHash ;
@ neededattrs = ( @ neededattrs , @ whereattrs ) ;
}
}
2007-11-26 20:09:18 +00:00
if ( $ ::objectsfrom_opto || $ ::objectsfrom_nr || $ ::objectsfrom_args )
2007-10-26 22:44:33 +00:00
{
2007-11-26 20:09:18 +00:00
my $ type = @ ::clobjtypes [ 0 ] ;
$ numtypes = 1 ;
foreach my $ obj ( sort @ ::clobjnames )
{
$ objhash { $ obj } = $ type ;
}
2010-01-07 14:19:57 +00:00
% myhash = xCAT::DBobjUtils - > getobjdefs ( \ % objhash , $ ::VERBOSE , \ @ neededattrs ) ;
2012-11-30 09:08:47 +00:00
if ( ! ( % myhash ) )
2007-10-26 22:44:33 +00:00
{
2007-11-26 20:09:18 +00:00
my $ rsp ;
2009-10-12 07:41:52 +00:00
$ rsp - > { data } - > [ 0 ] = "Could not get xCAT object definitions." ;
2007-11-26 20:09:18 +00:00
xCAT::MsgUtils - > message ( "E" , $ rsp , $ ::callback ) ;
2007-10-26 22:44:33 +00:00
return 1 ;
2007-11-26 20:09:18 +00:00
2007-10-26 22:44:33 +00:00
}
}
2007-11-26 20:09:18 +00:00
# if just provided type list then find all objects of these types
if ( $ ::objectsfrom_optt )
2007-10-26 22:44:33 +00:00
{
2007-11-26 20:09:18 +00:00
% objhash = % ::ObjTypeHash ;
2010-01-07 14:19:57 +00:00
% myhash = xCAT::DBobjUtils - > getobjdefs ( \ % objhash , $ ::VERBOSE , \ @ neededattrs ) ;
2012-11-30 09:08:47 +00:00
if ( ! ( % myhash ) )
2007-11-26 20:09:18 +00:00
{
my $ rsp ;
2009-10-12 07:41:52 +00:00
$ rsp - > { data } - > [ 0 ] = "Could not get xCAT object definitions." ;
2007-11-26 20:09:18 +00:00
xCAT::MsgUtils - > message ( "E" , $ rsp , $ ::callback ) ;
return 1 ;
}
2007-10-26 22:44:33 +00:00
}
2007-11-26 20:09:18 +00:00
# if specify all
if ( $ ::opt_a )
2007-10-26 22:44:33 +00:00
{
2007-11-26 20:09:18 +00:00
# could be modified by type
if ( $ ::opt_t )
{
# get all objects matching type list
# Get all object in this type list
foreach my $ t ( @ ::clobjtypes )
{
2008-07-09 16:10:10 +00:00
my @ tmplist = xCAT::DBobjUtils - > getObjectsOfType ( $ t ) ;
2007-11-26 20:09:18 +00:00
if ( scalar ( @ tmplist ) > 1 )
{
foreach my $ obj ( @ tmplist )
{
$ objhash { $ obj } = $ t ;
}
}
else
{
my $ rsp ;
$ rsp - > { data } - > [ 0 ] =
2009-10-12 07:41:52 +00:00
"Could not get objects of type \'$t\'." ;
2007-11-26 20:09:18 +00:00
xCAT::MsgUtils - > message ( "I" , $ rsp , $ ::callback ) ;
}
}
% myhash = xCAT::DBobjUtils - > getobjdefs ( \ % objhash ) ;
2012-11-30 09:08:47 +00:00
if ( ! ( % myhash ) )
2007-11-26 20:09:18 +00:00
{
my $ rsp ;
2009-10-12 07:41:52 +00:00
$ rsp - > { data } - > [ 0 ] = "Could not get xCAT object definitions." ;
2007-11-26 20:09:18 +00:00
xCAT::MsgUtils - > message ( "E" , $ rsp , $ ::callback ) ;
return 1 ;
}
}
else
{
2009-03-22 19:15:19 +00:00
% myhash = xCAT::DBobjUtils - > getobjdefs ( \ % ::AllObjTypeHash , $ ::VERBOSE ) ;
2012-11-30 09:08:47 +00:00
if ( ! ( % myhash ) )
2007-11-26 20:09:18 +00:00
{
my $ rsp ;
2009-10-12 07:41:52 +00:00
$ rsp - > { data } - > [ 0 ] = "Could not get xCAT object definitions." ;
2007-11-26 20:09:18 +00:00
xCAT::MsgUtils - > message ( "E" , $ rsp , $ ::callback ) ;
return 1 ;
}
}
foreach my $ t ( keys % { xCAT::Schema:: defspec } )
{
push ( @ ::clobjtypes , $ t ) ;
}
2008-02-28 19:05:31 +00:00
} # end - if specify all
2007-11-26 20:09:18 +00:00
2012-11-30 09:08:47 +00:00
if ( ! ( % myhash ) )
2007-11-26 20:09:18 +00:00
{
my $ rsp ;
2009-10-12 07:41:52 +00:00
$ rsp - > { data } - > [ 0 ] = "Could not find any objects to display." ;
2007-11-26 20:09:18 +00:00
xCAT::MsgUtils - > message ( "I" , $ rsp , $ ::callback ) ;
return 0 ;
}
2007-10-26 22:44:33 +00:00
2009-08-07 04:26:25 +00:00
# need a special case for the node postscripts attribute,
2009-12-18 08:40:54 +00:00
# The 'xcatdefaults' postscript should be added to the postscripts and postbootscripts attribute
2009-08-07 04:26:25 +00:00
my $ getnodes = 0 ;
2009-08-07 04:34:10 +00:00
if ( ! $ ::opt_z ) { #if -z flag is specified, do not add the xcatdefaults
foreach my $ objtype ( @ ::clobjtypes )
2009-08-07 04:26:25 +00:00
{
2009-08-07 04:34:10 +00:00
if ( $ objtype eq 'node' )
{
$ getnodes = 1 ;
last ;
}
2009-08-07 04:26:25 +00:00
}
}
2011-06-07 06:51:28 +00:00
my % nodeosimagehash = ( ) ;
2009-08-07 04:26:25 +00:00
if ( $ getnodes )
{
2011-06-07 06:51:28 +00:00
# Show osimage information
if ( $ ::opt_osimg )
{
my % nodeosimgname ;
my % imghash ;
my % imglist ;
2012-12-29 08:33:14 +00:00
my % tmpprofilelist ;
# get the site.installdir for osimage searching with nodes prvomethod= install/netboot/statelite
# it might not be used at all, but should not do this for each node
my $ installroot = "/install" ;
my @ ents = xCAT::TableUtils - > get_site_attribute ( "installdir" ) ;
my $ site_ent = $ ents [ 0 ] ;
if ( defined ( $ site_ent ) )
{
$ installroot = $ site_ent ;
}
2011-06-07 06:51:28 +00:00
foreach my $ obj ( keys % myhash )
{
if ( $ myhash { $ obj } { 'objtype' } eq 'node' )
{
my $ osimagename ;
#provmethod can be set to osimage name
if ( $ myhash { $ obj } { 'provmethod' } && ( $ myhash { $ obj } { 'provmethod' } ne 'install' )
&& ( $ myhash { $ obj } { 'provmethod' } ne 'netboot' ) && ( $ myhash { $ obj } { 'provmethod' } ne 'statelite' ) )
{
$ osimagename = $ myhash { $ obj } { 'provmethod' } ;
}
else
{
2012-12-29 08:33:14 +00:00
# prvomethod = install/netboot/statelite,
# search /opt/xcat/share/xcat/<provmethod/<platform>
# and /install/custom/<provmethod/<platform>
my $ profile = $ myhash { $ obj } { 'profile' } ;
my $ os = $ myhash { $ obj } { 'os' } ;
my $ arch = $ myhash { $ obj } { 'arch' } ;
my $ provmethod = $ myhash { $ obj } { 'provmethod' } ;
# tmp hash for performance considerations,
# do not search paths for each node.
if ( defined ( $ tmpprofilelist { $ os } { $ arch } { $ provmethod } { $ profile } ) )
{
$ nodeosimagehash { $ obj } { 'template' } = $ tmpprofilelist { $ os } { $ arch } { $ provmethod } { $ profile } { 'template' } ;
$ nodeosimagehash { $ obj } { 'pkglist' } = $ tmpprofilelist { $ os } { $ arch } { $ provmethod } { $ profile } { 'pkglist' } ;
$ nodeosimagehash { $ obj } { 'otherpkglist' } = $ tmpprofilelist { $ os } { $ arch } { $ provmethod } { $ profile } { 'otherpkglist' } ;
$ nodeosimagehash { $ obj } { 'postinstall' } = $ tmpprofilelist { $ os } { $ arch } { $ provmethod } { $ profile } { 'postinstall' } ;
$ nodeosimagehash { $ obj } { 'extlist' } = $ tmpprofilelist { $ os } { $ arch } { $ provmethod } { $ profile } { 'extlist' } ;
$ nodeosimagehash { $ obj } { 'synclists' } = $ tmpprofilelist { $ os } { $ arch } { $ provmethod } { $ profile } { 'synclists' } ;
$ nodeosimagehash { $ obj } { 'pkgdir' } = $ tmpprofilelist { $ os } { $ arch } { $ provmethod } { $ profile } { 'pkgdir' } ;
$ nodeosimagehash { $ obj } { 'otherpkgdir' } = $ tmpprofilelist { $ os } { $ arch } { $ provmethod } { $ profile } { 'otherpkgdir' } ;
$ nodeosimagehash { $ obj } { 'rootimgdir' } = $ tmpprofilelist { $ os } { $ arch } { $ provmethod } { $ profile } { 'rootimgdir' } ;
$ nodeosimagehash { $ obj } { 'osvers' } = $ tmpprofilelist { $ os } { $ arch } { $ provmethod } { $ profile } { 'osvers' } ;
$ nodeosimagehash { $ obj } { 'osarch' } = $ tmpprofilelist { $ os } { $ arch } { $ provmethod } { $ profile } { 'osarch' } ;
$ nodeosimagehash { $ obj } { 'imagetype' } = $ tmpprofilelist { $ os } { $ arch } { $ provmethod } { $ profile } { 'imagetype' } ;
$ nodeosimagehash { $ obj } { 'osname' } = $ tmpprofilelist { $ os } { $ arch } { $ provmethod } { $ profile } { 'osname' } ;
$ nodeosimagehash { $ obj } { 'profile' } = $ tmpprofilelist { $ os } { $ arch } { $ provmethod } { $ profile } { 'profile' } ;
$ nodeosimagehash { $ obj } { 'provmethod' } = $ tmpprofilelist { $ os } { $ arch } { $ provmethod } { $ profile } { 'provmethod' } ;
next ;
}
2011-06-07 06:51:28 +00:00
if ( $ myhash { $ obj } { 'os' } && $ myhash { $ obj } { 'arch' }
&& $ myhash { $ obj } { 'provmethod' } && $ myhash { $ obj } { 'profile' } )
{
2012-12-29 08:33:14 +00:00
#$osimagename = "$myhash{$obj}{'os'}-$myhash{$obj}{'arch'}-$myhash{$obj}{'provmethod'}-$myhash{$obj}{'profile'}";
my $ platform = xCAT::SvrUtils - > getplatform ( $ myhash { $ obj } { 'os' } ) ;
my $ pm = $ myhash { $ obj } { 'provmethod' } ;
if ( $ pm eq 'statelite' ) { $ pm = 'netboot' ; }
my $ custpath = "$installroot/custom/$pm/$platform" ;
my $ defpath = "$::XCATROOT/share/xcat/$pm/$platform" ;
$ nodeosimagehash { $ obj } { 'osvers' } = $ os ;
$ tmpprofilelist { $ os } { $ arch } { $ provmethod } { $ profile } { 'osvers' } = $ os ;
$ nodeosimagehash { $ obj } { 'osarch' } = $ arch ;
$ tmpprofilelist { $ os } { $ arch } { $ provmethod } { $ profile } { 'osarch' } = $ arch ;
$ nodeosimagehash { $ obj } { 'imagetype' } = "linux" ;
$ tmpprofilelist { $ os } { $ arch } { $ provmethod } { $ profile } { 'imagetype' } = "linux" ;
$ nodeosimagehash { $ obj } { 'osname' } = "Linux" ;
$ tmpprofilelist { $ os } { $ arch } { $ provmethod } { $ profile } { 'osname' } = "Linux" ;
$ nodeosimagehash { $ obj } { 'profile' } = $ profile ;
$ tmpprofilelist { $ os } { $ arch } { $ provmethod } { $ profile } { 'profile' } = $ profile ;
$ nodeosimagehash { $ obj } { 'provmethod' } = $ provmethod ;
$ tmpprofilelist { $ os } { $ arch } { $ provmethod } { $ profile } { 'provmethod' } = $ provmethod ;
# pkgdir both diskful and diskless
$ nodeosimagehash { $ obj } { 'pkgdir' } = "$installroot/$os/$arch" ;
$ tmpprofilelist { $ os } { $ arch } { $ provmethod } { $ profile } { 'pkgdir' } = "$installroot/$os/$arch" ;
# rootimgdir only for diskless
if ( ( $ provmethod eq 'netboot' ) || ( $ provmethod eq 'statelite' ) )
{
$ nodeosimagehash { $ obj } { 'rootimgdir' } = "$installroot/netboot/$os/$arch/$profile" ;
$ tmpprofilelist { $ os } { $ arch } { $ provmethod } { $ profile } { 'rootimgdir' } = "$installroot/netboot/$os/$arch/$profile" ;
}
# otherpkgdir for both diskful and diskless
$ nodeosimagehash { $ obj } { 'otherpkgdir' } = "$installroot/post/otherpkgs/$os/$arch" ;
$ tmpprofilelist { $ os } { $ arch } { $ provmethod } { $ profile } { 'otherpkgdir' } = "$installroot/post/otherpkgs/$os/$arch" ;
# template file only for diskful
if ( $ provmethod eq 'install' )
{
my $ tmplfile = xCAT::SvrUtils - > get_tmpl_file_name ( $ custpath , $ profile , $ os , $ arch , $ os ) ;
if ( ! $ tmplfile )
{
$ tmplfile = xCAT::SvrUtils - > get_tmpl_file_name ( $ defpath , $ profile , $ os , $ arch , $ os ) ;
}
if ( $ tmplfile )
{
$ nodeosimagehash { $ obj } { 'template' } = $ tmplfile ;
$ tmpprofilelist { $ os } { $ arch } { $ provmethod } { $ profile } { 'template' } = $ tmplfile ;
}
}
# pkglist for both diskful and diskless
my $ pkglistfile = xCAT::SvrUtils - > get_pkglist_file_name ( $ custpath , $ profile , $ os , $ arch , $ os ) ;
if ( ! $ pkglistfile )
{
$ pkglistfile = xCAT::SvrUtils - > get_pkglist_file_name ( $ defpath , $ profile , $ os , $ arch , $ os ) ;
}
if ( $ pkglistfile )
{
$ nodeosimagehash { $ obj } { 'pkglist' } = $ pkglistfile ;
$ tmpprofilelist { $ os } { $ arch } { $ provmethod } { $ profile } { 'pkglist' } = $ pkglistfile ;
}
#otherpkglist for both diskful and diskless
my $ otherpkgsfile = xCAT::SvrUtils - > get_otherpkgs_pkglist_file_name ( $ custpath , $ profile , $ os , $ arch ) ;
if ( ! $ otherpkgsfile )
{
$ otherpkgsfile = xCAT::SvrUtils - > get_otherpkgs_pkglist_file_name ( $ defpath , $ profile , $ os , $ arch ) ;
}
if ( $ otherpkgsfile )
{
$ nodeosimagehash { $ obj } { 'otherpkglist' } = $ otherpkgsfile ;
$ tmpprofilelist { $ os } { $ arch } { $ provmethod } { $ profile } { 'otherpkglist' } = $ otherpkgsfile ;
}
# postinstall and exlist only for diskless
if ( ( $ provmethod eq 'netboot' ) || ( $ provmethod eq 'statelite' ) )
{
# Get postinstall file
my $ postfile = xCAT::SvrUtils - > get_postinstall_file_name ( $ custpath , $ profile , $ os , $ arch ) ;
if ( ! $ postfile )
{
$ postfile = xCAT::SvrUtils - > get_postinstall_file_name ( $ defpath , $ profile , $ os , $ arch ) ;
}
if ( $ postfile )
{
$ nodeosimagehash { $ obj } { 'postinstall' } = $ postfile ;
$ tmpprofilelist { $ os } { $ arch } { $ provmethod } { $ profile } { 'postinstall' } = $ postfile ;
}
# Get exclude list
my $ extfile = xCAT::SvrUtils - > get_exlist_file_name ( $ custpath , $ profile , $ os , $ arch ) ;
if ( ! $ extfile )
{
$ extfile = xCAT::SvrUtils - > get_exlist_file_name ( $ defpath , $ profile , $ os , $ arch ) ;
}
if ( $ extfile )
{
$ nodeosimagehash { $ obj } { 'extlist' } = $ extfile ;
$ tmpprofilelist { $ os } { $ arch } { $ provmethod } { $ profile } { 'extlist' } = $ extfile ;
}
}
# Get synclist
my $ synclist = xCAT::SvrUtils - > getsynclistfile ( undef , $ os , $ arch , $ profile , $ provmethod ) ;
if ( $ synclist )
{
$ nodeosimagehash { $ obj } { 'synclists' } = $ synclist ;
$ tmpprofilelist { $ os } { $ arch } { $ provmethod } { $ profile } { 'synclists' } = $ synclist ;
}
}
else
{
my $ rsp ;
$ rsp - > { data } - > [ 0 ] =
"Missing attributes for node $obj, check the node attributes \'os\', \'arch\',\'profile\' and \'provmethod\'." ;
xCAT::MsgUtils - > message ( "E" , $ rsp , $ ::callback ) ;
next ;
2011-06-07 06:51:28 +00:00
}
}
# do not call xCAT::DBobjUtils->getobjdefs for each object
# for performance consideration
if ( $ osimagename )
{
if ( ! defined ( $ imglist { $ osimagename } ) )
{
$ imglist { $ osimagename } = 'osimage' ;
}
$ nodeosimgname { $ obj } = $ osimagename ;
}
}
}
# Get osimage definition info in one invocation
if ( scalar ( keys % imglist ) > 0 )
{
my @ attrs = ( ) ;
% imghash = xCAT::DBobjUtils - > getobjdefs ( \ % imglist , 0 , \ @ attrs ) ;
}
# Put the osimage definition in %nodeosimagehash
foreach my $ obj ( keys % myhash )
{
if ( $ myhash { $ obj } { 'objtype' } eq 'node' )
{
my $ imgname = $ nodeosimgname { $ obj } ;
if ( $ imgname && defined ( $ imghash { $ imgname } ) )
{
my % imgentry = % { $ imghash { $ imgname } } ;
foreach my $ imgattr ( keys % imgentry )
{
# Only store the attributes that are not in general node attributes
2013-09-18 05:46:00 +00:00
# This is not the right thing to do, list all the osimage attributes instead,
# like the postscripts and postbootscripts attributes,
#if(!defined($myhash{$obj}{$imgattr}) && defined($imgentry{$imgattr}))
#{
2011-06-07 06:51:28 +00:00
$ nodeosimagehash { $ obj } { $ imgattr } = $ imgentry { $ imgattr } ;
2013-09-18 05:46:00 +00:00
#}
2011-06-07 06:51:28 +00:00
}
}
2012-04-12 07:57:24 +00:00
if ( scalar ( keys % { $ nodeosimagehash { $ obj } } ) == 0 )
{
my $ rsp ;
$ rsp - > { data } - > [ 0 ] = "$obj: could not find information for osimage $nodeosimgname{$obj}" ;
xCAT::MsgUtils - > message ( "W" , $ rsp , $ ::callback ) ;
}
2011-06-07 06:51:28 +00:00
}
}
}
2009-08-07 04:26:25 +00:00
my $ xcatdefaultsps ;
2009-12-18 08:40:54 +00:00
my $ xcatdefaultspbs ;
2009-08-07 04:26:25 +00:00
my @ TableRowArray = xCAT::DBobjUtils - > getDBtable ( 'postscripts' ) ;
2012-11-30 09:08:47 +00:00
if ( @ TableRowArray )
2009-08-07 04:26:25 +00:00
{
foreach my $ tablerow ( @ TableRowArray )
{
if ( ( $ tablerow - > { node } eq 'xcatdefaults' ) && ! ( $ tablerow - > { disable } ) )
{
$ xcatdefaultsps = $ tablerow - > { postscripts } ;
2009-12-18 08:40:54 +00:00
$ xcatdefaultspbs = $ tablerow - > { postbootscripts } ;
2009-08-07 04:26:25 +00:00
last ;
}
}
}
foreach my $ obj ( keys % myhash )
{
if ( $ myhash { $ obj } { objtype } eq 'node' )
{
if ( $ xcatdefaultsps )
{
if ( $ myhash { $ obj } { postscripts } )
{
$ myhash { $ obj } { postscripts } = $ xcatdefaultsps . ',' . $ myhash { $ obj } { postscripts } ;
}
else
{
$ myhash { $ obj } { postscripts } = $ xcatdefaultsps ;
}
2010-12-03 09:16:46 +00:00
if ( $ ::opt_V && ( $ myhash { $ obj } { postscripts } eq $ xcatdefaultsps ) )
{
$ myhash { $ obj } { postscripts } . = " (Table:postscripts - Key:node - Column:postscripts)" ;
}
2009-08-07 04:26:25 +00:00
}
2009-12-18 08:40:54 +00:00
if ( $ xcatdefaultspbs )
{
if ( $ myhash { $ obj } { postbootscripts } )
{
$ myhash { $ obj } { postbootscripts } = $ xcatdefaultspbs . ',' . $ myhash { $ obj } { postbootscripts } ;
}
else
{
$ myhash { $ obj } { postbootscripts } = $ xcatdefaultspbs ;
}
2010-12-03 09:16:46 +00:00
if ( $ ::opt_V && ( $ myhash { $ obj } { postbootscripts } eq $ xcatdefaultspbs ) )
{
$ myhash { $ obj } { postbootscripts } . = " (Table:postscripts - Key:node - Column:postbootscripts)" ;
}
2009-12-18 08:40:54 +00:00
}
2009-08-07 04:26:25 +00:00
}
}
}
2007-11-26 20:09:18 +00:00
# the list of objects may be limited by the "-w" option
# see which objects have attr/val that match the where values
2011-03-20 05:56:47 +00:00
# - if provided
2007-11-26 20:09:18 +00:00
if ( $ ::opt_w )
{
foreach my $ obj ( sort ( keys % myhash ) )
2007-10-26 22:44:33 +00:00
{
2009-06-30 09:47:48 +00:00
if ( xCAT::Utils - > selection_string_match ( \ % myhash , $ obj , \ % ::WhereHash ) ) {
2007-11-26 20:09:18 +00:00
push ( @ displayObjList , $ obj ) ;
}
2007-10-26 22:44:33 +00:00
}
}
2007-11-26 20:09:18 +00:00
#
# output in specified format
#
my @ foundobjlist ;
if ( $ ::opt_z )
{
2010-01-07 14:19:57 +00:00
push ( @ { $ rsp_info - > { data } } , "# <xCAT data object stanza file>" ) ;
2007-11-26 20:09:18 +00:00
}
# group the objects by type to make the output easier to read
my $ numobjects = 0 ; # keep track of how many object we want to display
2011-03-20 05:56:47 +00:00
# for each type
2007-11-26 20:09:18 +00:00
foreach my $ type ( @ ::clobjtypes )
2007-10-26 22:44:33 +00:00
{
2012-12-06 13:30:55 +00:00
# Check if -i specifies valid attributes
# get the data type definition from Schema.pm
my % validattrslist ;
if ( $ ::opt_i )
{
my $ datatype = $ xCAT:: Schema:: defspec { $ type } ;
foreach my $ this_attr ( sort @ { $ datatype - > { 'attrs' } } )
{
my $ a = $ this_attr - > { attr_name } ;
$ validattrslist { $ a } = 1 ;
}
}
2007-10-26 22:44:33 +00:00
2007-11-26 20:09:18 +00:00
my % defhash ;
foreach my $ obj ( keys % myhash )
{
if ( $ obj )
{
$ numobjects + + ;
if ( $ myhash { $ obj } { 'objtype' } eq $ type )
{
$ defhash { $ obj } = $ myhash { $ obj } ;
}
}
}
if ( $ numobjects == 0 )
{
my $ rsp ;
$ rsp - > { data } - > [ 0 ] =
2009-10-12 07:41:52 +00:00
"Could not find any object definitions to display." ;
2007-11-26 20:09:18 +00:00
xCAT::MsgUtils - > message ( "I" , $ rsp , $ ::callback ) ;
return 0 ;
}
2007-10-26 22:44:33 +00:00
2011-03-20 05:56:47 +00:00
if ( $ type eq "node" ) {
my % newhash ;
my $ listtab = xCAT::Table - > new ( 'nodelist' ) ;
2011-04-12 06:32:58 +00:00
if ( ! $ listtab ) {
my $ rsp ;
$ rsp - > { data } - > [ 0 ] =
"Could not open nodelist table." ;
xCAT::MsgUtils - > message ( "E" , $ rsp , $ ::callback ) ;
return 1 ;
}
if ( ! defined ( $ ::opt_S ) ) {
2011-03-20 05:56:47 +00:00
#my $tmp1=$listtab->getAllEntries("all");
#if (defined($tmp1) && (@$tmp1 > 0)) {
# foreach(@$tmp1) {
# $newhash{$_->{node}} = 1;
# }
#}
foreach my $ n ( keys % defhash ) {
#if ($newhash{$n} eq 1) {
my ( $ hidhash ) = $ listtab - > getNodeAttribs ( $ n , [ 'hidden' ] ) ;
if ( $ hidhash ) {
if ( $ hidhash - > { hidden } eq 1 ) {
delete $ defhash { $ n } ;
}
}
#}
}
2011-04-12 06:32:58 +00:00
}
2011-03-20 05:56:47 +00:00
}
2010-01-07 14:19:57 +00:00
# Get all the objects of this type
my @ allobjoftype ;
2010-06-02 09:43:30 +00:00
@ allobjoftype = xCAT::DBobjUtils - > getObjectsOfType ( $ type ) ;
2010-01-07 14:19:57 +00:00
2010-06-02 09:43:30 +00:00
unless ( @ allobjoftype )
{
my $ rsp ;
$ rsp - > { data } - > [ 0 ] =
"Could not find any objects of type \'$type\'." ;
xCAT::MsgUtils - > message ( "I" , $ rsp , $ ::callback ) ;
next ;
}
2010-01-07 14:19:57 +00:00
my @ attrlist ;
if ( ( $ type ne 'site' ) && ( $ type ne 'monitoring' ) )
{
2012-12-06 13:30:55 +00:00
# -i is specified
2010-01-07 14:19:57 +00:00
if ( scalar ( @ ::AttrList ) > 0 ) {
2012-12-06 13:30:55 +00:00
foreach my $ attr ( @ ::AttrList )
{
# For site and monitoring, does not check if -i attributes are valid
if ( ( $ type eq 'site' ) || ( $ type eq 'monitoring' ) )
{
@ attrlist = @ ::AttrList ;
} else {
if ( defined ( $ validattrslist { $ attr } ) )
{
if ( ! grep ( /^$attr$/ , @ attrlist ) )
{
push @ attrlist , $ attr ;
}
} else {
my $ rsp ;
$ rsp - > { data } - > [ 0 ] =
"\'$attr\' is not a valid attribute name for an object type of \'$type\'." ;
xCAT::MsgUtils - > message ( "E" , $ rsp , $ ::callback ) ;
next ;
}
}
}
2010-01-07 14:19:57 +00:00
} else {
2012-12-06 13:30:55 +00:00
# get the list of all attrs for this type object
2010-01-07 14:19:57 +00:00
# get the data type definition from Schema.pm
my $ datatype =
$ xCAT:: Schema:: defspec { $ type } ;
foreach my $ this_attr ( @ { $ datatype - > { 'attrs' } } )
{
if ( ! grep ( /^$this_attr->{attr_name}$/ , @ attrlist ) ) {
push ( @ attrlist , $ this_attr - > { attr_name } ) ;
}
}
}
}
2009-12-30 07:50:28 +00:00
# for each object
2009-10-12 08:26:35 +00:00
foreach my $ obj ( sort keys % defhash )
2007-10-26 22:44:33 +00:00
{
2007-11-26 20:09:18 +00:00
unless ( $ obj )
{
next ;
}
2010-01-07 14:19:57 +00:00
# Return if this obj does not match the filter string
if ( $ ::opt_w )
2007-11-26 20:09:18 +00:00
{
2010-01-07 14:19:57 +00:00
# just display objects that match -w
if ( ! grep /^$obj$/ , @ displayObjList )
2007-11-26 20:09:18 +00:00
{
next ;
}
2010-01-07 14:19:57 +00:00
}
2007-11-26 20:09:18 +00:00
2010-06-02 09:43:30 +00:00
# check the object names only if
# the object names are passed in through command line
if ( $ ::objectsfrom_args || $ ::opt_o || ( ( $ type eq 'node' ) && ( $ ::opt_o || @ ::noderange ) ) )
2010-01-07 14:19:57 +00:00
{
2010-06-02 09:43:30 +00:00
if ( ! grep ( /^$obj$/ , @ allobjoftype ) )
2007-11-26 20:09:18 +00:00
{
2010-06-02 09:43:30 +00:00
my $ rsp ;
$ rsp - > { data } - > [ 0 ] =
"Could not find an object named \'$obj\' of type \'$type\'." ;
xCAT::MsgUtils - > message ( "I" , $ rsp , $ ::callback ) ;
next ;
}
}
2007-11-26 20:09:18 +00:00
# special handling for site table - for now !!!!!!!
2010-01-07 14:19:57 +00:00
if ( ( $ type eq 'site' ) || ( $ type eq 'monitoring' ) )
2007-11-26 20:09:18 +00:00
{
foreach my $ a ( keys % { $ defhash { $ obj } } )
{
push ( @ attrlist , $ a ) ;
}
2007-10-26 22:44:33 +00:00
}
2007-11-26 20:09:18 +00:00
if ( $ ::opt_x )
2007-10-26 22:44:33 +00:00
{
2008-02-28 19:05:31 +00:00
# TBD - do output in XML format
2007-10-26 22:44:33 +00:00
}
else
{
2010-01-07 14:19:57 +00:00
# display all data
# do we want the short or long output?
if ( $ long )
2007-10-26 22:44:33 +00:00
{
2010-01-07 14:19:57 +00:00
if ( $ ::opt_z )
{
push ( @ { $ rsp_info - > { data } } , "\n$obj:" ) ;
push ( @ { $ rsp_info - > { data } } , " objtype=$defhash{$obj}{'objtype'}" ) ;
}
else
{
if ( $# ::clobjtypes > 0 )
{
2010-12-19 13:37:20 +00:00
push ( @ { $ rsp_info - > { data } } , "Object name: $obj ($defhash{$obj}{'objtype'})" ) ;
2010-01-07 14:19:57 +00:00
}
else
{
2011-05-18 06:19:48 +00:00
if ( ! $ ::opt_c )
{
push ( @ { $ rsp_info - > { data } } , "Object name: $obj" ) ;
}
2010-01-07 14:19:57 +00:00
}
}
2007-10-26 22:44:33 +00:00
2010-01-07 14:19:57 +00:00
foreach my $ showattr ( sort @ attrlist )
2007-11-26 20:09:18 +00:00
{
2010-01-07 14:19:57 +00:00
if ( $ showattr eq 'objtype' )
{
next ;
}
2007-11-26 20:09:18 +00:00
2010-01-07 14:19:57 +00:00
my $ attrval ;
if ( exists ( $ defhash { $ obj } { $ showattr } ) )
2007-11-26 20:09:18 +00:00
{
2010-01-07 14:19:57 +00:00
$ attrval = $ defhash { $ obj } { $ showattr } ;
}
# if an attr list was provided then just display those
if ( $ ::opt_i )
{
if ( grep ( /^$showattr$/ , @ ::AttrList ) )
2007-11-26 20:09:18 +00:00
{
2010-01-07 14:19:57 +00:00
if ( ( $ defhash { $ obj } { 'objtype' } eq 'group' ) && ( $ showattr eq 'members' ) )
2007-11-26 20:09:18 +00:00
{
2010-01-07 14:19:57 +00:00
my $ memberlist =
xCAT::DBobjUtils - > getGroupMembers (
$ obj ,
\ % defhash ) ;
push ( @ { $ rsp_info - > { data } } , " $showattr=$memberlist" ) ;
2007-11-26 20:09:18 +00:00
}
else
{
2013-03-20 06:56:32 +00:00
# nics attributes, like nicips, nichostnamesuffix.
if ( $ showattr =~ /^nic/ )
2011-05-18 06:19:48 +00:00
{
2013-03-20 06:56:32 +00:00
my $ nicval = "$showattr=$attrval" ;
my $ nicnames ;
if ( defined ( $ ::NicsAttrHash { $ showattr } ) )
{
$ nicnames = join ( ',' , @ { $ ::NicsAttrHash { $ showattr } } ) ;
}
my $ nicsstr ;
if ( $ nicnames )
{
$ nicsstr = xCAT::DBobjUtils - > expandnicsattr ( $ nicval , $ nicnames ) ;
}
else
{
$ nicsstr = xCAT::DBobjUtils - > expandnicsattr ( $ nicval ) ;
}
# Compress mode, format the output
if ( $ ::opt_c )
{
$ nicsstr =~ s/^\s+/$obj: / ;
$ nicsstr =~ s/\n\s+/\n$obj: /g ;
}
if ( $ nicsstr )
{
push ( @ { $ rsp_info - > { data } } , "$nicsstr" ) ;
}
}
2011-05-18 06:19:48 +00:00
else
{
2013-03-20 06:56:32 +00:00
# since they asked for this attr
# show it even if not set
if ( ! $ ::opt_c )
{
push ( @ { $ rsp_info - > { data } } , " $showattr=$attrval" ) ;
}
else
{
push ( @ { $ rsp_info - > { data } } , "$obj: $showattr=$attrval" ) ;
}
}
2007-11-26 20:09:18 +00:00
}
}
}
else
{
2010-01-07 14:19:57 +00:00
if ( ( $ defhash { $ obj } { 'objtype' } eq 'group' )
&& ( $ showattr eq 'members' ) )
2007-11-26 20:09:18 +00:00
{
2011-03-20 05:56:47 +00:00
#$defhash{$obj}{'grouptype'} = "static";
2010-01-07 14:19:57 +00:00
my $ memberlist =
xCAT::DBobjUtils - > getGroupMembers ( $ obj , \ % defhash ) ;
push ( @ { $ rsp_info - > { data } } , " $showattr=$memberlist" ) ;
2007-11-26 20:09:18 +00:00
}
else
{
2010-01-07 14:19:57 +00:00
# don't print unless set
if ( ( defined ( $ attrval ) ) && ( $ attrval ne '' ) )
{
2013-03-20 06:56:32 +00:00
# nics attributes, like nicips, nichostnamesuffix.
if ( $ showattr =~ /^nic/ )
{
my $ nicval = "$showattr=$attrval" ;
my $ nicsstr = xCAT::DBobjUtils - > expandnicsattr ( $ nicval ) ;
if ( $ nicsstr )
{
push ( @ { $ rsp_info - > { data } } , "$nicsstr" ) ;
}
}
else
{
push ( @ { $ rsp_info - > { data } } , " $showattr=$attrval" ) ;
}
2010-01-07 14:19:57 +00:00
}
2007-11-26 20:09:18 +00:00
}
}
}
2011-06-07 06:51:28 +00:00
# Additional osimage attributes
if ( ( $ type eq "node" ) && $ ::opt_osimg )
{
if ( defined ( $ nodeosimagehash { $ obj } ) )
{
foreach my $ attr ( keys % { $ nodeosimagehash { $ obj } } )
{
if ( $ nodeosimagehash { $ obj } { $ attr } )
{
push ( @ { $ rsp_info - > { data } } , " $attr=$nodeosimagehash{$obj}{$attr}" ) ;
}
}
}
}
2007-10-26 22:44:33 +00:00
}
else
{
2010-01-07 14:19:57 +00:00
if ( $ ::opt_a )
2007-11-26 20:09:18 +00:00
{
if ( $ ::opt_z )
{
2010-01-07 14:19:57 +00:00
push ( @ { $ rsp_info - > { data } } , "\n$obj:" ) ;
2007-11-26 20:09:18 +00:00
}
else
{
2010-01-07 14:19:57 +00:00
# give the type also
push ( @ { $ rsp_info - > { data } } , "$obj ($::AllObjTypeHash{$obj})" ) ;
2007-11-26 20:09:18 +00:00
}
}
else
{
2010-01-07 14:19:57 +00:00
# just give the name
if ( $ ::opt_z )
2007-11-26 20:09:18 +00:00
{
2010-01-07 14:19:57 +00:00
push ( @ { $ rsp_info - > { data } } , "\n$obj:" ) ;
2007-11-26 20:09:18 +00:00
}
else
{
2010-03-26 11:45:16 +00:00
if ( scalar ( @ ::clobjtypes ) > 0 )
2007-11-26 20:09:18 +00:00
{
2010-01-07 14:19:57 +00:00
push ( @ { $ rsp_info - > { data } } , "$obj ($defhash{$obj}{'objtype'})" ) ;
2007-11-26 20:09:18 +00:00
}
else
{
2010-01-07 14:19:57 +00:00
push ( @ { $ rsp_info - > { data } } , "$obj" ) ;
2007-11-26 20:09:18 +00:00
}
}
}
2007-10-26 22:44:33 +00:00
}
2010-01-07 14:19:57 +00:00
}
2008-02-28 19:05:31 +00:00
} # end - for each object
} # end - for each type
2010-01-07 14:19:57 +00:00
2011-03-20 05:56:47 +00:00
#delete the fsp and bpa node from the hash
#my $newrsp;
#my $listtab = xCAT::Table->new( 'nodelist' );
#if ($listtab and (!defined($::opt_S)) ) {
# foreach my $n (@{$rsp_info->{data}}) {
# if ( $n =~ /\(node\)/ ) {
# $_= $n;
# s/ +\(node\)//;
# my ($hidhash) = $listtab->getNodeAttribs($_ ,['hidden']);
# if ( $hidhash->{hidden} ne 1) {
# push (@{$newrsp->{data}}, $n);
# }
# }else{
# push (@{$newrsp->{data}}, $n);
# }
# }
# if (defined($newrsp->{data}) && scalar(@{$newrsp->{data}}) > 0) {
# xCAT::MsgUtils->message("I", $newrsp, $::callback);
# return 0;
# }
#}else {
# my $rsp;
# $rsp->{data}->[0] =
# "Could not open nodelist table.";
# xCAT::MsgUtils->message("I", $rsp, $::callback);
#}
2011-01-10 09:12:35 +00:00
2010-01-07 14:19:57 +00:00
# Display the definition of objects
if ( defined ( $ rsp_info - > { data } ) && scalar ( @ { $ rsp_info - > { data } } ) > 0 ) {
xCAT::MsgUtils - > message ( "I" , $ rsp_info , $ ::callback ) ;
}
2007-10-26 22:44:33 +00:00
return 0 ;
}
#----------------------------------------------------------------------------
2007-11-26 20:09:18 +00:00
= head3 defrm
2007-10-26 22:44:33 +00:00
2007-11-26 20:09:18 +00:00
Support for the xCAT defrm command .
2007-10-26 22:44:33 +00:00
Arguments:
Returns:
0 - OK
1 - error
Globals:
2008-02-21 21:10:35 +00:00
2007-10-26 22:44:33 +00:00
Error:
Example:
Comments:
2011-03-20 05:56:47 +00:00
Object names to remove are derived from - o , - t , w , - a , - f ,
or noderange !
2007-10-26 22:44:33 +00:00
= cut
#-----------------------------------------------------------------------------
2007-11-26 20:09:18 +00:00
sub defrm
2007-10-26 22:44:33 +00:00
{
2007-11-26 20:09:18 +00:00
my % objhash ;
my $ error = 0 ;
my % rmhash ;
my % myhash ;
2011-03-24 10:41:58 +00:00
my % childrenhash ;
my % typehash ;
2007-10-26 22:44:33 +00:00
# process the command line
my $ rc = & processArgs ;
2011-03-18 14:58:45 +00:00
2007-10-26 22:44:33 +00:00
if ( $ rc != 0 )
{
2007-11-26 20:09:18 +00:00
# rc: 0 - ok, 1 - return, 2 - help, 3 - error
2011-03-20 05:56:47 +00:00
# 0 - continue
# 1 - return (like for version option)
# 2 - return with usage
# 3 - return error
if ( $ rc == 1 ) {
return 0 ;
} elsif ( $ rc == 2 ) {
& defrm_usage ;
return 0 ;
} elsif ( $ rc == 3 ) {
return 1 ;
}
}
2011-03-18 14:58:45 +00:00
2007-10-26 22:44:33 +00:00
2007-11-26 20:09:18 +00:00
if ( $ ::opt_a && ! $ ::opt_f )
2007-10-26 22:44:33 +00:00
{
2007-11-26 20:09:18 +00:00
my $ rsp ;
$ rsp - > { data } - > [ 0 ] =
2009-10-12 07:41:52 +00:00
"You must use the \'-f\' option when using the \'-a\' option." ;
2007-11-26 20:09:18 +00:00
xCAT::MsgUtils - > message ( "E" , $ rsp , $ ::callback ) ;
& defrm_usage ;
return 1 ;
2007-10-26 22:44:33 +00:00
}
2007-11-26 20:09:18 +00:00
#
# build a hash of object names and their types
#
2007-10-26 22:44:33 +00:00
2007-11-26 20:09:18 +00:00
# the list of objects to remove could have come from: the arg list,
2011-03-20 05:56:47 +00:00
# opt_o, a noderange, opt_t, or opt_a. (rmdef doesn't take file
# input)
2007-10-26 22:44:33 +00:00
2007-11-26 20:09:18 +00:00
# if a set of objects was specifically provided on the cmd line then
2011-03-20 05:56:47 +00:00
# there can only be one type value
2007-11-26 20:09:18 +00:00
if ( $ ::objectsfrom_opto || $ ::objectsfrom_nr || $ ::objectsfrom_args )
{
my $ type = @ ::clobjtypes [ 0 ] ;
2007-10-26 22:44:33 +00:00
2007-11-26 20:09:18 +00:00
foreach my $ obj ( sort @ ::clobjnames )
{
$ objhash { $ obj } = $ type ;
}
}
2007-10-26 22:44:33 +00:00
2007-11-26 20:09:18 +00:00
# if we derived a list of objects from a list of types
if ( $ ::objectsfrom_optt )
{
% objhash = % ::ObjTypeHash ;
}
2007-10-26 22:44:33 +00:00
2007-11-26 20:09:18 +00:00
# if we derived the list of objects from the "all" option
if ( $ ::objectsfrom_opta )
{
% objhash = % ::AllObjTypeHash ;
}
2007-10-26 22:44:33 +00:00
2007-11-26 20:09:18 +00:00
# handle the "-w" value - if provided
# the list of objects may be limited by the "-w" option
# see which objects have attr/val that match the where values
# - if provided
# !!!!! don't support -w for now - gets way too complicated with groups!!!!
if ( $ ::opt_w )
{
my $ rsp ;
$ rsp - > { data } - > [ 0 ] =
"The \'-w\' option is not supported for the rmdef command." ;
xCAT::MsgUtils - > message ( "I" , $ rsp , $ ::callback ) ;
$ error = 1 ;
return 1 ;
}
if ( 0 )
{
2007-10-26 22:44:33 +00:00
2007-11-26 20:09:18 +00:00
# need to get object defs from DB
% myhash = xCAT::DBobjUtils - > getobjdefs ( \ % objhash ) ;
2012-11-30 09:08:47 +00:00
if ( ! ( % myhash ) )
2007-11-26 20:09:18 +00:00
{
$ error = 1 ;
}
2007-10-26 22:44:33 +00:00
2007-11-26 20:09:18 +00:00
foreach my $ obj ( sort ( keys % objhash ) )
{
foreach my $ testattr ( keys % ::WhereHash )
{
if ( $ myhash { $ obj } { $ testattr } eq $ ::WhereHash { $ testattr } )
{
2007-10-26 22:44:33 +00:00
2007-11-26 20:09:18 +00:00
# add this object to the remove hash
$ rmhash { $ obj } = $ objhash { $ obj } ;
}
}
2007-10-26 22:44:33 +00:00
2007-11-26 20:09:18 +00:00
}
% objhash = % rmhash ;
}
2007-10-26 22:44:33 +00:00
2007-11-26 20:09:18 +00:00
# if the object to remove is a group then the "groups" attr of
2011-03-20 05:56:47 +00:00
# the memberlist nodes must be updated.
2007-10-26 22:44:33 +00:00
2007-11-26 20:09:18 +00:00
my $ numobjects = 0 ;
2009-06-10 06:31:37 +00:00
my % objTypeLists ;
2007-11-26 20:09:18 +00:00
foreach my $ obj ( keys % objhash )
{
2009-06-10 06:31:37 +00:00
my $ objtype = $ objhash { $ obj } ;
if ( ! defined ( $ objTypeLists { $ objtype } ) ) # Do no call getObjectsOfType for the same objtype more than once.
{
@ { $ objTypeLists { $ objtype } } = xCAT::DBobjUtils - > getObjectsOfType ( $ objtype ) ;
}
if ( ! grep ( /^$obj$/ , @ { $ objTypeLists { $ objtype } } ) ) #Object is not in the db, do not need to delete
{
my $ rsp ;
2009-10-12 07:41:52 +00:00
$ rsp - > { data } - > [ 0 ] = "Could not find an object named \'$obj\' of type \'$objtype\'." ;
2009-06-10 06:31:37 +00:00
xCAT::MsgUtils - > message ( "E" , $ rsp , $ ::callback ) ;
next ;
}
2007-11-26 20:09:18 +00:00
$ numobjects + + ;
2007-10-26 22:44:33 +00:00
2007-11-26 20:09:18 +00:00
if ( $ objhash { $ obj } eq 'group' )
{
2007-10-26 22:44:33 +00:00
2007-11-26 20:09:18 +00:00
# get the group object definition
2011-03-20 05:56:47 +00:00
my % ghash ;
2007-11-26 20:09:18 +00:00
$ ghash { $ obj } = 'group' ;
2009-12-30 07:50:28 +00:00
my @ attrs = ( 'grouptype' , 'wherevals' ) ;
my % grphash = xCAT::DBobjUtils - > getobjdefs ( \ % ghash , 0 , \ @ attrs ) ;
2012-11-30 09:08:47 +00:00
if ( ! ( % grphash ) )
2007-11-26 20:09:18 +00:00
{
my $ rsp ;
$ rsp - > { data } - > [ 0 ] =
"Could not get xCAT object definition for \'$obj\'." ;
xCAT::MsgUtils - > message ( "I" , $ rsp , $ ::callback ) ;
next ;
}
2007-10-26 22:44:33 +00:00
2009-06-23 05:22:57 +00:00
# Dynamic node group stores in nodegroup table
# do not need to update the nodelist table
if ( $ grphash { $ obj } { 'grouptype' } eq 'dynamic' )
{
next ;
}
2007-11-26 20:09:18 +00:00
# get the members list
2011-03-20 05:56:47 +00:00
# all groups are "static" for now
$ grphash { $ obj } { 'grouptype' } = "static" ;
2007-11-26 20:09:18 +00:00
my $ memberlist = xCAT::DBobjUtils - > getGroupMembers ( $ obj , \ % grphash ) ;
my @ members = split ( ',' , $ memberlist ) ;
2007-10-26 22:44:33 +00:00
2009-06-23 05:33:53 +00:00
# No node in the group
if ( scalar ( @ members ) == 0 )
{
next ;
}
2007-11-26 20:09:18 +00:00
# foreach member node of the group
my % nodehash ;
my % nhash ;
my @ gprslist ;
foreach my $ m ( @ members )
{
# get the def of this node
$ nhash { $ m } = 'node' ;
2009-06-23 05:22:57 +00:00
}
# Performance: Only call getobjdefs once
2009-12-30 07:50:28 +00:00
my @ attrs = ( 'groups' ) ;
% nodehash = xCAT::DBobjUtils - > getobjdefs ( \ % nhash , 0 , \ @ attrs ) ;
2012-11-30 09:08:47 +00:00
if ( ! ( % nodehash ) )
2007-11-26 20:09:18 +00:00
{
my $ rsp ;
2009-06-23 05:22:57 +00:00
my @ nodes = keys % nhash ;
my $ m = join ',' , @ nodes ;
2007-11-26 20:09:18 +00:00
$ rsp - > { data } - > [ 0 ] =
"Could not get xCAT object definition for \'$m\'." ;
xCAT::MsgUtils - > message ( "I" , $ rsp , $ ::callback ) ;
next ;
}
2009-06-23 05:22:57 +00:00
foreach my $ m ( keys % nodehash )
{
# need to update the "groups" attr of the node def
2007-11-26 20:09:18 +00:00
# split the "groups" to get a list
@ gprslist = split ( ',' , $ nodehash { $ m } { groups } ) ;
# make a new "groups" list for the node without the
2011-03-20 05:56:47 +00:00
# group that is being removed
2007-11-26 20:09:18 +00:00
my $ first = 1 ;
2008-12-11 17:58:37 +00:00
my $ newgrps = "" ;
2007-11-26 20:09:18 +00:00
foreach my $ grp ( @ gprslist )
{
chomp ( $ grp ) ;
if ( $ grp eq $ obj )
{
next ;
}
else
{
# set new groups list for node
if ( ! $ first )
{
$ newgrps . = "," ;
}
2008-12-11 17:58:37 +00:00
$ newgrps . = "$grp" ;
2007-11-26 20:09:18 +00:00
$ first = 0 ;
}
}
# make the change to %nodehash
$ nodehash { $ m } { groups } = $ newgrps ;
}
# set the new node attr values
if ( xCAT::DBobjUtils - > setobjdefs ( \ % nodehash ) != 0 )
{
my $ rsp ;
$ rsp - > { data } - > [ 0 ] = "Could not write data to xCAT database." ;
xCAT::MsgUtils - > message ( "E" , $ rsp , $ ::callback ) ;
$ error = 1 ;
}
}
2007-10-26 22:44:33 +00:00
}
2011-03-24 10:41:58 +00:00
# find the children of the node.
for my $ tob ( keys % objhash ) {
if ( $ objhash { $ tob } eq 'node' ) {
my $ ntype = xCAT::DBobjUtils - > getnodetype ( $ tob ) ;
if ( $ ntype =~ /^(cec|frame)$/ ) {
my $ cnodep = xCAT::DBobjUtils - > getchildren ( $ tob ) ;
if ( $ cnodep ) {
my $ cnode = join ',' , @$ cnodep ;
$ childrenhash { $ tob } = $ cnode ;
$ typehash { $ tob } = $ ntype ;
}
}
}
}
2007-11-26 20:09:18 +00:00
# remove the objects
if ( xCAT::DBobjUtils - > rmobjdefs ( \ % objhash ) != 0 )
2007-10-26 22:44:33 +00:00
{
2007-11-26 20:09:18 +00:00
$ error = 1 ;
2007-10-26 22:44:33 +00:00
}
2007-11-26 20:09:18 +00:00
if ( $ error )
{
my $ rsp ;
$ rsp - > { data } - > [ 0 ] =
"One or more errors occured when attempting to remove xCAT object definitions." ;
xCAT::MsgUtils - > message ( "E" , $ rsp , $ ::callback ) ;
return 1 ;
}
else
2007-10-26 22:44:33 +00:00
{
2007-11-26 20:09:18 +00:00
if ( $ numobjects > 0 )
{
if ( $ ::verbose )
{
2007-10-26 22:44:33 +00:00
2007-11-26 20:09:18 +00:00
# give results
my $ rsp ;
$ rsp - > { data } - > [ 0 ] = "The following objects were removed:" ;
xCAT::MsgUtils - > message ( "I" , $ rsp , $ ::callback ) ;
2007-10-26 22:44:33 +00:00
2007-11-26 20:09:18 +00:00
my $ n = 1 ;
foreach my $ o ( sort ( keys % objhash ) )
{
$ rsp - > { data } - > [ $ n ] = "$o" ;
$ n + + ;
}
xCAT::MsgUtils - > message ( "I" , $ rsp , $ ::callback ) ;
}
else
{
my $ rsp ;
2011-10-09 01:39:37 +00:00
my $ nodenum = scalar ( keys % objhash ) ;
$ rsp - > { data } - > [ 0 ] = "$nodenum object definitions have been removed." ;
2007-11-26 20:09:18 +00:00
xCAT::MsgUtils - > message ( "I" , $ rsp , $ ::callback ) ;
}
2011-03-24 10:41:58 +00:00
# Give a warning message to the user to remove the children of the node.
for my $ tn ( keys % objhash ) {
if ( $ childrenhash { $ tn } ) {
my $ rsp ;
$ rsp - > { data } - > [ 0 ] =
"You have removed a $typehash{$tn} node, please remove these nodes belongs to it manually: $childrenhash{$tn} ." ;
xCAT::MsgUtils - > message ( "W" , $ rsp , $ ::callback ) ;
}
}
2007-11-26 20:09:18 +00:00
}
else
{
my $ rsp ;
$ rsp - > { data } - > [ 0 ] =
"No objects have been removed from the xCAT database." ;
xCAT::MsgUtils - > message ( "I" , $ rsp , $ ::callback ) ;
}
return 0 ;
2007-10-26 22:44:33 +00:00
2007-11-26 20:09:18 +00:00
}
2007-10-26 22:44:33 +00:00
}
#----------------------------------------------------------------------------
= head3 defmk_usage
Arguments:
Returns:
Globals:
Error:
Example:
Comments:
= cut
#-----------------------------------------------------------------------------
# subroutines to display the usage
sub defmk_usage
{
2007-11-26 20:09:18 +00:00
my $ rsp ;
2007-10-26 22:44:33 +00:00
$ rsp - > { data } - > [ 0 ] =
2007-11-26 20:09:18 +00:00
"\nUsage: mkdef - Create xCAT data object definitions.\n" ;
$ rsp - > { data } - > [ 1 ] = " mkdef [-h | --help ] [-t object-types]\n" ;
2007-10-26 22:44:33 +00:00
$ rsp - > { data } - > [ 2 ] =
2007-11-26 20:09:18 +00:00
" mkdef [-V | --verbose] [-t object-types] [-o object-names] [-z|--stanza ]" ;
2007-10-26 22:44:33 +00:00
$ rsp - > { data } - > [ 3 ] =
2009-07-06 09:45:22 +00:00
" [-d | --dynamic] [-w attr==val [-w attr=~val] ...]" ;
2007-11-26 20:09:18 +00:00
$ rsp - > { data } - > [ 4 ] =
2013-04-08 07:03:24 +00:00
" [-f | --force] [noderange] [attr=val [attr=val...]]" ;
$ rsp - > { data } - > [ 5 ] =
" [-u provmethod=<install|netboot|statelite> profile=<xxx> [attr=value]]\n" ;
$ rsp - > { data } - > [ 6 ] =
2007-11-26 20:09:18 +00:00
"\nThe following data object types are supported by xCAT.\n" ;
2013-04-08 07:03:24 +00:00
my $ n = 7 ;
2007-11-26 20:09:18 +00:00
foreach my $ t ( sort ( keys % { xCAT::Schema:: defspec } ) )
{
$ rsp - > { data } - > [ $ n ] = "$t" ;
$ n + + ;
}
$ rsp - > { data } - > [ $ n ] =
"\nUse the \'-h\' option together with the \'-t\' option to" ;
$ n + + ;
$ rsp - > { data } - > [ $ n ] =
"get a list of valid attribute names for each object type.\n" ;
xCAT::MsgUtils - > message ( "I" , $ rsp , $ ::callback ) ;
return 0 ;
2007-10-26 22:44:33 +00:00
}
#----------------------------------------------------------------------------
= head3 defch_usage
Arguments:
Returns:
Globals:
Error:
Example:
Comments:
= cut
#-----------------------------------------------------------------------------
sub defch_usage
{
2007-11-26 20:09:18 +00:00
my $ rsp ;
2007-10-26 22:44:33 +00:00
$ rsp - > { data } - > [ 0 ] =
2007-11-26 20:09:18 +00:00
"\nUsage: chdef - Change xCAT data object definitions.\n" ;
$ rsp - > { data } - > [ 1 ] = " chdef [-h | --help ] [-t object-types]\n" ;
2010-09-08 10:38:12 +00:00
$ rsp - > { data } - > [ 2 ] = " chdef [-t object-types] [-o object-names] [-n new-name] [node]\n" ;
2007-10-26 22:44:33 +00:00
$ rsp - > { data } - > [ 3 ] =
2010-09-08 10:38:12 +00:00
" chdef [-V | --verbose] [-t object-types] [-o object-names] [-d | --dynamic]" ;
2007-11-26 20:09:18 +00:00
$ rsp - > { data } - > [ 4 ] =
2010-09-08 10:38:12 +00:00
" [-z | --stanza] [-m | --minus] [-p | --plus]" ;
2007-11-26 20:09:18 +00:00
$ rsp - > { data } - > [ 5 ] =
2010-09-08 10:38:12 +00:00
" [-w attr==val [-w attr=~val] ... ] [noderange] [attr=val [attr=val...]]\n" ;
2013-04-08 07:03:24 +00:00
$ rsp - > { data } - > [ 6 ] =
" [-u [provmethod=<install|netboot|statelite>]|[profile=<xxx>]|[attr=value]]" ;
$ rsp - > { data } - > [ 7 ] =
2007-11-26 20:09:18 +00:00
"\nThe following data object types are supported by xCAT.\n" ;
2013-04-08 07:03:24 +00:00
my $ n = 8 ;
2007-11-26 20:09:18 +00:00
foreach my $ t ( sort ( keys % { xCAT::Schema:: defspec } ) )
{
$ rsp - > { data } - > [ $ n ] = "$t" ;
$ n + + ;
}
$ rsp - > { data } - > [ $ n ] =
"\nUse the \'-h\' option together with the \'-t\' option to" ;
$ n + + ;
$ rsp - > { data } - > [ $ n ] =
"get a list of valid attribute names for each object type.\n" ;
xCAT::MsgUtils - > message ( "I" , $ rsp , $ ::callback ) ;
return 0 ;
2007-10-26 22:44:33 +00:00
}
#----------------------------------------------------------------------------
= head3 defls_usage
Arguments:
Returns:
Globals:
Error:
Example:
Comments:
= cut
#-----------------------------------------------------------------------------
sub defls_usage
{
2007-11-26 20:09:18 +00:00
my $ rsp ;
$ rsp - > { data } - > [ 0 ] = "\nUsage: lsdef - List xCAT data object definitions.\n" ;
$ rsp - > { data } - > [ 1 ] = " lsdef [-h | --help ] [-t object-types]\n" ;
2007-10-26 22:44:33 +00:00
$ rsp - > { data } - > [ 2 ] =
2007-11-26 20:09:18 +00:00
" lsdef [-V | --verbose] [-t object-types] [-o object-names]" ;
2007-10-26 22:44:33 +00:00
$ rsp - > { data } - > [ 3 ] =
2011-01-10 09:26:46 +00:00
" [ -l | --long] [-s | --short] [-a | --all] [-z | --stanza ] [-S]" ;
2007-11-26 20:09:18 +00:00
$ rsp - > { data } - > [ 4 ] =
2009-07-06 09:45:22 +00:00
" [-i attr-list] [-w attr==val [-w attr=~val] ...] [noderange]\n" ;
2007-11-26 20:09:18 +00:00
$ rsp - > { data } - > [ 5 ] =
"\nThe following data object types are supported by xCAT.\n" ;
my $ n = 6 ;
foreach my $ t ( sort ( keys % { xCAT::Schema:: defspec } ) )
{
$ rsp - > { data } - > [ $ n ] = "$t" ;
$ n + + ;
}
$ rsp - > { data } - > [ $ n ] =
"\nUse the \'-h\' option together with the \'-t\' option to" ;
$ n + + ;
$ rsp - > { data } - > [ $ n ] =
"get a list of valid attribute names for each object type.\n" ;
xCAT::MsgUtils - > message ( "I" , $ rsp , $ ::callback ) ;
return 0 ;
2007-10-26 22:44:33 +00:00
}
#----------------------------------------------------------------------------
= head3 defrm_usage
Arguments:
Returns:
Globals:
Error:
Example:
Comments:
= cut
#-----------------------------------------------------------------------------
sub defrm_usage
{
2007-11-26 20:09:18 +00:00
my $ rsp ;
2007-10-26 22:44:33 +00:00
$ rsp - > { data } - > [ 0 ] =
2007-11-26 20:09:18 +00:00
"\nUsage: rmdef - Remove xCAT data object definitions.\n" ;
$ rsp - > { data } - > [ 1 ] = " rmdef [-h | --help ] [-t object-types]\n" ;
2007-10-26 22:44:33 +00:00
$ rsp - > { data } - > [ 2 ] =
2007-11-26 20:09:18 +00:00
" rmdef [-V | --verbose] [-t object-types] [-a | --all] [-f | --force]" ;
$ rsp - > { data } - > [ 3 ] =
" [-o object-names] [-w attr=val,[attr=val...] [noderange]\n" ;
$ rsp - > { data } - > [ 4 ] =
"\nThe following data object types are supported by xCAT.\n" ;
my $ n = 5 ;
foreach my $ t ( sort ( keys % { xCAT::Schema:: defspec } ) )
{
$ rsp - > { data } - > [ $ n ] = "$t" ;
$ n + + ;
}
$ rsp - > { data } - > [ $ n ] =
"\nUse the \'-h\' option together with the \'-t\' option to" ;
$ n + + ;
$ rsp - > { data } - > [ $ n ] =
"get a list of valid attribute names for each object type.\n" ;
xCAT::MsgUtils - > message ( "I" , $ rsp , $ ::callback ) ;
return 0 ;
2007-10-26 22:44:33 +00:00
}
2011-03-02 06:36:24 +00:00
#----------------------------------------------------------------------------
= head3 initialize_variables
Initialize the global variables
Arguments:
Returns:
Globals:
Error:
Example:
Comments:
= cut
#-----------------------------------------------------------------------------
sub initialize_variables
{
% ::CLIATTRS = ( ) ;
% ::FILEATTRS = ( ) ;
% ::FINALATTRS = ( ) ;
% ::objfilehash = ( ) ;
% ::WhereHash = ( ) ;
@ ::AttrList = ( ) ;
2013-03-20 06:56:32 +00:00
% ::NicsAttrHash = ( ) ;
2011-03-02 06:36:24 +00:00
@ ::clobjtypes = ( ) ;
@ ::fileobjtypes = ( ) ;
@ ::clobjnames = ( ) ;
@ ::fileobjnames = ( ) ;
@ ::objfilelist = ( ) ;
@ ::allobjnames = ( ) ;
@ ::noderange = ( ) ;
}
2007-10-26 22:44:33 +00:00
1 ;
2007-11-26 20:09:18 +00:00