#!/usr/bin/env perl # IBM(c) 2007 EPL license http://www.eclipse.org/legal/epl-v10.html package xCAT::Utils; use xCAT::Table; use xCAT::Schema; use Data::Dumper; use xCAT::NodeRange; #-------------------------------------------------------------------------------- =head1 xCAT::Utils =head2 Package Description This program module file, is a set of utilities used by xCAT commands. =cut #-------------------------------------------------------------------------------- =head3 quote Quote a string, taking into account embedded quotes. This function is most useful when passing string through the shell to another cmd. It handles one level of embedded double quotes, single quotes, and dollar signs. Arguments: string to quote Returns: quoted string Globals: none Error: none Example: if (defined($$opthashref{'WhereStr'})) { $where = xCAT::Utils->quote($$opthashref{'WhereStr'}); } Comments: none =cut #-------------------------------------------------------------------------------- sub quote { my ($class, $str) = @_; # if the value has imbedded double quotes, use single quotes. If it also has # single quotes, escape the double quotes. if (!($str =~ /\"/)) # no embedded double quotes { $str =~ s/\$/\\\$/sg; # escape the dollar signs $str =~ s/\`/\\\`/sg; $str = qq("$str"); } elsif (!($str =~ /\'/)) { $str = qq('$str'); } # no embedded single quotes else # has both embedded double and single quotes { # Escape the double quotes. (Escaping single quotes does not seem to work # in the shells.) $str =~ s/\"/\\\"/sg; #" this comment helps formating $str =~ s/\$/\\\$/sg; # escape the dollar signs $str =~ s/\`/\\\`/sg; $str = qq("$str"); } } #------------------------------------------------------------------------------- =head3 isAIX returns 1 if localHost is AIX Arguments: none Returns: 1 - localHost is AIX 0 - localHost is some other platform Globals: none Error: none Example: if (xCAT::Utils->isAIX()) { blah; } Comments: none =cut #------------------------------------------------------------------------------- sub isAIX { if ($^O =~ /^aix/i) { return 1; } else { return 0; } } #------------------------------------------------------------------------------- =head3 isLinux returns 1 if localHost is Linux Arguments: none Returns: 1 - localHost is Linux 0 - localHost is some other platform Globals: none Error: none Example: if (xCAT::Utils->isLinux()) { blah; } Comments: none =cut #------------------------------------------------------------------------------- sub isLinux { if ($^O =~ /^linux/i) { return 1; } else { return 0; } } #------------------------------------------------------------------------------- =head3 make_node_list_file Makes a node list file. Arguments: (\@list_of_nodes) - reference to an arrary of nodes. Returns: $file_name and sets the global var: $::NODE_LIST_FILE Globals: the ENV vars: DSH_LIST, RPOWER_LIST, RCONSOLE_LIST Error: None documented Example: xCAT::Utils->make_node_list_file(\@nodelist); Comments: IMPORTANT: Make sure to cleanup afterwards with: xCAT::Utils->close_delete_file($file_handle, $file_name) =cut #-------------------------------------------------------------------------------- sub make_node_list_file { my ($class, $ref_node_list) = @_; my @node_list = @$ref_node_list; srand(time | $$); #random number generator start my $file = "/tmp/csm_$$"; while (-e $file) { $file = xCAT::Utils->CreateRandomName($file); } open($::NODE_LIST_FILE, ">$file") or MsgUtils->message("E", "Cannot write to file: $file\n"); foreach my $node (@node_list) { print $::NODE_LIST_FILE "$node\n"; } return $file; } #-------------------------------------------------------------------------------- =head3 CreateRandomName Create a randome file name. Arguments: Prefix of name Returns: Prefix with 8 random letters appended Error: none Example: $file = xCAT::Utils->CreateRandomName($namePrefix); Comments: None =cut #------------------------------------------------------------------------------- sub CreateRandomName { my ($class, $name) = @_; my $nI; for ($nI = 0 ; $nI < 8 ; $nI++) { my $char = ('a' .. 'z', 'A' .. 'Z')[int(rand(52)) + 1]; $name .= $char; } $name; } #----------------------------------------------------------------------- =head3 close_delete_file. Arguments: file handle,filename Returns: none Globals: none Error: undef Example: xCAT::Utils->close_delete_file($file_handle, $file_name); Comments: none =cut #------------------------------------------------------------------------ sub close_delete_file { my ($class, $file_handle, $file_name) = @_; close $file_handle; unlink($file_name); } #----------------------------------------------------------------------- =head3 list_all_nodes Arguments: Returns: an array of all define nodes from the nodelist table Globals: none Error: undef Example: @nodes=xCAT::Utils->list_all_nodes; Comments: none =cut #------------------------------------------------------------------------ sub list_all_nodes { my @nodes; my @nodelist; my $nodelisttab; if ($nodelisttab = xCAT::Table->new("nodelist")) { my @attribs = ("node"); @nodes = $nodelisttab->getAllAttribs(@attribs); foreach my $node (@nodes) { push @nodelist, $node->{node}; } } else { xCAT::MsgUtils->message("E", " Could not read the nodelist table\n"); } return @nodelist; } #----------------------------------------------------------------------- =head3 list_all_nodegroups Arguments: Returns: an array of all define node groups from the nodelist table Globals: none Error: undef Example: @nodegrps=xCAT::Utils->list_all_nodegroups; Comments: none =cut #------------------------------------------------------------------------ sub list_all_node_groups { my @grouplist; my @grouplist2; my @distinctgroups; my $nodelisttab; if ($nodelisttab = xCAT::Table->new("nodelist")) { my @attribs = ("groups"); @grouplist = $nodelisttab->getAllAttribs(@attribs); # build a distinct list of unique group names foreach my $group (@grouplist) { my $gnames = $group->{groups}; my @groupnames = split ",", $gnames; foreach my $groupname (@groupnames) { if (!grep(/$groupname/, @distinctgroups)) { # not already in list push @distinctgroups, $groupname; } } } } else { xCAT::MsgUtils->message("E", " Could not read the nodelist table\n"); } return @distinctgroups; } #----------------------------------------------------------------------- =head3 list_nodes_in_nodegroup Arguments: nodegroup Returns: an array of all define nodes in the node group Globals: none Error: undef Example: @nodes=xCAT::Utils->list_nodes_in_nodegroup($group); Comments: none =cut #------------------------------------------------------------------------ sub list_nodes_in_nodegroups { my ($class, $group) = @_; $req->{noderange}->[0] = $group; my @nodes = noderange($req->{noderange}->[0]); return @nodes; } #----------------------------------------------------------------------- =head3 get_site_attribute Arguments: Returns: The value of the attribute requested from the site table Globals: none Error: undef Example: @attr=xCAT::Utils->get_site_attribute($attribute); Comments: none =cut #------------------------------------------------------------------------ sub get_site_attribute { my ($class, $attr) = @_; my $values; my $sitetab = xCAT::Table->new('site'); if ($sitetab) { (my $ref) = $sitetab->getAttribs({key => $attr}, value); if ($ref and $ref->{value}) { $values = $ref->{value}; } } else { xCAT::MsgUtils->message("E", " Could not read the site table\n"); } return $values; } 1;