1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-01-10 01:17:40 +03:00

feature #206: VirtualMachine uses SqlDB

This commit is contained in:
Ruben S. Montero 2010-04-10 22:16:47 +02:00
parent aeb27c1004
commit ce7d0681e3
6 changed files with 277 additions and 365 deletions

View File

@ -17,17 +17,10 @@
#ifndef HISTORY_H_ #ifndef HISTORY_H_
#define HISTORY_H_ #define HISTORY_H_
#include <sqlite3.h>
#include "ObjectSQL.h" #include "ObjectSQL.h"
using namespace std; using namespace std;
extern "C" int history_select_cb (
void * _history,
int num,
char ** values,
char ** names);
/** /**
* The History class, it represents an execution record of a Virtual Machine. * The History class, it represents an execution record of a Virtual Machine.
*/ */
@ -37,23 +30,23 @@ class History:public ObjectSQL
public: public:
enum MigrationReason enum MigrationReason
{ {
NONE, /** < Normal termination in host */ NONE, /** < Normal termination in host */
ERROR, /** < The VM was migrated because of an error */ ERROR, /** < The VM was migrated because of an error */
STOP_RESUME,/** < The VM was migrated because of a stop/resume request*/ STOP_RESUME,/** < The VM was migrated because of a stop/resume request*/
USER, /** < The VM was migrated because of an explicit request */ USER, /** < The VM was migrated because of an explicit request */
CANCEL /** < The VM was migrated because of an explicit cancel */ CANCEL /** < The VM was migrated because of an explicit cancel */
}; };
History(int oid, int _seq = -1); History(int oid, int _seq = -1);
History( History(
int oid, int oid,
int seq, int seq,
int hid, int hid,
string& hostname, string& hostname,
string& vm_dir, string& vm_dir,
string& vmm, string& vmm,
string& tm); string& tm);
~History(){}; ~History(){};
@ -77,9 +70,10 @@ public:
* @return a reference to the generated string * @return a reference to the generated string
*/ */
string& to_xml(string& xml) const; string& to_xml(string& xml) const;
private: private:
friend class VirtualMachine; friend class VirtualMachine;
friend class VirtualMachinePool;
// ---------------------------------------- // ----------------------------------------
// DataBase implementation variables // DataBase implementation variables
@ -166,25 +160,19 @@ private:
string checkpoint_file; string checkpoint_file;
string rdeployment_file; string rdeployment_file;
friend int history_select_cb (
void * _history,
int num,
char ** values,
char ** names);
/** /**
* Writes the history record in the DB * Writes the history record in the DB
* @param db pointer to the database. * @param db pointer to the database.
* @return 0 on success. * @return 0 on success.
*/ */
int insert(SqliteDB * db); int insert(SqlDB * db);
/** /**
* Reads the history record from the DB * Reads the history record from the DB
* @param db pointer to the database. * @param db pointer to the database.
* @return 0 on success. * @return 0 on success.
*/ */
int select(SqliteDB * db); int select(SqlDB * db);
/** /**
* Removes the all history records from the DB * Removes the all history records from the DB
@ -192,26 +180,26 @@ private:
* @return 0 on success. * @return 0 on success.
*/ */
int drop(SqliteDB * db); int drop(SqlDB * db);
/** /**
* Updates the history record * Updates the history record
* @param db pointer to the database. * @param db pointer to the database.
* @return 0 on success. * @return 0 on success.
*/ */
int update(SqliteDB * db) int update(SqlDB * db)
{ {
return insert(db); return insert(db);
} }
/** /**
* Function to unmarshall a history object * Callback function to unmarshall a history object (History::select)
* @param num the number of columns read from the DB * @param num the number of columns read from the DB
* @para names the column names * @para names the column names
* @para vaues the column values * @para vaues the column values
* @return 0 on success * @return 0 on success
*/ */
int unmarshall(int num, char **names, char ** values); int select_cb(void *nil, int num, char **values, char **names);
/** /**
* Function to unmarshall a History object into an output stream with XML * Function to unmarshall a History object into an output stream with XML
@ -222,10 +210,7 @@ private:
* @param vaues the column values * @param vaues the column values
* @return 0 on success * @return 0 on success
*/ */
static int unmarshall(ostringstream& oss, static int dump(ostringstream& oss, int num, char **names, char **values);
int num,
char ** names,
char ** values);
}; };
#endif /*HISTORY_H_*/ #endif /*HISTORY_H_*/

View File

@ -27,9 +27,6 @@
using namespace std; using namespace std;
extern "C" int vm_select_cb (void * _vm, int num,char ** values, char ** names);
extern "C" int vm_dump_cb (void * _oss, int num,char ** values, char ** names);
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
@ -125,7 +122,7 @@ public:
* Function to print the VirtualMachine object into a string in * Function to print the VirtualMachine object into a string in
* plain text * plain text
* @param str the resulting string * @param str the resulting string
* @return a reference to the generated string * @return a reference to the generated string
*/ */
string& to_str(string& str) const; string& to_str(string& str) const;
@ -133,10 +130,10 @@ public:
* Function to print the VirtualMachine object into a string in * Function to print the VirtualMachine object into a string in
* XML format * XML format
* @param xml the resulting XML string * @param xml the resulting XML string
* @return a reference to the generated string * @return a reference to the generated string
*/ */
string& to_xml(string& xml) const; string& to_xml(string& xml) const;
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
// Dynamic Info // Dynamic Info
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
@ -291,7 +288,7 @@ public:
}; };
/** /**
* Returns the TM driver name for the previous host. The * Returns the TM driver name for the previous host. The
* hasPreviousHistory() function MUST be called before this one. * hasPreviousHistory() function MUST be called before this one.
* @return the TM mad name * @return the TM mad name
*/ */
@ -608,19 +605,19 @@ public:
{ {
vm_template.to_xml(xml); vm_template.to_xml(xml);
} }
/** /**
* Parse a string and substitute variables (e.g. $NAME) using the VM * Parse a string and substitute variables (e.g. $NAME) using the VM
* template values: * template values:
* @param attribute, the string to be parsed * @param attribute, the string to be parsed
* @param parsed, the resulting parsed string * @param parsed, the resulting parsed string
* @return 0 on success. * @return 0 on success.
*/ */
int parse_template_attribute(const string& attribute, int parse_template_attribute(const string& attribute,
string& parsed); string& parsed);
/** /**
* Parse a string and substitute variables (e.g. $NAME) using the VM * Parse a string and substitute variables (e.g. $NAME) using the VM
* template values (blocking-free version for cross references): * template values (blocking-free version for cross references):
* @param vm_id ID of the VM used to substitute the variables * @param vm_id ID of the VM used to substitute the variables
* @param attribute, the string to be parsed * @param attribute, the string to be parsed
@ -632,14 +629,14 @@ public:
const string& attribute, const string& attribute,
string& parsed, string& parsed,
char ** error_msg) char ** error_msg)
{ {
return parse_attribute(0,vm_id,attribute,parsed,error_msg); return parse_attribute(0,vm_id,attribute,parsed,error_msg);
} }
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
// States // States
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
/** /**
* Returns the VM state (Dispatch Manager) * Returns the VM state (Dispatch Manager)
* @return the VM state * @return the VM state
@ -747,18 +744,6 @@ private:
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
friend class VirtualMachinePool; friend class VirtualMachinePool;
friend int vm_select_cb (
void * _vm,
int num,
char ** values,
char ** names);
friend int vm_dump_cb (
void * _vm,
int num,
char ** values,
char ** names);
// ************************************************************************* // *************************************************************************
// Virtual Machine Attributes // Virtual Machine Attributes
// ************************************************************************* // *************************************************************************
@ -857,9 +842,9 @@ private:
/** /**
* Log class for the virtual machine, it writes log messages in * Log class for the virtual machine, it writes log messages in
* $ONE_LOCATION/var/$VID/vm.log * $ONE_LOCATION/var/$VID/vm.log
* or, in case that OpenNebula is installed in root * or, in case that OpenNebula is installed in root
* /var/log/one/$VM_ID.log * /var/log/one/$VM_ID.log
*/ */
Log * _log; Log * _log;
@ -870,44 +855,33 @@ private:
/** /**
* Bootstraps the database table(s) associated to the VirtualMachine * Bootstraps the database table(s) associated to the VirtualMachine
*/ */
static void bootstrap(SqliteDB * db) static void bootstrap(SqlDB * db)
{ {
db->exec(VirtualMachine::db_bootstrap); ostringstream oss_vm(VirtualMachine::db_bootstrap);
ostringstream oss_tmpl(VirtualMachineTemplate::db_bootstrap);
ostringstream oss_hist(History::db_bootstrap);
db->exec(VirtualMachineTemplate::db_bootstrap); db->exec(oss_vm);
db->exec(oss_tmpl);
db->exec(History::db_bootstrap); db->exec(oss_hist);
}; };
/** /**
* Function to unmarshall a VM object, an associated classes. * Callback function to unmarshall a VirtualMachine object
* (VirtualMachine::select)
* @param num the number of columns read from the DB * @param num the number of columns read from the DB
* @param names the column names * @param names the column names
* @param vaues the column values * @param vaues the column values
* @return 0 on success * @return 0 on success
*/ */
int unmarshall(int num, char **names, char ** values); int select_cb(void *nil, int num, char **names, char ** values);
/**
* Function to unmarshall a VM object into an output stream with XML
* format.
* @param oss the output stream
* @param num the number of columns read from the DB
* @param names the column names
* @param vaues the column values
* @return 0 on success
*/
static int unmarshall(ostringstream& oss,
int num,
char ** names,
char ** values);
/** /**
* Updates the VM history record * Updates the VM history record
* @param db pointer to the db * @param db pointer to the db
* @return 0 on success * @return 0 on success
*/ */
int update_history(SqliteDB * db) int update_history(SqlDB * db)
{ {
if ( history != 0 ) if ( history != 0 )
{ {
@ -922,7 +896,7 @@ private:
* @param db pointer to the db * @param db pointer to the db
* @return 0 on success * @return 0 on success
*/ */
int update_previous_history(SqliteDB * db) int update_previous_history(SqlDB * db)
{ {
if ( previous_history != 0 ) if ( previous_history != 0 )
{ {
@ -941,9 +915,9 @@ private:
* @return 0 on success * @return 0 on success
*/ */
int update_template_attribute( int update_template_attribute(
SqliteDB * db, SqlDB * db,
string& name, string& name,
string& value) string& value)
{ {
SingleAttribute * sattr; SingleAttribute * sattr;
int rc; int rc;
@ -966,7 +940,7 @@ private:
* @param attribute the new attribute for the template * @param attribute the new attribute for the template
* @return 0 on success * @return 0 on success
*/ */
int insert_template_attribute(SqliteDB * db, Attribute * attribute) int insert_template_attribute(SqlDB * db, Attribute * attribute)
{ {
return vm_template.insert_attribute(db,attribute); return vm_template.insert_attribute(db,attribute);
} }
@ -991,13 +965,13 @@ private:
* @param parsed, the resulting parsed string * @param parsed, the resulting parsed string
* @param error_msg, string describing the syntax error * @param error_msg, string describing the syntax error
* @return 0 on success. * @return 0 on success.
*/ */
static int parse_attribute(VirtualMachine * vm, static int parse_attribute(VirtualMachine * vm,
int vm_id, int vm_id,
const string& attribute, const string& attribute,
string& parsed, string& parsed,
char ** error_msg); char ** error_msg);
protected: protected:
//************************************************************************** //**************************************************************************
@ -1042,22 +1016,22 @@ protected:
* @param db pointer to the db * @param db pointer to the db
* @return 0 on success * @return 0 on success
*/ */
int select(SqliteDB * db); int select(SqlDB * db);
/** /**
* Writes the Virtual Machine and its associated template in the database. * Writes the Virtual Machine and its associated template in the database.
* @param db pointer to the db * @param db pointer to the db
* @return 0 on success * @return 0 on success
*/ */
virtual int insert(SqliteDB * db); virtual int insert(SqlDB * db);
/** /**
* Writes/updates the Virtual Machine data fields in the database. * Writes/updates the Virtual Machine data fields in the database.
* @param db pointer to the db * @param db pointer to the db
* @return 0 on success * @return 0 on success
*/ */
virtual int update(SqliteDB * db); virtual int update(SqlDB * db);
/** /**
* Deletes a VM from the database and all its associated information: * Deletes a VM from the database and all its associated information:
* - History records * - History records
@ -1065,29 +1039,30 @@ protected:
* @param db pointer to the db * @param db pointer to the db
* @return 0 on success * @return 0 on success
*/ */
virtual int drop(SqliteDB * db) virtual int drop(SqlDB * db)
{ {
int rc; int rc;
rc = vm_template.drop(db); rc = vm_template.drop(db);
if ( history != 0 ) if ( history != 0 )
{ {
rc += history->drop(db); rc += history->drop(db);
} }
return rc; return rc;
} }
/** /**
* Dumps the contect of a set of VirtualMachine objects in the given stream * Dumps the contect of a set of VirtualMachine objects in the given stream
* using XML format * using XML format
* @param db pointer to the db
* @param oss the output stream * @param oss the output stream
* @param where string to filter the VirtualMachine objects * @param num the number of columns read from the DB
* @param names the column names
* @param vaues the column values
* @return 0 on success * @return 0 on success
*/ */
static int dump(SqliteDB * db, ostringstream& oss, const string& where); static int dump(ostringstream& oss, int num, char ** values, char ** names);
}; };
#endif /*VIRTUAL_MACHINE_H_*/ #endif /*VIRTUAL_MACHINE_H_*/

View File

@ -32,7 +32,7 @@ class VirtualMachinePool : public PoolSQL
{ {
public: public:
VirtualMachinePool(SqliteDB * db, vector<const Attribute *> hook_mads); VirtualMachinePool(SqlDB * db, vector<const Attribute *> hook_mads);
~VirtualMachinePool(){}; ~VirtualMachinePool(){};
@ -126,11 +126,11 @@ public:
/** /**
* Bootstraps the database table(s) associated to the VirtualMachine pool * Bootstraps the database table(s) associated to the VirtualMachine pool
*/ */
static void bootstrap(SqliteDB * _db) static void bootstrap(SqlDB * _db)
{ {
VirtualMachine::bootstrap(_db); VirtualMachine::bootstrap(_db);
}; };
/** /**
* Dumps the VM pool in XML format. A filter can be also added to the query * Dumps the VM pool in XML format. A filter can be also added to the query
* Also the hostname where the VirtualMachine is running is added to the * Also the hostname where the VirtualMachine is running is added to the
@ -140,19 +140,8 @@ public:
* *
* @return 0 on success * @return 0 on success
*/ */
int dump(ostringstream& oss, const string& where) int dump(ostringstream& oss, const string& where);
{
int rc;
oss << "<VM_POOL>";
rc = VirtualMachine::dump(db,oss,where);
oss << "</VM_POOL>";
return rc;
}
private: private:
/** /**
* Generate context file to be sourced upon VM booting * Generate context file to be sourced upon VM booting
@ -169,6 +158,16 @@ private:
{ {
return new VirtualMachine; return new VirtualMachine;
}; };
/**
* Callback function to get output the vm pool in XML format
* (VirtualMachinePool::dump)
* @param num the number of columns read from the DB
* @param names the column names
* @param vaues the column values
* @return 0 on success
*/
int dump_cb(void * _oss, int num, char **values, char **names);
}; };
#endif /*VIRTUAL_MACHINE_POOL_H_*/ #endif /*VIRTUAL_MACHINE_POOL_H_*/

View File

@ -134,44 +134,44 @@ void History::non_persistent_data()
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
int History::insert(SqliteDB * db) int History::insert(SqlDB * db)
{ {
ostringstream oss; ostringstream oss;
int rc; int rc;
char * sql_hostname; char * sql_hostname;
char * sql_vm_dir; char * sql_vm_dir;
char * sql_vmm_mad_name; char * sql_vmm_mad_name;
char * sql_tm_mad_name; char * sql_tm_mad_name;
if (seq == -1) if (seq == -1)
{ {
return 0; return 0;
} }
sql_hostname = sqlite3_mprintf("%q",hostname.c_str()); sql_hostname = db->escape_str(hostname.c_str());
if ( sql_hostname == 0 ) if ( sql_hostname == 0 )
{ {
goto error_hostname; goto error_hostname;
} }
sql_vm_dir = sqlite3_mprintf("%q",vm_dir.c_str()); sql_vm_dir = db->escape_str(vm_dir.c_str());
if ( sql_vm_dir == 0 ) if ( sql_vm_dir == 0 )
{ {
goto error_vm_dir; goto error_vm_dir;
} }
sql_vmm_mad_name = sqlite3_mprintf("%q",vmm_mad_name.c_str()); sql_vmm_mad_name = db->escape_str(vmm_mad_name.c_str());
if ( sql_vmm_mad_name == 0 ) if ( sql_vmm_mad_name == 0 )
{ {
goto error_vmm; goto error_vmm;
} }
sql_tm_mad_name = sqlite3_mprintf("%q",tm_mad_name.c_str()); sql_tm_mad_name = db->escape_str(tm_mad_name.c_str());
if ( sql_tm_mad_name == 0 ) if ( sql_tm_mad_name == 0 )
{ {
@ -198,19 +198,19 @@ int History::insert(SqliteDB * db)
rc = db->exec(oss); rc = db->exec(oss);
sqlite3_free(sql_hostname); db->free_str(sql_hostname);
sqlite3_free(sql_vm_dir); db->free_str(sql_vm_dir);
sqlite3_free(sql_vmm_mad_name); db->free_str(sql_vmm_mad_name);
sqlite3_free(sql_tm_mad_name); db->free_str(sql_tm_mad_name);
return rc; return rc;
error_tm: error_tm:
sqlite3_free(sql_vmm_mad_name); db->free_str(sql_vmm_mad_name);
error_vmm: error_vmm:
sqlite3_free(sql_vm_dir); db->free_str(sql_vm_dir);
error_vm_dir: error_vm_dir:
sqlite3_free(sql_hostname); db->free_str(sql_hostname);
error_hostname: error_hostname:
return -1; return -1;
} }
@ -218,7 +218,7 @@ error_hostname:
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
int History::unmarshall(int num, char **names, char ** values) int History::select_cb(void *nil, int num, char **values, char **names)
{ {
if ((!values[VID]) || if ((!values[VID]) ||
(!values[SEQ]) || (!values[SEQ]) ||
@ -243,10 +243,10 @@ int History::unmarshall(int num, char **names, char ** values)
oid = atoi(values[VID]); oid = atoi(values[VID]);
seq = atoi(values[SEQ]); seq = atoi(values[SEQ]);
hostname = values[HOSTNAME]; hostname = values[HOSTNAME];
vm_dir = values[VM_DIR]; vm_dir = values[VM_DIR];
hid = atoi(values[HID]); hid = atoi(values[HID]);
vmm_mad_name = values[VMMMAD]; vmm_mad_name = values[VMMMAD];
@ -272,12 +272,9 @@ int History::unmarshall(int num, char **names, char ** values)
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
int History::unmarshall(ostringstream& oss, int History::dump(ostringstream& oss, int num, char **values, char **names)
int num,
char ** names,
char ** values)
{ {
if ((!values[VID])|| if ((!values[VID])||
(!values[SEQ])|| (!values[SEQ])||
(!values[HOSTNAME])|| (!values[HOSTNAME])||
(!values[HID])|| (!values[HID])||
@ -290,11 +287,11 @@ int History::unmarshall(ostringstream& oss,
(!values[EPILOG_STIME])|| (!values[EPILOG_STIME])||
(!values[EPILOG_ETIME])|| (!values[EPILOG_ETIME])||
(!values[REASON])|| (!values[REASON])||
(num != LIMIT)) (num != LIMIT))
{ {
return -1; return -1;
} }
oss << oss <<
"<HISTORY>" << "<HISTORY>" <<
"<SEQ>" << values[SEQ] << "</SEQ>" << "<SEQ>" << values[SEQ] << "</SEQ>" <<
@ -310,34 +307,13 @@ int History::unmarshall(ostringstream& oss,
"<EETIME>" << values[EPILOG_ETIME] << "</EETIME>" << "<EETIME>" << values[EPILOG_ETIME] << "</EETIME>" <<
"<REASON>" << values[REASON] << "</REASON>" << "<REASON>" << values[REASON] << "</REASON>" <<
"</HISTORY>"; "</HISTORY>";
return 0; return 0;
} }
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
extern "C" int history_select_cb ( int History::select(SqlDB * db)
void * _history,
int num,
char ** values,
char ** names)
{
History * history;
history = static_cast<History *>(_history);
if (history == 0)
{
return -1;
}
return history->unmarshall(num,names,values);
};
/* -------------------------------------------------------------------------- */
int History::select(SqliteDB * db)
{ {
ostringstream oss; ostringstream oss;
int rc; int rc;
@ -349,15 +325,17 @@ int History::select(SqliteDB * db)
if ( seq == -1) if ( seq == -1)
{ {
oss << "SELECT * FROM history WHERE vid = "<< oid << oss << "SELECT * FROM history WHERE vid = "<< oid <<
" AND seq=(SELECT MAX(seq) FROM history WHERE vid = " << oid << ")"; " AND seq=(SELECT MAX(seq) FROM history WHERE vid = " << oid << ")";
} }
else else
{ {
oss << "SELECT * FROM history WHERE vid = "<< oid <<" AND seq = "<< seq; oss << "SELECT * FROM history WHERE vid = "<< oid <<" AND seq = "<< seq;
} }
rc = db->exec(oss,history_select_cb,(void *) this); set_callback(static_cast<Callbackable::Callback>(&History::select_cb));
rc = db->exec(oss,this);
if ( rc == 0 ) // Regenerate non-persistent data if ( rc == 0 ) // Regenerate non-persistent data
{ {
@ -370,7 +348,7 @@ int History::select(SqliteDB * db)
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
int History::drop(SqliteDB * db) int History::drop(SqlDB * db)
{ {
ostringstream oss; ostringstream oss;
@ -424,7 +402,7 @@ string& History::to_str(string& str) const
string& History::to_xml(string& xml) const string& History::to_xml(string& xml) const
{ {
ostringstream oss; ostringstream oss;
oss << oss <<
"<HISTORY>" << "<HISTORY>" <<
"<SEQ>" << seq << "</SEQ>" << "<SEQ>" << seq << "</SEQ>" <<

View File

@ -87,7 +87,7 @@ const char * VirtualMachine::db_bootstrap = "CREATE TABLE vm_pool ("
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
int VirtualMachine::unmarshall(int num, char **names, char ** values) int VirtualMachine::select_cb(void *nil, int num, char **values, char **names)
{ {
if ((values[OID] == 0) || if ((values[OID] == 0) ||
(values[UID] == 0) || (values[UID] == 0) ||
@ -134,30 +134,9 @@ int VirtualMachine::unmarshall(int num, char **names, char ** values)
return 0; return 0;
} }
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
extern "C" int vm_select_cb ( int VirtualMachine::select(SqlDB * db)
void * _vm,
int num,
char ** values,
char ** names)
{
VirtualMachine * vm;
vm = static_cast<VirtualMachine *>(_vm);
if (vm == 0)
{
return -1;
}
return vm->unmarshall(num,names,values);
};
/* -------------------------------------------------------------------------- */
int VirtualMachine::select(SqliteDB * db)
{ {
ostringstream oss; ostringstream oss;
ostringstream ose; ostringstream ose;
@ -166,15 +145,17 @@ int VirtualMachine::select(SqliteDB * db)
int boid; int boid;
string filename; string filename;
Nebula& nd = Nebula::instance(); Nebula& nd = Nebula::instance();
set_callback(
static_cast<Callbackable::Callback>(&VirtualMachine::select_cb));
oss << "SELECT * FROM " << table << " WHERE oid = " << oid; oss << "SELECT * FROM " << table << " WHERE oid = " << oid;
boid = oid; boid = oid;
oid = -1; oid = -1;
rc = db->exec(oss,vm_select_cb,(void *) this); rc = db->exec(oss,this);
if ((rc != 0) || (oid != boid )) if ((rc != 0) || (oid != boid ))
{ {
@ -208,14 +189,14 @@ int VirtualMachine::select(SqliteDB * db)
} }
else if (history->seq > 0) else if (history->seq > 0)
{ {
previous_history = new History(oid,history->seq - 1); previous_history = new History(oid,history->seq - 1);
rc = previous_history->select(db); rc = previous_history->select(db);
if ( rc != 0) if ( rc != 0)
{ {
goto error_previous_history; goto error_previous_history;
} }
} }
//Create support directory fo this VM //Create support directory fo this VM
@ -230,15 +211,15 @@ int VirtualMachine::select(SqliteDB * db)
try try
{ {
_log = new Log(nd.get_vm_log_filename(oid),Log::DEBUG); _log = new Log(nd.get_vm_log_filename(oid),Log::DEBUG);
} }
catch(exception &e) catch(exception &e)
{ {
ose << "Error creating log: " << e.what(); ose << "Error creating log: " << e.what();
Nebula::log("ONE",Log::ERROR, ose); Nebula::log("ONE",Log::ERROR, ose);
_log = 0; _log = 0;
} }
return 0; return 0;
@ -258,8 +239,8 @@ error_history:
return -1; return -1;
error_previous_history: error_previous_history:
ose << "Can not get previous history record (seq:" << history->seq ose << "Can not get previous history record (seq:" << history->seq
<< ") for VM id: " << oid; << ") for VM id: " << oid;
log("ONE", Log::ERROR, ose); log("ONE", Log::ERROR, ose);
return -1; return -1;
} }
@ -267,7 +248,7 @@ error_previous_history:
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
int VirtualMachine::insert(SqliteDB * db) int VirtualMachine::insert(SqlDB * db)
{ {
int rc; int rc;
string name; string name;
@ -285,8 +266,8 @@ int VirtualMachine::insert(SqliteDB * db)
attr = new SingleAttribute("VMID",value); attr = new SingleAttribute("VMID",value);
vm_template.set(attr); vm_template.set(attr);
get_template_attribute("NAME",name); get_template_attribute("NAME",name);
if ( name.empty() == true ) if ( name.empty() == true )
@ -333,25 +314,25 @@ int VirtualMachine::insert(SqliteDB * db)
return 0; return 0;
error_update: error_update:
Nebula::log("ONE",Log::ERROR, "Can not update VM in the database"); Nebula::log("ONE",Log::ERROR, "Can not update VM in the database");
vm_template.drop(db); vm_template.drop(db);
return -1; return -1;
error_template: error_template:
Nebula::log("ONE",Log::ERROR, "Can not insert template in the database"); Nebula::log("ONE",Log::ERROR, "Can not insert template in the database");
release_network_leases(); release_network_leases();
return -1; return -1;
error_leases: error_leases:
Nebula::log("ONE",Log::ERROR, "Could not get network lease for VM"); Nebula::log("ONE",Log::ERROR, "Could not get network lease for VM");
release_network_leases(); release_network_leases();
return -1; return -1;
} }
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
int VirtualMachine::update(SqliteDB * db) int VirtualMachine::update(SqlDB * db)
{ {
ostringstream oss; ostringstream oss;
int rc; int rc;
@ -359,18 +340,18 @@ int VirtualMachine::update(SqliteDB * db)
char * sql_deploy_id; char * sql_deploy_id;
char * sql_name; char * sql_name;
sql_deploy_id = sqlite3_mprintf("%q",deploy_id.c_str()); sql_deploy_id = db->escape_str(deploy_id.c_str());
if ( sql_deploy_id == 0 ) if ( sql_deploy_id == 0 )
{ {
return -1; return -1;
} }
sql_name = sqlite3_mprintf("%q",name.c_str()); sql_name = db->escape_str(name.c_str());
if ( sql_name == 0 ) if ( sql_name == 0 )
{ {
sqlite3_free(sql_deploy_id); db->free_str(sql_deploy_id);
return -1; return -1;
} }
@ -390,8 +371,8 @@ int VirtualMachine::update(SqliteDB * db)
net_tx << "," << net_tx << "," <<
net_rx << ")"; net_rx << ")";
sqlite3_free(sql_deploy_id); db->free_str(sql_deploy_id);
sqlite3_free(sql_name); db->free_str(sql_name);
rc = db->exec(oss); rc = db->exec(oss);
@ -401,10 +382,7 @@ int VirtualMachine::update(SqliteDB * db)
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
int VirtualMachine::unmarshall(ostringstream& oss, int VirtualMachine::dump(ostringstream& oss,int num,char **values,char **names)
int num,
char ** names,
char ** values)
{ {
if ((!values[OID])|| if ((!values[OID])||
(!values[UID])|| (!values[UID])||
@ -429,7 +407,7 @@ int VirtualMachine::unmarshall(ostringstream& oss,
"<ID>" << values[OID] << "</ID>" << "<ID>" << values[OID] << "</ID>" <<
"<UID>" << values[UID] << "</UID>" << "<UID>" << values[UID] << "</UID>" <<
"<USERNAME>" << values[LIMIT] << "</USERNAME>"<< "<USERNAME>" << values[LIMIT] << "</USERNAME>"<<
"<NAME>" << values[NAME] << "</NAME>" << "<NAME>" << values[NAME] << "</NAME>" <<
"<LAST_POLL>"<< values[LAST_POLL]<< "</LAST_POLL>"<< "<LAST_POLL>"<< values[LAST_POLL]<< "</LAST_POLL>"<<
"<STATE>" << values[STATE] << "</STATE>" << "<STATE>" << values[STATE] << "</STATE>" <<
"<LCM_STATE>"<< values[LCM_STATE]<< "</LCM_STATE>"<< "<LCM_STATE>"<< values[LCM_STATE]<< "</LCM_STATE>"<<
@ -441,67 +419,18 @@ int VirtualMachine::unmarshall(ostringstream& oss,
"<NET_TX>" << values[NET_TX] << "</NET_TX>" << "<NET_TX>" << values[NET_TX] << "</NET_TX>" <<
"<NET_RX>" << values[NET_RX] << "</NET_RX>"; "<NET_RX>" << values[NET_RX] << "</NET_RX>";
History::unmarshall(oss, num-LIMIT-2, names+LIMIT+1, values+LIMIT+1); History::dump(oss, num-LIMIT-2, values+LIMIT+1, names+LIMIT+1);
oss << "</VM>"; oss << "</VM>";
return 0; return 0;
}
/* -------------------------------------------------------------------------- */
extern "C" int vm_dump_cb (
void * _oss,
int num,
char ** values,
char ** names)
{
ostringstream * oss;
ostringstream dbg;
oss = static_cast<ostringstream *>(_oss);
if (oss == 0)
{
return -1;
}
return VirtualMachine::unmarshall(*oss,num,names,values);
};
/* -------------------------------------------------------------------------- */
int VirtualMachine::dump(SqliteDB * db, ostringstream& oss, const string& where)
{
int rc;
ostringstream cmd;
cmd << "SELECT " << VirtualMachine::table << ".*, "
<< "user_pool.user_name, " << History::table << ".* FROM "
<< VirtualMachine::table
<< " LEFT OUTER JOIN (SELECT *,MAX(seq) FROM "
<< History::table << " GROUP BY vid) AS " << History::table
<< " ON " << VirtualMachine::table << ".oid = "
<< History::table << ".vid LEFT OUTER JOIN (SELECT oid,user_name FROM "
<< "user_pool) AS user_pool ON "
<< VirtualMachine::table << ".uid = user_pool.oid WHERE "
<< VirtualMachine::table << ".state != " << VirtualMachine::DONE;
if ( !where.empty() )
{
cmd << " AND " << where;
}
rc = db->exec(cmd,vm_dump_cb,(void *) &oss);
return rc;
} }
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
void VirtualMachine::add_history( void VirtualMachine::add_history(
int hid, int hid,
string& hostname, string& hostname,
string& vm_dir, string& vm_dir,
string& vmm_mad, string& vmm_mad,
@ -520,7 +449,7 @@ void VirtualMachine::add_history(
if (previous_history != 0) if (previous_history != 0)
{ {
delete previous_history; delete previous_history;
} }
previous_history = history; previous_history = history;
@ -534,29 +463,29 @@ void VirtualMachine::add_history(
void VirtualMachine::cp_history() void VirtualMachine::cp_history()
{ {
History * htmp; History * htmp;
if (history == 0) if (history == 0)
{ {
return; return;
} }
htmp = new History(oid, htmp = new History(oid,
history->seq + 1, history->seq + 1,
history->hid, history->hid,
history->hostname, history->hostname,
history->vm_dir, history->vm_dir,
history->vmm_mad_name, history->vmm_mad_name,
history->tm_mad_name); history->tm_mad_name);
if ( previous_history != 0 ) if ( previous_history != 0 )
{ {
delete previous_history; delete previous_history;
} }
previous_history = history; previous_history = history;
history = htmp; history = htmp;
} }
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
@ -564,26 +493,26 @@ void VirtualMachine::cp_history()
void VirtualMachine::cp_previous_history() void VirtualMachine::cp_previous_history()
{ {
History * htmp; History * htmp;
if ( previous_history == 0 || history == 0) if ( previous_history == 0 || history == 0)
{ {
return; return;
} }
htmp = new History(oid, htmp = new History(oid,
history->seq + 1, history->seq + 1,
previous_history->hid, previous_history->hid,
previous_history->hostname, previous_history->hostname,
previous_history->vm_dir, previous_history->vm_dir,
previous_history->vmm_mad_name, previous_history->vmm_mad_name,
previous_history->tm_mad_name); previous_history->tm_mad_name);
delete previous_history; delete previous_history;
previous_history = history; previous_history = history;
history = htmp; history = htmp;
} }
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
@ -667,10 +596,10 @@ int VirtualMachine::get_network_leases()
return -1; return -1;
} }
if ( vn->get_uid() != uid && vn->get_uid() != 0 && uid != 0) if ( vn->get_uid() != uid && vn->get_uid() != 0 && uid != 0)
{ {
ostringstream ose; ostringstream ose;
ose << "Owner " << uid << " of the VM doesn't have ownership of Virtual Network " ose << "Owner " << uid << " of the VM doesn't have ownership of Virtual Network "
<< vn->get_uid(); << vn->get_uid();
Nebula::log("VMM", Log::ERROR, ose); Nebula::log("VMM", Log::ERROR, ose);
return -1; return -1;
@ -707,7 +636,7 @@ int VirtualMachine::get_network_leases()
if ( !model.empty() ) if ( !model.empty() )
{ {
new_nic.insert(make_pair("MODEL",model)); new_nic.insert(make_pair("MODEL",model));
} }
nic->replace(new_nic); nic->replace(new_nic);
@ -738,7 +667,7 @@ void VirtualMachine::release_network_leases()
for(int i=0; i<num_nics; i++) for(int i=0; i<num_nics; i++)
{ {
VectorAttribute const * nic = VectorAttribute const * nic =
dynamic_cast<VectorAttribute const * >(nics[i]); dynamic_cast<VectorAttribute const * >(nics[i]);
if ( nic == 0 ) if ( nic == 0 )
@ -838,17 +767,17 @@ int VirtualMachine::parse_template_attribute(const string& attribute,
{ {
int rc; int rc;
char * err = 0; char * err = 0;
rc = parse_attribute(this,-1,attribute,parsed,&err); rc = parse_attribute(this,-1,attribute,parsed,&err);
if ( rc != 0 && err != 0 ) if ( rc != 0 && err != 0 )
{ {
ostringstream oss; ostringstream oss;
oss << "Error parsing: " << attribute << ". " << err; oss << "Error parsing: " << attribute << ". " << err;
log("VM",Log::ERROR,oss); log("VM",Log::ERROR,oss);
} }
return rc; return rc;
} }
@ -858,19 +787,19 @@ int VirtualMachine::parse_template_attribute(const string& attribute,
pthread_mutex_t VirtualMachine::lex_mutex = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_t VirtualMachine::lex_mutex = PTHREAD_MUTEX_INITIALIZER;
extern "C" extern "C"
{ {
typedef struct yy_buffer_state * YY_BUFFER_STATE; typedef struct yy_buffer_state * YY_BUFFER_STATE;
int vm_var_parse (VirtualMachine * vm, int vm_var_parse (VirtualMachine * vm,
int vm_id, int vm_id,
ostringstream * parsed, ostringstream * parsed,
char ** errmsg); char ** errmsg);
int vm_var_lex_destroy(); int vm_var_lex_destroy();
YY_BUFFER_STATE vm_var__scan_string(const char * str); YY_BUFFER_STATE vm_var__scan_string(const char * str);
void vm_var__delete_buffer(YY_BUFFER_STATE); void vm_var__delete_buffer(YY_BUFFER_STATE);
} }
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
@ -922,9 +851,9 @@ error_yy:
ostream& operator<<(ostream& os, const VirtualMachine& vm) ostream& operator<<(ostream& os, const VirtualMachine& vm)
{ {
string vm_str; string vm_str;
os << vm.to_xml(vm_str); os << vm.to_xml(vm_str);
return os; return os;
}; };
@ -934,9 +863,9 @@ string& VirtualMachine::to_xml(string& xml) const
{ {
string template_xml; string template_xml;
string history_xml; string history_xml;
ostringstream oss; ostringstream oss;
oss << "<VM>" oss << "<VM>"
<< "<ID>" << oid << "</ID>" << "<ID>" << oid << "</ID>"
<< "<UID>" << uid << "</UID>" << "<UID>" << uid << "</UID>"
@ -952,15 +881,15 @@ string& VirtualMachine::to_xml(string& xml) const
<< "<NET_TX>" << net_tx << "</NET_TX>" << "<NET_TX>" << net_tx << "</NET_TX>"
<< "<NET_RX>" << net_rx << "</NET_RX>" << "<NET_RX>" << net_rx << "</NET_RX>"
<< vm_template.to_xml(template_xml); << vm_template.to_xml(template_xml);
if ( hasHistory() ) if ( hasHistory() )
{ {
oss << history->to_xml(history_xml); oss << history->to_xml(history_xml);
} }
oss << "</VM>"; oss << "</VM>";
xml = oss.str(); xml = oss.str();
return xml; return xml;
} }
@ -970,9 +899,9 @@ string& VirtualMachine::to_str(string& str) const
{ {
string template_str; string template_str;
string history_str; string history_str;
ostringstream oss; ostringstream oss;
oss<< "ID : " << oid << endl oss<< "ID : " << oid << endl
<< "UID : " << uid << endl << "UID : " << uid << endl
<< "NAME : " << name << endl << "NAME : " << name << endl
@ -987,14 +916,14 @@ string& VirtualMachine::to_str(string& str) const
<< "NET TX : " << net_tx << endl << "NET TX : " << net_tx << endl
<< "NET RX : " << net_rx << endl << "NET RX : " << net_rx << endl
<< "Template" << endl << vm_template.to_str(template_str) << endl; << "Template" << endl << vm_template.to_str(template_str) << endl;
if ( hasHistory() ) if ( hasHistory() )
{ {
oss << "Last History Record" << endl << history->to_str(history_str); oss << "Last History Record" << endl << history->to_str(history_str);
} }
str = oss.str(); str = oss.str();
return str; return str;
} }

View File

@ -22,7 +22,7 @@
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
VirtualMachinePool::VirtualMachinePool(SqliteDB * db, VirtualMachinePool::VirtualMachinePool(SqlDB * db,
vector<const Attribute *> hook_mads) vector<const Attribute *> hook_mads)
: PoolSQL(db,VirtualMachine::table) : PoolSQL(db,VirtualMachine::table)
{ {
@ -199,7 +199,7 @@ int VirtualMachinePool::allocate (
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
// Insert the Object in the pool // Insert the Object in the pool
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
*oid = PoolSQL::allocate(vm); *oid = PoolSQL::allocate(vm);
if ( *oid == -1 ) if ( *oid == -1 )
@ -210,7 +210,7 @@ int VirtualMachinePool::allocate (
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
// Insert parsed context in the VM template and clean-up // Insert parsed context in the VM template and clean-up
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
if ((num_attr = (int) attrs.size()) > 0) if ((num_attr = (int) attrs.size()) > 0)
{ {
generate_context(*oid,attrs[0]); generate_context(*oid,attrs[0]);
@ -331,4 +331,50 @@ void VirtualMachinePool::generate_context(int vm_id, Attribute * attr)
vm->unlock(); vm->unlock();
} }
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int VirtualMachinePool::dump_cb(void * _oss,int num,char **values,char **names)
{
ostringstream * oss;
oss = static_cast<ostringstream *>(_oss);
return VirtualMachine::dump(*oss, num, values, names);
}
/* -------------------------------------------------------------------------- */
int VirtualMachinePool::dump(ostringstream& oss, const string& where)
{
int rc;
ostringstream cmd;
oss << "<VM_POOL>";
set_callback(
static_cast<Callbackable::Callback>(&VirtualMachinePool::dump_cb),
static_cast<void *>(&oss));
cmd << "SELECT " << VirtualMachine::table << ".*, "
<< "user_pool.user_name, " << History::table << ".* FROM "
<< VirtualMachine::table
<< " LEFT OUTER JOIN (SELECT *,MAX(seq) FROM "
<< History::table << " GROUP BY vid) AS " << History::table
<< " ON " << VirtualMachine::table << ".oid = "
<< History::table << ".vid LEFT OUTER JOIN (SELECT oid,user_name FROM "
<< "user_pool) AS user_pool ON "
<< VirtualMachine::table << ".uid = user_pool.oid WHERE "
<< VirtualMachine::table << ".state != " << VirtualMachine::DONE;
if ( !where.empty() )
{
cmd << " AND " << where;
}
rc = db->exec(cmd,this);
oss << "</VM_POOL>";
return rc;
}