2012-08-30 10:00:37 +00:00
|
|
|
# IBM(c) 2007 EPL license http://www.eclipse.org/legal/epl-v10.html
|
|
|
|
#-------------------------------------------------------
|
|
|
|
|
|
|
|
=head1
|
2012-08-30 10:06:40 +00:00
|
|
|
xCAT plugin package to handle osdistro management
|
2012-08-30 10:00:37 +00:00
|
|
|
|
|
|
|
Supported command:
|
2012-08-30 10:06:40 +00:00
|
|
|
rmosdistro->rmosdistro
|
2012-08-30 10:00:37 +00:00
|
|
|
|
|
|
|
=cut
|
|
|
|
|
|
|
|
#-------------------------------------------------------
|
|
|
|
package xCAT_plugin::osdistro;
|
|
|
|
use Sys::Hostname;
|
|
|
|
use xCAT::Table;
|
|
|
|
|
|
|
|
use xCAT::Utils;
|
|
|
|
|
|
|
|
use xCAT::MsgUtils;
|
|
|
|
use Getopt::Long;
|
2012-11-05 09:32:54 +00:00
|
|
|
use Data::Dumper;
|
2012-08-30 10:00:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
#-------------------------------------------------------
|
|
|
|
|
|
|
|
=head3 handled_commands
|
|
|
|
|
|
|
|
Return list of commands handled by this plugin
|
|
|
|
|
|
|
|
=cut
|
|
|
|
|
|
|
|
#-------------------------------------------------------
|
|
|
|
|
|
|
|
sub handled_commands
|
|
|
|
{
|
|
|
|
return {
|
|
|
|
rmosdistro => "osdistro",
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#-------------------------------------------------------
|
|
|
|
|
|
|
|
=head3 process_request
|
|
|
|
|
|
|
|
Process the command
|
|
|
|
|
|
|
|
=cut
|
|
|
|
|
|
|
|
#-------------------------------------------------------
|
|
|
|
sub process_request
|
|
|
|
{
|
|
|
|
|
|
|
|
my $request = shift;
|
|
|
|
my $callback = shift;
|
|
|
|
|
|
|
|
my $command = $request->{command}->[0];
|
|
|
|
my $args = $request->{arg};
|
|
|
|
my $envs = $request->{env};
|
|
|
|
my %rsp;
|
|
|
|
# do your processing here
|
|
|
|
# return info
|
|
|
|
|
2012-08-31 03:29:22 +00:00
|
|
|
if($command eq "rmosdistro")
|
2012-08-30 10:00:37 +00:00
|
|
|
{
|
|
|
|
&rmosdistro($request,$callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#-------------------------------------------------------
|
|
|
|
|
2012-08-30 10:06:40 +00:00
|
|
|
=head3 getOSdistroref
|
2012-08-30 10:00:37 +00:00
|
|
|
|
2012-08-31 03:29:22 +00:00
|
|
|
check whether the specified osdistro is referenced
|
|
|
|
by any osimage. if yes, return the string of
|
|
|
|
osimage names, return undef otherwise
|
2012-08-30 10:00:37 +00:00
|
|
|
|
|
|
|
=cut
|
|
|
|
|
|
|
|
#-------------------------------------------------------
|
|
|
|
sub getOSdistroref
|
|
|
|
{
|
|
|
|
my $osimagetab=shift;
|
|
|
|
my $osdistroname=shift;
|
|
|
|
|
|
|
|
my $ret=();
|
|
|
|
|
|
|
|
|
|
|
|
unless($osimagetab)
|
|
|
|
{
|
|
|
|
return undef;
|
|
|
|
}
|
|
|
|
|
|
|
|
my @clause=();
|
|
|
|
|
|
|
|
push(@clause,"osdistroname==".$osdistroname);
|
|
|
|
|
|
|
|
my @result=$osimagetab->getAllAttribsWhere(\@clause,'imagename');
|
|
|
|
|
|
|
|
if(scalar @result)
|
|
|
|
{
|
|
|
|
foreach(@result)
|
|
|
|
{
|
|
|
|
$ret=$ret.$_->{'imagename'}.",";
|
|
|
|
}
|
2012-08-31 03:29:22 +00:00
|
|
|
$ret =~ s/,$//;
|
2012-08-30 10:00:37 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return undef;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#-------------------------------------------------------
|
|
|
|
|
|
|
|
=head3 rmosdistro
|
|
|
|
|
2012-08-31 03:29:22 +00:00
|
|
|
remove osdistro,including remove osdistro directory
|
|
|
|
and entry in osdistro table
|
2012-08-30 10:00:37 +00:00
|
|
|
|
|
|
|
=cut
|
|
|
|
|
|
|
|
#-------------------------------------------------------
|
|
|
|
|
|
|
|
sub rmosdistro
|
|
|
|
{
|
|
|
|
my $request = shift;
|
|
|
|
my $callback = shift;
|
|
|
|
|
2012-11-05 09:32:54 +00:00
|
|
|
my $all=undef;
|
2012-08-30 10:00:37 +00:00
|
|
|
my $force=undef;
|
|
|
|
my $help=undef;
|
|
|
|
|
|
|
|
my $osdistropath=undef;
|
2012-08-31 03:29:22 +00:00
|
|
|
#an array of all the osdistronames to remove
|
2012-08-30 10:00:37 +00:00
|
|
|
my @OSdistroListToDel=();
|
|
|
|
|
|
|
|
if ($request->{arg}) {
|
|
|
|
@ARGV = @{$request->{arg}};
|
|
|
|
}
|
2012-11-05 09:32:54 +00:00
|
|
|
|
|
|
|
|
2012-08-30 10:00:37 +00:00
|
|
|
GetOptions(
|
|
|
|
'h|help' => \$help,
|
2012-11-05 09:32:54 +00:00
|
|
|
'a|all' => \$all,
|
2012-08-31 03:29:22 +00:00
|
|
|
'f|force' => \$force,
|
|
|
|
);
|
2012-08-30 10:00:37 +00:00
|
|
|
|
2012-11-05 09:32:54 +00:00
|
|
|
|
2012-08-31 03:29:22 +00:00
|
|
|
if ($help) {
|
2012-11-05 09:32:54 +00:00
|
|
|
$callback->({info=>"rmosdistro [{-a|--all}] [-f|--force] [osdistroname] ...",errorcode=>[0]});
|
2012-08-30 10:00:37 +00:00
|
|
|
return;
|
2012-08-31 03:29:22 +00:00
|
|
|
}
|
2012-08-30 10:00:37 +00:00
|
|
|
|
2012-11-05 09:32:54 +00:00
|
|
|
unless($all)
|
|
|
|
{
|
|
|
|
unless(scalar @ARGV)
|
|
|
|
{
|
|
|
|
$callback->({info=>"please specify osdistroname to remove, or specify \"-a|--all\" to remove all osdistros ",errorcode=>[1]});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#if any osdistro has been specified,push it into array
|
|
|
|
push(@OSdistroListToDel,@ARGV);
|
|
|
|
}
|
2012-08-30 10:00:37 +00:00
|
|
|
|
2012-08-31 03:29:22 +00:00
|
|
|
my $osdistrotab = xCAT::Table->new('osdistro',-create=>1);
|
|
|
|
unless($osdistrotab)
|
|
|
|
{
|
|
|
|
$callback->({error=>"rmosdistro: failed to open table 'osdistro'!",errorcode=>[1]});
|
|
|
|
return;
|
|
|
|
}
|
2012-08-30 10:00:37 +00:00
|
|
|
|
2012-08-31 03:29:22 +00:00
|
|
|
|
2012-11-05 09:32:54 +00:00
|
|
|
#if -a or --all is specified,push all the osdistronames to the array to delete
|
|
|
|
if($all)
|
2012-08-31 03:29:22 +00:00
|
|
|
{
|
|
|
|
my @result=$osdistrotab->getAllAttribs('osdistroname');
|
|
|
|
if(defined(@result) and scalar @result >0)
|
2012-08-30 10:00:37 +00:00
|
|
|
{
|
2012-08-31 03:29:22 +00:00
|
|
|
foreach(@result)
|
|
|
|
{
|
|
|
|
push(@OSdistroListToDel,$_->{'osdistroname'});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(scalar @OSdistroListToDel)
|
|
|
|
{
|
2012-11-05 09:32:54 +00:00
|
|
|
#if -f|--force is not specified,need to open osimage table to check the reference of osdistro
|
2012-08-31 03:29:22 +00:00
|
|
|
my $osimagetab=undef;
|
|
|
|
unless($force)
|
2012-08-30 10:00:37 +00:00
|
|
|
{
|
2012-08-31 03:29:22 +00:00
|
|
|
$osimagetab=xCAT::Table->new('osimage');
|
|
|
|
unless($osimagetab)
|
|
|
|
{
|
|
|
|
$callback->({error=>"rmosdistro: failed to open table 'osimage'!",errorcode=>[1]});
|
2012-11-05 09:32:54 +00:00
|
|
|
$osdistrotab->close();
|
2012-08-31 03:29:22 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-08-30 10:00:37 +00:00
|
|
|
}
|
|
|
|
|
2012-08-31 03:29:22 +00:00
|
|
|
foreach(@OSdistroListToDel)
|
2012-08-30 10:00:37 +00:00
|
|
|
{
|
2012-08-31 03:29:22 +00:00
|
|
|
|
2012-11-05 09:32:54 +00:00
|
|
|
#if -f|--force not specified,check the reference of osdistro,complain if the osdistro is referenced by some osimage
|
2012-08-30 10:00:37 +00:00
|
|
|
unless($force)
|
|
|
|
{
|
2012-08-31 03:29:22 +00:00
|
|
|
my $result=&getOSdistroref($osimagetab,$_);
|
|
|
|
if($result)
|
2012-08-30 10:00:37 +00:00
|
|
|
{
|
2012-08-31 03:29:22 +00:00
|
|
|
$callback->({error=>"rmosdistro: failed to remove $_, it is referenced by osimages:\n$result\nretry with -f option !",errorcode=>[1]});
|
|
|
|
next;
|
|
|
|
}
|
2012-08-30 10:00:37 +00:00
|
|
|
}
|
|
|
|
|
2012-11-05 09:32:54 +00:00
|
|
|
#get "dirpaths" attribute of osdistro to remove the directory, complain if failed to lookup the osdistroname
|
|
|
|
my %keyhash=('osdistroname' => $_,);
|
2012-08-31 03:29:22 +00:00
|
|
|
my $result=$osdistrotab->getAttribs(\%keyhash,'dirpaths');
|
|
|
|
unless($result)
|
|
|
|
{
|
|
|
|
$callback->({error=>"rmosdistro: $keyhash{osdistroname} not exist!",errorcode=>[1]});
|
|
|
|
next;
|
|
|
|
}
|
2012-08-30 10:00:37 +00:00
|
|
|
|
2012-11-05 09:32:54 +00:00
|
|
|
#remove the osdistro directories
|
2012-08-31 03:29:22 +00:00
|
|
|
if($result->{'dirpaths'})
|
|
|
|
{
|
2012-08-30 10:00:37 +00:00
|
|
|
$result->{'dirpaths'} =~ s/,/\ /g;
|
|
|
|
#$callback->({error=>"rmosdistro: remove $result->{'dirpaths'} directory!",errorcode=>[0]});
|
|
|
|
system("rm -rf $result->{'dirpaths'}");
|
|
|
|
if($? != 0)
|
|
|
|
{
|
|
|
|
$callback->({error=>"rmosdistro: failed to remove $keyhash{osdistroname} directory!",errorcode=>[1]});
|
|
|
|
next;
|
|
|
|
}
|
2012-11-05 09:32:54 +00:00
|
|
|
}
|
2012-08-31 03:29:22 +00:00
|
|
|
|
2012-11-05 09:32:54 +00:00
|
|
|
#remove the osdistro entry
|
|
|
|
$osdistrotab->delEntries(\%keyhash);
|
|
|
|
$osdistrotab->commit;
|
|
|
|
$callback->({info=>"rmosdistro: remove $_ success",errorcode=>[0]});
|
2012-08-30 10:00:37 +00:00
|
|
|
|
2012-11-05 09:32:54 +00:00
|
|
|
}
|
2012-08-30 10:00:37 +00:00
|
|
|
|
2012-11-05 09:32:54 +00:00
|
|
|
if($osimagetab)
|
|
|
|
{
|
|
|
|
$osimagetab->close;
|
|
|
|
}
|
|
|
|
}
|
2012-08-30 10:00:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
$osdistrotab->close;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-08-31 03:29:22 +00:00
|
|
|
1;
|