From d0efc48ffd1be19d11ea25a53d62d34336806d43 Mon Sep 17 00:00:00 2001 From: lissav Date: Thu, 27 Mar 2008 19:48:28 +0000 Subject: [PATCH] Add sample client and plugin code. Need to add documentation in a manpage/usage message. git-svn-id: https://svn.code.sf.net/p/xcat/code/xcat-core/trunk@916 8638fb3e-16cb-4fca-ae20-7b5d299a9bcd --- xCAT-client-2.0/bin/xCATWorld | 210 ++++++++++++++++++ xCAT-server-2.0/lib/xcat/plugins/xCATWorld.pm | 69 ++++++ 2 files changed, 279 insertions(+) create mode 100644 xCAT-client-2.0/bin/xCATWorld create mode 100644 xCAT-server-2.0/lib/xcat/plugins/xCATWorld.pm diff --git a/xCAT-client-2.0/bin/xCATWorld b/xCAT-client-2.0/bin/xCATWorld new file mode 100644 index 000000000..65f66e940 --- /dev/null +++ b/xCAT-client-2.0/bin/xCATWorld @@ -0,0 +1,210 @@ +#!/usr/bin/env perl +# IBM(c) 2007 EPL license http://www.eclipse.org/legal/epl-v10.html +BEGIN +{ + $::XCATROOT = $ENV{'XCATROOT'} ? $ENV{'XCATROOT'} : -d '/opt/xcat' ? '/opt/xcat' : '/usr'; +} +use lib "$::XCATROOT/lib/perl"; +use IO::Socket::SSL; +use IO::Socket::INET; +use File::Basename; +use Data::Dumper; +use Getopt::Long; +use xCAT::MsgUtils; +use xCAT::Client submit_request; +my $bname = basename($0); + +#----------------------------------------------------------------------------- + +=head1 xCATWorld + +This program is an example client program for xCAT +which interfaces to the /opt/xcat/lib/perl/xcat_plugin/CATWorld.pm plugin + + xCATWorld + +=cut + +#----------------------------------------------------------------------------- +# Main + +my $rc = 0; + + +my $cmdref; +my $arg; +my @SaveARGV = @ARGV; +$cmdref->{command}->[0] = $bname; # save my command name +my $arg = shift(@SaveARGV); + +if ($arg =~ /^-/) # no noderange +{ + push @{$cmdref->{arg}}, $arg; + foreach (@SAVEARGV) + { + push(@{$cmdref->{arg}}, $_); + } + @ARGV = @{$cmdref->{arg}}; # save just the argument to parse +} +else +{ + $cmdref->{noderange}->[0] = $arg; # save noderange + @ARGV = @SaveARGV; # noderange removed for parsing +} + + +foreach (@SaveARGV) +{ + push(@{$cmdref->{arg}}, $_); +} + + +xCAT::Client::submit_request($cmdref, \&handle_response); +exit $rc; + + +#----------------------------------------------------------------------------- + +=head3 handle_response + handle_response is the callback that is + invoked to print out the data returned by + the plugin. + + Format of the response hash: + {data => [ 'data str1', 'data str2', '...' ] } + + Results are printed as: + data str1 + data str2 + + or: + {data => [ {desc => [ 'desc1' ], + contents => [ 'contents1' ] }, + {desc => [ 'desc2 ], + contents => [ 'contents2' ] } + : + ] } + NOTE: In this format, only the data array can have more than one + element. All other arrays are assumed to be a single element. + Results are printed as: + desc1: contents1 + desc2: contents2 + + or: + {node => [ {name => ['node1'], + data => [ {desc => [ 'node1 desc' ], + contents => [ 'node1 contents' ] } ] }, + {name => ['node2'], + data => [ {desc => [ 'node2 desc' ], + contents => [ 'node2 contents' ] } ] }, + : + ] } + NOTE: Only the node array can have more than one element. + All other arrays are assumed to be a single element. + + This was generated from the corresponding HTML: + + + node1 + + node1 desc + node1 contents + + + + node2 + + node2 desc + node2 contents + + + + + Results are printed as: + node_name: desc: contents +=cut + +#----------------------------------------------------------------------------- +sub handle_response +{ + my $rsp = shift; + + # Handle {node} structure + if ($rsp->{errorcode}) + { + foreach my $ecode (@{$rsp->{errorcode}}) + { + $exitcode |= $ecode; + } + } + + # Handle {node} structure + if ($rsp->{node}) + { + my $nodes = ($rsp->{node}); + my $node; + foreach $node (@$nodes) + { + my $desc = $node->{name}->[0]; + if ($node->{data}) + { + if (ref(\($node->{data}->[0])) eq 'SCALAR') + { + $desc = $desc . ": " . $node->{data}->[0]; + } + else + { + if ($node->{data}->[0]->{desc}) + { + $desc = $desc . ": " . $node->{data}->[0]->{desc}->[0]; + } + if ($node->{data}->[0]->{contents}) + { + $desc = "$desc: " . $node->{data}->[0]->{contents}->[0]; + } + } + } + if ($desc) + { + print "$desc\n"; + } + } + } + + # Handle {data} structure with no nodes + if ($rsp->{info}) + { + my $data = ($rsp->{info}); + my $data_entry; + foreach $data_entry (@$data) + { + my $desc; + if (ref(\($data_entry)) eq 'SCALAR') + { + $desc = $data_entry; + } + else + { + if ($data_entry->{desc}) + { + $desc = $data_entry->{desc}->[0]; + } + if ($data_entry->{contents}) + { + if ($desc) + { + $desc = "$desc: " . $data_entry->{contents}->[0]; + } + else + { + $desc = $data_entry->{contents}->[0]; + } + } + } + if ($desc) + { + print "$desc\n"; + } + } + } +} diff --git a/xCAT-server-2.0/lib/xcat/plugins/xCATWorld.pm b/xCAT-server-2.0/lib/xcat/plugins/xCATWorld.pm new file mode 100644 index 000000000..5914e63ef --- /dev/null +++ b/xCAT-server-2.0/lib/xcat/plugins/xCATWorld.pm @@ -0,0 +1,69 @@ +# IBM(c) 2007 EPL license http://www.eclipse.org/legal/epl-v10.html +#------------------------------------------------------- + +=head1 + xCAT plugin package to handle xCATWorld + + Supported command: + xCATWorld->xCATWorld + +=cut + +#------------------------------------------------------- +package xCAT_plugin::xCATWorld; +use xCAT::Table; + +use xCAT::Utils; + +use xCAT::MsgUtils; +use Getopt::Long; +1; + +#------------------------------------------------------- + +=head3 handled_commands + +Return list of commands handled by this plugin + +=cut + +#------------------------------------------------------- + +sub handled_commands +{ + return {xCATWorld => "xCATWorld"}; +} + +#------------------------------------------------------- + +=head3 process_request + + Process the command + +=cut + +#------------------------------------------------------- +sub process_request +{ + + my $request = shift; + my $callback = shift; + my $nodes = $request->{node}; + my $command = $request->{command}->[0]; + my $args = $request->{arg}; + my $envs = $request->{env}; + my %rsp; + my $i = 0; + my @nodes=@$nodes; + # do your processing here + # return info + foreach $node (@nodes) + { + $rsp->{data}->[$i] = "Hello $node\n"; + $i++; + } + xCAT::MsgUtils->message("I", $rsp, $callback, 0); + return; + +} +