Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:03:14

0001 #!/usr/bin/perl
0002 
0003 use warnings;
0004 use strict;
0005 $|++;
0006 
0007 use File::Basename;
0008 use Getopt::Long;
0009 use Data::Dumper;
0010 
0011 
0012 my $cmssw_base = $ENV{'CMSSW_BASE'};
0013 unless ($cmssw_base) {
0014     die "CMSSW_BASE is not set.  Be sure to eval `scramv1 runtime` first!\n";
0015 }
0016 
0017 # Manually including library
0018 push @INC, "${cmssw_base}/src/CondTools/OracleDBA/perllib";
0019 require CMSDBA;
0020 
0021 # Directories used
0022 my $o2o_dbconfigdir = $cmssw_base.'/src/CondTools/O2OFramework/dbconfig';
0023 my $o2o_sqldir = $cmssw_base.'/src/CondTools/O2OFramework/sql';
0024 my $dba_dbconfigdir = $cmssw_base.'/src/CondTools/OracleDBA/dbconfig';
0025 
0026 
0027 my $usage = basename($0)." [options] [detector1 detector2 ...]\n".
0028     "Options:\n".
0029     "--general         Setup the general offline schema for O2O\n".
0030     "--dbaconfig       Offline DB configuration file (hardcoded default in project area)\n".
0031     "--o2oconfig       O2O configuration file (hardcoded default in project area)\n".
0032     "--auth            DB connection file (hardcoded default in project area)\n".
0033     "--online_connect  Online connect string  (default in o2oconfig)\n".
0034     "--offline_connect Offline connect string (default in dbaconfig)\n".
0035     "--general_conect  General schema connect string (default in dbaconfig)\n".
0036     "--all             Setup all detectors in O2O configuration file\n".
0037     "--fake            Don't actually do anything, only print commands\n".
0038     "--debug           Print additional debug information\n".
0039     "--log             Log file\n".
0040     "--help, -h        Print this message and exit\n";
0041 
0042 
0043 my $cmd_online_connect = '';
0044 my $cmd_offline_connect = '';
0045 my $cmd_general_connect = '';
0046 my $o2o_configfile = $o2o_dbconfigdir.'/o2oconfiguration.xml';
0047 my $dba_configfile = $dba_dbconfigdir.'/dbconfiguration.xml';
0048 my $authfile = $dba_dbconfigdir.'/authentication.xml';
0049 my $doall = 0;
0050 my $dogeneral = 0;
0051 my $fake = 0;
0052 my $debug = 0;
0053 my $log = '';
0054 my $help = 0;
0055 
0056 GetOptions('general' => \$dogeneral,
0057        'o2oconfig=s' => \$o2o_configfile,
0058        'dbaconfig=s' => \$dba_configfile,
0059        'auth=s' => \$authfile,
0060        'all' => \$doall,
0061        'online_connect=s' => \$cmd_online_connect,
0062        'offline_connect=s' => \$cmd_offline_connect,
0063        'general_connect=s' => \$cmd_general_connect,
0064        'fake' => \$fake,
0065        'help|h' => \$help,
0066        'log=s' => \$log,
0067        'debug' => \$debug);
0068 
0069 if ($help) {
0070     print "$usage";
0071     exit;
0072 }
0073 
0074 # Parse config files
0075 foreach($o2o_configfile, $dba_configfile, $authfile) {
0076     unless (-e $_) { die "Configuration file $_ does not exist!\n"; }
0077     else { print "Using config file $_\n"; }
0078 }
0079 
0080 my $o2o_config = CMSDBA::parse_o2oconfiguration($o2o_configfile);
0081 print "Result of parsing $o2o_configfile:\n".Dumper($o2o_config) if $debug;
0082 
0083 my $dba_config = CMSDBA::parse_dbconfiguration($dba_configfile);
0084 print "Result of parsing $dba_configfile:\n".Dumper($dba_config) if $debug;
0085 
0086 my $auth = CMSDBA::parse_authentication($authfile);
0087 print "Result of parsing $authfile:\n".Dumper($auth) if $debug;
0088 
0089 
0090 if ($cmd_online_connect && $doall) {
0091     warn "[WARN]   online connect $cmd_online_connect will be used for ALL detectors in $o2o_config\n";
0092 }
0093 
0094 if ($cmd_offline_connect && $doall) {
0095     warn "[WARN]   offline connect $cmd_offline_connect will be used for ALL detectors in $o2o_config\n";
0096 }
0097 
0098 
0099 # Build commands
0100 my @commands;
0101 my $cmd;
0102 
0103 # Determine General Connect
0104 my $general_connect;
0105 if (!$cmd_general_connect && exists $dba_config->{general}->{general_connect}) {
0106     $general_connect = $dba_config->{general}->{general_connect};
0107 } elsif ($cmd_general_connect) {
0108     $general_connect = $cmd_general_connect;
0109 } else {
0110     die "general_connect not defined at command line or at $dba_configfile";
0111 }
0112 
0113 # Get connection info
0114 my ($general_user, $general_pass, $general_db, $general_schema) = CMSDBA::connection_test($auth, $general_connect);
0115 
0116 
0117 if ($dogeneral) {    
0118     # Create all database objects
0119     foreach my $dbobject ( qw(o2o_setup o2o_log master_payload_o2o) ) { # XXX hardcoded because order matters
0120     my $sqlfile = $o2o_sqldir.'/'.
0121         $o2o_config->{general}->{dbobject}->{$dbobject}->{sqlfile};
0122 
0123     CMSDBA::check_files($sqlfile);
0124     
0125     $cmd = CMSDBA::get_sqlplus_cmd('user' => $general_user, 'pass' => $general_pass, 'db' => $general_db,
0126                        'file' => $sqlfile);
0127     push(@commands, { 'info' => "Creating $dbobject",
0128               'cmd'  => $cmd });
0129     }
0130 }
0131 
0132 
0133 # Determine what detectors to set up
0134 my @detectors;
0135 if ($doall) {
0136     @detectors = keys %{$o2o_config->{detector}}
0137 } else {
0138     die "Must provide detector name(s) or --all, unless --general" unless(@ARGV || $dogeneral);
0139     @detectors = @ARGV;
0140     foreach (@detectors) {
0141     unless (exists $o2o_config->{detector}->{$_}) {
0142         die "$_ not configured in $o2o_configfile";
0143     }
0144     }
0145 }
0146 
0147 foreach my $detector (@detectors) {
0148     if (!exists $dba_config->{detector}->{$detector}) {
0149     die "Detector $detector is defined in o2oconfig but not in dbconfig";
0150     }
0151 
0152     # Determine online connection
0153     my $online_connect;
0154     if (!$cmd_online_connect && exists $o2o_config->{detector}->{$detector}->{online_connect}) {
0155     $online_connect = $o2o_config->{detector}->{$detector}->{online_connect};
0156     } elsif ($cmd_online_connect) {
0157     $online_connect = $cmd_online_connect;
0158     } else {
0159     die "online_connect not defined at command line or at $o2o_configfile";
0160     }
0161     my ($online_user, $online_pass, $online_db, $online_schema) = CMSDBA::connection_test($auth, $online_connect);
0162 
0163     # Determine offline connection
0164     my $offline_connect;
0165     if (!$cmd_offline_connect && exists $dba_config->{detector}->{$detector}->{offline_connect}) {
0166     $offline_connect = $dba_config->{detector}->{$detector}->{offline_connect};
0167     } elsif ($cmd_offline_connect) {
0168     $offline_connect = $cmd_offline_connect;
0169     } else {
0170     die "offline_connect not defined at command line or at $dba_configfile";
0171     }
0172     my ($offline_user, $offline_pass, $offline_db, $offline_schema) = CMSDBA::connection_test($auth, $offline_connect);
0173 
0174     # Create a database link from offline schema to online schema
0175     my $sql = qq[CREATE DATABASE LINK $online_db CONNECT TO $online_schema IDENTIFIED BY $online_pass USING '$online_db'];
0176     $cmd = CMSDBA::get_sqlplus_cmd('user' => $offline_user, 'pass' => $offline_pass, 'db' => $offline_db, 
0177                    'sql' => $sql);
0178     push(@commands, { 'info' => "Creating database link from $offline_db to $online_db",
0179               'cmd'  => $cmd });
0180 
0181 
0182     foreach my $object (keys %{$o2o_config->{detector}->{$detector}->{object}}) {
0183     my $object_name = $object;
0184     $object = $o2o_config->{detector}->{$detector}->{object}->{$object_name};
0185     
0186         # Add time column to top-level-table in offline schema
0187     my $table = $object->{table};
0188     my $sql = qq[ALTER TABLE $table ADD time NUMBER(38)];
0189     $cmd = CMSDBA::get_sqlplus_cmd('user' => $offline_user, 'pass' => $offline_pass, 'db' => $offline_db, 
0190                        'sql' => $sql);
0191     push(@commands, { 'info' => "Adding TIME column to $table",
0192               'cmd'  => $cmd });
0193 
0194 
0195     # Write payload procedure to offline schema
0196     my $procedure = $object_name.'_payload_o2o';  # XXX Should this be hardcoded?
0197     my $procedure_sqlfile = $o2o_sqldir.'/'.$procedure.'.sql';
0198     
0199     CMSDBA::check_files($procedure_sqlfile);
0200     $cmd = CMSDBA::get_sqlplus_cmd('user' => $offline_user, 'pass' => $offline_pass, 'db' => $offline_db, 
0201                        'file' => $procedure_sqlfile);
0202     push(@commands, { 'info' => "Adding $procedure to offline schema",
0203               'cmd'  => $cmd });
0204 
0205     # Grant access to top-level-table and procedure to general schema
0206     $sql = qq[GRANT SELECT ON $table TO $general_schema];
0207     $cmd = CMSDBA::get_sqlplus_cmd('user' => $offline_user, 'pass' => $offline_pass, 'db' => $offline_db, 
0208                        'sql' => $sql);
0209     push(@commands, { 'info' => "Granting $general_schema access to $table",
0210               'cmd'  => $cmd });
0211     
0212     $sql = qq[GRANT EXECUTE ON $procedure TO $general_schema];
0213     $cmd = CMSDBA::get_sqlplus_cmd('user' => $offline_user, 'pass' => $offline_pass, 'db' => $offline_db, 
0214                        'sql' => $sql);
0215     push(@commands, { 'info' => "Granting $general_schema access to $procedure",
0216               'cmd'  => $cmd });
0217     
0218     # Register the object to O2O_SETUP
0219     $sql = qq[INSERT INTO o2o_setup VALUES ('$object_name', '$offline_schema', '$table')];
0220     $cmd = CMSDBA::get_sqlplus_cmd('user' => $general_user, 'pass' => $general_pass, 'db' => $offline_db, 
0221                        'sql' => $sql);
0222     push(@commands, { 'info' => "Registering $object_name to O2O_SETUP",
0223               'cmd'  => $cmd });
0224 
0225     }
0226     
0227 
0228 }
0229 
0230 # Execution of commands
0231 CMSDBA::execute_commands('cmd_array' => \@commands, 'fake' => $fake, 'debug' => $debug, 'log' => $log);