2
0
mirror of https://github.com/xcat2/xcat-core.git synced 2025-06-16 11:20:32 +00:00

fix issue 4856: rewrite parse_args for rspconfig

This commit is contained in:
ertaozh
2018-03-14 03:56:44 -04:00
parent 06a3db57c8
commit 84ad957d3f

View File

@ -16,7 +16,6 @@ use Getopt::Long;
use xCAT::Usage;
use xCAT::SvrUtils;
use xCAT::OPENBMC;
use xCAT_plugin::openbmc;
#-------------------------------------------------------
@ -90,6 +89,10 @@ sub preprocess_request {
$callback->({ errorcode => [$parse_result->[0]], data => [$error_data] });
$request = {};
return;
} elsif ($command eq 'rspconfig') {
$callback->({ data => ["rspconfig is fine"]});
$request = {};
return;
}
my $sn = xCAT::ServiceNodeUtils->get_ServiceNode($noderange, "xcat", "MN");
@ -142,6 +145,11 @@ sub process_request {
xCAT::OPENBMC::wait_agent($pid, $callback);
}
my @rsp_common_options = qw/autoreboot bootmode powersupplyredundancy powerrestorepolicy timesyncmethod
ip netmask gateway hostname vlan ntpservers/;
my @rspconfig_set_options = (@rsp_common_options, qw/admin_passwd/);
my @rspconfig_get_options = (@rsp_common_options, qw/ipsrc sshcfg gard dump/);
#-------------------------------------------------------
=head3 parse_args
@ -166,7 +174,7 @@ sub parse_args {
if (scalar(@ARGV) >= 2 and ($command =~ /rbeacon|rinv|rpower|rvitals/)) {
return ([ 1, "Only one option is supported at the same time for $command" ]);
} elsif (scalar(@ARGV) == 0 and $command =~ /rbeacon|rpower|rflash/) {
} elsif (scalar(@ARGV) == 0 and $command =~ /rbeacon|rspconfig|rpower|rflash/) {
return ([ 1, "No option specified for $command" ]);
} else {
$subcommand = $ARGV[0];
@ -248,7 +256,107 @@ sub parse_args {
return ([ 1, "Unsupported command: $command $subcommand" ]);
}
} elsif ($command eq 'rspconfig') {
xCAT_plugin::openbmc::parse_args('rspconfig', $extrargs, $noderange);
my $num_subcommand = @ARGV;
my ($set, $get);
my $all_subcommand = "";
my %set_net_info = ();
foreach $subcommand (@ARGV) {
my ($key, $value);
if ($subcommand =~ /^(\w+)=(.*)/) {
$key = $1;
$value = $2;
$set = 1;
} else {
$key = $subcommand;
$get = 1;
}
if ($set and $get) {
return ([1, "Can not set and query OpenBMC information at the same time"]);
} elsif ($set and $value eq '') {
return ([1, "Invalid parameter for option $key"]);
}
if (($set and !grep /$key/, @rspconfig_set_options) or
($get and !grep /$key/, @rspconfig_get_options)) {
return ([1, "Unsupported command: $command $subcommand"]);
}
if ($set) {
if ($key =~ /^hostname$|^admin_passwd$|^ntpservers$/ and $num_subcommand > 1) {
return([1, "The option '$key' can not work with other options"]);
}
elsif ($key eq "admin_passwd") {
if ($value =~ /(.*),(.*)/) {
if ($1 eq '' or $2 eq '') {
return([1, "Invalid parameter for option $key: $value"]);
}
} else {
return([1, "Invalid parameter for option $key: $value"]);
}
}
elsif ($key eq "netmask") {
if (!xCAT::NetworkUtils->isIpaddr($value)) {
return ([ 1, "Invalid parameter for option $key: $value" ]);
}
$set_net_info{"netmask"} = 1;
}
elsif ($key eq "gateway") {
if ($value ne "0.0.0.0" and !xCAT::NetworkUtils->isIpaddr($value)) {
return ([ 1, "Invalid parameter for option $key: $value" ]);
}
$set_net_info{"gateway"} = 1;
}
elsif ($key eq "vlan") {
$set_net_info{"vlan"} = 1;
}
elsif ($key eq "ip") {
if ($value ne "dhcp") {
if (@$noderange > 1) {
return ([ 1, "Can not configure more than 1 nodes' ip at the same time" ]);
} elsif (!xCAT::NetworkUtils->isIpaddr($value)) {
return ([ 1, "Invalid parameter for option $key: $value" ]);
}
$set_net_info{"ip"} = 1;
} elsif($num_subcommand > 1) {
return ([ 1, "Setting ip=dhcp must be issued without other options." ]);
}
}
} else {
if ($key eq "sshcfg" and $num_subcommand > 1) {
return ([ 1, "Configure sshcfg must be issued without other options." ]);
}
elsif ($key eq "gard") {
if ($num_subcommand > 2) {
return ([ 1, "Clear GARD cannot be issued with other options." ]);
} elsif (!defined($ARGV[1]) or $ARGV[1] !~ /^-c$|^--clear$/) {
return ([ 1, "Invalid parameter for $command $key" ]);
}
return;
}
elsif ($key eq "dump") {
my $dump_option = "";
$dump_option = $ARGV[1] if (defined $ARGV[1]);
if ($dump_option =~ /^-d$|^--download$/) {
return ([ 1, "No dump file ID specified" ]) unless ($ARGV[2]);
return ([ 1, "Invalid parameter for $command $key $dump_option $ARGV[2]" ]) if ($ARGV[2] !~ /^\d*$/ and $ARGV[2] ne "all");
} elsif ($dump_option =~ /^-c$|^--clear$/) {
return ([ 1, "No dump file ID specified. To clear all, specify 'all'." ]) unless ($ARGV[2]);
return ([ 1, "Invalid parameter for $command $key $dump_option $ARGV[2]" ]) if ($ARGV[2] !~ /^\d*$/ and $ARGV[2] ne "all");
} elsif ($dump_option and $dump_option !~ /^-l$|^--list$|^-g$|^--generate$/) {
return ([ 1, "Invalid parameter for $command $dump_option" ]);
}
return;
}
}
}
if ($set) {
if (!exists($set_net_info{"ip"}) or !exists($set_net_info{"netmask"}) or !exists($set_net_info{"gateway"})) {
if (exists($set_net_info{"vlan"})) {
return ([ 1, "VLAN must be configured with IP, netmask and gateway" ]);
} else {
return ([ 1, "IP, netmask and gateway must be configured together." ]);
}
}
}
} elsif ($command eq "reventlog") {
$subcommand = "all" if (!defined($ARGV[0]));
if ($subcommand =~ /^resolved=(.*)/) {