From ba81e050ce06007215e07bd7d17b2fb40783da1a Mon Sep 17 00:00:00 2001 From: sakolish Date: Wed, 19 Mar 2008 12:44:22 +0000 Subject: [PATCH] Added -w option to write MAC-address to xCAT 'mac' database git-svn-id: https://svn.code.sf.net/p/xcat/code/xcat-core/trunk@820 8638fb3e-16cb-4fca-ae20-7b5d299a9bcd --- perl-xCAT-2.0/xCAT/PPCmac.pm | 77 +++++++++++++++++++++++++++++------- 1 file changed, 63 insertions(+), 14 deletions(-) diff --git a/perl-xCAT-2.0/xCAT/PPCmac.pm b/perl-xCAT-2.0/xCAT/PPCmac.pm index 819e840f8..5517e94ca 100644 --- a/perl-xCAT-2.0/xCAT/PPCmac.pm +++ b/perl-xCAT-2.0/xCAT/PPCmac.pm @@ -25,14 +25,15 @@ sub parse_args { return( [ $_[0], "getmacs -h|--help", "getmacs -v|--version", - "getmacs [-V|--verbose] noderange [-c][-S server -G gateway -C client]", + "getmacs [-V|--verbose] noderange [-c][-w][-S server -G gateway -C client]", " -h writes usage information to standard output", " -v displays command version", " -c colon seperated output", " -C IP of the partition", " -G Gateway IP of the partition specified", " -S Server IP to ping", - " -V verbose output" ]); + " -V verbose output", + " -w writes first adapter MAC to the xCAT database"]); }; ############################################# # Process command-line arguments @@ -50,7 +51,7 @@ sub parse_args { $Getopt::Long::ignorecase = 0; Getopt::Long::Configure( "bundling" ); - if ( !GetOptions( \%opt, qw(h|help V|Verbose v|version C=s G=s S=s c) )) { + if ( !GetOptions( \%opt,qw(h|help V|Verbose v|version C=s G=s S=s c w))) { return( usage() ); } #################################### @@ -234,8 +235,10 @@ sub ivm_getmacs { last; } } - return( [$Rc,$result] ); - + ###################################### + # Split results into array + ###################################### + return( [$Rc, split( /\n/, $result)] ); } @@ -249,6 +252,7 @@ sub getmacs { my $exp = shift; my $opt = $request->{opt}; my $hwtype = @$exp[2]; + my $delim = ( exists( $opt->{c} )) ? ":" : " "; my $result; my $node; @@ -316,6 +320,11 @@ sub getmacs { # Form string from array results ################################## if ( exists($request->{verbose}) ) { + if ( $Rc == SUCCESS ) { + if ( exists( $opt->{w} )) { + writemac( $name, $delim, $result ); + } + } return( [[$name,join( '', @$result ),$Rc]] ); } ################################## @@ -327,14 +336,7 @@ sub getmacs { } return( [[$name,join( '', @$result ),$Rc]] ); } - ################################## - # Split results into array - ################################## - if ( $hwtype eq "ivm" ) { - my $data = @$result[0]; - @$result = split /\n/, $data; - } - ################################## + ##################################### # lpar_netboot returns: # # # Connecting to lpar4\n @@ -349,19 +351,66 @@ sub getmacs { # ##################################### my $values; + foreach ( @$result ) { if ( /^#\s?Type/ ) { $values.= "\n$_\n"; - } elsif ( /^ent:?/ ) { + } elsif ( /^ent$delim/ ) { $values.= "$_\n"; } } + ##################################### + # Write first adapter MAC to database + ##################################### + if ( exists( $opt->{w} )) { + writemac( $name, $delim, $result ); + } return( [[$name,$values,$Rc]] ); } +########################################################################## +# Writes the first adapter MAC to the database +########################################################################## +sub writemac { + + my $name = shift; + my $delim = shift; + my $data = shift; + my $values; + + ##################################### + # Find first adapter + ##################################### + foreach ( @$data ) { + if ( /^ent$delim/ ) { + $values = $_; + } + } + ##################################### + # Get adapter mac + ##################################### + my ($k,$u); + my @fields = split $delim, $values; + my $mac = $fields[2]; + + ##################################### + # Write adapter mac to database + ##################################### + my $tab = xCAT::Table->new( "mac", -create=>1, -autocommit=>1 ); + if ( !$tab ) { + return( [[$name,"Error opening 'mac'",RC_ERROR]] ); + } + $k->{node} = $name; + $u->{mac} = $mac; + my $d = $tab->setAttribs( $k,$u ); + return undef; +} + + 1; +