184 lines
4.5 KiB
Perl
184 lines
4.5 KiB
Perl
#!/usr/bin/perl
|
|
|
|
package xCAT_plugin::vlan;
|
|
BEGIN
|
|
{
|
|
$::XCATROOT = $ENV{'XCATROOT'} ? $ENV{'XCATROOT'} : '/opt/xcat';
|
|
}
|
|
use lib "$::XCATROOT/lib/perl";
|
|
use strict;
|
|
use warnings "all";
|
|
use xCAT::GlobalDef;
|
|
|
|
use xCAT::Utils;
|
|
use xCAT::TableUtils;
|
|
use xCAT::SvrUtils;
|
|
use xCAT::Usage;
|
|
use xCAT::NodeRange;
|
|
|
|
sub handled_commands {
|
|
return {
|
|
mkvlan => 'switch:vlan,port,switch', # in progress
|
|
chvlan => 'switch:vlan,port,switch', # in progress
|
|
lsvlan => 'switch:vlan,port,switch', # in progress
|
|
rmvlan => 'switch:vlan,port,switch', # in progress
|
|
}
|
|
}
|
|
|
|
use POSIX "WNOHANG";
|
|
use IO::Handle;
|
|
use IO::Socket;
|
|
use IO::Select;
|
|
|
|
#local to module
|
|
my $callback;
|
|
my $timeout;
|
|
my $port;
|
|
my $debug;
|
|
my $ndebug = 0;
|
|
my $sock;
|
|
my $noclose;
|
|
my %sessiondata; #hold per session variables, in preparation for single-process strategy
|
|
my %pendingtransactions; #list of peers with callbacks, callback arguments, and timer expiry data
|
|
my $enable_cache="yes";
|
|
my $cache_dir = "/var/cache/xcat";
|
|
|
|
my $vlanid;
|
|
|
|
sub process_request
|
|
{
|
|
my $req = shift;
|
|
my $callback = shift;
|
|
my $reqcmd = shift;
|
|
|
|
if ($command eq 'mkvlan')
|
|
{
|
|
return mkvlan($req, $callback, $reqcmd);
|
|
}
|
|
elsif ($command eq 'chvlan')
|
|
{
|
|
return chvlan($req, $callback, $reqcmd);
|
|
}
|
|
elsif ($command eq 'lsvlan')
|
|
{
|
|
return lsvlan($req, $callback, $reqcmd);
|
|
}
|
|
elsif ($command eq 'rmvlan')
|
|
{
|
|
return rmvlan($req, $callback, $reqcmd);
|
|
}
|
|
}
|
|
|
|
sub mkvlan
|
|
{
|
|
my $request = shift;
|
|
my $callback = shift;
|
|
my $reqcmd = shift;
|
|
|
|
# Variables for this function
|
|
my $vlanid;
|
|
|
|
# A quick function for usage information for the command
|
|
my $mkvlan_usage = sub {
|
|
my $exitcode = shift @_;
|
|
my %rsp;
|
|
push @{$rsp{data}}, "Usage: mkvlan [vlanid] -n noderange [-t subnet | -m netmask | -i interface]";
|
|
push @{$rsp{data}}, " mkvlan [-?|-h|--help]";
|
|
if ($exitcode) { $rsp{errorcode} = $exitcode; }
|
|
$callback->(\%rsp);
|
|
};
|
|
|
|
# Go through the arguments, and get the relevant information
|
|
@ARGV = @{$req->{arg}};
|
|
|
|
#Detect if the first variable is a number or not, as it may be the vlanid
|
|
if ($ARGV[0] !~ /\D/) {$vlanid=$ARGV[0];}
|
|
|
|
GetOptions(
|
|
't=s' => \$subnet,
|
|
'm=s' => \$mask,
|
|
'n=s' => \$noderange,
|
|
'i=s' => \$interface,
|
|
'h|?|help' => \$help,
|
|
'v|version' => \$VERSION,
|
|
);
|
|
|
|
# Show usage info if help is defined
|
|
if ($help) { $mkvlan_usage->(0); return;}
|
|
|
|
# Node range must be defined
|
|
if (!defined($noderange)) { $mkvlan_usage->(1); return;}
|
|
|
|
# Display the version of the code
|
|
if ($VERSION) {
|
|
my %rsp;
|
|
my $version = xCAT::Utils->Version();
|
|
$rsp{data}->[0] = "$version";
|
|
$cb->(\%rsp);
|
|
return;
|
|
}
|
|
|
|
# put the nodes in an array
|
|
my @nodes = split(/,/,$noderange);
|
|
my $swtab = xCAT::Table->new('switch');
|
|
my $swhash = $swtab->getNodesAttribs(\@nodes,['switch','vlan','port','interface']);
|
|
|
|
foreach (@nodes)
|
|
{
|
|
my $node = $_;
|
|
|
|
foreach (@$swhash->{$node})
|
|
{
|
|
my $nswhash = $_;
|
|
|
|
my $switch = $nswhash->{'switch'};
|
|
my $vlan = $nswhash->{'vlan'};
|
|
my $port = $nswhash->{'port'};
|
|
my $swinterface = $nswhash->{'interface'};
|
|
|
|
if ($vlanid == "" || !defined($vlanid)) {$vlanid = $vlan;}
|
|
if ($interface == "" || !defined($interface)) { $interface = $swinterface;}
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sub lsvlan
|
|
{
|
|
my $request = shift;
|
|
my $callback = shift;
|
|
my $reqcmd = shift;
|
|
|
|
my @args = @{$req->{arg}} if(exists($req->{arg}));
|
|
|
|
# A quick function for usage information for the command
|
|
my $lsvlan_usage = sub {
|
|
my $exitcode = shift @_;
|
|
my %rsp;
|
|
push @{$rsp{data}}, "Usage: mkvlan vlanid";
|
|
push @{$rsp{data}}, " mkvlan [-?|-h|--help]";
|
|
if ($exitcode) { $rsp{errorcode} = $exitcode; }
|
|
$callback->(\%rsp);
|
|
};
|
|
|
|
# Go through the arguments, and get the relevant information
|
|
@ARGV = @{$req->{arg}};
|
|
|
|
#Detect if the first variable is a number or not, as it may be the vlanid
|
|
if ($ARGV[0] !~ /\D/) {$vlanid=$ARGV[0];}
|
|
|
|
GetOptions(
|
|
'h|?|help' => \$help,
|
|
);
|
|
|
|
# Show usage info if help is defined
|
|
if ($help) { $lsvlan_usage->(0); return; }
|
|
# Show usage if we have less then 2 arguments, or the vlan option is not defined
|
|
if (scalar(@ARGV)<1 || !defined($vlanid)) { $lsvlan_usage->(1); return; }
|
|
|
|
}
|
|
|
|
|
|
1;
|