2009-01-09 22:25:35 +00:00
|
|
|
#!/usr/bin/env perl
|
|
|
|
# IBM(c) 2007 EPL license http://www.eclipse.org/legal/epl-v10.html
|
|
|
|
#-------------------------------------------------------
|
|
|
|
package xCAT::ExtTab;
|
|
|
|
BEGIN
|
|
|
|
{
|
2010-08-24 19:12:17 +00:00
|
|
|
$::XCATROOT = $ENV{'XCATROOT'} ? $ENV{'XCATROOT'} : -d '/opt/xcat' ? '/opt/xcat' : '/usr';
|
|
|
|
|
2009-01-09 22:25:35 +00:00
|
|
|
}
|
2010-08-24 19:12:17 +00:00
|
|
|
#
|
|
|
|
#NO xCAT perl library routines should be used in this begin block
|
|
|
|
#(i.e. MsgUtils,Utils, etc)
|
|
|
|
#
|
|
|
|
#use lib "$::XCATROOT/lib/perl";
|
2009-01-09 22:25:35 +00:00
|
|
|
|
|
|
|
use File::Path;
|
|
|
|
%ext_tabspec=();
|
2011-03-31 19:38:13 +00:00
|
|
|
%ext_defspec=();
|
2009-01-09 22:25:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
# loads user defined table spec. They are stored under /opt/xcat/lib/perl/xCAT_schema directory
|
|
|
|
my $path="$::XCATROOT/lib/perl/xCAT_schema";
|
2010-08-24 19:12:17 +00:00
|
|
|
my $filelist; # no specific files
|
|
|
|
my @extSchema = &get_filelist($path, $filelist,"pm");
|
|
|
|
# print "\nextSchema=@extSchema\n";
|
2009-01-09 22:25:35 +00:00
|
|
|
|
|
|
|
foreach (@extSchema) {
|
|
|
|
/.*\/([^\/]*).pm$/;
|
|
|
|
my $file=$_;
|
|
|
|
my $modname = $1;
|
|
|
|
no strict 'refs';
|
2010-08-24 19:12:17 +00:00
|
|
|
my $warning;
|
2010-08-24 19:17:41 +00:00
|
|
|
`logger -t xCAT processing $_`;
|
2009-01-09 22:25:35 +00:00
|
|
|
eval {require($_)};
|
|
|
|
if ($@) {
|
2010-08-24 19:12:17 +00:00
|
|
|
$warning ="Warning: The user defined database table schema file $file cannot be located or has compiling errors.\n";
|
|
|
|
print $warning;
|
2010-08-24 19:17:41 +00:00
|
|
|
`logger -t xCAT $warning`;
|
2009-01-09 22:25:35 +00:00
|
|
|
next;
|
|
|
|
}
|
|
|
|
if (${"xCAT_schema::" . "$modname" . "::"}{tabspec}) {
|
|
|
|
my %tabspec=%{${"xCAT_schema::" . "$modname" . "::"}{tabspec}};
|
|
|
|
foreach my $tabname (keys(%tabspec)) {
|
2009-01-13 21:29:34 +00:00
|
|
|
if (exists($ext_tabspec{$tabname})) {
|
2010-08-24 19:12:17 +00:00
|
|
|
$warning = "Warning: File $file: the table name $tabname is used by other applications. Please rename the table.\n";
|
|
|
|
print $warning;
|
2010-08-24 19:17:41 +00:00
|
|
|
`logger -t xCAT $warning`;
|
2009-01-13 21:29:34 +00:00
|
|
|
} else {
|
|
|
|
$ext_tabspec{$tabname}=$tabspec{$tabname};
|
|
|
|
}
|
2009-01-09 22:25:35 +00:00
|
|
|
}
|
|
|
|
} else {
|
2010-08-24 19:12:17 +00:00
|
|
|
$warning ="\n Warning: Cannot find \%tabspec variable in the user defined database table schema file $file\n";
|
|
|
|
print $warning;
|
2010-08-24 19:17:41 +00:00
|
|
|
`logger -t xCAT $warning`;
|
2009-01-09 22:25:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#get the defspec from each file and merge them into %ext_defspec
|
|
|
|
if (${"xCAT_schema::" . "$modname" . "::"}{defspec}) {
|
|
|
|
my %defspec=%{${"xCAT_schema::" . "$modname" . "::"}{defspec}};
|
|
|
|
foreach my $objname (keys(%defspec)) {
|
|
|
|
if (exists($defspec{$objname}->{'attrs'})) {
|
|
|
|
if (exists($ext_defspec{$objname})) {
|
|
|
|
#print "insert\n";
|
|
|
|
my @attr_new=@{$defspec{$objname}->{'attrs'}};
|
|
|
|
my @attr=@{$ext_defspec{$objname}->{'attrs'}};
|
|
|
|
my %tmp_hash=();
|
|
|
|
foreach my $orig (@attr) {
|
|
|
|
my $attrname=$orig->{attr_name};
|
|
|
|
$tmp_hash{$attrname}=1;
|
|
|
|
}
|
|
|
|
foreach my $h (@attr_new) {
|
|
|
|
my $attrname=$h->{attr_name};
|
|
|
|
if (exists($tmp_hash{$attrname})) {
|
2010-08-24 19:12:17 +00:00
|
|
|
$warning= " Warning: Conflict when adding user defined defspec from file $file. Attribute name $attrname is already defined in object $objname. \n";
|
|
|
|
print $warning;
|
2010-08-24 19:17:41 +00:00
|
|
|
`logger -t xCAT $warning`;
|
2009-01-09 22:25:35 +00:00
|
|
|
} else {
|
|
|
|
#print "\ngot here objname=$objname, attrname=" . $h->{attr_name} . "\n";
|
|
|
|
push(@{$ext_defspec{$objname}->{'attrs'}}, $h);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
#print "\ngot here objname=$objname, file=$file\n";
|
|
|
|
$ext_defspec{$objname}=$defspec{$objname};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} #foreach
|
|
|
|
|
|
|
|
#print out the defspec
|
|
|
|
#print "\nexternal defspec:\n";
|
|
|
|
#foreach(%ext_defspec) {
|
|
|
|
# print " $_:\n";
|
|
|
|
# my @attr=@{$ext_defspec{$_}->{'attrs'}};
|
|
|
|
# foreach my $h (@attr) {
|
|
|
|
# print " " . $h->{attr_name} . "\n";
|
|
|
|
# }
|
|
|
|
#}
|
|
|
|
|
|
|
|
#-------------------------------------------------------
|
|
|
|
=head1 xCAT::ExtTab
|
|
|
|
|
|
|
|
Handles user defined database tables.
|
|
|
|
|
|
|
|
=cut
|
|
|
|
|
|
|
|
#-------------------------------------------------------
|
|
|
|
|
|
|
|
=head3 updateTables
|
|
|
|
|
|
|
|
It is called by xcatd to generate the user-defined tables
|
|
|
|
if they do not exist, it also updates the tables if there is
|
2010-09-01 14:09:38 +00:00
|
|
|
a schema change.
|
2009-01-09 22:25:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
=cut
|
|
|
|
#-------------------------------------------------------
|
|
|
|
|
|
|
|
sub updateTables
|
|
|
|
{
|
|
|
|
#print "\nupdateTables\n";
|
2009-09-23 14:09:12 +00:00
|
|
|
#print "\n";
|
2009-01-09 22:25:35 +00:00
|
|
|
foreach (keys %ext_tabspec) {
|
2010-09-01 14:09:38 +00:00
|
|
|
my $table= xCAT::Table->new($_,-create=>1);
|
|
|
|
my $rc=$table->updateschema();
|
|
|
|
$table->close();
|
2009-01-09 22:25:35 +00:00
|
|
|
}
|
|
|
|
}
|
2010-08-24 19:12:17 +00:00
|
|
|
#--------------------------------------------------------------------------
|
|
|
|
|
|
|
|
=head3
|
|
|
|
Note this is a copy of the one in Table.pm but we cannot use any of the
|
|
|
|
xCAT perl libraries in this routine,since the function was done in the
|
|
|
|
Begin block.
|
|
|
|
Description: get_filelist
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
directory,filelist,type
|
|
|
|
Returns:
|
|
|
|
The list of sql files to be processed which consists of all the
|
|
|
|
files with <name>.sql and <name>_<databasename>.sql
|
|
|
|
or
|
|
|
|
files with <name>.pm and <name>_<databasename>.pm
|
|
|
|
Globals:
|
|
|
|
|
|
|
|
Error:
|
|
|
|
|
|
|
|
Example:
|
|
|
|
my @filelist =get_filelist($directory,$filelist,$type);
|
|
|
|
where type = "sql" or "pm"
|
|
|
|
|
|
|
|
=cut
|
|
|
|
|
|
|
|
#--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
sub get_filelist
|
|
|
|
|
|
|
|
{
|
|
|
|
use File::Basename;
|
|
|
|
my $directory = shift;
|
|
|
|
my $files = shift;
|
|
|
|
my $ext = shift;
|
|
|
|
my $dbname = "sqlite";
|
|
|
|
my $xcatcfg = get_xcatcfg();
|
|
|
|
|
|
|
|
if ($xcatcfg =~ /^DB2:/)
|
|
|
|
{
|
|
|
|
$dbname = "db2";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ($xcatcfg =~ /^mysql:/)
|
|
|
|
{
|
|
|
|
$dbname = "mysql";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ($xcatcfg =~ /^Pg:/)
|
|
|
|
{
|
|
|
|
$dbname = "pgsql";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$directory .= "/";
|
|
|
|
|
|
|
|
my @filelist = ();
|
|
|
|
|
|
|
|
my @list = glob($directory . "*.$ext"); # all files
|
|
|
|
foreach my $file (@list)
|
|
|
|
{
|
|
|
|
my $filename= basename($file); # strip filename
|
|
|
|
my($name,$ext1) = split '\.', $filename;
|
2010-09-13 13:05:14 +00:00
|
|
|
my($tmpname,$ext2) = split '\_', $name;
|
2010-09-08 18:58:59 +00:00
|
|
|
if ($ext2 eq $dbname) # matches the database
|
2010-08-24 19:12:17 +00:00
|
|
|
{
|
|
|
|
push @filelist, $file;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-09-08 18:58:59 +00:00
|
|
|
if ($ext2 eq "") # no database designated
|
2010-08-24 19:12:17 +00:00
|
|
|
{
|
|
|
|
push @filelist, $file;
|
2010-09-08 18:58:59 +00:00
|
|
|
} else { # if not one of the databases, they just have _ in
|
|
|
|
# the file name
|
|
|
|
if ($ext2 ne "db2" && $ext2 ne "mysql" && $ext2 ne "pgsql" && $ext2 ne "sqlite" ) {
|
|
|
|
push @filelist, $file;
|
|
|
|
}
|
2010-08-24 19:12:17 +00:00
|
|
|
}
|
2010-09-08 18:58:59 +00:00
|
|
|
}
|
2010-08-24 19:12:17 +00:00
|
|
|
$ext2 = "";
|
|
|
|
$ext1 = "";
|
|
|
|
}
|
|
|
|
return @filelist;
|
|
|
|
}
|
|
|
|
#--------------------------------------------------------------------------
|
|
|
|
|
|
|
|
=head3
|
|
|
|
|
|
|
|
Note this is a copy of the one in Table.pm but we cannot use any of the
|
|
|
|
xCAT perl libraries in this routine,since the function was done in the
|
|
|
|
|
|
|
|
Description: get_xcatcfg
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
none
|
|
|
|
Returns:
|
|
|
|
the database name from /etc/xcat/cfgloc or sqlite
|
|
|
|
Globals:
|
|
|
|
|
|
|
|
Error:
|
|
|
|
|
|
|
|
Example:
|
|
|
|
my $xcatcfg =get_xcatcfg();
|
|
|
|
|
|
|
|
|
|
|
|
=cut
|
|
|
|
|
|
|
|
#--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
sub get_xcatcfg
|
|
|
|
{
|
|
|
|
my $xcatcfg = (defined $ENV{'XCATCFG'} ? $ENV{'XCATCFG'} : '');
|
|
|
|
unless ($xcatcfg) {
|
|
|
|
if (-r "/etc/xcat/cfgloc") {
|
|
|
|
my $cfgl;
|
|
|
|
open($cfgl,"<","/etc/xcat/cfgloc");
|
|
|
|
$xcatcfg = <$cfgl>;
|
|
|
|
close($cfgl);
|
|
|
|
chomp($xcatcfg);
|
|
|
|
$ENV{'XCATCFG'}=$xcatcfg; #Store it in env to avoid many file reads
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($xcatcfg =~ /^$/)
|
|
|
|
{
|
|
|
|
if (-d "/opt/xcat/cfg")
|
|
|
|
{
|
|
|
|
$xcatcfg = "SQLite:/opt/xcat/cfg";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (-d "/etc/xcat")
|
|
|
|
{
|
|
|
|
$xcatcfg = "SQLite:/etc/xcat";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
unless ($xcatcfg =~ /:/)
|
|
|
|
{
|
|
|
|
$xcatcfg = "SQLite:" . $xcatcfg;
|
|
|
|
}
|
|
|
|
return $xcatcfg;
|
|
|
|
}
|
|
|
|
|
2009-01-09 22:25:35 +00:00
|
|
|
|
|
|
|
1;
|