From f4e8d1c2332c3f238a1ec4e8719e0e059d296f86 Mon Sep 17 00:00:00 2001 From: Casandra Qiu Date: Wed, 6 May 2015 16:49:47 -0400 Subject: [PATCH] Create a plugin to handle switchdiscover command --- perl-xCAT/xCAT/Usage.pm | 6 +- xCAT-client/xCAT-client.spec | 1 + .../lib/xcat/plugins/switchdiscover.pm | 248 ++++++++++++++++++ 3 files changed, 254 insertions(+), 1 deletion(-) create mode 100755 xCAT-server/lib/xcat/plugins/switchdiscover.pm diff --git a/perl-xCAT/xCAT/Usage.pm b/perl-xCAT/xCAT/Usage.pm index c8b10825e..08e7847a4 100644 --- a/perl-xCAT/xCAT/Usage.pm +++ b/perl-xCAT/xCAT/Usage.pm @@ -194,7 +194,7 @@ my %usage = ( PPC specific: getmacs [-F filter] getmacs [-M] - getmacs [-V| --verbose] [-f] [-d] [--arp] | [-D {[-o] [-S server] [-G gateway] [-C client] | [--noping]}] + getmacs [-V| --verbose] [-f] [-d] [--arp] | [-D [-o] [-S server] [-G gateway] [-C client]] blade specific: getmacs [-V| --verbose] [-d] [--arp] [-i ethN|enN] ", @@ -286,6 +286,9 @@ my %usage = ( "Usage: lsslp [-h|--help|-v|--version] lsslp [][-V|--verbose][-i ip[,ip..]][-w][-r|-x|-z][-n][-I][-s FRAME|CEC|MM|IVM|RSA|HMC|CMM|IMM2|FSP] [-u] [--range IPranges][-t tries][--vpdtable][-C counts][-T timeout]", + "switchdiscover" => +"Usage: switchdiscover [-h|--help|-v|--version] + switchdiscover [][-V|--verbose][-i adpt[,adpt..]][-w][-r|-x|-z][-n][-s scan_methods]", "rflash" => "Usage: rflash [ -h|--help|-v|--version] @@ -481,6 +484,7 @@ my %version = ( "chvm" => "$vers", "rmvm" => "$vers", "lsslp" => "$vers", + "switchdiscover" => "$vers", "rflash" => "$vers", "renergy" => "$vers", "lsflexnode" => "$vers", diff --git a/xCAT-client/xCAT-client.spec b/xCAT-client/xCAT-client.spec index 116e4d369..fe4d0c13f 100644 --- a/xCAT-client/xCAT-client.spec +++ b/xCAT-client/xCAT-client.spec @@ -163,6 +163,7 @@ ln -sf ../bin/xcatclient $RPM_BUILD_ROOT/%{prefix}/bin/lsflexnode ln -sf ../bin/xcatclient $RPM_BUILD_ROOT/%{prefix}/bin/rmflexnode ln -sf ../bin/xcatclient $RPM_BUILD_ROOT/%{prefix}/bin/mkflexnode ln -sf ../bin/xcatclientnnr $RPM_BUILD_ROOT/%{prefix}/bin/lsslp +ln -sf ../bin/xcatclientnnr $RPM_BUILD_ROOT/%{prefix}/bin/switchdiscover ln -sf ../bin/xcatclient $RPM_BUILD_ROOT/%{prefix}/bin/imgcapture ln -sf ../bin/xcatclientnnr $RPM_BUILD_ROOT/%{prefix}/bin/swapnodes ln -sf ../bin/xcatclientnnr $RPM_BUILD_ROOT/%{prefix}/bin/nodegrpch diff --git a/xCAT-server/lib/xcat/plugins/switchdiscover.pm b/xCAT-server/lib/xcat/plugins/switchdiscover.pm new file mode 100755 index 000000000..f0c9b6cca --- /dev/null +++ b/xCAT-server/lib/xcat/plugins/switchdiscover.pm @@ -0,0 +1,248 @@ +# IBM(c) 2007 EPL license http://www.eclipse.org/legal/epl-v10.html + +package xCAT_plugin::switchdiscover; +use lib "/opt/xcat/lib/perl"; +use strict; +use Getopt::Long; +use xCAT::Usage; +use xCAT::NodeRange; +use xCAT::Utils; + +####################################### +# Power methods +####################################### +my %globalopt; +my @filternodes; + + +########################################################################## +# Invokes the callback with the specified message +########################################################################## +sub send_msg { + + my $request = shift; + my $ecode = shift; + my $msg = shift; + my %output; + + ################################################# + # Called from child process - send to parent + ################################################# + if ( exists( $request->{pipe} )) { + my $out = $request->{pipe}; + + $output{errorcode} = $ecode; + $output{data} = \@_; + print $out freeze( [\%output] ); + print $out "\nENDOFFREEZE6sK4ci\n"; + } + ################################################# + # Called from parent - invoke callback directly + ################################################# + elsif ( exists( $request->{callback} )) { + my $callback = $request->{callback}; + $output{errorcode} = $ecode; + $output{data} = $msg; + $callback->( \%output ); + } +} + + +########################################################################## +# Command handler method from tables +########################################################################## +sub handled_commands { + return( {switchdiscover=>"switchdiscover"} ); +} + +########################################################################## +# Parse the command line options and operands +########################################################################## +sub parse_args { + + my $request = shift; + my $args = $request->{arg}; + my $cmd = $request->{command}; + my %opt; + + ############################################# + # Responds with usage statement + ############################################# + local *usage = sub { + my $usage_string = xCAT::Usage->getUsage($cmd); + return( [$_[0], $usage_string] ); + }; + ############################################# + # No command-line arguments - use defaults + ############################################# + if ( !defined( $args )) { + return(0); + } + ############################################# + # Checks case in GetOptions, allows opts + # to be grouped (e.g. -vx), and terminates + # at the first unrecognized option. + ############################################# + @ARGV = @$args; + $Getopt::Long::ignorecase = 0; + Getopt::Long::Configure( "bundling" ); + + ############################################# + # Process command-line flags + ############################################# + if (!GetOptions( \%opt, + qw(h|help V|Verbose v|version i=s x z w r n range=s s=s))) { + return( usage() ); + } + + ############################################# + # Check for node range + ############################################# + if ( scalar(@ARGV) eq 1 ) { + my @nodes = xCAT::NodeRange::noderange( @ARGV ); + foreach (@nodes) { + push @filternodes, $_; + } + unless (@filternodes) { + return(usage( "Invalid Argument: $ARGV[0]" )); + } + } elsif ( scalar(@ARGV) > 1 ) { + return(usage( "Invalid flag, please check and retry." )); + } + + ############################################# + # Option -V for verbose output + ############################################# + if ( exists( $opt{V} )) { + $globalopt{verbose} = 1; + } + + ############################################# + # Check for mutually-exclusive formatting + ############################################# + if ( (exists($opt{r}) + exists($opt{x}) + exists($opt{z}) ) > 1 ) { + return( usage() ); + } + + ############################################# + # Check for unsupported service type + ############################################# + if ( exists( $opt{s} )) { + $globalopt{service} = $opt{s}; + } + + ############################################# + # Check the validation of -i option + ############################################# + if ( exists( $opt{i} )) { + foreach ( split /,/, $opt{i} ) { + } + $globalopt{i} = $opt{i}; + } + + ############################################# + # write to the database + ############################################# + if ( exists( $opt{w} )) { + $globalopt{w} = 1; + } + + ############################################# + # list the raw information + ############################################# + if ( exists( $opt{r} )) { + $globalopt{r} = 1; + } + + ############################################# + # list the xml formate data + ############################################# + if ( exists( $opt{x} )) { + $globalopt{x} = 1; + } + + ############################################# + # list the stanza formate data + ############################################# + if ( exists( $opt{z} )) { + $globalopt{z} = 1; + } + + ######################################################### + # only list the nodes that discovered for the first time + ######################################################### + if ( exists( $opt{n} )) { + $globalopt{n} = 1; + } + + ######################################################### + # only list the nodes that discovered for the first time + ######################################################### + if ( exists( $opt{n} )) { + $globalopt{n} = 1; + } + + return; +} + + +############################################################################# +# Preprocess request from xCAT daemon +############################################################################# +sub preprocess_request { + my $req = shift; + if ($req->{_xcatpreprocessed}->[0] == 1) { return [$req]; } + my $callback=shift; + my $command = $req->{command}->[0]; + my $extrargs = $req->{arg}; + my @exargs=($req->{arg}); + if (ref($extrargs)) { + @exargs=@$extrargs; + } + my $usage_string=xCAT::Usage->parseCommand($command, @exargs); + if ($usage_string) { + $callback->({data=>[$usage_string]}); + $req = {}; + return; + } + + my @result = (); + my $mncopy = {%$req}; + push @result, $mncopy; + return \@result; +} + +########################################################################## +# Process request from xCat daemon +########################################################################## +sub process_request { + my $req = shift; + my $callback = shift; + + ########################################### + # Build hash to pass around + ########################################### + my %request; + $request{arg} = $req->{arg}; + $request{callback} = $callback; + $request{command} = $req->{command}->[0]; + + #################################### + # Process command-specific options + #################################### + my $result = parse_args( \%request ); + + #################################### + # Return error + #################################### + if ( ref($result) eq 'ARRAY' ) { + send_msg( \%request, 1, @$result ); + return(1); + } + + return; + +} + +1; +