2
0
mirror of https://github.com/xcat2/xcat-core.git synced 2025-05-22 03:32:04 +00:00

Merge pull request #4944 from zet809/fix_issue_4856

fix issue 4856: rewrite parse_args for rspconfig
This commit is contained in:
xuweibj 2018-03-15 15:52:06 +08:00 committed by GitHub
commit 36146ce0dd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -16,7 +16,6 @@ use Getopt::Long;
use xCAT::Usage;
use xCAT::SvrUtils;
use xCAT::OPENBMC;
use xCAT_plugin::openbmc;
#-------------------------------------------------------
@ -146,6 +145,17 @@ 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 %rsp_set_valid_values = (
autoreboot => "0|1",
bootmode => "regular|safe|setup",
powersupplyredundancy => "disabled|enabled",
powerrestorepolicy => "restore|always_on|always_off",
timesyncmethod => "ntp|manual",
);
my @rspconfig_get_options = (@rsp_common_options, qw/ipsrc sshcfg gard dump/);
#-------------------------------------------------------
=head3 parse_args
@ -169,7 +179,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];
@ -251,7 +261,108 @@ 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 '' and ($key ne "ntpservers")) {
return ([1, "Invalid parameter for option $key"]);
} elsif ($set and $value ne '' and exists($rsp_set_valid_values{$key})) {
unless ($value =~ /^($rsp_set_valid_values{$key})$/) {
return([1, "Invalid value '$value' for '$key', Valid values: " . join(',', split('\|',$rsp_set_valid_values{$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");
return ([ 1, "dump $dump_option must be issued without other options." ]) if ($num_subcommand > 3);
} 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");
return ([ 1, "dump $dump_option must be issued without other options." ]) if ($num_subcommand > 3);
} elsif ($dump_option =~ /^-l$|^--list$|^-g$|^--generate$/) {
return ([ 1, "dump $dump_option must be issued without other options." ]) if ($num_subcommand > 2);
} elsif ($dump_option) {
return ([ 1, "Invalid parameter for $command $dump_option" ]);
}
return;
}
}
}
if ($set and scalar(keys %set_net_info) > 0) {
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=(.*)/) {