2012-08-31 01:37:38 +00:00
|
|
|
# IBM(c) 2012 EPL license http://www.eclipse.org/legal/epl-v10.html
|
|
|
|
|
|
|
|
package xCAT::CFMUtils;
|
|
|
|
|
|
|
|
BEGIN
|
|
|
|
{
|
|
|
|
$::XCATROOT = $ENV{'XCATROOT'} ? $ENV{'XCATROOT'} : '/opt/xcat';
|
|
|
|
}
|
|
|
|
use lib "$::XCATROOT/lib/perl";
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
use File::Path;
|
|
|
|
use File::Copy;
|
|
|
|
use File::Find;
|
|
|
|
use Getopt::Long;
|
|
|
|
use Data::Dumper;
|
|
|
|
use File::Basename;
|
2012-09-26 02:32:10 +00:00
|
|
|
use xCAT::Table;
|
2012-08-31 01:37:38 +00:00
|
|
|
use xCAT::Utils;
|
|
|
|
use xCAT::MsgUtils;
|
|
|
|
1;
|
|
|
|
|
|
|
|
#-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
=head3 initCFMdir
|
2012-09-26 18:06:42 +00:00
|
|
|
Initialize CFM directories and files. The default layout under cfmdir is:
|
2012-09-10 02:48:52 +00:00
|
|
|
.
|
|
|
|
|-- etc
|
|
|
|
| |-- group.merge
|
2012-09-10 02:56:08 +00:00
|
|
|
| |-- hosts -> /etc/hosts
|
|
|
|
| |-- passwd.merge
|
|
|
|
| |-- shadow.merge
|
2012-09-10 02:48:52 +00:00
|
|
|
|-- group.OS
|
|
|
|
|-- passwd.OS
|
|
|
|
|-- shadow.OS
|
2012-08-31 01:37:38 +00:00
|
|
|
Note: the *.OS files are the backups for the original /etc/passwd, shadow, group files
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
$cfmdir
|
|
|
|
Returns:
|
|
|
|
0 - initialize successfully
|
|
|
|
1 - initialize failed
|
|
|
|
Globals:
|
|
|
|
none
|
|
|
|
Error:
|
|
|
|
none
|
|
|
|
Example:
|
|
|
|
xCAT::CFMUtils->initCFMdir($cfmdir);
|
|
|
|
|
|
|
|
=cut
|
|
|
|
|
|
|
|
#-----------------------------------------------------------------------------
|
|
|
|
sub initCFMdir
|
|
|
|
{
|
|
|
|
my ($class, $cfmdir) = @_;
|
|
|
|
|
|
|
|
# below system files will be synced to all compute nodes
|
|
|
|
my @sysfiles = ("/etc/hosts");
|
|
|
|
|
|
|
|
# the /etc/passwd, shadow, group files will be merged
|
|
|
|
my @userfiles = ("/etc/passwd", "/etc/shadow", "/etc/group");
|
|
|
|
|
|
|
|
if (! -d $cfmdir)
|
|
|
|
{
|
|
|
|
mkpath $cfmdir;
|
|
|
|
}
|
|
|
|
|
|
|
|
# backup original /etc/passwd, shadow, group files
|
|
|
|
foreach my $file (@userfiles)
|
|
|
|
{
|
|
|
|
my $backup = basename($file).".OS";
|
|
|
|
if (! -e "$cfmdir/$backup")
|
|
|
|
{
|
|
|
|
copy($file, "$cfmdir/$backup");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Initialize CFM directory and related files
|
|
|
|
if (! -d "$cfmdir/etc")
|
|
|
|
{
|
|
|
|
mkpath "$cfmdir/etc";
|
|
|
|
}
|
|
|
|
|
|
|
|
# link the system files
|
|
|
|
foreach my $file (@sysfiles)
|
|
|
|
{
|
|
|
|
symlink($file, "$cfmdir/$file");
|
|
|
|
}
|
|
|
|
# touch the merge files for /etc/passwd, shadow, group
|
|
|
|
foreach my $file (@userfiles)
|
|
|
|
{
|
|
|
|
my $merge = $file.".merge";
|
|
|
|
if (! -e "$cfmdir/$merge")
|
|
|
|
{
|
|
|
|
system("touch $cfmdir/$merge");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
=head3 updateUserInfo
|
|
|
|
Update the /etc/passwd, shadow, group merge files under specified CFM directory
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
$cfmdir
|
|
|
|
Returns:
|
|
|
|
0 - update successfully
|
|
|
|
1 - update failed
|
|
|
|
Globals:
|
|
|
|
$::CALLBACK
|
|
|
|
Error:
|
|
|
|
none
|
|
|
|
Example:
|
|
|
|
my $ret = xCAT::CFMUtils->updateUserInfo($cfmdir);
|
|
|
|
|
|
|
|
=cut
|
|
|
|
|
|
|
|
#-----------------------------------------------------------------------------
|
|
|
|
sub updateUserInfo {
|
|
|
|
my ($class, $cfmdir) = @_;
|
|
|
|
|
|
|
|
my @userfiles = ("/etc/passwd", "/etc/shadow", "/etc/group");
|
|
|
|
|
|
|
|
my @osfiles = glob("$cfmdir/*.OS");
|
2012-10-01 16:12:16 +00:00
|
|
|
if (!@osfiles)
|
2012-08-31 01:37:38 +00:00
|
|
|
{
|
2012-09-28 01:49:39 +00:00
|
|
|
if ($::VERBOSE)
|
2012-10-01 16:12:16 +00:00
|
|
|
{
|
2012-09-28 01:49:39 +00:00
|
|
|
my $rsp = {};
|
2012-10-08 08:55:58 +00:00
|
|
|
$rsp->{data}->[0] = "Skiping the update of the /etc/passwd, shadow, group merge files under the CFM directory.";
|
2012-09-28 01:49:39 +00:00
|
|
|
xCAT::MsgUtils->message("I", $rsp, $::CALLBACK);
|
|
|
|
}
|
2012-10-08 08:55:58 +00:00
|
|
|
return 0;
|
2012-08-31 01:37:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach my $file (@userfiles)
|
|
|
|
{
|
|
|
|
my @oldrecords = ();
|
|
|
|
my @newrecords = ();
|
|
|
|
my $backup = basename($file).".OS";
|
|
|
|
|
|
|
|
# get the records from /etc/passwd, shadow, group file and backup
|
2012-10-01 16:12:16 +00:00
|
|
|
# and all the files from /install/osimages/$imgname/cfmdir directory
|
2012-08-31 01:37:38 +00:00
|
|
|
foreach my $userinfo ($file, "$cfmdir/$backup")
|
|
|
|
{
|
|
|
|
my $fp;
|
|
|
|
open($fp, $userinfo);
|
|
|
|
my @records = ();
|
|
|
|
while (<$fp>)
|
|
|
|
{
|
|
|
|
my $line = xCAT::CFMUtils->trim($_);
|
|
|
|
if (($line =~ /^#/) || ($line =~ /^\s*$/ ))
|
|
|
|
{ #comment line or blank line
|
|
|
|
next;
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
push @records, $line;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close($fp);
|
|
|
|
|
|
|
|
# check the records from /etc/passwd, shadow, group file or backup
|
|
|
|
if ($userinfo =~ /^\/etc/ )
|
|
|
|
{
|
|
|
|
@newrecords = @records;
|
|
|
|
} else {
|
|
|
|
@oldrecords = @records;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# update the merge file
|
2012-10-08 08:55:58 +00:00
|
|
|
my $mergefile = $cfmdir."/".$file.".merge";
|
2012-08-31 01:37:38 +00:00
|
|
|
my @diff = xCAT::CFMUtils->arrayops("D", \@newrecords, \@oldrecords);
|
|
|
|
# output the diff to merge files
|
|
|
|
if (@diff)
|
|
|
|
{
|
|
|
|
my $fp;
|
|
|
|
open($fp, '>', $mergefile);
|
|
|
|
for my $record (@diff)
|
|
|
|
{
|
|
|
|
print $fp "$record\n";
|
|
|
|
}
|
|
|
|
close ($fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-09-26 02:32:10 +00:00
|
|
|
|
2012-08-31 01:37:38 +00:00
|
|
|
#-----------------------------------------------------------------------------
|
2012-09-26 02:32:10 +00:00
|
|
|
=head3 setCFMSynclistFile
|
|
|
|
Set osimage synclists attribute for CFM function, the CMF synclist file is:
|
|
|
|
/install/osimages/<imagename>/synclist.cfm
|
2012-08-31 01:37:38 +00:00
|
|
|
|
2012-09-26 02:32:10 +00:00
|
|
|
Arguments:
|
|
|
|
$imagename - the specified osimage name
|
|
|
|
Returns:
|
|
|
|
It returns the cfmdir path if it is defined for an osimage object
|
|
|
|
Globals:
|
|
|
|
$::CALLBACK
|
|
|
|
Error:
|
|
|
|
none
|
|
|
|
Example:
|
|
|
|
my $cfmdir = xCAT::CFMUtils->setCFMSynclistFile($imagename);
|
|
|
|
if ($cfmdir) { # update the CFM synclist file }
|
|
|
|
=cut
|
|
|
|
#-----------------------------------------------------------------------------
|
|
|
|
sub setCFMSynclistFile {
|
|
|
|
my ($class, $img) = @_;
|
|
|
|
|
2012-10-01 16:12:16 +00:00
|
|
|
my $cfmdir;
|
|
|
|
my $synclists;
|
2012-09-26 02:32:10 +00:00
|
|
|
my $cfmsynclist = "/install/osimages/$img/synclist.cfm";
|
|
|
|
|
|
|
|
# get the cfmdir and synclists attributes
|
2012-09-26 18:06:42 +00:00
|
|
|
my $osimage_t = xCAT::Table->new('osimage');
|
2012-09-26 02:32:10 +00:00
|
|
|
my $records = $osimage_t->getAttribs({imagename=>$img}, 'cfmdir', 'synclists');
|
|
|
|
if ($records)
|
|
|
|
{
|
|
|
|
if ($records->{'cfmdir'}) {$cfmdir = $records->{'cfmdir'}}
|
|
|
|
if ($records->{'synclists'}) {$synclists = $records->{'synclists'}}
|
2012-10-01 16:12:16 +00:00
|
|
|
} else {
|
2012-10-08 08:55:58 +00:00
|
|
|
if ($::VERBOSE)
|
|
|
|
{
|
2012-09-28 01:49:39 +00:00
|
|
|
my $rsp = {};
|
|
|
|
$rsp->{data}->[0] = "There are no records for cfmdir and synclists attribute in the osimage:$img. There is nothing to process.";
|
|
|
|
xCAT::MsgUtils->message("I", $rsp, $::CALLBACK);
|
2012-10-08 08:55:58 +00:00
|
|
|
}
|
|
|
|
return;
|
2012-09-28 01:49:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# no cfmdir defined, return directly
|
|
|
|
if (!$cfmdir)
|
|
|
|
{
|
|
|
|
return;
|
2012-09-26 02:32:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
my $found = 0;
|
2012-09-28 01:49:39 +00:00
|
|
|
my $index = 0;
|
2012-09-26 02:32:10 +00:00
|
|
|
if ($synclists)
|
|
|
|
{
|
2012-10-01 16:12:16 +00:00
|
|
|
# the synclists is a comma separated list
|
|
|
|
my @lists = split(/,/, $synclists);
|
2012-09-26 02:32:10 +00:00
|
|
|
foreach my $synclist (@lists)
|
|
|
|
{
|
2012-10-01 16:12:16 +00:00
|
|
|
# find the synclist configuration for CFM
|
|
|
|
if ($synclist eq $cfmsynclist)
|
2012-09-26 02:32:10 +00:00
|
|
|
{
|
|
|
|
$found = 1;
|
|
|
|
last;
|
|
|
|
}
|
|
|
|
$index += 1;
|
|
|
|
}
|
2012-10-01 16:12:16 +00:00
|
|
|
if ($found == 0)
|
2012-09-28 01:49:39 +00:00
|
|
|
{
|
|
|
|
# the CFM synclist is not defined, append it to $synclists
|
|
|
|
$synclists = "$synclists,$cfmsynclist";
|
|
|
|
# set the synclists attribute
|
|
|
|
$osimage_t->setAttribs({imagename=>$img}, {'synclists' => $synclists});
|
2012-10-01 16:12:16 +00:00
|
|
|
}
|
|
|
|
} else {
|
2012-09-28 01:49:39 +00:00
|
|
|
# no synclists defined, set it to CFM synclist file
|
2012-09-26 02:32:10 +00:00
|
|
|
if ($cfmdir) { $synclists = $cfmsynclist; }
|
2012-09-28 01:49:39 +00:00
|
|
|
$osimage_t->setAttribs({imagename=>$img}, {'synclists' => $synclists});
|
2012-09-26 02:32:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $cfmdir;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
=head3 updateCFMSynclistFile
|
2012-09-26 18:06:42 +00:00
|
|
|
Update the synclist file(/install/osimages/<imagename>/synclist.cfm) for CFM function.
|
2012-09-26 02:32:10 +00:00
|
|
|
It will recursively scan the files under cfmdir directory and then add them to CFM synclist file.
|
2012-08-31 01:37:38 +00:00
|
|
|
Note:
|
|
|
|
The files with suffix ".append" will be appended to the dest file(records in "APPEND:" section).
|
|
|
|
The files with suffix ".merge" will be merged to the dest file(records in "MERGE:" section).
|
|
|
|
|
|
|
|
In addition, it will reserve the user specified records in the synclist file. The example synclist file:
|
|
|
|
<cfmdir>/etc/hosts -> /etc/hosts
|
|
|
|
/root/install.log -> /tmp/install.log
|
|
|
|
...
|
|
|
|
|
|
|
|
APPEND:
|
|
|
|
<cfmdir>/etc/hosts.append -> /etc/hosts
|
|
|
|
/root/install.log.syslog -> /tmp/install.log
|
|
|
|
...
|
|
|
|
EXECUTE:
|
|
|
|
...
|
|
|
|
EXECUTEALWAYS:
|
|
|
|
...
|
|
|
|
MERGE:
|
|
|
|
<cfmdir>/etc/group.merge -> /etc/group
|
|
|
|
<cfmdir>/etc/shadow.merge -> /etc/shadow
|
|
|
|
<cfmdir>/etc/passwd.merge -> /etc/passwd
|
|
|
|
|
|
|
|
Arguments:
|
2012-09-26 02:32:10 +00:00
|
|
|
\@imagenames - reference to the osimage names array
|
2012-08-31 01:37:38 +00:00
|
|
|
Returns:
|
|
|
|
0 - update successfully
|
|
|
|
1 - update failed
|
|
|
|
Globals:
|
|
|
|
$::CALLBACK
|
|
|
|
Error:
|
|
|
|
none
|
|
|
|
Example:
|
2012-09-26 02:32:10 +00:00
|
|
|
my $ret = CAT::CFMUtils->updateCFMSynclistFile(\@imagenames);
|
2012-08-31 01:37:38 +00:00
|
|
|
|
|
|
|
=cut
|
|
|
|
|
|
|
|
#-----------------------------------------------------------------------------
|
2012-09-26 02:32:10 +00:00
|
|
|
sub updateCFMSynclistFile {
|
|
|
|
my ($class, $imgs) = @_;
|
2012-08-31 01:37:38 +00:00
|
|
|
|
2012-09-26 02:32:10 +00:00
|
|
|
my @osimgs = @$imgs;
|
|
|
|
if (!@osimgs)
|
2012-08-31 01:37:38 +00:00
|
|
|
{
|
|
|
|
my $rsp = {};
|
2012-09-26 18:06:42 +00:00
|
|
|
$rsp->{error}->[0] = "No osimage names specified to process.";
|
2012-08-31 01:37:38 +00:00
|
|
|
xCAT::MsgUtils->message("E", $rsp, $::CALLBACK);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-09-26 02:32:10 +00:00
|
|
|
foreach my $osimg (@osimgs)
|
2012-08-31 01:37:38 +00:00
|
|
|
{
|
2012-10-01 16:12:16 +00:00
|
|
|
my $cfmdir;
|
2012-09-26 02:32:10 +00:00
|
|
|
$cfmdir = xCAT::CFMUtils->setCFMSynclistFile($osimg);
|
2012-10-01 16:12:16 +00:00
|
|
|
if ($cfmdir) # check for /install/osiamges/$osimg/cfmdir
|
2012-09-26 02:32:10 +00:00
|
|
|
{
|
|
|
|
my $cfmsynclist = "/install/osimages/$osimg/synclist.cfm";
|
|
|
|
if (! -d $cfmdir)
|
|
|
|
{
|
2012-10-08 08:55:58 +00:00
|
|
|
# skip this one go on to the next image, nothing to do for
|
2012-10-01 16:12:16 +00:00
|
|
|
# CFMUtils in this image
|
|
|
|
next;
|
2012-09-26 02:32:10 +00:00
|
|
|
}
|
2012-10-08 08:55:58 +00:00
|
|
|
# create the parent directory of CFM synclist file
|
|
|
|
if (! -d dirname($cfmsynclist))
|
|
|
|
{
|
|
|
|
mkpath dirname($cfmsynclist);
|
|
|
|
}
|
2012-08-31 01:37:38 +00:00
|
|
|
|
2012-09-26 02:32:10 +00:00
|
|
|
# update /etc/passwd, shadow, group merge files
|
|
|
|
my $ret = xCAT::CFMUtils->updateUserInfo($cfmdir);
|
2012-10-01 16:12:16 +00:00
|
|
|
if ($ret !=0 )
|
2012-09-26 02:32:10 +00:00
|
|
|
{
|
|
|
|
my $rsp = {};
|
2012-10-01 16:12:16 +00:00
|
|
|
$rsp->{error}->[0] =
|
|
|
|
"Update /etc/passwd, shadow, group merge files failed.";
|
2012-09-26 02:32:10 +00:00
|
|
|
xCAT::MsgUtils->message("E", $rsp, $::CALLBACK);
|
2012-10-01 16:12:16 +00:00
|
|
|
return 1;
|
2012-09-26 02:32:10 +00:00
|
|
|
}
|
2012-08-31 01:37:38 +00:00
|
|
|
|
2012-09-26 02:32:10 +00:00
|
|
|
# recursively list the files under cfm directory
|
|
|
|
my @files = ();
|
|
|
|
find ( sub { push @files, $File::Find::name if (! -d) }, $cfmdir);
|
|
|
|
if (!@files) # not files under cfm directory, skip to next loop
|
|
|
|
{
|
|
|
|
next;
|
|
|
|
}
|
2012-08-31 01:37:38 +00:00
|
|
|
|
2012-09-26 02:32:10 +00:00
|
|
|
my $fp;
|
|
|
|
open($fp, '>', $cfmsynclist);
|
|
|
|
my @mergefiles = ();
|
|
|
|
my @appendfiles = ();
|
|
|
|
foreach my $file (@files)
|
|
|
|
{
|
|
|
|
my $name = basename($file);
|
2012-10-01 16:12:16 +00:00
|
|
|
#TODO: find a better way to get the suffix
|
2012-09-26 02:32:10 +00:00
|
|
|
my $suffix = ($name =~ m/([^.]+)$/)[0];
|
|
|
|
my $dest = substr($file, length($cfmdir));
|
|
|
|
if ($suffix eq "OS") # skip the backup files
|
|
|
|
{
|
|
|
|
next;
|
|
|
|
} elsif ($suffix eq "merge") # merge file
|
|
|
|
{
|
|
|
|
push(@mergefiles, $file);
|
|
|
|
} elsif ($suffix eq "append") { # append file
|
|
|
|
push(@appendfiles, $file);
|
|
|
|
} else { # output the syncing files maintained by CFM
|
|
|
|
print $fp "$file -> $dest\n";
|
|
|
|
}
|
|
|
|
}
|
2012-10-08 08:55:58 +00:00
|
|
|
|
|
|
|
# output the APPEND records maintained by CFM
|
|
|
|
if (@appendfiles) {
|
|
|
|
print $fp "APPEND:\n";
|
2012-10-01 16:12:16 +00:00
|
|
|
}
|
2012-09-26 02:32:10 +00:00
|
|
|
foreach my $file (@appendfiles)
|
|
|
|
{
|
|
|
|
my $dest = substr($file, length($cfmdir), length($file) - length(".append") - length($cfmdir));
|
|
|
|
print $fp "$file -> $dest\n";
|
|
|
|
}
|
|
|
|
|
2012-10-01 16:12:16 +00:00
|
|
|
# output the MERGE records maintained by CFM
|
2012-10-08 08:55:58 +00:00
|
|
|
if (@mergefiles) {
|
|
|
|
print $fp "MERGE:\n";
|
2012-10-01 16:12:16 +00:00
|
|
|
}
|
2012-09-26 02:32:10 +00:00
|
|
|
foreach my $file (@mergefiles)
|
|
|
|
{
|
2012-10-08 08:55:58 +00:00
|
|
|
my @userfiles = ("/etc/passwd", "/etc/shadow", "/etc/group");
|
2012-09-26 02:32:10 +00:00
|
|
|
my $dest = substr($file, length($cfmdir), length($file) - length(".merge") - length($cfmdir));
|
2012-10-08 08:55:58 +00:00
|
|
|
# only /etc/passwd, /etc/shadow, /etc/groups merging is supported
|
|
|
|
if (grep(/$dest/, @userfiles)) {
|
|
|
|
print $fp "$file -> $dest\n";
|
|
|
|
}
|
2012-09-26 02:32:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# close the file
|
|
|
|
close($fp);
|
2012-08-31 01:37:38 +00:00
|
|
|
}
|
|
|
|
}
|
2012-09-26 02:32:10 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2012-08-31 01:37:38 +00:00
|
|
|
|
2012-09-26 02:32:10 +00:00
|
|
|
#-----------------------------------------------------------------------------
|
|
|
|
=head3 setCFMPkglistFile
|
|
|
|
Set the pkglist attribute of linuximage object for CFM function
|
2012-08-31 01:37:38 +00:00
|
|
|
|
2012-09-26 02:32:10 +00:00
|
|
|
Arguments:
|
|
|
|
$imagename - the specified linuximage name
|
|
|
|
Returns:
|
|
|
|
0 - update successfully
|
|
|
|
1 - update failed
|
|
|
|
Globals:
|
|
|
|
$::CALLBACK
|
|
|
|
Error:
|
|
|
|
none
|
|
|
|
Example:
|
|
|
|
my $ret = xCAT::CFMUtils->setCFMPkglistFile($imagename);
|
|
|
|
=cut
|
|
|
|
#-----------------------------------------------------------------------------
|
|
|
|
sub setCFMPkglistFile {
|
|
|
|
my ($class, $img) = @_;
|
2012-08-31 01:37:38 +00:00
|
|
|
|
2012-09-26 02:32:10 +00:00
|
|
|
my $pkglists = "";
|
|
|
|
my $cfmpkglist = "/install/osimages/$img/pkglist.cfm";
|
|
|
|
|
|
|
|
# get the pkglist files
|
2012-09-26 18:06:42 +00:00
|
|
|
my $linuximage_t = xCAT::Table->new('linuximage');
|
2012-09-26 02:32:10 +00:00
|
|
|
my $records = $linuximage_t->getAttribs({imagename => $img}, 'pkglist');
|
|
|
|
if ($records)
|
|
|
|
{
|
|
|
|
if ($records->{'pkglist'}) { $pkglists = $records->{'pkglist'}; }
|
|
|
|
} else
|
2012-08-31 01:37:38 +00:00
|
|
|
{
|
2012-09-28 01:49:39 +00:00
|
|
|
if ($::VERBOSE)
|
|
|
|
{
|
|
|
|
my $rsp = {};
|
|
|
|
$rsp->{data}->[0] = "There are no records for pkglist attribute in the linuximage:$img. There is nothing to process.";
|
|
|
|
xCAT::MsgUtils->message("I", $rsp, $::CALLBACK);
|
|
|
|
}
|
2012-08-31 01:37:38 +00:00
|
|
|
}
|
|
|
|
|
2012-09-26 02:32:10 +00:00
|
|
|
my $found = 0;
|
|
|
|
if ($pkglists)
|
2012-08-31 01:37:38 +00:00
|
|
|
{
|
2012-09-26 02:32:10 +00:00
|
|
|
foreach my $pkglist (split(/,/, $pkglists))
|
|
|
|
{
|
2012-09-28 01:49:39 +00:00
|
|
|
if ($pkglist eq $cfmpkglist) # the pkglist file for CFM is found, exit the loop
|
2012-09-26 02:32:10 +00:00
|
|
|
{
|
|
|
|
$found = 1;
|
|
|
|
last;
|
|
|
|
}
|
|
|
|
}
|
2012-09-28 01:49:39 +00:00
|
|
|
# the pkglist file for CFM is not found, append it to $pkglits
|
|
|
|
if (!$found)
|
|
|
|
{
|
|
|
|
$pkglists = "$pkglists,$cfmpkglist";
|
|
|
|
# set the pkglist attribute for linuximage
|
|
|
|
$linuximage_t->setAttribs({imagename => $img}, {'pkglist' => $pkglists});
|
|
|
|
}
|
2012-09-26 02:32:10 +00:00
|
|
|
} else
|
2012-08-31 01:37:38 +00:00
|
|
|
{
|
2012-09-28 01:49:39 +00:00
|
|
|
# the pkglist file for linuximage is not defined, set it to $cfmpkglist
|
2012-09-26 02:32:10 +00:00
|
|
|
$pkglists = $cfmpkglist;
|
2012-09-28 01:49:39 +00:00
|
|
|
$linuximage_t->setAttribs({imagename => $img}, {'pkglist' => $pkglists});
|
2012-08-31 01:37:38 +00:00
|
|
|
}
|
2012-09-26 02:32:10 +00:00
|
|
|
|
|
|
|
return 0;
|
2012-08-31 01:37:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#-----------------------------------------------------------------------------
|
|
|
|
|
2012-09-26 02:32:10 +00:00
|
|
|
=head3 updateCFMPkglistFile
|
2012-08-31 01:37:38 +00:00
|
|
|
Update the ospkglist file
|
|
|
|
|
|
|
|
Arguments:
|
2012-09-26 02:32:10 +00:00
|
|
|
$imagename - the specified linuximage name
|
2012-08-31 01:37:38 +00:00
|
|
|
@curospkgs - the currently selected OS packages list
|
|
|
|
Returns:
|
|
|
|
0 - update successfully
|
|
|
|
1 - update failed
|
|
|
|
Globals:
|
|
|
|
none
|
|
|
|
Error:
|
|
|
|
none
|
|
|
|
Example:
|
2012-09-26 02:32:10 +00:00
|
|
|
my $ret = CAT::CFMUtils->updateCFMPkglistFile($imagename, @cur_selected_pkgs);
|
2012-08-31 01:37:38 +00:00
|
|
|
|
|
|
|
=cut
|
|
|
|
|
|
|
|
#-----------------------------------------------------------------------------
|
2012-09-26 02:32:10 +00:00
|
|
|
sub updateCFMPkglistFile {
|
|
|
|
my ($class, $img, $ospkgs) = @_;
|
|
|
|
|
2012-08-31 01:37:38 +00:00
|
|
|
my @cur_selected = @$ospkgs;
|
2012-09-26 02:32:10 +00:00
|
|
|
my $cfmpkglist = "/install/osimages/$img/pkglist.cfm";
|
|
|
|
|
|
|
|
my $ret = xCAT::CFMUtils->setCFMPkglistFile($img);
|
|
|
|
if ($ret)
|
|
|
|
{
|
|
|
|
my $rsp = {};
|
|
|
|
$rsp->{error}->[0] = "Set pkglist attribute for CFM failed.";
|
|
|
|
xCAT::MsgUtils->message("E", $rsp, $::CALLBACK);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-10-08 08:55:58 +00:00
|
|
|
# check the parent directory of cfmpkglist file
|
2012-09-26 02:32:10 +00:00
|
|
|
if (! -d dirname($cfmpkglist))
|
|
|
|
{
|
|
|
|
mkpath dirname($cfmpkglist);
|
|
|
|
}
|
2012-08-31 01:37:38 +00:00
|
|
|
|
|
|
|
# get previous selected and removed OS packages list from pkglist file
|
2012-09-26 02:32:10 +00:00
|
|
|
my ($pre_selected_ref, $pre_removed_ref) = xCAT::CFMUtils->getPreOSpkgsList($cfmpkglist);
|
2012-08-31 01:37:38 +00:00
|
|
|
my @pre_selected = @$pre_selected_ref;
|
|
|
|
my @pre_removed = @$pre_removed_ref;
|
|
|
|
|
|
|
|
# get diff between previous and current selected OS packages lists
|
|
|
|
my @diff = xCAT::CFMUtils->getOSpkgsDiff(\@pre_selected, \@cur_selected);
|
|
|
|
|
|
|
|
# merge the diff to previous removed OS packages list
|
|
|
|
my @all_removed = xCAT::CFMUtils->arrayops("U", \@pre_removed, \@diff);
|
|
|
|
|
|
|
|
# get the rollbacked OS packages list, the packages are existing in both removed and selected lists
|
|
|
|
# if so, we should remove the rollbacked OS packages from removed list
|
|
|
|
my @rollback = xCAT::CFMUtils->arrayops("I", \@all_removed, \@cur_selected);
|
|
|
|
my @cur_removed = xCAT::CFMUtils->arrayops("D", \@all_removed, \@rollback);
|
|
|
|
|
|
|
|
# update the pkglist file
|
|
|
|
my $fp;
|
2012-09-26 02:32:10 +00:00
|
|
|
open($fp, '>', $cfmpkglist);
|
2012-08-31 01:37:38 +00:00
|
|
|
# the pacakges be installed
|
|
|
|
if (@cur_selected)
|
|
|
|
{
|
|
|
|
print $fp "#The OS packages be installed:\n";
|
|
|
|
foreach my $pkg (@cur_selected)
|
|
|
|
{
|
|
|
|
print $fp "$pkg\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
# the packages be removed
|
|
|
|
if (@cur_removed)
|
|
|
|
{
|
|
|
|
print $fp "#The OS packages be removed:\n";
|
|
|
|
foreach my $pkg (@cur_removed)
|
|
|
|
{
|
|
|
|
print $fp "-$pkg\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
# close the file
|
|
|
|
close($fp);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
=head3 getPreOSpkgsList
|
|
|
|
Get previously selected and removed OS packages lists from pkglist file
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
$ospkglist - the path for ospkglist file
|
|
|
|
Returns:
|
|
|
|
refs for selected and removed OS packages arrays
|
|
|
|
Globals:
|
|
|
|
none
|
|
|
|
Error:
|
|
|
|
none
|
|
|
|
Example:
|
|
|
|
my ($pre_selected_ref, $pre_removed_ref) = xCAT::CFMUtils->getPreOSpkgsList($ospkglist);
|
|
|
|
my @pre_selected = @$pre_selected_ref;
|
|
|
|
my @pre_removed = @$pre_removed_ref;
|
|
|
|
|
|
|
|
=cut
|
|
|
|
|
|
|
|
#-----------------------------------------------------------------------------
|
|
|
|
sub getPreOSpkgsList {
|
|
|
|
my ($class, $pkglist) = @_;
|
|
|
|
my @selected = ();
|
|
|
|
my @removed = ();
|
|
|
|
|
|
|
|
my $pkglistfp;
|
|
|
|
open($pkglistfp, $pkglist);
|
|
|
|
while (<$pkglistfp>)
|
|
|
|
{
|
|
|
|
my $line = xCAT::CFMUtils->trim($_);
|
|
|
|
if (($line =~ /^#/) || ($line =~ /^\s*$/ ))
|
|
|
|
{ #comment line or blank line
|
|
|
|
next;
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
if ($line =~ /^-/)
|
|
|
|
{ # the package be removed
|
|
|
|
push @removed, substr($line, 1);
|
|
|
|
} else
|
|
|
|
{ # the package be installed
|
|
|
|
push @selected, $line;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close($pkglistfp);
|
|
|
|
|
|
|
|
return (\@selected, \@removed);
|
|
|
|
}
|
|
|
|
|
|
|
|
#-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
=head3 getOSpkgsDiff
|
|
|
|
Get the differences between previous and current OS packages list
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
@pre - previous selected OS packages list
|
|
|
|
@cur - current selected OS packages list
|
|
|
|
Returns:
|
|
|
|
@diff - the differencen list
|
|
|
|
Globals:
|
|
|
|
none
|
|
|
|
Error:
|
|
|
|
none
|
|
|
|
Example:
|
|
|
|
my @diff = xCAT::CFMUtils->getOSpkgsDiff(\@pre_selected, \@cur_selected);
|
|
|
|
|
|
|
|
=cut
|
|
|
|
|
|
|
|
#-----------------------------------------------------------------------------
|
|
|
|
sub getOSpkgsDiff {
|
|
|
|
my ($class, $pre, $cur) = @_;
|
|
|
|
|
|
|
|
# get the intersection firstly
|
|
|
|
my @tmp = xCAT::CFMUtils->arrayops("I", \@$pre, \@$cur);
|
|
|
|
|
|
|
|
# get the difference
|
|
|
|
my @diff = xCAT::CFMUtils->arrayops("D", \@$pre, \@tmp);
|
|
|
|
#print Dumper(@diff);
|
|
|
|
|
|
|
|
return @diff;
|
|
|
|
}
|
|
|
|
|
|
|
|
#-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
=head3 trim
|
|
|
|
Strip left and right whitspaces for a string
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
$string
|
|
|
|
Returns:
|
|
|
|
@string
|
|
|
|
Globals:
|
|
|
|
none
|
|
|
|
Error:
|
|
|
|
none
|
|
|
|
Example:
|
|
|
|
my @new_string = xCAT::CFMUtils->trim($string);
|
|
|
|
|
|
|
|
=cut
|
|
|
|
|
|
|
|
#-----------------------------------------------------------------------------
|
|
|
|
sub trim {
|
|
|
|
my ($class, $string) = @_;
|
|
|
|
|
|
|
|
# trim the left whitespaces
|
|
|
|
$string =~ s/^\s*//;
|
|
|
|
|
|
|
|
# trim the right whitespaces
|
|
|
|
$string =~ s/\s*$//;
|
|
|
|
|
|
|
|
return $string;
|
|
|
|
}
|
|
|
|
|
|
|
|
# Function: compute Union, Intersection or Difference of unique lists
|
|
|
|
# Usage: arrayops ("U"/"I"/"D", @a, @b)
|
|
|
|
# Return: @union/@intersection/@difference
|
|
|
|
#-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
=head3 arrayops
|
|
|
|
Compute Union/Intersection/Difference for 2 unique lists
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
$flag - "U"/"I"/"D"
|
2012-09-26 02:32:10 +00:00
|
|
|
\@array1 - reference to an arrary
|
|
|
|
\@array2 - reference to an arrary
|
2012-08-31 01:37:38 +00:00
|
|
|
Returns:
|
|
|
|
@union/@intersection/@difference
|
|
|
|
Globals:
|
|
|
|
none
|
|
|
|
Error:
|
|
|
|
none
|
|
|
|
Example:
|
2012-09-26 02:32:10 +00:00
|
|
|
my @array = xCAT::CFMUtils->arrayops(\@array1, \@array2);
|
2012-08-31 01:37:38 +00:00
|
|
|
|
|
|
|
=cut
|
|
|
|
|
|
|
|
#-----------------------------------------------------------------------------
|
|
|
|
sub arrayops {
|
|
|
|
my ($class, $ops, $array1, $array2) = @_;
|
|
|
|
|
|
|
|
my @union = ();
|
|
|
|
my @intersection = ();
|
|
|
|
my @difference = ();
|
|
|
|
my %count = ();
|
|
|
|
foreach my $element (@$array1, @$array2)
|
|
|
|
{
|
|
|
|
$count{$element}++
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach my $element (keys %count) {
|
|
|
|
push @union, $element;
|
|
|
|
push @{ $count{$element} > 1 ? \@intersection : \@difference }, $element;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($ops eq "U") { return @union; }
|
|
|
|
|
|
|
|
if ($ops eq "I") { return @intersection; }
|
|
|
|
|
|
|
|
if ($ops eq "D") { return @difference; }
|
|
|
|
|
|
|
|
#return (\@union, \@intersection, \@difference);
|
|
|
|
}
|