From 84ad957d3f305c955e2c5021906617e48fafc67f Mon Sep 17 00:00:00 2001 From: ertaozh Date: Wed, 14 Mar 2018 03:56:44 -0400 Subject: [PATCH 1/3] fix issue 4856: rewrite parse_args for rspconfig --- xCAT-server/lib/xcat/plugins/openbmc2.pm | 114 ++++++++++++++++++++++- 1 file changed, 111 insertions(+), 3 deletions(-) diff --git a/xCAT-server/lib/xcat/plugins/openbmc2.pm b/xCAT-server/lib/xcat/plugins/openbmc2.pm index f4c750777..ebc5e26a9 100644 --- a/xCAT-server/lib/xcat/plugins/openbmc2.pm +++ b/xCAT-server/lib/xcat/plugins/openbmc2.pm @@ -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=(.*)/) { From 3dc4b0a60aedbdb6b33d20ef4d9be1a3d188b8b3 Mon Sep 17 00:00:00 2001 From: ertaozh Date: Wed, 14 Mar 2018 05:27:32 -0400 Subject: [PATCH 2/3] update parse_args based on comments --- xCAT-server/lib/xcat/plugins/openbmc2.pm | 37 ++++++++++-------------- 1 file changed, 15 insertions(+), 22 deletions(-) diff --git a/xCAT-server/lib/xcat/plugins/openbmc2.pm b/xCAT-server/lib/xcat/plugins/openbmc2.pm index ebc5e26a9..162a5bf02 100644 --- a/xCAT-server/lib/xcat/plugins/openbmc2.pm +++ b/xCAT-server/lib/xcat/plugins/openbmc2.pm @@ -89,10 +89,6 @@ 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"); @@ -272,7 +268,7 @@ sub parse_args { } if ($set and $get) { return ([1, "Can not set and query OpenBMC information at the same time"]); - } elsif ($set and $value eq '') { + } elsif ($set and $value eq '' and ($key ne "ntpservers")) { return ([1, "Invalid parameter for option $key"]); } if (($set and !grep /$key/, @rspconfig_set_options) or @@ -282,32 +278,27 @@ sub parse_args { 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 =~ /(.*),(.*)/) { + } 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") { + } 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") { + } 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") { + } elsif ($key eq "vlan") { $set_net_info{"vlan"} = 1; - } - elsif ($key eq "ip") { + } 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" ]); @@ -322,32 +313,34 @@ sub parse_args { } else { if ($key eq "sshcfg" and $num_subcommand > 1) { return ([ 1, "Configure sshcfg must be issued without other options." ]); - } - elsif ($key eq "gard") { + } 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") { + } 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"); - } elsif ($dump_option and $dump_option !~ /^-l$|^--list$|^-g$|^--generate$/) { + 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) { + 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" ]); From 75940bfda15951d73f60d119b96dda9a5f2d3737 Mon Sep 17 00:00:00 2001 From: ertaozh Date: Thu, 15 Mar 2018 03:10:06 -0400 Subject: [PATCH 3/3] add value check for rspconfig options powersupplyredundancy,... --- xCAT-server/lib/xcat/plugins/openbmc2.pm | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/xCAT-server/lib/xcat/plugins/openbmc2.pm b/xCAT-server/lib/xcat/plugins/openbmc2.pm index 162a5bf02..f5e4a8920 100644 --- a/xCAT-server/lib/xcat/plugins/openbmc2.pm +++ b/xCAT-server/lib/xcat/plugins/openbmc2.pm @@ -144,7 +144,13 @@ sub process_request { 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/); #------------------------------------------------------- @@ -270,6 +276,10 @@ sub parse_args { 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)) {