1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-08-24 17:49:28 +03:00

Feature #1683: create a way to initialize without service start + This allows a mode to just initialize the database + Changes the decision making about DB bootstrapping such that a missing DB no longer produces misleading messages in the log file + Allows the DB to be initialized/bootstrapped during an automated install process without having to wait for some arbitrary time to kill the daemon - return the SQL error code from the DB::exec method + This allows to make decisions about DB bootstrapping earlier and avoids misleading messages in the log file

This commit is contained in:
Robert Schweikert
2013-10-05 15:34:55 -04:00
committed by Carlos Martín
parent 515f127acf
commit 2054836301
5 changed files with 112 additions and 55 deletions

View File

@ -343,6 +343,11 @@ public:
*/
void start();
/**
* Initialize the database
*/
void init();
// -----------------------------------------------------------------------
// Configuration attributes (read from oned.conf)
// -----------------------------------------------------------------------

View File

@ -79,7 +79,7 @@ if [ ! -d /var/lock/one ]; then
fi
# Start the one daemon
$ONED -f 2>&1 &
$ONED -i 2>&1 &
STARTED=$?
CURPID=$!
@ -89,9 +89,10 @@ if [ $STARTED -ne 0 ]; then
fi
# Give oned a chance to do it's thing...
sleep 2
sleep 5
# OK we're all done here
# Just in case the process gets stuck, kill it
kill -TERM $CURPID > /dev/null 2>&1
counter=0
@ -105,4 +106,4 @@ while ps $CURPID > /dev/null 2>&1; do
done
# If the lock file is left over remove it
rm -f /var/lol/one/one
rm -f /var/lock/one/one

View File

@ -23,7 +23,8 @@
#include <stdlib.h>
#include <stdexcept>
#include <libxml/parser.h>
#include <errno.h>
#include <signal.h>
#include <unistd.h>
#include <fcntl.h>
@ -116,7 +117,6 @@ int SystemDB::select_cb(void *_loaded_db_version, int num, char **values,
int SystemDB::check_db_version()
{
int rc;
ostringstream oss;
string loaded_db_version = "";
@ -133,21 +133,6 @@ int SystemDB::check_db_version()
oss.str("");
unset_callback();
if( loaded_db_version == "" )
{
// Table user_pool is present for all OpenNebula versions, and it
// always contains at least the oneadmin user.
oss << "SELECT MAX(oid) FROM user_pool";
rc = db->exec(oss);
oss.str("");
if( rc != 0 ) // Database needs bootstrap
{
return -2;
}
}
if( Nebula::db_version() != loaded_db_version )
{
oss << "Database version mismatch. "
@ -268,21 +253,10 @@ int SystemDB::select_sys_attribute(const string& attr_name, string& attr_xml)
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
void Nebula::start()
void Nebula::init()
{
int rc;
int fd;
sigset_t mask;
int signal;
char hn[80];
string scripts_remote_dir;
if ( gethostname(hn,79) != 0 )
{
throw runtime_error("Error getting hostname");
}
hostname = hn;
int rc;
string scripts_remote_dir;
// -----------------------------------------------------------
// Configuration
@ -384,6 +358,7 @@ void Nebula::start()
int rc;
bool db_is_sqlite = true;
bool bootstrap_db = false;
string server = "localhost";
string port_str;
@ -445,8 +420,19 @@ void Nebula::start()
if ( db_is_sqlite )
{
string db_name = var_location + "one.db";
db_name = var_location + "one.db";
rc = access(db_name.c_str(), R_OK|W_OK|F_OK);
if (rc == -1)
{
if (errno == ENOENT)
{
bootstrap_db = true;
}
else
{
throw runtime_error("Could not acces one.db file");
}
}
db = new SqliteDB(db_name);
}
else
@ -454,28 +440,41 @@ void Nebula::start()
ostringstream oss;
db = new MySqlDB(server,port,user,passwd,db_name);
oss << "USE " << db_name;
rc = db->exec(oss);
if ( rc == 1044 )
{
oss.str("");
oss << "CREATE DATABASE " << db_name;
rc = db->exec(oss);
if ( rc == 0 )
{
bootstrap_db = true;
}
else if ( rc == 1044 )
{
throw runtime_error("Could not create database.");
}
}
else if ( rc != 0 )
{
throw runtime_error("Could not open database.");
}
}
// ---------------------------------------------------------------------
// Prepare the SystemDB and check versions
// ---------------------------------------------------------------------
NebulaLog::log("ONE",Log::INFO,"Checking database version.");
system_db = new SystemDB(db);
rc = system_db->check_db_version();
if( rc == -1 )
{
throw runtime_error("Database version mismatch.");
}
if( rc == -2 )
{
if (bootstrap_db) {
rc = 0;
NebulaLog::log("ONE",Log::INFO,"Bootstrapping OpenNebula database.");
NebulaLog::log("ONE",Log::INFO,
"Bootstrapping OpenNebula database.");
rc += VirtualMachinePool::bootstrap(db);
rc += HostPool::bootstrap(db);
@ -502,13 +501,42 @@ void Nebula::start()
if ( rc != 0 )
{
throw runtime_error("Error bootstrapping database.");
}
}
}
NebulaLog::log("ONE",Log::INFO,"Checking database version.");
rc = system_db->check_db_version();
if( rc == -1 )
{
throw runtime_error("Database version mismatch.");
}
}
catch (exception&)
{
throw;
}
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
void Nebula::start()
{
int rc;
int fd;
sigset_t mask;
int signal;
char hn[80];
if ( gethostname(hn,79) != 0 )
{
throw runtime_error("Error getting hostname");
}
hostname = hn;
Nebula::init();
try
{

View File

@ -32,9 +32,10 @@ static const char * usage =
"SYNOPSIS\n"
" Starts the OpenNebula daemon\n\n"
"OPTIONS\n"
"\t-h\tprints this help.\n"
"\t-v\tprints OpenNebula version and license\n"
"\t-f\tforeground, do not fork the oned daemon\n";
"\t-h\tprints this help.\n"
"\t-f\tforeground, do not fork the oned daemon\n"
"\t-i\tinitialize the dabase and exit.\n";
static const char * susage =
"usage: oned [-h] [-v] [-f]\n";
@ -50,6 +51,24 @@ static void print_license()
<< "(http://www.apache.org/licenses/LICENSE-2.0).\n";
}
/* ------------------------------------------------------------------------- */
/* ------------------------------------------------------------------------- */
static void oned_init()
{
try
{
Nebula& nd = Nebula::instance();
nd.init();
}
catch (exception &e)
{
cerr << e.what() << endl;
return;
}
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
@ -81,7 +100,7 @@ int main(int argc, char **argv)
string wd;
int rc;
while((opt = getopt(argc,argv,"vhf")) != -1)
while((opt = getopt(argc,argv,"vhif")) != -1)
switch(opt)
{
case 'v':
@ -92,6 +111,10 @@ int main(int argc, char **argv)
cout << usage;
exit(0);
break;
case 'i':
oned_init();
exit(0);
break;
case 'f':
foreground = true;
break;

View File

@ -180,7 +180,7 @@ int MySqlDB::exec(ostringstream& cmd, Callbackable* obj)
free_db_connection(db);
return -1;
return err_num;
}
@ -208,7 +208,7 @@ int MySqlDB::exec(ostringstream& cmd, Callbackable* obj)
free_db_connection(db);
return -1;
return err_num;
}
// Fetch the names of the fields