Hi,

I've made some patch to oda so it doesn't start database multiple times in 
multiple ways.

1) Database.pm.svnpatch make start_database_service more robust and allows 
different databases engin to be handeled.
 for now, just a last small stuff needs  rework and configManager:read_config 
does not collect DB_TYPE which contains mysql.

2) bootstrap.pm.svnpatch from ODA. This patches cleans up the code, adds some 
hardenning and removes a db daemon start that is useless.

3) oda.svnpatch added code to remove mysql hardcodding.

4) prepare_oda.svnpatch: cleanup and comments

5) set_global_oscar_values.svnpatch: this patch can be ommitted. I did a 
set_install_mode('systemimager-rsync',...) so that the database and the system 
status reflects what the GUI displays at step 6.

Best regards,

Olivier.

-- 
        Olivier LAHAYE
        CEA Saclay
        DRT-LIST-DETECS-SSTM
Index: Database.pm
===================================================================
--- Database.pm	(révision 9116)
+++ Database.pm	(copie de travail)
@@ -78,6 +78,9 @@
 use OSCAR::ConfigManager;
 use OSCAR::ODA_Defs;
 use OSCAR::Utils;
+use OSCAR::SystemServices;
+use OSCAR::SystemServicesDefs;
+use OSCAR::OCA::OS_Settings;
 use Data::Dumper;
 
 # oda may or may not be installed and initialized
@@ -102,7 +105,6 @@
               database_connect
               database_disconnect
               start_database_service
-
               delete_package
               delete_node
               delete_group_packages
@@ -1354,7 +1356,7 @@
 }
 
 
-# Return: 1 if success, 0 else.
+# Return: 1 if success, 0 else. 
 # TODO: this function has to been extended for the case where we do not use a
 #       real db but configuration files.
 sub update_node ($$$$) {
@@ -2792,47 +2794,112 @@
 # Starts the appropriate database service.                                     #
 #                                                                              #
 # Input: None.                                                                 #
-# Return: None.                                                                #
+# Return: 0 if success, -1 else.                                               #
 ################################################################################
 sub start_database_service {
-    my $db_service_name = "";
-    my ($db_type, $chk) = split(':', $ENV{OSCAR_DB});
-    if( $db_type eq "mysql" || $db_type eq "" ){
-        # find the name of the mysql service and start the mysql server,
-        # and make sure it is run on every system reboot
-        my ($server_distrib, $server_distrib_version) = which_distro_server();
-        $db_service_name = which_mysql_name($server_distrib,
-                                            $server_distrib_version);
-    } elsif( $db_type eq "pgsql" ) {
-        $db_service_name = "postgresql";
+    # We get the configuration from the OSCAR configuration file.
+    my $oscar_configurator = OSCAR::ConfigManager->new();
+    if ( ! defined ($oscar_configurator) ) {
+        print "ERROR: [$0] start_database_service:Impossible to get the OSCAR configuration\n";
+        exit 1;
     }
+    my $config = $oscar_configurator->get_config();
+    # TODO: integrate DB_TYPE into config object.
+    # right now: $config->db_type = $config->get('ODA_TYPE');
+    # but there is no variable that stores $config->get('DB_TYPE');
+    # Thus we need some hardening here that could be avoided.
+    if ( !defined($config) ) {
+        carp "ERROR: [$0] Unable to read oscar.conf";
+        return -1;
+    }
 
-    my $command="/etc/init.d/$db_service_name status";
-    my @command_output = `$command`;
-    my $status = $?;
-    chomp @command_output;
-    if ( ! grep( /is\ running/, @command_output ) || ! $status ) {
-        print "Starting the $db_service_name database server ...\n";
-        my $command = "/etc/init.d/$db_service_name start";
-        print "$command\n";
-        if ( system( $command ) ) {
-        my @error_strings = ();
-        OSCAR::oda::oda_disconnect( \%options, undef );
-        warn shift @error_strings while @error_strings;
-        die "DB_DEBUG>$0:\n====> cannot start the $db_service_name database ".
-            "server!";
+    if ($config->{'db_type'} eq "file") { # test ODA_TYPE db|file
+        oscar_log_subsection (">$0: Not using a real database, connection done");
+        return 0;
+    }
+
+    # my $db_engine = $config->get('DB_TYPE'); # Does not work we realy need to have that
+                                               # into OSCAR::ConfigManager::load_oscar_config
+    # Let's harcode for the moment.
+    my $db_engine = "mysql"; # to be replaced by $db_engine = $config->{'db_engine'}
+
+    if ( !defined($db_engine) || ($db_engine eq "")) {
+        carp "ERROR: [$0] DB_TYPE not set in oscar.conf";
+        return -1;
+    }
+
+    if ($db_engine eq "file") { # test DB_TYPE=file
+        oscar_log_subsection (">$0: Not using a real database, connection done");
+        return 0;
+    }
+
+    # TODO: (OSCAR 6.2+) handle more logicaly DB_TYPE and ODA_TYPE.
+    # One variable to state if we're using files or daemon
+    # One variable to state the engine
+    # we could have: (file,db3) (file,access) (daemon,access) (daemon,mysql) (file,sqlite)...
+    # Better names would be: DB_TYPE=(file|daemon|plugin) DB_ENGINE=(mysq|pgsql|access|sqlite...)
+    if (defined ($db_engine) && ($db_engine ne "")) { # DB_TYPE=(mysql|pgsql|...)
+        my $db_daemon=OSCAR::OCA::OS_Settings::getitem($db_engine."_daemon");
+        if (defined($db_daemon) && ($db_daemon ne "")) {
+            # Enable database server start at each boot (Should not be here, but was in previous code...)
+            if (enable_system_services($db_daemon)) {
+                print "WARNING: [$0] start_database_service: Unable to enable ".$db_daemon." service at each boot.\n";
+                # It's not an dramatic error if we are still able to start the DB. (so no return)
+            }
+            # Start the database service.
+            if (system_service($db_engine, OSCAR::SystemServicesDefs::START())) { # Start the database daemon
+                carp "ERROR: [$0] start_database_service: Unable to start ".$db_daemon." service.";
+                return -1;
+            }
+        } else {
+            carp "ERROR: [$0] Unknown DB_TYPE=".$db_daemon."\n".
+                 "supported values are: mysql. (pgsql,sqlite,db,file,... not yet supported)";
+            return -1;
         }
-        sleep 2; # The database deamon may be starting in background. We wait
-                 # 2 seconds to make sure the deamon is up (do not ask me why
-                 # 2s and not more or less).
+    } else {
+        carp "ERROR: [$0] unsing ODA_TYPE=db, but DB_TYPE not set in oscar.conf (mysql, pgsql, ...)";
+        return -1;
     }
-    print "Making sure that the $db_service_name database server starts on ".   
-          "subsequent boots ...\n";
-    $command = "chkconfig $db_service_name on";
-    print "$command\n";
-    warn "DB_DEBUG>$0:\n====> WARNING: the $db_service_name database service ".
-         "may not start on subsequent reboots"
-        if system( $command );
+    return 0;
+
+#    my $db_service_name = "";
+#    my ($db_type, $chk) = split(':', $ENV{OSCAR_DB});
+#    if( $db_type eq "mysql" || $db_type eq "" ){
+#        # find the name of the mysql service and start the mysql server,
+#        # and make sure it is run on every system reboot
+#        my ($server_distrib, $server_distrib_version) = which_distro_server();
+#        $db_service_name = which_mysql_name($server_distrib,
+#                                            $server_distrib_version);
+#    } elsif( $db_type eq "pgsql" ) {
+#        $db_service_name = "postgresql";
+#    }
+#
+#    my $command="/etc/init.d/$db_service_name status";
+#    my @command_output = `$command`;
+#    my $status = $?;
+#    chomp @command_output;
+#    if ( ! grep( /is\ running/, @command_output ) || ! $status ) {
+#        print "Starting the $db_service_name database server ...\n";
+#        my $command = "/etc/init.d/$db_service_name start";
+#        print "$command\n";
+#        if ( system( $command ) ) {
+#        my @error_strings = ();
+#        OSCAR::oda::oda_disconnect( \%options, undef );
+#        warn shift @error_strings while @error_strings;
+#        die "DB_DEBUG>$0:\n====> cannot start the $db_service_name database ".
+#            "server!";
+#        }
+#        sleep 2; # The database deamon may be starting in background. We wait
+#                 # 2 seconds to make sure the deamon is up (do not ask me why
+#                 # 2s and not more or less).
+#    }
+#    print "Making sure that the $db_service_name database server starts on ".   
+#          "subsequent boots ...\n";
+#    $command = "chkconfig $db_service_name on";
+#    print "$command\n";
+#    warn "DB_DEBUG>$0:\n====> WARNING: the $db_service_name database service ".
+#         "may not start on subsequent reboots"
+#        if system( $command );
 }
 
 ################################################################################
Index: Bootstrap.pm
===================================================================
--- Bootstrap.pm	(révision 9116)
+++ Bootstrap.pm	(copie de travail)
@@ -41,7 +41,7 @@
 
 ################################################################################
 # Initialize the database used by ODA when a real database is used (e.g.,      #
-# mysql).                                                                      #
+# mysql). Database engine daemon must be running.                               #
 #                                                                              #
 # Input: OSCAR configurator object (ConfigManager object).                     #
 # Return: 0 if success, -1 else.                                               #
@@ -56,12 +56,14 @@
     my $config = $configurator->get_config();
     
     require OSCAR::oda;
-    print "Database Initialization...\n";
-    print "Restarting the database service...\n";
-    require OSCAR::SystemServices;
-    require OSCAR::SystemServicesDefs;
-    OSCAR::SystemServices::system_service (OSCAR::SystemServicesDefs::MYSQL(),
-        OSCAR::SystemServicesDefs::START());
+    require OSCAR::Database;
+
+    print "Database Initialization:\nStarting daemon...\n";
+    # OSCAR::Database::start_database_service knows the engine (mysql, pgsql or other)
+    if (OSCAR::Database::start_database_service()) {
+        carp "ERROR: Failed to start database service.";
+    }
+
     my (%options, %errors);
     my $database_status = OSCAR::oda::check_oscar_database(
         \%options,
@@ -93,23 +95,28 @@
         # should be composed by two phases: the common initialization, and
         # the db specific initialization. This will avoid code duplication.
         print "--> Password ok, now creating the database\n";
-        $cmd = "$scripts_path/create_oscar_database";
-        if (system ($cmd)) {
-            carp "ERROR: Impossible to create the database";
-            return -1;
-        }
+
+        # OL: Code below is useless: prepare_oda already does that.
+        # Create database and empty tables
+        # $cmd = "$scripts_path/create_oscar_database";
+        # if (system ($cmd)) {
+        #     carp "ERROR: Impossible to create the database";
+        #    return -1;
+        # }
+
+        # Create database and empty tables
         $cmd = "$scripts_path/prepare_oda";
         if (system ($cmd)) {
             carp "ERROR: Impossible to populate the database ($cmd)\n";
             return -1;
         }
-
-        # TODO: That should not be there, this is not about the database
-        # initialization but the OSCAR initialization.
+ 
+        # TODO: That should not be there, this is not about the database 
+        # initialization but the OSCAR initialization. 
         $cmd = "$scripts_path/populate_default_package_set";
         my $exit_status = system($cmd);
         if ($exit_status) {
-            carp ("Couldn't set up a default package set ($exit_status)");
+            carp ("ERROR: Couldn't set up a default package set ($exit_status)");
             return -1;
         }
 
@@ -187,10 +194,11 @@
     #
     # We setup ODA, creating few symlinks
     #
+    # OL: TODO: rewirte this code: check $option non empty, then test if $option.".pm" exists
 
     my $orig;
     my $dest;
-    if ($option eq "mysql" || $option eq "postgresql") {
+    if ($option eq "mysql" || $option eq "pgsql") {
         # First we deal with oda.pm
         if (defined $ENV{OSCAR_HOME}) {
             $dest = "$ENV{OSCAR_HOME}/lib/OSCAR/oda.pm";
@@ -213,6 +221,7 @@
             $orig = $orig . "pgsql.pm";
         }
 
+        # OL: UGLY: we shoul never write into system directories.
         unlink ($dest) if (-f $dest);
         if (symlink($orig, $dest) == 0) {
             carp "ERROR: Impossible to create symlink $orig -> $dest";
@@ -268,15 +277,8 @@
         return -1;
     }
     
-    # We start the database daemon
-    require OSCAR::SystemServices;
-    require OSCAR::SystemServicesDefs;
-    if ($option eq "mysql") {
-        OSCAR::SystemServices::system_service (OSCAR::SystemServicesDefs::MYSQL(),
-            OSCAR::SystemServicesDefs::RESTART());
-    } else {
-        carp "Restart of postgres not yet supported";
-    }
+    # We used to start the database daemon, but init_db already does that above.
+    # database_service($option,'restart');
     
     return 0;
 }
Index: oda
===================================================================
--- oda	(révision 9116)
+++ oda	(copie de travail)
@@ -895,31 +895,26 @@
     $ENV{OSCAR_VERBOSE}=5;
     
     #
+    # We remove the oda prereq
+    #
+    print "Removing ODA prereqs\n";
+
+    #
     # Make sure the database server is actually running
     #
-    require OSCAR::SystemServices;
-    require OSCAR::SystemServicesDefs;
-    my $db_type = "mysql"; # TOOD: We currently support only mysql, we need to
-                           # be able to detect that automatically.
-    if ($db_type eq "mysql") {
-        OSCAR::SystemServices::system_service (OSCAR::SystemServicesDefs::MYSQL(),
-            OSCAR::SystemServicesDefs::RESTART());
-    } else {
-        carp "Restart of postgres not yet supported";
+    # can't put use OSCAR::Database at the top otherwise it fails, because OSCAR::oda
+    # does not exists yet (ODA/mysql.pm -> oda.pm trick)
+    require OSCAR::Database;
+    # OSCAR::Database::start_database_service knows the engine (mysql, pgsql or other)
+    # it is read from oscar.conf
+    if (OSCAR::Database::start_database_service()) {
+        carp "ERROR: Failed to start database service.";
     }
     
     #
     # We drop the database
     #
-    print "Dropping the OSCAR database";
-    my $cmd = "/usr/bin/mysqladmin drop oscar --force";
-    system $cmd;
-        
-    #
-    # We remove the oda prereq
-    #
-    print "Removing ODA prereqs\n";
-    # We get the configuration from the OSCAR configuration file.
+    # OL: IMHO Should be in the same place as init_db
     require OSCAR::ConfigManager;
     require OSCAR::Startover;
     my $oscar_configurator = OSCAR::ConfigManager->new();
@@ -928,6 +923,25 @@
     }
     my $config = $oscar_configurator->get_config();
 
+    # Get the database engine
+    # TODO: my $db_engine = $config->get('DB_TYPE');
+    my $db_engine="mysql";
+    if (!defined($db_engine) || ($db_engine eq "")) {
+        carp "ERROR: DB_TYPE not set in oscar.conf";
+    }
+
+    print "Dropping the OSCAR database";
+    my $cmd;
+    if ($db_engine eq "mysql") {
+        $cmd = "/usr/bin/mysqladmin drop oscar --force";
+    } elsif ($db_engine eq "pgsql") {
+        $cmd = "/usr/bin/dropdb -q oscar"; # Not yet tested
+    } else {
+        carp "WARNING: Dropping database oscar from ".$db_engine." not yet supported";
+    }
+    system $cmd;
+        
+    
     # Then we deal with prereq.cfg
     my $dir;
     if (defined $ENV{OSCAR_HOME}) {
@@ -941,6 +955,8 @@
     #
     # We remove the oda symlink
     #
+    # OL: This trick is ugly. We should not write into system directories
+    # Otherwise tools like msec or other security tools may whine.
     my @data = Config::config_re("vendorlib");
     if (scalar (@data) > 1 || scalar (@data) == 0) {
         die "ERROR: Impossible to know where are the Perl modules";
Index: prepare_oda
===================================================================
--- prepare_oda	(révision 9116)
+++ prepare_oda	(copie de travail)
@@ -132,26 +132,31 @@
 my $package_name;
 my $package_version = "";
 
-if ($db_type eq "db") {
-    OSCAR::Database::start_database_service ();
+if ($db_type ne "file") {
+    # OL :not our job to start the service
+    # (it has been started by our caller OSCAR::ODA::bootstrap::init_db)
+    # I left this here for now.
+    if (start_database_service()) {
+        carp "ERROR: Failed to start database service.";
+    }
 
     # create the database if not already there and leave us connected to it
     if (OSCAR::Database::create_database(\%options, \...@error_strings)) {
         die ("ERROR: Impossible to create the database");
     }
 
+    # OL: Maybe useless as all actions below are done by external scripts that connects themself.
     if ( OSCAR::oda::oda_connect( \%options, \...@error_strings ) == 0 ) {
         warn shift @error_strings while @error_strings;
         die "DB_DEBUG>$0:\n====> cannot connect to the OSCAR database";
     }
 
-    my $msg = "";
+    # OL: The 3 following lines seems unused.( $msg $os $distro_string )
+    #my $msg = "";
+    #my $os = &distro_detect_or_die();
+    #my $distro_string = $os->{compat_distro} . "-" . $os->{compat_distrover} .
+    #    "-" . $os->{arch};
 
-    my $os = &distro_detect_or_die();
-
-    my $distro_string = $os->{compat_distro} . "-" . $os->{compat_distrover} .
-        "-" . $os->{arch};
-
     if (OSCAR::Database::create_database_tables (\%options, \...@error_strings)) {
         die "ERROR: Impossible to create the database tables";
     }
@@ -168,6 +173,10 @@
     # The env variable $ENV{OSCAR_HEAD_INTERNAL_INTERFACE} should be defined
     # at wizard_prep. If not, we setup by default (to be eth0).
     # This is necessary for the embeded script "set_global_oscar_values"
+
+    # OL: 2 lines below and comment above obsoletes
+    #     (only used here and network sanity check).
+
     $ENV{OSCAR_HEAD_INTERNAL_INTERFACE} = "eth0"
         if ! exists $ENV{OSCAR_HEAD_INTERNAL_INTERFACE};
 
@@ -233,10 +242,8 @@
 
 
     OSCAR::oda::oda_disconnect( \%options , undef );
-} elsif ($db_type eq "file") {
+} else {
     print "File based ODA, nothing to do\n";
-} else {
-    die "ERROR: Unknow ODA type ($db_type)\n";
 }
 exit 0;
 
Index: set_global_oscar_values
===================================================================
--- set_global_oscar_values	(révision 9116)
+++ set_global_oscar_values	(copie de travail)
@@ -44,6 +44,7 @@
 use OSCAR::Package;
 use OSCAR::PackagePath;
 use OSCAR::ConfigManager;
+use OSCAR::MAC;
 use POSIX;
 use OSCAR::oda;
 use Carp;
@@ -227,6 +228,13 @@
     }
 }
 
+# Set the default install mode to systemimager-rsync that correspond to the default at step 6
+...@error_strings = ("cannot update table clusters (install_mode field)");
+OSCAR::Database::set_install_mode('systemimager-rsync',
+                                  \%options,
+                                  \...@error_strings);
+OSCAR::MAC::__enable_install_mode();
+
 # Set up "Group" table with the global values.
 # The current group values are..
 #my @groups = ("oscar_server", "oscar_clients", "Default", "image", "core", "included");
------------------------------------------------------------------------------
Join us December 9, 2009 for the Red Hat Virtual Experience,
a free event focused on virtualization and cloud computing. 
Attend in-depth sessions from your desk. Your couch. Anywhere.
http://p.sf.net/sfu/redhat-sfdev2dev
_______________________________________________
Oscar-devel mailing list
Oscar-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/oscar-devel

Reply via email to