From 85d05682d47a6a15421a18c6835461b3c44baf35 Mon Sep 17 00:00:00 2001 From: bybai Date: Mon, 8 Aug 2016 04:47:54 -0400 Subject: [PATCH 1/5] add list_netip rest api --- xCAT-server/lib/xcat/plugins/localrest.pm | 30 +++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/xCAT-server/lib/xcat/plugins/localrest.pm b/xCAT-server/lib/xcat/plugins/localrest.pm index 8474acad3..6c3b2ccdb 100644 --- a/xCAT-server/lib/xcat/plugins/localrest.pm +++ b/xCAT-server/lib/xcat/plugins/localrest.pm @@ -161,6 +161,36 @@ sub list_adapter { return $result; } +#------------------------------------------------------- + +=head3 handler to list network adapter first ip + + Subroutine to handle rest request + GET /localres/netip/ + + Usage example: + This function is called from handle_rest_request, + do not call it directly. +=cut + +#------------------------------------------------------- +sub list_netip { + my @params = @_; + my ($rsp, $result, $iface, $cmd); + if (!@params) { + $rsp->{data}->[0] = "Argmument error."; + xCAT::MsgUtils->message("E", $rsp, $::callback); + return 1; + } + $iface = shift @params; + $cmd = "ip addr show dev $iface |grep inet|head -1|awk '{print \$2}'|awk -F/ '{print \$1}'"; + + $result->[ 0 ] = xCAT::Utils->runcmd("$cmd", -1); + + return $result; +} + + #------------------------------------------------------- =head3 handler to download credential files From c309f372e0b668577978bb8551332df697cd6132 Mon Sep 17 00:00:00 2001 From: bybai Date: Wed, 10 Aug 2016 04:04:53 -0400 Subject: [PATCH 2/5] add get the first net ip mask --- xCAT-server/lib/xcat/plugins/localrest.pm | 29 +++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/xCAT-server/lib/xcat/plugins/localrest.pm b/xCAT-server/lib/xcat/plugins/localrest.pm index 6c3b2ccdb..c0e388710 100644 --- a/xCAT-server/lib/xcat/plugins/localrest.pm +++ b/xCAT-server/lib/xcat/plugins/localrest.pm @@ -190,6 +190,35 @@ sub list_netip { return $result; } +#------------------------------------------------------- + +=head3 handler to list network adapter first ip mask + + Subroutine to handle rest request + GET /localres/netmask/ + + Usage example: + This function is called from handle_rest_request, + do not call it directly. +=cut + +#------------------------------------------------------- +sub list_netmask { + my @params = @_; + my ($rsp, $iface, $netmask, $cmd, $prefix, $result); + if (!@params) { + $rsp->{data}->[0] = "Argmument error."; + xCAT::MsgUtils->message("E", $rsp, $::callback); + return 1; + } + $iface = shift @params; + $prefix = `ip addr show dev $iface |grep inet|head -1|awk '{print \$2}'`; + $cmd = "ipcalc -m $prefix"; + $netmask->[0] = xCAT::Utils->runcmd("$cmd", -1); + return $netmask; +} + + #------------------------------------------------------- From 3d60c48f852107827b7d019a5f346d8610a2baf6 Mon Sep 17 00:00:00 2001 From: bybai Date: Wed, 10 Aug 2016 04:04:53 -0400 Subject: [PATCH 3/5] get adapters and get adapter resource --- xCAT-server/lib/xcat/plugins/localrest.pm | 24 +++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/xCAT-server/lib/xcat/plugins/localrest.pm b/xCAT-server/lib/xcat/plugins/localrest.pm index 6c3b2ccdb..b69c8983e 100644 --- a/xCAT-server/lib/xcat/plugins/localrest.pm +++ b/xCAT-server/lib/xcat/plugins/localrest.pm @@ -134,7 +134,7 @@ sub handle_rest_request { =head3 handler to list network adapters Subroutine to handle rest request - GET /localres/adapter/ + GET /localres/adapters/ Usage example: This function is called from handle_rest_request, @@ -142,8 +142,8 @@ sub handle_rest_request { =cut #------------------------------------------------------- -sub list_adapter { - my ($rsp, $result, $i); +sub list_adapters { + my ($rsp, $result, $cmd, $i, $tmpres); if (!opendir DIR, "/sys/class/net") { $rsp->{data}->[0] = "Unable open /sys/class/net dir."; xCAT::MsgUtils->message("E", $rsp, $::callback); @@ -156,17 +156,19 @@ sub list_adapter { if ($item eq '.' || $item eq '..') { next; } - $result->[ $i++ ] = $item; + $cmd = "ifconfig| grep $item"; + $tmpres = xCAT::Utils->runcmd("$cmd", -1); + $result->[ $i++ ] = $tmpres; } - return $result; + return $result } #------------------------------------------------------- -=head3 handler to list network adapter first ip +=head3 handler to show network device resource Subroutine to handle rest request - GET /localres/netip/ + GET /localres/netres//detail Usage example: This function is called from handle_rest_request, @@ -174,7 +176,7 @@ sub list_adapter { =cut #------------------------------------------------------- -sub list_netip { +sub show_netres { my @params = @_; my ($rsp, $result, $iface, $cmd); if (!@params) { @@ -183,13 +185,11 @@ sub list_netip { return 1; } $iface = shift @params; - $cmd = "ip addr show dev $iface |grep inet|head -1|awk '{print \$2}'|awk -F/ '{print \$1}'"; - + $cmd = "ip addr show dev $iface"; $result->[ 0 ] = xCAT::Utils->runcmd("$cmd", -1); - return $result; -} +} #------------------------------------------------------- From da0040be726433c1517cae35b846610dd07385d5 Mon Sep 17 00:00:00 2001 From: bybai Date: Wed, 14 Sep 2016 05:36:18 -0400 Subject: [PATCH 4/5] polished --- xCAT-server/lib/xcat/plugins/localrest.pm | 74 +++++++++++++---------- 1 file changed, 42 insertions(+), 32 deletions(-) diff --git a/xCAT-server/lib/xcat/plugins/localrest.pm b/xCAT-server/lib/xcat/plugins/localrest.pm index 9bff14c7c..0414d9a54 100644 --- a/xCAT-server/lib/xcat/plugins/localrest.pm +++ b/xCAT-server/lib/xcat/plugins/localrest.pm @@ -143,49 +143,59 @@ sub handle_rest_request { #------------------------------------------------------- sub list_adapters { - my ($rsp, $result, $ifaces, $cmd, $i, $tmpres, @cmdres, $mac, $ip); - my (@cmdres, @iparr, @macarr); - if (!opendir DIR, "/sys/class/net") { - $rsp->{data}->[0] = "Unable open /sys/class/net dir."; - xCAT::MsgUtils->message("E", $rsp, $::callback); - return 1; - } - my @dir = readdir(DIR); - closedir(DIR); - $i = 0; - foreach my $item (@dir) { - if ($item eq '.' || $item eq '..' || $item eq 'lo') { - next; - } - $cmd = "ip addr show dev $item"; + my ($rsp, $result, $cmd, $tmpres, $vline); + my ($mac, $ip, $adapter, $preadapter, $samenic); + my (@cmdres, @origin, @eachline, @line); + $cmd = "ip -o addr"; @cmdres = xCAT::Utils->runcmd("$cmd", -1); - if (!@cmdres) { + if ($::RUNCMD_RC != 0) { $rsp->{data}->[0] = "Executing ip command failed."; xCAT::MsgUtils->message("E", $rsp, $::callback); return 1; } - # get net ip and mac - foreach my $in (@cmdres){ - if ( $in =~ "inet" ) { - @iparr = split(' ',$in); - if ( $iparr[0] =~ /^inet$/ ) { - $ip = $iparr[1]; - $tmpres->{'ip'} = $ip; - } + # sort ip -o addr result + for (my $i=0; $i<@cmdres; $i++) { + @eachline = split(' ',$cmdres[$i]); + if ( $eachline[1] =~ "lo" ) { + next; } - if ( $in =~ 'ether' ) { - @macarr = split(' ',$in); - $mac = $macarr[1]; - $tmpres->{'mac'} = $mac; + $adapter = $eachline[1]; + $adapter =~ s/://; + if ( !$preadapter ) { + $preadapter = $adapter; + } elsif ( $preadapter ne $adapter ) { + $samenic->{$preadapter} = "@origin"; + @origin = ""; + $preadapter = $adapter; + } + push (@origin, @eachline); + if ( @cmdres == $i+1 ) { + $samenic->{$adapter} = "@origin"; } } - $result->{$item} = $tmpres; - } + # get net ip and mac + foreach my $key (keys %{$samenic}){ + $vline=${$samenic}{$key}; + @line = split(' ',$vline); + my @tmpattr; + for (my $i=0; $i<@line; $i++) { + + if ( $line[$i] =~ /^inet$/ ) { + $ip = $line[$i+1]; + $tmpres->{'ip'} = $ip; + } + + if ( $line[$i] =~ 'ether' ) { + $mac = $line[$i+1]; + $tmpres->{'mac'} = $mac; + } + } + push (@tmpattr, $tmpres); + $result->{$key}=\@tmpattr; + } return $result - } - #------------------------------------------------------- =head3 handler to download credential files From e4bc58c961531a8970d979e8eaf2e05a68b35d82 Mon Sep 17 00:00:00 2001 From: bybai Date: Thu, 22 Sep 2016 04:53:08 -0400 Subject: [PATCH 5/5] modify output format --- xCAT-server/lib/xcat/plugins/localrest.pm | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/xCAT-server/lib/xcat/plugins/localrest.pm b/xCAT-server/lib/xcat/plugins/localrest.pm index 0414d9a54..24e5a6b4f 100644 --- a/xCAT-server/lib/xcat/plugins/localrest.pm +++ b/xCAT-server/lib/xcat/plugins/localrest.pm @@ -143,9 +143,9 @@ sub handle_rest_request { #------------------------------------------------------- sub list_adapters { - my ($rsp, $result, $cmd, $tmpres, $vline); + my ($rsp, $cmd, $tmpres, $vline); my ($mac, $ip, $adapter, $preadapter, $samenic); - my (@cmdres, @origin, @eachline, @line); + my (@cmdres, @origin, @eachline, @line, @result); $cmd = "ip -o addr"; @cmdres = xCAT::Utils->runcmd("$cmd", -1); if ($::RUNCMD_RC != 0) { @@ -177,7 +177,7 @@ sub list_adapters { foreach my $key (keys %{$samenic}){ $vline=${$samenic}{$key}; @line = split(' ',$vline); - my @tmpattr; + $tmpres->{'name'} = $key; for (my $i=0; $i<@line; $i++) { if ( $line[$i] =~ /^inet$/ ) { @@ -190,12 +190,13 @@ sub list_adapters { $tmpres->{'mac'} = $mac; } } - push (@tmpattr, $tmpres); - $result->{$key}=\@tmpattr; + push (@result, $tmpres); } - return $result + return \@result; } + + #------------------------------------------------------- =head3 handler to download credential files