diff --git a/perl-xCAT/xCAT/DBobjUtils.pm b/perl-xCAT/xCAT/DBobjUtils.pm index 19428d0e0..1cbaeac1d 100755 --- a/perl-xCAT/xCAT/DBobjUtils.pm +++ b/perl-xCAT/xCAT/DBobjUtils.pm @@ -49,23 +49,19 @@ sub getObjectsOfType my @objlist; # special case for site table - if ($type eq 'site') - { + if ($type eq 'site') { push(@objlist, 'clustersite'); return @objlist; } # The database may be changed between getObjectsOfType calls # do not use cache %::saveObjList if --nocache is specified - if ($::saveObjList{$type} && !$::opt_nc) - { + if ($::saveObjList{$type} && !$::opt_nc) { @objlist = @{$::saveObjList{$type}}; - } - else - { + } else { # get the key for this type object - # ex. for "network" type the key is "netname" + # ex. for "network" type the key is "netname" # get the data type spec from Schema.pm my $datatype = $xCAT::Schema::defspec{$type}; @@ -75,11 +71,9 @@ sub getObjectsOfType my $table; my $tabkey; - foreach my $this_attr (@{$datatype->{'attrs'}}) - { + foreach my $this_attr (@{$datatype->{'attrs'}}) { my $attr = $this_attr->{attr_name}; - if ($attr eq $objkey) - { + if ($attr eq $objkey) { # get the table & key for to lookup # get the actual attr name to use in the table # - may be different then the attr name used for the object. @@ -92,31 +86,26 @@ sub getObjectsOfType # to the list of objects. my @TableRowArray = xCAT::DBobjUtils->getDBtable($table); - foreach (@TableRowArray) - { + foreach (@TableRowArray) { push(@objlist, $_->{$tabkey}); - } # if this is type "group" we need to check the nodelist table my @nodeGroupList=(); - if ($type eq 'group') - { + if ($type eq 'group') { my $table = "nodelist"; my @TableRowArray = xCAT::DBobjUtils->getDBtable($table); - foreach (@TableRowArray) - { + foreach (@TableRowArray) { my @tmplist = split(',', $_->{'groups'}); push(@nodeGroupList, @tmplist); } - foreach my $n (@nodeGroupList) - { + foreach my $n (@nodeGroupList) { if (!grep(/^$n$/, @objlist) ) { push(@objlist, $n); } } } - + @{$::saveObjList{$type}} = @objlist; } @@ -127,7 +116,7 @@ sub getObjectsOfType =head3 getobjattrs - Get data from tables + Get data from tables $type_hash: objectname=>objtype hash $attrs_ref: only get the specific attributes, @@ -143,7 +132,7 @@ sub getObjectsOfType %tabhash = xCAT::DBobjUtils->getobjattrs(\%typehash); Comments: - For now - only support tables that have 'node' as key !!! + For now - only support tables that have 'node' as key !!! =cut #----------------------------------------------------------------------------- @@ -157,10 +146,10 @@ sub getobjattrs @attrs = @{shift()}; } my %typehash = %$ref_hash; - + my %tableattrs; my %tabhash; - + # get a list of object names for each type my %objtypelist; foreach my $objname (sort (keys %typehash)) { @@ -168,14 +157,14 @@ sub getobjattrs # $objtypelist{$typehash{$objname}}=$objname; push @{$objtypelist{$typehash{$objname}}}, $objname; } - + # go through each object type and look up all the info for each object foreach my $objtype (keys %objtypelist) { - - # only do node type for now - if ($objtype eq 'node') { - # find the list of tables and corresponding attrs - # - for this object type + + # only do node type for now + if ($objtype eq 'node') { + # find the list of tables and corresponding attrs + # - for this object type # get the object type decription from Schema.pm my $datatype = $xCAT::Schema::defspec{$objtype}; foreach my $this_attr (@{$datatype->{'attrs'}}) { @@ -185,7 +174,7 @@ sub getobjattrs next; # This attribute is not needed } } - + # table_attr is the attr that actually appears in the # table which could possibly be different then the attr # used in the node def @@ -195,8 +184,8 @@ sub getobjattrs push @{$tableattrs{$lookup_table}}, $table_attr; } } - - # foreach table look up the list of attrs for this + + # foreach table look up the list of attrs for this # list of object names foreach my $table (keys %tableattrs) { # open the table @@ -208,15 +197,15 @@ sub getobjattrs xCAT::MsgUtils->message("E", $rsp, $::callback); next; } - + my @objlist = @{$objtypelist{$objtype}}; - + my $rec = $thistable->getNodesAttribs(\@objlist, @{$tableattrs{$table}}); - + # fill in %tabhash with any values that are set foreach my $n (@objlist) { - my $tmp1=$rec->{$n}->[0]; - foreach $a (@{$tableattrs{$table}}) { + my $tmp1=$rec->{$n}->[0]; + foreach $a (@{$tableattrs{$table}}) { if (defined($tmp1->{$a})) { $tabhash{$table}{$n}{$a} = $tmp1->{$a}; #print "obj = $n, table = $table, attr =$a, val = $tabhash{$table}{$n}{$a}\n"; @@ -224,7 +213,7 @@ sub getobjattrs # Add a has been searched flag to improve the performance $tabhash{$table}{$n}{"$a"."_hassearched"} = 1; } - } + } } #$thistable->commit; } @@ -252,11 +241,11 @@ sub getobjattrs Error: Example: - To use create hash for objectname and object type - ex. $objhash{$obj} = $type; + To use create hash for objectname and object type + ex. $objhash{$obj} = $type; - - then call as follows: - %myhash = xCAT::DBobjUtils->getobjdefs(\%objhash); + - then call as follows: + %myhash = xCAT::DBobjUtils->getobjdefs(\%objhash); Comments: @@ -270,24 +259,22 @@ sub getobjdefs my %typehash = %$hash_ref; my %tabhash; my @attrs; - if (ref($attrs_ref)) - { + if (ref($attrs_ref)) { @attrs = @$attrs_ref; } @::foundTableList = (); - + if ($::ATTRLIST eq "none") { # just return the list of obj names - foreach my $objname (sort (keys %typehash)) - { + foreach my $objname (sort (keys %typehash)) { my $type = $typehash{$objname}; $objhash{$objname}{'objtype'} = $type; } return %objhash; } - # see if we need to get any objects of type 'node' + # see if we need to get any objects of type 'node' my $getnodes=0; foreach my $objname (keys %typehash) { if ($typehash{$objname} eq 'node') { @@ -301,8 +288,7 @@ sub getobjdefs # values from when using 'only_if' - see below # - but this saves lots of time if ($getnodes) { - if (scalar(@attrs) > 0) # Only get specific attributes of the node - { + if (scalar(@attrs) > 0) { # Only get specific attributes of the node # find the onlyif key for the attributes REDO: my $datatype = $xCAT::Schema::defspec{'node'}; foreach my $this_attr (@{$datatype->{'attrs'}}) { @@ -316,9 +302,7 @@ sub getobjdefs } } %tabhash = xCAT::DBobjUtils->getobjattrs(\%typehash, \@attrs); - } - else - { + } else { %tabhash = xCAT::DBobjUtils->getobjattrs(\%typehash); } } @@ -333,13 +317,10 @@ sub getobjdefs if ($objtype eq 'site') { my @TableRowArray = xCAT::DBobjUtils->getDBtable('site'); foreach my $objname (sort @{$type_obj{$objtype}}) { - if (@TableRowArray) - { + if (@TableRowArray) { my $foundinfo = 0; - foreach (@TableRowArray) - { - if ($_->{key}) - { + foreach (@TableRowArray) { + if ($_->{key}) { if (defined($_->{value}) ) { $foundinfo++; if ($verbose == 1) { @@ -350,30 +331,26 @@ sub getobjdefs } } } - if ($foundinfo) - { + if ($foundinfo) { $objhash{$objname}{'objtype'} = 'site'; } - } - else - { + } else { my $rsp; $rsp->{data}->[0] ="Could not read the \'$objname\' object from the \'site\' table."; - xCAT::MsgUtils->message("E", $rsp, $::callback); + xCAT::MsgUtils->message("E", $rsp, $::callback); } } } elsif ($objtype eq 'monitoring') { # need a special case for the monitoring table - # - need to check the monsetting table for entries that contain - # the same name as the monitoring table entry. + # - need to check the monsetting table for entries that contain + # the same name as the monitoring table entry. my @TableRowArray = xCAT::DBobjUtils->getDBtable('monsetting'); foreach my $objname (sort @{$type_obj{$objtype}}) { if (@TableRowArray) { my $foundinfo = 0; - foreach (@TableRowArray) { + foreach (@TableRowArray) { if ($_->{name} eq $objname ) { - if ($_->{key}) - { + if ($_->{key}) { if (defined($_->{value}) ) { $foundinfo++; if ($verbose == 1) { @@ -385,16 +362,13 @@ sub getobjdefs } } } - if ($foundinfo) - { + if ($foundinfo) { $objhash{$objname}{'objtype'} = 'monitoring'; } - } - else - { + } else { my $rsp; $rsp->{data}->[0] ="Could not read the \'$objname\' object from the \'monsetting\' table."; - xCAT::MsgUtils->message("E", $rsp, $::callback); + xCAT::MsgUtils->message("E", $rsp, $::callback); } } } elsif (($objtype eq 'auditlog') || ($objtype eq 'eventlog')) { @@ -405,15 +379,11 @@ sub getobjdefs # performance is a big concern with the general logic my @TableRowArray = xCAT::DBobjUtils->getDBtable($objtype); foreach my $objname (sort @{$type_obj{$objtype}}) { - if (@TableRowArray) - { + if (@TableRowArray) { my $foundinfo = 0; - foreach my $entry (@TableRowArray) - { - if ($entry->{recid} eq $objname) - { - foreach my $k (keys %{$entry}) - { + foreach my $entry (@TableRowArray) { + if ($entry->{recid} eq $objname) { + foreach my $k (keys %{$entry}) { # recid is the object name, do not need to be in the attributes list if ($k eq 'recid') { next; } if (defined($entry->{$k}) ) { @@ -425,76 +395,71 @@ sub getobjdefs } } } - if ($foundinfo) - { + if ($foundinfo) { $objhash{$objname}{'objtype'} = $objtype; } # There should not be multiple entries with the same recid last; } # end if($entry-> } # end foreach my $entry - } # end if(@TableTowArray - } # end foreach my $objname + } # end if(@TableTowArray + } # end foreach my $objname } else { # get the object type decription from Schema.pm my $datatype = $xCAT::Schema::defspec{$objtype}; # 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'}}) - { + # go through the list of valid attrs + foreach my $this_attr (@{$datatype->{'attrs'}}) { my $ent; my $attr = $this_attr->{attr_name}; - + # skip the key attr ??? - if ($attr eq $objkey) - { + if ($attr eq $objkey) { next; } # skip the attributes that does not needed for node type if ($getnodes) { if (scalar(@attrs) > 0 && !grep(/^$attr$/, @attrs)) { next; - } + } } - + # OK - get the info needed to access the DB table # - i.e. table name, key name, attr names - + # need the actual table attr name corresponding # to the object attr name # ex. noderes.nfsdir my ($tab, $tabattr) = split('\.', $this_attr->{tabentry}); - + foreach my $objname (sort @{$type_obj{$objtype}}) { # get table lookup info from Schema.pm # !!!! some tables depend on the value of certain attrs # we need to look up attrs in the correct order or we will # not be able to determine what tables to look - # in for some attrs. - if (exists($this_attr->{only_if})) - { - my ($check_attr, $check_value) = split('\=', $this_attr->{only_if}); - # if the object value is not the value we need - # to match then try the next only_if value - next if ( !($objhash{$objname}{$check_attr} =~ /\b$check_value\b/) ); + # in for some attrs. + if (exists($this_attr->{only_if})) { + my ($check_attr, $check_value) = split('\=', $this_attr->{only_if}); + # if the object value is not the value we need + # to match then try the next only_if value + next if ( !($objhash{$objname}{$check_attr} =~ /\b$check_value\b/) ); } - - + + $objhash{$objname}{'objtype'} = $objtype; my %tabentry = (); # def commands need to support multiple keys in one table # the subroutine parse_access_tabentry is used for supporting multiple keys - my $rc = xCAT::DBobjUtils->parse_access_tabentry($objname, + my $rc = xCAT::DBobjUtils->parse_access_tabentry($objname, $this_attr->{access_tabentry}, \%tabentry); - if ($rc != 0) - { - my $rsp; - $rsp->{data}->[0] = - "access_tabentry \'$this_attr->{access_tabentry}\' is not valid."; - xCAT::MsgUtils->message("E", $rsp, $::callback); - next; + if ($rc != 0) { + my $rsp; + $rsp->{data}->[0] = + "access_tabentry \'$this_attr->{access_tabentry}\' is not valid."; + xCAT::MsgUtils->message("E", $rsp, $::callback); + next; } # # Only allow one table in the access_tabentry @@ -502,18 +467,14 @@ sub getobjdefs my $lookup_table = $tabentry{'lookup_table'}; my $intabhash = 0; my $notsearched = 0; - foreach my $lookup_attr (keys %{$tabentry{'lookup_attrs'}}) - { + 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') ){ + if ( ($lookup_attr eq 'node') && ($objtype eq 'node') ) { if (defined($tabhash{$lookup_table}{$objname}{$tabattr})) { - if ($verbose == 1) - { + if ($verbose == 1) { $objhash{$objname}{$attr} = "$tabhash{$lookup_table}{$objname}{$tabattr}\t(Table:$lookup_table - Key:$lookup_attr - Column:$tabattr)"; - } - else - { + } else { $objhash{$objname}{$attr} = $tabhash{$lookup_table}{$objname}{$tabattr}; } if (defined $chname_ref) { @@ -523,7 +484,7 @@ sub getobjdefs last; } elsif (! defined($tabhash{$lookup_table}{$objname}{"$tabattr"."_hassearched"})) { $notsearched = 1; - } + } } else { $notsearched = 1; } @@ -531,40 +492,31 @@ sub getobjdefs # Not in tabhash, # Need to lookup the table - if ($intabhash == 0 && $notsearched == 1) - { + if ($intabhash == 0 && $notsearched == 1) { # look up attr values my @rows = xCAT::DBobjUtils->getDBtable($lookup_table); - if (@rows) - { - foreach my $rowent (@rows) - { + if (@rows) { + foreach my $rowent (@rows) { my $match = 1; my $matchedattr; # Again, multiple keys support needs the "foreach" - foreach my $lookup_attr (keys %{$tabentry{'lookup_attrs'}}) - { - if ($rowent->{$lookup_attr} ne $tabentry{'lookup_attrs'}{$lookup_attr}) - { + foreach my $lookup_attr (keys %{$tabentry{'lookup_attrs'}}) { + if ($rowent->{$lookup_attr} ne $tabentry{'lookup_attrs'}{$lookup_attr}) { $match = 0; last; } } - if ($match == 1) - { - if ($verbose == 1) - { - my @lookup_attrs = keys %{$tabentry{'lookup_attrs'}}; - $objhash{$objname}{$attr} = "$rowent->{$tabattr}\t(Table:$lookup_table - Key: @lookup_attrs - Column:$tabattr)"; - } - else - { - $objhash{$objname}{$attr} = $rowent->{$tabattr}; - } - if (defined $chname_ref) { - push @{$chname_ref->{$lookup_table}}, ($tabentry{'lookup_attrs'}, (keys %{$tabentry{'lookup_attrs'}}) [0]); - } - } #end if ($match... + if ($match == 1) { + if ($verbose == 1) { + my @lookup_attrs = keys %{$tabentry{'lookup_attrs'}}; + $objhash{$objname}{$attr} = "$rowent->{$tabattr}\t(Table:$lookup_table - Key: @lookup_attrs - Column:$tabattr)"; + } else { + $objhash{$objname}{$attr} = $rowent->{$tabattr}; + } + if (defined $chname_ref) { + push @{$chname_ref->{$lookup_table}}, ($tabentry{'lookup_attrs'}, (keys %{$tabentry{'lookup_attrs'}}) [0]); + } + } #end if ($match... } #end foreach } # end if (defined... } #end if ($intabhash... @@ -572,7 +524,7 @@ sub getobjdefs } } - + } #foreach my $objtype return %objhash; @@ -593,7 +545,7 @@ sub getobjdefs Example: call as follows - my @TableRowArray= xCAT::DBobjUtils->getDBtable($tablename); + my @TableRowArray= xCAT::DBobjUtils->getDBtable($tablename); Comments: @@ -605,47 +557,40 @@ sub getDBtable my ($class, $table) = @_; my @rows = []; - # save this table info - in case this subr gets called multiple times + # save this table info - in case this subr gets called multiple times # --nocache flag specifies not to use cahe - if (grep(/^$table$/, @::foundTableList) && !$::opt_nc) - { + if (grep(/^$table$/, @::foundTableList) && !$::opt_nc) { # already have this @rows = @{$::TableHash{$table}}; - } - else - { + } else { - # need to get info from DB - my $thistable = xCAT::Table->new($table, -create => 1); - if (!$thistable) - { - return undef; - } + # need to get info from DB + my $thistable = xCAT::Table->new($table, -create => 1); + if (!$thistable) { + return undef; + } - #@rows = $thistable->getTable; - @rows = @{$thistable->getAllEntries()}; + #@rows = $thistable->getTable; + @rows = @{$thistable->getAllEntries()}; - # !!!! this routine returns rows even if the table is empty!!!!!! + # !!!! this routine returns rows even if the table is empty!!!!!! - # keep track of the fact that we checked this table + # keep track of the fact that we checked this table # - even if it's empty! push(@::foundTableList, $thistable->{tabname}); @{$::TableHash{$table}} = @rows; - #$thistable->commit; + #$thistable->commit; - } # end if not cached + } # end if not cached - if (@rows) - { - return @rows; - } - else - { - return undef; + if (@rows) { + return @rows; + } else { + return undef; } } @@ -665,11 +610,11 @@ sub getDBtable Example: To use: - -create hash for objectname and object type - ex. $objhash{$object}{$attribute} = value; + -create hash for objectname and object type + ex. $objhash{$object}{$attribute} = value; - -then call as follows: - if (xCAT::DBobjUtils->setobjdefs(\%objhash) != 0) + -then call as follows: + if (xCAT::DBobjUtils->setobjdefs(\%objhash) != 0) Comments: @@ -686,15 +631,14 @@ sub setobjdefs my $setattrs=0; # get the attr=vals for these objects from the DB - if any - # - so we can figure out where to put additional attrs + # - so we can figure out where to put additional attrs # The getobjdefs call was in the foreach loop, # it caused mkdef/chdef performance issue, # so it is moved out of the foreach loop my %DBhash; my @attrs; - foreach my $objname (keys %objhash) - { + foreach my $objname (keys %objhash) { my $type = $objhash{$objname}{objtype}; $DBhash{$objname} = $type; @attrs = keys %{$objhash{$objname}}; @@ -704,36 +648,32 @@ sub setobjdefs %DBattrvals = xCAT::DBobjUtils->getobjdefs(\%DBhash, 0, \@attrs); # for each object figure out: - # - what tables to update - # - which table attrs correspond to which object attrs - # - what the keys are for each table + # - what tables to update + # - which table attrs correspond to which object attrs + # - what the keys are for each table # update the tables a row at a time - foreach my $objname (keys %objhash) - { + foreach my $objname (keys %objhash) { # get attr=val that are set in the DB ?? my $type = $objhash{$objname}{objtype}; - # handle the monitoring table as a special case !!!!! - if ($type eq 'monitoring') - { + # handle the monitoring table as a special case !!!!! + if ($type eq 'monitoring') { # Get the names of the attrs stored in monitoring table # get the object type decription from Schema.pm my $datatype = $xCAT::Schema::defspec{$type}; - + # get a list of valid attr names # for this type object my @attrlist; - foreach my $entry (@{$datatype->{'attrs'}}) - { - push(@attrlist, $entry->{'attr_name'}); + foreach my $entry (@{$datatype->{'attrs'}}) { + push(@attrlist, $entry->{'attr_name'}); } # open the tables (monitoring and monsetting) my $montable = xCAT::Table->new('monitoring', -create => 1, -autocommit => 0); - if (!$montable) - { + if (!$montable) { my $rsp; $rsp->{data}->[0] = "Could not set the \'$montable\' table."; xCAT::MsgUtils->message("E", $rsp, $::callback); @@ -741,44 +681,34 @@ sub setobjdefs } # open the table my $monsettable = xCAT::Table->new('monsetting', -create => 1, -autocommit => 0); - if (!$monsettable) - { + if (!$monsettable) { my $rsp; $rsp->{data}->[0] = "Could not set the \'$monsettable\' table."; xCAT::MsgUtils->message("E", $rsp, $::callback); return 1; } - my %keyhash; + my %keyhash; my %updates; - - foreach my $attr (keys %{$objhash{$objname}}) - { + + foreach my $attr (keys %{$objhash{$objname}}) { my $val; - if ($attr eq 'objtype') - { + if ($attr eq 'objtype') { next; } # determine the value if we have plus or minus - if ($::plus_option) - { + if ($::plus_option) { # add new to existing - at the end - comma separated - if (defined($DBattrvals{$objname}{$attr})) - { + if (defined($DBattrvals{$objname}{$attr})) { $val = "$DBattrvals{$objname}{$attr},$objhash{$objname}{$attr}"; - } - else - { + } else { $val = "$objhash{$objname}{$attr}"; } - } - elsif ($::minus_option) - { + } elsif ($::minus_option) { # remove the specified list of values from the current # attr values. - if ($DBattrvals{$objname}{$attr}) - { + if ($DBattrvals{$objname}{$attr}) { # get the list of attrs to remove my @currentList = split(/,/, $DBattrvals{$objname}{$attr}); my @minusList = split(/,/, $objhash{$objname}{$attr}); @@ -786,14 +716,11 @@ sub setobjdefs # make a new list without the one specified my $first = 1; my $newlist; - foreach my $i (@currentList) - { + foreach my $i (@currentList) { chomp $i; - if (!grep(/^$i$/, @minusList)) - { + if (!grep(/^$i$/, @minusList)) { # set new groups list for node - if (!$first) - { + if (!$first) { $newlist .= ","; } $newlist .= $i; @@ -802,9 +729,7 @@ sub setobjdefs } $val = $newlist; } - } - else - { + } else { #just set the attr to what was provided! - replace $val = $objhash{$objname}{$attr}; } @@ -822,30 +747,26 @@ sub setobjdefs $monsettable->setAttribs(\%keyhash, \%updates); } } - + $montable->commit; $monsettable->commit; next; } #if ($type eq 'monitoring') # handle the site table as a special case !!!!! - if ($type eq 'site') - { + if ($type eq 'site') { # open the table my $thistable = xCAT::Table->new('site', -create => 1, -autocommit => 0); - if (!$thistable) - { + if (!$thistable) { my $rsp; $rsp->{data}->[0] = "Could not set the \'$thistable\' table."; xCAT::MsgUtils->message("E", $rsp, $::callback); return 1; } - foreach my $attr (keys %{$objhash{$objname}}) - { - if ($attr eq 'objtype') - { + foreach my $attr (keys %{$objhash{$objname}}) { + if ($attr eq 'objtype') { next; } @@ -853,25 +774,18 @@ sub setobjdefs $keyhash{key} = $attr; my $val; - if ($::plus_option) - { + if ($::plus_option) { # add new to existing - at the end - comma separated - if (defined($DBattrvals{$objname}{$attr})) - { + if (defined($DBattrvals{$objname}{$attr})) { $val = "$DBattrvals{$objname}{$attr},$objhash{$objname}{$attr}"; - } - else - { + } else { $val = "$objhash{$objname}{$attr}"; } - } - elsif ($::minus_option) - { + } elsif ($::minus_option) { # remove the specified list of values from the current # attr values. - if ($DBattrvals{$objname}{$attr}) - { + if ($DBattrvals{$objname}{$attr}) { # get the list of attrs to remove my @currentList = split(/,/, $DBattrvals{$objname}{$attr}); my @minusList = split(/,/, $objhash{$objname}{$attr}); @@ -879,14 +793,11 @@ sub setobjdefs # make a new list without the one specified my $first = 1; my $newlist; - foreach my $i (@currentList) - { + foreach my $i (@currentList) { chomp $i; - if (!grep(/^$i$/, @minusList)) - { + if (!grep(/^$i$/, @minusList)) { # set new groups list for node - if (!$first) - { + if (!$first) { $newlist .= ","; } $newlist .= $i; @@ -895,9 +806,7 @@ sub setobjdefs } $val = $newlist; } - } - else - { + } else { #just set the attr to what was provided! - replace $val = $objhash{$objname}{$attr}; @@ -907,22 +816,20 @@ sub setobjdefs if ( $val eq "") { # delete the line $thistable->delEntries(\%keyhash); } else { # change the attr - - my %updates; - $updates{value} = $val; - - my ($rc, $str) = $thistable->setAttribs(\%keyhash, \%updates); - if (!defined($rc)) - { - if ($::verbose) - { - my $rsp; - $rsp->{data}->[0] = - "Could not set the \'$attr\' attribute of the \'$objname\' object in the xCAT database."; - $rsp->{data}->[1] = - "Error returned is \'$str->errstr\'."; - xCAT::MsgUtils->message("I", $rsp, $::callback); - } + + my %updates; + $updates{value} = $val; + + my ($rc, $str) = $thistable->setAttribs(\%keyhash, \%updates); + if (!defined($rc)) { + if ($::verbose) { + my $rsp; + $rsp->{data}->[0] = + "Could not set the \'$attr\' attribute of the \'$objname\' object in the xCAT database."; + $rsp->{data}->[1] = + "Error returned is \'$str->errstr\'."; + xCAT::MsgUtils->message("I", $rsp, $::callback); + } $ret = 1; } } @@ -943,14 +850,13 @@ sub setobjdefs # get the object type decription from Schema.pm my $datatype = $xCAT::Schema::defspec{$type}; - # get the object key to look for, for this object type + # get the object key to look for, for this object type my $objkey = $datatype->{'objkey'}; # get a list of valid attr names # for this type object my %attrlist; - foreach my $entry (@{$datatype->{'attrs'}}) - { + foreach my $entry (@{$datatype->{'attrs'}}) { #push(@{$attrlist{$type}}, $entry->{'attr_name'}); $attrlist{$type}{$entry->{'attr_name'}} = 1; } @@ -958,24 +864,19 @@ sub setobjdefs my @attrprovided=(); # check FINALATTRS to see if all the attrs are valid - foreach my $attr (keys %{$objhash{$objname}}) - { + foreach my $attr (keys %{$objhash{$objname}}) { - if ($attr eq $objkey) - { + if ($attr eq $objkey) { next; } - if ($attr eq "objtype") - { + if ($attr eq "objtype") { # objtype not stored in object definition next; } - if (!defined($attrlist{$type}{$attr})) - { - if ($::verbose) - { + if (!defined($attrlist{$type}{$attr})) { + if ($::verbose) { my $rsp; $rsp->{data}->[0] = "\'$attr\' is not a valid attribute for type \'$type\'."; @@ -984,7 +885,7 @@ sub setobjdefs } next; } - push(@attrprovided, $attr); + push(@attrprovided, $attr); } # we need to figure out what table to @@ -995,23 +896,20 @@ sub setobjdefs my %checkedattrs; my $invalidattr; - foreach my $this_attr (@{$datatype->{'attrs'}}) - { + foreach my $this_attr (@{$datatype->{'attrs'}}) { my %keyhash; my %updates; my %tabentry; my ($lookup_table, $lookup_attr, $lookup_data); my $attr_name = $this_attr->{attr_name}; - if ($attr_name eq $objkey) - { + if ($attr_name eq $objkey) { next; } # if we have a value for this attribute then process it # - otherwise go to the next attr - if (defined($objhash{$objname}{$attr_name})) - { + if (defined($objhash{$objname}{$attr_name})) { # check the defspec to see where this attr goes # the table for this attr might depend on the @@ -1020,8 +918,7 @@ sub setobjdefs # other attr value matches what we have # ex. like if I want to set hdwctrlpoint I will have # to match the right value for mgtmethod - if (exists($this_attr->{only_if})) - { + if (exists($this_attr->{only_if})) { my ($check_attr, $check_value) = split('\=', $this_attr->{only_if}); @@ -1034,7 +931,7 @@ sub setobjdefs # need to check the attrs we are setting for the object # as well as the attrs for this object that may be - # already set in DB + # already set in DB if ( !($objhash{$objname}{$check_attr}) && !($DBattrvals{$objname}{$check_attr}) ) { # if I didn't already check for this attr @@ -1062,8 +959,7 @@ sub setobjdefs next; } - if ( !($objhash{$objname}{$check_attr} =~ /\b$check_value\b/) && !($DBattrvals{$objname}{$check_attr} =~ /\b$check_value\b/) ) - { + if ( !($objhash{$objname}{$check_attr} =~ /\b$check_value\b/) && !($DBattrvals{$objname}{$check_attr} =~ /\b$check_value\b/) ) { if ( $invalidattr->{$attr_name}->{valid} ne 1 ) { $invalidattr->{$attr_name}->{valid} = 0; $invalidattr->{$attr_name}->{condition} = "\'$check_attr=$check_value\'"; @@ -1082,104 +978,84 @@ sub setobjdefs my $ntab; ($ntab, $::tabattr) = split('\.', $this_attr->{tabentry}); - my $rc = xCAT::DBobjUtils->parse_access_tabentry($objname, + my $rc = xCAT::DBobjUtils->parse_access_tabentry($objname, $this_attr->{access_tabentry}, \%tabentry); - if ($rc != 0) - { + if ($rc != 0) { my $rsp; $rsp->{data}->[0] = "access_tabentry \'$this_attr->{access_tabentry}\' is not valid."; - xCAT::MsgUtils->message("E", $rsp, $::callback); - next; + xCAT::MsgUtils->message("E", $rsp, $::callback); + next; } $lookup_table = $tabentry{'lookup_table'}; # Set the lookup criteria for this attribute into %allupdates # the key is 'lookup_attrs' - foreach my $lookup_attr (keys %{$tabentry{'lookup_attrs'}}) - { - $allupdates{$lookup_table}{$objname}{$attr_name}{'lookup_attrs'}{$lookup_attr} + foreach my $lookup_attr (keys %{$tabentry{'lookup_attrs'}}) { + $allupdates{$lookup_table}{$objname}{$attr_name}{'lookup_attrs'}{$lookup_attr} =$tabentry{'lookup_attrs'}{$lookup_attr}; } - } - else - { + } else { next; } my $val; my $delim = ','; - if(($type eq 'group') && ($DBattrvals{$objname}{'grouptype'} eq 'dynamic')) - { + if(($type eq 'group') && ($DBattrvals{$objname}{'grouptype'} eq 'dynamic')) { # dynamic node group selection string use "::" as delimiter $delim = '::'; } - - if ($::plus_option) - { + + if ($::plus_option) { # add new to existing - at the end - comma separated - if (defined($DBattrvals{$objname}{$attr_name})) - { + if (defined($DBattrvals{$objname}{$attr_name})) { # add the attr into the list if it's not already in the list! # and avoid the duplicate values my @DBattrarray = split(/$delim/, $DBattrvals{$objname}{$attr_name}); my @objhasharray = split(/$delim/, $objhash{$objname}{$attr_name}); - foreach my $objattr (@objhasharray) - { - if (!grep(/^\Q$objattr\E$/, @DBattrarray)) - { + foreach my $objattr (@objhasharray) { + if (!grep(/^\Q$objattr\E$/, @DBattrarray)) { push @DBattrarray, $objattr; } - } - $val = join($delim, @DBattrarray); - } - else - { + } + $val = join($delim, @DBattrarray); + } else { $val = "$objhash{$objname}{$attr_name}"; } - } - elsif ($::minus_option) - { + } elsif ($::minus_option) { # remove the specified list of values from the current - # attr values. - if ($DBattrvals{$objname}{$attr_name}) - { + # attr values. + if ($DBattrvals{$objname}{$attr_name}) { # get the list of attrs to remove my @currentList = split(/$delim/, $DBattrvals{$objname}{$attr_name}); my @minusList = split(/$delim/, $objhash{$objname}{$attr_name}); - foreach my $em (@minusList) - { - if (!(grep {$_ eq $em} @currentList)) - { - if (($::opt_t eq 'group') && ($DBattrvals{$objname}{'grouptype'} ne 'dynamic')) - { + foreach my $em (@minusList) { + if (!(grep {$_ eq $em} @currentList)) { + if (($::opt_t eq 'group') && ($DBattrvals{$objname}{'grouptype'} ne 'dynamic')) { my $rsp; - $rsp->{data}->[0] = "$objname is not a member of \'$em\'."; - xCAT::MsgUtils->message("W", $rsp, $::callback); - } else { + $rsp->{data}->[0] = "$objname is not a member of \'$em\'."; + xCAT::MsgUtils->message("W", $rsp, $::callback); + } else { my $rsp; - $rsp->{data}->[0] = "$em is not in the attribute of \'$attr_name\' for the \'$objname\' definition."; - xCAT::MsgUtils->message("W", $rsp, $::callback); - } + $rsp->{data}->[0] = "$em is not in the attribute of \'$attr_name\' for the \'$objname\' definition."; + xCAT::MsgUtils->message("W", $rsp, $::callback); + } } } # make a new list without the one specified my $first = 1; my $newlist; - foreach my $i (@currentList) - { + foreach my $i (@currentList) { chomp $i; - if (!grep(/^\Q$i\E$/, @minusList)) - { + if (!grep(/^\Q$i\E$/, @minusList)) { # set new list for node - if (!$first) - { + if (!$first) { $newlist .= "$delim"; } $newlist .= $i; @@ -1189,9 +1065,7 @@ sub setobjdefs $val = $newlist; } - } - else - { + } else { #just set the attr to what was provided! - replace $val = $objhash{$objname}{$attr_name}; @@ -1202,7 +1076,7 @@ sub setobjdefs # the key is 'tabattrs' $allupdates{$lookup_table}{$objname}{$attr_name}{'tabattrs'}{$::tabattr} = $val; $setattrs=1; - + push(@setattrlist, $attr_name); } # end - foreach attribute @@ -1217,36 +1091,34 @@ my $tt = $invalidattr->{$att}->{valid}; } -# TODO - need to get back to this -if (0) { - # - # check to see if all the attrs got set - # + # TODO - need to get back to this + if (0) { + # + # check to see if all the attrs got set + # - my @errlist; - foreach $a (@attrprovided) - { - # is this attr was not set then add it to the error list - if (!grep(/^$a$/, @setattrlist)) - { - push(@errlist, $a); - $ret = 2; - } + my @errlist; + foreach $a (@attrprovided) { + # is this attr was not set then add it to the error list + if (!grep(/^$a$/, @setattrlist)) { + push(@errlist, $a); + $ret = 2; + } - } - if ($ret == 2) { - my $rsp; - $rsp->{data}->[0] = "Could not set the following attributes for the \'$objname\' definition in the xCAT database: \'@errlist\'"; - xCAT::MsgUtils->message("E", $rsp, $::callback); - } + } + if ($ret == 2) { + my $rsp; + $rsp->{data}->[0] = "Could not set the following attributes for the \'$objname\' definition in the xCAT database: \'@errlist\'"; + xCAT::MsgUtils->message("E", $rsp, $::callback); + } -} + } } # end - foreach object #==========================================================# #%allupdates structure: -# for command: chdef -t node -o node1 groups=all -# usercomment=ddee passwd.HMC=HMC +# for command: chdef -t node -o node1 groups=all +# usercomment=ddee passwd.HMC=HMC # passwd.admin=cluster passwd.general=abc123 # the %allupdates will be: #0 'ppcdirect' @@ -1284,24 +1156,24 @@ if (0) { # 'tabattrs' => HASH(0x127842f4) # 'comments' => 'ddee' #=================================================================# - # now set the attribute values in the tables - # - handles all except site, monitoring & monsetting for now - if ($setattrs) { - foreach my $table (keys %allupdates) { - - # get the keys for this table + # now set the attribute values in the tables + # - handles all except site, monitoring & monsetting for now + if ($setattrs) { + foreach my $table (keys %allupdates) { + + # get the keys for this table my $schema = xCAT::Table->getTableSchema($table); my $keys = $schema->{keys}; - + # open the table my $thistable = xCAT::Table->new($table, -create => 1, -autocommit => 0); if (!$thistable) { - my $rsp; - $rsp->{data}->[0] = "Could not set the \'$thistable\' table."; - xCAT::MsgUtils->message("E", $rsp, $::callback); - return 1; + my $rsp; + $rsp->{data}->[0] = "Could not set the \'$thistable\' table."; + xCAT::MsgUtils->message("E", $rsp, $::callback); + return 1; } - + # Special case for the postscripts table # Does not set the postscripts to the postscripts table # if the postscripts already in xcatdefaults @@ -1312,12 +1184,9 @@ if (0) { my $xcatdefaultsps; my $xcatdefaultspbs; my @TableRowArray = xCAT::DBobjUtils->getDBtable('postscripts'); - if (@TableRowArray) - { - foreach my $tablerow (@TableRowArray) - { - if(($tablerow->{node} eq 'xcatdefaults') && !($tablerow->{disable})) - { + if (@TableRowArray) { + foreach my $tablerow (@TableRowArray) { + if(($tablerow->{node} eq 'xcatdefaults') && !($tablerow->{disable})) { $xcatdefaultsps = $tablerow->{postscripts}; $xcatdefaultspbs = $tablerow->{postbootscripts}; last; @@ -1332,21 +1201,21 @@ if (0) { next; } my @newps; - if (defined($allupdates{$table}{$obj}{'postscripts'}) + if (defined($allupdates{$table}{$obj}{'postscripts'}) && defined($allupdates{$table}{$obj}{'postscripts'}{'tabattrs'}{'postscripts'})) { foreach my $tempps (split(/,/, $allupdates{$table}{$obj}{'postscripts'}{'tabattrs'}{'postscripts'})) { if (grep(/^$tempps$/, @xcatdefps)) { - my $rsp; - $rsp->{data}->[0] = "$obj: postscripts \'$tempps\' is already included in the \'xcatdefaults\'."; - xCAT::MsgUtils->message("E", $rsp, $::callback); - } else { - push @newps, $tempps; - } + my $rsp; + $rsp->{data}->[0] = "$obj: postscripts \'$tempps\' is already included in the \'xcatdefaults\'."; + xCAT::MsgUtils->message("E", $rsp, $::callback); + } else { + push @newps, $tempps; + } } $allupdates{$table}{$obj}{'postscripts'}{'tabattrs'}{'postscripts'} = join(',', @newps); } my @newpbs; - if (defined($allupdates{$table}{$obj}{'postbootscripts'}) + if (defined($allupdates{$table}{$obj}{'postbootscripts'}) && defined($allupdates{$table}{$obj}{'postbootscripts'}{'tabattrs'}{'postbootscripts'})) { foreach my $temppbs (split(/,/, $allupdates{$table}{$obj}{'postbootscripts'}{'tabattrs'}{'postbootscripts'})) { if (grep(/^$temppbs$/, @xcatdefpbs)) { @@ -1358,7 +1227,7 @@ if (0) { } } $allupdates{$table}{$obj}{'postbootscripts'}{'tabattrs'}{'postbootscripts'} = join(',', @newpbs); - } + } } } @@ -1379,7 +1248,7 @@ if (0) { next ROW; } } - + if ($firsttime) { # lookup keys in %hashkey # ex. $keyhash{'hcp'} = node1 @@ -1391,8 +1260,8 @@ if (0) { # check if the look_attrs is the same as the %keyhash foreach my $key (keys %{$allupdates{$table}{$obj}{$row}{'lookup_attrs'}}) { # The lookup_attrs can be different for tables with more than one keys such as ppcdirect - if ((scalar(keys %keyhash) != scalar(keys %{$allupdates{$table}{$obj}{$row}{'lookup_attrs'}})) - || !defined($keyhash{$key}) + if ((scalar(keys %keyhash) != scalar(keys %{$allupdates{$table}{$obj}{$row}{'lookup_attrs'}})) + || !defined($keyhash{$key}) ||($keyhash{$key} ne $allupdates{$table}{$obj}{$row}{'lookup_attrs'}{$key})) { # different keys, set the existing attributes into database # update the %keyhash and clean up the %updates hash @@ -1407,7 +1276,7 @@ if (0) { } } } - + # set values in %updates # ex. $updates{'groups'} = 'all,lpar' foreach my $attr (keys %{$allupdates{$table}{$obj}{$row}{'tabattrs'}}) { @@ -1450,10 +1319,10 @@ if (0) { Error: Example: - To use create hash for object name and object type - ex. $objhash{$obj} = $type; - - then call as follows: - xCAT::DBobjUtils->rmobjdefs(\%objhash); + To use create hash for object name and object type + ex. $objhash{$obj} = $type; + - then call as follows: + xCAT::DBobjUtils->rmobjdefs(\%objhash); Comments: @@ -1471,19 +1340,16 @@ sub rmobjdefs # get the attr=vals for these objects so we know how to # find what tables have to be modified - foreach my $objname (sort (keys %typehash)) - { + foreach my $objname (sort (keys %typehash)) { my $type = $typehash{$objname}; # special handling for site table - if ($type eq 'site') - { + if ($type eq 'site') { my %DBattrvals = xCAT::DBobjUtils->getobjdefs(\%typehash); my $thistable = xCAT::Table->new('site', -create => 1, -autocommit => 0); my %keyhash; - foreach my $attr (keys %{$DBattrvals{$objname}}) - { + foreach my $attr (keys %{$DBattrvals{$objname}}) { # ex. key = attr $keyhash{key} = $attr; @@ -1501,8 +1367,7 @@ sub rmobjdefs # go through the list of valid attrs # - need to delete the row with a $key value of $objname from $table # - make a hash containing $delhash{$table}{$key}= $objname - foreach my $this_attr (@{$datatype->{'attrs'}}) - { + foreach my $this_attr (@{$datatype->{'attrs'}}) { my $attr = $this_attr->{attr_name}; # get table lookup info from Schema.pm @@ -1510,13 +1375,12 @@ sub rmobjdefs # the subroutine parse_access_tabentry is used for supporting multiple keys my %tabentry = (); my $rc = xCAT::DBobjUtils->parse_access_tabentry($objname, $this_attr->{access_tabentry}, \%tabentry); - if ($rc != 0) - { + if ($rc != 0) { my $rsp; $rsp->{data}->[0] = "access_tabentry \'$this_attr->{access_tabentry}\' is not valid."; - xCAT::MsgUtils->message("E", $rsp, $::callback); - next; + xCAT::MsgUtils->message("E", $rsp, $::callback); + next; } # Only allow one table in the access_tabentry @@ -1526,9 +1390,8 @@ sub rmobjdefs my $attr_name = $this_attr->{'attr_name'}; # we'll need table name, object name, attribute name and the lookup entries # put this info in a hash - we'll process it later - below - foreach my $lookup_attr (keys %{$tabentry{'lookup_attrs'}}) - { - $tablehash{$lookup_table}{$objname}{$attr_name}{$lookup_attr} + foreach my $lookup_attr (keys %{$tabentry{'lookup_attrs'}}) { + $tablehash{$lookup_table}{$objname}{$attr_name}{$lookup_attr} = $tabentry{'lookup_attrs'}{$lookup_attr}; } @@ -1583,8 +1446,7 @@ sub rmobjdefs #... ##=========================================================# # now for each table - clear the entry - foreach my $table (keys %tablehash) - { + foreach my $table (keys %tablehash) { my @all_keyhash; my $thistable = @@ -1592,18 +1454,14 @@ sub rmobjdefs foreach my $obj (keys %{$tablehash{$table}}) { my %keyhash; - foreach my $attr (keys %{$tablehash{$table}{$obj}}) - { - foreach my $key (keys %{$tablehash{$table}{$obj}{$attr}}) - { + foreach my $attr (keys %{$tablehash{$table}{$obj}}) { + foreach my $key (keys %{$tablehash{$table}{$obj}{$attr}}) { #multiple keys support - if (defined($keyhash{$key}) && ($keyhash{$key} ne $tablehash{$table}{$obj}{$attr}{$key})) - { + if (defined($keyhash{$key}) && ($keyhash{$key} ne $tablehash{$table}{$obj}{$attr}{$key})) { my %tmpkeyhash; # copy hash - foreach my $hashkey (keys %keyhash) - { + foreach my $hashkey (keys %keyhash) { $tmpkeyhash{$hashkey} = $keyhash{$hashkey}; } push @all_keyhash, \%tmpkeyhash; @@ -1630,7 +1488,7 @@ sub rmobjdefs =head3 readFileInput Process the command line input piped in from a file. - (Support stanza or xml format.) + (Support stanza or xml format.) Arguments: Returns: @@ -1641,8 +1499,8 @@ sub rmobjdefs Example: Comments: - Set @::fileobjtypes, @::fileobjnames, %::FILEATTRS - (i.e.- $::FILEATTRS{objname}{attr}=val) + Set @::fileobjtypes, @::fileobjnames, %::FILEATTRS + (i.e.- $::FILEATTRS{objname}{attr}=val) =cut @@ -1650,7 +1508,7 @@ sub rmobjdefs sub readFileInput { my ($class, $filedata) = @_; - my ($objectname, $junk1, $junk2); + my ($objectname, $junk1, $junk2); @::fileobjnames = (); @@ -1666,17 +1524,15 @@ sub readFileInput #} my $look_for_colon = 1; # start with first line that has a colon - my $objtype; + my $objtype; - foreach my $l (@lines) - { + foreach my $l (@lines) { # skip blank and comment lines next if ($l =~ /^\s*$/ || $l =~ /^\s*#/); # see if it's a stanza name - if (grep(/:\s*$/, $l)) - { + if (grep(/:\s*$/, $l)) { $look_for_colon = 0; # ok - we have a colon @@ -1689,9 +1545,8 @@ sub readFileInput $l =~ /^(.*):(.*?)$/; ($objectname, $junk2) = ($1, $2); - # if $junk2 is defined or there's an = - if ($junk2 || grep(/=/, $objectname)) - { + # if $junk2 is defined or there's an = + if ($junk2 || grep(/=/, $objectname)) { # error - invalid header $line in node definition file # skipping to next node stanza @@ -1705,13 +1560,11 @@ sub readFileInput # could have different default stanzas for different object types - if ($objectname =~ /default/) - { + if ($objectname =~ /default/) { ($junk1, $objtype) = split(/-/, $objectname); - if ($objtype) - { + if ($objtype) { $objectname = 'default'; } @@ -1720,9 +1573,7 @@ sub readFileInput push(@::fileobjnames, $objectname); - } - elsif (($l =~ /^\s*(.*?)\s*=\s*(.*)\s*/) && (!$look_for_colon)) - { + } elsif (($l =~ /^\s*(.*?)\s*=\s*(.*)\s*/) && (!$look_for_colon)) { my $attr = $1; my $val = $2; $attr =~ s/^\s*//; # Remove any leading whitespace @@ -1734,31 +1585,26 @@ sub readFileInput $val =~ s/^\s*"\s*//; $val =~ s/\s*"\s*$//; - if ($objectname eq "default") - { + if ($objectname eq "default") { # set the default for this attribute $::defAttrs{$objtype}{$attr} = $val; - } - else - { + } else { # set the value in the hash for this object $::FILEATTRS{$objectname}{$attr} = $val; # if the attr being set is "objtype" then check - # to see if we have any defaults set for this type + # to see if we have any defaults set for this type # the objtype should be the first etntry in each stanza - # so after we set the defaults they will be overwritten - # by any values that appear in the rest of the stanza - if ($attr eq 'objtype') - { + # so after we set the defaults they will be overwritten + # by any values that appear in the rest of the stanza + if ($attr eq 'objtype') { push(@::fileobjtypes, $val); # $val will be the object type ex. site, node etc. - foreach my $a (keys %{$::defAttrs{$val}}) - { + foreach my $a (keys %{$::defAttrs{$val}}) { # set the default values for this object hash $::FILEATTRS{$objectname}{$a} = $::defAttrs{$val}{$a}; @@ -1766,9 +1612,7 @@ sub readFileInput } } - } - else - { + } else { # error - invalid line in node definition file $look_for_colon++; @@ -1793,15 +1637,15 @@ sub readFileInput Globals: Error: Example: - To use: - - create hash for objectname and and attr values (need group - name (object), and grouptype & members attr values at a - minimum.) - - ex. $objhash{$obj}{$attr} = value; + To use: + - create hash for objectname and and attr values (need group + name (object), and grouptype & members attr values at a + minimum.) + + ex. $objhash{$obj}{$attr} = value; - then call as follows: - xCAT::DBobjUtils->getGroupMembers($objectname, \%objhash); + xCAT::DBobjUtils->getGroupMembers($objectname, \%objhash); Comments: @@ -1818,32 +1662,27 @@ sub getGroupMembers # set 'static' as the dafault of nodetype if (!defined($objhash{$objectname}{'grouptype'}) || - $objhash{$objectname}{'grouptype'} eq "") { + $objhash{$objectname}{'grouptype'} eq "") { $objhash{$objectname}{'grouptype'} = 'static'; } - if ($objhash{$objectname}{'grouptype'} eq 'static') - { + if ($objhash{$objectname}{'grouptype'} eq 'static') { my $table = "nodelist"; my @TableRowArray = xCAT::DBobjUtils->getDBtable($table); my $first = 1; - foreach (@TableRowArray) - { + foreach (@TableRowArray) { # if find the group name in the "groups" attr value then add the - # node name to the member list + # node name to the member list #if ($_->{'groups'} =~ /$objectname/) my @nodeGroupList = split(',', $_->{'groups'}); - if (grep(/^$objectname$/, @nodeGroupList)) - - { + if (grep(/^$objectname$/, @nodeGroupList)) { chomp($_->{'node'}); - if (!$first) - { + if (!$first) { $members .= ","; } $members .= $_->{'node'}; @@ -1851,12 +1690,10 @@ sub getGroupMembers } } - } - elsif ($objhash{$objectname}{'grouptype'} eq 'dynamic') - { + } elsif ($objhash{$objectname}{'grouptype'} eq 'dynamic') { # find all nodes that satisfy the criteria specified in "wherevals" - # value + # value my %whereHash; my %tabhash; @@ -1866,8 +1703,7 @@ sub getGroupMembers my @tmpWhereList = split('::', $objhash{$objectname}{'wherevals'}); my $rc = xCAT::Utils->parse_selection_string(\@tmpWhereList, \%whereHash); - if ($rc != 0) - { + if ($rc != 0) { my $rsp; $rsp->{data}->[0] = "The \'-w\' option has an incorrect attr*val pair."; @@ -1879,9 +1715,8 @@ sub getGroupMembers my @tmplist = xCAT::DBobjUtils->getObjectsOfType('node'); # create a hash of obj names and types - my %tmphash; - foreach my $n (@tmplist) - { + my %tmphash; + foreach my $n (@tmplist) { $tmphash{$n} = 'node'; } @@ -1891,18 +1726,14 @@ sub getGroupMembers # The attribute 'node' can be used as a key of selection string, # however, the 'node' attribute is not included in the getobjdefs hash - foreach my $objname (keys %nodeattrhash) - { + foreach my $objname (keys %nodeattrhash) { $nodeattrhash{$objname}{'node'} = $objname; } my $first = 1; - foreach my $objname (keys %nodeattrhash) - { - if (xCAT::Utils->selection_string_match(\%nodeattrhash, $objname, \%whereHash)) - { + foreach my $objname (keys %nodeattrhash) { + if (xCAT::Utils->selection_string_match(\%nodeattrhash, $objname, \%whereHash)) { chomp($objname); - if (!$first) - { + if (!$first) { $members .= ","; } $members .= $objname; @@ -1930,102 +1761,93 @@ sub getGroupMembers %nethash = xCAT::DBobjUtils->getNetwkInfo(\@targetnodes); - Comments: + Comments: =cut #----------------------------------------------------------------------------- sub getNetwkInfo { - my ($class, $ref_nodes) = @_; - my @nodelist = @$ref_nodes; + my ($class, $ref_nodes) = @_; + my @nodelist = @$ref_nodes; - my %nethash; - my @attrnames; + my %nethash; + my @attrnames; - # get the current list of network attrs (networks table columns) + # get the current list of network attrs (networks table columns) my $datatype = $xCAT::Schema::defspec{'network'}; - foreach my $a (@{$datatype->{'attrs'}}) { - my $attr = $a->{attr_name}; - push(@attrnames, $attr); - } + foreach my $a (@{$datatype->{'attrs'}}) { + my $attr = $a->{attr_name}; + push(@attrnames, $attr); + } - # read the networks table - my @TableRowArray = xCAT::DBobjUtils->getDBtable('networks'); - if (! @TableRowArray) - { - return undef; - } + # read the networks table + my @TableRowArray = xCAT::DBobjUtils->getDBtable('networks'); + if (! @TableRowArray) { + return undef; + } - # for each node - get the network info - foreach my $node (@nodelist) - { + # for each node - get the network info + foreach my $node (@nodelist) { - # get, check, split the node IP - my $IP = xCAT::NetworkUtils->getipaddr($node); - chomp $IP; - unless (($IP =~ /\d+\.\d+\.\d+\.\d+/) || ($IP =~ /:/)) - { - next; - } - my ($ia, $ib, $ic, $id) = split('\.', $IP); + # get, check, split the node IP + my $IP = xCAT::NetworkUtils->getipaddr($node); + chomp $IP; + unless (($IP =~ /\d+\.\d+\.\d+\.\d+/) || ($IP =~ /:/)) { + next; + } + my ($ia, $ib, $ic, $id) = split('\.', $IP); - # check the entries of the networks table - # - if the bitwise AND of the IP and the netmask gives you - # the "net" name then that is the entry you want. - foreach (@TableRowArray) { - my $NM = $_->{'mask'}; - my $net=$_->{'net'}; - chomp $NM; - chomp $net; + # check the entries of the networks table + # - if the bitwise AND of the IP and the netmask gives you + # the "net" name then that is the entry you want. + foreach (@TableRowArray) { + my $NM = $_->{'mask'}; + my $net=$_->{'net'}; + chomp $NM; + chomp $net; - if(xCAT::NetworkUtils->ishostinsubnet($IP, $NM, $net)) - { - # fill in the hash - - foreach my $attr (@attrnames) { - if ( defined($_->{$attr}) ) { - $nethash{$node}{$attr} = $_->{$attr}; - } - } - if($nethash{$node}{'gateway'} eq '') - { - if(xCAT::NetworkUtils->ip_forwarding_enabled()) - { - $nethash{$node}{'gateway'} = xCAT::NetworkUtils->my_ip_in_subnet($net, $NM); - } - else - { - $nethash{$node}{'gateway'} = ''; - } - $nethash{$node}{'myselfgw'} = 1; - # For hwctrl commands, it is possible that this subroutine is called - # on MN instead of SN, if the hcp SN is not set - if (xCAT::Utils->isMN() && !$nethash{$node}{'gateway'}) - { - # does not have ip address in this subnet, - # use the node attribute 'xcatmaster' or site.master - my @nodes = ("$node"); - my $sn = xCAT::ServiceNodeUtils->get_ServiceNode(\@nodes,"xcat","Node"); - my $snkey = (keys %{$sn})[0]; - my $gw = xCAT::NetworkUtils->getipaddr($snkey); - # two possible cases when this code is run: - # 1. flat cluster: ip forwarding is not enabled on MN - # 2. hw ctrl in hierarchy cluster, in which HCP SN is not set - # in either case, MN itself should not be the gateway - if (xCAT::NetworkUtils->thishostisnot($gw)) { - $nethash{$node}{'gateway'} = $gw; - } - } - - } - next; + if(xCAT::NetworkUtils->ishostinsubnet($IP, $NM, $net)) { + # fill in the hash - + foreach my $attr (@attrnames) { + if ( defined($_->{$attr}) ) { + $nethash{$node}{$attr} = $_->{$attr}; + } + } + if($nethash{$node}{'gateway'} eq '') { + if(xCAT::NetworkUtils->ip_forwarding_enabled()) { + $nethash{$node}{'gateway'} = xCAT::NetworkUtils->my_ip_in_subnet($net, $NM); + } else { + $nethash{$node}{'gateway'} = ''; + } + $nethash{$node}{'myselfgw'} = 1; + # For hwctrl commands, it is possible that this subroutine is called + # on MN instead of SN, if the hcp SN is not set + if (xCAT::Utils->isMN() && !$nethash{$node}{'gateway'}) { + # does not have ip address in this subnet, + # use the node attribute 'xcatmaster' or site.master + my @nodes = ("$node"); + my $sn = xCAT::ServiceNodeUtils->get_ServiceNode(\@nodes,"xcat","Node"); + my $snkey = (keys %{$sn})[0]; + my $gw = xCAT::NetworkUtils->getipaddr($snkey); + # two possible cases when this code is run: + # 1. flat cluster: ip forwarding is not enabled on MN + # 2. hw ctrl in hierarchy cluster, in which HCP SN is not set + # in either case, MN itself should not be the gateway + if (xCAT::NetworkUtils->thishostisnot($gw)) { + $nethash{$node}{'gateway'} = $gw; } - - } + } - } #end - for each node + } + next; + } - return %nethash; + } + + } #end - for each node + + return %nethash; } #---------------------------------------------------------------------------- @@ -2036,17 +1858,17 @@ sub getNetwkInfo Arguments: $objname: objectname=>objtype hash $access_tabentry: the access_tabentry defined in Schema.pm - $tabentry_ref: return the parsed result through this hash ref - The structure of the hash is: - { - 'lookup_tables' => - 'lookup_attrs' => - { - 'attr1' => 'val1' - 'attr2' => 'val2' - ... - } - } + $tabentry_ref: return the parsed result through this hash ref + The structure of the hash is: + { + 'lookup_tables' => + 'lookup_attrs' => + { + 'attr1' => 'val1' + 'attr2' => 'val2' + ... + } + } Returns: 0 - success 1 - failed @@ -2054,7 +1876,7 @@ sub getNetwkInfo Error: Example: - To parse the access_tabentry field + To parse the access_tabentry field my $rc = xCAT::DBobjUtils->parse_access_tabentry($objname, $this_attr->{access_tabentry}, \%tabentry); @@ -2068,8 +1890,7 @@ sub parse_access_tabentry() my ($class, $objname, $access_tabentry, $tabentry_ref) = @_; # ex. 'nodelist.node', 'attr:node' - foreach my $ent (split('::', $access_tabentry)) - { + foreach my $ent (split('::', $access_tabentry)) { # ex. 'nodelist.node', 'attr:node' my ($lookup_key, $lookup_value) = split('\=', $ent); @@ -2079,16 +1900,14 @@ sub parse_access_tabentry() # ex. 'attr', 'node' my ($lookup_type, $lookup_data) = split('\:', $lookup_value); - if (!defined($tabentry_ref->{'lookup_table'})) - { + if (!defined($tabentry_ref->{'lookup_table'})) { $tabentry_ref->{'lookup_table'} = $lookup_table; } # Only support one lookup table in the access_tabentry # Do we need to support multiple tables in one access_tabentry ???? # has not seen any requirement... - if ($lookup_table ne $tabentry_ref->{'lookup_table'}) - { + if ($lookup_table ne $tabentry_ref->{'lookup_table'}) { my $rsp; $rsp->{data}->[0] = "The access_tabentry \"$access_tabentry\" is not valid, can not specify more than one tables to look up."; @@ -2096,28 +1915,23 @@ sub parse_access_tabentry() return 1; } - if ($lookup_type eq 'attr') - { - # TODO: may need to update in the future - # for now, the "val" in attr:val in - # Schema.pm can only be the object name - # In the future, even if we need to change here, - # be caution about the performance - # looking up table is time consuming - $tabentry_ref->{'lookup_attrs'}->{$lookup_attr} = $objname; - } - elsif ($lookup_type eq 'str') - { - $tabentry_ref->{'lookup_attrs'}->{$lookup_attr} = $lookup_data; - } - else - { - my $rsp; - $rsp->{data}->[0] = + if ($lookup_type eq 'attr') { + # TODO: may need to update in the future + # for now, the "val" in attr:val in + # Schema.pm can only be the object name + # In the future, even if we need to change here, + # be caution about the performance + # looking up table is time consuming + $tabentry_ref->{'lookup_attrs'}->{$lookup_attr} = $objname; + } elsif ($lookup_type eq 'str') { + $tabentry_ref->{'lookup_attrs'}->{$lookup_attr} = $lookup_data; + } else { + my $rsp; + $rsp->{data}->[0] = "The access_tabentry \"$access_tabentry\" is not valid, the lookup type can only be 'attr' or 'str'."; - xCAT::MsgUtils->message("E", $rsp, $::callback); - return 1; - } + xCAT::MsgUtils->message("E", $rsp, $::callback); + return 1; + } } return 0; } @@ -2128,17 +1942,17 @@ sub parse_access_tabentry() single hostname optional port number Returns: - array of fsp/bpa hostnames (IPs) - if specified port, it will just return nodes within the port. + array of fsp/bpa hostnames (IPs) + if specified port, it will just return nodes within the port. Globals: %PPCHASH - HASH of nodename -> array of ip addresses - where the nodetype is fsp or bpa + where the nodetype is fsp or bpa Error: - $::RUNCMD_RC = 1; - Writes error to syslog + $::RUNCMD_RC = 1; + Writes error to syslog Example: - $c1 = getchildren($nodetocheck); - $c1 = getchildren($nodetocheck,$port); + $c1 = getchildren($nodetocheck); + $c1 = getchildren($nodetocheck,$port); Comments: none =cut @@ -2149,8 +1963,7 @@ my %PPCHASH; sub getchildren { my $parent = shift; - if (($parent) && ($parent =~ /xCAT::/)) - { + if (($parent) && ($parent =~ /xCAT::/)) { $parent = shift; } $::RUNCMD_RC = 0; @@ -2161,9 +1974,9 @@ sub getchildren if (!%PPCHASH) { my $ppctab = xCAT::Table->new( 'ppc' ); unless ($ppctab) { # cannot open the table return with error - xCAT::MsgUtils->message('S', "getchildren:Unable to open ppc table.\n"); - $::RUNCMD_RC = 1; - return undef; + xCAT::MsgUtils->message('S', "getchildren:Unable to open ppc table.\n"); + $::RUNCMD_RC = 1; + return undef; } my @ps = $ppctab->getAllNodeAttribs(['node','parent','nodetype','hcp']); foreach my $entry ( @ps ) { @@ -2171,28 +1984,25 @@ sub getchildren my $c = $entry->{node}; my $t = $entry->{nodetype}; if ( $p and $c) { - if ($t) { # the nodetype exists in the ppc table, use it - if ( $t eq 'fsp' or $t eq 'bpa') { - # build hash of ppc.parent -> ppc.node - push @{$PPCHASH{$p}}, $c; - } - elsif ($t eq 'blade') { - push @{$PPCHASH{$c}}, $entry->{hcp}; + if ($t) { # the nodetype exists in the ppc table, use it + if ( $t eq 'fsp' or $t eq 'bpa') { + # build hash of ppc.parent -> ppc.node + push @{$PPCHASH{$p}}, $c; + } elsif ($t eq 'blade') { + push @{$PPCHASH{$c}}, $entry->{hcp}; + } + } else { # go look in the nodetype table to find nodetype + my $type = getnodetype($c, "ppc"); + if ( $type eq 'fsp' or $type eq 'bpa') { + # build hash of ppc.parent -> ppc.node + push @{$PPCHASH{$p}}, $c; + } elsif ($type eq "blade") { + push @{$PPCHASH{$c}}, $entry->{hcp}; + } } - } else { # go look in the nodetype table to find nodetype - my $type = getnodetype($c, "ppc"); - if ( $type eq 'fsp' or $type eq 'bpa') - { - # build hash of ppc.parent -> ppc.node - push @{$PPCHASH{$p}}, $c; - } - elsif ($type eq "blade") { - push @{$PPCHASH{$c}}, $entry->{hcp}; - } - } } # not $p and $c } - # Find parent in the hash and build return values + # Find parent in the hash and build return values foreach (@{$PPCHASH{$parent}}) { push @children, $_; } @@ -2204,34 +2014,30 @@ sub getchildren } } # if port not input - if ( !defined($port )) - { + if ( !defined($port )) { return \@children; } else { - if (@children) { - my $vpdtab = xCAT::Table->new( 'vpd' ); - unless ($vpdtab) { # cannot open the table return with error - xCAT::MsgUtils->message('S', "getchildren:Unable to open vpd table.\n"); - $::RUNCMD_RC = 1; - return undef; - } - my $sides = $vpdtab->getNodesAttribs(\@children, ['side']); - if(!$sides) - { - return undef; - } - foreach my $n (@children) - { - my $nside = $sides->{$n}->[0]; - if ($nside->{side} =~ /$port/) - { - push @children_port, $n; + if (@children) { + my $vpdtab = xCAT::Table->new( 'vpd' ); + unless ($vpdtab) { # cannot open the table return with error + xCAT::MsgUtils->message('S', "getchildren:Unable to open vpd table.\n"); + $::RUNCMD_RC = 1; + return undef; } + my $sides = $vpdtab->getNodesAttribs(\@children, ['side']); + if(!$sides) { + return undef; + } + foreach my $n (@children) { + my $nside = $sides->{$n}->[0]; + if ($nside->{side} =~ /$port/) { + push @children_port, $n; + } + } + return \@children_port; + } else { # no children + return undef; } - return \@children_port; - } else { # no children - return undef; - } } } #------------------------------------------------------------------------------- @@ -2239,22 +2045,22 @@ sub getchildren =head3 getnodetype Query ppc table, if no type found query nodetype table Arguments: - An array of nodenames or 1 nodename + An array of nodenames or 1 nodename Returns: - If the input is an array, it returns a hash, + If the input is an array, it returns a hash, for the nodes that can't get node type, it will be 'node' => undef; If the input is not an array, it returns the value of type, for the node that can't get node type, it will be undef; Globals: %NODETYPEHASH Error: - $::RUNCMD_RC = 1; + $::RUNCMD_RC = 1; Errors written to syslog Example: - $type = xCAT::DBobjUtils->getnodetype($node, "ppc"); - $type = xCAT::DBobjUtils->getnodetype($node); - $typerefer = xCAT::DBobjUtils->getnodetype(\@nodes, "PPC"); - $typerefer = xCAT::DBobjUtils->getnodetype(\@nodes); + $type = xCAT::DBobjUtils->getnodetype($node, "ppc"); + $type = xCAT::DBobjUtils->getnodetype($node); + $typerefer = xCAT::DBobjUtils->getnodetype(\@nodes, "PPC"); + $typerefer = xCAT::DBobjUtils->getnodetype(\@nodes); Comments: none =cut @@ -2264,8 +2070,7 @@ my %NODETYPEHASH; sub getnodetype { my $nodes = shift; - if (($nodes) && ($nodes =~ /xCAT::/)) - { + if (($nodes) && ($nodes =~ /xCAT::/)) { $nodes = shift; } my $table = shift; @@ -2273,8 +2078,8 @@ sub getnodetype my @tabletype = qw(ppc zvm); my %typehash; my %tablehash; - $::RUNCMD_RC = 0; - + $::RUNCMD_RC = 0; + my @failnodes; my @failnodes1; ###################################################################### @@ -2283,17 +2088,17 @@ sub getnodetype ###################################################################### if ($table) { my $nodetypetab = xCAT::Table->new( $table ); - unless ($nodetypetab) { + unless ($nodetypetab) { xCAT::MsgUtils->message('S', "getnodetype:Unable to open $table table.\n"); - $::RUNCMD_RC = 1; + $::RUNCMD_RC = 1; if ( $nodes =~ /^ARRAY/) { foreach my $tn (@$nodes) { $typehash{$tn} = undef; } } else { - $typehash{$nodes} = undef; - } - return \%typehash; + $typehash{$nodes} = undef; + } + return \%typehash; } ############################################ # if the input node arg is an array, @@ -2303,21 +2108,21 @@ sub getnodetype my $nodetypes = $nodetypetab->getNodesAttribs($nodes, ['nodetype']); foreach my $tn (@$nodes) { my $gottype = $nodetypes->{$tn}->[0]->{'nodetype'}; - if ( $gottype ) { + if ( $gottype ) { $NODETYPEHASH{$tn} = $gottype; - $typehash{$tn} = $gottype; + $typehash{$tn} = $gottype; } else { push @failnodes, $tn; } } ################################################ - # for the failed nodes, go to nodetype table + # for the failed nodes, go to nodetype table ################################################ if ( @failnodes ) { my $typetable = xCAT::Table->new( 'nodetype' ); unless ($typetable) { # cannot open the table return with error xCAT::MsgUtils->message('S', "getnodetype:Unable to open nodetype table.\n"); - $::RUNCMD_RC = 1; + $::RUNCMD_RC = 1; foreach my $tn (@failnodes) { $typehash{$tn} = undef; } @@ -2329,30 +2134,30 @@ sub getnodetype $typehash{$tn} = $nodetypes->{$tn}->[0]->{'nodetype'}; } else { push @failnodes1, $tn; - $typehash{$tn} = undef; + $typehash{$tn} = undef; } ################################################## # give error msg for the nodes can't get nodetype ################################################## - } + } if ( @failnodes1 ) { my $nodelist = join(",", @failnodes1); xCAT::MsgUtils->message('S', "getnodetype:Can't find these nodes' type: $nodelist.\n"); - } - } - } + } + } + } ##################### # return the result - ##################### + ##################### return \%typehash; - + } else { ############################################ # if the input node arg is not an array, # query table and use the global hash first ############################################ if ( $NODETYPEHASH{$nodes} ) { - return $NODETYPEHASH{$nodes}; + return $NODETYPEHASH{$nodes}; } else { my $typep = $nodetypetab->getNodeAttribs($nodes, ['nodetype']); if ( $typep->{nodetype} ) { @@ -2362,7 +2167,7 @@ sub getnodetype my $typetable = xCAT::Table->new( 'nodetype' ); unless ($typetable) { # cannot open the table return with error xCAT::MsgUtils->message('S', "getnodetype:Unable to open nodetype table.\n"); - $::RUNCMD_RC = 1; + $::RUNCMD_RC = 1; return undef; } my $typep = $typetable->getNodeAttribs($nodes, ['nodetype']); @@ -2371,7 +2176,7 @@ sub getnodetype return $typep->{nodetype}; } else { return undef; - } + } } } } @@ -2381,17 +2186,17 @@ sub getnodetype # if can't get anything from the specified table, go to nodetype table ###################################################################### my $nodetypetab = xCAT::Table->new( 'nodetype' ); - unless ($nodetypetab) { + unless ($nodetypetab) { xCAT::MsgUtils->message('S', "getnodetype:Unable to open $table table.\n"); - $::RUNCMD_RC = 1; + $::RUNCMD_RC = 1; if ( $nodes =~ /^ARRAY/) { foreach my $tn (@$nodes) { $typehash{$tn} = undef; } } else { - $typehash{$nodes} = undef; - } - return \%typehash; + $typehash{$nodes} = undef; + } + return \%typehash; } ############################################ # if the input node arg is an array, @@ -2411,17 +2216,17 @@ sub getnodetype } } } elsif (grep(/$gottype/, @tabletype)){ #if find ppc or zvm - $tablehash{ $tn } = $gottype; - } else { + $tablehash{ $tn } = $gottype; + } else { $NODETYPEHASH{ $tn } = $gottype; - $typehash{ $tn } = $gottype; - } + $typehash{ $tn } = $gottype; + } } else { $typehash{ $tn } = undef; } } ################################################ - # for the failed nodes, go to related tables + # for the failed nodes, go to related tables ################################################ if ( %tablehash ) { foreach my $ttable (@tabletype) { @@ -2430,13 +2235,13 @@ sub getnodetype if ($tablehash{$fnode} eq $ttable) { push @nodegroup, $fnode; } - } - next unless (@nodegroup); + } + next unless (@nodegroup); my $typetable = xCAT::Table->new( $ttable); unless ($typetable) { my $failnodes = join(",", @nodegroup); xCAT::MsgUtils->message('S', "getnodetype:Unable to open $ttable table, can't find $failnodes type.\n"); - $::RUNCMD_RC = 1; + $::RUNCMD_RC = 1; foreach (@nodegroup) { $typehash{$_} = undef; } @@ -2449,14 +2254,14 @@ sub getnodetype } else { $typehash{$fn} = undef; } - } + } } } - } - return \%typehash; + } + return \%typehash; } else { # if not an array if ( $NODETYPEHASH{$nodes} ) { - return $NODETYPEHASH{$nodes}; + return $NODETYPEHASH{$nodes}; } else { my $typep = $nodetypetab->getNodeAttribs($nodes, ["nodetype"]); if ( $typep->{nodetype} and !(grep(/$typep->{nodetype}/, @tabletype))) { @@ -2466,7 +2271,7 @@ sub getnodetype my $typetable = xCAT::Table->new( $typep->{nodetype} ); unless ($typetable) { xCAT::MsgUtils->message('S', "getnodetype:Unable to open nodetype table.\n"); - $::RUNCMD_RC = 1; + $::RUNCMD_RC = 1; return undef; } my $typep = $typetable->getNodeAttribs($nodes, ["nodetype"]); @@ -2475,7 +2280,7 @@ sub getnodetype return $typep->{nodetype}; } else { return undef; - } + } } } } @@ -2490,11 +2295,11 @@ sub getnodetype Returns: Array of cec hostnames Globals: - %PARENT_CHILDREN_CEC + %PARENT_CHILDREN_CEC Error: none Example: - @frame_members = getcecchildren($frame); + @frame_members = getcecchildren($frame); Comments: none =cut @@ -2504,51 +2309,49 @@ my %PARENT_CHILDREN_CEC; sub getcecchildren { my $parent = shift; - if (($parent) && ($parent =~ /xCAT::/)) - { + if (($parent) && ($parent =~ /xCAT::/)) { $parent = shift; } my @children = (); if (!%PARENT_CHILDREN_CEC) { my $ppctab = xCAT::Table->new( 'ppc' ); unless ($ppctab) { # cannot open the table return with error - xCAT::MsgUtils->message('S', "getcecchildren:Unable to open ppc table.\n"); - $::RUNCMD_RC = 1; - return undef; + xCAT::MsgUtils->message('S', "getcecchildren:Unable to open ppc table.\n"); + $::RUNCMD_RC = 1; + return undef; } - if ($ppctab) - { + if ($ppctab) { my @ps = $ppctab->getAllNodeAttribs(['node','parent','nodetype']); foreach my $entry ( @ps ) { my $p = $entry->{parent}; my $c = $entry->{node}; my $t = $entry->{nodetype}; if ( $p and $c) { - if ($t) { # the nodetype exists in the ppc table, use it - if ( $t eq 'cec') { - # build hash of ppc.parent -> ppc.node - push @{$PARENT_CHILDREN_CEC{$p}}, $c; - } - } else { # go look in the nodetype table to find nodetype - my $type = getnodetype($c); - if ( $type eq 'cec') { - push @{$PARENT_CHILDREN_CEC{$p}}, $c; - } - } + if ($t) { # the nodetype exists in the ppc table, use it + if ( $t eq 'cec') { + # build hash of ppc.parent -> ppc.node + push @{$PARENT_CHILDREN_CEC{$p}}, $c; + } + } else { # go look in the nodetype table to find nodetype + my $type = getnodetype($c); + if ( $type eq 'cec') { + push @{$PARENT_CHILDREN_CEC{$p}}, $c; + } + } } } - # find a match for the parent and build the return array + # find a match for the parent and build the return array foreach (@{$PARENT_CHILDREN_CEC{$parent}}) { - push @children, $_; + push @children, $_; } - return \@children; - } + return \@children; + } } else { # already built the HASH if (exists($PARENT_CHILDREN_CEC{$parent})) { foreach (@{$PARENT_CHILDREN_CEC{$parent}}) { push @children, $_; } - return \@children; + return \@children; } } return undef; @@ -2556,7 +2359,7 @@ sub getcecchildren #------------------------------------------------------------------------------- =head3 judge_node - judge the node is a real FSP/BPA, + judge the node is a real FSP/BPA, use to distinguish if the data is defined in xCAT 2.5 or later Arguments: node name @@ -2566,7 +2369,7 @@ sub getcecchildren Error: none Example: - $result = judge_node($nodetocheck); + $result = judge_node($nodetocheck); Comments: none =cut @@ -2575,43 +2378,38 @@ sub getcecchildren sub judge_node { my $node = shift; - if (($node) && ($node =~ /xCAT::/)) - { + if (($node) && ($node =~ /xCAT::/)) { $node = shift; - } + } my $type = shift; my $flag = 0; my $parenttype; my $nodeparent; - my $ppctab = xCAT::Table->new( 'ppc' ); + my $ppctab = xCAT::Table->new( 'ppc' ); if ( $ppctab ) { $nodeparent = $ppctab->getNodeAttribs($node, ["parent"]); if ($nodeparent and $nodeparent->{parent}) { $parenttype = getnodetype($nodeparent->{parent}); } } - + if ($type =~ /^fsp$/) { - if ($parenttype =~ /^cec$/) - { + if ($parenttype =~ /^cec$/) { $flag = 1; - } else - { + } else { $flag = 0; } } if ($type =~ /^bpa$/) { - if ($parenttype =~ /^frame$/) - { + if ($parenttype =~ /^frame$/) { $flag = 1; - } else - { + } else { $flag = 0; } - } - - return $flag; + } + + return $flag; } @@ -2625,7 +2423,7 @@ sub judge_node nicsips.eth1=3.1.1.1|4.1.1.1 Arguments: - nicsattr value, like niccsips=eth0!1.1.1.1|2.1.1.1,eth1!3.1.1.1|4.1.1.1 + nicsattr value, like niccsips=eth0!1.1.1.1|2.1.1.1,eth1!3.1.1.1|4.1.1.1 nicnames: only return the value for specific nics, like "eth0,eth1" Returns: expanded format, like: @@ -2645,13 +2443,12 @@ sub judge_node sub expandnicsattr() { my $nicstr = shift; - if (($nicstr) && ($nicstr =~ /xCAT::/)) - { + if (($nicstr) && ($nicstr =~ /xCAT::/)) { $nicstr = shift; } my $nicnames = shift; - my $ret; + my $ret; $nicstr =~ /^(.*?)=(.*?)$/; @@ -2662,33 +2459,28 @@ sub expandnicsattr() my $nicval=$2; # $nicarr[0]: eth0!1.1.1.1|2.1.1.1 - # $nicarr[1]: eth1!3.1.1.1|4.1.1.1 + # $nicarr[1]: eth1!3.1.1.1|4.1.1.1 my @nicarr = split(/,/, $nicval); - - foreach my $nicentry (@nicarr) - { + + foreach my $nicentry (@nicarr) { #nicentry: eth0!1.1.1.1|2.1.1.1 # $nicv[0]: eth0 # $nicv[1]: 1.1.1.1|2.1.1.1 my @nicv = split(/!/, $nicentry); # only return nic* attr for these specific nics - if ($nicnames) - { + if ($nicnames) { my @nics = split(/,/, $nicnames); - if ($nicv[0]) - { + if ($nicv[0]) { # Do not need to return the nic attr for this nic - if (!grep(/^$nicv[0]$/, @nics)) - { + if (!grep(/^$nicv[0]$/, @nics)) { next; } } } # ignore the line that does not have nicname or value - if ($nicv[0] && $nicv[1]) - { + if ($nicv[0] && $nicv[1]) { $ret .= " $nicattr.$nicv[0]=$nicv[1]\n"; } } @@ -2703,13 +2495,13 @@ sub expandnicsattr() =head3 collapsenicsattr Collapse the nics related attributes into the database format, - for example, + for example, nicsips.eth0=1.1.1.1|2.1.1.1 nicsips.eth1=3.1.1.1|4.1.1.1 - + the collapsed format: nicsips=eth0!1.1.1.1|2.1.1.1,eth1!3.1.1.1|4.1.1.1 - + The collapse will be done against the hash %::FILEATTRS or %::CLIATTRS, remove the nicips.thx attributes from %::FILEATTRS or %::CLIATTRS, add the collapsed info nicips into %::FILEATTRS or %::CLIATTRS. @@ -2735,48 +2527,41 @@ sub expandnicsattr() sub collapsenicsattr() { my $nodeattrhash = shift; - if (($nodeattrhash) && ($nodeattrhash =~ /xCAT::/)) - { + if (($nodeattrhash) && ($nodeattrhash =~ /xCAT::/)) { $nodeattrhash = shift; } my $objname = shift; my %nicattrs = (); - foreach my $nodeattr (keys %{$nodeattrhash}) - { + foreach my $nodeattr (keys %{$nodeattrhash}) { # e.g nicips.eth0 # do not need to handle nic attributes without the postfix .ethx, # it will be overwritten by the attributes with the postfix .ethx, - if ($nodeattr =~ /^(nic\w+)\.(.*)$/) - { - if ($1 && $2) - { - # chdef nicips.eth2= to remove the definition for eth2 - # in this case, the $nodeattrhash->{'nicips.eth0'} is blank - if ($nodeattrhash->{$nodeattr}) - { - # $nicattrs{nicips}{eth0} = "1.1.1.1|1.2.1.1" - $nicattrs{$1}{$2} = $nodeattrhash->{$nodeattr}; - } + if ($nodeattr =~ /^(nic\w+)\.(.*)$/) { + if ($1 && $2) { + # chdef nicips.eth2= to remove the definition for eth2 + # in this case, the $nodeattrhash->{'nicips.eth0'} is blank + if ($nodeattrhash->{$nodeattr}) { + # $nicattrs{nicips}{eth0} = "1.1.1.1|1.2.1.1" + $nicattrs{$1}{$2} = $nodeattrhash->{$nodeattr}; + } - # remove nicips.eth0 from the %::FILEATTRS - delete $nodeattrhash->{$nodeattr}; - } - } - } + # remove nicips.eth0 from the %::FILEATTRS + delete $nodeattrhash->{$nodeattr}; + } + } + } # $nicattrs{'nicips'}{'eth0'} = "1.1.1.1|1.2.1.1" # $nicattrs{'nicips'}{'eth1'} = "2.1.1.1|2.2.1.1" - foreach my $nicattr (keys %nicattrs) - { - my @tmparray = (); - foreach my $nicname (keys %{$nicattrs{$nicattr}}) - { - # eth0!1.1.1.1|1.2.1.1 - push @tmparray, "$nicname!$nicattrs{$nicattr}{$nicname}"; - } - # eth0!1.1.1.1|1.2.1.1,eth1!2.1.1.1|2.2.1.1 - $nodeattrhash->{$nicattr} = join(',', @tmparray); - } + foreach my $nicattr (keys %nicattrs) { + my @tmparray = (); + foreach my $nicname (keys %{$nicattrs{$nicattr}}) { + # eth0!1.1.1.1|1.2.1.1 + push @tmparray, "$nicname!$nicattrs{$nicattr}{$nicname}"; + } + # eth0!1.1.1.1|1.2.1.1,eth1!2.1.1.1|2.2.1.1 + $nodeattrhash->{$nicattr} = join(',', @tmparray); + } } 1;