From ec4e31ce464e4b7a87f771874154c2bda59fc8e0 Mon Sep 17 00:00:00 2001 From: xuweibj Date: Fri, 15 Dec 2017 16:09:39 +0800 Subject: [PATCH] Task 4472, Allow rspconfig to set NTP servers for OpenBMC (#4493) * Task 4472, rspconfig ntpservers for OpenBMC --- xCAT-server/lib/xcat/plugins/openbmc.pm | 141 ++++++++++++------ xCAT-test/autotest/testcase/rspconfig/cases0 | 15 ++ .../testcase/rspconfig/rspconfig_ntp.sh | 53 +++++++ 3 files changed, 164 insertions(+), 45 deletions(-) create mode 100755 xCAT-test/autotest/testcase/rspconfig/rspconfig_ntp.sh diff --git a/xCAT-server/lib/xcat/plugins/openbmc.pm b/xCAT-server/lib/xcat/plugins/openbmc.pm index 3977f16f6..28ff43deb 100644 --- a/xCAT-server/lib/xcat/plugins/openbmc.pm +++ b/xCAT-server/lib/xcat/plugins/openbmc.pm @@ -366,6 +366,13 @@ my %status_info = ( RSPCONFIG_GET_RESPONSE => { process => \&rspconfig_response, }, + RSPCONFIG_GET_NIC_REQUEST => { + method => "GET", + init_url => "$openbmc_project_url/network/enumerate", + }, + RSPCONFIG_GET_NIC_RESPONSE => { + process => \&rspconfig_response, + }, RSPCONFIG_SET_PASSWD_REQUEST => { method => "POST", init_url => "/xyz/openbmc_project/user/root/action/SetPassword", @@ -379,6 +386,11 @@ my %status_info = ( init_url => "$openbmc_project_url/network/config/attr/HostName", data => "[]", }, + RSPCONFIG_SET_NTPSERVERS_REQUEST => { + method => "PUT", + init_url => "$openbmc_project_url/network/#NIC#/attr/NTPServers", + data => "[]", + }, RSPCONFIG_SET_RESPONSE => { process => \&rspconfig_response, }, @@ -1049,8 +1061,8 @@ sub parse_args { my $key = $1; my $value = $2; return ([ 1, "Changing ipsrc value is currently not supported." ]) if ($key eq "ipsrc"); - return ([ 1, "Unsupported command: $command $key" ]) unless ($key =~ /^ip$|^netmask$|^gateway$|^hostname$|^vlan$|^admin_passwd$/); - return ([ 1, "The option '$key' can not work with other options." ]) if ($key =~ /^hostname$|^admin_passwd$/ and $num_subcommand > 1); + return ([ 1, "Unsupported command: $command $key" ]) unless ($key =~ /^ip$|^netmask$|^gateway$|^hostname$|^vlan$|^admin_passwd$|^ntpservers$/); + return ([ 1, "The option '$key' can not work with other options." ]) if ($key =~ /^hostname$|^admin_passwd$|^ntpservers$/ and $num_subcommand > 1); if ($key eq "admin_passwd") { my $comma_num = $value =~ tr/,/,/; return ([ 1, "Invalid parameter for option $key: $value" ]) if ($comma_num != 1); @@ -1060,7 +1072,7 @@ sub parse_args { } my $nodes_num = @$noderange; - return ([ 1, "Invalid parameter for option $key" ]) unless ($value); + return ([ 1, "Invalid parameter for option $key" ]) if (!$value and $key ne ("ntpservers")); return ([ 1, "Invalid parameter for option $key: $value" ]) if (($key eq "netmask") and !xCAT::NetworkUtils->isIpaddr($value)); return ([ 1, "Invalid parameter for option $key: $value" ]) if (($key eq "gateway") and ($value !~ "0.0.0.0" and !xCAT::NetworkUtils->isIpaddr($value))); if ($key eq "ip") { @@ -1078,7 +1090,7 @@ sub parse_args { $all_subcommand .= $key . ","; } $setorget = "set"; - } elsif ($subcommand =~ /^ip$|^netmask$|^gateway$|^hostname$|^vlan$|^ipsrc$/) { + } elsif ($subcommand =~ /^ip$|^netmask$|^gateway$|^hostname$|^vlan$|^ipsrc$|^ntpservers$/) { return ([ 1, "Can not set and query OpenBMC information at the same time" ]) if ($setorget and $setorget eq "set"); $setorget = "get"; } elsif ($subcommand =~ /^sshcfg$/) { @@ -1470,6 +1482,19 @@ sub parse_command_status { $status_info{RSPCONFIG_GET_RESPONSE}{argv} = "hostname"; return 0; } + if ($subcommand =~ /^ntpservers=(.*)/) { + $next_status{LOGIN_RESPONSE} = "RSPCONFIG_GET_NIC_REQUEST"; + $next_status{RSPCONFIG_GET_NIC_REQUEST} = "RSPCONFIG_GET_NIC_RESPONSE"; + $next_status{RSPCONFIG_GET_NIC_RESPONSE} = "RSPCONFIG_SET_NTPSERVERS_REQUEST"; + $next_status{RSPCONFIG_SET_NTPSERVERS_REQUEST} = "RSPCONFIG_SET_RESPONSE"; + $next_status{RSPCONFIG_SET_RESPONSE} = "RSPCONFIG_GET_REQUEST"; + $next_status{RSPCONFIG_GET_REQUEST} = "RSPCONFIG_GET_RESPONSE"; + + $status_info{RSPCONFIG_GET_RESPONSE}{argv} = "ntpservers"; + $status_info{RSPCONFIG_SET_RESPONSE}{argv} = "NTPServers"; + $status_info{RSPCONFIG_SET_NTPSERVERS_REQUEST}{data} = "[\"$1\"]"; + return 0; + } } $subcommand = $$subcommands[0]; @@ -1539,7 +1564,7 @@ sub parse_command_status { my $type = "obj"; my %tmp_hash = (); foreach $subcommand (@$subcommands) { - if ($subcommand =~ /^ip$|^netmask$|^gateway$|^hostname$|^vlan$|^ipsrc$/) { + if ($subcommand =~ /^ip$|^netmask$|^gateway$|^hostname$|^vlan$|^ipsrc$|^ntpservers$/) { $type = "get"; push @options, $subcommand; } elsif ($subcommand =~ /^(\w+)=(.+)/) { @@ -2900,11 +2925,11 @@ sub rspconfig_response { my $response_info; $response_info = decode_json $response->content if ($response); - if ($node_info{$node}{cur_status} eq "RSPCONFIG_GET_RESPONSE") { + if ($node_info{$node}{cur_status} eq "RSPCONFIG_GET_RESPONSE" or $node_info{$node}{cur_status} eq "RSPCONFIG_GET_NIC_RESPONSE") { my $hostname = ""; my $default_gateway = "n/a"; my %nicinfo = (); - my $error; + my $multiple_error = ""; my @output; my $grep_string = $status_info{RSPCONFIG_GET_RESPONSE}{argv}; foreach my $key_url (keys %{$response_info->{data}}) { @@ -2935,79 +2960,99 @@ sub rspconfig_response { my $nic = $path; $nic =~ s/(.*\/)//g; unless (defined($nicinfo{$nic}{address})) { - $nicinfo{$nic}{address} = "n/a"; - $nicinfo{$nic}{gateway} = "n/a"; - $nicinfo{$nic}{ipsrc} = "n/a"; - $nicinfo{$nic}{netmask} = "n/a"; - $nicinfo{$nic}{prefix} = "n/a"; + $nicinfo{$nic}{address} = (); + $nicinfo{$nic}{gateway} = (); + $nicinfo{$nic}{ipsrc} = (); + $nicinfo{$nic}{netmask} = (); + $nicinfo{$nic}{prefix} = (); $nicinfo{$nic}{vlan} = "Disable"; } if (defined($content{Address}) and $content{Address}) { - unless ($nicinfo{$nic}{address} =~ /n\/a/) { - # We have already processed an entry with adapter information. - # This must be a second entry. Display an error. Currently only supporting - # an adapter with a single IP address set. - $error = "Interfaces with multiple IP addresses are not supported"; - $node_info{$node}{cur_status} = ""; - # Terminate loop on this error unless we are looking for hostname to display - last unless ($grep_string =~ /(.*)hostname(.*)/); + if ($content{Address} eq $node_info{$node}{bmcip} and $node_info{$node}{cur_status} eq "RSPCONFIG_GET_NIC_RESPONSE") { + $status_info{RSPCONFIG_SET_NTPSERVERS_REQUEST}{init_url} =~ s/#NIC#/$nic/g; + if ($next_status{"RSPCONFIG_GET_NIC_RESPONSE"}) { + $node_info{$node}{cur_status} = $next_status{"RSPCONFIG_GET_NIC_RESPONSE"}; + gen_send_request($node); + return; + } } - $nicinfo{$nic}{address} = $content{Address}; + if ($nicinfo{$nic}{address}) { + $multiple_error = "Interfaces with multiple IP addresses are not supported"; + } + push @{ $nicinfo{$nic}{address} }, $content{Address}; } if (defined($content{Gateway}) and $content{Gateway}) { - $nicinfo{$nic}{gateway} = $content{Gateway}; + push @{ $nicinfo{$nic}{gateway} }, $content{Gateway}; } if (defined($content{PrefixLength}) and $content{PrefixLength}) { - $nicinfo{$nic}{prefix} = $content{PrefixLength}; + push @{ $nicinfo{$nic}{prefix} }, $content{PrefixLength}; } if (defined($content{Origin})) { - $nicinfo{$nic}{ipsrc} = $content{Origin}; - $nicinfo{$nic}{ipsrc} =~ s/^.*\.(\w+)/$1/; + my $ipsrc_tmp = $content{Origin}; + $ipsrc_tmp =~ s/^.*\.(\w+)/$1/; + push @{ $nicinfo{$nic}{ipsrc} }, $ipsrc_tmp; } - + if (defined($response_info->{data}->{$path}->{Id})) { $nicinfo{$nic}{vlan} = $response_info->{data}->{$path}->{Id}; } + + if (defined($response_info->{data}->{$path}->{NTPServers})) { + $nicinfo{$nic}{ntpservers} = join(",", @{ $response_info->{data}->{$path}->{NTPServers} }); + } } } - if ($grep_string =~ /(.*)hostname(.*)/) { - xCAT::SvrUtils::sendmsg("BMC hostname: $hostname", $callback, $node); - unless ($1 or $2) { - $wait_node_num--; - return; - } - } + if (scalar (keys %nicinfo) == 0) { - $error = "No valid BMC network information"; + my $error = "No valid BMC network information"; + xCAT::SvrUtils::sendmsg([1, "$error"], $callback, $node); $node_info{$node}{cur_status} = ""; - } - if ($error) { - xCAT::SvrUtils::sendmsg([1,"$error"], $callback, $node); } else { my @address = (); my @ipsrc = (); my @netmask = (); my @gateway = (); my @vlan = (); + my @ntpservers = (); my @nics = keys %nicinfo; foreach my $nic (@nics) { my $addon_info = ''; if ($#nics > 1) { $addon_info = " for $nic"; } - push @address, "BMC IP$addon_info: $nicinfo{$nic}{address}"; - push @ipsrc, "BMC IP Source$addon_info: $nicinfo{$nic}{ipsrc}"; + + if ($nicinfo{$nic}{ntpservers}) { + push @ntpservers, "BMC NTP Servers$addon_info: $nicinfo{$nic}{ntpservers}"; + } else { + push @ntpservers, "BMC NTP Servers$addon_info: None"; + } + + next if ($multiple_error); + + push @address, "BMC IP$addon_info: ${ $nicinfo{$nic}{address} }[0]"; + push @ipsrc, "BMC IP Source$addon_info: ${ $nicinfo{$nic}{ipsrc} }[0]"; if ($nicinfo{$nic}{address}) { - my $mask_shift = 32 - $nicinfo{$nic}{prefix}; - my $decimal_mask = (2 ** $nicinfo{$nic}{prefix} - 1) << $mask_shift; + my $mask_shift = 32 - ${ $nicinfo{$nic}{prefix} }[0]; + my $decimal_mask = (2 ** ${ $nicinfo{$nic}{prefix} }[0] - 1) << $mask_shift; push @netmask, "BMC Netmask$addon_info: " . join('.', unpack("C4", pack("N", $decimal_mask))); } - push @gateway, "BMC Gateway$addon_info: $nicinfo{$nic}{gateway} (default: $default_gateway)"; + push @gateway, "BMC Gateway$addon_info: ${ $nicinfo{$nic}{gateway} }[0] (default: $default_gateway)"; push @vlan, "BMC VLAN ID$addon_info: $nicinfo{$nic}{vlan}"; } + my $mul_out = 0; foreach my $opt (split /,/,$grep_string) { + if ($opt eq "hostname") { + push @output, "BMC Hostname: $hostname"; + } elsif ($opt eq "ntpservers") { + push @output, @ntpservers; + } + + if ($multiple_error and ($opt =~ /^ip$|^ipsrc$|^netmask$|^gateway$|^vlan$/)) { + $mul_out = 1; + next; + } if ($opt eq "ip") { push @output, @address; } elsif ($opt eq "ipsrc") { @@ -3020,7 +3065,13 @@ sub rspconfig_response { push @output, @vlan; } } + xCAT::SvrUtils::sendmsg("$_", $callback, $node) foreach (@output); + if ($multiple_error and $mul_out) { + xCAT::SvrUtils::sendmsg([1, "$multiple_error"], $callback, $node); + $wait_node_num--; + return; + } if ($grep_string eq "all") { # If all current values equal the input, just print out message @@ -3033,9 +3084,9 @@ sub rspconfig_response { my ($check_ip,$check_netmask,$check_gateway) = @checks; my $the_nic_to_config = undef; foreach my $nic (@nics) { - my $address = $nicinfo{$nic}{address}; - my $prefix = $nicinfo{$nic}{prefix}; - my $gateway = $nicinfo{$nic}{gateway}; + my $address = ${ $nicinfo{$nic}{address} }[0]; + my $prefix = ${ $nicinfo{$nic}{prefix} }[0]; + my $gateway = ${ $nicinfo{$nic}{gateway} }[0]; if ($check_ip eq $address and $check_netmask eq $prefix and $check_gateway eq $gateway) { if (($check_vlan and $check_vlan eq $nicinfo{$nic}{vlan}) or !$check_vlan) { $next_status{ $node_info{$node}{cur_status} } = "RSPCONFIG_PRINT_BMCINFO"; diff --git a/xCAT-test/autotest/testcase/rspconfig/cases0 b/xCAT-test/autotest/testcase/rspconfig/cases0 index da4ddb70f..266ef319c 100644 --- a/xCAT-test/autotest/testcase/rspconfig/cases0 +++ b/xCAT-test/autotest/testcase/rspconfig/cases0 @@ -41,6 +41,21 @@ check:rc==0 check:output=~__GETNODEATTR($$CN,hcp)__: \w+ end +start:rspconfig_list_ntpservers +description: rspconfig list ntpservers info +Attribute: $$CN-The operation object of rspconfig command +cmd:rspconfig $$CN ntpservers +check:rc==0 +check:output=~$$CN: BMC NTP Servers +end + +start:rspconfig_set_ntpservers +description: rspconfig set ntpservers +Attribute: $$CN-The operation object of rspconfig command +cmd:/opt/xcat/share/xcat/tools/autotest/testcase/rspconfig/rspconfig_ntp.sh $$CN $$MN +check:rc==0 +end + start:rspconfig_set_ip description:rspconfig change openbmc ip Attribute: $$CN-The operation object of rspconfig command diff --git a/xCAT-test/autotest/testcase/rspconfig/rspconfig_ntp.sh b/xCAT-test/autotest/testcase/rspconfig/rspconfig_ntp.sh new file mode 100755 index 000000000..13c53fa61 --- /dev/null +++ b/xCAT-test/autotest/testcase/rspconfig/rspconfig_ntp.sh @@ -0,0 +1,53 @@ +#!/bin/bash + +cn=$1 +mn=$2 + +ntpservers=`rspconfig $cn ntpservers | awk -F":" '{print $3}' | sed 's/^ //;s/ $//'` +if [ $? -ne 0 ]; then + echo "rspconfig $cn ntpservers failed" + exit 1 +fi + +echo "The original BMC NTP Servers is $ntpservers" + +if [ $ntpservers != "None" ]; then + new_ntpservers=$ntpservers"_test" +else + new_ntpservers=$mn +fi + +output=`rspconfig $cn ntpservers=$new_ntpservers` +if [ $? -ne 0 ]; then + echo "rspconfig $cn ntpservers=$new_ntpservers failed" + exit 1 +fi + +if [[ $output =~ "$cn: BMC NTP Servers" ]] && [[ $output =~ "$new_ntpservers" ]]; then + echo "Setting NTPServers as $new_ntpservers success" +else + echo "Setting NTPServers as $new_ntpservers failed, the output is $output" + exit 1 +fi + +echo "To clear environment" + +if [ $ntpservers != "None" ]; then + original_ntpservers="$ntpservers" +else + original_ntpservers="" +fi + +output=`rspconfig $cn ntpservers=$original_ntpservers` +if [ $? -ne 0 ]; then + echo "rspconfig $cn ntpservers=$ntpservers failed when clearing environment" + exit 1 +fi + +if [[ "$output" =~ "$cn: BMC NTP Servers" ]] && [[ $output =~ "$ntpservers" ]]; then + echo "Setting NTPServers as $ntpservers success when clearing environment" + exit 0 +fi + +echo "Setting NTPServers as $ntpservers failed when clearing environment" +exit 1