mirror of
https://github.com/OpenNebula/one.git
synced 2025-03-26 06:50:09 +03:00
feature #206: New configuration parameters to select the DB backend
This commit is contained in:
parent
e9ba339e2b
commit
ce6116bf24
@ -15,6 +15,13 @@
|
||||
#
|
||||
# PORT: Port where oned will listen for xmlrpc calls.
|
||||
#
|
||||
# DB: Configuration attributes for the database backend
|
||||
# backend : can be sqlite or mysql (default is sqlite)
|
||||
# server : (mysql) host name or an IP address for the MySQL server
|
||||
# user : (mysql) user's MySQL login ID
|
||||
# passwd : (mysql) the password for user
|
||||
# db_name : (mysql) the database name
|
||||
#
|
||||
# DEBUG_LEVEL: 0 = ERROR, 1 = WARNING, 2 = INFO, 3 = DEBUG
|
||||
#*******************************************************************************
|
||||
|
||||
@ -26,6 +33,15 @@ VM_POLLING_INTERVAL = 60
|
||||
|
||||
PORT=2633
|
||||
|
||||
DB = [ backend = "sqlite" ]
|
||||
|
||||
# Sample configuration for MySQL
|
||||
# DB = [ backend = "mysql",
|
||||
# server = "localhost",
|
||||
# user = "oneadmin",
|
||||
# passwd = "oneadmin",
|
||||
# db_name = "opennebula" ]
|
||||
|
||||
DEBUG_LEVEL=3
|
||||
|
||||
#*******************************************************************************
|
||||
|
@ -18,6 +18,7 @@
|
||||
#include "NebulaLog.h"
|
||||
#include "VirtualMachine.h"
|
||||
#include "SqliteDB.h"
|
||||
#include "MySqlDB.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdexcept>
|
||||
@ -120,21 +121,90 @@ void Nebula::start()
|
||||
|
||||
try
|
||||
{
|
||||
string db_name = var_location + "one.db";
|
||||
struct stat db_stat;
|
||||
bool db_bootstrap = stat(db_name.c_str(), &db_stat) != 0;
|
||||
vector<const Attribute *> dbs;
|
||||
int rc;
|
||||
|
||||
db = new SqliteDB(db_name);
|
||||
bool db_is_sqlite = true;
|
||||
|
||||
if (db_bootstrap)
|
||||
string server = "localhost";
|
||||
string user = "oneadmin";
|
||||
string passwd = "oneadmin";
|
||||
string db_name = "opennebula";
|
||||
|
||||
rc = nebula_configuration->get("DB", dbs);
|
||||
|
||||
if ( rc != 0 )
|
||||
{
|
||||
NebulaLog::log("ONE",Log::INFO,"Bootstraping OpenNebula database.");
|
||||
string value;
|
||||
const VectorAttribute * db = static_cast<const VectorAttribute *>
|
||||
(dbs[0]);
|
||||
value = db->vector_value("BACKEND");
|
||||
|
||||
VirtualMachinePool::bootstrap(db);
|
||||
HostPool::bootstrap(db);
|
||||
VirtualNetworkPool::bootstrap(db);
|
||||
UserPool::bootstrap(db);
|
||||
if (value == "mysql")
|
||||
{
|
||||
db_is_sqlite = false;
|
||||
|
||||
value = db->vector_value("SERVER");
|
||||
if (!value.empty())
|
||||
{
|
||||
server = value;
|
||||
}
|
||||
|
||||
value = db->vector_value("USER");
|
||||
if (!value.empty())
|
||||
{
|
||||
user = value;
|
||||
}
|
||||
|
||||
value = db->vector_value("PASSWD");
|
||||
if (!value.empty())
|
||||
{
|
||||
passwd = value;
|
||||
}
|
||||
|
||||
value = db->vector_value("DB_NAME");
|
||||
if (!value.empty())
|
||||
{
|
||||
db_name = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ( db_is_sqlite )
|
||||
{
|
||||
string db_name = var_location + "one.db";
|
||||
|
||||
db = new SqliteDB(db_name);
|
||||
}
|
||||
else
|
||||
{
|
||||
ostringstream oss;
|
||||
|
||||
db = new MySqlDB(server,user,passwd,0);
|
||||
|
||||
oss << "CREATE DATABASE IF NOT EXISTS " << db_name;
|
||||
rc = db->exec(oss);
|
||||
|
||||
if ( rc != 0 )
|
||||
{
|
||||
throw runtime_error("Could not create database.");
|
||||
}
|
||||
|
||||
oss.str("");
|
||||
oss << "USE " << db_name;
|
||||
rc = db->exec(oss);
|
||||
if ( rc != 0 )
|
||||
{
|
||||
throw runtime_error("Could not open database.");
|
||||
}
|
||||
}
|
||||
|
||||
NebulaLog::log("ONE",Log::INFO,"Bootstraping OpenNebula database.");
|
||||
|
||||
VirtualMachinePool::bootstrap(db);
|
||||
HostPool::bootstrap(db);
|
||||
VirtualNetworkPool::bootstrap(db);
|
||||
UserPool::bootstrap(db);
|
||||
}
|
||||
catch (exception&)
|
||||
{
|
||||
|
Loading…
x
Reference in New Issue
Block a user