add picking files that are specific to the running database, fix defect 3051710
git-svn-id: https://svn.code.sf.net/p/xcat/code/xcat-core/trunk@7208 8638fb3e-16cb-4fca-ae20-7b5d299a9bcd
This commit is contained in:
		| @@ -4,43 +4,55 @@ | ||||
| package xCAT::ExtTab; | ||||
| BEGIN | ||||
| { | ||||
|     $::XCATROOT = $ENV{'XCATROOT'} ? $ENV{'XCATROOT'} : -d '/opt/xcat' ? '/opt/xcat' : '/usr'; | ||||
| $::XCATROOT = $ENV{'XCATROOT'} ? $ENV{'XCATROOT'} : -d '/opt/xcat' ? '/opt/xcat' : '/usr'; | ||||
|  | ||||
| } | ||||
| use lib "$::XCATROOT/lib/perl"; | ||||
| #   | ||||
| #NO xCAT perl library routines should be used in this begin block | ||||
| #(i.e. MsgUtils,Utils, etc)  | ||||
| #   | ||||
| #use lib "$::XCATROOT/lib/perl"; | ||||
|  | ||||
| use xCAT::MsgUtils; | ||||
| use File::Path; | ||||
|  | ||||
| %ext_tabspec=();  | ||||
| $ext_defspec=(); | ||||
|  | ||||
|  | ||||
| # loads user defined table spec. They are stored under /opt/xcat/lib/perl/xCAT_schema directory | ||||
| my $path="$::XCATROOT/lib/perl/xCAT_schema"; | ||||
| my @extSchema=glob($path."/*.pm"); | ||||
|    # print "\nextSchema=@extSchema\n"; | ||||
| my $filelist;  # no specific files | ||||
| my @extSchema = &get_filelist($path, $filelist,"pm"); | ||||
| #   print "\nextSchema=@extSchema\n"; | ||||
|  | ||||
| foreach (@extSchema) { | ||||
|     /.*\/([^\/]*).pm$/; | ||||
|     my $file=$_; | ||||
|     my $modname = $1; | ||||
|     no strict 'refs'; | ||||
|     my $warning; | ||||
|     `/bin/logger -t xCAT processing $_`;  | ||||
|     eval {require($_)}; | ||||
|     if ($@) {  | ||||
| 	xCAT::MsgUtils->message('ES',"\n  Warning: The user defined database table schema file $file cannot be located or has compiling errors.\n");  | ||||
| 	$warning ="Warning: The user defined database table schema file $file cannot be located or has compiling errors.\n"; | ||||
|         print $warning; | ||||
|         `/bin/logger -t xCAT $warning`;  | ||||
| 	next; | ||||
|     }    | ||||
|     if (${"xCAT_schema::" . "$modname" . "::"}{tabspec}) { | ||||
| 	my %tabspec=%{${"xCAT_schema::" . "$modname" . "::"}{tabspec}}; | ||||
| 	foreach my $tabname (keys(%tabspec)) { | ||||
|             if (exists($ext_tabspec{$tabname})) { | ||||
| 		xCAT::MsgUtils->message('ES', "\n  Warning: File $file: the table name $tabname is used by other applications. Please rename the table.\n"); | ||||
| 		$warning = "Warning: File $file: the table name $tabname is used by other applications. Please rename the table.\n"; | ||||
|                 print $warning; | ||||
|                `/bin/logger -t xCAT $warning`;  | ||||
| 	    } else { | ||||
| 		$ext_tabspec{$tabname}=$tabspec{$tabname}; | ||||
| 	    } | ||||
| 	} | ||||
|     } else { | ||||
| 	xCAT::MsgUtils->message('ES', "\n  Warning: Cannot find \%tabspec variable in the user defined database table schema file $file\n"); | ||||
| 	$warning ="\n  Warning: Cannot find \%tabspec variable in the user defined database table schema file $file\n"; | ||||
|          print $warning; | ||||
|          `/bin/logger -t xCAT $warning`;  | ||||
|     } | ||||
|     | ||||
|     #get the defspec from each file and merge them into %ext_defspec | ||||
| @@ -60,7 +72,9 @@ foreach (@extSchema) { | ||||
| 		    foreach my $h (@attr_new) { | ||||
| 			my $attrname=$h->{attr_name}; | ||||
| 			if (exists($tmp_hash{$attrname})) { | ||||
| 			    xCAT::MsgUtils->message('ES', "\n  Warning: Conflict when adding user defined defspec from file $file. Attribute name $attrname is already defined in object $objname.  \n"); | ||||
| 			    $warning= "  Warning: Conflict when adding user defined defspec from file $file. Attribute name $attrname is already defined in object $objname.  \n"; | ||||
|                             print $warning; | ||||
|                            `/bin/logger -t xCAT $warning`;  | ||||
| 			} else { | ||||
| 			    #print "\ngot here objname=$objname, attrname=" . $h->{attr_name} . "\n"; | ||||
| 			    push(@{$ext_defspec{$objname}->{'attrs'}}, $h);  | ||||
| @@ -92,7 +106,6 @@ foreach (@extSchema) { | ||||
|     Handles user defined database tables. | ||||
|  | ||||
| =cut | ||||
| #------------------------------------------------------ | ||||
|  | ||||
| #------------------------------------------------------- | ||||
|  | ||||
| @@ -114,5 +127,146 @@ sub updateTables | ||||
| 	my $table= xCAT::Table->new($_,-create=>1,-autocommit=>1); | ||||
|     } | ||||
| } | ||||
| #-------------------------------------------------------------------------- | ||||
|  | ||||
| =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; | ||||
|         my($name,$ext2) = split '\_', $name; | ||||
|         if ($ext2 eq $dbname) | ||||
|         { | ||||
|             push @filelist, $file; | ||||
|         } | ||||
|         else | ||||
|         { | ||||
|             if ($ext2 eq "") | ||||
|             { | ||||
|                 push @filelist, $file; | ||||
|             } | ||||
|         } | ||||
|          $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"; | ||||
|             } | ||||
|         } | ||||
|     } | ||||
|     ($xcatcfg =~ /^$/) && die "Can't locate xCAT configuration"; | ||||
|     unless ($xcatcfg =~ /:/) | ||||
|     { | ||||
|         $xcatcfg = "SQLite:" . $xcatcfg; | ||||
|     } | ||||
|     return $xcatcfg; | ||||
| } | ||||
|  | ||||
|  | ||||
| 1; | ||||
|   | ||||
		Reference in New Issue
	
	Block a user