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
|
2012-11-30 07:14:06 +00:00
|
|
|
| |-- group.merge -> /etc/group.merge
|
2012-09-10 02:56:08 +00:00
|
|
|
| |-- hosts -> /etc/hosts
|
2012-11-30 07:14:06 +00:00
|
|
|
| |-- passwd.merge -> /etc/passwd.merge
|
|
|
|
| |-- shadow.merge -> /etc/shadow.merge
|
|
|
|
|-- group.OS -> /etc/group.OS
|
|
|
|
|-- passwd.OS -> /etc/passwd.OS
|
|
|
|
|-- shadow.OS -> /etc/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");
|
|
|
|
|
2012-11-30 07:14:06 +00:00
|
|
|
# create the cfmdir
|
2012-08-31 01:37:38 +00:00
|
|
|
if (! -d $cfmdir)
|
|
|
|
{
|
|
|
|
mkpath $cfmdir;
|
|
|
|
}
|
|
|
|
|
2012-11-30 07:14:06 +00:00
|
|
|
# backup the OS files and create links under cfmdir
|
2012-08-31 01:37:38 +00:00
|
|
|
foreach my $file (@userfiles)
|
|
|
|
{
|
2012-11-30 07:14:06 +00:00
|
|
|
my $backup = $file.".OS";
|
|
|
|
if (! -e $backup)
|
2012-08-31 01:37:38 +00:00
|
|
|
{
|
2012-11-30 07:14:06 +00:00
|
|
|
copy($file, $backup);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! -e "$cfmdir/".basename($backup))
|
|
|
|
{
|
|
|
|
symlink($backup, "$cfmdir/".basename($backup));
|
2012-08-31 01:37:38 +00:00
|
|
|
}
|
|
|
|
}
|
2012-11-30 07:14:06 +00:00
|
|
|
|
2012-08-31 01:37:38 +00:00
|
|
|
# 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");
|
|
|
|
}
|
2012-11-30 07:14:06 +00:00
|
|
|
# touch and link the merge files for /etc/passwd, shadow, group
|
2012-08-31 01:37:38 +00:00
|
|
|
foreach my $file (@userfiles)
|
|
|
|
{
|
|
|
|
my $merge = $file.".merge";
|
2012-11-30 07:14:06 +00:00
|
|
|
if (! -e "$merge")
|
|
|
|
{
|
|
|
|
xCAT::Utils->runcmd("touch $merge", -1);
|
|
|
|
}
|
|
|
|
|
2012-08-31 01:37:38 +00:00
|
|
|
if (! -e "$cfmdir/$merge")
|
|
|
|
{
|
2012-11-30 07:14:06 +00:00
|
|
|
symlink($merge, "$cfmdir/$merge");
|
2012-08-31 01:37:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
=head3 updateUserInfo
|
|
|
|
Update the /etc/passwd, shadow, group merge files under specified CFM directory
|
|
|
|
|
|
|
|
Arguments:
|
2012-11-30 07:14:06 +00:00
|
|
|
$cfmdir - CFM directory for osimage
|
2012-08-31 01:37:38 +00:00
|
|
|
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";
|
|
|
|
|
2012-11-30 07:14:06 +00:00
|
|
|
# get the records from /etc/passwd, shadow, group file and backup files(.OS files)
|
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
|
2012-11-30 07:14:06 +00:00
|
|
|
my $fp;
|
|
|
|
open($fp, '>', $mergefile);
|
2012-08-31 01:37:38 +00:00
|
|
|
if (@diff)
|
|
|
|
{
|
|
|
|
for my $record (@diff)
|
|
|
|
{
|
2013-07-22 05:55:43 +00:00
|
|
|
# skip to add ROOT relative records into MERGE file
|
|
|
|
if ($record =~ /^root/)
|
|
|
|
{
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
print $fp "$record\n";
|
2012-08-31 01:37:38 +00:00
|
|
|
}
|
|
|
|
}
|
2012-11-30 07:14:06 +00:00
|
|
|
close ($fp);
|
2012-08-31 01:37:38 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
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');
|
2013-09-17 02:32:49 +00:00
|
|
|
if (defined ($records->{'cfmdir'}))
|
2012-09-26 02:32:10 +00:00
|
|
|
{
|
2013-09-17 02:32:49 +00:00
|
|
|
$cfmdir = $records->{'cfmdir'};
|
|
|
|
if (defined ($records->{'synclists'})) {$synclists = $records->{'synclists'}}
|
2012-10-01 16:12:16 +00:00
|
|
|
} else {
|
2013-09-17 02:32:49 +00:00
|
|
|
# no cfmdir defined, return directly
|
|
|
|
return 0;
|
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 = ();
|
2013-09-22 06:34:40 +00:00
|
|
|
|
|
|
|
find ( { wanted => sub { push @files, $File::Find::name if -f }, follow => 1 }, $cfmdir);
|
2012-09-26 02:32:10 +00:00
|
|
|
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;
|
|
|
|
|
2013-01-18 08:13:08 +00:00
|
|
|
# get the #INCLUDE file from cfmpkglist file
|
|
|
|
my @incfiles = xCAT::CFMUtils->getIncludefiles($cfmpkglist);
|
|
|
|
# get the packages list in the #INCLUDE files
|
|
|
|
my @basepkgs = ();
|
|
|
|
foreach my $inc (@incfiles)
|
|
|
|
{
|
|
|
|
my ($selected_ref, $removed_ref) = xCAT::CFMUtils->getPreOSpkgsList($inc);
|
|
|
|
my @selected = @$selected_ref;
|
|
|
|
@basepkgs = xCAT::CFMUtils->arrayops("U", \@basepkgs, \@selected);
|
|
|
|
}
|
|
|
|
|
2012-08-31 01:37:38 +00:00
|
|
|
# get diff between previous and current selected OS packages lists
|
2012-10-25 06:19:15 +00:00
|
|
|
my @diff = xCAT::CFMUtils->getPkgsDiff(\@pre_selected, \@cur_selected);
|
2012-08-31 01:37:38 +00:00
|
|
|
|
|
|
|
# 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);
|
|
|
|
|
2013-01-18 08:13:08 +00:00
|
|
|
# remove the BASE packages from selected pakages
|
|
|
|
@basepkgs = xCAT::CFMUtils->arrayops("I", \@basepkgs, \@cur_selected);
|
|
|
|
@cur_selected = xCAT::CFMUtils->arrayops("D", \@cur_selected, \@basepkgs);
|
|
|
|
|
2012-08-31 01:37:38 +00:00
|
|
|
# update the pkglist file
|
|
|
|
my $fp;
|
2012-09-26 02:32:10 +00:00
|
|
|
open($fp, '>', $cfmpkglist);
|
2013-01-18 08:13:08 +00:00
|
|
|
foreach my $inc (@incfiles)
|
|
|
|
{
|
|
|
|
print $fp "#INCLUDE:$inc#\n";
|
|
|
|
}
|
2012-08-31 01:37:38 +00:00
|
|
|
# the pacakges be installed
|
|
|
|
if (@cur_selected)
|
|
|
|
{
|
|
|
|
foreach my $pkg (@cur_selected)
|
|
|
|
{
|
|
|
|
print $fp "$pkg\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
# the packages be removed
|
|
|
|
if (@cur_removed)
|
|
|
|
{
|
|
|
|
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 = ();
|
2013-01-18 08:13:08 +00:00
|
|
|
my @pkglistfiles = ();
|
2012-08-31 01:37:38 +00:00
|
|
|
|
2013-01-18 08:13:08 +00:00
|
|
|
# get the #INCLUDE file from cfmpkglist file
|
|
|
|
my @incfiles = xCAT::CFMUtils->getIncludefiles($pkglist);
|
|
|
|
foreach my $inc (@incfiles)
|
2012-08-31 01:37:38 +00:00
|
|
|
{
|
2013-01-18 08:13:08 +00:00
|
|
|
push @pkglistfiles, $inc;
|
|
|
|
}
|
|
|
|
# assume the #INCLUDE file includes the BASE packages
|
|
|
|
push @pkglistfiles, $pkglist;
|
|
|
|
|
|
|
|
foreach my $file (@pkglistfiles)
|
|
|
|
{
|
|
|
|
my $pkglistfp;
|
|
|
|
open($pkglistfp, xCAT::CFMUtils->trim($file));
|
|
|
|
while (<$pkglistfp>)
|
2012-08-31 01:37:38 +00:00
|
|
|
{
|
2013-01-18 08:13:08 +00:00
|
|
|
my $line = xCAT::CFMUtils->trim($_);
|
2013-01-28 06:56:36 +00:00
|
|
|
if (($line =~ /^#/) || ($line =~ /^\s*$/ ) || ($line =~ /^@/))
|
2013-01-18 08:13:08 +00:00
|
|
|
{ #comment line or blank line
|
|
|
|
next;
|
2012-08-31 01:37:38 +00:00
|
|
|
} else
|
2013-01-18 08:13:08 +00:00
|
|
|
{
|
|
|
|
if ($line =~ /^-/)
|
|
|
|
{ # the package be removed
|
|
|
|
push @removed, substr($line, 1);
|
|
|
|
} else
|
|
|
|
{ # the package be installed
|
|
|
|
push @selected, $line;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close($pkglistfp);
|
|
|
|
}
|
|
|
|
|
|
|
|
# delete the removed packages from selected list
|
|
|
|
my @intersection = xCAT::CFMUtils->arrayops("I", \@removed, \@selected);
|
|
|
|
@selected = xCAT::CFMUtils->arrayops("D", \@selected, \@intersection);
|
2012-08-31 01:37:38 +00:00
|
|
|
|
|
|
|
return (\@selected, \@removed);
|
|
|
|
}
|
|
|
|
|
|
|
|
#-----------------------------------------------------------------------------
|
|
|
|
|
2012-10-25 06:19:15 +00:00
|
|
|
=head3 getPkgsDiff
|
|
|
|
Get the differences between previous and current packages list
|
2012-08-31 01:37:38 +00:00
|
|
|
|
|
|
|
Arguments:
|
2012-10-25 06:19:15 +00:00
|
|
|
@pre - previous selected packages list
|
|
|
|
@cur - current selected packages list
|
2012-08-31 01:37:38 +00:00
|
|
|
Returns:
|
|
|
|
@diff - the differencen list
|
|
|
|
Globals:
|
|
|
|
none
|
|
|
|
Error:
|
|
|
|
none
|
|
|
|
Example:
|
2012-10-25 06:19:15 +00:00
|
|
|
my @diff = xCAT::CFMUtils->getPkgsDiff(\@pre_selected, \@cur_selected);
|
2012-08-31 01:37:38 +00:00
|
|
|
|
|
|
|
=cut
|
|
|
|
|
|
|
|
#-----------------------------------------------------------------------------
|
2012-10-25 06:19:15 +00:00
|
|
|
sub getPkgsDiff {
|
2012-08-31 01:37:38 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
#-----------------------------------------------------------------------------
|
|
|
|
|
2013-01-18 08:13:08 +00:00
|
|
|
=head3 getIncludefiles
|
|
|
|
Get the #INCLUDE files from the given file
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
$file - the given file
|
|
|
|
Returns:
|
|
|
|
@files - the #INCLUDE files list
|
|
|
|
Globals:
|
|
|
|
none
|
|
|
|
Error:
|
|
|
|
none
|
|
|
|
Example:
|
|
|
|
my @diff = xCAT::CFMUtils->getIncludefiles($file);
|
|
|
|
|
|
|
|
=cut
|
|
|
|
|
|
|
|
#-----------------------------------------------------------------------------
|
|
|
|
sub getIncludefiles {
|
|
|
|
my ($class, $file) = @_;
|
|
|
|
my @files = ();
|
|
|
|
|
|
|
|
my $fp;
|
|
|
|
open($fp, $file);
|
|
|
|
while (<$fp>)
|
|
|
|
{
|
|
|
|
my $line = xCAT::CFMUtils->trim($_);
|
|
|
|
if ($line =~ /^\s*$/)
|
|
|
|
{ # blank line
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
# find the #INCLUDE line
|
|
|
|
if ($line =~ /^\s*#INCLUDE:[^#^\n]+#/)
|
|
|
|
{
|
|
|
|
#print "The line is: [$line]\n";
|
|
|
|
my $incfile = substr($line, length("#INCLUDE:"), length($line)-length("#INCLUDE:")-1);
|
|
|
|
push @files, $incfile;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close($fp);
|
|
|
|
|
|
|
|
return @files;
|
|
|
|
}
|
|
|
|
|
|
|
|
#-----------------------------------------------------------------------------
|
|
|
|
|
2012-08-31 01:37:38 +00:00
|
|
|
=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);
|
|
|
|
}
|