mirror of
https://github.com/OpenNebula/one.git
synced 2025-02-02 09:47:00 +03:00
Feature #1683: Simplify bootstrap methods
This commit is contained in:
parent
2054836301
commit
a9bccd4423
@ -58,7 +58,7 @@ public:
|
|||||||
* @param obj Callbackable obj to call if the query succeeds
|
* @param obj Callbackable obj to call if the query succeeds
|
||||||
* @return 0 on success
|
* @return 0 on success
|
||||||
*/
|
*/
|
||||||
int exec(ostringstream& cmd, Callbackable* obj=0);
|
int exec(ostringstream& cmd, Callbackable* obj=0, bool quiet=false);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This function returns a legal SQL string that can be used in an SQL
|
* This function returns a legal SQL string that can be used in an SQL
|
||||||
@ -143,7 +143,7 @@ public:
|
|||||||
|
|
||||||
~MySqlDB(){};
|
~MySqlDB(){};
|
||||||
|
|
||||||
int exec(ostringstream& cmd, Callbackable* obj=0){return -1;};
|
int exec(ostringstream& cmd, Callbackable* obj=0, bool quiet=false){return -1;};
|
||||||
|
|
||||||
char * escape_str(const string& str){return 0;};
|
char * escape_str(const string& str){return 0;};
|
||||||
|
|
||||||
|
@ -341,7 +341,7 @@ public:
|
|||||||
/**
|
/**
|
||||||
* Starts all the modules and services for OpenNebula
|
* Starts all the modules and services for OpenNebula
|
||||||
*/
|
*/
|
||||||
void start();
|
void start(bool bootstrap_only=false);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initialize the database
|
* Initialize the database
|
||||||
|
@ -37,10 +37,10 @@ public:
|
|||||||
* Performs a DB transaction
|
* Performs a DB transaction
|
||||||
* @param sql_cmd the SQL command
|
* @param sql_cmd the SQL command
|
||||||
* @param callbak function to execute on each data returned
|
* @param callbak function to execute on each data returned
|
||||||
* @param arg to pass to the callback function
|
* @param quiet True to log errors with DDEBUG level instead of ERROR
|
||||||
* @return 0 on success
|
* @return 0 on success
|
||||||
*/
|
*/
|
||||||
virtual int exec(ostringstream& cmd, Callbackable* obj=0) = 0;
|
virtual int exec(ostringstream& cmd, Callbackable* obj=0, bool quiet=false) = 0;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This function returns a legal SQL string that can be used in an SQL
|
* This function returns a legal SQL string that can be used in an SQL
|
||||||
|
@ -57,7 +57,7 @@ public:
|
|||||||
* @param arg to pass to the callback function
|
* @param arg to pass to the callback function
|
||||||
* @return 0 on success
|
* @return 0 on success
|
||||||
*/
|
*/
|
||||||
int exec(ostringstream& cmd, Callbackable* obj=0);
|
int exec(ostringstream& cmd, Callbackable* obj=0, bool quiet=false);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This function returns a legal SQL string that can be used in an SQL
|
* This function returns a legal SQL string that can be used in an SQL
|
||||||
@ -113,7 +113,7 @@ public:
|
|||||||
|
|
||||||
~SqliteDB(){};
|
~SqliteDB(){};
|
||||||
|
|
||||||
int exec(ostringstream& cmd, Callbackable* obj=0){return -1;};
|
int exec(ostringstream& cmd, Callbackable* obj=0, bool quiet=false){return -1;};
|
||||||
|
|
||||||
char * escape_str(const string& str){return 0;};
|
char * escape_str(const string& str){return 0;};
|
||||||
|
|
||||||
|
@ -24,7 +24,6 @@
|
|||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <libxml/parser.h>
|
#include <libxml/parser.h>
|
||||||
|
|
||||||
#include <errno.h>
|
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
@ -117,6 +116,7 @@ int SystemDB::select_cb(void *_loaded_db_version, int num, char **values,
|
|||||||
|
|
||||||
int SystemDB::check_db_version()
|
int SystemDB::check_db_version()
|
||||||
{
|
{
|
||||||
|
int rc;
|
||||||
ostringstream oss;
|
ostringstream oss;
|
||||||
|
|
||||||
string loaded_db_version = "";
|
string loaded_db_version = "";
|
||||||
@ -133,6 +133,21 @@ int SystemDB::check_db_version()
|
|||||||
oss.str("");
|
oss.str("");
|
||||||
unset_callback();
|
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 )
|
if( Nebula::db_version() != loaded_db_version )
|
||||||
{
|
{
|
||||||
oss << "Database version mismatch. "
|
oss << "Database version mismatch. "
|
||||||
@ -254,10 +269,29 @@ int SystemDB::select_sys_attribute(const string& attr_name, string& attr_xml)
|
|||||||
/* -------------------------------------------------------------------------- */
|
/* -------------------------------------------------------------------------- */
|
||||||
|
|
||||||
void Nebula::init()
|
void Nebula::init()
|
||||||
|
{
|
||||||
|
start(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* -------------------------------------------------------------------------- */
|
||||||
|
/* -------------------------------------------------------------------------- */
|
||||||
|
|
||||||
|
void Nebula::start(bool bootstrap_only)
|
||||||
{
|
{
|
||||||
int rc;
|
int rc;
|
||||||
|
int fd;
|
||||||
|
sigset_t mask;
|
||||||
|
int signal;
|
||||||
|
char hn[80];
|
||||||
string scripts_remote_dir;
|
string scripts_remote_dir;
|
||||||
|
|
||||||
|
if ( gethostname(hn,79) != 0 )
|
||||||
|
{
|
||||||
|
throw runtime_error("Error getting hostname");
|
||||||
|
}
|
||||||
|
|
||||||
|
hostname = hn;
|
||||||
|
|
||||||
// -----------------------------------------------------------
|
// -----------------------------------------------------------
|
||||||
// Configuration
|
// Configuration
|
||||||
// -----------------------------------------------------------
|
// -----------------------------------------------------------
|
||||||
@ -350,7 +384,7 @@ void Nebula::init()
|
|||||||
xmlInitParser();
|
xmlInitParser();
|
||||||
|
|
||||||
// -----------------------------------------------------------
|
// -----------------------------------------------------------
|
||||||
// Pools
|
// Database
|
||||||
// -----------------------------------------------------------
|
// -----------------------------------------------------------
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@ -358,7 +392,6 @@ void Nebula::init()
|
|||||||
int rc;
|
int rc;
|
||||||
|
|
||||||
bool db_is_sqlite = true;
|
bool db_is_sqlite = true;
|
||||||
bool bootstrap_db = false;
|
|
||||||
|
|
||||||
string server = "localhost";
|
string server = "localhost";
|
||||||
string port_str;
|
string port_str;
|
||||||
@ -420,19 +453,8 @@ void Nebula::init()
|
|||||||
|
|
||||||
if ( db_is_sqlite )
|
if ( db_is_sqlite )
|
||||||
{
|
{
|
||||||
db_name = var_location + "one.db";
|
string 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);
|
db = new SqliteDB(db_name);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@ -440,41 +462,28 @@ void Nebula::init()
|
|||||||
ostringstream oss;
|
ostringstream oss;
|
||||||
|
|
||||||
db = new MySqlDB(server,port,user,passwd,db_name);
|
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
|
// Prepare the SystemDB and check versions
|
||||||
// ---------------------------------------------------------------------
|
// ---------------------------------------------------------------------
|
||||||
|
|
||||||
|
NebulaLog::log("ONE",Log::INFO,"Checking database version.");
|
||||||
|
|
||||||
system_db = new SystemDB(db);
|
system_db = new SystemDB(db);
|
||||||
|
|
||||||
if (bootstrap_db) {
|
rc = system_db->check_db_version();
|
||||||
|
|
||||||
|
if( rc == -1 )
|
||||||
|
{
|
||||||
|
throw runtime_error("Database version mismatch.");
|
||||||
|
}
|
||||||
|
|
||||||
|
if( rc == -2 )
|
||||||
|
{
|
||||||
rc = 0;
|
rc = 0;
|
||||||
NebulaLog::log("ONE",Log::INFO,
|
|
||||||
"Bootstrapping OpenNebula database.");
|
NebulaLog::log("ONE",Log::INFO,"Bootstrapping OpenNebula database.");
|
||||||
|
|
||||||
rc += VirtualMachinePool::bootstrap(db);
|
rc += VirtualMachinePool::bootstrap(db);
|
||||||
rc += HostPool::bootstrap(db);
|
rc += HostPool::bootstrap(db);
|
||||||
@ -503,41 +512,25 @@ void Nebula::init()
|
|||||||
throw runtime_error("Error bootstrapping database.");
|
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&)
|
catch (exception&)
|
||||||
{
|
{
|
||||||
throw;
|
throw;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (bootstrap_only)
|
||||||
|
{
|
||||||
|
//XML Library
|
||||||
|
xmlCleanupParser();
|
||||||
|
|
||||||
|
NebulaLog::log("ONE", Log::INFO, "Database bootstrap finalized, exiting.\n");
|
||||||
|
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* -------------------------------------------------------------------------- */
|
// -----------------------------------------------------------
|
||||||
/* -------------------------------------------------------------------------- */
|
// Pools
|
||||||
|
// -----------------------------------------------------------
|
||||||
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
|
try
|
||||||
{
|
{
|
||||||
int size;
|
int size;
|
||||||
|
@ -132,7 +132,7 @@ MySqlDB::~MySqlDB()
|
|||||||
|
|
||||||
/* -------------------------------------------------------------------------- */
|
/* -------------------------------------------------------------------------- */
|
||||||
|
|
||||||
int MySqlDB::exec(ostringstream& cmd, Callbackable* obj)
|
int MySqlDB::exec(ostringstream& cmd, Callbackable* obj, bool quiet)
|
||||||
{
|
{
|
||||||
int rc;
|
int rc;
|
||||||
|
|
||||||
@ -142,6 +142,8 @@ int MySqlDB::exec(ostringstream& cmd, Callbackable* obj)
|
|||||||
str = cmd.str();
|
str = cmd.str();
|
||||||
c_str = str.c_str();
|
c_str = str.c_str();
|
||||||
|
|
||||||
|
Log::MessageType error_level = quiet ? Log::DDEBUG : Log::ERROR;
|
||||||
|
|
||||||
MYSQL *db;
|
MYSQL *db;
|
||||||
|
|
||||||
db = get_db_connection();
|
db = get_db_connection();
|
||||||
@ -176,11 +178,11 @@ int MySqlDB::exec(ostringstream& cmd, Callbackable* obj)
|
|||||||
oss << ", error " << err_num << " : " << err_msg;
|
oss << ", error " << err_num << " : " << err_msg;
|
||||||
}
|
}
|
||||||
|
|
||||||
NebulaLog::log("ONE",Log::ERROR,oss);
|
NebulaLog::log("ONE",error_level,oss);
|
||||||
|
|
||||||
free_db_connection(db);
|
free_db_connection(db);
|
||||||
|
|
||||||
return err_num;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -204,11 +206,11 @@ int MySqlDB::exec(ostringstream& cmd, Callbackable* obj)
|
|||||||
oss << "SQL command was: " << c_str;
|
oss << "SQL command was: " << c_str;
|
||||||
oss << ", error " << err_num << " : " << err_msg;
|
oss << ", error " << err_num << " : " << err_msg;
|
||||||
|
|
||||||
NebulaLog::log("ONE",Log::ERROR,oss);
|
NebulaLog::log("ONE",error_level,oss);
|
||||||
|
|
||||||
free_db_connection(db);
|
free_db_connection(db);
|
||||||
|
|
||||||
return err_num;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fetch the names of the fields
|
// Fetch the names of the fields
|
||||||
|
@ -66,7 +66,7 @@ SqliteDB::~SqliteDB()
|
|||||||
|
|
||||||
/* -------------------------------------------------------------------------- */
|
/* -------------------------------------------------------------------------- */
|
||||||
|
|
||||||
int SqliteDB::exec(ostringstream& cmd, Callbackable* obj)
|
int SqliteDB::exec(ostringstream& cmd, Callbackable* obj, bool quiet)
|
||||||
{
|
{
|
||||||
int rc;
|
int rc;
|
||||||
|
|
||||||
@ -119,10 +119,12 @@ int SqliteDB::exec(ostringstream& cmd, Callbackable* obj)
|
|||||||
{
|
{
|
||||||
if (err_msg != 0)
|
if (err_msg != 0)
|
||||||
{
|
{
|
||||||
|
Log::MessageType error_level = quiet ? Log::DDEBUG : Log::ERROR;
|
||||||
|
|
||||||
ostringstream oss;
|
ostringstream oss;
|
||||||
|
|
||||||
oss << "SQL command was: " << c_str << ", error: " << err_msg;
|
oss << "SQL command was: " << c_str << ", error: " << err_msg;
|
||||||
NebulaLog::log("ONE",Log::ERROR,oss);
|
NebulaLog::log("ONE",error_level,oss);
|
||||||
|
|
||||||
sqlite3_free(err_msg);
|
sqlite3_free(err_msg);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user