From 98785fbddf464c9983d294fd38033394554bbfa5 Mon Sep 17 00:00:00 2001 From: chenglch Date: Thu, 19 Jan 2017 14:24:48 +0800 Subject: [PATCH] Make nics attribute inhertable As the attribute value in the nics table can not match the regular expresion format supported by the table library, this patch add the logic in the def utils to handle the regular expresion. close-issue: #2412 --- perl-xCAT/xCAT/DBobjUtils.pm | 9 +- perl-xCAT/xCAT/Table.pm | 158 +++++++++++-------- xCAT-server/lib/xcat/plugins/DBobjectdefs.pm | 8 +- 3 files changed, 94 insertions(+), 81 deletions(-) diff --git a/perl-xCAT/xCAT/DBobjUtils.pm b/perl-xCAT/xCAT/DBobjUtils.pm index 5400fb7c8..31bc6c14a 100755 --- a/perl-xCAT/xCAT/DBobjUtils.pm +++ b/perl-xCAT/xCAT/DBobjUtils.pm @@ -424,7 +424,6 @@ sub getobjdefs # get the key to look for, for this object type my $objkey = $datatype->{'objkey'}; - # go through the list of valid attrs foreach my $this_attr (@{ $datatype->{'attrs'} }) { my $ent; @@ -487,7 +486,6 @@ sub getobjdefs my $intabhash = 0; my $notsearched = 0; foreach my $lookup_attr (keys %{ $tabentry{'lookup_attrs'} }) { - # Check whether the attribute is already in %tabhash # The %tabhash is for performance considerations if (($lookup_attr eq 'node') && ($objtype eq 'node')) { @@ -513,7 +511,6 @@ sub getobjdefs # Not in tabhash, # Need to lookup the table if ($intabhash == 0 && $notsearched == 1) { - # look up attr values my @rows = xCAT::DBobjUtils->getDBtable($lookup_table); if (@rows) { @@ -2502,6 +2499,7 @@ sub judge_node Arguments: nicsattr value, like niccsips=eth0!1.1.1.1|2.1.1.1,eth1!3.1.1.1|4.1.1.1 + node name, like frame10node10 nicnames: only return the value for specific nics, like "eth0,eth1" Returns: expanded format, like: @@ -2524,8 +2522,8 @@ sub expandnicsattr() if (($nicstr) && ($nicstr =~ /xCAT::/)) { $nicstr = shift; } + my $node = shift; my $nicnames = shift; - my $ret; $nicstr =~ /^(.*?)=(.*?)$/; @@ -2547,7 +2545,6 @@ sub expandnicsattr() #$nicval Value: node(d+)|eth0!192.1.1.($1+10) if (($nicval) && ($nicval =~ /\|/)) { my ($str1, $str2) = split('\|', $nicval); - #$nivval Value: eth0!192.1.1.($1+10) $nicval = $str2; } @@ -2576,6 +2573,7 @@ sub expandnicsattr() } } + $nicv[1]= xCAT::Table::transRegexAttrs($node, $nicv[1]); # ignore the line that does not have nicname or value if ($nicv[0] && $nicv[1]) { $ret .= " $nicattr.$nicv[0]=$nicv[1]\n"; @@ -2628,7 +2626,6 @@ sub collapsenicsattr() $nodeattrhash = shift; } my $objname = shift; - my %nicattrs = (); foreach my $nodeattr (keys %{$nodeattrhash}) { diff --git a/perl-xCAT/xCAT/Table.pm b/perl-xCAT/xCAT/Table.pm index 67b1b2b8f..5f1714f50 100644 --- a/perl-xCAT/xCAT/Table.pm +++ b/perl-xCAT/xCAT/Table.pm @@ -2299,6 +2299,91 @@ $evalcpt->permit('require'); #-------------------------------------------------------------------------- +=head3 transRegexAttrs + + Description: Transform the regular expression attribute to the target value + based on the node name. + + Arguments: + Node + Attribute value (may have regular expression) + Returns: + Attribute value + undef + Globals: + + Error: + + Example: + if (defined($retval = transRegexAttrs($node, $datum->{$attrib}))) { + $datum->{$attrib} = $retval; + } else { + delete $datum->{$attrib}; + } + + Comments: + none + +=cut + +#--------------------------------------------------------------------------- +sub transRegexAttrs +{ + my ($node, $attr) = @_; + my $retval = $attr; + if ($attr =~ /^\/[^\/]*\/[^\/]*\/$/) { + my $exp = substr($attr, 1); + chop $exp; + my @parts = split('/', $exp, 2); + $retval = $node; + $retval =~ s/$parts[0]/$parts[1]/; + } elsif ($attr =~ /^\|.*\|$/) { + my $exp = substr($attr, 1); + chop $exp; + my @parts = split('\|', $exp, 2); + my $arraySize = @parts; + if ($arraySize < 2) { # easy regx, generate lhs from node + my $lhs; + my @numbers = $node =~ m/[\D0]*(\d+)/g; + $lhs = '[\D0]*(\d+)' x scalar(@numbers); + $lhs .= '.*$'; + unshift(@parts, $lhs); + } + my ($curr, $next, $prev); + $retval = $parts[1]; + + ($curr, $next, $prev) = + extract_bracketed($retval, '()', qr/[^()]*/); + unless ($curr) { #If there were no paramaters to save, treat this one like a plain regex + undef $@; #extract_bracketed would have set $@ if it didn't return, undef $@ + $retval = $node; + $retval =~ s/$parts[0]/$parts[1]/; + } + while ($curr) + { + my $value = $node; + $value =~ s/$parts[0]/$curr/; + $value = $evalcpt->reval('use integer;' . $value); + $retval = $prev . $value . $next; + ($curr, $next, $prev) = + extract_bracketed($retval, '()', qr/[^()]*/); + } + undef $@; + + #At this point, $retval is the expression after being arithmetically contemplated, a generated regex, and therefore + #must be applied in total + my $answval = $node; + $answval =~ s/$parts[0]/$retval/; + $retval = $answval; + } + if ($retval =~ /^$/) { + $retval = undef; + } + return $retval; +} + +#-------------------------------------------------------------------------- + =head3 getNodeAttribs Description: Retrieves the requested attribute @@ -2392,77 +2477,10 @@ sub getNodeAttribs #skip undefined values, save time next; } - if ($datum->{$attrib} =~ /^\/[^\/]*\/[^\/]*\/$/) - { - my $exp = substr($datum->{$attrib}, 1); - chop $exp; - my @parts = split('/', $exp, 2); - my $retval = $node; - $retval =~ s/$parts[0]/$parts[1]/; + my $retval; + if (defined($retval = transRegexAttrs($node, $datum->{$attrib}))) { $datum->{$attrib} = $retval; - } - elsif ($datum->{$attrib} =~ /^\|.*\|$/) - { - - #Perform arithmetic and only arithmetic operations in bracketed issues on the right. - #Tricky part: don't allow potentially dangerous code, only eval if - #to-be-evaled expression is only made up of ()\d+-/%$ - #Futher paranoia? use Safe module to make sure I'm good - my $exp = substr($datum->{$attrib}, 1); - chop $exp; - my @parts = split('\|', $exp, 2); - my $arraySize = @parts; - if ($arraySize < 2) { # easy regx, generate lhs from node - my $lhs; - my @numbers = $node =~ m/[\D0]*(\d+)/g; - $lhs = '[\D0]*(\d+)' x scalar(@numbers); - $lhs .= '.*$'; - unshift(@parts, $lhs); - } - my $curr; - my $next; - my $prev; - my $retval = $parts[1]; - ($curr, $next, $prev) = - extract_bracketed($retval, '()', qr/[^()]*/); - - unless ($curr) { #If there were no paramaters to save, treat this one like a plain regex - undef $@; #extract_bracketed would have set $@ if it didn't return, undef $@ - $retval = $node; - $retval =~ s/$parts[0]/$parts[1]/; - $datum->{$attrib} = $retval; - if ($datum->{$attrib} =~ /^$/) { - - #If regex forces a blank, act like a normal blank does - delete $datum->{$attrib}; - } - next; #skip the redundancy that follows otherwise - } - while ($curr) - { - - #my $next = $comps[0]; - my $value = $node; - $value =~ s/$parts[0]/$curr/; - $value = $evalcpt->reval('use integer;' . $value); - $retval = $prev . $value . $next; - ($curr, $next, $prev) = - extract_bracketed($retval, '()', qr/[^()]*/); - } - undef $@; - - #At this point, $retval is the expression after being arithmetically contemplated, a generated regex, and therefore - #must be applied in total - my $answval = $node; - $answval =~ s/$parts[0]/$retval/; - $datum->{$attrib} = $answval; #$retval; - - #print Data::Dumper::Dumper(extract_bracketed($parts[1],'()',qr/[^()]*/)); - #use text::balanced extract_bracketed to parse earch atom, make sure nothing but arith operators, parans, and numbers are in it to guard against code execution - } - if ($datum->{$attrib} =~ /^$/) { - - #If regex forces a blank, act like a normal blank does + } else { delete $datum->{$attrib}; } } diff --git a/xCAT-server/lib/xcat/plugins/DBobjectdefs.pm b/xCAT-server/lib/xcat/plugins/DBobjectdefs.pm index 90ddb541f..d91d51c3e 100755 --- a/xCAT-server/lib/xcat/plugins/DBobjectdefs.pm +++ b/xCAT-server/lib/xcat/plugins/DBobjectdefs.pm @@ -2973,7 +2973,6 @@ sub setFINALattrs } } } - # $tmphash{nicips} = "eth0!1.1.1.1|1.2.1.1,eth1!2.1.1.1|2.2.1.1" foreach my $nicattr (keys %tmphash) { @@ -3189,7 +3188,6 @@ sub defls } } - # if just provided type list then find all objects of these types if (!defined $::opt_template && $::objectsfrom_optt) { @@ -3845,11 +3843,11 @@ sub defls my $nicsstr; if ($nicnames) { - $nicsstr = xCAT::DBobjUtils->expandnicsattr($nicval, $nicnames); + $nicsstr = xCAT::DBobjUtils->expandnicsattr($nicval, $obj, $nicnames); } else { - $nicsstr = xCAT::DBobjUtils->expandnicsattr($nicval); + $nicsstr = xCAT::DBobjUtils->expandnicsattr($nicval, $obj); } # Compress mode, format the output @@ -3902,7 +3900,7 @@ sub defls if ($showattr =~ /^nic/) { my $nicval = "$showattr=$attrval"; - my $nicsstr = xCAT::DBobjUtils->expandnicsattr($nicval); + my $nicsstr = xCAT::DBobjUtils->expandnicsattr($nicval, $obj); if ($nicsstr) { push(@{ $rsp_info->{data} }, "$nicsstr");