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

Merge branch 'feature-407'

Conflicts:
	src/host/ClusterPool.cc
This commit is contained in:
Ruben S. Montero 2011-03-29 00:01:00 +02:00
commit 35de55685d
115 changed files with 3867 additions and 4085 deletions

View File

@ -57,6 +57,7 @@ main_env.Append(LIBPATH=[
cwd+'/src/log',
cwd+'/src/sql',
cwd+'/src/host',
cwd+'/src/cluster',
cwd+'/src/mad',
cwd+'/src/nebula',
cwd+'/src/pool',
@ -73,6 +74,7 @@ main_env.Append(LIBPATH=[
cwd+'/src/hm',
cwd+'/src/um',
cwd+'/src/authm',
cwd+'/src/xml',
])
# Compile flags
@ -171,6 +173,7 @@ build_scripts=[
'src/common/SConstruct',
'src/template/SConstruct',
'src/host/SConstruct',
'src/cluster/SConstruct',
'src/mad/SConstruct',
'src/nebula/SConstruct',
'src/pool/SConstruct',
@ -187,6 +190,7 @@ build_scripts=[
'src/hm/SConstruct',
'src/um/SConstruct',
'src/authm/SConstruct',
'src/xml/SConstruct',
]
# Testing
@ -214,6 +218,7 @@ if testing=='yes':
'src/authm/test/SConstruct',
'src/common/test/SConstruct',
'src/host/test/SConstruct',
'src/cluster/test/SConstruct',
'src/image/test/SConstruct',
'src/lcm/test/SConstruct',
'src/pool/test/SConstruct',
@ -222,6 +227,7 @@ if testing=='yes':
'src/um/test/SConstruct',
'src/vm/test/SConstruct',
'src/vnm/test/SConstruct',
'src/xml/test/SConstruct',
])
else:
main_env.Append(testing='no')

132
include/Cluster.h Normal file
View File

@ -0,0 +1,132 @@
/* ------------------------------------------------------------------------ */
/* Copyright 2002-2010, OpenNebula Project Leads (OpenNebula.org) */
/* */
/* Licensed under the Apache License, Version 2.0 (the "License"); you may */
/* not use this file except in compliance with the License. You may obtain */
/* a copy of the License at */
/* */
/* http://www.apache.org/licenses/LICENSE-2.0 */
/* */
/* Unless required by applicable law or agreed to in writing, software */
/* distributed under the License is distributed on an "AS IS" BASIS, */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
/* See the License for the specific language governing permissions and */
/* limitations under the License. */
/* -------------------------------------------------------------------------*/
#ifndef CLUSTER_H_
#define CLUSTER_H_
#include "PoolSQL.h"
#include "Host.h"
using namespace std;
/**
* The Cluster class.
*/
class Cluster : public PoolObjectSQL
{
public:
/**
* Adds the host to this cluster
* @param host The host to add
*
* @return 0 on success
*/
/*
int add_host(Host * host)
{
return host->set_cluster(name);
};
*/
/**
* Removes the host from this cluster
* @param host The host to add
*
* @return 0 on success
*/
// May be needed in a future
// int remove_host(Host * host);
/**
* Function to write a Cluster on an output stream
*/
friend ostream& operator<<(ostream& os, Cluster& cluster);
/**
* Function to print the Cluster object into a string in XML format
* @param xml the resulting XML string
* @return a reference to the generated string
*/
string& to_xml(string& xml) const;
/**
* Rebuilds the object from an xml formatted string
* @param xml_str The xml-formatted string
*
* @return 0 on success, -1 otherwise
*/
int from_xml(const string &xml_str);
private:
// -------------------------------------------------------------------------
// Friends
// -------------------------------------------------------------------------
friend class ClusterPool;
// *************************************************************************
// Constructor
// *************************************************************************
Cluster(int id, const string& name);
virtual ~Cluster();
// *************************************************************************
// DataBase implementation (Private)
// *************************************************************************
static const char * db_names;
static const char * db_bootstrap;
static const char * table;
/**
* Execute an INSERT or REPLACE Sql query.
* @param db The SQL DB
* @param replace Execute an INSERT or a REPLACE
* @return 0 one success
*/
int insert_replace(SqlDB *db, bool replace);
/**
* Bootstraps the database table(s) associated to the Cluster
*/
static void bootstrap(SqlDB * db)
{
ostringstream oss_cluster(Cluster::db_bootstrap);
db->exec(oss_cluster);
};
/**
* Writes the Cluster in the database.
* @param db pointer to the db
* @return 0 on success
*/
int insert(SqlDB *db, string& error_str);
/**
* Writes/updates the Cluster's data fields in the database.
* @param db pointer to the db
* @return 0 on success
*/
int update(SqlDB *db);
};
#endif /*CLUSTER_H_*/

View File

@ -17,124 +17,123 @@
#ifndef CLUSTER_POOL_H_
#define CLUSTER_POOL_H_
#include <string>
#include <sstream>
#include <map>
#include "Cluster.h"
#include "Host.h"
#include "SqlDB.h"
using namespace std;
/**
* A cluster helper class. It is not a normal PoolSQL,
* but a series of static methods related to clusters.
*/
class ClusterPool
class ClusterPool : public PoolSQL
{
public:
ClusterPool(SqlDB * db);
~ClusterPool(){};
/**
* Removes the host from the given cluster setting the default one.
* @param host The host to assign
*
* @return 0 on success
*/
int set_default_cluster(Host * host)
{
return host->set_cluster(ClusterPool::DEFAULT_CLUSTER_NAME);
};
/**
* Cluster name for the default cluster
*/
static const string DEFAULT_CLUSTER_NAME;
private:
// -------------------------------------------------------------------------
// Friends
// -------------------------------------------------------------------------
friend class HostPool;
/* ---------------------------------------------------------------------- */
/* Attributes */
/* Methods for DB management */
/* ---------------------------------------------------------------------- */
/**
* This map stores the clusters
*/
map<int, string> cluster_names;
/* ---------------------------------------------------------------------- */
/* Methods for cluster management */
/* ---------------------------------------------------------------------- */
/**
* Returns true if the clid is an id for an existing cluster
* @param clid ID of the cluster
*
* @return true if the clid is an id for an existing cluster
*/
bool exists(int clid)
{
return cluster_names.count(clid) > 0;
};
/**
* Allocates a new cluster in the pool
* @param clid the id assigned to the cluster
* @return the id assigned to the cluster or -1 in case of failure
*/
int allocate(int * clid, string name, SqlDB *db, string& error_str);
int allocate(int * oid, string name, string& error_str);
/**
* Returns the xml representation of the given cluster
* @param clid ID of the cluster
*
* @return the xml representation of the given cluster
* Function to get a Cluster from the pool, if the object is not in memory
* it is loaded from the DB
* @param oid Cluster unique id
* @param lock locks the Cluster mutex
* @return a pointer to the Cluster, 0 if the Cluster could not be loaded
*/
string info(int clid);
/**
* Removes the given cluster from the pool and the DB
* @param clid ID of the cluster
*
* @return 0 on success
*/
int drop(int clid, SqlDB *db);
/**
* Dumps the cluster pool in XML format.
* @param oss the output stream to dump the pool contents
*
* @return 0 on success
*/
int dump(ostringstream& oss);
/**
* Bootstraps the database table(s) associated to the Cluster
*/
static void bootstrap(SqlDB * db)
Cluster * get(int oid, bool lock)
{
ostringstream oss(ClusterPool::db_bootstrap);
db->exec(oss);
return static_cast<Cluster *>(PoolSQL::get(oid,lock));
};
/**
* Function to insert new Cluster in the pool
* @param oid the id assigned to the Cluster
* @param name the Cluster's name
* @return 0 on success, -1 in case of failure
*/
int insert (int oid, string name, SqlDB *db);
/**
* Formats as XML the given id and name.
* @param oss the output stream to dump the pool contents
* Gets an object from the pool (if needed the object is loaded from the
* database).
* @param name of the object
* @param lock locks the object if true
*
* @return a pointer to the object, 0 in case of failure
*/
Cluster * get(const string& name, bool lock)
{
return static_cast<Cluster *>(PoolSQL::get(name,-1,lock));
};
/** Update a particular Cluster
* @param user pointer to Cluster
* @return 0 on success
*/
void dump_cluster(ostringstream& oss, int id, string name);
int update(Cluster * cluster)
{
return cluster->update(db);
};
/**
* Drops the Cluster from the data base. The object mutex SHOULD be
* locked.
* @param cluster a pointer to the object
* @return 0 on success.
*/
int drop(Cluster * cluster);
/* ---------------------------------------------------------------------- */
/* DB manipulation */
/* ---------------------------------------------------------------------- */
/**
* Bootstraps the database table(s) associated to the Cluster pool
*/
static void bootstrap(SqlDB * _db)
{
Cluster::bootstrap(_db);
};
static const char * db_names;
/**
* Dumps the Cluster pool in XML format. A filter can be also added to the
* query
* @param oss the output stream to dump the pool contents
* @param where filter for the objects, defaults to all
*
* @return 0 on success
*/
int dump(ostringstream& oss, const string& where)
{
return PoolSQL::dump(oss, "CLUSTER_POOL", Cluster::table, where);
};
static const char * db_bootstrap;
static const char * table;
private:
/**
* Factory method to produce objects
* @return a pointer to the new object
*/
PoolObjectSQL * create()
{
return new Cluster(-1,"");
};
};
#endif /*CLUSTER_POOL_H_*/

View File

@ -150,6 +150,13 @@ private:
* @return 0 if success
*/
int unset(const string& ip);
/**
* Updates the DB entry for this lease
* @param lease Lease to be updated
* @return 0 if success
*/
int update_lease(Lease * lease);
};
#endif /*FIXED_LEASES_H_*/

View File

@ -25,7 +25,7 @@ using namespace std;
* The History class, it represents an execution record of a Virtual Machine.
*/
class History:public ObjectSQL
class History:public ObjectSQL, public ObjectXML
{
public:
enum MigrationReason
@ -40,13 +40,13 @@ public:
History(int oid, int _seq = -1);
History(
int oid,
int seq,
int hid,
string& hostname,
string& vm_dir,
string& vmm,
string& tm);
int oid,
int seq,
int hid,
const string& hostname,
const string& vm_dir,
const string& vmm,
const string& tm);
~History(){};
@ -55,14 +55,6 @@ public:
*/
friend ostream& operator<<(ostream& os, const History& history);
/**
* Function to print the History object into a string in
* plain text
* @param str the resulting string
* @return a reference to the generated string
*/
string& to_str(string& str) const;
/**
* Function to print the History object into a string in
* XML format
@ -78,52 +70,15 @@ private:
// ----------------------------------------
// DataBase implementation variables
// ----------------------------------------
enum ColNames
{
VID = 0,
SEQ = 1,
HOSTNAME = 2,
VM_DIR = 3,
HID = 4,
VMMMAD = 5,
TMMAD = 6,
STIME = 7,
ETIME = 8,
PROLOG_STIME = 9,
PROLOG_ETIME = 10,
RUNNING_STIME = 11,
RUNNING_ETIME = 12,
EPILOG_STIME = 13,
EPILOG_ETIME = 14,
REASON = 15,
LIMIT = 16
};
static const char * table;
static const char * db_names;
static const char * extended_db_names;
static const char * db_bootstrap;
void non_persistent_data();
static string column_name(const ColNames column)
{
switch (column)
{
case HID:
return "hid";
case ETIME:
return "etime";
case RUNNING_ETIME:
return "retime";
default:
return "";
}
}
// ----------------------------------------
// History fields
// ----------------------------------------
@ -167,7 +122,12 @@ private:
* @param db pointer to the database.
* @return 0 on success.
*/
int insert(SqlDB * db, string& error_str);
int insert(SqlDB * db, string& error_str)
{
error_str.clear();
return insert_replace(db, false);
}
/**
* Reads the history record from the DB
@ -181,7 +141,10 @@ private:
* @param db pointer to the database.
* @return 0 on success.
*/
int update(SqlDB * db);
int update(SqlDB * db)
{
return insert_replace(db, true);
}
/**
* Removes the all history records from the DB
@ -208,15 +171,35 @@ private:
int select_cb(void *nil, int num, char **values, char **names);
/**
* Function to unmarshall a History 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
* Rebuilds the object from an xml node
* @param node The xml node pointer
*
* @return 0 on success, -1 otherwise
*/
static int dump(ostringstream& oss, int num, char **names, char **values);
int from_xml_node(const xmlNodePtr node)
{
ObjectXML::update_from_node(node);
return rebuild_attributes();
}
/**
* Rebuilds the object from an xml formatted string
* @param xml_str The xml-formatted string
*
* @return 0 on success, -1 otherwise
*/
int from_xml(const string &xml_str)
{
ObjectXML::update_from_str(xml_str);
return rebuild_attributes();
}
/**
* Rebuilds the internal attributes using xpath
*/
int rebuild_attributes();
};
#endif /*HISTORY_H_*/

View File

@ -20,7 +20,6 @@
#include "PoolSQL.h"
#include "HostTemplate.h"
#include "HostShare.h"
#include "ClusterPool.h"
using namespace std;
@ -49,28 +48,20 @@ public:
*/
friend ostream& operator<<(ostream& os, Host& h);
/**
* Function to print the Host object into a string in plain text
* @param str the resulting string
* @return a reference to the generated string
*/
string& to_str(string& str) const;
/**
* Function to print the Host object into a string in XML format
* @param xml the resulting XML string
* @return a reference to the generated string
*/
string& to_xml(string& xml) const;
/**
* Function to print the Host object into a string in XML format
* @param xml the resulting XML string
* @return a reference to the generated string
*/
string& to_xml(string& xml) const;
/**
* Get the Host unique identifier HID, that matches the OID of the object
* @return HID Host identifier
* Rebuilds the object from an xml formatted string
* @param xml_str The xml-formatted string
*
* @return 0 on success, -1 otherwise
*/
int get_hid() const
{
return oid;
};
int from_xml(const string &xml_str);
/**
* Check if the host is enabled
@ -120,15 +111,6 @@ public:
state = INIT;
};
/**
* Returns host host_name
* @return host_name Host's hostname
*/
const string& get_hostname() const
{
return hostname;
};
/** Update host counters and update the whole host on the DB
* @param parse_str string with values to be parsed
* @return 0 on success
@ -378,9 +360,6 @@ private:
// -------------------------------------------------------------------------
// Host Description
// -------------------------------------------------------------------------
string hostname;
/**
* The state of the Host
*/
@ -402,7 +381,7 @@ private:
string tm_mad_name;
/**
* If Host State = MONITORED last time it got fully monitored or 1 Jan 1970
* If Host State= MONITORED last time it got fully monitored or 1 Jan 1970
* Host State = MONITORING last time it got a signal to be monitored
*/
time_t last_monitored;
@ -426,10 +405,29 @@ private:
*/
HostShare host_share;
// *************************************************************************
// Constructor
// *************************************************************************
Host(int id=-1,
const string& hostname="",
const string& im_mad_name="",
const string& vmm_mad_name="",
const string& tm_mad_name="",
const string& cluster="");
virtual ~Host();
// *************************************************************************
// DataBase implementation (Private)
// *************************************************************************
static const char * db_names;
static const char * db_bootstrap;
static const char * table;
/**
* Execute an INSERT or REPLACE Sql query.
* @param db The SQL DB
@ -438,102 +436,29 @@ private:
*/
int insert_replace(SqlDB *db, bool replace);
/**
* Callback function to unmarshall a Host object (Host::select)
* @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 select_cb(void *nil, int num, char **values, char **names);
/**
* Bootstraps the database table(s) associated to the Host
*/
static void bootstrap(SqlDB * db)
{
ostringstream oss_host(Host::db_bootstrap);
ostringstream oss_share(HostShare::db_bootstrap);
db->exec(oss_host);
db->exec(oss_share);
};
protected:
// *************************************************************************
// Constructor
// *************************************************************************
Host(int id=-1,
string _hostname="",
string _im_mad_name="",
string _vmm_mad_name="",
string _tm_mad_name="");
virtual ~Host();
// *************************************************************************
// DataBase implementation
// *************************************************************************
enum ColNames
{
OID = 0,
HOST_NAME = 1,
STATE = 2,
IM_MAD = 3,
VM_MAD = 4,
TM_MAD = 5,
LAST_MON_TIME = 6,
CLUSTER = 7,
TEMPLATE = 8,
LIMIT = 9
};
static const char * db_names;
static const char * db_bootstrap;
static const char * table;
/**
* Reads the Host (identified with its OID=HID) from the database.
* @param db pointer to the db
* @return 0 on success
*/
virtual int select(SqlDB *db);
/**
* Writes the Host and its associated HostShares in the database.
* @param db pointer to the db
* @return 0 on success
*/
virtual int insert(SqlDB *db, string& error_str);
int insert(SqlDB *db, string& error_str);
/**
* Writes/updates the Hosts data fields in the database.
* @param db pointer to the db
* @return 0 on success
*/
virtual int update(SqlDB *db);
/**
* Drops host from the database
* @param db pointer to the db
* @return 0 on success
*/
virtual int drop(SqlDB *db);
/**
* Function to output a Host object in to an stream in 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 dump(ostringstream& oss, int num, char **values, char **names);
int update(SqlDB *db);
};
#endif /*HOST_H_*/

View File

@ -19,7 +19,6 @@
#include "PoolSQL.h"
#include "Host.h"
#include "ClusterPool.h"
#include <time.h>
#include <sstream>
@ -69,14 +68,24 @@ public:
return static_cast<Host *>(PoolSQL::get(oid,lock));
};
/**
* Function to get a Host from the pool, if the object is not in memory
* it is loaded from the DB
* @param hostname
* @param lock locks the Host mutex
* @return a pointer to the Host, 0 if the Host could not be loaded
*/
Host * get(string name, bool lock)
{
return static_cast<Host *>(PoolSQL::get(name,-1,lock));
};
/**
* Bootstraps the database table(s) associated to the Host pool
*/
static void bootstrap(SqlDB *_db)
{
Host::bootstrap(_db);
ClusterPool::bootstrap(_db);
};
/**
@ -138,102 +147,25 @@ public:
*
* @return 0 on success
*/
int dump(ostringstream& oss, const string& where);
/* ---------------------------------------------------------------------- */
/* ---------------------------------------------------------------------- */
/* Methods for cluster management */
/* ---------------------------------------------------------------------- */
/* ---------------------------------------------------------------------- */
/**
* Returns true if the clid is an id for an existing cluster
* @param clid ID of the cluster
*
* @return true if the clid is an id for an existing cluster
*/
/* bool exists_cluster(int clid)
int dump(ostringstream& oss, const string& where)
{
return cluster_pool.exists(clid);
};*/
/**
* Allocates a new cluster in the pool
* @param clid the id assigned to the cluster
* @return the id assigned to the cluster or -1 in case of failure
*/
int allocate_cluster(int * clid, const string& name, string& error_str)
{
return cluster_pool.allocate(clid, name, db, error_str);
return PoolSQL::dump(oss, "HOST_POOL", Host::table, where);
};
/**
* Returns the xml representation of the given cluster
* @param clid ID of the cluster
* Finds a set objects that satisfies a given condition
* @param oids a vector with the oids of the objects.
* @param the name of the DB table.
* @param where condition in SQL format.
*
* @return the xml representation of the given cluster
* @return 0 on success
*/
string info_cluster(int clid)
int search(vector<int>& oids, const string& where)
{
return cluster_pool.info(clid);
};
/**
* Removes the given cluster from the pool and the DB
* @param clid ID of the cluster
*
* @return 0 on success
*/
int drop_cluster(int clid);
/**
* Dumps the cluster pool in XML format.
* @param oss the output stream to dump the pool contents
*
* @return 0 on success
*/
int dump_cluster(ostringstream& oss)
{
return cluster_pool.dump(oss);
};
/**
* Assigns the host to the given cluster
* @param host The host to assign
* @param clid ID of the cluster
*
* @return 0 on success
*/
int set_cluster(Host* host, int clid)
{
map<int, string>::iterator it;
it = cluster_pool.cluster_names.find(clid);
if (it == cluster_pool.cluster_names.end())
{
return -1;
}
return host->set_cluster( it->second );
};
/**
* Removes the host from the given cluster setting the default one.
* @param host The host to assign
*
* @return 0 on success
*/
int set_default_cluster(Host* host)
{
return host->set_cluster(ClusterPool::DEFAULT_CLUSTER_NAME);
return PoolSQL::search(oids, Host::table, where);
};
private:
/**
* ClusterPool, clusters defined and persistance functionality
*/
ClusterPool cluster_pool;
/**
* Factory method to produce Host objects
@ -244,15 +176,6 @@ private:
return new Host;
};
/**
* Callback function to build the cluster pool
* @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 init_cb(void *nil, int num, char **values, char **names);
/**
* Callback function to get the IDs of the hosts to be monitored
* (Host::discover)
@ -262,16 +185,6 @@ private:
* @return 0 on success
*/
int discover_cb(void * _map, int num, char **values, char **names);
/**
* Callback function to get output the host pool in XML format
* (Host::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 /*HOST_POOL_H_*/

View File

@ -17,8 +17,7 @@
#ifndef HOST_SHARE_H_
#define HOST_SHARE_H_
#include "SqlDB.h"
#include "ObjectSQL.h"
#include "ObjectXML.h"
#include <time.h>
using namespace std;
@ -29,12 +28,11 @@ using namespace std;
/**
* The HostShare class. It represents a logical partition of a host...
*/
class HostShare : public ObjectSQL
class HostShare : public ObjectXML
{
public:
HostShare(
int _hsid=-1,
int _max_disk=0,
int _max_mem=0,
int _max_cpu=0);
@ -92,14 +90,6 @@ public:
*/
friend ostream& operator<<(ostream& os, HostShare& hs);
/**
* Function to print the HostShare object into a string in
* plain text
* @param str the resulting string
* @return a reference to the generated string
*/
string& to_str(string& str) const;
/**
* Function to print the HostShare object into a string in
* XML format
@ -110,8 +100,6 @@ public:
private:
int hsid; /**< HostShare identifier */
int disk_usage; /**< Disk allocated to VMs (in Mb). */
int mem_usage; /**< Memory allocated to VMs (in Mb) */
int cpu_usage; /**< CPU allocated to VMs (in percentage) */
@ -128,7 +116,7 @@ private:
int used_mem; /**< Used memory from the IM monitor */
int used_cpu; /**< Used cpu from the IM monitor */
int running_vms; /**< Number of running VMs in this Host */
int running_vms;/**< Number of running VMs in this Host */
// ----------------------------------------
// Friends
@ -137,95 +125,13 @@ private:
friend class Host;
friend class HostPool;
// ----------------------------------------
// DataBase implementation variables
// ----------------------------------------
enum ColNames
{
HID = 0,
DISK_USAGE = 1,
MEM_USAGE = 2,
CPU_USAGE = 3,
MAX_DISK = 4,
MAX_MEMORY = 5,
MAX_CPU = 6,
FREE_DISK = 7,
FREE_MEMORY = 8,
FREE_CPU = 9,
USED_DISK = 10,
USED_MEMORY = 11,
USED_CPU = 12,
RUNNING_VMS = 13,
LIMIT = 14
};
static const char * table;
static const char * db_names;
static const char * db_bootstrap;
// ----------------------------------------
// Database methods
// ----------------------------------------
/**
* Reads the HostShare (identified with its HSID) from the database.
* @param db pointer to the db
* @return 0 on success
* Rebuilds the object from an xml node
* @param node The xml node pointer
*
* @return 0 on success, -1 otherwise
*/
int select(SqlDB * db);
/**
* Writes the HostShare in the database.
* @param db pointer to the db
* @return 0 on success
*/
int insert(SqlDB * db, string& error_str);
/**
* Writes/updates the HostShare data fields in the database.
* @param db pointer to the db
* @return 0 on success
*/
int update(SqlDB * db);
/**
* Drops hostshare from the database
* @param db pointer to the db
* @return 0 on success
*/
int drop(SqlDB * db);
/**
* Execute an INSERT or REPLACE Sql query.
* @param db The SQL DB
* @param replace Execute an INSERT or a REPLACE
* @return 0 one success
*/
int insert_replace(SqlDB *db, bool replace);
/**
* Callback function to unmarshall a HostShare object (HostShare::select)
* @param num the number of columns read from the DB
* @para names the column names
* @para vaues the column values
* @return 0 on success
*/
int select_cb(void * nil, int num, char **values, char **names);
/**
* Function to unmarshall a HostShare object in to an output stream in XML
* @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 dump(ostringstream& oss, int num, char **values, char **names);
int from_xml_node(const xmlNodePtr node);
};
#endif /*HOST_SHARE_H_*/

View File

@ -59,13 +59,6 @@ public:
// Image Public Methods
// *************************************************************************
/**
* Function to print the Image object into a string in plain text
* @param str the resulting string
* @return a reference to the generated string
*/
string& to_str(string& str) const;
/**
* Function to print the Image object into a string in XML format
* @param xml the resulting XML string
@ -74,31 +67,12 @@ public:
string& to_xml(string& xml) const;
/**
* Get the Image unique identifier IID, that matches the OID of the object
* @return IID Image identifier
* Rebuilds the object from an xml formatted string
* @param xml_str The xml-formatted string
*
* @return 0 on success, -1 otherwise
*/
int get_iid() const
{
return oid;
};
/**
* Gets the uid of the owner of the Image
* @return uid
**/
int get_uid()
{
return uid;
}
/**
* Returns Image's name
* @return name Image's name
*/
const string& get_name() const
{
return name;
};
int from_xml(const string &xml_str);
/**
* Returns true if the image is public
@ -356,34 +330,29 @@ private:
// -------------------------------------------------------------------------
/**
* Owner if the image
* Image owner's name
*/
int uid;
/**
* The name of the Image
*/
string name;
string user_name;
/**
* Type of the Image
*/
ImageType type;
ImageType type;
/**
* Public scope of the Image
*/
int public_img;
int public_img;
/**
* Persistency of the Image
*/
int persistent_img;
int persistent_img;
/**
* Registration time
*/
time_t regtime;
time_t regtime;
/**
* Path to the image
@ -422,15 +391,6 @@ private:
*/
int insert_replace(SqlDB *db, bool replace);
/**
* Callback function to unmarshall a Image object (Image::select)
* @param num the number of columns read from the DB
* @param names the column names
* @param values the column values
* @return 0 on success
*/
int select_cb(void *nil, int num, char **values, char **names);
/**
* Bootstraps the database table(s) associated to the Image
*/
@ -455,7 +415,9 @@ protected:
// Constructor
// *************************************************************************
Image(int uid=-1, ImageTemplate *img_template = 0);
Image(int uid,
const string& user_name,
ImageTemplate* img_template);
virtual ~Image();
@ -463,37 +425,12 @@ protected:
// DataBase implementation
// *************************************************************************
enum ColNames
{
OID = 0, /* Image identifier (IID) */
UID = 1, /* Image owner id */
NAME = 2, /* Image name */
TYPE = 3, /* 0) OS 1) CDROM 2) DATABLOCK */
PUBLIC = 4, /* Public scope (YES OR NO) */
PERSISTENT = 5, /* Peristency (YES OR NO) */
REGTIME = 6, /* Time of registration */
SOURCE = 7, /* Path to the image */
STATE = 8, /* 0) INIT 1) ALLOCATED */
RUNNING_VMS = 9, /* Number of VMs using the img */
TEMPLATE = 10, /* Image template xml data */
LIMIT = 11
};
static const char * extended_db_names;
static const char * db_names;
static const char * db_bootstrap;
static const char * table;
/**
* Reads the Image (identified with its OID=IID) from the database.
* @param db pointer to the db
* @return 0 on success
*/
virtual int select(SqlDB *db);
/**
* Writes the Image in the database.
* @param db pointer to the db
@ -507,23 +444,6 @@ protected:
* @return 0 on success
*/
virtual int update(SqlDB *db);
/**
* Drops Image and associated template from the database
* @param db pointer to the db
* @return 0 on success
*/
virtual int drop(SqlDB *db);
/**
* Function to output an Image object in to an stream in 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 dump(ostringstream& oss, int num, char **values, char **names);
};
#endif /*IMAGE_H_*/

View File

@ -38,10 +38,10 @@ class ImagePool : public PoolSQL
{
public:
ImagePool(SqlDB * db,
const string& _source_prefix,
const string& _default_type,
const string& _default_dev_prefix);
ImagePool(SqlDB * db,
const string& _source_prefix,
const string& _default_type,
const string& _default_dev_prefix);
~ImagePool(){};
@ -56,20 +56,19 @@ public:
*/
int allocate (
int uid,
string user_name,
ImageTemplate * img_template,
int * oid,
string& error_str);
/**
* Function to get a Image from the pool, if the object is not in memory
** Function to get a Image from the pool, if the object is not in memory
* it is loaded from the DB
* @param oid Image unique id
* @param lock locks the Image mutex
* @return a pointer to the Image, 0 if the Image could not be loaded
*/
Image * get(
int oid,
bool lock)
Image * get(int oid, bool lock)
{
return static_cast<Image *>(PoolSQL::get(oid,lock));
};
@ -78,25 +77,15 @@ public:
* Function to get an Image from the pool using the image name
* @param name of the image
* @param lock locks the User mutex
* @return a pointer to the Image, 0 if the User could not be loaded
* @return a pointer to the Image, 0 if the image could not be loaded
*/
Image * get(
const string& name,
bool lock)
Image * get(const string& name, int uid, bool lock)
{
map<string, int>::iterator index;
index = image_names.find(name);
if ( index != image_names.end() )
{
return get((int)index->second,lock);
}
return 0;
return static_cast<Image *>(PoolSQL::get(name,uid,lock));
}
/** Update a particular Image
/**
* Update a particular Image
* @param image pointer to Image
* @return 0 on success
*/
@ -111,14 +100,7 @@ public:
*/
int drop(Image * image)
{
int rc = PoolSQL::drop(image);
if ( rc == 0)
{
image_names.erase(image->get_name());
}
return rc;
return PoolSQL::drop(image);
};
/**
@ -136,7 +118,10 @@ public:
* @param where filter for the objects, defaults to all
* @return 0 on success
*/
int dump(ostringstream& oss, const string& where);
int dump(ostringstream& oss, const string& where)
{
return PoolSQL::dump(oss, "IMAGE_POOL", Image::table, where);
}
/**
* Generates a DISK attribute for VM templates using the Image metadata
@ -145,18 +130,21 @@ public:
* @param index number of datablock images used by the same VM. Will be
* automatically increased.
* @param img_type will be set to the used image's type
* @param uid of VM owner (to look for the image id within its images)
* @return 0 on success, -1 error, -2 not using the pool
*/
int disk_attribute(VectorAttribute * disk,
int disk_id,
int * index,
Image::ImageType * img_type);
Image::ImageType * img_type,
int uid);
/**
* Generates an Authorization token for the DISK attribute
* @param disk the disk to be authorized
* @param uid of owner (to look for the image id within her images)
* @param ar the AuthRequest
*/
void authorize_disk(VectorAttribute * disk, AuthRequest * ar);
void authorize_disk(VectorAttribute * disk, int uid, AuthRequest * ar);
static const string& source_prefix()
{
@ -180,54 +168,29 @@ private:
/**
* Path to the image repository
**/
static string _source_prefix;
static string _source_prefix;
/**
* Default image type
**/
static string _default_type;
static string _default_type;
/**
* Default device prefix
**/
static string _default_dev_prefix;
static string _default_dev_prefix;
//--------------------------------------------------------------------------
// Pool Attributes
// -------------------------------------------------------------------------
/**
* This map stores the association between IIDs and Image names
*/
map<string, int> image_names;
/**
* Factory method to produce Image objects
* @return a pointer to the new Image
*/
PoolObjectSQL * create()
{
return new Image;
return new Image(-1,"",0);
};
/**
* Callback function to get output the image pool in XML format
* (Image::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);
/**
* Callback function to build the image_names map
* @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 init_cb(void *nil, int num, char **values, char **names);
};
#endif /*IMAGE_POOL_H_*/

View File

@ -18,6 +18,7 @@
#define LEASES_H_
#include "ObjectSQL.h"
#include "ObjectXML.h"
#include "Attribute.h"
#include <map>
@ -40,7 +41,8 @@ public:
* @param _size the max number of leases
*/
Leases(SqlDB * _db, int _oid, unsigned long _size):
oid(_oid), size(_size), db(_db){};
ObjectSQL(),
oid(_oid), size(_size), n_used(0), db(_db){};
virtual ~Leases()
{
@ -108,7 +110,7 @@ protected:
* The Lease class, it represents a pair of IP and MAC assigned to
* a Virtual Machine
*/
class Lease
class Lease : public ObjectXML
{
public:
/**
@ -129,13 +131,19 @@ protected:
* @param _used, the lease is in use
*/
Lease(unsigned int _ip, unsigned int _mac[], int _vid, bool _used=true)
:ip(_ip), vid(_vid), used(_used)
:ObjectXML(),ip(_ip), vid(_vid), used(_used)
{
// TODO check size
mac[PREFIX]=_mac[PREFIX];
mac[SUFFIX]=_mac[SUFFIX];
};
/**
* Creates a new empty lease. Method from_xml should be called right
* after this constructor.
*/
Lease():ObjectXML(){};
~Lease(){};
/**
@ -172,14 +180,6 @@ protected:
*/
friend ostream& operator<<(ostream& os, Lease& _lease);
/**
* Function to print the Lease object into a string in
* plain text
* @param str the resulting string
* @return a reference to the generated string
*/
string& to_str(string& str) const;
/**
* Function to print the Lease object into a string in
* XML format
@ -188,6 +188,23 @@ protected:
*/
string& to_xml(string& xml) const;
/**
* Function to print the Lease object into a string in
* XML format. The output contains all the internal attributes,
* to be saved in the DB as a blob
* @param xml the resulting XML string
* @return a reference to the generated string
*/
string& to_xml_db(string& xml) const;
/**
* Rebuilds the object from an xml formatted string
* @param xml_str The xml-formatted string
*
* @return 0 on success, -1 otherwise
*/
int from_xml(const string &xml_str);
/**
* Constants to access the array storing the MAC address
*/
@ -227,6 +244,11 @@ protected:
*/
map<unsigned int, Lease *> leases;
/**
* Number of used leases
*/
int n_used;
// -------------------------------------------------------------------------
// DataBase implementation variables
// -------------------------------------------------------------------------
@ -235,17 +257,6 @@ protected:
*/
SqlDB * db;
enum ColNames
{
OID = 0,
IP = 1,
MAC_PREFIX = 2,
MAC_SUFFIX = 3,
VID = 4,
USED = 5,
LIMIT = 6
};
static const char * table;
static const char * db_names;
@ -274,14 +285,6 @@ protected:
friend ostream& operator<<(ostream& os, Lease& _lease);
/**
* Function to print the Leases object into a string in
* plain text
* @param str the resulting string
* @return a reference to the generated string
*/
string& to_str(string& str) const;
/**
* Function to print the Leases object into a string in
* XML format

View File

@ -75,6 +75,11 @@ public:
return ipool;
};
ClusterPool * get_cpool()
{
return cpool;
};
// --------------------------------------------------------------
// Manager Accessors
// --------------------------------------------------------------
@ -224,7 +229,7 @@ private:
// -----------------------------------------------------------------------
Nebula():nebula_configuration(0),db(0),vmpool(0),hpool(0),vnpool(0),upool(0),
ipool(0),lcm(0),vmm(0),im(0),tm(0),dm(0),rm(0),hm(0),authm(0)
ipool(0),cpool(0),lcm(0),vmm(0),im(0),tm(0),dm(0),rm(0),hm(0),authm(0)
{
const char * nl = getenv("ONE_LOCATION");
@ -284,6 +289,11 @@ private:
delete ipool;
}
if ( cpool != 0)
{
delete cpool;
}
if ( vmm != 0)
{
delete vmm;
@ -370,6 +380,7 @@ private:
VirtualNetworkPool * vnpool;
UserPool * upool;
ImagePool * ipool;
ClusterPool * cpool;
// ---------------------------------------------------------------
// Nebula Managers

View File

@ -60,6 +60,61 @@ public:
*/
vector<string> operator[] (const char * xpath_expr);
/**
* Gets and sets a xpath attribute, if the attribute is not found a default
* is used
* @param value to set
* @param xpath_expr of the xml element
* @param def default value if the element is not found
*
* @return -1 if default was set
*/
int xpath(string& value, const char * xpath_expr, const char * def);
/**
* Gets and sets a xpath attribute, if the attribute is not found a default
* is used
* @param value to set
* @param xpath_expr of the xml element
* @param def default value if the element is not found
*
* @return -1 if default was set
*/
int xpath(int& value, const char * xpath_expr, const int& def);
/**
* Gets and sets a xpath attribute, if the attribute is not found a default
* is used
* @param value to set
* @param xpath_expr of the xml element
* @param def default value if the element is not found
*
* @return -1 if default was set
*/
int xpath(unsigned int& value, const char * xpath_expr,
const unsigned int& def);
/**
* Gets and sets a xpath attribute, if the attribute is not found a default
* is used
* @param value to set
* @param xpath_expr of the xml element
* @param def default value if the element is not found
*
* @return -1 if default was set
*/
int xpath(time_t& value, const char * xpath_expr, const time_t& def);
/**
* Gets the value of an element from an xml string
* @param value the value of the element
* @param xml the xml string
* @param xpath the xpath of the target element
*
* @return -1 if the element was not found
*/
static int xpath_value(string& value, const char *xml, const char *xpath);
/**
* Get xml nodes by Xpath
* @param xpath_expr the Xpath for the elements
@ -67,14 +122,21 @@ public:
* returned as pointers to the object nodes.
* @return the number of nodes found
*/
int get_nodes (const char * xpath_expr, vector<xmlNodePtr>& content);
int get_nodes(const char * xpath_expr, vector<xmlNodePtr>& content);
/**
* Updates the object representation with a new XML document. Previous
* XML resources are freed
* @param xml_doc the new xml document
*/
int update(const string &xml_doc);
int update_from_str(const string &xml_doc);
/**
* Updates the object representation with a new XML document. Previous
* XML resources are freed
* @param xml_doc the new xml document
*/
int update_from_node(const xmlNodePtr node);
// ---------------------------------------------------------
// Lex & bison parser for requirements and rank expressions

View File

@ -18,7 +18,9 @@
#define POOL_OBJECT_SQL_H_
#include "ObjectSQL.h"
#include "ObjectXML.h"
#include <pthread.h>
#include <string.h>
using namespace std;
@ -31,11 +33,13 @@ using namespace std;
* is called.
*/
class PoolObjectSQL : public ObjectSQL
class PoolObjectSQL : public ObjectSQL, public ObjectXML
{
public:
PoolObjectSQL(int id=-1):oid(id),valid(true)
PoolObjectSQL(int id, const string& _name, int _uid,const char *_table)
:ObjectSQL(),ObjectXML(),oid(id),name(_name),uid(_uid),
valid(true),table(_table)
{
pthread_mutex_init(&mutex,0);
};
@ -47,11 +51,25 @@ public:
pthread_mutex_destroy(&mutex);
};
/* --------------------------------------------------------------------- */
int get_oid() const
{
return oid;
};
const string& get_name() const
{
return name;
};
int get_uid()
{
return uid;
};
/* --------------------------------------------------------------------- */
/**
* Check if the object is valid
* @return true if object is valid
@ -68,7 +86,7 @@ public:
void set_valid(const bool _valid)
{
valid = _valid;
}
};
/**
* Function to lock the object
@ -86,17 +104,99 @@ public:
pthread_mutex_unlock(&mutex);
};
/**
* Function to print the object into a string in XML format
* @param xml the resulting XML string
* @return a reference to the generated string
*/
virtual string& to_xml(string& xml) const = 0;
/**
* Rebuilds the object from an xml formatted string
* @param xml_str The xml-formatted string
*
* @return 0 on success, -1 otherwise
*/
virtual int from_xml(const string &xml_str) = 0;
protected:
/**
* The object unique ID
* Callback function to unmarshall a PoolObjectSQL
* @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 oid;
int select_cb(void *nil, int num, char **values, char **names)
{
if ( (!values[0]) || (num != 1) )
{
return -1;
}
return from_xml(values[0]);
};
/**
* The contents ob this object are valid
* Reads the PoolObjectSQL (identified by its OID) from the database.
* @param db pointer to the db
* @return 0 on success
*/
bool valid;
virtual int select(SqlDB *db);
/**
* Reads the PoolObjectSQL (identified by its OID) from the database.
* @param db pointer to the db
* @return 0 on success
*/
virtual int select(SqlDB *db, const string& _name, int _uid);
/**
* Drops object from the database
* @param db pointer to the db
* @return 0 on success
*/
virtual int drop(SqlDB *db);
/**
* Function to output a pool object into a stream in 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 dump(ostringstream& oss, int num, char **values, char **names)
{
if ( (!values[0]) || (num != 1) )
{
return -1;
}
oss << values[0];
return 0;
};
/**
* The object's unique ID
*/
int oid;
/**
* The object's name
*/
string name;
/**
* Object's owner, set it to -1 if owner is not used
*/
int uid;
/**
* The contents of this object are valid
*/
bool valid;
private:
@ -110,6 +210,11 @@ private:
* IS LOCKED when the class destructor is called.
*/
pthread_mutex_t mutex;
/**
* Pointer to the SQL table for the PoolObjectSQL
*/
const char * table;
};
#endif /*POOL_OBJECT_SQL_H_*/

View File

@ -45,6 +45,7 @@ public:
* @param _db a pointer to the database
* @param table the name of the table supporting the pool (to set the oid
* counter). If null the OID counter is not updated.
* @param with_uid the Pool objects have an owner id (uid)
*/
PoolSQL(SqlDB * _db, const char * table);
@ -68,9 +69,18 @@ public:
*
* @return a pointer to the object, 0 in case of failure
*/
virtual PoolObjectSQL * get(
int oid,
bool lock);
PoolObjectSQL * get(int oid, bool lock);
/**
* Gets an object from the pool (if needed the object is loaded from the
* database).
* @param name of the object
* @param uid id of owner
* @param lock locks the object if true
*
* @return a pointer to the object, 0 in case of failure
*/
PoolObjectSQL * get(const string& name, int uid, bool lock);
/**
* Finds a set objects that satisfies a given condition
@ -141,6 +151,27 @@ protected:
*/
SqlDB * db;
/**
* Dumps the pool in XML format. A filter can be also added to the
* query
* @param oss the output stream to dump the pool contents
* @param elem_name Name of the root xml pool name
* @param table Pool table name
* @param where filter for the objects, defaults to all
*
* @return 0 on success
*/
int dump(ostringstream& oss, const string& elem_name,
const char * table, const string& where);
/**
* Returns the value of the last identifier assigned by the pool
*/
int get_lastOID()
{
return lastOID;
};
private:
pthread_mutex_t mutex;
@ -156,7 +187,7 @@ private:
* Last object ID assigned to an object. It must be initialized by the
* target pool.
*/
int lastOID;
int lastOID;
/**
* The pool is implemented with a Map of SQL object pointers, using the
@ -164,6 +195,12 @@ private:
*/
map<int,PoolObjectSQL *> pool;
/**
* This is a name index for the pool map. The key is the name of the object
* , that may be combained with the owner id.
*/
map<string,PoolObjectSQL *> name_pool;
/**
* Factory method, must return an ObjectSQL pointer to an allocated pool
* specific object.
@ -200,6 +237,25 @@ private:
*/
void replace();
/**
* Generate an index key for the object
* @param name of the object
* @param uid owner of the object, only used if needed
*
* @return the key, a string
*/
string key(const string& name, int uid)
{
ostringstream key;
key << name << ':' << uid;
return key.str();
};
/* ---------------------------------------------------------------------- */
/* ---------------------------------------------------------------------- */
/**
* Callback to set the lastOID (PoolSQL::PoolSQL)
*/
@ -209,6 +265,15 @@ private:
* Callback to store the IDs of pool objects (PoolSQL::search)
*/
int search_cb(void *_oids, int num, char **values, char **names);
/**
* Callback function to get output in XML format
* @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 /*POOL_SQL_H_*/

View File

@ -23,6 +23,7 @@
#include "UserPool.h"
#include "VirtualNetworkPool.h"
#include "ImagePool.h"
#include "ClusterPool.h"
#include <xmlrpc-c/base.hpp>
#include <xmlrpc-c/registry.hpp>
@ -44,10 +45,12 @@ public:
VirtualNetworkPool * _vnpool,
UserPool * _upool,
ImagePool * _ipool,
ClusterPool * _cpool,
int _port,
string _xml_log_file)
:vmpool(_vmpool),hpool(_hpool),vnpool(_vnpool),upool(_upool),
ipool(_ipool),port(_port),socket_fd(-1),xml_log_file(_xml_log_file)
ipool(_ipool),cpool(_cpool),port(_port),socket_fd(-1),
xml_log_file(_xml_log_file)
{
am.addListener(this);
};
@ -126,6 +129,11 @@ private:
*/
ImagePool * ipool;
/**
* Pointer to the Image Pool, to access images
*/
ClusterPool * cpool;
/**
* Port number where the connection will be open
*/
@ -478,7 +486,7 @@ private:
vmpool(_vmpool),
upool(_upool)
{
_signature="A:sibi";
_signature="A:sii";
_help="Returns the virtual machine pool";
};
@ -632,10 +640,10 @@ private:
{
public:
ClusterAllocate(
HostPool * _hpool,
UserPool * _upool):
hpool(_hpool),
upool(_upool)
UserPool * _upool,
ClusterPool * _cpool):
upool(_upool),
cpool(_cpool)
{
_signature="A:ss";
_help="Allocates a cluster in the pool";
@ -648,8 +656,8 @@ private:
xmlrpc_c::value * const retvalP);
private:
HostPool * hpool;
UserPool * upool;
UserPool * upool;
ClusterPool * cpool;
};
/* ---------------------------------------------------------------------- */
@ -658,10 +666,10 @@ private:
{
public:
ClusterInfo(
HostPool * _hpool,
UserPool * _upool):
hpool(_hpool),
upool(_upool)
UserPool * _upool,
ClusterPool * _cpool):
upool(_upool),
cpool(_cpool)
{
_signature="A:si";
_help="Returns cluster information";
@ -674,8 +682,8 @@ private:
xmlrpc_c::value * const retvalP);
private:
HostPool * hpool;
UserPool * upool;
UserPool * upool;
ClusterPool * cpool;
};
/* ---------------------------------------------------------------------- */
@ -684,10 +692,10 @@ private:
{
public:
ClusterDelete(
HostPool * _hpool,
UserPool * _upool):
hpool(_hpool),
upool(_upool)
UserPool * _upool,
ClusterPool * _cpool):
upool(_upool),
cpool(_cpool)
{
_signature="A:si";
_help="Deletes a cluster from the pool";
@ -700,8 +708,8 @@ private:
xmlrpc_c::value * const retvalP);
private:
HostPool * hpool;
UserPool * upool;
UserPool * upool;
ClusterPool * cpool;
};
/* ---------------------------------------------------------------------- */
@ -710,10 +718,12 @@ private:
{
public:
ClusterAdd(
HostPool * _hpool,
UserPool * _upool):
HostPool * _hpool,
UserPool * _upool,
ClusterPool * _cpool):
hpool(_hpool),
upool(_upool)
upool(_upool),
cpool(_cpool)
{
_signature="A:sii";
_help="Adds a host to a cluster";
@ -726,8 +736,9 @@ private:
xmlrpc_c::value * const retvalP);
private:
HostPool * hpool;
UserPool * upool;
HostPool * hpool;
UserPool * upool;
ClusterPool * cpool;
};
/* ---------------------------------------------------------------------- */
@ -736,10 +747,12 @@ private:
{
public:
ClusterRemove(
HostPool * _hpool,
UserPool * _upool):
HostPool * _hpool,
UserPool * _upool,
ClusterPool * _cpool):
hpool(_hpool),
upool(_upool)
upool(_upool),
cpool(_cpool)
{
_signature="A:si";
_help="Removes a host from its cluster";
@ -752,8 +765,9 @@ private:
xmlrpc_c::value * const retvalP);
private:
HostPool * hpool;
UserPool * upool;
HostPool * hpool;
UserPool * upool;
ClusterPool * cpool;
};
/* ---------------------------------------------------------------------- */
@ -762,10 +776,10 @@ private:
{
public:
ClusterPoolInfo(
HostPool * _hpool,
UserPool * _upool):
hpool(_hpool),
upool(_upool)
UserPool * _upool,
ClusterPool * _cpool):
upool(_upool),
cpool(_cpool)
{
_signature="A:s";
_help="Returns the cluster pool information";
@ -778,8 +792,8 @@ private:
xmlrpc_c::value * const retvalP);
private:
HostPool * hpool;
UserPool * upool;
UserPool * upool;
ClusterPool * cpool;
};

View File

@ -176,6 +176,14 @@ public:
*/
int from_xml(const string &xml_str);
/**
* Rebuilds the object from an xml node
* @param node The xml node pointer
*
* @return 0 on success, -1 otherwise
*/
int from_xml_node(const xmlNodePtr node);
protected:
/**
* The template attributes
@ -215,6 +223,12 @@ private:
* Name of the Root element for the XML document
*/
string xml_root;
/**
* Builds the template attribute from the node
* @param root_element The xml element to build the template from.
*/
void rebuild_attributes(const xmlNode * root_element);
};
/* -------------------------------------------------------------------------- */

View File

@ -36,13 +36,6 @@ public:
*/
friend ostream& operator<<(ostream& os, User& u);
/**
* Function to print the User object into a string in plain text
* @param str the resulting string
* @return a reference to the generated string
*/
string& to_str(string& str) const;
/**
* Function to print the User object into a string in XML format
* @param xml the resulting XML string
@ -50,15 +43,6 @@ public:
*/
string& to_xml(string& xml) const;
/**
* Get the User unique identifier UID, that matches the OID of the object
* @return UID User identifier
*/
int get_uid() const
{
return oid;
};
/**
* Check if the user is enabled
* @return true if the user is enabled
@ -68,15 +52,6 @@ public:
return enabled;
}
/**
* Returns user username
* @return username User's hostname
*/
const string& get_username() const
{
return username;
};
/**
* Returns user password
* @return username User's hostname
@ -102,14 +77,6 @@ public:
enabled = false;
};
/**
* Sets user username
*/
void set_username(string _username)
{
username = _username;
};
/**
* Sets user password
*/
@ -145,11 +112,6 @@ private:
// User Attributes
// -------------------------------------------------------------------------
/**
* User's username
*/
string username;
/**
* User's password
*/
@ -191,16 +153,24 @@ private:
db->exec(oss_user);
};
/**
* Rebuilds the object from an xml formatted string
* @param xml_str The xml-formatted string
*
* @return 0 on success, -1 otherwise
*/
int from_xml(const string &xml_str);
protected:
// *************************************************************************
// Constructor
// *************************************************************************
User(int id=-1,
string _username="",
string _password="",
bool _enabled=true);
User(int id,
string _username,
string _password,
bool _enabled);
virtual ~User();
@ -208,58 +178,28 @@ protected:
// DataBase implementation
// *************************************************************************
enum ColNames
{
OID = 0,
USERNAME = 1,
PASSWORD = 2,
ENABLED = 3, // 0 = false, 1 = true
LIMIT = 4
};
static const char * db_names;
static const char * db_bootstrap;
static const char * table;
/**
* Reads the User (identified with its OID=UID) from the database.
* @param db pointer to the db
* @return 0 on success
*/
virtual int select(SqlDB *db);
/**
* Writes the User in the database.
* @param db pointer to the db
* @return 0 on success
*/
virtual int insert(SqlDB *db, string& error_str);
int insert(SqlDB *db, string& error_str);
/**
* Writes/updates the User data fields in the database.
* @param db pointer to the db
* @return 0 on success
*/
virtual int update(SqlDB *db);
/**
* Drops USer from the database
* @param db pointer to the db
* @return 0 on success
*/
virtual int drop(SqlDB *db);
/**
* Function to output a User object in to an stream in 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 dump(ostringstream& oss, int num, char **values, char **names);
int update(SqlDB *db)
{
return insert_replace(db, true);
}
};
#endif /*USER_H_*/

View File

@ -59,17 +59,12 @@ public:
* it is loaded from the DB
* @param oid User unique id
* @param lock locks the User mutex
* @return a pointer to the Host, 0 if the User could not be loaded
* @return a pointer to the User, 0 if the User could not be loaded
*/
User * get(
int oid,
bool lock)
User * get(int oid, bool lock)
{
User * user = static_cast<User *>(PoolSQL::get(oid,lock));
return user;
}
return static_cast<User *>(PoolSQL::get(oid,lock));
};
/**
* Function to get a User from the pool, if the object is not in memory
@ -78,21 +73,10 @@ public:
* @param lock locks the User mutex
* @return a pointer to the User, 0 if the User could not be loaded
*/
User * get(
string username,
bool lock)
User * get(string name, bool lock)
{
map<string, int>::iterator index;
index = known_users.find(username);
if ( index != known_users.end() )
{
return get((int)index->second,lock);
}
return 0;
}
return static_cast<User *>(PoolSQL::get(name,-1,lock));
};
/** Update a particular User
* @param user pointer to User
@ -103,20 +87,12 @@ public:
return user->update(db);
};
/** Drops a user from the DB, the user mutex MUST BE locked
* @param user pointer to User
*/
int drop(User * user)
{
int rc = PoolSQL::drop(user);
if ( rc == 0)
{
known_users.erase(user->get_username());
}
return rc;
return PoolSQL::drop(user);
};
/**
@ -149,7 +125,10 @@ public:
*
* @return 0 on success
*/
int dump(ostringstream& oss, const string& where);
int dump(ostringstream& oss, const string& where)
{
return PoolSQL::dump(oss, "USER_POOL", User::table, where);
};
private:
/**
@ -158,33 +137,8 @@ private:
*/
PoolObjectSQL * create()
{
return new User;
return new User(-1,"","",true);
};
/**
* This map stores the association between UIDs and Usernames
*/
map<string, int> known_users;
/**
* Callback function to get output the user pool in XML format
* (User::dump)
* @param _oss pointer to 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
*/
int dump_cb(void * _oss, int num, char **values, char **names);
/**
* Callback function to build the knwon_user map (User::User)
* @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 init_cb(void *nil, int num, char **values, char **names);
};
#endif /*USER_POOL_H_*/

View File

@ -119,14 +119,6 @@ public:
*/
friend ostream& operator<<(ostream& os, const VirtualMachine& vm);
/**
* Function to print the VirtualMachine object into a string in
* plain text
* @param str the resulting string
* @return a reference to the generated string
*/
string& to_str(string& str) const;
/**
* Function to print the VirtualMachine object into a string in
* XML format
@ -135,6 +127,14 @@ public:
*/
string& to_xml(string& xml) const;
/**
* Rebuilds the object from an xml formatted string
* @param xml_str The xml-formatted string
*
* @return 0 on success, -1 otherwise
*/
int from_xml(const string &xml_str);
// ------------------------------------------------------------------------
// Dynamic Info
// ------------------------------------------------------------------------
@ -183,16 +183,6 @@ public:
}
};
/**
* Returns the name of the VM
* @return the VM name
*/
const string& get_name() const
{
return name;
};
/**
* Returns the deployment ID
* @return the VMM driver specific ID
@ -218,11 +208,11 @@ public:
* Adds a new history record an writes it in the database.
*/
void add_history(
int hid,
string& hostname,
string& vm_dir,
string& vmm_mad,
string& tm_mad);
int hid,
const string& hostname,
const string& vm_dir,
const string& vmm_mad,
const string& tm_mad);
/**
* Duplicates the last history record. Only the host related fields are
@ -678,16 +668,6 @@ public:
lcm_state = s;
};
/**
* Gets the user id of the owner of this VM
* @return the VM uid
*/
int get_uid() const
{
return uid;
};
// ------------------------------------------------------------------------
// Timers
// ------------------------------------------------------------------------
@ -733,6 +713,7 @@ public:
/**
* Get all disk images for this Virtual Machine
* @param error_str Returns the error reason, if any
* @return 0 if success
*/
int get_disk_images(string &error_str);
@ -753,7 +734,6 @@ public:
*/
int generate_context(string &files);
// ------------------------------------------------------------------------
// Image repository related functions
// ------------------------------------------------------------------------
@ -779,11 +759,10 @@ private:
// -------------------------------------------------------------------------
// Identification variables
// -------------------------------------------------------------------------
/**
* User (owner) id
* Owner's name
*/
int uid;
string user_name;
// -------------------------------------------------------------------------
// VM Scheduling & Managing Information
@ -797,11 +776,6 @@ private:
// Virtual Machine Description
// -------------------------------------------------------------------------
/**
* Name of the VM
*/
string name;
/**
* The Virtual Machine template, holds the VM attributes.
*/
@ -854,11 +828,6 @@ private:
*/
int net_rx;
/**
* Sequence number of the last history item.
*/
int last_seq;
/**
* History record, for the current host
*/
@ -958,16 +927,18 @@ private:
/**
* Parse the "CONTEXT" attribute of the template by substituting
* $VARIABLE, $VARIABLE[ATTR] and $VARIABLE[ATTR, ATTR = VALUE]
* @param error_str Returns the error reason, if any
* @return 0 on success
*/
int parse_context();
int parse_context(string& error_str);
/**
* Parse the "REQUIREMENTS" attribute of the template by substituting
* $VARIABLE, $VARIABLE[ATTR] and $VARIABLE[ATTR, ATTR = VALUE]
* @param error_str Returns the error reason, if any
* @return 0 on success
*/
int parse_requirements();
int parse_requirements(string& error_str);
/**
* Parse the "GRAPHICS" attribute and generates a default PORT if not
@ -981,7 +952,8 @@ protected:
// Constructor
//**************************************************************************
VirtualMachine(int id=-1, VirtualMachineTemplate * _vm_template = 0);
VirtualMachine(int id, int uid, string _user_name,
VirtualMachineTemplate * _vm_template);
virtual ~VirtualMachine();
@ -989,32 +961,10 @@ protected:
// DataBase implementation
// *************************************************************************
enum ColNames
{
OID = 0,
UID = 1,
NAME = 2,
LAST_POLL = 3,
STATE = 4,
LCM_STATE = 5,
STIME = 6,
ETIME = 7,
DEPLOY_ID = 8,
MEMORY = 9,
CPU = 10,
NET_TX = 11,
NET_RX = 12,
LAST_SEQ = 13,
TEMPLATE = 14,
LIMIT = 15
};
static const char * table;
static const char * db_names;
static const char * extended_db_names;
static const char * db_bootstrap;
/**
@ -1029,48 +979,28 @@ protected:
* @param db pointer to the db
* @return 0 on success
*/
virtual int insert(SqlDB * db, string& error_str);
int insert(SqlDB * db, string& error_str);
/**
* Writes/updates the Virtual Machine data fields in the database.
* @param db pointer to the db
* @return 0 on success
*/
virtual int update(SqlDB * db);
int update(SqlDB * db)
{
return insert_replace(db, true);
}
/**
* Deletes a VM from the database and all its associated information
* @param db pointer to the db
* @return -1
*/
virtual int drop(SqlDB * db)
int drop(SqlDB * db)
{
NebulaLog::log("ONE",Log::ERROR, "VM Drop not implemented!");
return -1;
}
/**
* Dumps the contect of a set of VirtualMachine objects in the given stream
* using 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 dump(ostringstream& oss, int num, char ** values, char ** names);
/**
* Dumps the contect of a set of VirtualMachine objects in the given stream
* using 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 dump_extended( ostringstream& oss,
int num, char ** values, char ** names);
};
#endif /*VIRTUAL_MACHINE_H_*/

View File

@ -48,11 +48,12 @@ public:
* the template
*/
int allocate (
int uid,
VirtualMachineTemplate *vm_template,
int * oid,
string& error_str,
bool on_hold = false);
int uid,
string user_name,
VirtualMachineTemplate * vm_template,
int * oid,
string& error_str,
bool on_hold = false);
/**
* Function to get a VM from the pool, if the object is not in memory
@ -134,7 +135,7 @@ public:
*/
int dump(ostringstream& oss, const string& where)
{
return dump(oss, true, -1, where);
return dump(oss, -1, where);
}
/**
@ -149,7 +150,7 @@ public:
*
* @return 0 on success
*/
int dump(ostringstream& oss, bool extended, int state, const string& where);
int dump(ostringstream& oss, int state, const string& where);
private:
/**
@ -158,29 +159,8 @@ private:
*/
PoolObjectSQL * create()
{
return new VirtualMachine;
return new VirtualMachine(-1,-1,"", 0);
};
/**
* 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);
/**
* 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_extended_cb(void * _oss, int num, char **values, char **names);
};
#endif /*VIRTUAL_MACHINE_POOL_H_*/

View File

@ -58,24 +58,6 @@ public:
// Virtual Network Public Methods
// *************************************************************************
/**
* Get the Vnet unique identifier VNID, that matches the OID of the object
* @return VNID Image identifier
*/
int get_vnid() const
{
return oid;
};
/**
* Gets the uid of the owner of the Virtual Network
* @return uid
**/
int get_uid()
{
return uid;
}
/**
* Returns true if the Virtual Network is public
* @return true if the Virtual Network is public
@ -175,14 +157,6 @@ public:
*/
friend ostream& operator<<(ostream& os, VirtualNetwork& vn);
/**
* Function to print the VirtualNetwork object into a string in
* plain text
* @param str the resulting string
* @return a reference to the generated string
*/
string& to_str(string& str) const;
/**
* Function to print the VirtualNetwork object into a string in
* XML format
@ -274,14 +248,9 @@ private:
// Identification variables
// -------------------------------------------------------------------------
/**
* Name of the Virtual Network
* Owner's name
*/
string name;
/**
* Owner of the Virtual Network
*/
int uid;
string user_name;
// -------------------------------------------------------------------------
// Binded physical attributes
@ -341,28 +310,29 @@ private:
};
/**
* Callback function to unmarshall a VNW object (VirtualNetwork::select)
* @param num the number of columns read from the DB
* @param names the column names
* @param vaues the column values
* @return 0 on success
* Function to print the VirtualNetwork object into a string in
* XML format
* @param xml the resulting XML string
* @param extended If true, leases are included
* @return a reference to the generated string
*/
int select_cb(void * nil, int num, char **values, char **names);
string& to_xml_extended(string& xml, bool extended) const;
/**
* Function to drop VN entry in vn_pool
* @return 0 on success
* Rebuilds the object from an xml formatted string
* @param xml_str The xml-formatted string
*
* @return 0 on success, -1 otherwise
*/
int vn_drop(SqlDB * db);
protected:
int from_xml(const string &xml_str);
//**************************************************************************
// Constructor
//**************************************************************************
VirtualNetwork(VirtualNetworkTemplate * _vn_template = 0);
VirtualNetwork(int uid,
string _user_name,
VirtualNetworkTemplate * _vn_template = 0);
~VirtualNetwork();
@ -370,24 +340,10 @@ protected:
// DataBase implementation
// *************************************************************************
enum ColNames
{
OID = 0,
UID = 1,
NAME = 2,
TYPE = 3,
BRIDGE = 4,
PUBLIC = 5,
TEMPLATE = 6,
LIMIT = 7
};
static const char * table;
static const char * db_names;
static const char * extended_db_names;
static const char * db_bootstrap;
/**
@ -397,6 +353,23 @@ protected:
*/
int select(SqlDB * db);
/**
* Reads the Virtual Network (identified with its OID) from the database.
* @param db pointer to the db
* @param name of the network
* @param uid of the owner
*
* @return 0 on success
*/
int select(SqlDB * db, const string& name, int uid);
/**
* Reads the Virtual Network leases from the database.
* @param db pointer to the db
* @return 0 on success
*/
int select_leases(SqlDB * db);
/**
* Writes the Virtual Network and its associated template and leases in the database.
* @param db pointer to the db
@ -409,7 +382,10 @@ protected:
* @param db pointer to the db
* @return 0 on success
*/
int update(SqlDB * db);
int update(SqlDB * db)
{
return insert_replace(db, true);
}
/**
* Deletes a VNW from the database and all its associated information:
@ -418,27 +394,7 @@ protected:
* @param db pointer to the db
* @return 0 on success
*/
int drop(SqlDB * db)
{
int rc;
rc = leases->drop(db);
rc += vn_drop(db);
return rc;
}
/**
* Dumps the contect of a VirtualNetwork object in the given stream using
* 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 dump(ostringstream& oss, int num, char **values, char **names);
int drop(SqlDB * db);
};
#endif /*VIRTUAL_NETWORK_H_*/

View File

@ -48,6 +48,7 @@ public:
*/
int allocate (
int uid,
string user_name,
VirtualNetworkTemplate * vn_template,
int * oid,
string& error_str);
@ -59,9 +60,7 @@ public:
* @param lock locks the VN mutex
* @return a pointer to the VN, 0 if the VN could not be loaded
*/
VirtualNetwork * get(
int oid,
bool lock)
VirtualNetwork * get(int oid, bool lock)
{
return static_cast<VirtualNetwork *>(PoolSQL::get(oid,lock));
};
@ -70,12 +69,14 @@ public:
* Function to get a VN from the pool using the network name
* If the object is not in memory it is loaded from the DB
* @param name VN unique name
* @param uid of the VN owner
* @param lock locks the VN mutex
* @return a pointer to the VN, 0 if the VN could not be loaded
*/
VirtualNetwork * get(
const string& name,
bool lock);
VirtualNetwork * get(const string& name, int uid, bool lock)
{
return static_cast<VirtualNetwork *>(PoolSQL::get(name,uid,lock));
};
//--------------------------------------------------------------------------
// Virtual Network DB access functions
@ -88,14 +89,14 @@ public:
* @param vid of the VM requesting the lease
* @return 0 on success, -1 error, -2 not using the pool
*/
int nic_attribute(VectorAttribute * nic, int vid);
int nic_attribute(VectorAttribute * nic, int uid, int vid);
/**
* Generates an Authorization token for a NIC attribute
* @param nic the nic to be authorized
* @param ar the AuthRequest
*/
void authorize_nic(VectorAttribute * nic, AuthRequest * ar);
void authorize_nic(VectorAttribute * nic, int uid, AuthRequest * ar);
/**
* Bootstraps the database table(s) associated to the VirtualNetwork pool
@ -113,7 +114,10 @@ public:
*
* @return 0 on success
*/
int dump(ostringstream& oss, const string& where);
int dump(ostringstream& oss, const string& where)
{
return PoolSQL::dump(oss, "VNET_POOL", VirtualNetwork::table,where);
}
/**
* Get the mac prefix
@ -150,28 +154,8 @@ private:
*/
PoolObjectSQL * create()
{
return new VirtualNetwork();
return new VirtualNetwork(0,"",0);
};
/**
* Callback function to get output the virtual network pool in XML format
* (VirtualNetworkPool::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);
/**
* Callback function to get the ID of a given virtual network
* (VirtualNetworkPool::get)
* @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 get_cb(void * _oss, int num, char **values, char **names);
};
#endif /*VIRTUAL_NETWORK_POOL_H_*/

View File

@ -25,6 +25,7 @@
#include "VirtualNetworkPool.h"
#include "HostPool.h"
#include "UserPool.h"
#include "ClusterPool.h"
#include "VirtualMachineManager.h"
#include "LifeCycleManager.h"
@ -41,7 +42,7 @@ protected:
NebulaTest():mysql(false), need_host_pool(false), need_vm_pool(false),
need_vnet_pool(false), need_image_pool(false),
need_user_pool(false), need_vmm(false),
need_user_pool(false), need_cluster_pool(false),need_vmm(false),
need_im(false), need_tm(false),
need_lcm(false), need_dm(false),
need_rm(false), need_hm(false),
@ -60,6 +61,7 @@ public:
bool need_vnet_pool;
bool need_image_pool;
bool need_user_pool;
bool need_cluster_pool;
bool need_vmm;
bool need_im;
@ -94,6 +96,8 @@ public:
string default_image_type,
string default_device_prefix);
virtual ClusterPool* create_cpool(SqlDB* db);
// ------------------------------------------------------------------------
// Managers
// ------------------------------------------------------------------------
@ -122,6 +126,7 @@ public:
VirtualNetworkPool * vnpool,
UserPool * upool,
ImagePool * ipool,
ClusterPool * cpool,
string log_file);
virtual HookManager* create_hm(VirtualMachinePool * vmpool);

View File

@ -166,14 +166,14 @@ public:
{
// The pool is empty
// Non existing oid
obj = pool->get(13, true);
obj = pool->get(13, false);
CPPUNIT_ASSERT( obj == 0 );
// Allocate an object
allocate(0);
// Ask again for a non-existing oid
obj = pool->get(213, true);
obj = pool->get(213, false);
CPPUNIT_ASSERT( obj == 0 );
}

View File

@ -6,10 +6,11 @@ TWD_DIR="../../src"
BASE_DIR=$PWD
TESTS="$TWD_DIR/vnm/test \
$TWD_DIR/scheduler/src/xml/test \
$TWD_DIR/xml/test \
$TWD_DIR/scheduler/src/pool/test \
$TWD_DIR/common/test \
$TWD_DIR/host/test \
$TWD_DIR/cluster/test \
$TWD_DIR/template/test \
$TWD_DIR/image/test \
$TWD_DIR/authm/test \

184
src/cluster/Cluster.cc Normal file
View File

@ -0,0 +1,184 @@
/* ------------------------------------------------------------------------ */
/* Copyright 2002-2010, OpenNebula Project Leads (OpenNebula.org) */
/* */
/* Licensed under the Apache License, Version 2.0 (the "License"); you may */
/* not use this file except in compliance with the License. You may obtain */
/* a copy of the License at */
/* */
/* http://www.apache.org/licenses/LICENSE-2.0 */
/* */
/* Unless required by applicable law or agreed to in writing, software */
/* distributed under the License is distributed on an "AS IS" BASIS, */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
/* See the License for the specific language governing permissions and */
/* limitations under the License. */
/* ------------------------------------------------------------------------ */
#include <limits.h>
#include <string.h>
#include <iostream>
#include <sstream>
#include "Cluster.h"
const char * Cluster::table = "cluster_pool";
const char * Cluster::db_names = "oid, name, body";
const char * Cluster::db_bootstrap = "CREATE TABLE IF NOT EXISTS cluster_pool ("
"oid INTEGER PRIMARY KEY, name VARCHAR(256), body TEXT, UNIQUE(name))";
/* ************************************************************************ */
/* Cluster :: Constructor/Destructor */
/* ************************************************************************ */
Cluster::Cluster(int id, const string& name):PoolObjectSQL(id,name,-1,table){};
Cluster::~Cluster(){};
/* ************************************************************************ */
/* Cluster :: Database Access Functions */
/* ************************************************************************ */
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
int Cluster::insert(SqlDB *db, string& error_str)
{
int rc;
rc = insert_replace(db, false);
if ( rc != 0 )
{
error_str = "Error inserting Cluster in DB.";
}
return rc;
}
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
int Cluster::update(SqlDB *db)
{
int rc;
rc = insert_replace(db, true);
return rc;
}
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
int Cluster::insert_replace(SqlDB *db, bool replace)
{
ostringstream oss;
int rc;
string xml_body;
char * sql_name;
char * sql_xml;
// Update the Cluster
sql_name = db->escape_str(name.c_str());
if ( sql_name == 0 )
{
goto error_name;
}
sql_xml = db->escape_str(to_xml(xml_body).c_str());
if ( sql_xml == 0 )
{
goto error_body;
}
if(replace)
{
oss << "REPLACE";
}
else
{
oss << "INSERT";
}
// Construct the SQL statement to Insert or Replace
oss <<" INTO "<<table <<" ("<< db_names <<") VALUES ("
<< oid << ","
<< "'" << sql_name << "',"
<< "'" << sql_xml << "')";
rc = db->exec(oss);
db->free_str(sql_name);
db->free_str(sql_xml);
return rc;
error_body:
db->free_str(sql_name);
error_name:
return -1;
}
/* ************************************************************************ */
/* Cluster :: Misc */
/* ************************************************************************ */
ostream& operator<<(ostream& os, Cluster& cluster)
{
string cluster_str;
os << cluster.to_xml(cluster_str);
return os;
}
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
string& Cluster::to_xml(string& xml) const
{
ostringstream oss;
oss <<
"<CLUSTER>" <<
"<ID>" << oid << "</ID>" <<
"<NAME>" << name << "</NAME>" <<
"</CLUSTER>";
xml = oss.str();
return xml;
}
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
int Cluster::from_xml(const string& xml)
{
int rc = 0;
// Initialize the internal XML object
update_from_str(xml);
// Get class base attributes
rc += xpath(oid, "/CLUSTER/ID", -1);
rc += xpath(name,"/CLUSTER/NAME", "not_found");
if (rc != 0)
{
return -1;
}
return 0;
}

150
src/cluster/ClusterPool.cc Normal file
View File

@ -0,0 +1,150 @@
/* -------------------------------------------------------------------------- */
/* Copyright 2002-2010, OpenNebula Project Leads (OpenNebula.org) */
/* */
/* Licensed under the Apache License, Version 2.0 (the "License"); you may */
/* not use this file except in compliance with the License. You may obtain */
/* a copy of the License at */
/* */
/* http://www.apache.org/licenses/LICENSE-2.0 */
/* */
/* Unless required by applicable law or agreed to in writing, software */
/* distributed under the License is distributed on an "AS IS" BASIS, */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
/* See the License for the specific language governing permissions and */
/* limitations under the License. */
/* -------------------------------------------------------------------------- */
#include "ClusterPool.h"
#include "Nebula.h"
#include "NebulaLog.h"
#include <stdexcept>
const string ClusterPool::DEFAULT_CLUSTER_NAME = "default";
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
ClusterPool::ClusterPool(SqlDB * db):PoolSQL(db, Cluster::table)
{
// lastOID is set in PoolSQL::init_cb
if (get_lastOID() == -1)
{
int rc;
Cluster * cluster;
string error_str;
// Build a new Cluster object
cluster = new Cluster(0, ClusterPool::DEFAULT_CLUSTER_NAME);
// Insert the Object in the pool
rc = PoolSQL::allocate(cluster, error_str);
if(rc != 0)
{
ostringstream oss;
oss << "Error trying to create default cluster: " << error_str;
NebulaLog::log("CLUSTER",Log::ERROR,oss);
throw runtime_error(oss.str());
}
}
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int ClusterPool::allocate(int * oid, string name, string& error_str)
{
Cluster * cluster;
ostringstream oss;
if ( name.empty() )
{
goto error_name;
}
// Check for duplicates
cluster = get(name, false);
if( cluster != 0 )
{
goto error_duplicated;
}
// Build a new Cluster object
cluster = new Cluster(-1, name);
// Insert the Object in the pool
*oid = PoolSQL::allocate(cluster, error_str);
return *oid;
error_name:
oss << "NAME cannot be empty.";
goto error_common;
error_duplicated:
oss << "NAME is already taken by CLUSTER " << cluster->get_oid() << ".";
error_common:
*oid = -1;
error_str = oss.str();
return *oid;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int ClusterPool::drop(Cluster * cluster)
{
int rc;
Host* host;
vector<int> hids;
vector<int>::iterator hid_it;
Nebula& nd = Nebula::instance();
HostPool * hpool = nd.get_hpool();
string cluster_name = cluster->get_name();
string where = "cluster = '" + cluster_name + "'";
// Return error if cluster is 'default'
if( cluster->get_oid() == 0 )
{
NebulaLog::log("CLUSTER",Log::WARNING,
"Default cluster cannot be deleted.");
return -1;
}
rc = cluster->drop(db);
// Move the hosts assigned to the deleted cluster to the default one
if( rc == 0 )
{
hpool->search(hids, where);
for ( hid_it=hids.begin() ; hid_it < hids.end(); hid_it++ )
{
host = hpool->get(*hid_it, true);
if ( host == 0 )
{
continue;
}
set_default_cluster(host);
hpool->update(host);
host->unlock();
}
}
return rc;
}

30
src/cluster/SConstruct Normal file
View File

@ -0,0 +1,30 @@
# SConstruct for src/cluster
# -------------------------------------------------------------------------- #
# Copyright 2002-2010, OpenNebula Project Leads (OpenNebula.org) #
# #
# Licensed under the Apache License, Version 2.0 (the "License"); you may #
# not use this file except in compliance with the License. You may obtain #
# a copy of the License at #
# #
# http://www.apache.org/licenses/LICENSE-2.0 #
# #
# Unless required by applicable law or agreed to in writing, software #
# distributed under the License is distributed on an "AS IS" BASIS, #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
# See the License for the specific language governing permissions and #
# limitations under the License. #
#--------------------------------------------------------------------------- #
Import('env')
lib_name='nebula_cluster'
# Sources to generate the library
source_files=[
'ClusterPool.cc',
'Cluster.cc'
]
# Build library
env.StaticLibrary(lib_name, source_files)

View File

@ -0,0 +1,415 @@
/* -------------------------------------------------------------------------- */
/* Copyright 2002-2010, OpenNebula Project Leads (OpenNebula.org) */
/* */
/* Licensed under the Apache License, Version 2.0 (the "License"); you may */
/* not use this file except in compliance with the License. You may obtain */
/* a copy of the License at */
/* */
/* http://www.apache.org/licenses/LICENSE-2.0 */
/* */
/* Unless required by applicable law or agreed to in writing, software */
/* distributed under the License is distributed on an "AS IS" BASIS, */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
/* See the License for the specific language governing permissions and */
/* limitations under the License. */
/* -------------------------------------------------------------------------- */
#include <string>
#include <iostream>
#include <stdlib.h>
#include "ClusterPool.h"
#include "PoolTest.h"
using namespace std;
const string names[] = {"cluster_a", "Second cluster"};
const string xmls[] =
{
"<CLUSTER><ID>1</ID><NAME>cluster_a</NAME></CLUSTER>",
"<CLUSTER><ID>2</ID><NAME>Second cluster</NAME></CLUSTER>"
};
const string cluster_default =
"<CLUSTER><ID>0</ID><NAME>default</NAME></CLUSTER>";
const string cluster_xml_dump =
"<CLUSTER_POOL><CLUSTER><ID>0</ID><NAME>default</NAME></CLUSTER><CLUSTER><ID>1</ID><NAME>cluster_a</NAME></CLUSTER><CLUSTER><ID>3</ID><NAME>cluster_c</NAME></CLUSTER><CLUSTER><ID>4</ID><NAME>cluster_d</NAME></CLUSTER></CLUSTER_POOL>";
const string host_0_cluster =
"<HOST><ID>0</ID><NAME>Host one</NAME><STATE>0</STATE><IM_MAD>im_mad</IM_MAD><VM_MAD>vmm_mad</VM_MAD><TM_MAD>tm_mad</TM_MAD><LAST_MON_TIME>0</LAST_MON_TIME><CLUSTER>cluster_a</CLUSTER><HOST_SHARE><DISK_USAGE>0</DISK_USAGE><MEM_USAGE>0</MEM_USAGE><CPU_USAGE>0</CPU_USAGE><MAX_DISK>0</MAX_DISK><MAX_MEM>0</MAX_MEM><MAX_CPU>0</MAX_CPU><FREE_DISK>0</FREE_DISK><FREE_MEM>0</FREE_MEM><FREE_CPU>0</FREE_CPU><USED_DISK>0</USED_DISK><USED_MEM>0</USED_MEM><USED_CPU>0</USED_CPU><RUNNING_VMS>0</RUNNING_VMS></HOST_SHARE><TEMPLATE></TEMPLATE></HOST>";
const string host_0_default =
"<HOST><ID>0</ID><NAME>Host one</NAME><STATE>0</STATE>"
"<IM_MAD>im_mad</IM_MAD><VM_MAD>vmm_mad</VM_MAD><TM_MAD>tm_mad</TM_MAD>"
"<LAST_MON_TIME>0</LAST_MON_TIME><CLUSTER>default</CLUSTER><HOST_SHARE>"
"<DISK_USAGE>0</DISK_USAGE><MEM_USAGE>0</MEM_USAGE><CPU_USAGE>0</CPU_USAGE>"
"<MAX_DISK>0</MAX_DISK><MAX_MEM>0</MAX_MEM><MAX_CPU>0</MAX_CPU>"
"<FREE_DISK>0</FREE_DISK><FREE_MEM>0</FREE_MEM><FREE_CPU>0</FREE_CPU>"
"<USED_DISK>0</USED_DISK><USED_MEM>0</USED_MEM><USED_CPU>0</USED_CPU>"
"<RUNNING_VMS>0</RUNNING_VMS></HOST_SHARE><TEMPLATE></TEMPLATE></HOST>";
/* ************************************************************************* */
/* ************************************************************************* */
#include "NebulaTest.h"
class NebulaTestCluster: public NebulaTest
{
public:
NebulaTestCluster():NebulaTest()
{
NebulaTest::the_tester = this;
need_host_pool = true;
need_cluster_pool= true;
}
};
class ClusterPoolTest : public PoolTest
{
CPPUNIT_TEST_SUITE (ClusterPoolTest);
// Not all tests from PoolTest can be used. Because
// of the initial default cluster added to the DB, the
// oid_assignment would fail.
CPPUNIT_TEST (get_from_cache);
CPPUNIT_TEST (get_from_db);
CPPUNIT_TEST (wrong_get);
CPPUNIT_TEST (drop_and_get);
CPPUNIT_TEST (name_pool);
CPPUNIT_TEST (duplicates);
CPPUNIT_TEST (set_cluster);
CPPUNIT_TEST (remove_cluster);
CPPUNIT_TEST (delete_cluster);
CPPUNIT_TEST (dump);
CPPUNIT_TEST_SUITE_END ();
protected:
NebulaTestCluster * tester;
HostPool * hpool;
ClusterPool * cpool;
void bootstrap(SqlDB* db)
{
// setUp overwritten
};
PoolSQL* create_pool(SqlDB* db)
{
// setUp overwritten
return cpool;
};
int allocate(int index)
{
int oid;
string err;
return cpool->allocate(&oid, names[index], err);
};
void check(int index, PoolObjectSQL* obj)
{
Cluster * cluster = static_cast<Cluster *>(obj);
CPPUNIT_ASSERT( obj != 0 );
string xml_str = "";
string name = cluster->get_name();
CPPUNIT_ASSERT( name == names[index] );
// Get the xml
cluster->to_xml(xml_str);
// A little help for debugging
/*
if( xml_str != xmls[index] )
{
cout << endl << xml_str << endl << "========"
<< endl << xmls[index];
}
//*/
CPPUNIT_ASSERT( xml_str == xmls[index]);
};
private:
/**
* Allocates a Host.
*/
void init_hp()
{
int oid;
string err;
// Allocate a host
oid = hpool->allocate(&oid, "Host one","im_mad","vmm_mad", "tm_mad", err);
CPPUNIT_ASSERT(oid == 0);
hpool->clean();
};
public:
ClusterPoolTest()
{
xmlInitParser();
};
~ClusterPoolTest()
{
xmlCleanupParser();
};
void setUp()
{
create_db();
tester = new NebulaTestCluster();
Nebula& neb = Nebula::instance();
neb.start();
hpool = neb.get_hpool();
cpool = neb.get_cpool();
pool = cpool;
};
void tearDown()
{
delete_db();
delete tester;
};
/* ********************************************************************* */
/* ********************************************************************* */
// Test intended to check the PoolSQL name index functionallity
void name_pool()
{
Cluster * cluster;
int oid;
string err;
// Allocate some clusters
cpool->allocate(&oid, "name_1", err);
CPPUNIT_ASSERT( oid == 1 );
cpool->allocate(&oid, "name_2", err);
CPPUNIT_ASSERT( oid == 2 );
cpool->allocate(&oid, "name_3", err);
CPPUNIT_ASSERT( oid == 3 );
// Clean the cache
cpool->clean();
cpool->allocate(&oid, "name_4", err);
CPPUNIT_ASSERT( oid == 4 );
cpool->allocate(&oid, "name_5", err);
CPPUNIT_ASSERT( oid == 5 );
// Cluster names 0-3 should be unknown, and 4-5 cached
// Ask for a cached object
cluster = cpool->get("name_5", false);
CPPUNIT_ASSERT( cluster != 0 );
CPPUNIT_ASSERT( cluster->get_oid() == 5 );
CPPUNIT_ASSERT( cluster->get_name() == "name_5" );
// Ask for non-cached object
cluster = cpool->get("name_2", false);
CPPUNIT_ASSERT( cluster != 0 );
CPPUNIT_ASSERT( cluster->get_oid() == 2 );
CPPUNIT_ASSERT( cluster->get_name() == "name_2" );
// Ask for non-existing object
cluster = cpool->get("name_X", false);
CPPUNIT_ASSERT( cluster == 0 );
};
/* ********************************************************************* */
void duplicates()
{
int rc, oid;
string err;
// Allocate a cluster.
rc = cpool->allocate(&oid, names[1], err);
CPPUNIT_ASSERT( oid == 1 );
CPPUNIT_ASSERT( oid == rc );
// Try to allocate twice the same cluster, should fail
rc = cpool->allocate(&oid, names[1], err);
CPPUNIT_ASSERT( rc == -1 );
CPPUNIT_ASSERT( oid == rc );
}
/* ********************************************************************* */
void set_cluster()
{
Host* host;
int rc;
string xml_str, err;
init_hp();
host = hpool->get(0, false);
CPPUNIT_ASSERT(host != 0);
rc = host->set_cluster("cluster_a");
CPPUNIT_ASSERT( rc == 0 );
hpool->update(host);
host->to_xml(xml_str);
CPPUNIT_ASSERT( xml_str == host_0_cluster);
}
/* ********************************************************************* */
void remove_cluster()
{
Host* host;
int rc;
string xml_str;
init_hp();
host = hpool->get(0, false);
CPPUNIT_ASSERT(host != 0);
// Set cluster
rc = host->set_cluster("cluster_a");
CPPUNIT_ASSERT( rc == 0 );
hpool->update(host);
host->to_xml(xml_str);
CPPUNIT_ASSERT( xml_str == host_0_cluster);
// Remove from the cluster
rc = cpool->set_default_cluster(host);
CPPUNIT_ASSERT( rc == 0 );
hpool->update(host);
// The host should have been moved to the default cluster
host = hpool->get(0, false);
CPPUNIT_ASSERT(host != 0);
host->to_xml(xml_str);
CPPUNIT_ASSERT( xml_str == host_0_default);
}
/* ********************************************************************* */
void delete_cluster()
{
Host * host;
Cluster * cluster;
int rc, oid;
string xml_str;
init_hp();
host = hpool->get(0, false);
CPPUNIT_ASSERT(host != 0);
// Allocate a cluster
oid = allocate(0);
CPPUNIT_ASSERT(oid == 1);
cluster = cpool->get(1, false);
// Set cluster
rc = host->set_cluster(cluster->get_name());
CPPUNIT_ASSERT( rc == 0 );
hpool->update(host);
host->to_xml(xml_str);
CPPUNIT_ASSERT( xml_str == host_0_cluster);
// Delete the cluster
rc = cpool->drop(cluster);
CPPUNIT_ASSERT( rc == 0 );
// The host should have been moved to the default cluster
host = hpool->get(0, false);
host->to_xml(xml_str);
/*
if( xml_str != host_0_default )
{
cout << endl << xml_str << endl << "========"
<< endl << host_0_default;
}
//*/
CPPUNIT_ASSERT( xml_str == host_0_default);
}
/* ********************************************************************* */
void dump()
{
Cluster * cluster;
int oid, rc;
ostringstream oss;
string err;
// Allocate some clusters
rc = cpool->allocate(&oid, "cluster_a", err);
CPPUNIT_ASSERT( rc == 1 );
rc = cpool->allocate(&oid, "cluster_b", err);
CPPUNIT_ASSERT( rc == 2 );
rc = cpool->allocate(&oid, "cluster_c", err);
CPPUNIT_ASSERT( rc == 3 );
rc = cpool->allocate(&oid, "cluster_d", err);
CPPUNIT_ASSERT( rc == 4 );
// Drop one of them
cluster = cpool->get(2, false);
CPPUNIT_ASSERT( cluster != 0 );
rc = cpool->drop(cluster);
CPPUNIT_ASSERT( rc == 0 );
// dump the pool
rc = cpool->dump(oss,"");
/*
if( oss.str() != cluster_xml_dump )
{
cout << endl << oss.str() << endl << "========"
<< endl << cluster_xml_dump;
}
//*/
CPPUNIT_ASSERT( oss.str() == cluster_xml_dump );
}
};
/* ************************************************************************* */
/* ************************************************************************* */
int main(int argc, char ** argv)
{
return PoolTest::main(argc, argv, ClusterPoolTest::suite());
}

View File

@ -0,0 +1,56 @@
# --------------------------------------------------------------------------
# Copyright 2002-2010, OpenNebula Project Leads (OpenNebula.org)
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# --------------------------------------------------------------------------
Import('env')
env.Prepend(LIBS=[
'nebula_cluster',
'nebula_host',
'nebula_pool',
'nebula_template',
'nebula_xml',
'nebula_log',
'nebula_common',
'nebula_sql',
### TODO: delete not needed
'nebula_core_test',
'nebula_host',
'nebula_cluster',
'nebula_xml',
'nebula_vmm',
'nebula_im',
'nebula_rm',
'nebula_tm',
'nebula_um',
'nebula_mad',
'nebula_template',
'nebula_vm',
'nebula_vnm',
'nebula_image',
'nebula_pool',
'nebula_hm',
'nebula_authm',
'nebula_common',
'nebula_lcm',
'nebula_dm',
'nebula_sql',
'nebula_log',
'crypto'
])
env.Program('test','ClusterPoolTest.cc')

View File

@ -1,185 +0,0 @@
/* -------------------------------------------------------------------------- */
/* Copyright 2002-2011, OpenNebula Project Leads (OpenNebula.org) */
/* */
/* Licensed under the Apache License, Version 2.0 (the "License"); you may */
/* not use this file except in compliance with the License. You may obtain */
/* a copy of the License at */
/* */
/* http://www.apache.org/licenses/LICENSE-2.0 */
/* */
/* Unless required by applicable law or agreed to in writing, software */
/* distributed under the License is distributed on an "AS IS" BASIS, */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
/* See the License for the specific language governing permissions and */
/* limitations under the License. */
/* -------------------------------------------------------------------------- */
#include "ClusterPool.h"
#include "NebulaLog.h"
const char * ClusterPool::table = "cluster_pool";
const char * ClusterPool::db_names = "oid, cluster_name";
const char * ClusterPool::db_bootstrap =
"CREATE TABLE IF NOT EXISTS cluster_pool ("
"oid INTEGER PRIMARY KEY, cluster_name VARCHAR(128), "
"UNIQUE(cluster_name) )";
const string ClusterPool::DEFAULT_CLUSTER_NAME = "default";
/* -------------------------------------------------------------------------- */
int ClusterPool::allocate(int * clid, string name, SqlDB *db, string& error_str)
{
int rc;
map<int, string>::iterator it;
ostringstream oss;
// Return error if name already exists
for(it=cluster_names.begin();it!=cluster_names.end();it++)
{
if(it->second == name)
{
goto error_existing_name;
}
}
// Get the highest key, and add 1
*clid = cluster_names.rbegin()->first + 1;
rc = insert(*clid, name, db);
if(rc != 0)
{
goto error_db;
}
return *clid;
error_existing_name:
oss << "Could not allocate new cluster: "
<< name << ", already exists.";
goto error_common;
error_db:
oss << "Could not allocate new cluster " << name << ".";
goto error_common;
error_common:
error_str = oss.str();
NebulaLog::log("CLUSTER", Log::ERROR, oss);
*clid = -1;
return *clid;
}
/* -------------------------------------------------------------------------- */
string ClusterPool::info(int clid)
{
ostringstream oss;
map<int, string>::iterator it;
it = cluster_names.find(clid);
if(it != cluster_names.end())
{
dump_cluster(oss, it->first, it->second);
}
return oss.str();
}
/* -------------------------------------------------------------------------- */
int ClusterPool::drop(int clid, SqlDB *db)
{
int rc;
ostringstream oss;
// Return error if cluster is 'default' or if it doesn't exist
if( clid == 0 || cluster_names.count(clid) == 0 )
{
return -1;
}
oss << "DELETE FROM " << table << " WHERE oid=" << clid;
rc = db->exec(oss);
if(rc == 0)
{
cluster_names.erase(clid);
}
return rc;
}
/* -------------------------------------------------------------------------- */
int ClusterPool::dump(ostringstream& oss)
{
map<int, string>::iterator it;
oss << "<CLUSTER_POOL>";
for(it=cluster_names.begin();it!=cluster_names.end();it++)
{
dump_cluster(oss, it->first, it->second);
}
oss << "</CLUSTER_POOL>";
return 0;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int ClusterPool::insert(int oid, string name, SqlDB *db)
{
ostringstream oss;
int rc;
char * sql_name;
sql_name = db->escape_str(name.c_str());
if ( sql_name == 0 )
{
return -1;
}
oss << "INSERT INTO "<< table <<" ("<< db_names <<") VALUES ("
<< oid << ","
<< "'" << sql_name << "')";
rc = db->exec(oss);
db->free_str(sql_name);
if( rc == 0 )
{
cluster_names.insert( make_pair(oid, name) );
}
return rc;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
void ClusterPool::dump_cluster(ostringstream& oss, int id, string name)
{
oss <<
"<CLUSTER>" <<
"<ID>" << id << "</ID>" <<
"<NAME>" << name << "</NAME>" <<
"</CLUSTER>";
}

View File

@ -29,18 +29,18 @@
Host::Host(
int id,
string _hostname,
string _im_mad_name,
string _vmm_mad_name,
string _tm_mad_name):
PoolObjectSQL(id),
hostname(_hostname),
const string& _hostname,
const string& _im_mad_name,
const string& _vmm_mad_name,
const string& _tm_mad_name,
const string& _cluster):
PoolObjectSQL(id,_hostname,-1,table),
state(INIT),
im_mad_name(_im_mad_name),
vmm_mad_name(_vmm_mad_name),
tm_mad_name(_tm_mad_name),
last_monitored(0),
cluster(ClusterPool::DEFAULT_CLUSTER_NAME),
cluster(_cluster),
host_template()
{}
@ -52,92 +52,11 @@ Host::~Host(){}
const char * Host::table = "host_pool";
const char * Host::db_names = "oid,host_name,state,im_mad,vm_mad,"
"tm_mad,last_mon_time, cluster, template";
const char * Host::db_names = "oid, name, body, state, last_mon_time, cluster";
const char * Host::db_bootstrap = "CREATE TABLE IF NOT EXISTS host_pool ("
"oid INTEGER PRIMARY KEY,host_name VARCHAR(256), state INTEGER,"
"im_mad VARCHAR(128),vm_mad VARCHAR(128),tm_mad VARCHAR(128),"
"last_mon_time INTEGER, cluster VARCHAR(128), template TEXT, "
"UNIQUE(host_name))";
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
int Host::select_cb(void * nil, int num, char **values, char ** names)
{
if ((!values[OID]) ||
(!values[HOST_NAME]) ||
(!values[STATE]) ||
(!values[IM_MAD]) ||
(!values[VM_MAD]) ||
(!values[TM_MAD]) ||
(!values[LAST_MON_TIME]) ||
(!values[CLUSTER]) ||
(!values[TEMPLATE]) ||
(num != LIMIT ))
{
return -1;
}
oid = atoi(values[OID]);
hostname = values[HOST_NAME];
state = static_cast<HostState>(atoi(values[STATE]));
im_mad_name = values[IM_MAD];
vmm_mad_name = values[VM_MAD];
tm_mad_name = values[TM_MAD];
last_monitored = static_cast<time_t>(atoi(values[LAST_MON_TIME]));
cluster = values[CLUSTER];
host_template.from_xml(values[TEMPLATE]);
host_share.hsid = oid;
return 0;
}
/* ------------------------------------------------------------------------ */
int Host::select(SqlDB *db)
{
ostringstream oss;
int rc;
int boid;
set_callback(static_cast<Callbackable::Callback>(&Host::select_cb));
oss << "SELECT " << db_names << " FROM " << table << " WHERE oid = " << oid;
boid = oid;
oid = -1;
rc = db->exec(oss, this);
unset_callback();
if ((rc != 0) || (oid != boid ))
{
return -1;
}
if ( rc != 0 )
{
return -1;
}
// Select the host shares from the DB
rc = host_share.select(db);
if ( rc != 0 )
{
return rc;
}
return 0;
}
"oid INTEGER PRIMARY KEY, name VARCHAR(256), body TEXT, state INTEGER, "
"last_mon_time INTEGER, cluster VARCHAR(128), UNIQUE(name))";
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
@ -145,34 +64,15 @@ int Host::select(SqlDB *db)
int Host::insert(SqlDB *db, string& error_str)
{
int rc;
map<int,HostShare *>::iterator iter;
// Set up the share ID, to insert it
if ( host_share.hsid == -1 )
{
host_share.hsid = oid;
}
// Update the HostShare
rc = host_share.insert(db, error_str);
if ( rc != 0 )
{
return rc;
}
//Insert the Host
rc = insert_replace(db, false);
if ( rc != 0 )
{
error_str = "Error inserting Host in DB.";
host_share.drop(db);
return rc;
}
return 0;
return rc;
}
/* ------------------------------------------------------------------------ */
@ -182,24 +82,9 @@ int Host::update(SqlDB *db)
{
int rc;
// Update the HostShare
rc = host_share.update(db);
if ( rc != 0 )
{
return rc;
}
rc = insert_replace(db, true);
if ( rc != 0 )
{
return rc;
}
return 0;
return rc;
}
/* ------------------------------------------------------------------------ */
@ -210,45 +95,21 @@ int Host::insert_replace(SqlDB *db, bool replace)
ostringstream oss;
int rc;
string xml_template;
string xml_body;
char * sql_hostname;
char * sql_im_mad_name;
char * sql_tm_mad_name;
char * sql_vmm_mad_name;
char * sql_cluster;
char * sql_template;
char * sql_xml;
// Update the Host
sql_hostname = db->escape_str(hostname.c_str());
sql_hostname = db->escape_str(name.c_str());
if ( sql_hostname == 0 )
{
goto error_hostname;
}
sql_im_mad_name = db->escape_str(im_mad_name.c_str());
if ( sql_im_mad_name == 0 )
{
goto error_im;
}
sql_tm_mad_name = db->escape_str(tm_mad_name.c_str());
if ( sql_tm_mad_name == 0 )
{
goto error_tm;
}
sql_vmm_mad_name = db->escape_str(vmm_mad_name.c_str());
if ( sql_vmm_mad_name == 0 )
{
goto error_vmm;
}
sql_cluster = db->escape_str(cluster.c_str());
if ( sql_cluster == 0 )
@ -256,12 +117,11 @@ int Host::insert_replace(SqlDB *db, bool replace)
goto error_cluster;
}
host_template.to_xml(xml_template);
sql_template = db->escape_str(xml_template.c_str());
sql_xml = db->escape_str(to_xml(xml_body).c_str());
if ( sql_template == 0 )
if ( sql_xml == 0 )
{
goto error_template;
goto error_body;
}
if(replace)
@ -275,37 +135,25 @@ int Host::insert_replace(SqlDB *db, bool replace)
// Construct the SQL statement to Insert or Replace
oss <<" INTO "<< table <<" ("<< db_names <<") VALUES ("
oss <<" INTO "<<table <<" ("<< db_names <<") VALUES ("
<< oid << ","
<< "'" << sql_hostname << "',"
<< "'" << sql_xml << "',"
<< state << ","
<< "'" << sql_im_mad_name << "',"
<< "'" << sql_vmm_mad_name << "',"
<< "'" << sql_tm_mad_name << "',"
<< last_monitored << ","
<< "'" << sql_cluster << "',"
<< "'" << sql_template << "')";
<< "'" << sql_cluster << "')";
rc = db->exec(oss);
db->free_str(sql_hostname);
db->free_str(sql_im_mad_name);
db->free_str(sql_tm_mad_name);
db->free_str(sql_vmm_mad_name);
db->free_str(sql_cluster);
db->free_str(sql_template);
db->free_str(sql_xml);
return rc;
error_template:
error_body:
db->free_str(sql_cluster);
error_cluster:
db->free_str(sql_vmm_mad_name);
error_vmm:
db->free_str(sql_tm_mad_name);
error_tm:
db->free_str(sql_im_mad_name);
error_im:
db->free_str(sql_hostname);
error_hostname:
return -1;
@ -314,66 +162,6 @@ error_hostname:
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
int Host::dump(ostringstream& oss, int num, char **values, char **names)
{
if ((!values[OID]) ||
(!values[HOST_NAME]) ||
(!values[STATE]) ||
(!values[IM_MAD]) ||
(!values[VM_MAD]) ||
(!values[TM_MAD]) ||
(!values[LAST_MON_TIME]) ||
(!values[CLUSTER]) ||
(!values[TEMPLATE]) ||
(num != LIMIT + HostShare::LIMIT ))
{
return -1;
}
oss <<
"<HOST>" <<
"<ID>" << values[OID] <<"</ID>" <<
"<NAME>" << values[HOST_NAME] <<"</NAME>" <<
"<STATE>" << values[STATE] <<"</STATE>" <<
"<IM_MAD>" << values[IM_MAD] <<"</IM_MAD>" <<
"<VM_MAD>" << values[VM_MAD] <<"</VM_MAD>" <<
"<TM_MAD>" << values[TM_MAD] <<"</TM_MAD>" <<
"<LAST_MON_TIME>"<< values[LAST_MON_TIME]<<"</LAST_MON_TIME>"<<
"<CLUSTER>" << values[CLUSTER] <<"</CLUSTER>" <<
values[TEMPLATE];
HostShare::dump(oss,num - LIMIT, values + LIMIT, names + LIMIT);
oss << "</HOST>";
return 0;
}
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
int Host::drop(SqlDB * db)
{
ostringstream oss;
int rc;
host_share.drop(db);
oss << "DELETE FROM " << table << " WHERE oid=" << oid;
rc = db->exec(oss);
if ( rc == 0 )
{
set_valid(false);
}
return rc;
}
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
int Host::update_info(string &parse_str)
{
char * error_msg;
@ -426,7 +214,7 @@ string& Host::to_xml(string& xml) const
oss <<
"<HOST>"
"<ID>" << oid << "</ID>" <<
"<NAME>" << hostname << "</NAME>" <<
"<NAME>" << name << "</NAME>" <<
"<STATE>" << state << "</STATE>" <<
"<IM_MAD>" << im_mad_name << "</IM_MAD>" <<
"<VM_MAD>" << vmm_mad_name << "</VM_MAD>" <<
@ -445,26 +233,55 @@ string& Host::to_xml(string& xml) const
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
string& Host::to_str(string& str) const
int Host::from_xml(const string& xml)
{
string template_str;
string share_str;
vector<xmlNodePtr> content;
int int_state;
int rc = 0;
ostringstream os;
// Initialize the internal XML object
update_from_str(xml);
os <<
"ID = " << oid << endl <<
"NAME = " << hostname << endl <<
"STATE = " << state << endl <<
"IM MAD = " << im_mad_name << endl <<
"VMM MAD = " << vmm_mad_name << endl <<
"TM MAD = " << tm_mad_name << endl <<
"LAST_MON = " << last_monitored << endl <<
"CLUSTER = " << cluster << endl <<
"ATTRIBUTES" << endl << host_template.to_str(template_str) << endl <<
"HOST SHARES" << endl << host_share.to_str(share_str) <<endl;
// Get class base attributes
rc += xpath(oid, "/HOST/ID", -1);
rc += xpath(name, "/HOST/NAME", "not_found");
rc += xpath(int_state, "/HOST/STATE", 0);
str = os.str();
rc += xpath(im_mad_name, "/HOST/IM_MAD", "not_found");
rc += xpath(vmm_mad_name, "/HOST/VM_MAD", "not_found");
rc += xpath(tm_mad_name, "/HOST/TM_MAD", "not_found");
return str;
rc += xpath(last_monitored, "/HOST/LAST_MON_TIME", 0);
rc += xpath(cluster, "/HOST/CLUSTER", "not_found");
state = static_cast<HostState>( int_state );
// Get associated classes
ObjectXML::get_nodes("/HOST/HOST_SHARE", content);
if( content.size() < 1 )
{
return -1;
}
rc += host_share.from_xml_node( content[0] );
content.clear();
ObjectXML::get_nodes("/HOST/TEMPLATE", content);
if( content.size() < 1 )
{
return -1;
}
rc += host_template.from_xml_node( content[0] );
if (rc != 0)
{
return -1;
}
return 0;
}

View File

@ -55,7 +55,7 @@ void HostAllocateHook::do_hook(void *arg)
{
hmd->execute(host->get_oid(),
name,
host->get_hostname(),
host->get_name(),
cmd,
parsed_args);
}
@ -179,7 +179,7 @@ void HostStateHook::do_hook(void *arg)
{
hmd->execute(host->get_oid(),
name,
host->get_hostname(),
host->get_name(),
cmd,
parsed_args);
}

View File

@ -22,54 +22,17 @@
#include "HostPool.h"
#include "HostHook.h"
#include "ClusterPool.h"
#include "NebulaLog.h"
#include "ClusterPool.h"
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int HostPool::init_cb(void *nil, int num, char **values, char **names)
{
if ( num != 2 || values == 0 || values[0] == 0 )
{
return -1;
}
cluster_pool.cluster_names.insert( make_pair(atoi(values[0]), values[1]) );
return 0;
}
/* -------------------------------------------------------------------------- */
HostPool::HostPool(SqlDB* db,
vector<const Attribute *> hook_mads,
const string& hook_location)
: PoolSQL(db,Host::table)
{
// ------------------ Initialize Cluster Array ----------------------
ostringstream sql;
set_callback(static_cast<Callbackable::Callback>(&HostPool::init_cb));
sql << "SELECT " << ClusterPool::db_names << " FROM "
<< ClusterPool::table;
db->exec(sql, this);
unset_callback();
if (cluster_pool.cluster_names.empty())
{
int rc = cluster_pool.insert(0, ClusterPool::DEFAULT_CLUSTER_NAME, db);
if(rc != 0)
{
throw runtime_error("Could not create default cluster HostPool");
}
}
// ------------------ Initialize Hooks fot the pool ----------------------
const VectorAttribute * vattr;
@ -180,6 +143,34 @@ int HostPool::allocate (
string& error_str)
{
Host * host;
ostringstream oss;
if ( hostname.empty() )
{
goto error_name;
}
if ( im_mad_name.empty() )
{
goto error_im;
}
if ( vmm_mad_name.empty() )
{
goto error_vmm;
}
if ( tm_mad_name.empty() )
{
goto error_tm;
}
host = get(hostname,false);
if ( host !=0)
{
goto error_duplicated;
}
// Build a new Host object
@ -187,13 +178,40 @@ int HostPool::allocate (
hostname,
im_mad_name,
vmm_mad_name,
tm_mad_name);
tm_mad_name,
ClusterPool::DEFAULT_CLUSTER_NAME);
// Insert the Object in the pool
*oid = PoolSQL::allocate(host, error_str);
return *oid;
error_name:
oss << "NAME cannot be empty.";
goto error_common;
error_im:
oss << "IM_MAD_NAME cannot be empty.";
goto error_common;
error_vmm:
oss << "VMM_MAD_NAME cannot be empty.";
goto error_common;
error_tm:
oss << "TM_MAD_NAME cannot be empty.";
goto error_common;
error_duplicated:
oss << "NAME is already taken by HOST " << host->get_oid() << ".";
error_common:
*oid = -1;
error_str = oss.str();
return *oid;
}
/* -------------------------------------------------------------------------- */
@ -202,18 +220,24 @@ int HostPool::allocate (
int HostPool::discover_cb(void * _map, int num, char **values, char **names)
{
map<int, string> * discovered_hosts;
string im_mad(values[1]);
string im_mad;
int hid;
int rc;
discovered_hosts = static_cast<map<int, string> *>(_map);
if ( (num<=0) || (values[0] == 0) )
if ( (num<2) || (values[0] == 0) || (values[1] == 0) )
{
return -1;
}
hid = atoi(values[0]);
im_mad = values[1];
hid = atoi(values[0]);
rc = ObjectXML::xpath_value(im_mad,values[1],"/HOST/IM_MAD");
if( rc != 0)
{
return -1;
}
discovered_hosts->insert(make_pair(hid,im_mad));
@ -230,7 +254,7 @@ int HostPool::discover(map<int, string> * discovered_hosts, int host_limit)
set_callback(static_cast<Callbackable::Callback>(&HostPool::discover_cb),
static_cast<void *>(discovered_hosts));
sql << "SELECT oid, im_mad FROM "
sql << "SELECT oid, body FROM "
<< Host::table << " WHERE state != "
<< Host::DISABLED << " ORDER BY last_mon_time ASC LIMIT " << host_limit;
@ -240,97 +264,3 @@ int HostPool::discover(map<int, string> * discovered_hosts, int host_limit)
return rc;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int HostPool::dump_cb(void * _oss, int num, char **values, char **names)
{
ostringstream * oss;
oss = static_cast<ostringstream *>(_oss);
return Host::dump(*oss, num, values, names);
}
/* -------------------------------------------------------------------------- */
int HostPool::dump(ostringstream& oss, const string& where)
{
int rc;
ostringstream cmd;
oss << "<HOST_POOL>";
set_callback(static_cast<Callbackable::Callback>(&HostPool::dump_cb),
static_cast<void *>(&oss));
cmd << "SELECT " << Host::db_names << " , " << HostShare::db_names
<< " FROM " << Host::table << " JOIN " << HostShare::table
<< " ON " << Host::table << ".oid = " << HostShare::table << ".hid";
if ( !where.empty() )
{
cmd << " WHERE " << where;
}
rc = db->exec(cmd, this);
oss << "</HOST_POOL>";
unset_callback();
return rc;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int HostPool::drop_cluster(int clid)
{
int rc;
map<int, string>::iterator it;
string cluster_name;
it = cluster_pool.cluster_names.find(clid);
if ( it == cluster_pool.cluster_names.end() )
{
return -1;
}
cluster_name = it->second;
// try to drop the cluster from the pool and DB
rc = cluster_pool.drop(clid, db);
// Move the hosts assigned to the deleted cluster to the default one
if( rc == 0 )
{
Host* host;
vector<int> hids;
vector<int>::iterator hid_it;
string where = "cluster = '" + cluster_name + "'";
search(hids, Host::table, where);
for ( hid_it=hids.begin() ; hid_it < hids.end(); hid_it++ )
{
host = get(*hid_it, true);
if ( host == 0 )
{
continue;
}
set_default_cluster(host);
update(host);
host->unlock();
}
}
return rc;
}

View File

@ -28,12 +28,10 @@
/* ************************************************************************ */
HostShare::HostShare(
int _hsid,
int _max_disk,
int _max_mem,
int _max_cpu):
ObjectSQL(),
hsid(_hsid),
ObjectXML(),
disk_usage(0),
mem_usage(0),
cpu_usage(0),
@ -46,228 +44,7 @@ HostShare::HostShare(
used_disk(0),
used_mem(0),
used_cpu(0),
running_vms(0)
{
}
/* ************************************************************************ */
/* HostShare :: Database Access Functions */
/* ************************************************************************ */
const char * HostShare::table = "host_shares";
const char * HostShare::db_names = "hid,"
"disk_usage, mem_usage, cpu_usage,"
"max_disk, max_mem, max_cpu,"
"free_disk, free_mem, free_cpu,"
"used_disk, used_mem, used_cpu,"
"running_vms";
const char * HostShare::db_bootstrap = "CREATE TABLE IF NOT EXISTS host_shares("
"hid INTEGER PRIMARY KEY,"
"disk_usage INTEGER, mem_usage INTEGER, cpu_usage INTEGER,"
"max_disk INTEGER, max_mem INTEGER, max_cpu INTEGER,"
"free_disk INTEGER, free_mem INTEGER, free_cpu INTEGER,"
"used_disk INTEGER, used_mem INTEGER, used_cpu INTEGER,"
"running_vms INTEGER)";
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
int HostShare::select_cb(void * nil, int num, char **values, char **names)
{
if ((!values[HID]) ||
(!values[DISK_USAGE]) ||
(!values[MEM_USAGE]) ||
(!values[CPU_USAGE]) ||
(!values[MAX_DISK]) ||
(!values[MAX_MEMORY]) ||
(!values[MAX_CPU]) ||
(!values[FREE_DISK]) ||
(!values[FREE_MEMORY]) ||
(!values[FREE_CPU]) ||
(!values[USED_DISK]) ||
(!values[USED_MEMORY]) ||
(!values[USED_CPU]) ||
(!values[RUNNING_VMS]) ||
(num != LIMIT ))
{
return -1;
}
hsid = atoi(values[HID]);
disk_usage = atoi(values[DISK_USAGE]);
mem_usage = atoi(values[MEM_USAGE]);
cpu_usage = atoi(values[CPU_USAGE]);
max_disk = atoi(values[MAX_DISK]);
max_mem = atoi(values[MAX_MEMORY]);
max_cpu = atoi(values[MAX_CPU]);
free_disk = atoi(values[FREE_DISK]);
free_mem = atoi(values[FREE_MEMORY]);
free_cpu = atoi(values[FREE_CPU]);
used_disk = atoi(values[USED_DISK]);
used_mem = atoi(values[USED_MEMORY]);
used_cpu = atoi(values[USED_CPU]);
running_vms = atoi(values[RUNNING_VMS]);
return 0;
}
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
int HostShare::dump(ostringstream& oss,
int num,
char ** values,
char ** names)
{
if ((!values[HID]) ||
(!values[DISK_USAGE]) ||
(!values[MEM_USAGE]) ||
(!values[CPU_USAGE]) ||
(!values[MAX_DISK]) ||
(!values[MAX_MEMORY]) ||
(!values[MAX_CPU]) ||
(!values[FREE_DISK]) ||
(!values[FREE_MEMORY]) ||
(!values[FREE_CPU]) ||
(!values[USED_DISK]) ||
(!values[USED_MEMORY]) ||
(!values[USED_CPU]) ||
(!values[RUNNING_VMS]) ||
(num != LIMIT))
{
return -1;
}
oss <<
"<HOST_SHARE>" <<
"<HID>" << values[HID] << "</HID>" <<
"<DISK_USAGE>"<< values[DISK_USAGE] << "</DISK_USAGE>"<<
"<MEM_USAGE>" << values[MEM_USAGE] << "</MEM_USAGE>" <<
"<CPU_USAGE>" << values[CPU_USAGE] << "</CPU_USAGE>" <<
"<MAX_DISK>" << values[MAX_DISK] << "</MAX_DISK>" <<
"<MAX_MEM>" << values[MAX_MEMORY] << "</MAX_MEM>" <<
"<MAX_CPU>" << values[MAX_CPU] << "</MAX_CPU>" <<
"<FREE_DISK>" << values[FREE_DISK] << "</FREE_DISK>" <<
"<FREE_MEM>" << values[FREE_MEMORY] << "</FREE_MEM>" <<
"<FREE_CPU>" << values[FREE_CPU] << "</FREE_CPU>" <<
"<USED_DISK>" << values[USED_DISK] << "</USED_DISK>" <<
"<USED_MEM>" << values[USED_MEMORY] << "</USED_MEM>" <<
"<USED_CPU>" << values[USED_CPU] << "</USED_CPU>" <<
"<RUNNING_VMS>"<<values[RUNNING_VMS] << "</RUNNING_VMS>"<<
"</HOST_SHARE>";
return 0;
}
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
int HostShare::select(SqlDB * db)
{
ostringstream oss;
int rc;
int bhsid;
set_callback(static_cast<Callbackable::Callback>(&HostShare::select_cb));
oss << "SELECT "<< db_names << " FROM " << table << " WHERE hid = " << hsid;
bhsid = hsid;
hsid = -1;
rc = db->exec(oss,this);
unset_callback();
if (hsid != bhsid )
{
rc = -1;
}
return rc;
}
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
int HostShare::insert(SqlDB * db, string& error_str)
{
int rc;
rc = insert_replace(db, false);
if ( rc == -1 )
{
error_str = "Error inserting Host Share in DB.";
}
return rc;
}
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
int HostShare::update(SqlDB * db)
{
int rc;
rc = insert_replace(db, true);
return rc;
}
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
int HostShare::insert_replace(SqlDB *db, bool replace)
{
ostringstream oss;
int rc;
if(replace)
{
oss << "REPLACE";
}
else
{
oss << "INSERT";
}
oss << " INTO " << table << " ("<< db_names <<") VALUES ("
<< hsid << ","
<< disk_usage <<","<< mem_usage <<","<< cpu_usage<< ","
<< max_disk <<","<< max_mem <<","<< max_cpu << ","
<< free_disk <<","<< free_mem <<","<< free_cpu << ","
<< used_disk <<","<< used_mem <<","<< used_cpu << ","
<< running_vms<< ")";
rc = db->exec(oss);
return rc;
}
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
int HostShare::drop(SqlDB * db)
{
ostringstream oss;
oss << "DELETE FROM " << table << " WHERE hid=" << hsid;
return db->exec(oss);
}
/* ************************************************************************ */
/* HostShare :: Misc */
/* ************************************************************************ */
running_vms(0){};
ostream& operator<<(ostream& os, HostShare& hs)
{
@ -287,7 +64,6 @@ string& HostShare::to_xml(string& xml) const
ostringstream oss;
oss << "<HOST_SHARE>"
<< "<HID>" << hsid << "</HID>"
<< "<DISK_USAGE>" << disk_usage << "</DISK_USAGE>"
<< "<MEM_USAGE>" << mem_usage << "</MEM_USAGE>"
<< "<CPU_USAGE>" << cpu_usage << "</CPU_USAGE>"
@ -311,30 +87,35 @@ string& HostShare::to_xml(string& xml) const
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
string& HostShare::to_str(string& str) const
int HostShare::from_xml_node(const xmlNodePtr node)
{
string template_xml;
ostringstream oss;
int rc = 0;
oss<< "\tHID = " << hsid
<< "\tCPU_USAGE = " << cpu_usage << endl
<< "\tMEMORY_USAGE = " << mem_usage << endl
<< "\tDISK_USAGE = " << disk_usage<< endl
<< "\tMAX_CPU = " << max_cpu << endl
<< "\tMAX_MEMORY = " << max_mem << endl
<< "\tMAX_DISK = " << max_disk<< endl
<< "\tFREE_CPU = " << free_cpu << endl
<< "\tFREE_MEMORY = " << free_mem << endl
<< "\tFREE_DISK = " << free_disk<< endl
<< "\tUSED_CPU = " << used_cpu << endl
<< "\tUSED_MEMORY = " << used_mem << endl
<< "\tUSED_DISK = " << used_disk<< endl
<< "\tRUNNING_VMS = " << running_vms<< endl;
// Initialize the internal XML object
ObjectXML::update_from_node(node);
str = oss.str();
rc += xpath(disk_usage, "/HOST_SHARE/DISK_USAGE", -1);
rc += xpath(mem_usage, "/HOST_SHARE/MEM_USAGE", -1);
rc += xpath(cpu_usage, "/HOST_SHARE/CPU_USAGE", -1);
return str;
rc += xpath(max_disk, "/HOST_SHARE/MAX_DISK", -1);
rc += xpath(max_mem , "/HOST_SHARE/MAX_MEM", -1);
rc += xpath(max_cpu , "/HOST_SHARE/MAX_CPU", -1);
rc += xpath(free_disk, "/HOST_SHARE/FREE_DISK", -1);
rc += xpath(free_mem , "/HOST_SHARE/FREE_MEM", -1);
rc += xpath(free_cpu , "/HOST_SHARE/FREE_CPU", -1);
rc += xpath(used_disk, "/HOST_SHARE/USED_DISK", -1);
rc += xpath(used_mem , "/HOST_SHARE/USED_MEM", -1);
rc += xpath(used_cpu , "/HOST_SHARE/USED_CPU", -1);
rc += xpath(running_vms,"/HOST_SHARE/RUNNING_VMS",-1);
if (rc != 0)
{
return -1;
}
return 0;
}
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */

View File

@ -25,7 +25,6 @@ source_files=[
'Host.cc',
'HostShare.cc',
'HostPool.cc',
'ClusterPool.cc',
'HostHook.cc'
]

View File

@ -33,7 +33,7 @@ const string xmls[] =
{
"<HOST><ID>0</ID><NAME>Host one</NAME><STATE>0</STATE>"
"<IM_MAD>im_mad</IM_MAD><VM_MAD>vmm_mad</VM_MAD><TM_MAD>tm_mad</TM_MAD>"
"<LAST_MON_TIME>0</LAST_MON_TIME><CLUSTER>default</CLUSTER><HOST_SHARE><HID>0</HID>"
"<LAST_MON_TIME>0</LAST_MON_TIME><CLUSTER>default</CLUSTER><HOST_SHARE>"
"<DISK_USAGE>0</DISK_USAGE><MEM_USAGE>0</MEM_USAGE><CPU_USAGE>0</CPU_USAGE>"
"<MAX_DISK>0</MAX_DISK><MAX_MEM>0</MAX_MEM><MAX_CPU>0</MAX_CPU>"
"<FREE_DISK>0</FREE_DISK><FREE_MEM>0</FREE_MEM><FREE_CPU>0</FREE_CPU>"
@ -42,7 +42,7 @@ const string xmls[] =
"<HOST><ID>1</ID><NAME>Second host</NAME><STATE>0</STATE>"
"<IM_MAD>im_mad</IM_MAD><VM_MAD>vmm_mad</VM_MAD><TM_MAD>tm_mad</TM_MAD>"
"<LAST_MON_TIME>0</LAST_MON_TIME><CLUSTER>default</CLUSTER><HOST_SHARE><HID>1</HID>"
"<LAST_MON_TIME>0</LAST_MON_TIME><CLUSTER>default</CLUSTER><HOST_SHARE>"
"<DISK_USAGE>0</DISK_USAGE><MEM_USAGE>0</MEM_USAGE><CPU_USAGE>0</CPU_USAGE>"
"<MAX_DISK>0</MAX_DISK><MAX_MEM>0</MAX_MEM><MAX_CPU>0</MAX_CPU>"
"<FREE_DISK>0</FREE_DISK><FREE_MEM>0</FREE_MEM><FREE_CPU>0</FREE_CPU>"
@ -54,72 +54,72 @@ const string xmls[] =
const string xml_dump =
"<HOST_POOL><HOST><ID>0</ID><NAME>a</NAME><STATE>0</STATE><IM_MAD>im_mad</I"
"M_MAD><VM_MAD>vmm_mad</VM_MAD><TM_MAD>tm_mad</TM_MAD><LAST_MON_TIME>0"
"</LAST_MON_TIME><CLUSTER>default</CLUSTER><TEMPLATE></TEMPLATE><HOST_SHARE><HID>0</HID><DISK_USAGE>0</DISK_USAGE><MEM"
"</LAST_MON_TIME><CLUSTER>default</CLUSTER><HOST_SHARE><DISK_USAGE>0</DISK_USAGE><MEM"
"_USAGE>0</MEM_USAGE><CPU_USAGE>0</CPU_USAGE><MAX_DISK>0</MAX_DISK><MAX_MEM"
">0</MAX_MEM><MAX_CPU>0</MAX_CPU><FREE_DISK>0</FREE_DISK><FREE_MEM>0</FREE_"
"MEM><FREE_CPU>0</FREE_CPU><USED_DISK>0</USED_DISK><USED_MEM>0</USED_MEM><U"
"SED_CPU>0</USED_CPU><RUNNING_VMS>0</RUNNING_VMS></HOST_SHARE></HOST><HOST>"
"SED_CPU>0</USED_CPU><RUNNING_VMS>0</RUNNING_VMS></HOST_SHARE><TEMPLATE></TEMPLATE></HOST><HOST>"
"<ID>1</ID><NAME>a name</NAME><STATE>0</STATE><IM_MAD>im_mad</IM_MAD><VM_MA"
"D>vmm_mad</VM_MAD><TM_MAD>tm_mad</TM_MAD><LAST_MON_TIME>0</LAST_M"
"ON_TIME><CLUSTER>default</CLUSTER><TEMPLATE></TEMPLATE><HOST_SHARE><HID>1</HID><DISK_USAGE>0</DISK_USAGE><MEM_USAGE>0</ME"
"ON_TIME><CLUSTER>default</CLUSTER><HOST_SHARE><DISK_USAGE>0</DISK_USAGE><MEM_USAGE>0</ME"
"M_USAGE><CPU_USAGE>0</CPU_USAGE><MAX_DISK>0</MAX_DISK><MAX_MEM>0</MAX_MEM>"
"<MAX_CPU>0</MAX_CPU><FREE_DISK>0</FREE_DISK><FREE_MEM>0</FREE_MEM><FREE_CP"
"U>0</FREE_CPU><USED_DISK>0</USED_DISK><USED_MEM>0</USED_MEM><USED_CPU>0</U"
"SED_CPU><RUNNING_VMS>0</RUNNING_VMS></HOST_SHARE></HOST><HOST><ID>2</ID><N"
"SED_CPU><RUNNING_VMS>0</RUNNING_VMS></HOST_SHARE><TEMPLATE></TEMPLATE></HOST><HOST><ID>2</ID><N"
"AME>a_name</NAME><STATE>0</STATE><IM_MAD>im_mad</IM_MAD><VM_MAD>vmm_mad</V"
"M_MAD><TM_MAD>tm_mad</TM_MAD><LAST_MON_TIME>0</LAST_MON_TIME><CLUSTER>default</CLUSTER><TEMPLATE></TEMPLATE><HOS"
"T_SHARE><HID>2</HID><DISK_USAGE>0</DISK_USAGE><MEM_USAGE>0</MEM_USAGE><CPU"
"M_MAD><TM_MAD>tm_mad</TM_MAD><LAST_MON_TIME>0</LAST_MON_TIME><CLUSTER>default</CLUSTER><HOS"
"T_SHARE><DISK_USAGE>0</DISK_USAGE><MEM_USAGE>0</MEM_USAGE><CPU"
"_USAGE>0</CPU_USAGE><MAX_DISK>0</MAX_DISK><MAX_MEM>0</MAX_MEM><MAX_CPU>0</"
"MAX_CPU><FREE_DISK>0</FREE_DISK><FREE_MEM>0</FREE_MEM><FREE_CPU>0</FREE_CP"
"U><USED_DISK>0</USED_DISK><USED_MEM>0</USED_MEM><USED_CPU>0</USED_CPU><RUN"
"NING_VMS>0</RUNNING_VMS></HOST_SHARE></HOST><HOST><ID>3</ID><NAME>another "
"NING_VMS>0</RUNNING_VMS></HOST_SHARE><TEMPLATE></TEMPLATE></HOST><HOST><ID>3</ID><NAME>another "
"name</NAME><STATE>0</STATE><IM_MAD>im_mad</IM_MAD><VM_MAD>vmm_mad</VM_MAD>"
"<TM_MAD>tm_mad</TM_MAD><LAST_MON_TIME>0</LAST_MON_TIME><CLUSTER>default</CLUSTER><TEMPLATE></TEMPLATE><HOST_SHAR"
"E><HID>3</HID><DISK_USAGE>0</DISK_USAGE><MEM_USAGE>0</MEM_USAGE><CPU_USAGE"
"<TM_MAD>tm_mad</TM_MAD><LAST_MON_TIME>0</LAST_MON_TIME><CLUSTER>default</CLUSTER><HOST_SHAR"
"E><DISK_USAGE>0</DISK_USAGE><MEM_USAGE>0</MEM_USAGE><CPU_USAGE"
">0</CPU_USAGE><MAX_DISK>0</MAX_DISK><MAX_MEM>0</MAX_MEM><MAX_CPU>0</MAX_CP"
"U><FREE_DISK>0</FREE_DISK><FREE_MEM>0</FREE_MEM><FREE_CPU>0</FREE_CPU><USE"
"D_DISK>0</USED_DISK><USED_MEM>0</USED_MEM><USED_CPU>0</USED_CPU><RUNNING_V"
"MS>0</RUNNING_VMS></HOST_SHARE></HOST><HOST><ID>4</ID><NAME>host</NAME><ST"
"MS>0</RUNNING_VMS></HOST_SHARE><TEMPLATE></TEMPLATE></HOST><HOST><ID>4</ID><NAME>host</NAME><ST"
"ATE>0</STATE><IM_MAD>im_mad</IM_MAD><VM_MAD>vmm_mad</VM_MAD><TM_MAD>tm_mad"
"</TM_MAD><LAST_MON_TIME>0</LAST_MON_TIME><CLUSTER>default</CLUSTER><TEMPLATE></TEMPLATE><HOST_SHARE><HID>4</HID>"
"</TM_MAD><LAST_MON_TIME>0</LAST_MON_TIME><CLUSTER>default</CLUSTER><HOST_SHARE>"
"<DISK_USAGE>0</DISK_USAGE><MEM_USAGE>0</MEM_USAGE><CPU_USAGE>0</CPU_USAGE>"
"<MAX_DISK>0</MAX_DISK><MAX_MEM>0</MAX_MEM><MAX_CPU>0</MAX_CPU><FREE_DISK>0"
"</FREE_DISK><FREE_MEM>0</FREE_MEM><FREE_CPU>0</FREE_CPU><USED_DISK>0</USED"
"_DISK><USED_MEM>0</USED_MEM><USED_CPU>0</USED_CPU><RUNNING_VMS>0</RUNNING_"
"VMS></HOST_SHARE></HOST></HOST_POOL>";
"VMS></HOST_SHARE><TEMPLATE></TEMPLATE></HOST></HOST_POOL>";
const string xml_dump_like_a =
"<HOST_POOL><HOST><ID>0</ID><NAME>a</NAME><STATE>0</STATE><IM_MAD>im_mad</I"
"M_MAD><VM_MAD>vmm_mad</VM_MAD><TM_MAD>tm_mad</TM_MAD><LAST_MON_TIME>0"
"</LAST_MON_TIME><CLUSTER>default</CLUSTER><TEMPLATE></TEMPLATE><HOST_SHARE><HID>0</HID><DISK_USAGE>0</DISK_USAGE><MEM"
"</LAST_MON_TIME><CLUSTER>default</CLUSTER><HOST_SHARE><DISK_USAGE>0</DISK_USAGE><MEM"
"_USAGE>0</MEM_USAGE><CPU_USAGE>0</CPU_USAGE><MAX_DISK>0</MAX_DISK><MAX_MEM"
">0</MAX_MEM><MAX_CPU>0</MAX_CPU><FREE_DISK>0</FREE_DISK><FREE_MEM>0</FREE_"
"MEM><FREE_CPU>0</FREE_CPU><USED_DISK>0</USED_DISK><USED_MEM>0</USED_MEM><U"
"SED_CPU>0</USED_CPU><RUNNING_VMS>0</RUNNING_VMS></HOST_SHARE></HOST><HOST>"
"SED_CPU>0</USED_CPU><RUNNING_VMS>0</RUNNING_VMS></HOST_SHARE><TEMPLATE></TEMPLATE></HOST><HOST>"
"<ID>1</ID><NAME>a name</NAME><STATE>0</STATE><IM_MAD>im_mad</IM_MAD><VM_MA"
"D>vmm_mad</VM_MAD><TM_MAD>tm_mad</TM_MAD><LAST_MON_TIME>0</LAST_M"
"ON_TIME><CLUSTER>default</CLUSTER><TEMPLATE></TEMPLATE><HOST_SHARE><HID>1</HID><DISK_USAGE>0</DISK_USAGE><MEM_USAGE>0</ME"
"ON_TIME><CLUSTER>default</CLUSTER><HOST_SHARE><DISK_USAGE>0</DISK_USAGE><MEM_USAGE>0</ME"
"M_USAGE><CPU_USAGE>0</CPU_USAGE><MAX_DISK>0</MAX_DISK><MAX_MEM>0</MAX_MEM>"
"<MAX_CPU>0</MAX_CPU><FREE_DISK>0</FREE_DISK><FREE_MEM>0</FREE_MEM><FREE_CP"
"U>0</FREE_CPU><USED_DISK>0</USED_DISK><USED_MEM>0</USED_MEM><USED_CPU>0</U"
"SED_CPU><RUNNING_VMS>0</RUNNING_VMS></HOST_SHARE></HOST><HOST><ID>2</ID><N"
"SED_CPU><RUNNING_VMS>0</RUNNING_VMS></HOST_SHARE><TEMPLATE></TEMPLATE></HOST><HOST><ID>2</ID><N"
"AME>a_name</NAME><STATE>0</STATE><IM_MAD>im_mad</IM_MAD><VM_MAD>vmm_mad</V"
"M_MAD><TM_MAD>tm_mad</TM_MAD><LAST_MON_TIME>0</LAST_MON_TIME><CLUSTER>default</CLUSTER><TEMPLATE></TEMPLATE><HOS"
"T_SHARE><HID>2</HID><DISK_USAGE>0</DISK_USAGE><MEM_USAGE>0</MEM_USAGE><CPU"
"M_MAD><TM_MAD>tm_mad</TM_MAD><LAST_MON_TIME>0</LAST_MON_TIME><CLUSTER>default</CLUSTER><HOS"
"T_SHARE><DISK_USAGE>0</DISK_USAGE><MEM_USAGE>0</MEM_USAGE><CPU"
"_USAGE>0</CPU_USAGE><MAX_DISK>0</MAX_DISK><MAX_MEM>0</MAX_MEM><MAX_CPU>0</"
"MAX_CPU><FREE_DISK>0</FREE_DISK><FREE_MEM>0</FREE_MEM><FREE_CPU>0</FREE_CP"
"U><USED_DISK>0</USED_DISK><USED_MEM>0</USED_MEM><USED_CPU>0</USED_CPU><RUN"
"NING_VMS>0</RUNNING_VMS></HOST_SHARE></HOST><HOST><ID>3</ID><NAME>another "
"NING_VMS>0</RUNNING_VMS></HOST_SHARE><TEMPLATE></TEMPLATE></HOST><HOST><ID>3</ID><NAME>another "
"name</NAME><STATE>0</STATE><IM_MAD>im_mad</IM_MAD><VM_MAD>vmm_mad</VM_MAD>"
"<TM_MAD>tm_mad</TM_MAD><LAST_MON_TIME>0</LAST_MON_TIME><CLUSTER>default</CLUSTER><TEMPLATE></TEMPLATE><HOST_SHAR"
"E><HID>3</HID><DISK_USAGE>0</DISK_USAGE><MEM_USAGE>0</MEM_USAGE><CPU_USAGE"
"<TM_MAD>tm_mad</TM_MAD><LAST_MON_TIME>0</LAST_MON_TIME><CLUSTER>default</CLUSTER><HOST_SHAR"
"E><DISK_USAGE>0</DISK_USAGE><MEM_USAGE>0</MEM_USAGE><CPU_USAGE"
">0</CPU_USAGE><MAX_DISK>0</MAX_DISK><MAX_MEM>0</MAX_MEM><MAX_CPU>0</MAX_CP"
"U><FREE_DISK>0</FREE_DISK><FREE_MEM>0</FREE_MEM><FREE_CPU>0</FREE_CPU><USE"
"D_DISK>0</USED_DISK><USED_MEM>0</USED_MEM><USED_CPU>0</USED_CPU><RUNNING_V"
"MS>0</RUNNING_VMS></HOST_SHARE></HOST></HOST_POOL>";
"MS>0</RUNNING_VMS></HOST_SHARE><TEMPLATE></TEMPLATE></HOST></HOST_POOL>";
const string host0_updated =
"<HOST><ID>0</ID><NAME>Host one</NAME><STATE>0</STATE><IM_MAD>im_mad</IM_MAD><VM_MAD>vmm_mad</VM_MAD><TM_MAD>tm_mad</TM_MAD><LAST_MON_TIME>0</LAST_MON_TIME><CLUSTER>default</CLUSTER><HOST_SHARE><HID>0</HID><DISK_USAGE>0</DISK_USAGE><MEM_USAGE>0</MEM_USAGE><CPU_USAGE>0</CPU_USAGE><MAX_DISK>0</MAX_DISK><MAX_MEM>0</MAX_MEM><MAX_CPU>0</MAX_CPU><FREE_DISK>0</FREE_DISK><FREE_MEM>0</FREE_MEM><FREE_CPU>0</FREE_CPU><USED_DISK>0</USED_DISK><USED_MEM>0</USED_MEM><USED_CPU>0</USED_CPU><RUNNING_VMS>0</RUNNING_VMS></HOST_SHARE><TEMPLATE><ATT_A><![CDATA[VALUE_A]]></ATT_A><ATT_B><![CDATA[VALUE_B]]></ATT_B></TEMPLATE></HOST>";
"<HOST><ID>0</ID><NAME>Host one</NAME><STATE>0</STATE><IM_MAD>im_mad</IM_MAD><VM_MAD>vmm_mad</VM_MAD><TM_MAD>tm_mad</TM_MAD><LAST_MON_TIME>0</LAST_MON_TIME><CLUSTER>default</CLUSTER><HOST_SHARE><DISK_USAGE>0</DISK_USAGE><MEM_USAGE>0</MEM_USAGE><CPU_USAGE>0</CPU_USAGE><MAX_DISK>0</MAX_DISK><MAX_MEM>0</MAX_MEM><MAX_CPU>0</MAX_CPU><FREE_DISK>0</FREE_DISK><FREE_MEM>0</FREE_MEM><FREE_CPU>0</FREE_CPU><USED_DISK>0</USED_DISK><USED_MEM>0</USED_MEM><USED_CPU>0</USED_CPU><RUNNING_VMS>0</RUNNING_VMS></HOST_SHARE><TEMPLATE><ATT_A><![CDATA[VALUE_A]]></ATT_A><ATT_B><![CDATA[VALUE_B]]></ATT_B></TEMPLATE></HOST>";
const string cluster_default =
"<CLUSTER><ID>0</ID><NAME>default</NAME></CLUSTER>";
@ -128,7 +128,7 @@ const string cluster_xml_dump =
"<CLUSTER_POOL><CLUSTER><ID>0</ID><NAME>default</NAME></CLUSTER><CLUSTER><ID>1</ID><NAME>cluster_a</NAME></CLUSTER><CLUSTER><ID>3</ID><NAME>cluster_c</NAME></CLUSTER><CLUSTER><ID>4</ID><NAME>cluster_d</NAME></CLUSTER></CLUSTER_POOL>";
const string host_0_cluster =
"<HOST><ID>0</ID><NAME>Host one</NAME><STATE>0</STATE><IM_MAD>im_mad</IM_MAD><VM_MAD>vmm_mad</VM_MAD><TM_MAD>tm_mad</TM_MAD><LAST_MON_TIME>0</LAST_MON_TIME><CLUSTER>cluster_a</CLUSTER><HOST_SHARE><HID>0</HID><DISK_USAGE>0</DISK_USAGE><MEM_USAGE>0</MEM_USAGE><CPU_USAGE>0</CPU_USAGE><MAX_DISK>0</MAX_DISK><MAX_MEM>0</MAX_MEM><MAX_CPU>0</MAX_CPU><FREE_DISK>0</FREE_DISK><FREE_MEM>0</FREE_MEM><FREE_CPU>0</FREE_CPU><USED_DISK>0</USED_DISK><USED_MEM>0</USED_MEM><USED_CPU>0</USED_CPU><RUNNING_VMS>0</RUNNING_VMS></HOST_SHARE><TEMPLATE></TEMPLATE></HOST>";
"<HOST><ID>0</ID><NAME>Host one</NAME><STATE>0</STATE><IM_MAD>im_mad</IM_MAD><VM_MAD>vmm_mad</VM_MAD><TM_MAD>tm_mad</TM_MAD><LAST_MON_TIME>0</LAST_MON_TIME><CLUSTER>cluster_a</CLUSTER><HOST_SHARE><DISK_USAGE>0</DISK_USAGE><MEM_USAGE>0</MEM_USAGE><CPU_USAGE>0</CPU_USAGE><MAX_DISK>0</MAX_DISK><MAX_MEM>0</MAX_MEM><MAX_CPU>0</MAX_CPU><FREE_DISK>0</FREE_DISK><FREE_MEM>0</FREE_MEM><FREE_CPU>0</FREE_CPU><USED_DISK>0</USED_DISK><USED_MEM>0</USED_MEM><USED_CPU>0</USED_CPU><RUNNING_VMS>0</RUNNING_VMS></HOST_SHARE><TEMPLATE></TEMPLATE></HOST>";
/* ************************************************************************* */
/* ************************************************************************* */
@ -143,16 +143,10 @@ class HostPoolTest : public PoolTest
CPPUNIT_TEST (dump_where);
CPPUNIT_TEST (discover);
CPPUNIT_TEST (duplicates);
CPPUNIT_TEST (cluster_init);
CPPUNIT_TEST (cluster_allocate);
CPPUNIT_TEST (cluster_drop);
CPPUNIT_TEST (cluster_id);
CPPUNIT_TEST (cluster_dump);
CPPUNIT_TEST (set_cluster);
CPPUNIT_TEST (remove_cluster);
CPPUNIT_TEST (update_info);
// CPPUNIT_TEST (scale_test);
CPPUNIT_TEST_SUITE_END ();
protected:
@ -184,12 +178,21 @@ protected:
CPPUNIT_ASSERT( obj != 0 );
string xml_str = "";
string name = host->get_hostname();
string name = host->get_name();
CPPUNIT_ASSERT( name == names[index] );
// Get the xml
host->to_xml(xml_str);
// A little help for debugging
/*
if( xml_str != xmls[index] )
{
cout << endl << xml_str << endl << "========"
<< endl << xmls[index];
}
//*/
CPPUNIT_ASSERT( xml_str == xmls[index]);
};
@ -285,6 +288,15 @@ public:
string result = oss.str();
// A little help for debugging
/*
if( result != xml_dump )
{
cout << endl << result << endl << "========"
<< endl << xml_dump;
}
//*/
CPPUNIT_ASSERT( result == xml_dump );
}
@ -304,12 +316,21 @@ public:
ostringstream oss;
rc = ((HostPool*)pool)->dump(oss, "host_name LIKE 'a%'");
rc = ((HostPool*)pool)->dump(oss, "name LIKE 'a%' ORDER BY oid");
CPPUNIT_ASSERT(rc == 0);
string result = oss.str();
// A little help for debugging
/*
if( result != xml_dump_like_a )
{
cout << endl << result << endl << "========"
<< endl << xml_dump_like_a;
}
//*/
CPPUNIT_ASSERT( result == xml_dump_like_a );
}
@ -360,201 +381,87 @@ public:
}
}
/* ********************************************************************* */
/* ********************************************************************* */
void cluster_init()
void scale_test()
{
time_t the_time, the_time2;
int oid,i,j,rc;
ostringstream oss,ossdump;
string err;
Host * host;
string monitor = "ARCH=x86_64 MODELNAME=\"Intel(R) Core(TM)2 Duo CPU P9300 @ 2.26GHz\" HYPERVISOR=kvm TOTALCPU=200 CPUSPEED=800 TOTALMEMORY=4005416 USEDMEMORY=2351928 FREEMEMORY=2826904 FREECPU=188.4 USEDCPU=11.599999999999994 NETRX=0 NETTX=0 HOSTNAME=pc-ruben";
cout << endl << "Allocate Test" << endl;
tearDown();
for (i=1000; i<30000 ; i = i + 5000)
{
setUp();
HostPool * hp = static_cast<HostPool *>(pool);
the_time = time(0);
for (j=0,oss.str(""); j<i ; j=j+1,oss.str(""))
{
oss << "host" << j;
hp->allocate(&oid, oss.str().c_str(),im_mad,vmm_mad,tm_mad,err);
}
the_time2 = time(0) - the_time;
hp->clean();
the_time = time(0);
rc = hp->dump(ossdump, "");
cout <<"\t"<<i<<"\t"<<the_time2<<"\t"<<time(0)-the_time<< endl;
tearDown();
}
cout << endl << "Read Test" << endl;
setUp();
// Allocate a HostPool
setUp();
HostPool * hp = static_cast<HostPool *>(pool);
CPPUNIT_ASSERT( hp->info_cluster(0) == cluster_default );
}
for (i=10000,oss.str(""); i<30000 ; i++,oss.str(""))
{
oss << "host" << i;
hp->allocate(&oid,oss.str().c_str(),im_mad,vmm_mad,tm_mad,err);
/* ********************************************************************* */
host = hp->get(oid, false);
void cluster_allocate()
{
HostPool * hp = static_cast<HostPool *>(pool);
int clid, rc;
string err;
host->update_info(monitor);
hp->update(host);
}
//Load test
for (i=0; i<25000; i=i+5000)
{
hp->clean();
rc = hp->allocate_cluster(&clid, "new_cluster", err);
CPPUNIT_ASSERT( rc == clid );
CPPUNIT_ASSERT( clid == 1 );
the_time = time(0);
CPPUNIT_ASSERT( hp->info_cluster(clid) ==
"<CLUSTER><ID>1</ID><NAME>new_cluster</NAME></CLUSTER>");
for (j=0; j<i ; j++)
{
host = hp->get(j,true);
host->unlock();
}
// Try to allocate using the same name
rc = hp->allocate_cluster(&clid, "new_cluster", err);
CPPUNIT_ASSERT( rc == clid );
CPPUNIT_ASSERT( clid == -1 );
}
/* ********************************************************************* */
void cluster_drop()
{
HostPool * hp = static_cast<HostPool *>(pool);
int clid, rc;
string err;
// Drop a non-existing cluster
rc = hp->drop_cluster(20);
CPPUNIT_ASSERT( rc == -1 );
// Allocate a cluster and drop it
rc = hp->allocate_cluster(&clid, "new_cluster", err);
CPPUNIT_ASSERT( clid == 1);
rc = hp->drop_cluster(clid);
CPPUNIT_ASSERT( rc == 0 );
// Try to drop the default cluster, should fail
rc = hp->drop_cluster(0);
CPPUNIT_ASSERT( rc == -1 );
}
/* ********************************************************************* */
void cluster_id()
{
HostPool * hp = static_cast<HostPool *>(pool);
int clid, rc;
ostringstream oss;
string err;
// Allocate some clusters
rc = hp->allocate_cluster(&clid, "cluster_a", err);
CPPUNIT_ASSERT( rc == 1 );
rc = hp->allocate_cluster(&clid, "cluster_b", err);
CPPUNIT_ASSERT( rc == 2 );
rc = hp->allocate_cluster(&clid, "cluster_c", err);
CPPUNIT_ASSERT( rc == 3 );
rc = hp->allocate_cluster(&clid, "cluster_d", err);
CPPUNIT_ASSERT( rc == 4 );
// Drop id 2
rc = hp->drop_cluster(2);
CPPUNIT_ASSERT( rc == 0 );
// Next one should use id 5, because the biggest id is 4
rc = hp->allocate_cluster(&clid, "cluster_e", err);
CPPUNIT_ASSERT( rc == 5 );
// Drop id 5
rc = hp->drop_cluster(5);
CPPUNIT_ASSERT( rc == 0 );
// Next one should use id 5, because the biggest id is 4 again
rc = hp->allocate_cluster(&clid, "cluster_f", err);
CPPUNIT_ASSERT( rc == 5 );
}
/* ********************************************************************* */
void cluster_dump()
{
HostPool * hp = static_cast<HostPool *>(pool);
int clid, rc;
ostringstream oss;
string err;
// Allocate some clusters
rc = hp->allocate_cluster(&clid, "cluster_a", err);
CPPUNIT_ASSERT( rc == 1 );
rc = hp->allocate_cluster(&clid, "cluster_b", err);
CPPUNIT_ASSERT( rc == 2 );
rc = hp->allocate_cluster(&clid, "cluster_c", err);
CPPUNIT_ASSERT( rc == 3 );
rc = hp->allocate_cluster(&clid, "cluster_d", err);
CPPUNIT_ASSERT( rc == 4 );
// Drop one of them
rc = hp->drop_cluster(2);
CPPUNIT_ASSERT( rc == 0 );
// dump the pool
rc = hp->dump_cluster(oss);
CPPUNIT_ASSERT( oss.str() == cluster_xml_dump );
}
/* ********************************************************************* */
void set_cluster()
{
HostPool * hp = static_cast<HostPool *>(pool);
Host* host;
int clid, rc, oid;
string xml_str, err;
// Allocate a host
oid = allocate(0);
CPPUNIT_ASSERT(oid >= 0);
host = hp->get(0, false);
CPPUNIT_ASSERT(host != 0);
rc = hp->allocate_cluster(&clid, "cluster_a", err);
CPPUNIT_ASSERT( rc == 1 );
rc = hp->set_cluster(host, clid);
CPPUNIT_ASSERT( rc == 0 );
host->to_xml(xml_str);
CPPUNIT_ASSERT( xml_str == host_0_cluster);
// Try to set a non-existing cluster
rc = hp->set_cluster(host, 20);
CPPUNIT_ASSERT( rc == -1 );
CPPUNIT_ASSERT( xml_str == host_0_cluster);
}
/* ********************************************************************* */
void remove_cluster()
{
HostPool * hp = static_cast<HostPool *>(pool);
Host* host;
int clid, rc, oid;
string xml_str, err;
// Allocate a host
oid = allocate(0);
CPPUNIT_ASSERT(oid >= 0);
host = hp->get(0, false);
CPPUNIT_ASSERT(host != 0);
rc = hp->allocate_cluster(&clid, "cluster_a", err);
CPPUNIT_ASSERT( rc == 1 );
// Set host 0 to cluster 1
rc = hp->set_cluster(host, clid);
CPPUNIT_ASSERT( rc == 0 );
// Check
host->to_xml(xml_str);
CPPUNIT_ASSERT( xml_str == host_0_cluster);
// Remove the cluster
rc = hp->set_default_cluster(host);
CPPUNIT_ASSERT( rc == 0 );
// The host should have been moved to the default cluster
host->to_xml(xml_str);
check(0, host);
cout << "\t" << i << "\t" << time(0) - the_time << endl;
}
}
/* ********************************************************************* */

View File

@ -21,6 +21,8 @@ Import('env')
env.Prepend(LIBS=[
'nebula_core_test',
'nebula_host',
'nebula_cluster',
'nebula_xml',
'nebula_vmm',
'nebula_im',
'nebula_rm',

View File

@ -201,7 +201,7 @@ void InformationManager::timer_action()
(thetime - host->get_last_monitored() >= monitor_period))
{
oss.str("");
oss << "Monitoring host " << host->get_hostname()
oss << "Monitoring host " << host->get_name()
<< " (" << it->first << ")";
NebulaLog::log("InM",Log::INFO,oss);
@ -225,7 +225,7 @@ void InformationManager::timer_action()
update_remotes = true;
}
imd->monitor(it->first,host->get_hostname(),update_remotes);
imd->monitor(it->first,host->get_name(),update_remotes);
host->set_state(Host::MONITORING);
}

View File

@ -28,14 +28,17 @@
#include "AuthManager.h"
#include "UserPool.h"
#define TO_UPPER(S) transform(S.begin(),S.end(),S.begin(),(int(*)(int))toupper)
/* ************************************************************************ */
/* Image :: Constructor/Destructor */
/* Image :: Constructor/Destructor */
/* ************************************************************************ */
Image::Image(int _uid, ImageTemplate * _image_template):
PoolObjectSQL(-1),
uid(_uid),
name(""),
Image::Image(int _uid,
const string& _user_name,
ImageTemplate * _image_template):
PoolObjectSQL(-1,"",_uid,table),
user_name(_user_name),
type(OS),
regtime(time(0)),
source(""),
@ -61,92 +64,16 @@ Image::~Image()
}
/* ************************************************************************ */
/* Image :: Database Access Functions */
/* Image :: Database Access Functions */
/* ************************************************************************ */
const char * Image::table = "image_pool";
const char * Image::db_names = "oid, uid, name, type, public, persistent, regtime, "
"source, state, running_vms, template";
const char * Image::extended_db_names = "image_pool.oid, image_pool.uid, "
"image_pool.name, image_pool.type, image_pool.public, "
"image_pool.persistent, image_pool.regtime, image_pool.source, "
"image_pool.state, image_pool.running_vms, image_pool.template";
const char * Image::db_names = "oid, name, body, uid, public";
const char * Image::db_bootstrap = "CREATE TABLE IF NOT EXISTS image_pool ("
"oid INTEGER PRIMARY KEY, uid INTEGER, name VARCHAR(128), "
"type INTEGER, public INTEGER, persistent INTEGER, regtime INTEGER, source TEXT, state INTEGER, "
"running_vms INTEGER, template TEXT, UNIQUE(name) )";
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
int Image::select_cb(void * nil, int num, char **values, char ** names)
{
if ((!values[OID]) ||
(!values[UID]) ||
(!values[NAME]) ||
(!values[TYPE]) ||
(!values[PUBLIC]) ||
(!values[PERSISTENT]) ||
(!values[REGTIME]) ||
(!values[SOURCE]) ||
(!values[STATE]) ||
(!values[RUNNING_VMS]) ||
(!values[TEMPLATE]) ||
(num != LIMIT ))
{
return -1;
}
oid = atoi(values[OID]);
uid = atoi(values[UID]);
name = values[NAME];
type = static_cast<ImageType>(atoi(values[TYPE]));
public_img = atoi(values[PUBLIC]);
persistent_img = atoi(values[PERSISTENT]);
regtime = static_cast<time_t>(atoi(values[REGTIME]));
source = values[SOURCE];
state = static_cast<ImageState>(atoi(values[STATE]));
running_vms = atoi(values[RUNNING_VMS]);
image_template->from_xml(values[TEMPLATE]);
return 0;
}
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
int Image::select(SqlDB *db)
{
ostringstream oss;
int rc;
int boid;
set_callback(static_cast<Callbackable::Callback>(&Image::select_cb));
oss << "SELECT " << db_names << " FROM " << table << " WHERE oid = " << oid;
boid = oid;
oid = -1;
rc = db->exec(oss, this);
if ((rc != 0) || (oid != boid ))
{
return -1;
}
return 0;
}
"oid INTEGER PRIMARY KEY, name VARCHAR(256), body TEXT, uid INTEGER, "
"public INTEGER, UNIQUE(name,uid) )";
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
@ -155,11 +82,11 @@ int Image::insert(SqlDB *db, string& error_str)
{
int rc;
string source_att;
string type_att;
string public_attr;
string persistent_attr;
string dev_prefix;
string path_attr;
string type_att;
string public_attr;
string persistent_attr;
string dev_prefix;
// ---------------------------------------------------------------------
// Check default image attributes
@ -169,17 +96,11 @@ int Image::insert(SqlDB *db, string& error_str)
get_template_attribute("NAME", name);
if ( name.empty() == true )
{
goto error_name;
}
// ------------ TYPE --------------------
get_template_attribute("TYPE", type_att);
transform (type_att.begin(), type_att.end(), type_att.begin(),
(int(*)(int))toupper);
TO_UPPER(type_att);
if ( type_att.empty() == true )
{
@ -194,20 +115,20 @@ int Image::insert(SqlDB *db, string& error_str)
// ------------ PUBLIC --------------------
get_template_attribute("PUBLIC", public_attr);
image_template->erase("PUBLIC");
transform (public_attr.begin(), public_attr.end(), public_attr.begin(),
(int(*)(int))toupper);
TO_UPPER(public_attr);
public_img = (public_attr == "YES");
// ------------ PERSISTENT --------------------
get_template_attribute("PERSISTENT", persistent_attr);
image_template->erase("PERSISTENT");
transform (persistent_attr.begin(), persistent_attr.end(), persistent_attr.begin(),
(int(*)(int))toupper);
TO_UPPER(persistent_attr);
persistent_img = (persistent_attr == "YES");
@ -230,10 +151,32 @@ int Image::insert(SqlDB *db, string& error_str)
image_template->set(dev_att);
}
// ------------ SOURCE (path to store the image)--------------------
// ------------ PATH --------------------
get_template_attribute("PATH", path_attr);
// ------------ SOURCE (path to store the image) --------------------
get_template_attribute("SOURCE", source);
// The template should contain PATH or SOURCE
if ( source.empty() && path_attr.empty() )
{
string size_attr;
string fstype_attr;
get_template_attribute("SIZE", size_attr);
get_template_attribute("FSTYPE", fstype_attr);
// It could be an empty DATABLOCK image, if it declares SIZE and FSTYPE
if ( type_att != "DATABLOCK" || size_attr.empty() || fstype_attr.empty() )
{
goto error_no_path;
}
}
else if ( !source.empty() && !path_attr.empty() )
{
goto error_path_and_source;
}
if (source.empty())
{
ostringstream tmp_hashstream;
@ -262,18 +205,30 @@ int Image::insert(SqlDB *db, string& error_str)
return rc;
error_name:
error_str = "NAME not present in image template.";
goto error_common;
error_type:
error_str = "Incorrect TYPE in image template.";
error_str = "Incorrect TYPE in template.";
goto error_common;
error_public_and_persistent:
error_str = "Image cannot be public and persistent.";
goto error_common;
error_no_path:
if ( type_att == "DATABLOCK" )
{
error_str = "A DATABLOCK type IMAGE has to declare a PATH, or both "
"SIZE and FSTYPE.";
}
else
{
error_str = "No PATH in template.";
}
goto error_common;
error_path_and_source:
error_str = "Template malformed, PATH and SOURCE are mutuallly exclusive.";
goto error_common;
error_common:
NebulaLog::log("IMG", Log::ERROR, error_str);
return -1;
@ -296,11 +251,10 @@ int Image::insert_replace(SqlDB *db, bool replace)
int rc;
string xml_template;
string xml_body;
char * sql_name;
char * sql_source;
char * sql_template;
char * sql_xml;
// Update the Image
@ -311,19 +265,11 @@ int Image::insert_replace(SqlDB *db, bool replace)
goto error_name;
}
sql_source = db->escape_str(source.c_str());
sql_xml = db->escape_str(to_xml(xml_body).c_str());
if ( sql_source == 0 )
if ( sql_xml == 0 )
{
goto error_source;
}
image_template->to_xml(xml_template);
sql_template = db->escape_str(xml_template.c_str());
if ( sql_template == 0 )
{
goto error_template;
goto error_body;
}
if(replace)
@ -339,100 +285,24 @@ int Image::insert_replace(SqlDB *db, bool replace)
oss <<" INTO "<< table <<" ("<< db_names <<") VALUES ("
<< oid << ","
<< uid << ","
<< "'" << sql_name << "',"
<< type << ","
<< public_img << ","
<< persistent_img << ","
<< regtime << ","
<< "'" << sql_source << "',"
<< state << ","
<< running_vms << ","
<< "'" << sql_template << "')";
<< "'" << sql_xml << "',"
<< uid << ","
<< public_img << ")";
rc = db->exec(oss);
db->free_str(sql_name);
db->free_str(sql_source);
db->free_str(sql_template);
db->free_str(sql_xml);
return rc;
error_template:
db->free_str(sql_source);
error_source:
error_body:
db->free_str(sql_name);
error_name:
return -1;
}
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
int Image::dump(ostringstream& oss, int num, char **values, char **names)
{
if ((!values[OID]) ||
(!values[UID]) ||
(!values[NAME]) ||
(!values[TYPE]) ||
(!values[PUBLIC]) ||
(!values[PERSISTENT]) ||
(!values[REGTIME]) ||
(!values[SOURCE]) ||
(!values[STATE]) ||
(!values[RUNNING_VMS]) ||
(!values[TEMPLATE]) ||
(num != LIMIT + 1))
{
return -1;
}
oss <<
"<IMAGE>" <<
"<ID>" << values[OID] << "</ID>" <<
"<UID>" << values[UID] << "</UID>" <<
"<USERNAME>" << values[LIMIT] << "</USERNAME>" <<
"<NAME>" << values[NAME] << "</NAME>" <<
"<TYPE>" << values[TYPE] << "</TYPE>" <<
"<PUBLIC>" << values[PUBLIC] << "</PUBLIC>" <<
"<PERSISTENT>" << values[PERSISTENT] << "</PERSISTENT>" <<
"<REGTIME>" << values[REGTIME] << "</REGTIME>" <<
"<SOURCE>" << values[SOURCE] << "</SOURCE>" <<
"<STATE>" << values[STATE] << "</STATE>" <<
"<RUNNING_VMS>" << values[RUNNING_VMS] << "</RUNNING_VMS>" <<
values[TEMPLATE] <<
"</IMAGE>";
return 0;
}
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
int Image::drop(SqlDB * db)
{
ostringstream oss;
int rc;
// Only delete the VM
if (running_vms != 0)
{
return -1;
}
oss << "DELETE FROM " << table << " WHERE oid=" << oid;
rc = db->exec(oss);
if ( rc == 0 )
{
set_valid(false);
}
return rc;
}
/* ************************************************************************ */
/* Image :: Misc */
/* ************************************************************************ */
@ -458,6 +328,7 @@ string& Image::to_xml(string& xml) const
"<IMAGE>" <<
"<ID>" << oid << "</ID>" <<
"<UID>" << uid << "</UID>" <<
"<USERNAME>" << user_name << "</USERNAME>" <<
"<NAME>" << name << "</NAME>" <<
"<TYPE>" << type << "</TYPE>" <<
"<PUBLIC>" << public_img << "</PUBLIC>" <<
@ -477,30 +348,50 @@ string& Image::to_xml(string& xml) const
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
string& Image::to_str(string& str) const
int Image::from_xml(const string& xml)
{
string template_str;
vector<xmlNodePtr> content;
int int_state;
int int_type;
ostringstream os;
int rc = 0;
os <<
"ID = " << oid << endl <<
"UID = " << uid << endl <<
"NAME = " << name << endl <<
"TYPE = " << type << endl <<
"PUBLIC = " << public_img << endl <<
"PERSISTENT = " << persistent_img << endl <<
"REGTIME = " << regtime << endl <<
"SOURCE = " << source << endl <<
"STATE = " << state << endl <<
"RUNNING_VMS = " << running_vms << endl <<
"TEMPLATE" << endl
<< image_template->to_str(template_str)
<< endl;
// Initialize the internal XML object
update_from_str(xml);
str = os.str();
// Get class base attributes
rc += xpath(oid, "/IMAGE/ID", -1);
rc += xpath(uid, "/IMAGE/UID", -1);
rc += xpath(user_name, "/IMAGE/USERNAME", "not_found");
rc += xpath(name, "/IMAGE/NAME", "not_found");
return str;
rc += xpath(int_type, "/IMAGE/TYPE", 0);
rc += xpath(public_img, "/IMAGE/PUBLIC", 0);
rc += xpath(persistent_img, "/IMAGE/PERSISTENT", 0);
rc += xpath(regtime, "/IMAGE/REGTIME", 0);
rc += xpath(source, "/IMAGE/SOURCE", "not_found");
rc += xpath(int_state, "/IMAGE/STATE", 0);
rc += xpath(running_vms, "/IMAGE/RUNNING_VMS", -1);
type = static_cast<ImageType>(int_type);
state = static_cast<ImageState>(int_state);
// Get associated classes
ObjectXML::get_nodes("/IMAGE/TEMPLATE", content);
if( content.size() < 1 )
{
return -1;
}
rc += image_template->from_xml_node(content[0]);
if (rc != 0)
{
return -1;
}
return 0;
}
/* ------------------------------------------------------------------------ */

View File

@ -27,61 +27,30 @@ string ImagePool::_source_prefix;
string ImagePool::_default_type;
string ImagePool::_default_dev_prefix;
int ImagePool::init_cb(void *nil, int num, char **values, char **names)
{
if ( num == 0 || values == 0 || values[0] == 0 )
{
return -1;
}
image_names.insert(make_pair(values[1],atoi(values[0])));
return 0;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
ImagePool::ImagePool( SqlDB * db,
const string& __source_prefix,
const string& __default_type,
const string& __default_dev_prefix):
ImagePool::ImagePool(SqlDB * db,
const string& __source_prefix,
const string& __default_type,
const string& __default_dev_prefix):
PoolSQL(db,Image::table)
{
ostringstream sql;
int rc;
ostringstream sql;
// Init static defaults
_source_prefix = __source_prefix;
_default_type = __default_type;
_default_dev_prefix = __default_dev_prefix;
_source_prefix = __source_prefix;
_default_type = __default_type;
_default_dev_prefix = __default_dev_prefix;
// Set default type
if (_default_type != "OS" &&
_default_type != "CDROM" &&
_default_type != "DATABLOCK" )
{
NebulaLog::log("IMG", Log::ERROR,
"Bad default for image type, setting OS");
NebulaLog::log("IMG", Log::ERROR, "Bad default for type, setting OS");
_default_type = "OS";
}
// Read from the DB the existing images, and build the ID:Name map
set_callback(static_cast<Callbackable::Callback>(&ImagePool::init_cb));
sql << "SELECT oid, name FROM " << Image::table;
rc = db->exec(sql, this);
unset_callback();
if ( rc != 0 )
{
NebulaLog::log("IMG", Log::ERROR,
"Could not load the existing images from the DB.");
}
}
/* -------------------------------------------------------------------------- */
@ -89,32 +58,57 @@ ImagePool::ImagePool( SqlDB * db,
int ImagePool::allocate (
int uid,
string user_name,
ImageTemplate* img_template,
int * oid,
string& error_str)
{
Image * img;
string name;
Image * img;
Image * img_aux;
string name;
ostringstream oss;
// ---------------------------------------------------------------------
// Build a new Image object
// ---------------------------------------------------------------------
img = new Image(uid,img_template);
img = new Image(uid, user_name, img_template);
// Check name
img->get_template_attribute("NAME", name);
if ( name.empty() )
{
goto error_name;
}
// Check for duplicates
img_aux = get(name,uid,false);
if( img_aux != 0 )
{
goto error_duplicated;
}
// ---------------------------------------------------------------------
// Insert the Object in the pool
// ---------------------------------------------------------------------
*oid = PoolSQL::allocate(img, error_str);
// ---------------------------------------------------------------------
// Add the image name to the map of image_names
// ---------------------------------------------------------------------
if ( *oid != -1 )
{
image_names.insert(make_pair(name, *oid));
}
return *oid;
error_name:
oss << "NAME cannot be empty.";
goto error_common;
error_duplicated:
oss << "NAME is already taken by IMAGE " << img_aux->get_oid() << ".";
error_common:
delete img;
*oid = -1;
error_str = oss.str();
return *oid;
}
@ -122,54 +116,11 @@ int ImagePool::allocate (
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int ImagePool::dump_cb(void * _oss, int num, char **values, char **names)
{
ostringstream * oss;
oss = static_cast<ostringstream *>(_oss);
return Image::dump(*oss, num, values, names);
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int ImagePool::dump(ostringstream& oss, const string& where)
{
int rc;
ostringstream cmd;
oss << "<IMAGE_POOL>";
set_callback(static_cast<Callbackable::Callback>(&ImagePool::dump_cb),
static_cast<void *>(&oss));
cmd << "SELECT "<< Image::extended_db_names << ", user_pool.user_name FROM "
<< Image::table
<< " LEFT OUTER JOIN (SELECT oid, user_name FROM user_pool) "
<< "AS user_pool ON " << Image::table << ".uid = user_pool.oid";
if ( !where.empty() )
{
cmd << " WHERE " << where;
}
rc = db->exec(cmd, this);
oss << "</IMAGE_POOL>";
unset_callback();
return rc;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int ImagePool::disk_attribute(VectorAttribute * disk,
int disk_id,
int * index,
Image::ImageType * img_type)
Image::ImageType * img_type,
int uid)
{
string source;
Image * img = 0;
@ -204,7 +155,7 @@ int ImagePool::disk_attribute(VectorAttribute * disk,
}
else
{
img = get(source,true);
img = get(source,uid,true);
if (img == 0)
{
@ -255,7 +206,7 @@ int ImagePool::disk_attribute(VectorAttribute * disk,
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
void ImagePool::authorize_disk(VectorAttribute * disk, AuthRequest * ar)
void ImagePool::authorize_disk(VectorAttribute * disk,int uid, AuthRequest * ar)
{
string source;
Image * img = 0;
@ -284,7 +235,7 @@ void ImagePool::authorize_disk(VectorAttribute * disk, AuthRequest * ar)
}
else
{
img = get(source,true);
img = get(source,uid,true);
}
if (img == 0)
@ -293,7 +244,7 @@ void ImagePool::authorize_disk(VectorAttribute * disk, AuthRequest * ar)
}
ar->add_auth(AuthRequest::IMAGE,
img->get_iid(),
img->get_oid(),
AuthRequest::USE,
img->get_uid(),
img->isPublic());

View File

@ -24,6 +24,7 @@
using namespace std;
const int uids[] = {0,1,2};
const string user_names[] = {"A user","B user","C user"};
const string names[] = {"Image one", "Second Image", "The third image"};
@ -49,19 +50,19 @@ const string templates[] =
const string xmls[] =
{
"<IMAGE><ID>0</ID><UID>0</UID><NAME>Image one</NAME><TYPE>0</TYPE><PUBLIC>0</PUBLIC><PERSISTENT>1</PERSISTENT><REGTIME>0000000000</REGTIME><SOURCE>source_prefix/9ab4a4e021ee2883f57e3aeecc9e2aed7c3fa198</SOURCE><STATE>3</STATE><RUNNING_VMS>0</RUNNING_VMS><TEMPLATE><DESCRIPTION><![CDATA[This is a very long description of an image, and to achieve the longness I will copy this over. This is a very long description of an image, and to achieve the longness I will copy this over. And over. This is a very long description of an image, and to achieve the longness I will copy this over. And over. This is a very long description of an image, and to achieve the longness I will copy this over. And over.This is a very long description of an image, and to achieve the longness I will copy this over.]]></DESCRIPTION><DEV_PREFIX><![CDATA[hd]]></DEV_PREFIX><NAME><![CDATA[Image one]]></NAME><ORIGINAL_PATH><![CDATA[/tmp/image_test]]></ORIGINAL_PATH></TEMPLATE></IMAGE>",
"<IMAGE><ID>0</ID><UID>0</UID><USERNAME>A user</USERNAME><NAME>Image one</NAME><TYPE>0</TYPE><PUBLIC>0</PUBLIC><PERSISTENT>1</PERSISTENT><REGTIME>0000000000</REGTIME><SOURCE>source_prefix/9ab4a4e021ee2883f57e3aeecc9e2aed7c3fa198</SOURCE><STATE>3</STATE><RUNNING_VMS>0</RUNNING_VMS><TEMPLATE><DESCRIPTION><![CDATA[This is a very long description of an image, and to achieve the longness I will copy this over. This is a very long description of an image, and to achieve the longness I will copy this over. And over. This is a very long description of an image, and to achieve the longness I will copy this over. And over. This is a very long description of an image, and to achieve the longness I will copy this over. And over.This is a very long description of an image, and to achieve the longness I will copy this over.]]></DESCRIPTION><DEV_PREFIX><![CDATA[hd]]></DEV_PREFIX><NAME><![CDATA[Image one]]></NAME><ORIGINAL_PATH><![CDATA[/tmp/image_test]]></ORIGINAL_PATH></TEMPLATE></IMAGE>",
"<IMAGE><ID>1</ID><UID>1</UID><NAME>Second Image</NAME><TYPE>0</TYPE><PUBLIC>1</PUBLIC><PERSISTENT>0</PERSISTENT><REGTIME>0000000000</REGTIME><SOURCE>source_prefix/c9d51800847467911c755e5e4c13dfe28c3a79f3</SOURCE><STATE>3</STATE><RUNNING_VMS>0</RUNNING_VMS><TEMPLATE><DESCRIPTION><![CDATA[This is a rather short description.]]></DESCRIPTION><DEV_PREFIX><![CDATA[hd]]></DEV_PREFIX><NAME><![CDATA[Second Image]]></NAME><ORIGINAL_PATH><![CDATA[/tmp/image_second_test]]></ORIGINAL_PATH></TEMPLATE></IMAGE>",
"<IMAGE><ID>1</ID><UID>1</UID><USERNAME>B user</USERNAME><NAME>Second Image</NAME><TYPE>0</TYPE><PUBLIC>1</PUBLIC><PERSISTENT>0</PERSISTENT><REGTIME>0000000000</REGTIME><SOURCE>source_prefix/c9d51800847467911c755e5e4c13dfe28c3a79f3</SOURCE><STATE>3</STATE><RUNNING_VMS>0</RUNNING_VMS><TEMPLATE><DESCRIPTION><![CDATA[This is a rather short description.]]></DESCRIPTION><DEV_PREFIX><![CDATA[hd]]></DEV_PREFIX><NAME><![CDATA[Second Image]]></NAME><ORIGINAL_PATH><![CDATA[/tmp/image_second_test]]></ORIGINAL_PATH></TEMPLATE></IMAGE>",
"<IMAGE><ID>0</ID><UID>2</UID><NAME>The third image</NAME><TYPE>0</TYPE><PUBLIC>0</PUBLIC><PERSISTENT>0</PERSISTENT><REGTIME>0000000000</REGTIME><SOURCE>source_prefix/e50b0c738be9d431475bf5859629e5580301a7d6</SOURCE><STATE>3</STATE><RUNNING_VMS>0</RUNNING_VMS><TEMPLATE><BUS><![CDATA[SCSI]]></BUS><DEV_PREFIX><![CDATA[hd]]></DEV_PREFIX><NAME><![CDATA[The third image]]></NAME><ORIGINAL_PATH><![CDATA[/tmp/image_test]]></ORIGINAL_PATH><PROFILE><![CDATA[STUDENT]]></PROFILE></TEMPLATE></IMAGE>"
"<IMAGE><ID>0</ID><UID>2</UID><USERNAME>C user</USERNAME><NAME>The third image</NAME><TYPE>0</TYPE><PUBLIC>0</PUBLIC><PERSISTENT>0</PERSISTENT><REGTIME>0000000000</REGTIME><SOURCE>source_prefix/e50b0c738be9d431475bf5859629e5580301a7d6</SOURCE><STATE>3</STATE><RUNNING_VMS>0</RUNNING_VMS><TEMPLATE><BUS><![CDATA[SCSI]]></BUS><DEV_PREFIX><![CDATA[hd]]></DEV_PREFIX><NAME><![CDATA[The third image]]></NAME><ORIGINAL_PATH><![CDATA[/tmp/image_test]]></ORIGINAL_PATH><PROFILE><![CDATA[STUDENT]]></PROFILE></TEMPLATE></IMAGE>"
};
// This xml dump result has the STIMEs modified to 0000000000
const string xml_dump =
"<IMAGE_POOL><IMAGE><ID>0</ID><UID>0</UID><USERNAME>one_user_test</USERNAME><NAME>Image one</NAME><TYPE>0</TYPE><PUBLIC>0</PUBLIC><PERSISTENT>1</PERSISTENT><REGTIME>0000000000</REGTIME><SOURCE>source_prefix/9ab4a4e021ee2883f57e3aeecc9e2aed7c3fa198</SOURCE><STATE>3</STATE><RUNNING_VMS>0</RUNNING_VMS><TEMPLATE><DESCRIPTION><![CDATA[This is a very long description of an image, and to achieve the longness I will copy this over. This is a very long description of an image, and to achieve the longness I will copy this over. And over. This is a very long description of an image, and to achieve the longness I will copy this over. And over. This is a very long description of an image, and to achieve the longness I will copy this over. And over.This is a very long description of an image, and to achieve the longness I will copy this over.]]></DESCRIPTION><DEV_PREFIX><![CDATA[hd]]></DEV_PREFIX><NAME><![CDATA[Image one]]></NAME><ORIGINAL_PATH><![CDATA[/tmp/image_test]]></ORIGINAL_PATH></TEMPLATE></IMAGE><IMAGE><ID>1</ID><UID>1</UID><USERNAME>A user</USERNAME><NAME>Second Image</NAME><TYPE>0</TYPE><PUBLIC>1</PUBLIC><PERSISTENT>0</PERSISTENT><REGTIME>0000000000</REGTIME><SOURCE>source_prefix/c9d51800847467911c755e5e4c13dfe28c3a79f3</SOURCE><STATE>3</STATE><RUNNING_VMS>0</RUNNING_VMS><TEMPLATE><DESCRIPTION><![CDATA[This is a rather short description.]]></DESCRIPTION><DEV_PREFIX><![CDATA[hd]]></DEV_PREFIX><NAME><![CDATA[Second Image]]></NAME><ORIGINAL_PATH><![CDATA[/tmp/image_second_test]]></ORIGINAL_PATH></TEMPLATE></IMAGE><IMAGE><ID>2</ID><UID>2</UID><USERNAME>B user</USERNAME><NAME>The third image</NAME><TYPE>0</TYPE><PUBLIC>0</PUBLIC><PERSISTENT>0</PERSISTENT><REGTIME>0000000000</REGTIME><SOURCE>source_prefix/e50b0c738be9d431475bf5859629e5580301a7d6</SOURCE><STATE>3</STATE><RUNNING_VMS>0</RUNNING_VMS><TEMPLATE><BUS><![CDATA[SCSI]]></BUS><DEV_PREFIX><![CDATA[hd]]></DEV_PREFIX><NAME><![CDATA[The third image]]></NAME><ORIGINAL_PATH><![CDATA[/tmp/image_test]]></ORIGINAL_PATH><PROFILE><![CDATA[STUDENT]]></PROFILE></TEMPLATE></IMAGE></IMAGE_POOL>";
"<IMAGE_POOL><IMAGE><ID>0</ID><UID>0</UID><USERNAME>A user</USERNAME><NAME>Image one</NAME><TYPE>0</TYPE><PUBLIC>0</PUBLIC><PERSISTENT>1</PERSISTENT><REGTIME>0000000000</REGTIME><SOURCE>source_prefix/9ab4a4e021ee2883f57e3aeecc9e2aed7c3fa198</SOURCE><STATE>3</STATE><RUNNING_VMS>0</RUNNING_VMS><TEMPLATE><DESCRIPTION><![CDATA[This is a very long description of an image, and to achieve the longness I will copy this over. This is a very long description of an image, and to achieve the longness I will copy this over. And over. This is a very long description of an image, and to achieve the longness I will copy this over. And over. This is a very long description of an image, and to achieve the longness I will copy this over. And over.This is a very long description of an image, and to achieve the longness I will copy this over.]]></DESCRIPTION><DEV_PREFIX><![CDATA[hd]]></DEV_PREFIX><NAME><![CDATA[Image one]]></NAME><ORIGINAL_PATH><![CDATA[/tmp/image_test]]></ORIGINAL_PATH></TEMPLATE></IMAGE><IMAGE><ID>1</ID><UID>1</UID><USERNAME>B user</USERNAME><NAME>Second Image</NAME><TYPE>0</TYPE><PUBLIC>1</PUBLIC><PERSISTENT>0</PERSISTENT><REGTIME>0000000000</REGTIME><SOURCE>source_prefix/c9d51800847467911c755e5e4c13dfe28c3a79f3</SOURCE><STATE>3</STATE><RUNNING_VMS>0</RUNNING_VMS><TEMPLATE><DESCRIPTION><![CDATA[This is a rather short description.]]></DESCRIPTION><DEV_PREFIX><![CDATA[hd]]></DEV_PREFIX><NAME><![CDATA[Second Image]]></NAME><ORIGINAL_PATH><![CDATA[/tmp/image_second_test]]></ORIGINAL_PATH></TEMPLATE></IMAGE><IMAGE><ID>2</ID><UID>2</UID><USERNAME>C user</USERNAME><NAME>The third image</NAME><TYPE>0</TYPE><PUBLIC>0</PUBLIC><PERSISTENT>0</PERSISTENT><REGTIME>0000000000</REGTIME><SOURCE>source_prefix/e50b0c738be9d431475bf5859629e5580301a7d6</SOURCE><STATE>3</STATE><RUNNING_VMS>0</RUNNING_VMS><TEMPLATE><BUS><![CDATA[SCSI]]></BUS><DEV_PREFIX><![CDATA[hd]]></DEV_PREFIX><NAME><![CDATA[The third image]]></NAME><ORIGINAL_PATH><![CDATA[/tmp/image_test]]></ORIGINAL_PATH><PROFILE><![CDATA[STUDENT]]></PROFILE></TEMPLATE></IMAGE></IMAGE_POOL>";
const string xml_dump_where =
"<IMAGE_POOL><IMAGE><ID>0</ID><UID>0</UID><USERNAME>one_user_test</USERNAME><NAME>Image one</NAME><TYPE>0</TYPE><PUBLIC>0</PUBLIC><PERSISTENT>1</PERSISTENT><REGTIME>0000000000</REGTIME><SOURCE>source_prefix/9ab4a4e021ee2883f57e3aeecc9e2aed7c3fa198</SOURCE><STATE>3</STATE><RUNNING_VMS>0</RUNNING_VMS><TEMPLATE><DESCRIPTION><![CDATA[This is a very long description of an image, and to achieve the longness I will copy this over. This is a very long description of an image, and to achieve the longness I will copy this over. And over. This is a very long description of an image, and to achieve the longness I will copy this over. And over. This is a very long description of an image, and to achieve the longness I will copy this over. And over.This is a very long description of an image, and to achieve the longness I will copy this over.]]></DESCRIPTION><DEV_PREFIX><![CDATA[hd]]></DEV_PREFIX><NAME><![CDATA[Image one]]></NAME><ORIGINAL_PATH><![CDATA[/tmp/image_test]]></ORIGINAL_PATH></TEMPLATE></IMAGE><IMAGE><ID>1</ID><UID>1</UID><USERNAME>A user</USERNAME><NAME>Second Image</NAME><TYPE>0</TYPE><PUBLIC>1</PUBLIC><PERSISTENT>0</PERSISTENT><REGTIME>0000000000</REGTIME><SOURCE>source_prefix/c9d51800847467911c755e5e4c13dfe28c3a79f3</SOURCE><STATE>3</STATE><RUNNING_VMS>0</RUNNING_VMS><TEMPLATE><DESCRIPTION><![CDATA[This is a rather short description.]]></DESCRIPTION><DEV_PREFIX><![CDATA[hd]]></DEV_PREFIX><NAME><![CDATA[Second Image]]></NAME><ORIGINAL_PATH><![CDATA[/tmp/image_second_test]]></ORIGINAL_PATH></TEMPLATE></IMAGE></IMAGE_POOL>";
"<IMAGE_POOL><IMAGE><ID>0</ID><UID>0</UID><USERNAME>A user</USERNAME><NAME>Image one</NAME><TYPE>0</TYPE><PUBLIC>0</PUBLIC><PERSISTENT>1</PERSISTENT><REGTIME>0000000000</REGTIME><SOURCE>source_prefix/9ab4a4e021ee2883f57e3aeecc9e2aed7c3fa198</SOURCE><STATE>3</STATE><RUNNING_VMS>0</RUNNING_VMS><TEMPLATE><DESCRIPTION><![CDATA[This is a very long description of an image, and to achieve the longness I will copy this over. This is a very long description of an image, and to achieve the longness I will copy this over. And over. This is a very long description of an image, and to achieve the longness I will copy this over. And over. This is a very long description of an image, and to achieve the longness I will copy this over. And over.This is a very long description of an image, and to achieve the longness I will copy this over.]]></DESCRIPTION><DEV_PREFIX><![CDATA[hd]]></DEV_PREFIX><NAME><![CDATA[Image one]]></NAME><ORIGINAL_PATH><![CDATA[/tmp/image_test]]></ORIGINAL_PATH></TEMPLATE></IMAGE><IMAGE><ID>1</ID><UID>1</UID><USERNAME>B user</USERNAME><NAME>Second Image</NAME><TYPE>0</TYPE><PUBLIC>1</PUBLIC><PERSISTENT>0</PERSISTENT><REGTIME>0000000000</REGTIME><SOURCE>source_prefix/c9d51800847467911c755e5e4c13dfe28c3a79f3</SOURCE><STATE>3</STATE><RUNNING_VMS>0</RUNNING_VMS><TEMPLATE><DESCRIPTION><![CDATA[This is a rather short description.]]></DESCRIPTION><DEV_PREFIX><![CDATA[hd]]></DEV_PREFIX><NAME><![CDATA[Second Image]]></NAME><ORIGINAL_PATH><![CDATA[/tmp/image_second_test]]></ORIGINAL_PATH></TEMPLATE></IMAGE></IMAGE_POOL>";
const string replacement = "0000000000";
@ -91,7 +92,7 @@ public:
if( rc == 0 )
{
return ImagePool::allocate(uid, img_template, oid, err);
return ImagePool::allocate(uid, user_names[uid], img_template, oid, err);
}
else
{
@ -167,33 +168,17 @@ protected:
((Image*)obj)->to_xml(xml_str);
xml_str.replace( xml_str.find("<REGTIME>")+9, 10, replacement);
//cout << endl << xml_str << endl << xmls[index] << endl;
/*
if( xml_str != xmls[index] )
{
cout << endl << xml_str << endl << xmls[index] << endl;
}
//*/
CPPUNIT_ASSERT( ((Image*)obj)->get_name() == names[index] );
CPPUNIT_ASSERT( xml_str == xmls[index]);
};
void set_up_user_pool()
{
string err;
UserPool::bootstrap(db);
UserPool * user_pool = new UserPool(db);
int uid_1, uid_2;
string username_1 = "A user";
string username_2 = "B user";
string pass_1 = "A pass";
string pass_2 = "B pass";
user_pool->allocate(&uid_1, username_1, pass_1, true, err);
user_pool->allocate(&uid_2, username_2, pass_2, true, err);
delete user_pool;
};
public:
ImagePoolTest(){xmlInitParser();};
@ -207,7 +192,7 @@ public:
ImagePool * imp;
Image * img;
// Allocate 2 users, so they are written to the DB.
// Allocate 2 images, so they are written to the DB.
allocate(0);
allocate(2);
@ -215,13 +200,13 @@ public:
// allocated images.
imp = new ImagePool(db, "source_prefix", "OS", "hd");
img = imp->get(names[0], false);
img = imp->get(names[0], uids[0], false);
CPPUNIT_ASSERT( img != 0 );
img = imp->get(names[1], false);
img = imp->get(names[1], uids[1], false);
CPPUNIT_ASSERT( img == 0 );
img = imp->get(names[2], false);
img = imp->get(names[2], uids[2], false);
CPPUNIT_ASSERT( img != 0 );
@ -312,7 +297,7 @@ public:
check(0, obj);
// Get using its name
obj = imp->get(names[1], true);
obj = imp->get(names[1], uids[1], true);
CPPUNIT_ASSERT( obj != 0 );
obj->unlock();
@ -324,7 +309,7 @@ public:
pool->clean();
// Get first object and check its integrity
obj = imp->get(names[0], false);
obj = imp->get(names[0], uids[0], false);
check(0, obj);
// Get using its name
@ -341,14 +326,14 @@ public:
// The pool is empty
// Non existing name
obj = imp->get("Wrong name", true);
obj = imp->get("Wrong name", 0, true);
CPPUNIT_ASSERT( obj == 0 );
// Allocate an object
allocate(0);
// Ask again for a non-existing name
obj = imp->get("Non existing name", true);
obj = imp->get("Non existing name",uids[0], true);
CPPUNIT_ASSERT( obj == 0 );
}
@ -370,9 +355,9 @@ public:
CPPUNIT_ASSERT( rc == -1 );
CPPUNIT_ASSERT( oid == rc );
// Try again, with different uid
// Try again, this time with different uid. Should be allowed
rc = imp->allocate(uids[1], templates[0], &oid);
CPPUNIT_ASSERT( rc == -1 );
CPPUNIT_ASSERT( rc >= 0 );
CPPUNIT_ASSERT( oid == rc );
}
@ -487,6 +472,7 @@ public:
CPPUNIT_ASSERT(oid >= 0);
img = imp->get(oid, false);
CPPUNIT_ASSERT( img != 0 );
img->enable(true);
img->disk_attribute(disk, &index, &img_type);
@ -508,6 +494,7 @@ public:
CPPUNIT_ASSERT(oid >= 0);
img = imp->get(oid, false);
CPPUNIT_ASSERT( img != 0 );
img->enable(true);
img->disk_attribute(disk, &index, &img_type);
@ -529,6 +516,7 @@ public:
CPPUNIT_ASSERT(oid >= 0);
img = imp->get(oid, false);
CPPUNIT_ASSERT( img != 0 );
img->enable(true);
img->disk_attribute(disk, &index, &img_type);
@ -557,6 +545,7 @@ public:
// Allocate an OS type image
oid = allocate(0);
img = imp->get(oid, false);
CPPUNIT_ASSERT( img != 0 );
// ---------------------------------------------------------------------
// A disk without a BUS attribute should not have it added.
@ -643,7 +632,7 @@ public:
disk = new VectorAttribute("DISK");
disk->replace("IMAGE", "Image 0");
((ImagePool*)imp)->disk_attribute(disk, 0, &index, &img_type);
((ImagePool*)imp)->disk_attribute(disk, 0, &index, &img_type,0);
value = "";
value = disk->vector_value("TARGET");
@ -660,7 +649,7 @@ public:
disk = new VectorAttribute("DISK");
disk->replace("IMAGE_ID", "1");
((ImagePool*)imp)->disk_attribute(disk, 0, &index, &img_type);
((ImagePool*)imp)->disk_attribute(disk, 0, &index, &img_type,0);
value = "";
value = disk->vector_value("TARGET");
@ -865,8 +854,6 @@ public:
int rc;
string nan;
set_up_user_pool();
allocate(0);
allocate(1);
allocate(2);
@ -876,9 +863,16 @@ public:
string result = oss.str();
result.replace(164, 10, replacement);
result.replace(1154, 10, replacement);
result.replace(1684, 10, replacement);
result.replace(157, 10, replacement);
result.replace(1147, 10, replacement);
result.replace(1677, 10, replacement);
/*
if( result != xml_dump )
{
cout << endl << result << endl << xml_dump << endl;
}
//*/
CPPUNIT_ASSERT( result == xml_dump );
}
@ -894,8 +888,6 @@ public:
ostringstream oss;
ostringstream where;
set_up_user_pool();
allocate(0);
allocate(1);
allocate(2);
@ -906,8 +898,15 @@ public:
CPPUNIT_ASSERT(rc == 0);
string result = oss.str();
result.replace(164, 10, replacement);
result.replace(1154, 10, replacement);
result.replace(157, 10, replacement);
result.replace(1147, 10, replacement);
/*
if( result != xml_dump_where )
{
cout << endl << result << endl << xml_dump_where << endl;
}
//*/
CPPUNIT_ASSERT( result == xml_dump_where );
}
@ -921,7 +920,5 @@ public:
int main(int argc, char ** argv)
{
OneUnitTest::set_one_auth();
return PoolTest::main(argc, argv, ImagePoolTest::suite());
}

View File

@ -30,6 +30,7 @@ env.Prepend(LIBS=[
'nebula_core',
'nebula_sql',
'nebula_log',
'nebula_xml',
'crypto'
])

View File

@ -185,7 +185,7 @@ private:
if( rc == 0 )
{
return vmpool->allocate(uids[index], vm_template, &oid,
return vmpool->allocate(uids[index], "username", vm_template, &oid,
err, false);
}
else

View File

@ -21,6 +21,7 @@ Import('env')
env.Prepend(LIBS=[
'nebula_core_test',
'nebula_host',
'nebula_cluster',
'nebula_vmm',
'nebula_im',
'nebula_rm',
@ -32,6 +33,7 @@ env.Prepend(LIBS=[
'nebula_vnm',
'nebula_image',
'nebula_pool',
'nebula_xml',
'nebula_hm',
'nebula_authm',
'nebula_common',

View File

@ -234,6 +234,7 @@ void Nebula::start()
VirtualNetworkPool::bootstrap(db);
UserPool::bootstrap(db);
ImagePool::bootstrap(db);
ClusterPool::bootstrap(db);
}
catch (exception&)
{
@ -273,6 +274,8 @@ void Nebula::start()
repository_path,
default_image_type,
default_device_prefix);
cpool = new ClusterPool(db);
}
catch (exception&)
{
@ -438,6 +441,7 @@ void Nebula::start()
vnpool,
upool,
ipool,
cpool,
rm_port,
log_location + "one_xmlrpc.log");
}

View File

@ -46,12 +46,14 @@ env.Prepend(LIBS=[
'nebula_template',
'nebula_image',
'nebula_pool',
'nebula_cluster',
'nebula_host',
'nebula_vnm',
'nebula_vm',
'nebula_common',
'nebula_sql',
'nebula_log',
'nebula_xml',
'crypto'
])

110
src/pool/PoolObjectSQL.cc Normal file
View File

@ -0,0 +1,110 @@
/* -------------------------------------------------------------------------- */
/* Copyright 2002-2010, OpenNebula Project Leads (OpenNebula.org) */
/* */
/* Licensed under the Apache License, Version 2.0 (the "License"); you may */
/* not use this file except in compliance with the License. You may obtain */
/* a copy of the License at */
/* */
/* http://www.apache.org/licenses/LICENSE-2.0 */
/* */
/* Unless required by applicable law or agreed to in writing, software */
/* distributed under the License is distributed on an "AS IS" BASIS, */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
/* See the License for the specific language governing permissions and */
/* limitations under the License. */
/* -------------------------------------------------------------------------- */
#include "PoolObjectSQL.h"
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int PoolObjectSQL::select(SqlDB *db)
{
ostringstream oss;
int rc;
int boid;
set_callback(
static_cast<Callbackable::Callback>(&PoolObjectSQL::select_cb));
oss << "SELECT body FROM " << table << " WHERE oid = " << oid;
boid = oid;
oid = -1;
rc = db->exec(oss, this);
unset_callback();
if ((rc != 0) || (oid != boid ))
{
return -1;
}
return 0;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int PoolObjectSQL::select(SqlDB *db, const string& _name, int _uid)
{
ostringstream oss;
int rc;
char * sql_name;
sql_name = db->escape_str(_name.c_str());
if ( sql_name == 0 )
{
return -1;
}
set_callback(
static_cast<Callbackable::Callback>(&PoolObjectSQL::select_cb));
oss << "SELECT body FROM " << table << " WHERE name = '" <<_name << "'";
if ( _uid != -1 )
{
oss << " AND uid = " << _uid;
}
name = "";
uid = -1;
rc = db->exec(oss, this);
unset_callback();
db->free_str(sql_name);
if ((rc != 0) || (_name != name) || (_uid != uid))
{
return -1;
}
return 0;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int PoolObjectSQL::drop(SqlDB *db)
{
ostringstream oss;
int rc;
oss << "DELETE FROM " << table << " WHERE oid=" << oid;
rc = db->exec(oss);
if ( rc == 0 )
{
set_valid(false);
}
return rc;
}

View File

@ -86,6 +86,7 @@ PoolSQL::~PoolSQL()
pthread_mutex_destroy(&mutex);
}
/* ************************************************************************** */
/* PoolSQL public interface */
/* ************************************************************************** */
@ -185,7 +186,82 @@ PoolObjectSQL * PoolSQL::get(
return 0;
}
string okey = key(objectsql->name,objectsql->uid);
pool.insert(make_pair(objectsql->oid,objectsql));
name_pool.insert(make_pair(okey, objectsql));
if ( olock == true )
{
objectsql->lock();
}
oid_queue.push(objectsql->oid);
if ( pool.size() > MAX_POOL_SIZE )
{
replace();
}
unlock();
return objectsql;
}
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
PoolObjectSQL * PoolSQL::get(const string& name, int ouid, bool olock)
{
map<string,PoolObjectSQL *>::iterator index;
PoolObjectSQL * objectsql;
int rc;
lock();
index = name_pool.find(key(name,ouid));
if ( index != name_pool.end() )
{
if ( index->second->isValid() == false )
{
objectsql = 0;
}
else
{
objectsql = index->second;
if ( olock == true )
{
objectsql->lock();
}
}
unlock();
return objectsql;
}
else
{
objectsql = create();
rc = objectsql->select(db,name,ouid);
if ( rc != 0 )
{
delete objectsql;
unlock();
return 0;
}
string okey = key(objectsql->name,objectsql->uid);
pool.insert(make_pair(objectsql->oid, objectsql));
name_pool.insert(make_pair(okey, objectsql));
if ( olock == true )
{
@ -236,10 +312,11 @@ void PoolSQL::replace()
}
else
{
PoolObjectSQL * tmp_ptr;
PoolObjectSQL * tmp_ptr = index->second;
string okey = key(tmp_ptr->name,tmp_ptr->uid);
tmp_ptr = index->second;
pool.erase(index);
name_pool.erase(okey);
delete tmp_ptr;
@ -266,12 +343,63 @@ void PoolSQL::clean()
}
pool.clear();
name_pool.clear();
unlock();
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int PoolSQL::dump_cb(void * _oss, int num, char **values, char **names)
{
ostringstream * oss;
oss = static_cast<ostringstream *>(_oss);
if ( (!values[0]) || (num != 1) )
{
return -1;
}
*oss << values[0];
return 0;
}
/* -------------------------------------------------------------------------- */
int PoolSQL::dump(ostringstream& oss,
const string& elem_name,
const char * table,
const string& where)
{
int rc;
ostringstream cmd;
oss << "<" << elem_name << ">";
set_callback(static_cast<Callbackable::Callback>(&PoolSQL::dump_cb),
static_cast<void *>(&oss));
cmd << "SELECT body FROM " << table;
if ( !where.empty() )
{
cmd << " WHERE " << where;
}
rc = db->exec(cmd, this);
oss << "</" << elem_name << ">";
unset_callback();
return rc;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int PoolSQL:: search_cb(void * _oids, int num, char **values, char **names)
{
vector<int> * oids;
@ -309,3 +437,4 @@ int PoolSQL::search(
return rc;
}

View File

@ -22,7 +22,8 @@ lib_name='nebula_pool'
# Sources to generate the library
source_files=[
'PoolSQL.cc'
'PoolSQL.cc',
'PoolObjectSQL.cc'
]
# Build library

View File

@ -23,6 +23,7 @@ env.Append(LIBPATH=[
env.Prepend(LIBS=[
'nebula_pool',
'nebula_xml',
'nebula_common',
'nebula_log',
'nebula_core',

View File

@ -32,10 +32,10 @@
const char * TestObjectSQL::table = "test_pool";
const char * TestObjectSQL::db_names = "(oid,number,text)";
const char * TestObjectSQL::db_names = "(oid,number,name)";
const char * TestObjectSQL::db_bootstrap = "CREATE TABLE test_pool ("
"oid INTEGER, number INTEGER, text TEXT, PRIMARY KEY(oid))";
"oid INTEGER, number INTEGER, name TEXT, PRIMARY KEY(oid))";
/* -------------------------------------------------------------------------- */

View File

@ -30,7 +30,7 @@ class TestObjectSQL : public PoolObjectSQL
{
public:
//OBJECT ATTRIBUTES
TestObjectSQL(int n=-1, string t="default"):number(n),text(t){};
TestObjectSQL(int n=-1, string t="default"):PoolObjectSQL(-1,"",0,0),number(n),text(t){};
~TestObjectSQL(){};
@ -71,6 +71,16 @@ public:
db->exec(oss,0);
};
string& to_xml(string& xml) const
{
return xml;
};
int from_xml(const string &xml_str)
{
return 0;
};
};
// THE POOL

View File

@ -141,7 +141,7 @@ public:
vector<int> results;
const char * table = "test_pool";
string where = "text = '" + stB + "'";
string where = "name = '" + stB + "'";
int ret;
ret = pool->search(results, table, where);
@ -205,4 +205,4 @@ public:
int main(int argc, char ** argv)
{
return OneUnitTest::main(argc, argv, PoolTest::suite());
}
}

View File

@ -250,22 +250,22 @@ void RequestManager::register_xml_methods()
RequestManager::HostEnable(hpool,upool));
xmlrpc_c::methodPtr cluster_allocate(new
RequestManager::ClusterAllocate(hpool,upool));
RequestManager::ClusterAllocate(upool,cpool));
xmlrpc_c::methodPtr cluster_info(new
RequestManager::ClusterInfo(hpool,upool));
RequestManager::ClusterInfo(upool,cpool));
xmlrpc_c::methodPtr cluster_delete(new
RequestManager::ClusterDelete(hpool,upool));
RequestManager::ClusterDelete(upool,cpool));
xmlrpc_c::methodPtr cluster_add(new
RequestManager::ClusterAdd(hpool,upool));
RequestManager::ClusterAdd(hpool,upool,cpool));
xmlrpc_c::methodPtr cluster_remove(new
RequestManager::ClusterRemove(hpool,upool));
RequestManager::ClusterRemove(hpool,upool,cpool));
xmlrpc_c::methodPtr clusterpool_info(new
RequestManager::ClusterPoolInfo(hpool,upool));
RequestManager::ClusterPoolInfo(upool,cpool));
xmlrpc_c::methodPtr vn_allocate(new
RequestManager::VirtualNetworkAllocate(vnpool,upool));

View File

@ -29,6 +29,7 @@ void RequestManager::VirtualMachineAllocate::execute(
string session;
string str_template;
string error_str;
string user_name;
const string method_name = "VirtualMachineAllocate";
@ -41,6 +42,7 @@ void RequestManager::VirtualMachineAllocate::execute(
xmlrpc_c::value_array * arrayresult;
VirtualMachineTemplate * vm_template;
User * user;
char * error_msg = 0;
int num;
@ -92,7 +94,7 @@ void RequestManager::VirtualMachineAllocate::execute(
continue;
}
VirtualMachineAllocate::ipool->authorize_disk(vector,&ar);
VirtualMachineAllocate::ipool->authorize_disk(vector,uid,&ar);
}
vectors.clear();
@ -108,7 +110,7 @@ void RequestManager::VirtualMachineAllocate::execute(
continue;
}
VirtualMachineAllocate::vnpool->authorize_nic(vector,&ar);
VirtualMachineAllocate::vnpool->authorize_nic(vector,uid,&ar);
}
ar.add_auth(AuthRequest::VM,
@ -123,10 +125,26 @@ void RequestManager::VirtualMachineAllocate::execute(
}
}
//--------------------------------------------------------------------------
// Get the User Name
//--------------------------------------------------------------------------
user = VirtualMachineAllocate::upool->get(uid,true);
if ( user == 0 )
{
goto error_user_get;
}
user_name = user->get_name();
user->unlock();
//--------------------------------------------------------------------------
// Allocate the VirtualMAchine
//--------------------------------------------------------------------------
rc = VirtualMachineAllocate::vmpool->allocate(uid,
user_name,
vm_template,
&vid,
error_str,
@ -147,6 +165,13 @@ void RequestManager::VirtualMachineAllocate::execute(
return;
error_user_get:
oss.str(get_error(method_name, "USER", uid));
delete vm_template;
goto error_common;
error_authenticate:
oss.str(authenticate_error(method_name));
goto error_common;

View File

@ -34,7 +34,8 @@ void RequestManager::ClusterAdd::execute(
const string method_name = "ClusterAdd";
Host * host;
Host * host;
Cluster * cluster;
ostringstream oss;
@ -71,6 +72,14 @@ void RequestManager::ClusterAdd::execute(
}
}
// Check if cluster exists
cluster = ClusterAdd::cpool->get(clid,true);
if ( cluster == 0 )
{
goto error_cluster_get;
}
// Check if host exists
host = ClusterAdd::hpool->get(hid,true);
@ -80,7 +89,7 @@ void RequestManager::ClusterAdd::execute(
}
// Set cluster
rc = ClusterAdd::hpool->set_cluster(host, clid);
rc = host->set_cluster(cluster->get_name());
if ( rc != 0 )
{
@ -92,6 +101,8 @@ void RequestManager::ClusterAdd::execute(
host->unlock();
cluster->unlock();
// All nice, return success to the client
arrayData.push_back(xmlrpc_c::value_boolean(true)); // SUCCESS
@ -112,11 +123,17 @@ error_authorize:
goto error_common;
error_host_get:
cluster->unlock();
oss.str(get_error(method_name, "HOST", hid));
goto error_common;
error_cluster_get:
oss.str(get_error(method_name, "CLUSTER", clid));
goto error_common;
error_cluster_add:
host->unlock();
cluster->unlock();
oss.str(action_error(method_name, "USE", "CLUSTER", clid, rc));
goto error_common;

View File

@ -69,7 +69,7 @@ void RequestManager::ClusterAllocate::execute(
}
// Perform the allocation in the hostpool
rc = ClusterAllocate::hpool->allocate_cluster(&id, clustername, error_str);
rc = ClusterAllocate::cpool->allocate(&id, clustername, error_str);
if ( rc == -1 )
{

View File

@ -26,14 +26,14 @@ void RequestManager::ClusterDelete::execute(
xmlrpc_c::paramList const& paramList,
xmlrpc_c::value * const retval)
{
string session;
string session;
// <clid> of the cluster to delete from the HostPool
int clid;
ostringstream oss;
int rc;
int clid;
Cluster * cluster;
ostringstream oss;
int rc;
const string method_name = "ClusterDelete";
const string method_name = "ClusterDelete";
/* -- RPC specific vars -- */
vector<xmlrpc_c::value> arrayData;
@ -66,7 +66,17 @@ void RequestManager::ClusterDelete::execute(
}
}
rc = ClusterDelete::hpool->drop_cluster(clid);
// Perform the deletion from the pool
cluster = ClusterDelete::cpool->get(clid,true);
if ( cluster == 0 )
{
goto error_cluster_get;
}
rc = ClusterDelete::cpool->drop(cluster);
cluster->unlock();
if ( rc != 0 )
{
@ -92,6 +102,10 @@ error_authorize:
oss.str(authorization_error(method_name, "DELETE", "CLUSTER", rc, clid));
goto error_common;
error_cluster_get:
oss.str(get_error(method_name, "CLUSTER", clid));
goto error_common;
error_cluster_delete:
oss.str(action_error(method_name, "DELETE", "CLUSTER", clid, rc));
goto error_common;

View File

@ -29,12 +29,14 @@ void RequestManager::ClusterInfo::execute(
string session;
string info;
int clid;
int rc;
Cluster * cluster;
ostringstream oss;
int clid;
int rc;
const string method_name = "ClusterInfo";
ostringstream oss;
/* -- RPC specific vars -- */
vector<xmlrpc_c::value> arrayData;
@ -54,14 +56,18 @@ void RequestManager::ClusterInfo::execute(
goto error_authenticate;
}
info = ClusterInfo::hpool->info_cluster(clid);
// Get the cluster
cluster = ClusterInfo::cpool->get(clid,true);
// Cluster does not exists
if ( info.empty() )
if ( cluster == 0 )
{
goto error_cluster;
goto error_cluster_get;
}
oss << *cluster;
cluster->unlock();
// All nice, return the cluster info to the client
arrayData.push_back(xmlrpc_c::value_boolean(true)); // SUCCESS
arrayData.push_back(xmlrpc_c::value_string(info));
@ -78,7 +84,7 @@ error_authenticate:
oss.str(authenticate_error(method_name));
goto error_common;
error_cluster:
error_cluster_get:
oss.str(get_error(method_name, "CLUSTER", clid));
goto error_common;

6
src/rm/RequestManagerClusterPoolInfo.cc Executable file → Normal file
View File

@ -29,7 +29,7 @@ void RequestManager::ClusterPoolInfo::execute(
string session;
ostringstream oss;
int rc;
const string method_name = "ClusterPoolInfo";
/* -- RPC specific vars -- */
@ -49,8 +49,8 @@ void RequestManager::ClusterPoolInfo::execute(
goto error_authenticate;
}
// Perform the allocation in the vmpool
rc = ClusterPoolInfo::hpool->dump_cluster(oss);
// Dump the pool
rc = ClusterPoolInfo::cpool->dump(oss, "");
if ( rc != 0 )
{

View File

@ -27,10 +27,10 @@ void RequestManager::ClusterRemove::execute(
xmlrpc_c::value * const retval)
{
string session;
int hid;
int rc;
const string method_name = "ClusterRemove";
Host * host;
@ -77,7 +77,7 @@ void RequestManager::ClusterRemove::execute(
}
// Remove host from cluster
rc = ClusterRemove::hpool->set_default_cluster(host);
rc = ClusterRemove::cpool->set_default_cluster(host);
if ( rc != 0 )
{

View File

@ -107,7 +107,7 @@ void RequestManager::VirtualMachineDeploy::execute(
goto error_host_get;
}
hostname = host->get_hostname();
hostname = host->get_name();
vmm_mad = host->get_vmm_mad();
tm_mad = host->get_tm_mad();

View File

@ -67,7 +67,7 @@ void RequestManager::HostDelete::execute(
}
}
// Perform the allocation in the hostpool
// Perform the deletion from the hostpool
host = HostDelete::hpool->get(hid,true);
if ( host == 0 )

0
src/rm/RequestManagerHostPoolInfo.cc Executable file → Normal file
View File

View File

@ -31,8 +31,10 @@ void RequestManager::ImageAllocate::execute(
string session;
string str_template;
string error_str;
string user_name;
ImageTemplate * img_template;
ImageTemplate * img_template = 0;
User * user;
int iid;
int uid;
@ -96,10 +98,28 @@ void RequestManager::ImageAllocate::execute(
}
}
//--------------------------------------------------------------------------
// Get the User Name
//--------------------------------------------------------------------------
user = ImageAllocate::upool->get(uid,true);
if ( user == 0 )
{
goto error_user_get;
}
user_name = user->get_name();
user->unlock();
//--------------------------------------------------------------------------
// Allocate the Image
//--------------------------------------------------------------------------
rc = ImageAllocate::ipool->allocate(uid,img_template,&iid, error_str);
rc = ImageAllocate::ipool->allocate(uid,user_name,
img_template,&iid, error_str);
if ( rc < 0 )
{
@ -118,6 +138,12 @@ void RequestManager::ImageAllocate::execute(
return;
error_user_get:
oss.str(get_error(method_name, "USER", uid));
delete img_template;
goto error_common;
error_authenticate:
oss.str(authenticate_error(method_name));
goto error_common;

0
src/rm/RequestManagerImagePoolInfo.cc Executable file → Normal file
View File

View File

@ -67,7 +67,7 @@ void RequestManager::VirtualMachineMigrate::execute(
goto error_host_get;
}
hostname = host->get_hostname();
hostname = host->get_name();
vmm_mad = host->get_vmm_mad();
tm_mad = host->get_tm_mad();

View File

@ -32,8 +32,6 @@ void RequestManager::VirtualMachinePoolInfo::execute(
int rc;
int state;
bool extended;
ostringstream oss;
ostringstream where_string;
@ -45,18 +43,17 @@ void RequestManager::VirtualMachinePoolInfo::execute(
NebulaLog::log("ReM",Log::DEBUG,"VirtualMachinePoolInfo method invoked");
// For backwards compatibility, 2 or 3 arguments can be present.
switch (paramList.size())
{
case 2:
extended = true;
state = -1;
break;
case 4:
extended = xmlrpc_c::value_boolean(paramList.getBoolean(2));
case 3:
state = xmlrpc_c::value_int (paramList.getInt(3));
break;
default:
paramList.verifyEnd(4);
paramList.verifyEnd(3);
return;
}
@ -86,7 +83,7 @@ void RequestManager::VirtualMachinePoolInfo::execute(
where_string << "UID=" << filter_flag;
}
rc = VirtualMachinePoolInfo::vmpool->dump(oss, extended, state, where_string.str());
rc = VirtualMachinePoolInfo::vmpool->dump(oss, state, where_string.str());
if ( rc != 0 )
{

View File

@ -28,10 +28,12 @@ void RequestManager::VirtualNetworkAllocate::execute(
{
string session;
string name;
string user_name;
string str_template;
string error_str;
VirtualNetworkTemplate * vn_template;
User * user;
int nid;
int uid;
@ -96,10 +98,25 @@ void RequestManager::VirtualNetworkAllocate::execute(
}
}
//--------------------------------------------------------------------------
// Get the User Name
//--------------------------------------------------------------------------
user = VirtualNetworkAllocate::upool->get(uid,true);
if ( user == 0 )
{
goto error_user_get;
}
user_name = user->get_name();
user->unlock();
//--------------------------------------------------------------------------
// Allocate the Virtual Network
//--------------------------------------------------------------------------
rc = vnpool->allocate(uid,vn_template,&nid,error_str);
rc = vnpool->allocate(uid,user_name,vn_template,&nid,error_str);
if ( rc < 0 )
{
@ -118,6 +135,12 @@ void RequestManager::VirtualNetworkAllocate::execute(
return;
error_user_get:
oss.str(get_error(method_name, "USER", uid));
delete vn_template;
goto error_common;
error_authenticate:
oss.str(authenticate_error(method_name));
goto error_common;

0
src/rm/RequestManagerVirtualNetworkPoolInfo.cc Executable file → Normal file
View File

View File

@ -28,7 +28,7 @@ env.Append(CPPPATH=[
# Library dirs
env.Append(LIBPATH=[
cwd+'/src/xml',
cwd+'/src/client',
cwd+'/src/pool',
cwd+'/src/sched'
])
@ -38,7 +38,7 @@ env.Append(LIBPATH=[
################################################################################
build_scripts=[
'src/xml/SConstruct',
'src/client/SConstruct',
'src/pool/SConstruct',
'src/sched/SConstruct'
]
@ -46,7 +46,6 @@ build_scripts=[
if env['testing']=='yes':
build_scripts.extend([
'src/pool/test/SConstruct',
'src/xml/test/SConstruct',
])
for script in build_scripts:

View File

@ -84,7 +84,7 @@ public:
return -1;
}
update(message);
update_from_str(message);
vector<xmlNodePtr> nodes;
int num_objs;

View File

@ -0,0 +1,26 @@
# SConstruct for src/pool
# -------------------------------------------------------------------------- #
# Copyright 2002-2010, OpenNebula Project Leads (OpenNebula.org) #
# #
# Licensed under the Apache License, Version 2.0 (the "License"); you may #
# not use this file except in compliance with the License. You may obtain #
# a copy of the License at #
# #
# http://www.apache.org/licenses/LICENSE-2.0 #
# #
# Unless required by applicable law or agreed to in writing, software #
# distributed under the License is distributed on an "AS IS" BASIS, #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
# See the License for the specific language governing permissions and #
# limitations under the License. #
#--------------------------------------------------------------------------- #
Import('sched_env')
lib_name='scheduler_client'
source_files=['Client.cc']
# Build library
sched_env.StaticLibrary(lib_name, source_files)

View File

@ -18,7 +18,7 @@ Import('sched_env')
# Libraries
sched_env.Prepend(LIBS=[
'scheduler_xml',
'nebula_xml',
'scheduler_pool',
'nebula_log',
'nebula_common',

View File

@ -31,7 +31,8 @@ sched_env.Prepend(LIBS=[
'scheduler_sched',
'scheduler_pool',
'nebula_log',
'scheduler_xml',
'scheduler_client',
'nebula_xml',
'nebula_common',
'crypto',
])

View File

@ -445,12 +445,7 @@ Attribute * Template::vector_xml_att(const xmlNode * node)
int Template::from_xml(const string &xml_str)
{
xmlDocPtr xml_doc = 0;
xmlNode * root_element;
xmlNode * cur_node = 0;
Attribute * attr;
// Parse xml string as libxml document
xml_doc = xmlParseMemory (xml_str.c_str(),xml_str.length());
@ -460,6 +455,44 @@ int Template::from_xml(const string &xml_str)
return -1;
}
// Get the <TEMPLATE> element
root_element = xmlDocGetRootElement(xml_doc);
if( root_element == 0 )
{
return -1;
}
rebuild_attributes(root_element);
xmlFreeDoc(xml_doc);
return 0;
}
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
int Template::from_xml_node(const xmlNodePtr node)
{
if (node == 0)
{
return -1;
}
rebuild_attributes(node);
return 0;
}
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
void Template::rebuild_attributes(const xmlNode * root_element)
{
xmlNode * cur_node = 0;
Attribute * attr;
//Clear the template if not empty
if (!attributes.empty())
{
@ -473,9 +506,6 @@ int Template::from_xml(const string &xml_str)
attributes.clear();
}
// Get the <TEMPLATE> element
root_element = xmlDocGetRootElement(xml_doc);
// Get the root's children and try to build attributes.
for (cur_node = root_element->children;
cur_node != 0;
@ -497,10 +527,6 @@ int Template::from_xml(const string &xml_str)
}
}
}
xmlFreeDoc(xml_doc);
return 0;
}
/* -------------------------------------------------------------------------- */

View File

@ -641,7 +641,7 @@ static int input (void );
/* This used to be an fputs(), but since the string might contain NUL's,
* we now use fwrite().
*/
#define ECHO fwrite( template_text, template_leng, 1, template_out )
#define ECHO do { if (fwrite( template_text, template_leng, 1, template_out )) {} } while (0)
#endif
/* Gets input and stuffs it into "buf". number of characters read, or YY_NULL,
@ -652,7 +652,7 @@ static int input (void );
if ( YY_CURRENT_BUFFER_LVALUE->yy_is_interactive ) \
{ \
int c = '*'; \
int n; \
unsigned n; \
for ( n = 0; n < max_size && \
(c = getc( template_in )) != EOF && c != '\n'; ++n ) \
buf[n] = (char) c; \

View File

@ -17,6 +17,7 @@
Import('env')
env.Append(LIBS=[
'nebula_xml',
'nebula_template',
'nebula_common',
'nebula_core',

View File

@ -77,6 +77,11 @@ void Nebula::start()
delete ipool;
}
if ( cpool != 0)
{
delete cpool;
}
if ( vmm != 0)
{
delete vmm;
@ -160,6 +165,7 @@ void Nebula::start()
VirtualNetworkPool::bootstrap(db);
UserPool::bootstrap(db);
ImagePool::bootstrap(db);
ClusterPool::bootstrap(db);
}
catch (exception&)
{
@ -203,6 +209,11 @@ void Nebula::start()
default_image_type,
default_device_prefix);
}
if (tester->need_cluster_pool)
{
cpool = tester->create_cpool(db);
}
}
catch (exception&)
{
@ -347,7 +358,7 @@ void Nebula::start()
{
try
{
rm = tester->create_rm(vmpool,hpool,vnpool,upool,ipool,
rm = tester->create_rm(vmpool,hpool,vnpool,upool,ipool,cpool,
log_location + "one_xmlrpc.log");
}
catch (bad_alloc&)

View File

@ -49,6 +49,11 @@ ImagePool* NebulaTest::create_ipool( SqlDB* db,
default_device_prefix);
}
ClusterPool* NebulaTest::create_cpool(SqlDB* db)
{
return new ClusterPool(db);
}
// -----------------------------------------------------------
// Managers
// -----------------------------------------------------------
@ -106,6 +111,7 @@ RequestManager* NebulaTest::create_rm(
VirtualNetworkPool * vnpool,
UserPool * upool,
ImagePool * ipool,
ClusterPool * cpool,
string log_file)
{
int rm_port = 2633;
@ -115,6 +121,7 @@ RequestManager* NebulaTest::create_rm(
vnpool,
upool,
ipool,
cpool,
rm_port,
log_file);
}

View File

@ -27,21 +27,13 @@
#include "User.h"
/* ************************************************************************** */
/* User :: Constructor/Destructor */
/* User :: Constructor/Destructor */
/* ************************************************************************** */
User::User(
int id,
string _username,
string _password,
bool _enabled):
PoolObjectSQL(id),
username (_username),
password (_password),
enabled (_enabled)
User::User(int id, string name, string pass, bool _enabled):
PoolObjectSQL(id,name,-1,table), password(pass), enabled(_enabled)
{};
User::~User(){};
/* ************************************************************************** */
@ -50,63 +42,10 @@ User::~User(){};
const char * User::table = "user_pool";
const char * User::db_names = "oid,user_name,password,enabled";
const char * User::db_names = "oid,name,body";
const char * User::db_bootstrap = "CREATE TABLE IF NOT EXISTS user_pool ("
"oid INTEGER PRIMARY KEY, user_name VARCHAR(256), password TEXT,"
"enabled INTEGER, UNIQUE(user_name))";
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int User::select_cb(void *nil, int num, char **values, char **names)
{
if ((!values[OID]) ||
(!values[USERNAME]) ||
(!values[PASSWORD]) ||
(!values[ENABLED]) ||
(num != LIMIT ))
{
return -1;
}
oid = atoi(values[OID]);
username = values[USERNAME];
password = values[PASSWORD];
enabled = (atoi(values[ENABLED])==0)?false:true;
return 0;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int User::select(SqlDB *db)
{
ostringstream oss;
int rc;
int boid;
set_callback(static_cast<Callbackable::Callback>(&User::select_cb));
oss << "SELECT " << db_names << " FROM " << table << " WHERE oid = " << oid;
boid = oid;
oid = -1;
rc = db->exec(oss, this);
unset_callback();
if ((rc != 0) || (oid != boid ))
{
return -1;
}
return 0;
}
"oid INTEGER PRIMARY KEY, name VARCHAR(256), body TEXT, UNIQUE(name))";
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
@ -129,48 +68,30 @@ int User::insert(SqlDB *db, string& error_str)
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int User::update(SqlDB *db)
{
int rc;
rc = insert_replace(db, true);
if ( rc != 0 )
{
return rc;
}
return 0;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int User::insert_replace(SqlDB *db, bool replace)
{
ostringstream oss;
int rc;
string xml_body;
char * sql_username;
char * sql_password;
int str_enabled = enabled?1:0;
char * sql_xml;
// Update the User
sql_username = db->escape_str(username.c_str());
sql_username = db->escape_str(name.c_str());
if ( sql_username == 0 )
{
goto error_username;
}
sql_password = db->escape_str(password.c_str());
sql_xml = db->escape_str(to_xml(xml_body).c_str());
if ( sql_password == 0 )
if ( sql_xml == 0 )
{
goto error_password;
goto error_body;
}
// Construct the SQL statement to Insert or Replace
@ -184,69 +105,23 @@ int User::insert_replace(SqlDB *db, bool replace)
}
oss << " INTO " << table << " ("<< db_names <<") VALUES ("
<< oid << ","
<< "'" << sql_username << "',"
<< "'" << sql_password << "',"
<< str_enabled << ")";
<< oid << ","
<< "'" << sql_username << "',"
<< "'" << sql_xml << "')";
rc = db->exec(oss);
db->free_str(sql_username);
db->free_str(sql_password);
db->free_str(sql_xml);
return rc;
error_password:
error_body:
db->free_str(sql_username);
error_username:
return -1;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int User::dump(ostringstream& oss, int num, char **values, char **names)
{
if ((!values[OID]) ||
(!values[USERNAME]) ||
(!values[PASSWORD]) ||
(!values[ENABLED]) ||
(num != LIMIT))
{
return -1;
}
oss <<
"<USER>" <<
"<ID>" << values[OID] <<"</ID>" <<
"<NAME>" << values[USERNAME]<<"</NAME>" <<
"<PASSWORD>"<< values[PASSWORD]<<"</PASSWORD>"<<
"<ENABLED>" << values[ENABLED] <<"</ENABLED>" <<
"</USER>";
return 0;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int User::drop(SqlDB * db)
{
ostringstream oss;
int rc;
oss << "DELETE FROM " << table << " WHERE oid=" << oid;
rc = db->exec(oss);
if ( rc == 0 )
{
set_valid(false);
}
return rc;
}
/* ************************************************************************** */
/* User :: Misc */
/* ************************************************************************** */
@ -260,7 +135,6 @@ ostream& operator<<(ostream& os, User& user)
return os;
};
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
@ -273,7 +147,7 @@ string& User::to_xml(string& xml) const
oss <<
"<USER>"
"<ID>" << oid <<"</ID>" <<
"<NAME>" << username <<"</NAME>" <<
"<NAME>" << name <<"</NAME>" <<
"<PASSWORD>" << password <<"</PASSWORD>" <<
"<ENABLED>" << enabled_int <<"</ENABLED>" <<
"</USER>";
@ -286,23 +160,28 @@ string& User::to_xml(string& xml) const
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
string& User::to_str(string& str) const
int User::from_xml(const string& xml)
{
ostringstream os;
int rc = 0;
int int_enabled;
string enabled_str = enabled?"True":"False";
// Initialize the internal XML object
update_from_str(xml);
os <<
"ID = " << oid << endl <<
"NAME = " << username << endl <<
"PASSWORD = " << password << endl <<
"ENABLED = " << enabled_str;
rc += xpath(oid, "/USER/ID", -1);
rc += xpath(name, "/USER/NAME", "not_found");
rc += xpath(password, "/USER/PASSWORD", "not_found");
rc += xpath(int_enabled, "/USER/ENABLED", 0);
str = os.str();
enabled = int_enabled;
return str;
if (rc != 0)
{
return -1;
}
return 0;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */

View File

@ -31,103 +31,81 @@
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int UserPool::init_cb(void *nil, int num, char **values, char **names)
{
if ( num == 0 || values == 0 || values[0] == 0 )
{
return -1;
}
known_users.insert(make_pair(values[1],atoi(values[0])));
return 0;
}
/* -------------------------------------------------------------------------- */
UserPool::UserPool(SqlDB * db):PoolSQL(db,User::table)
{
ostringstream sql;
int one_uid = -1;
ostringstream oss;
string one_token;
string one_name;
string one_pass;
string one_auth_file;
set_callback(static_cast<Callbackable::Callback>(&UserPool::init_cb));
const char * one_auth;
ifstream file;
sql << "SELECT oid,user_name FROM " << User::table;
db->exec(sql, this);
unset_callback();
if ((int) known_users.size() == 0)
if (get(0,false) != 0)
{
// User oneadmin needs to be added in the bootstrap
int one_uid = -1;
ostringstream oss;
string one_token;
string one_name;
string one_pass;
string one_auth_file;
return;
}
const char * one_auth;
ifstream file;
// User oneadmin needs to be added in the bootstrap
one_auth = getenv("ONE_AUTH");
one_auth = getenv("ONE_AUTH");
if (!one_auth)
{
struct passwd * pw_ent;
if (!one_auth)
pw_ent = getpwuid(getuid());
if ((pw_ent != NULL) && (pw_ent->pw_dir != NULL))
{
struct passwd * pw_ent;
one_auth_file = pw_ent->pw_dir;
one_auth_file += "/.one/one_auth";
pw_ent = getpwuid(getuid());
if ((pw_ent != NULL) && (pw_ent->pw_dir != NULL))
{
one_auth_file = pw_ent->pw_dir;
one_auth_file += "/.one/one_auth";
one_auth = one_auth_file.c_str();
}
else
{
oss << "Could not get one_auth file location";
}
}
file.open(one_auth);
if (file.good())
{
getline(file,one_token);
if (file.fail())
{
oss << "Error reading file: " << one_auth;
}
else
{
if (User::split_secret(one_token,one_name,one_pass) == 0)
{
string error_str;
string sha1_pass = User::sha1_digest(one_pass);
allocate(&one_uid, one_name, sha1_pass, true, error_str);
}
else
{
oss << "Wrong format must be <username>:<password>";
}
}
one_auth = one_auth_file.c_str();
}
else
{
oss << "Cloud not open file: " << one_auth;
oss << "Could not get one_auth file location";
}
}
file.close();
file.open(one_auth);
if (one_uid != 0)
if (file.good())
{
getline(file,one_token);
if (file.fail())
{
NebulaLog::log("ONE",Log::ERROR,oss);
throw;
oss << "Error reading file: " << one_auth;
}
else
{
if (User::split_secret(one_token,one_name,one_pass) == 0)
{
string error_str;
string sha1_pass = User::sha1_digest(one_pass);
allocate(&one_uid, one_name, sha1_pass, true, error_str);
}
else
{
oss << "Wrong format must be <username>:<password>";
}
}
}
else
{
oss << "Cloud not open file: " << one_auth;
}
file.close();
if (one_uid != 0)
{
NebulaLog::log("ONE",Log::ERROR,oss);
throw;
}
}
@ -142,23 +120,39 @@ int UserPool::allocate (
string& error_str)
{
User * user;
ostringstream oss;
if ( username.empty() )
{
goto error_name;
}
user = get(username,false);
if ( user !=0 )
{
goto error_duplicated;
}
// Build a new User object
user = new User(-1,
username,
password,
enabled);
user = new User(-1, username, password, enabled);
// Insert the Object in the pool
*oid = PoolSQL::allocate(user, error_str);
if (*oid != -1)
{
// Add the user to the map of known_users
known_users.insert(make_pair(username,*oid));
}
return *oid;
error_name:
oss << "NAME cannot be empty.";
goto error_common;
error_duplicated:
oss << "NAME is already taken by USER " << user->get_oid() << ".";
error_common:
*oid = -1;
error_str = oss.str();
return *oid;
}
@ -188,19 +182,12 @@ int UserPool::authenticate(string& session)
return -1;
}
index = known_users.find(username);
user = get(username,true);
if ( index != known_users.end() ) //User known to OpenNebula
if (user != 0) //User known to OpenNebula
{
user = get((int)index->second,true);
if ( user == 0 )
{
return -1;
}
u_pass = user->password;
uid = user->get_uid();
uid = user->oid;
user->unlock();
}
@ -319,42 +306,3 @@ int UserPool::authorize(AuthRequest& ar)
return rc;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int UserPool::dump_cb(void * _oss, int num, char **values, char **names)
{
ostringstream * oss;
oss = static_cast<ostringstream *>(_oss);
return User::dump(*oss, num, values, names);
}
/* -------------------------------------------------------------------------- */
int UserPool::dump(ostringstream& oss, const string& where)
{
int rc;
ostringstream cmd;
oss << "<USER_POOL>";
set_callback(static_cast<Callbackable::Callback>(&UserPool::dump_cb),
static_cast<void *>(&oss));
cmd << "SELECT " << User::db_names << " FROM " << User::table;
if ( !where.empty() )
{
cmd << " WHERE " << where;
}
rc = db->exec(cmd, this);
unset_callback();
oss << "</USER_POOL>";
return rc;
}

View File

@ -19,6 +19,7 @@ Import('env')
env.Prepend(LIBS=[
'nebula_um',
'nebula_pool',
'nebula_xml',
'nebula_log',
'nebula_authm',
'nebula_common',

View File

@ -100,16 +100,16 @@ protected:
{
CPPUNIT_ASSERT( obj != 0 );
string name = ((User*)obj)->get_username();
string name = ((User*)obj)->get_name();
CPPUNIT_ASSERT( name == usernames[index] );
CPPUNIT_ASSERT( ((User*)obj)->get_password() == passwords[index] );
};
public:
UserPoolTest(){};
UserPoolTest(){xmlInitParser();};
~UserPoolTest(){};
~UserPoolTest(){xmlCleanupParser();};
/* ********************************************************************* */
/* ********************************************************************* */
@ -149,8 +149,8 @@ public:
User* user = (User*) pool->get(0, false);
CPPUNIT_ASSERT(user != 0);
CPPUNIT_ASSERT( user->get_uid() == 0 );
CPPUNIT_ASSERT( user->get_username() == "one_user_test" );
CPPUNIT_ASSERT( user->get_oid() == 0 );
CPPUNIT_ASSERT( user->get_name() == "one_user_test" );
CPPUNIT_ASSERT( user->get_password() == User::sha1_digest("password") );
}
@ -287,6 +287,14 @@ public:
ostringstream oss;
((UserPool*)pool)->dump(oss, "");
/*
if( oss.str() != dump_result )
{
cout << endl << oss.str() << endl << "========"
<< endl << dump_result << endl << "--------";
}
//*/
CPPUNIT_ASSERT( oss.str() == dump_result );
}
@ -307,7 +315,15 @@ public:
// by" is a dirty fix (SQL injection, actually) because MySQL orders the
// results by user_name
ostringstream oss;
((UserPool*)pool)->dump(oss, "user_name LIKE 'a%' ORDER BY oid");
((UserPool*)pool)->dump(oss, "name LIKE 'a%' ORDER BY oid");
/*
if( oss.str() != dump_where_result )
{
cout << endl << oss.str() << endl << "========"
<< endl << dump_where_result << endl << "--------";
}
//*/
CPPUNIT_ASSERT( oss.str() == dump_where_result );
}

View File

@ -25,21 +25,10 @@
const char * History::table = "history";
const char * History::db_names = "vid,seq,host_name,vm_dir,hid,vm_mad,tm_mad,stime,"
"etime,pstime,petime,rstime,retime,estime,eetime,reason";
const char * History::extended_db_names =
"history.vid, history.seq, history.host_name, history.vm_dir, history.hid, "
"history.vm_mad, history.tm_mad, history.stime, history.etime, "
"history.pstime, history.petime, history.rstime, history.retime, "
"history.estime, history.eetime, history.reason";
const char * History::db_names = "vid, seq, body";
const char * History::db_bootstrap = "CREATE TABLE IF NOT EXISTS "
"history (vid INTEGER,"
"seq INTEGER,host_name TEXT,vm_dir TEXT,hid INTEGER,vm_mad TEXT,tm_mad TEXT,"
"stime INTEGER,etime INTEGER,pstime INTEGER,petime INTEGER,rstime INTEGER,"
"retime INTEGER,estime INTEGER,eetime INTEGER,reason INTEGER,"
"PRIMARY KEY(vid,seq))";
"history (vid INTEGER, seq INTEGER, body TEXT, PRIMARY KEY(vid,seq))";
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
@ -47,6 +36,7 @@ const char * History::db_bootstrap = "CREATE TABLE IF NOT EXISTS "
History::History(
int _oid,
int _seq):
ObjectXML(),
oid(_oid),
seq(_seq),
hostname(""),
@ -67,13 +57,13 @@ History::History(
/* -------------------------------------------------------------------------- */
History::History(
int _oid,
int _seq,
int _hid,
string& _hostname,
string& _vm_dir,
string& _vmm,
string& _tm):
int _oid,
int _seq,
int _hid,
const string& _hostname,
const string& _vm_dir,
const string& _vmm,
const string& _tm):
oid(_oid),
seq(_seq),
hostname(_hostname),
@ -141,72 +131,25 @@ void History::non_persistent_data()
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int History::insert(SqlDB * db, string& error_str)
{
int rc;
rc = insert_replace(db, false);
return rc;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int History::update(SqlDB * db)
{
int rc;
rc = insert_replace(db, true);
return rc;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int History::insert_replace(SqlDB *db, bool replace)
{
ostringstream oss;
int rc;
string xml_body;
char * sql_xml;
char * sql_hostname;
char * sql_vm_dir;
char * sql_vmm_mad_name;
char * sql_tm_mad_name;
int rc;
if (seq == -1)
{
return 0;
}
sql_hostname = db->escape_str(hostname.c_str());
sql_xml = db->escape_str(to_xml(xml_body).c_str());
if ( sql_hostname == 0 )
if ( sql_xml == 0 )
{
goto error_hostname;
}
sql_vm_dir = db->escape_str(vm_dir.c_str());
if ( sql_vm_dir == 0 )
{
goto error_vm_dir;
}
sql_vmm_mad_name = db->escape_str(vmm_mad_name.c_str());
if ( sql_vmm_mad_name == 0 )
{
goto error_vmm;
}
sql_tm_mad_name = db->escape_str(tm_mad_name.c_str());
if ( sql_tm_mad_name == 0 )
{
goto error_tm;
goto error_body;
}
if(replace)
@ -218,40 +161,18 @@ int History::insert_replace(SqlDB *db, bool replace)
oss << "INSERT";
}
oss << " INTO " << table << " ("<< db_names <<") VALUES ("<<
oid << "," <<
seq << "," <<
"'" << sql_hostname << "',"<<
"'" << sql_vm_dir << "'," <<
hid << "," <<
"'" << sql_vmm_mad_name << "'," <<
"'" << sql_tm_mad_name << "'," <<
stime << "," <<
etime << "," <<
prolog_stime << "," <<
prolog_etime << "," <<
running_stime << "," <<
running_etime << "," <<
epilog_stime << "," <<
epilog_etime << "," <<
reason << ")";
oss << " INTO " << table << " ("<< db_names <<") VALUES ("
<< oid << ","
<< seq << ","
<< "'" << sql_xml << "')";
rc = db->exec(oss);
db->free_str(sql_hostname);
db->free_str(sql_vm_dir);
db->free_str(sql_vmm_mad_name);
db->free_str(sql_tm_mad_name);
db->free_str(sql_xml);
return rc;
error_tm:
db->free_str(sql_vmm_mad_name);
error_vmm:
db->free_str(sql_vm_dir);
error_vm_dir:
db->free_str(sql_hostname);
error_hostname:
error_body:
return -1;
}
@ -260,99 +181,17 @@ error_hostname:
int History::select_cb(void *nil, int num, char **values, char **names)
{
if ((!values[VID]) ||
(!values[SEQ]) ||
(!values[HOSTNAME]) ||
(!values[VM_DIR]) ||
(!values[HID]) ||
(!values[VMMMAD]) ||
(!values[TMMAD]) ||
(!values[STIME]) ||
(!values[ETIME]) ||
(!values[PROLOG_STIME]) ||
(!values[PROLOG_ETIME]) ||
(!values[RUNNING_STIME]) ||
(!values[RUNNING_ETIME]) ||
(!values[EPILOG_STIME]) ||
(!values[EPILOG_ETIME]) ||
(!values[REASON]) ||
(num != LIMIT ))
if ( (!values[0]) || (num != 1) )
{
return -1;
}
oid = atoi(values[VID]);
seq = atoi(values[SEQ]);
hostname = values[HOSTNAME];
vm_dir = values[VM_DIR];
hid = atoi(values[HID]);
vmm_mad_name = values[VMMMAD];
tm_mad_name = values[TMMAD];
stime = static_cast<time_t>(atoi(values[STIME]));
etime = static_cast<time_t>(atoi(values[ETIME]));
prolog_stime = static_cast<time_t>(atoi(values[PROLOG_STIME]));
prolog_etime = static_cast<time_t>(atoi(values[PROLOG_ETIME]));
running_stime = static_cast<time_t>(atoi(values[RUNNING_STIME]));
running_etime = static_cast<time_t>(atoi(values[RUNNING_ETIME]));
epilog_stime = static_cast<time_t>(atoi(values[EPILOG_STIME]));
epilog_etime = static_cast<time_t>(atoi(values[EPILOG_ETIME]));
reason = static_cast<MigrationReason>(atoi(values[REASON]));
return 0;
return from_xml(values[0]);
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int History::dump(ostringstream& oss, int num, char **values, char **names)
{
if ((!values[VID])||
(!values[SEQ])||
(!values[HOSTNAME])||
(!values[HID])||
(!values[STIME])||
(!values[ETIME])||
(!values[PROLOG_STIME])||
(!values[PROLOG_ETIME])||
(!values[RUNNING_STIME])||
(!values[RUNNING_ETIME])||
(!values[EPILOG_STIME])||
(!values[EPILOG_ETIME])||
(!values[REASON])||
(num != LIMIT))
{
return -1;
}
oss <<
"<HISTORY>" <<
"<SEQ>" << values[SEQ] << "</SEQ>" <<
"<HOSTNAME>"<< values[HOSTNAME] << "</HOSTNAME>"<<
"<HID>" << values[HID] << "</HID>" <<
"<STIME>" << values[STIME] << "</STIME>" <<
"<ETIME>" << values[ETIME] << "</ETIME>" <<
"<PSTIME>" << values[PROLOG_STIME] << "</PSTIME>" <<
"<PETIME>" << values[PROLOG_ETIME] << "</PETIME>" <<
"<RSTIME>" << values[RUNNING_STIME] << "</RSTIME>" <<
"<RETIME>" << values[RUNNING_ETIME] << "</RETIME>" <<
"<ESTIME>" << values[EPILOG_STIME] << "</ESTIME>" <<
"<EETIME>" << values[EPILOG_ETIME] << "</EETIME>" <<
"<REASON>" << values[REASON] << "</REASON>" <<
"</HISTORY>";
return 0;
}
/* -------------------------------------------------------------------------- */
int History::select(SqlDB * db)
{
ostringstream oss;
@ -365,12 +204,12 @@ int History::select(SqlDB * db)
if ( seq == -1)
{
oss << "SELECT " << db_names << " FROM history WHERE vid = "<< oid <<
oss << "SELECT body FROM history WHERE vid = "<< oid <<
" AND seq=(SELECT MAX(seq) FROM history WHERE vid = " << oid << ")";
}
else
{
oss << "SELECT " << db_names << " FROM history WHERE vid = " << oid
oss << "SELECT body FROM history WHERE vid = " << oid
<< " AND seq = " << seq;
}
@ -415,33 +254,6 @@ ostream& operator<<(ostream& os, const History& history)
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
string& History::to_str(string& str) const
{
ostringstream oss;
oss<< "\tSEQ = " << seq << endl
<< "\tHOSTNAME = " << hostname << endl
<< "\tHID = " << hid << endl
<< "\tSTIME = " << stime << endl
<< "\tETIME = " << etime << endl
<< "\tPSTIME = " << prolog_stime << endl
<< "\tPETIME = " << prolog_etime << endl
<< "\tRSTIME = " << running_stime << endl
<< "\tRETIME = " << running_etime << endl
<< "\tESTIME = " << epilog_stime << endl
<< "\tEETIME = " << epilog_etime << endl
<< "\tREASON = " << reason;
str = oss.str();
return str;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
string& History::to_xml(string& xml) const
{
ostringstream oss;
@ -450,9 +262,12 @@ string& History::to_xml(string& xml) const
"<HISTORY>" <<
"<SEQ>" << seq << "</SEQ>" <<
"<HOSTNAME>"<< hostname << "</HOSTNAME>"<<
"<VM_DIR>" << vm_dir << "</VM_DIR>"<<
"<HID>" << hid << "</HID>" <<
"<STIME>" << stime << "</STIME>" <<
"<ETIME>" << etime << "</ETIME>" <<
"<VMMMAD>" << vmm_mad_name << "</VMMMAD>"<<
"<TMMAD>" << tm_mad_name << "</TMMAD>" <<
"<PSTIME>" << prolog_stime << "</PSTIME>"<<
"<PETIME>" << prolog_etime << "</PETIME>"<<
"<RSTIME>" << running_stime << "</RSTIME>"<<
@ -466,3 +281,40 @@ string& History::to_xml(string& xml) const
return xml;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int History::rebuild_attributes()
{
int int_reason;
int rc = 0;
rc += xpath(seq , "/HISTORY/SEQ", -1);
rc += xpath(hostname , "/HISTORY/HOSTNAME", "not_found");
rc += xpath(vm_dir , "/HISTORY/VM_DIR", "not_found");
rc += xpath(hid , "/HISTORY/HID", -1);
rc += xpath(stime , "/HISTORY/STIME", 0);
rc += xpath(etime , "/HISTORY/ETIME", 0);
rc += xpath(vmm_mad_name , "/HISTORY/VMMMAD", "not_found");
rc += xpath(tm_mad_name , "/HISTORY/TMMAD", "not_found");
rc += xpath(prolog_stime , "/HISTORY/PSTIME", 0);
rc += xpath(prolog_etime , "/HISTORY/PETIME", 0);
rc += xpath(running_stime, "/HISTORY/RSTIME", 0);
rc += xpath(running_etime, "/HISTORY/RETIME", 0);
rc += xpath(epilog_stime , "/HISTORY/ESTIME", 0);
rc += xpath(epilog_etime , "/HISTORY/EETIME", 0);
rc += xpath(int_reason , "/HISTORY/REASON", 0);
reason = static_cast<MigrationReason>(int_reason);
if (rc != 0)
{
return -1;
}
return 0;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */

View File

@ -36,11 +36,13 @@
/* Virtual Machine :: Constructor/Destructor */
/* ************************************************************************** */
VirtualMachine::VirtualMachine(int id, VirtualMachineTemplate * _vm_template):
PoolObjectSQL(id),
uid(-1),
VirtualMachine::VirtualMachine(int id,
int _uid,
string _user_name,
VirtualMachineTemplate * _vm_template):
PoolObjectSQL(id,"",_uid,table),
user_name(_user_name),
last_poll(0),
name(""),
state(INIT),
lcm_state(LCM_INIT),
stime(time(0)),
@ -50,7 +52,6 @@ VirtualMachine::VirtualMachine(int id, VirtualMachineTemplate * _vm_template):
cpu(0),
net_tx(0),
net_rx(0),
last_seq(-1),
history(0),
previous_history(0),
_log(0)
@ -95,133 +96,52 @@ VirtualMachine::~VirtualMachine()
const char * VirtualMachine::table = "vm_pool";
const char * VirtualMachine::db_names =
"oid,uid,name,last_poll, state,lcm_state,stime,etime,deploy_id"
",memory,cpu,net_tx,net_rx,last_seq, template";
const char * VirtualMachine::extended_db_names =
"vm_pool.oid, vm_pool.uid, vm_pool.name, vm_pool.last_poll, vm_pool.state, "
"vm_pool.lcm_state, vm_pool.stime, vm_pool.etime, vm_pool.deploy_id, "
"vm_pool.memory, vm_pool.cpu, vm_pool.net_tx, vm_pool.net_rx, "
"vm_pool.last_seq, vm_pool.template";
"oid, name, body, uid, last_poll, state, lcm_state";
const char * VirtualMachine::db_bootstrap = "CREATE TABLE IF NOT EXISTS "
"vm_pool ("
"oid INTEGER PRIMARY KEY,uid INTEGER,name TEXT,"
"last_poll INTEGER, state INTEGER,lcm_state INTEGER,"
"stime INTEGER,etime INTEGER,deploy_id TEXT,memory INTEGER,cpu INTEGER,"
"net_tx INTEGER,net_rx INTEGER, last_seq INTEGER, template TEXT)";
"vm_pool (oid INTEGER PRIMARY KEY, name TEXT, body TEXT, uid INTEGER, "
"last_poll INTEGER, state INTEGER, lcm_state INTEGER)";
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int VirtualMachine::select_cb(void *nil, int num, char **values, char **names)
{
if ((values[OID] == 0) ||
(values[UID] == 0) ||
(values[NAME] == 0) ||
(values[LAST_POLL] == 0) ||
(values[STATE] == 0) ||
(values[LCM_STATE] == 0) ||
(values[STIME] == 0) ||
(values[ETIME] == 0) ||
(values[MEMORY] == 0) ||
(values[CPU] == 0) ||
(values[NET_TX] == 0) ||
(values[NET_RX] == 0) ||
(values[LAST_SEQ] == 0) ||
(values[TEMPLATE] == 0) ||
(num != LIMIT ))
{
return -1;
}
oid = atoi(values[OID]);
uid = atoi(values[UID]);
name = values[NAME];
last_poll = static_cast<time_t>(atoi(values[LAST_POLL]));
state = static_cast<VmState>(atoi(values[STATE]));
lcm_state = static_cast<LcmState>(atoi(values[LCM_STATE]));
stime = static_cast<time_t>(atoi(values[STIME]));
etime = static_cast<time_t>(atoi(values[ETIME]));
if ( values[DEPLOY_ID] != 0 )
{
deploy_id = values[DEPLOY_ID];
}
memory = atoi(values[MEMORY]);
cpu = atoi(values[CPU]);
net_tx = atoi(values[NET_TX]);
net_rx = atoi(values[NET_RX]);
last_seq = atoi(values[LAST_SEQ]);
// Virtual Machine template
vm_template->from_xml(values[TEMPLATE]);
return 0;
}
/* -------------------------------------------------------------------------- */
int VirtualMachine::select(SqlDB * db)
{
ostringstream oss;
ostringstream ose;
int rc;
int boid;
int last_seq;
string filename;
Nebula& nd = Nebula::instance();
set_callback(
static_cast<Callbackable::Callback>(&VirtualMachine::select_cb));
// Rebuld the VirtualMachine object
rc = PoolObjectSQL::select(db);
oss << "SELECT " << db_names << " FROM " << table << " WHERE oid = " << oid;
boid = oid;
oid = -1;
rc = db->exec(oss,this);
unset_callback();
if ((rc != 0) || (oid != boid ))
if( rc != 0 )
{
goto error_id;
return rc;
}
//Get the History Records
if ( last_seq != -1 )
//Get History Records. Current history is record is built in from_xml() (if any).
if( hasHistory() )
{
history = new History(oid, last_seq);
last_seq = history->seq;
rc = history->select(db);
if (rc != 0)
if ( last_seq > 0 )
{
goto error_history;
}
}
previous_history = new History(oid, last_seq - 1);
if ( last_seq > 0 )
{
previous_history = new History(oid, last_seq - 1);
rc = previous_history->select(db);
rc = previous_history->select(db);
if ( rc != 0)
{
goto error_previous_history;
if ( rc != 0)
{
goto error_previous_history;
}
}
}
//Create support directory for this VM
oss.str("");
oss << nd.get_var_location() << oid;
@ -229,7 +149,6 @@ int VirtualMachine::select(SqlDB * db)
chmod(oss.str().c_str(), 0777);
//Create Log support for this VM
try
{
_log = new FileLog(nd.get_vm_log_filename(oid),Log::DEBUG);
@ -244,16 +163,6 @@ int VirtualMachine::select(SqlDB * db)
return 0;
error_id:
ose << "Error getting VM id: " << oid;
log("VMM", Log::ERROR, ose);
return -1;
error_history:
ose << "Can not get history for VM id: " << oid;
log("ONE", Log::ERROR, ose);
return -1;
error_previous_history:
ose << "Can not get previous history record (seq:" << history->seq
<< ") for VM id: " << oid;
@ -324,14 +233,14 @@ int VirtualMachine::insert(SqlDB * db, string& error_str)
// Parse the context & requirements
// -------------------------------------------------------------------------
rc = parse_context();
rc = parse_context(error_str);
if ( rc != 0 )
{
goto error_context;
}
rc = parse_requirements();
rc = parse_requirements(error_str);
if ( rc != 0 )
{
@ -367,11 +276,9 @@ error_images:
goto error_common;
error_context:
error_str = "Could not parse CONTEXT for VM.";
goto error_common;
error_requirements:
error_str = "Could not parse REQUIREMENTS for VM.";
goto error_common;
error_common:
@ -384,7 +291,7 @@ error_common:
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int VirtualMachine::parse_context()
int VirtualMachine::parse_context(string& error_str)
{
int rc, num;
@ -400,12 +307,17 @@ int VirtualMachine::parse_context()
{
return 0;
}
else if ( num > 1 )
{
error_str = "Only one CONTEXT attribute can be defined.";
return -1;
}
context = dynamic_cast<VectorAttribute *>(array_context[0]);
if ( context == 0 )
{
NebulaLog::log("ONE",Log::ERROR, "Wrong format for CONTEXT attribute");
error_str = "Wrong format for CONTEXT attribute.";
return -1;
}
@ -504,7 +416,7 @@ void VirtualMachine::parse_graphics()
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int VirtualMachine::parse_requirements()
int VirtualMachine::parse_requirements(string& error_str)
{
int rc, num;
@ -519,12 +431,17 @@ int VirtualMachine::parse_requirements()
{
return 0;
}
else if ( num > 1 )
{
error_str = "Only one REQUIREMENTS attribute can be defined.";
return -1;
}
reqs = dynamic_cast<SingleAttribute *>(array_reqs[0]);
if ( reqs == 0 )
{
NebulaLog::log("ONE",Log::ERROR,"Wrong format for REQUIREMENTS");
error_str = "Wrong format for REQUIREMENTS attribute.";
return -1;
}
@ -554,23 +471,15 @@ int VirtualMachine::parse_requirements()
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
int VirtualMachine::update(SqlDB * db)
{
return insert_replace(db, true);
}
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
int VirtualMachine::insert_replace(SqlDB *db, bool replace)
{
ostringstream oss;
int rc;
string xml_template;
string xml_body;
char * sql_deploy_id;
char * sql_name;
char * sql_template;
char * sql_xml;
sql_deploy_id = db->escape_str(deploy_id.c_str());
@ -586,15 +495,13 @@ int VirtualMachine::insert_replace(SqlDB *db, bool replace)
goto error_name;
}
vm_template->to_xml(xml_template);
sql_template = db->escape_str(xml_template.c_str());
sql_xml = db->escape_str(to_xml(xml_body).c_str());
if ( sql_template == 0 )
if ( sql_xml == 0 )
{
goto error_template;
goto error_body;
}
if(replace)
{
oss << "REPLACE";
@ -606,30 +513,23 @@ int VirtualMachine::insert_replace(SqlDB *db, bool replace)
oss << " INTO " << table << " ("<< db_names <<") VALUES ("
<< oid << ","
<< uid << ","
<< "'" << sql_name << "',"
<< "'" << sql_xml << "',"
<< uid << ","
<< last_poll << ","
<< state << ","
<< lcm_state << ","
<< stime << ","
<< etime << ","
<< "'" << sql_deploy_id << "',"
<< memory << ","
<< cpu << ","
<< net_tx << ","
<< net_rx << ","
<< last_seq << ","
<< "'" << sql_template << "')";
<< lcm_state << ")";
db->free_str(sql_deploy_id);
db->free_str(sql_name);
db->free_str(sql_template);
db->free_str(sql_xml);
rc = db->exec(oss);
return rc;
error_template:
error_body:
db->free_str(sql_name);
error_name:
db->free_str(sql_deploy_id);
@ -640,116 +540,12 @@ error_deploy:
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int VirtualMachine::dump( ostringstream& oss,
int num,
char **values,
char **names)
{
if ((!values[OID])||
(!values[UID])||
(!values[NAME]) ||
(!values[LAST_POLL])||
(!values[STATE])||
(!values[LCM_STATE])||
(!values[STIME])||
(!values[ETIME])||
(!values[DEPLOY_ID])||
(!values[MEMORY])||
(!values[CPU])||
(!values[NET_TX])||
(!values[NET_RX])||
(!values[LAST_SEQ])||
(!values[TEMPLATE])||
(num != LIMIT))
{
return -1;
}
oss <<
"<VM>" <<
"<ID>" << values[OID] << "</ID>" <<
"<UID>" << values[UID] << "</UID>" <<
"<NAME>" << values[NAME] << "</NAME>" <<
"<LAST_POLL>"<< values[LAST_POLL]<< "</LAST_POLL>"<<
"<STATE>" << values[STATE] << "</STATE>" <<
"<LCM_STATE>"<< values[LCM_STATE]<< "</LCM_STATE>"<<
"<STIME>" << values[STIME] << "</STIME>" <<
"<ETIME>" << values[ETIME] << "</ETIME>" <<
"<DEPLOY_ID>"<< values[DEPLOY_ID]<< "</DEPLOY_ID>"<<
"<MEMORY>" << values[MEMORY] << "</MEMORY>" <<
"<CPU>" << values[CPU] << "</CPU>" <<
"<NET_TX>" << values[NET_TX] << "</NET_TX>" <<
"<NET_RX>" << values[NET_RX] << "</NET_RX>" <<
"<LAST_SEQ>" << values[LAST_SEQ] << "</LAST_SEQ>" <<
values[TEMPLATE];
oss << "</VM>";
return 0;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int VirtualMachine::dump_extended( ostringstream& oss,
int num,
char **values,
char **names)
{
if ((!values[OID])||
(!values[UID])||
(!values[NAME]) ||
(!values[LAST_POLL])||
(!values[STATE])||
(!values[LCM_STATE])||
(!values[STIME])||
(!values[ETIME])||
(!values[DEPLOY_ID])||
(!values[MEMORY])||
(!values[CPU])||
(!values[NET_TX])||
(!values[NET_RX])||
(!values[LAST_SEQ])||
(!values[TEMPLATE])||
(num != (LIMIT + History::LIMIT + 1)))
{
return -1;
}
oss <<
"<VM>" <<
"<ID>" << values[OID] << "</ID>" <<
"<UID>" << values[UID] << "</UID>" <<
"<USERNAME>" << values[LIMIT] << "</USERNAME>"<<
"<NAME>" << values[NAME] << "</NAME>" <<
"<LAST_POLL>"<< values[LAST_POLL]<< "</LAST_POLL>"<<
"<STATE>" << values[STATE] << "</STATE>" <<
"<LCM_STATE>"<< values[LCM_STATE]<< "</LCM_STATE>"<<
"<STIME>" << values[STIME] << "</STIME>" <<
"<ETIME>" << values[ETIME] << "</ETIME>" <<
"<DEPLOY_ID>"<< values[DEPLOY_ID]<< "</DEPLOY_ID>"<<
"<MEMORY>" << values[MEMORY] << "</MEMORY>" <<
"<CPU>" << values[CPU] << "</CPU>" <<
"<NET_TX>" << values[NET_TX] << "</NET_TX>" <<
"<NET_RX>" << values[NET_RX] << "</NET_RX>" <<
"<LAST_SEQ>" << values[LAST_SEQ] << "</LAST_SEQ>" <<
values[TEMPLATE];
History::dump(oss, num-LIMIT-1, values+LIMIT+1, names+LIMIT+1);
oss << "</VM>";
return 0;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
void VirtualMachine::add_history(
int hid,
string& hostname,
string& vm_dir,
string& vmm_mad,
string& tm_mad)
int hid,
const string& hostname,
const string& vm_dir,
const string& vmm_mad,
const string& tm_mad)
{
ostringstream os;
int seq;
@ -770,8 +566,6 @@ void VirtualMachine::add_history(
previous_history = history;
}
last_seq = seq;
history = new History(oid,seq,hid,hostname,vm_dir,vmm_mad,tm_mad);
};
@ -803,8 +597,6 @@ void VirtualMachine::cp_history()
previous_history = history;
history = htmp;
last_seq = history->seq;
}
/* -------------------------------------------------------------------------- */
@ -832,8 +624,6 @@ void VirtualMachine::cp_previous_history()
previous_history = history;
history = htmp;
last_seq = history->seq;
}
/* -------------------------------------------------------------------------- */
@ -898,7 +688,7 @@ int VirtualMachine::get_disk_images(string& error_str)
continue;
}
rc = ipool->disk_attribute(disk, i, &index, &img_type);
rc = ipool->disk_attribute(disk, i, &index, &img_type, uid);
if (rc == 0 )
{
@ -1041,7 +831,7 @@ int VirtualMachine::get_network_leases()
continue;
}
rc = vnpool->nic_attribute(nic, oid);
rc = vnpool->nic_attribute(nic, uid, oid);
if (rc == -1)
{
@ -1304,6 +1094,7 @@ string& VirtualMachine::to_xml(string& xml) const
oss << "<VM>"
<< "<ID>" << oid << "</ID>"
<< "<UID>" << uid << "</UID>"
<< "<USERNAME>" << user_name << "</USERNAME>"
<< "<NAME>" << name << "</NAME>"
<< "<LAST_POLL>" << last_poll << "</LAST_POLL>"
<< "<STATE>" << state << "</STATE>"
@ -1315,7 +1106,6 @@ string& VirtualMachine::to_xml(string& xml) const
<< "<CPU>" << cpu << "</CPU>"
<< "<NET_TX>" << net_tx << "</NET_TX>"
<< "<NET_RX>" << net_rx << "</NET_RX>"
<< "<LAST_SEQ>" << last_seq << "</LAST_SEQ>"
<< vm_template->to_xml(template_xml);
if ( hasHistory() )
@ -1330,39 +1120,66 @@ string& VirtualMachine::to_xml(string& xml) const
return xml;
}
/* -------------------------------------------------------------------------- */
string& VirtualMachine::to_str(string& str) const
int VirtualMachine::from_xml(const string &xml_str)
{
string template_str;
string history_str;
vector<xmlNodePtr> content;
ostringstream oss;
int int_state;
int int_lcmstate;
int rc = 0;
oss<< "ID : " << oid << endl
<< "UID : " << uid << endl
<< "NAME : " << name << endl
<< "STATE : " << state << endl
<< "LCM STATE : " << lcm_state << endl
<< "DEPLOY ID : " << deploy_id << endl
<< "MEMORY : " << memory << endl
<< "CPU : " << cpu << endl
<< "LAST POLL : " << last_poll << endl
<< "START TIME : " << stime << endl
<< "STOP TIME : " << etime << endl
<< "NET TX : " << net_tx << endl
<< "NET RX : " << net_rx << endl
<< "LAST SEQ : " << last_seq << endl
<< "Template" << endl << vm_template->to_str(template_str) << endl;
// Initialize the internal XML object
update_from_str(xml_str);
if ( hasHistory() )
// Get class base attributes
rc += xpath(oid, "/VM/ID", -1);
rc += xpath(uid, "/VM/UID", -1);
rc += xpath(user_name, "/VM/USERNAME", "not_found");
rc += xpath(name, "/VM/NAME", "not_found");
rc += xpath(last_poll, "/VM/LAST_POLL",0);
rc += xpath(int_state, "/VM/STATE", 0);
rc += xpath(int_lcmstate,"/VM/LCM_STATE", 0);
rc += xpath(stime, "/VM/STIME", 0);
rc += xpath(etime, "/VM/ETIME", 0);
rc += xpath(deploy_id, "/VM/DEPLOY_ID","");
rc += xpath(memory, "/VM/MEMORY", 0);
rc += xpath(cpu, "/VM/CPU", 0);
rc += xpath(net_tx, "/VM/NET_TX", 0);
rc += xpath(net_rx, "/VM/NET_RX", 0);
state = static_cast<VmState>( int_state );
lcm_state = static_cast<LcmState>( int_lcmstate );
// Get associated classes
ObjectXML::get_nodes("/VM/TEMPLATE", content);
if( content.size() < 1 )
{
oss << "Last History Record" << endl << history->to_str(history_str);
return -1;
}
str = oss.str();
// Virtual Machine template
rc += vm_template->from_xml_node(content[0]);
return str;
// Last history entry
content.clear();
ObjectXML::get_nodes("/VM/HISTORY", content);
if( !content.empty() )
{
history = new History(oid);
rc += history->from_xml_node(content[0]);
}
if (rc != 0)
{
return -1;
}
return 0;
}
/* -------------------------------------------------------------------------- */

View File

@ -167,6 +167,7 @@ VirtualMachinePool::VirtualMachinePool(SqlDB * db,
int VirtualMachinePool::allocate (
int uid,
string user_name,
VirtualMachineTemplate * vm_template,
int * oid,
string& error_str,
@ -177,7 +178,7 @@ int VirtualMachinePool::allocate (
// ------------------------------------------------------------------------
// Build a new Virtual Machine object
// ------------------------------------------------------------------------
vm = new VirtualMachine(-1,vm_template);
vm = new VirtualMachine(-1, uid, user_name, vm_template);
if (on_hold == true)
{
@ -188,8 +189,6 @@ int VirtualMachinePool::allocate (
vm->state = VirtualMachine::PENDING;
}
vm->uid = uid;
// ------------------------------------------------------------------------
// Insert the Object in the pool
// ------------------------------------------------------------------------
@ -240,84 +239,27 @@ int VirtualMachinePool::get_pending(
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
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_extended_cb(
void * _oss,int num,char **values,char **names)
{
ostringstream * oss;
oss = static_cast<ostringstream *>(_oss);
return VirtualMachine::dump_extended(*oss, num, values, names);
}
/* -------------------------------------------------------------------------- */
int VirtualMachinePool::dump( ostringstream& oss,
bool extended,
int state,
const string& where)
{
int rc;
ostringstream cmd;
oss << "<VM_POOL>";
if(extended)
{
set_callback(
static_cast<Callbackable::Callback>(
&VirtualMachinePool::dump_extended_cb),
static_cast<void *>(&oss));
cmd << "SELECT " << VirtualMachine::extended_db_names
<< ", user_pool.user_name, "
<< History::extended_db_names << " FROM " << VirtualMachine::table
<< " LEFT OUTER JOIN " << History::table << " ON "
<< VirtualMachine::table << ".oid = " << History::table << ".vid AND "
<< History::table << ".seq = " << VirtualMachine::table
<< ".last_seq LEFT OUTER JOIN (SELECT oid,user_name FROM user_pool) "
<< "AS user_pool ON "<< VirtualMachine::table << ".uid = user_pool.oid";
}
else
{
set_callback(
static_cast<Callbackable::Callback>(&VirtualMachinePool::dump_cb),
static_cast<void *>(&oss));
cmd << "SELECT " << VirtualMachine::db_names << " FROM "
<< VirtualMachine::table;
}
ostringstream where_oss;
if ( state != -1 )
{
cmd << " WHERE " << VirtualMachine::table << ".state = " << state;
where_oss << VirtualMachine::table << ".state = " << state;
}
else
{
cmd << " WHERE " << VirtualMachine::table << ".state <> 6";
where_oss << VirtualMachine::table << ".state <> 6";
}
if ( !where.empty() )
{
cmd << " AND " << where;
where_oss << " AND " << where;
}
rc = db->exec(cmd,this);
oss << "</VM_POOL>";
unset_callback();
return rc;
return PoolSQL::dump(oss, "VM_POOL", VirtualMachine::table,where_oss.str());
}
/* -------------------------------------------------------------------------- */

View File

@ -24,6 +24,7 @@ env.Prepend(LIBS=[
'nebula_authm',
'nebula_template',
'nebula_pool',
'nebula_xml',
'nebula_image',
'nebula_mad',
'nebula_common',

View File

@ -25,6 +25,7 @@
using namespace std;
const int uids[] = {123, 261, 123};
const string user_names[] = {"A user","B user","C user"};
const string names[] = {"VM one", "Second VM", "VM one"};
@ -46,38 +47,37 @@ const string templates[] =
const string xmls[] =
{
"<VM><ID>0</ID><UID>123</UID><NAME>VM one</NAME><LAST_POLL>0</LAST_POLL><ST"
"<VM><ID>0</ID><UID>123</UID><USERNAME>A user</USERNAME><NAME>VM one</NAME><LAST_POLL>0</LAST_POLL><ST"
"ATE>1</STATE><LCM_STATE>0</LCM_STATE><STIME>0000000000</STIME><ETIME>0</ET"
"IME><DEPLOY_ID></DEPLOY_ID><MEMORY>0</MEMORY><CPU>0</CPU><NET_TX>0</NET_TX"
"><NET_RX>0</NET_RX><LAST_SEQ>-1</LAST_SEQ><TEMPLATE><CPU><![CDATA[1]]></CPU><MEMORY><![CDATA[128]"
"><NET_RX>0</NET_RX><TEMPLATE><CPU><![CDATA[1]]></CPU><MEMORY><![CDATA[128]"
"]></MEMORY><NAME><![CDATA[VM one]]></NAME><VMID><![CDATA[0]]></VMID>"
"</TEMPLATE></VM>",
"<VM><ID>1</ID><UID>261</UID><NAME>Second VM</NAME><LAST_POLL>0</LAST_POLL>"
"<VM><ID>1</ID><UID>261</UID><USERNAME>B user</USERNAME><NAME>Second VM</NAME><LAST_POLL>0</LAST_POLL>"
"<STATE>1</STATE><LCM_STATE>0</LCM_STATE><STIME>0000000000</STIME><ETIME>0<"
"/ETIME><DEPLOY_ID></DEPLOY_ID><MEMORY>0</MEMORY><CPU>0</CPU><NET_TX>0</NET"
"_TX><NET_RX>0</NET_RX><LAST_SEQ>-1</LAST_SEQ><TEMPLATE><CPU><![CDATA[2]]></CPU><MEMORY>"
"_TX><NET_RX>0</NET_RX><TEMPLATE><CPU><![CDATA[2]]></CPU><MEMORY>"
"<![CDATA[256]]></MEMORY><NAME><![CDATA[Second VM]]></NAME><VMID>"
"<![CDATA[1]]></VMID></TEMPLATE></VM>",
"<VM><ID>0</ID><UID>123</UID><NAME>VM one</NAME><LAST_POLL>0</LAST_POLL><ST"
"<VM><ID>0</ID><UID>123</UID><USERNAME>A user</USERNAME><NAME>VM one</NAME><LAST_POLL>0</LAST_POLL><ST"
"ATE>1</STATE><LCM_STATE>0</LCM_STATE><STIME>0000000000</STIME><ETIME>0</ET"
"IME><DEPLOY_ID></DEPLOY_ID><MEMORY>0</MEMORY><CPU>0</CPU><NET_TX>0</NET_TX"
"><NET_RX>0</NET_RX><LAST_SEQ>-1</LAST_SEQ><TEMPLATE><CPU>1</CPU><MEMORY>1024</MEMORY><NAME>VM one"
"><NET_RX>0</NET_RX><TEMPLATE><CPU>1</CPU><MEMORY>1024</MEMORY><NAME>VM one"
"</NAME><VMID>0</VMID></TEMPLATE></VM>"
};
// This xml dump result has the STIMEs modified to 0000000000
const string xml_dump =
"<VM_POOL><VM><ID>0</ID><UID>1</UID><USERNAME>A user</USERNAME><NAME>VM one</NAME><LAST_POLL>0</LAST_POLL><STATE>1</STATE><LCM_STATE>0</LCM_STATE><STIME>0000000000</STIME><ETIME>0</ETIME><DEPLOY_ID></DEPLOY_ID><MEMORY>0</MEMORY><CPU>0</CPU><NET_TX>0</NET_TX><NET_RX>0</NET_RX><LAST_SEQ>-1</LAST_SEQ><TEMPLATE><CPU><![CDATA[1]]></CPU><MEMORY><![CDATA[128]]></MEMORY><NAME><![CDATA[VM one]]></NAME><VMID><![CDATA[0]]></VMID></TEMPLATE></VM><VM><ID>1</ID><UID>2</UID><USERNAME>B user</USERNAME><NAME>Second VM</NAME><LAST_POLL>0</LAST_POLL><STATE>2</STATE><LCM_STATE>0</LCM_STATE><STIME>0000000000</STIME><ETIME>0</ETIME><DEPLOY_ID></DEPLOY_ID><MEMORY>0</MEMORY><CPU>0</CPU><NET_TX>0</NET_TX><NET_RX>0</NET_RX><LAST_SEQ>-1</LAST_SEQ><TEMPLATE><CPU><![CDATA[2]]></CPU><MEMORY><![CDATA[256]]></MEMORY><NAME><![CDATA[Second VM]]></NAME><VMID><![CDATA[1]]></VMID></TEMPLATE></VM></VM_POOL>";
"<VM_POOL><VM><ID>0</ID><UID>1</UID><USERNAME>A user</USERNAME><NAME>VM one</NAME><LAST_POLL>0</LAST_POLL><STATE>1</STATE><LCM_STATE>0</LCM_STATE><STIME>0000000000</STIME><ETIME>0</ETIME><DEPLOY_ID></DEPLOY_ID><MEMORY>0</MEMORY><CPU>0</CPU><NET_TX>0</NET_TX><NET_RX>0</NET_RX><TEMPLATE><CPU><![CDATA[1]]></CPU><MEMORY><![CDATA[128]]></MEMORY><NAME><![CDATA[VM one]]></NAME><VMID><![CDATA[0]]></VMID></TEMPLATE></VM><VM><ID>1</ID><UID>2</UID><USERNAME>B user</USERNAME><NAME>Second VM</NAME><LAST_POLL>0</LAST_POLL><STATE>2</STATE><LCM_STATE>0</LCM_STATE><STIME>0000000000</STIME><ETIME>0</ETIME><DEPLOY_ID></DEPLOY_ID><MEMORY>0</MEMORY><CPU>0</CPU><NET_TX>0</NET_TX><NET_RX>0</NET_RX><TEMPLATE><CPU><![CDATA[2]]></CPU><MEMORY><![CDATA[256]]></MEMORY><NAME><![CDATA[Second VM]]></NAME><VMID><![CDATA[1]]></VMID></TEMPLATE></VM></VM_POOL>";
const string xml_dump_where =
"<VM_POOL><VM><ID>0</ID><UID>1</UID><USERNAME>A user</USERNAME><NAME>VM one</NAME><LAST_POLL>0</LAST_POLL><STATE>1</STATE><LCM_STATE>0</LCM_STATE><STIME>0000000000</STIME><ETIME>0</ETIME><DEPLOY_ID></DEPLOY_ID><MEMORY>0</MEMORY><CPU>0</CPU><NET_TX>0</NET_TX><NET_RX>0</NET_RX><LAST_SEQ>-1</LAST_SEQ><TEMPLATE><CPU><![CDATA[1]]></CPU><MEMORY><![CDATA[128]]></MEMORY><NAME><![CDATA[VM one]]></NAME><VMID><![CDATA[0]]></VMID></TEMPLATE></VM></VM_POOL>";
"<VM_POOL><VM><ID>0</ID><UID>1</UID><USERNAME>A user</USERNAME><NAME>VM one</NAME><LAST_POLL>0</LAST_POLL><STATE>1</STATE><LCM_STATE>0</LCM_STATE><STIME>0000000000</STIME><ETIME>0</ETIME><DEPLOY_ID></DEPLOY_ID><MEMORY>0</MEMORY><CPU>0</CPU><NET_TX>0</NET_TX><NET_RX>0</NET_RX><TEMPLATE><CPU><![CDATA[1]]></CPU><MEMORY><![CDATA[128]]></MEMORY><NAME><![CDATA[VM one]]></NAME><VMID><![CDATA[0]]></VMID></TEMPLATE></VM></VM_POOL>";
const string xml_history_dump =
"<VM_POOL><VM><ID>0</ID><UID>0</UID><USERNAME>one_user_test</USERNAME><NAME>VM one</NAME><LAST_POLL>0</LAST_POLL><STATE>1</STATE><LCM_STATE>0</LCM_STATE><STIME>0000000000</STIME><ETIME>0</ETIME><DEPLOY_ID></DEPLOY_ID><MEMORY>0</MEMORY><CPU>0</CPU><NET_TX>0</NET_TX><NET_RX>0</NET_RX><LAST_SEQ>-1</LAST_SEQ><TEMPLATE><CPU><![CDATA[1]]></CPU><MEMORY><![CDATA[128]]></MEMORY><NAME><![CDATA[VM one]]></NAME><VMID><![CDATA[0]]></VMID></TEMPLATE></VM><VM><ID>1</ID><UID>0</UID><USERNAME>one_user_test</USERNAME><NAME>Second VM</NAME><LAST_POLL>0</LAST_POLL><STATE>2</STATE><LCM_STATE>0</LCM_STATE><STIME>0000000000</STIME><ETIME>0</ETIME><DEPLOY_ID></DEPLOY_ID><MEMORY>0</MEMORY><CPU>0</CPU><NET_TX>0</NET_TX><NET_RX>0</NET_RX><LAST_SEQ>0</LAST_SEQ><TEMPLATE><CPU><![CDATA[2]]></CPU><MEMORY><![CDATA[256]]></MEMORY><NAME><![CDATA[Second VM]]></NAME><VMID><![CDATA[1]]></VMID></TEMPLATE><HISTORY><SEQ>0</SEQ><HOSTNAME>A_hostname</HOSTNAME><HID>0</HID><STIME>0</STIME><ETIME>0</ETIME><PSTIME>0</PSTIME><PETIME>0</PETIME><RSTIME>0</RSTIME><RETIME>0</RETIME><ESTIME>0</ESTIME><EETIME>0</EETIME><REASON>0</REASON></HISTORY></VM><VM><ID>2</ID><UID>0</UID><USERNAME>one_user_test</USERNAME><NAME>VM one</NAME><LAST_POLL>0</LAST_POLL><STATE>2</STATE><LCM_STATE>0</LCM_STATE><STIME>0000000000</STIME><ETIME>0</ETIME><DEPLOY_ID></DEPLOY_ID><MEMORY>0</MEMORY><CPU>0</CPU><NET_TX>0</NET_TX><NET_RX>0</NET_RX><LAST_SEQ>1</LAST_SEQ><TEMPLATE><CPU><![CDATA[1]]></CPU><MEMORY><![CDATA[1024]]></MEMORY><NAME><![CDATA[VM one]]></NAME><VMID><![CDATA[2]]></VMID></TEMPLATE><HISTORY><SEQ>1</SEQ><HOSTNAME>C_hostname</HOSTNAME><HID>2</HID><STIME>0</STIME><ETIME>0</ETIME><PSTIME>0</PSTIME><PETIME>0</PETIME><RSTIME>0</RSTIME><RETIME>0</RETIME><ESTIME>0</ESTIME><EETIME>0</EETIME><REASON>0</REASON></HISTORY></VM></VM_POOL>";
"<VM_POOL><VM><ID>0</ID><UID>0</UID><USERNAME>one_user_test</USERNAME><NAME>VM one</NAME><LAST_POLL>0</LAST_POLL><STATE>1</STATE><LCM_STATE>0</LCM_STATE><STIME>0000000000</STIME><ETIME>0</ETIME><DEPLOY_ID></DEPLOY_ID><MEMORY>0</MEMORY><CPU>0</CPU><NET_TX>0</NET_TX><NET_RX>0</NET_RX><TEMPLATE><CPU><![CDATA[1]]></CPU><MEMORY><![CDATA[128]]></MEMORY><NAME><![CDATA[VM one]]></NAME><VMID><![CDATA[0]]></VMID></TEMPLATE></VM><VM><ID>1</ID><UID>0</UID><USERNAME>one_user_test</USERNAME><NAME>Second VM</NAME><LAST_POLL>0</LAST_POLL><STATE>2</STATE><LCM_STATE>0</LCM_STATE><STIME>0000000000</STIME><ETIME>0</ETIME><DEPLOY_ID></DEPLOY_ID><MEMORY>0</MEMORY><CPU>0</CPU><NET_TX>0</NET_TX><NET_RX>0</NET_RX><TEMPLATE><CPU><![CDATA[2]]></CPU><MEMORY><![CDATA[256]]></MEMORY><NAME><![CDATA[Second VM]]></NAME><VMID><![CDATA[1]]></VMID></TEMPLATE><HISTORY><SEQ>0</SEQ><HOSTNAME>A_hostname</HOSTNAME><VM_DIR>A_vm_dir</VM_DIR><HID>0</HID><STIME>0</STIME><ETIME>0</ETIME><VMMMAD>A_vmm_mad</VMMMAD><TMMAD>A_tm_mad</TMMAD><PSTIME>0</PSTIME><PETIME>0</PETIME><RSTIME>0</RSTIME><RETIME>0</RETIME><ESTIME>0</ESTIME><EETIME>0</EETIME><REASON>0</REASON></HISTORY></VM><VM><ID>2</ID><UID>0</UID><USERNAME>one_user_test</USERNAME><NAME>VM one</NAME><LAST_POLL>0</LAST_POLL><STATE>2</STATE><LCM_STATE>0</LCM_STATE><STIME>0000000000</STIME><ETIME>0</ETIME><DEPLOY_ID></DEPLOY_ID><MEMORY>0</MEMORY><CPU>0</CPU><NET_TX>0</NET_TX><NET_RX>0</NET_RX><TEMPLATE><CPU><![CDATA[1]]></CPU><MEMORY><![CDATA[1024]]></MEMORY><NAME><![CDATA[VM one]]></NAME><VMID><![CDATA[2]]></VMID></TEMPLATE><HISTORY><SEQ>1</SEQ><HOSTNAME>C_hostname</HOSTNAME><VM_DIR>C_vm_dir</VM_DIR><HID>2</HID><STIME>0</STIME><ETIME>0</ETIME><VMMMAD>C_vmm_mad</VMMMAD><TMMAD>C_tm_mad</TMMAD><PSTIME>0</PSTIME><PETIME>0</PETIME><RSTIME>0</RSTIME><RETIME>0</RETIME><ESTIME>0</ESTIME><EETIME>0</EETIME><REASON>0</REASON></HISTORY></VM></VM_POOL>";
const string replacement = "0000000000";
@ -94,6 +94,7 @@ public:
int allocate (
int uid,
const string& user_name,
const string& stemplate,
int * oid,
bool on_hold = false)
@ -108,8 +109,8 @@ public:
if( rc == 0 )
{
return VirtualMachinePool::allocate(uid, vm_template, oid,
err, on_hold);
return VirtualMachinePool::allocate(uid, user_name,
vm_template, oid, err, on_hold);
}
else
{
@ -158,6 +159,7 @@ protected:
{
int oid;
return ((VirtualMachinePoolFriend*)pool)->allocate( uids[index],
user_names[index],
templates[index],
&oid, false);
};
@ -171,31 +173,19 @@ protected:
// Get the xml and replace the STIME to 0, so we can compare it
((VirtualMachine*)obj)->to_xml(xml_str);
xml_str.replace( xml_str.find("<STIME>")+7, 10, replacement);
//cout << endl << xml_str << endl;
/*
if( xml_str != xmls[index] )
{
cout << endl << xml_str << endl << "========"
<< endl << xmls[index] << endl << "--------";
}
//*/
CPPUNIT_ASSERT( ((VirtualMachine*)obj)->get_name() == names[index] );
CPPUNIT_ASSERT( xml_str == xmls[index]);
};
void set_up_user_pool()
{
string err;
UserPool::bootstrap(db);
UserPool * user_pool = new UserPool(db);
int uid_1, uid_2;
string username_1 = "A user";
string username_2 = "B user";
string pass_1 = "A pass";
string pass_2 = "B pass";
user_pool->allocate(&uid_1, username_1, pass_1, true, err);
user_pool->allocate(&uid_2, username_2, pass_2, true, err);
delete user_pool;
};
public:
VirtualMachinePoolTest(){xmlInitParser();};
@ -263,20 +253,26 @@ public:
VirtualMachinePoolFriend * vmp =
static_cast<VirtualMachinePoolFriend*>(pool);
set_up_user_pool();
ostringstream oss;
int oid, rc;
vmp->allocate(1, templates[0], &oid, false);
vmp->allocate(2, templates[1], &oid, true);
vmp->allocate(1, user_names[0], templates[0], &oid, false);
vmp->allocate(2, user_names[1], templates[1], &oid, true);
rc = vmp->dump(oss, "");
CPPUNIT_ASSERT(rc == 0);
string result = oss.str();
result.replace(152, 10, replacement);
result.replace(583, 10, replacement);
result.replace(560, 10, replacement);
/*
if( result != xml_dump )
{
cout << endl << result << endl << "========"
<< endl << xml_dump << endl << "--------";
}
//*/
CPPUNIT_ASSERT( result == xml_dump );
}
@ -286,14 +282,12 @@ public:
VirtualMachinePoolFriend * vmp =
static_cast<VirtualMachinePoolFriend*>(pool);
set_up_user_pool();
int oid, rc;
ostringstream oss;
ostringstream where;
vmp->allocate(1, templates[0], &oid, false);
vmp->allocate(2, templates[1], &oid, true);
vmp->allocate(1, user_names[0], templates[0], &oid, false);
vmp->allocate(2, user_names[1], templates[1], &oid, true);
where << "uid < 2";
rc = vmp->dump(oss, where.str());
@ -320,16 +314,14 @@ public:
ostringstream where;
set_up_user_pool();
// Allocate a VM
rc = vmp->allocate(0, templates[0], &oid, false);
rc = vmp->allocate(0, "one_user_test", templates[0], &oid, false);
CPPUNIT_ASSERT( rc == oid );
CPPUNIT_ASSERT( oid >= 0 );
//----------------------------------------------------------------------
// Allocate a VM with one history item
rc = vmp->allocate(0, templates[1], &oid, true);
rc = vmp->allocate(0, "one_user_test", templates[1], &oid, true);
CPPUNIT_ASSERT( rc == oid );
CPPUNIT_ASSERT( oid >= 0 );
@ -347,7 +339,7 @@ public:
//----------------------------------------------------------------------
// Allocate a VM with two history items
rc = vmp->allocate(0, templates[2], &oid, true);
rc = vmp->allocate(0, "one_user_test", templates[2], &oid, true);
CPPUNIT_ASSERT( rc == oid );
CPPUNIT_ASSERT( oid >= 0 );
@ -374,7 +366,7 @@ public:
//----------------------------------------------------------------------
// Allocate a VM, will be set to DONE
rc = vmp->allocate(1, templates[0], &oid, false);
rc = vmp->allocate(1, "one_user_test", templates[0], &oid, false);
CPPUNIT_ASSERT( rc == oid );
CPPUNIT_ASSERT( oid >= 0 );
@ -398,8 +390,16 @@ public:
string result = oss.str();
result.replace(159, 10, replacement);
result.replace(597, 10, replacement);
result.replace(1266,10, replacement);
result.replace(574, 10, replacement);
result.replace(1295,10, replacement);
/*
if( result != xml_history_dump )
{
cout << endl << result << endl << "========"
<< endl << xml_history_dump << endl << "--------";
}
//*/
CPPUNIT_ASSERT( result == xml_history_dump );
}
@ -472,7 +472,5 @@ public:
int main(int argc, char ** argv)
{
OneUnitTest::set_one_auth();
return PoolTest::main(argc, argv, VirtualMachinePoolTest::suite());
}

View File

@ -620,7 +620,7 @@ static int input (void );
/* This used to be an fputs(), but since the string might contain NUL's,
* we now use fwrite().
*/
#define ECHO fwrite( vm_var_text, vm_var_leng, 1, vm_var_out )
#define ECHO do { if (fwrite( vm_var_text, vm_var_leng, 1, vm_var_out )) {} } while (0)
#endif
/* Gets input and stuffs it into "buf". number of characters read, or YY_NULL,
@ -631,7 +631,7 @@ static int input (void );
if ( YY_CURRENT_BUFFER_LVALUE->yy_is_interactive ) \
{ \
int c = '*'; \
int n; \
unsigned n; \
for ( n = 0; n < max_size && \
(c = getc( vm_var_in )) != EOF && c != '\n'; ++n ) \
buf[n] = (char) c; \

View File

@ -1,9 +1,9 @@
/* A Bison parser, made by GNU Bison 2.4.2. */
/* A Bison parser, made by GNU Bison 2.4.3. */
/* Skeleton implementation for Bison's Yacc-like parsers in C
Copyright (C) 1984, 1989-1990, 2000-2006, 2009-2010 Free Software
Foundation, Inc.
Copyright (C) 1984, 1989, 1990, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
2009, 2010 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@ -45,7 +45,7 @@
#define YYBISON 1
/* Bison version. */
#define YYBISON_VERSION "2.4.2"
#define YYBISON_VERSION "2.4.3"
/* Skeleton name. */
#define YYSKELETON_NAME "yacc.c"
@ -180,7 +180,7 @@ void get_network_attribute(VirtualMachine * vm,
return;
}
vn = vnpool->get(network,true);
vn = vnpool->get(network, vm->get_uid(), true);
if ( vn == 0 )
{
@ -1374,7 +1374,7 @@ YYLTYPE yylloc;
YYLTYPE *yylsp;
/* The locations where the error started and ended. */
YYLTYPE yyerror_range[2];
YYLTYPE yyerror_range[3];
YYSIZE_T yystacksize;
@ -1754,7 +1754,7 @@ yyerrlab:
#endif
}
yyerror_range[0] = yylloc;
yyerror_range[1] = yylloc;
if (yyerrstatus == 3)
{
@ -1791,7 +1791,7 @@ yyerrorlab:
if (/*CONSTCOND*/ 0)
goto yyerrorlab;
yyerror_range[0] = yylsp[1-yylen];
yyerror_range[1] = yylsp[1-yylen];
/* Do not reclaim the symbols of the rule which action triggered
this YYERROR. */
YYPOPSTACK (yylen);
@ -1825,7 +1825,7 @@ yyerrlab1:
if (yyssp == yyss)
YYABORT;
yyerror_range[0] = *yylsp;
yyerror_range[1] = *yylsp;
yydestruct ("Error: popping",
yystos[yystate], yyvsp, yylsp, mc, vm, parsed, errmsg);
YYPOPSTACK (1);
@ -1835,10 +1835,10 @@ yyerrlab1:
*++yyvsp = yylval;
yyerror_range[1] = yylloc;
yyerror_range[2] = yylloc;
/* Using YYLLOC is tempting, but would change the location of
the lookahead. YYLOC is available though. */
YYLLOC_DEFAULT (yyloc, (yyerror_range - 1), 2);
YYLLOC_DEFAULT (yyloc, yyerror_range, 2);
*++yylsp = yyloc;
/* Shift the error token. */

View File

@ -1,9 +1,9 @@
/* A Bison parser, made by GNU Bison 2.4.2. */
/* A Bison parser, made by GNU Bison 2.4.3. */
/* Skeleton interface for Bison's Yacc-like parsers in C
Copyright (C) 1984, 1989-1990, 2000-2006, 2009-2010 Free Software
Foundation, Inc.
Copyright (C) 1984, 1989, 1990, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
2009, 2010 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by

View File

@ -118,7 +118,7 @@ void get_network_attribute(VirtualMachine * vm,
return;
}
vn = vnpool->get(network,true);
vn = vnpool->get(network, vm->get_uid(), true);
if ( vn == 0 )
{

View File

@ -62,6 +62,10 @@ int FixedLeases::add(const string& ip, const string& mac, int vid,
unsigned int _ip;
unsigned int _mac [2];
Lease * lease;
string xml_body;
char * sql_xml;
int rc;
if ( Leases::Lease::ip_to_number(ip,_ip) )
@ -84,13 +88,21 @@ int FixedLeases::add(const string& ip, const string& mac, int vid,
goto error_mac;
}
oss << "INSERT INTO " << table << " ("<< db_names <<") VALUES (" <<
oid << "," <<
_ip << "," <<
_mac[Lease::PREFIX] << "," <<
_mac[Lease::SUFFIX] << "," <<
vid << "," <<
used << ")";
lease = new Lease(_ip,_mac,vid,used);
sql_xml = db->escape_str(lease->to_xml_db(xml_body).c_str());
if ( sql_xml == 0 )
{
goto error_body;
}
oss << "INSERT INTO " << table << " ("<< db_names <<") VALUES ("
<< oid << ","
<< _ip << ","
<< "'" << sql_xml << "')";
db->free_str(sql_xml);
rc = db->exec(oss);
@ -99,9 +111,14 @@ int FixedLeases::add(const string& ip, const string& mac, int vid,
goto error_db;
}
leases.insert(make_pair(_ip,new Lease(_ip,_mac,vid,used)));
return rc;
leases.insert( make_pair(_ip,lease) );
if(lease->used)
{
n_used++;
}
return rc;
error_ip:
oss.str("");
@ -118,9 +135,16 @@ error_duplicate:
oss << "Error inserting lease, IP " << ip << " already exists";
goto error_common;
error_body:
oss.str("");
oss << "Error inserting lease, marshall error";
delete lease;
goto error_common;
error_db:
oss.str("");
oss << "Error inserting lease in database.";
delete lease;
error_common:
NebulaLog::log("VNM", Log::ERROR, oss);
@ -167,7 +191,10 @@ int FixedLeases::remove(const string& ip, string& error_msg)
goto error_db;
}
delete it->second;
leases.erase(it);
return rc;
@ -202,7 +229,6 @@ error_common:
int FixedLeases::unset(const string& ip)
{
unsigned int _ip;
ostringstream oss;
map<unsigned int, Lease *>::iterator it_ip;
@ -219,14 +245,11 @@ int FixedLeases::unset(const string& ip)
}
// Flip used flag to false
it_ip->second->used = false;
it_ip->second->vid = -1;
oss << "UPDATE " << table << " SET used='0', vid='-1' "
<< " WHERE oid=" << oid << " AND ip='" << _ip <<"'";
return db->exec(oss);
// Update the lease
return update_lease(it_ip->second);
}
/* -------------------------------------------------------------------------- */
@ -234,7 +257,7 @@ int FixedLeases::unset(const string& ip)
int FixedLeases::get(int vid, string& ip, string& mac)
{
int rc = -1;
int rc = -1;
if (leases.empty())
{
@ -252,22 +275,14 @@ int FixedLeases::get(int vid, string& ip, string& mac)
{
ostringstream oss;
oss << "UPDATE " << table << " SET used='1', vid='" << vid
<< "' WHERE oid=" << oid
<< " AND ip='" << current->second->ip <<"'";
current->second->used = true;
current->second->vid = vid;
rc = db->exec(oss);
rc = update_lease(current->second);
if ( rc == 0 )
{
current->second->used = true;
current->second->vid = vid;
current->second->to_string(ip,mac);
current++;
}
current->second->to_string(ip,mac);
current++;
break;
}
}
@ -282,7 +297,6 @@ int FixedLeases::set(int vid, const string& ip, string& mac)
{
map<unsigned int,Lease *>::iterator it;
ostringstream oss;
unsigned int num_ip;
int rc;
@ -304,22 +318,45 @@ int FixedLeases::set(int vid, const string& ip, string& mac)
return -1;
}
oss << "UPDATE " << table << " SET used='1', vid='" << vid << "'"
<< " WHERE oid=" << oid << " AND ip='" << it->second->ip <<"'";
rc = db->exec(oss);
if ( rc != 0 )
{
return -1;
}
it->second->used = true;
it->second->vid = vid;
Leases::Lease::mac_to_string(it->second->mac,mac);
return 0;
return update_lease(it->second);
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int FixedLeases::update_lease(Lease * lease)
{
ostringstream oss;
string xml_body;
char * sql_xml;
sql_xml = db->escape_str(lease->to_xml_db(xml_body).c_str());
if ( sql_xml == 0 )
{
return -1;
}
if( lease->used )
{
n_used++;
}
else
{
n_used--;
}
oss << "UPDATE " << table << " SET body='" << sql_xml << "'"
<< " WHERE oid=" << oid << " AND ip='" << lease->ip <<"'";
db->free_str(sql_xml);
return db->exec(oss);
}
/* -------------------------------------------------------------------------- */

View File

@ -181,6 +181,8 @@ void Leases::Lease::mac_to_string(const unsigned int i_mac[], string& mac)
mac = oss.str();
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
ostream& operator<<(ostream& os, Leases::Lease& _lease)
{
@ -191,32 +193,8 @@ ostream& operator<<(ostream& os, Leases::Lease& _lease)
return os;
}
string& Leases::Lease::to_str(string& str) const
{
string ip;
string mac;
ostringstream os;
to_string(ip,mac);
ip = "IP = " + ip;
mac = "MAC = " + mac;
os.width(20);
os << left << ip;
os.width(24);
os << left << mac;
os << left << " USED = " << used;
os << left << " VID = " << vid;
str = os.str();
return str;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
string& Leases::Lease::to_xml(string& str) const
{
@ -229,10 +207,10 @@ string& Leases::Lease::to_xml(string& str) const
os <<
"<LEASE>" <<
"<IP>"<< ip << "</IP>" <<
"<MAC>" << mac << "</MAC>" <<
"<USED>" << used << "</USED>" <<
"<VID>" << vid << "</VID>" <<
"<IP>" << ip << "</IP>" <<
"<MAC>" << mac << "</MAC>" <<
"<USED>"<< used << "</USED>"<<
"<VID>" << vid << "</VID>" <<
"</LEASE>";
str = os.str();
@ -240,6 +218,54 @@ string& Leases::Lease::to_xml(string& str) const
return str;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
string& Leases::Lease::to_xml_db(string& str) const
{
ostringstream os;
os <<
"<LEASE>" <<
"<IP>" << ip << "</IP>" <<
"<MAC_PREFIX>" << mac[Lease::PREFIX] << "</MAC_PREFIX>" <<
"<MAC_SUFFIX>" << mac[Lease::SUFFIX] << "</MAC_SUFFIX>" <<
"<USED>" << used << "</USED>"<<
"<VID>" << vid << "</VID>" <<
"</LEASE>";
str = os.str();
return str;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int Leases::Lease::from_xml(const string &xml_str)
{
int rc = 0;
int int_used;
// Initialize the internal XML object
update_from_str(xml_str);
rc += xpath(ip , "/LEASE/IP" , 0);
rc += xpath(mac[Lease::PREFIX], "/LEASE/MAC_PREFIX" , 0);
rc += xpath(mac[Lease::SUFFIX], "/LEASE/MAC_SUFFIX" , 0);
rc += xpath(int_used , "/LEASE/USED" , 0);
rc += xpath(vid , "/LEASE/VID" , 0);
used = static_cast<bool>(int_used);
if (rc != 0)
{
return -1;
}
return 0;
}
/* ************************************************************************** */
/* ************************************************************************** */
/* Leases class */
@ -248,55 +274,38 @@ string& Leases::Lease::to_xml(string& str) const
const char * Leases::table = "leases";
const char * Leases::db_names = "oid,ip,mac_prefix,mac_suffix,vid,used";
const char * Leases::db_names = "oid, ip, body";
const char * Leases::db_bootstrap = "CREATE TABLE IF NOT EXISTS leases ("
"oid INTEGER, ip BIGINT, mac_prefix BIGINT, mac_suffix BIGINT,"
"vid INTEGER, used INTEGER, PRIMARY KEY(oid,ip))";
"oid INTEGER, ip BIGINT, body TEXT, PRIMARY KEY(oid,ip))";
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int Leases::select_cb(void *nil, int num, char **values, char **names)
{
if ( (values[OID] == 0) ||
(values[IP] == 0) ||
(values[MAC_PREFIX] == 0) ||
(values[MAC_SUFFIX] == 0) ||
(values[VID] == 0) ||
(values[USED]== 0) ||
(num != LIMIT ))
Lease * lease;
int rc;
if ( (!values[0]) || (num != 1) )
{
return -1;
}
unsigned int mac[2];
unsigned int ip;
int vid;
bool used;
lease = new Lease();
rc = lease->from_xml(values[0]);
istringstream iss;
if (rc != 0)
{
return -1;
}
iss.str(values[IP]);
iss >> ip;
leases.insert(make_pair(lease->ip, lease));
iss.clear();
iss.str(values[MAC_PREFIX]);
iss >> mac[Lease::PREFIX];
iss.clear();
iss.str(values[MAC_SUFFIX]);
iss >> mac[Lease::SUFFIX];
iss.clear();
iss.str(values[VID]);
iss >> vid;
iss.clear();
iss.str(values[USED]);
iss >> used;
leases.insert(make_pair(ip,new Lease(ip,mac,vid,used)));
if(lease->used)
{
n_used++;
}
return 0;
}
@ -308,9 +317,12 @@ int Leases::select(SqlDB * db)
ostringstream oss;
int rc;
// Reset the used leases counter
n_used = 0;
set_callback(static_cast<Callbackable::Callback>(&Leases::select_cb));
oss << "SELECT " << db_names << " FROM " << table << " WHERE oid = " << oid;
oss << "SELECT body FROM " << table << " WHERE oid = " << oid;
rc = db->exec(oss,this);
@ -434,25 +446,6 @@ string& Leases::to_xml(string& xml) const
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
string& Leases::to_str(string& str) const
{
map<unsigned int, Leases::Lease *>::const_iterator it;
ostringstream os;
string lease_str;
for(it=leases.begin();it!=leases.end();it++)
{
os << it->second->to_str(lease_str) << endl;
}
str = os.str();
return str;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
ostream& operator<<(ostream& os, Leases& _leases)
{
string xml;

View File

@ -132,26 +132,56 @@ int RangedLeases::add(
int vid,
bool used)
{
ostringstream oss;
int rc;
ostringstream oss;
//Insert the lease in the database
oss << "INSERT INTO " << table << " ("<< db_names <<") VALUES ("<<
oid << "," <<
ip << "," <<
mac[Lease::PREFIX] << "," <<
mac[Lease::SUFFIX] << "," <<
vid << "," <<
used << ")";
Lease * lease;
string xml_body;
char * sql_xml;
int rc;
lease = new Lease(ip,mac,vid,used);
sql_xml = db->escape_str(lease->to_xml_db(xml_body).c_str());
if ( sql_xml == 0 )
{
goto error_body;
}
oss << "INSERT INTO " << table << " ("<< db_names <<") VALUES ("
<< oid << ","
<< ip << ","
<< "'" << sql_xml << "')";
db->free_str(sql_xml);
rc = db->exec(oss);
if ( rc == 0 )
if ( rc != 0 )
{
leases.insert(make_pair(ip,new Lease(ip,mac,vid,used)));
goto error_db;
}
leases.insert( make_pair(ip,lease) );
n_used++;
return rc;
error_body:
oss.str("");
oss << "Error inserting lease, marshall error";
goto error_common;
error_db:
oss.str("");
oss << "Error inserting lease in database.";
error_common:
NebulaLog::log("VNM", Log::ERROR, oss);
return -1;
}
/* -------------------------------------------------------------------------- */
@ -187,6 +217,8 @@ int RangedLeases::del(const string& ip)
if ( rc == 0 )
{
n_used--;
delete it_ip->second;
leases.erase(it_ip);

View File

@ -29,10 +29,11 @@
/* Virtual Network :: Constructor/Destructor */
/* ************************************************************************** */
VirtualNetwork::VirtualNetwork(VirtualNetworkTemplate *_vn_template):
PoolObjectSQL(-1),
name(""),
uid(-1),
VirtualNetwork::VirtualNetwork(int uid,
string _user_name,
VirtualNetworkTemplate *_vn_template):
PoolObjectSQL(-1,"",uid,table),
user_name(_user_name),
bridge(""),
type(UNINITIALIZED),
leases(0)
@ -69,85 +70,57 @@ VirtualNetwork::~VirtualNetwork()
const char * VirtualNetwork::table = "network_pool";
const char * VirtualNetwork::db_names =
"oid,uid,name,type,bridge,public,template";
const char * VirtualNetwork::extended_db_names =
"network_pool.oid, network_pool.uid, network_pool.name, network_pool.type, "
"network_pool.bridge, network_pool.public, network_pool.template";
const char * VirtualNetwork::db_names = "oid, name, body, uid";
const char * VirtualNetwork::db_bootstrap = "CREATE TABLE IF NOT EXISTS"
" network_pool ("
"oid INTEGER PRIMARY KEY, uid INTEGER, name VARCHAR(256), type INTEGER, "
"bridge TEXT, public INTEGER, template TEXT, UNIQUE(name))";
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int VirtualNetwork::select_cb(void * nil, int num, char **values, char **names)
{
if ((!values[OID]) ||
(!values[UID]) ||
(!values[NAME]) ||
(!values[TYPE]) ||
(!values[BRIDGE]) ||
(!values[PUBLIC]) ||
(!values[TEMPLATE]) ||
(num != LIMIT ))
{
return -1;
}
oid = atoi(values[OID]);
uid = atoi(values[UID]);
name = values[NAME];
type = (NetworkType)atoi(values[TYPE]);
bridge = values[BRIDGE];
public_vnet = atoi(values[PUBLIC]);
// Virtual Network template
vn_template->from_xml(values[TEMPLATE]);
return 0;
}
" network_pool (oid INTEGER PRIMARY KEY, name VARCHAR(256),"
" body TEXT, uid INTEGER, UNIQUE(name,uid))";
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int VirtualNetwork::select(SqlDB * db)
{
int rc;
rc = PoolObjectSQL::select(db);
if ( rc != 0 )
{
return rc;
}
return select_leases(db);
}
/* -------------------------------------------------------------------------- */
int VirtualNetwork::select(SqlDB * db, const string& name, int uid)
{
int rc;
rc = PoolObjectSQL::select(db,name,uid);
if ( rc != 0 )
{
return rc;
}
return select_leases(db);
}
/* -------------------------------------------------------------------------- */
int VirtualNetwork::select_leases(SqlDB * db)
{
ostringstream oss;
ostringstream ose;
int rc;
int boid;
string network_address;
unsigned int default_size = VirtualNetworkPool::default_size();
unsigned int mac_prefix = VirtualNetworkPool::mac_prefix();
set_callback(
static_cast<Callbackable::Callback>(&VirtualNetwork::select_cb));
oss << "SELECT " << db_names << " FROM " << table << " WHERE oid = " << oid;
boid = oid;
oid = -1;
rc = db->exec(oss, this);
unset_callback();
if ((rc != 0) || (oid != boid ))
{
goto error_id;
}
//Get the leases
if (type == RANGED)
{
@ -207,9 +180,6 @@ int VirtualNetwork::select(SqlDB * db)
return leases->select(db);
error_id:
ose << "Error getting Virtual Network nid: " << oid;
goto error_common;
error_leases:
ose << "Error getting Virtual Network leases nid: " << oid;
@ -230,43 +200,6 @@ error_common:
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int VirtualNetwork::dump(ostringstream& oss,
int num,
char ** values,
char ** names)
{
if ((!values[OID]) ||
(!values[UID]) ||
(!values[NAME]) ||
(!values[TYPE]) ||
(!values[BRIDGE]) ||
(!values[PUBLIC]) ||
(!values[TEMPLATE]) ||
(!values[LIMIT]) ||
(num != LIMIT + 2 ))
{
return -1;
}
oss <<
"<VNET>" <<
"<ID>" << values[OID] << "</ID>" <<
"<UID>" << values[UID] << "</UID>" <<
"<USERNAME>" << values[LIMIT+1] << "</USERNAME>" <<
"<NAME>" << values[NAME] << "</NAME>" <<
"<TYPE>" << values[TYPE] << "</TYPE>" <<
"<BRIDGE>" << values[BRIDGE] << "</BRIDGE>" <<
"<PUBLIC>" << values[PUBLIC] << "</PUBLIC>" <<
"<TOTAL_LEASES>"<< values[LIMIT] << "</TOTAL_LEASES>"
<< values[TEMPLATE] <<
"</VNET>";
return 0;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int VirtualNetwork::insert(SqlDB * db, string& error_str)
{
ostringstream ose;
@ -295,9 +228,13 @@ int VirtualNetwork::insert(SqlDB * db, string& error_str)
{
type = VirtualNetwork::FIXED;
}
else if ( s_type.empty() )
{
goto error_type_defined;
}
else
{
goto error_type;
goto error_wrong_type;
}
// ------------ NAME ----------------------
@ -410,32 +347,34 @@ int VirtualNetwork::insert(SqlDB * db, string& error_str)
return 0;
error_type:
ose << "Wrong type in template for Virtual Network, id: ";
error_type_defined:
ose << "No TYPE in template for Virtual Network.";
goto error_common;
error_wrong_type:
ose << "Wrong type \""<< s_type <<"\" in template for Virtual Network.";
goto error_common;
error_name:
ose << "No NAME in template for Virtual Network, id: ";
ose << "No NAME in template for Virtual Network.";
goto error_common;
error_bridge:
ose << "No BRIDGE in template for Virtual Network, id: ";
ose << "No BRIDGE in template for Virtual Network.";
goto error_common;
error_update:
ose << "Can not update Virtual Network, id: ";
ose << "Can not update Virtual Network.";
goto error_common;
error_addr:
ose << "Network address is not defined, id: ";
ose << "No NETWORK_ADDRESS in template for Virtual Network.";
goto error_common;
error_null_leases:
ose << "Error getting Virtual Network leases, id: ";
ose << "Error getting Virtual Network leases.";
error_common:
ose << oid << ".";
error_str = ose.str();
NebulaLog::log("VNM", Log::ERROR, ose);
return -1;
@ -444,23 +383,16 @@ error_common:
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int VirtualNetwork::update(SqlDB * db)
{
return insert_replace(db, true);
}
int VirtualNetwork::insert_replace(SqlDB *db, bool replace)
{
ostringstream oss;
int rc;
string xml_template;
string xml_body;
char * sql_name;
char * sql_bridge;
char * sql_template;
char * sql_xml;
sql_name = db->escape_str(name.c_str());
@ -470,22 +402,13 @@ int VirtualNetwork::insert_replace(SqlDB *db, bool replace)
goto error_name;
}
sql_bridge = db->escape_str(bridge.c_str());
sql_xml = db->escape_str(to_xml(xml_body).c_str());
if ( sql_bridge == 0 )
if ( sql_xml == 0 )
{
goto error_bridge;
goto error_body;
}
vn_template->to_xml(xml_template);
sql_template = db->escape_str(xml_template.c_str());
if ( sql_template == 0 )
{
goto error_template;
}
// Construct the SQL statement to Insert or Replace
if(replace)
{
@ -498,24 +421,19 @@ int VirtualNetwork::insert_replace(SqlDB *db, bool replace)
oss << " INTO " << table << " (" << db_names << ") VALUES ("
<< oid << ","
<< uid << ","
<< "'" << sql_name << "',"
<< type << ","
<< "'" << sql_bridge << "',"
<< public_vnet << ","
<< "'" << sql_template<< "')";
<< "'" << sql_xml << "',"
<< uid << ")";
rc = db->exec(oss);
db->free_str(sql_name);
db->free_str(sql_bridge);
db->free_str(sql_template);
db->free_str(sql_xml);
return rc;
error_template:
db->free_str(sql_bridge);
error_bridge:
error_body:
db->free_str(sql_name);
error_name:
return -1;
@ -524,30 +442,20 @@ error_name:
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int VirtualNetwork::vn_drop(SqlDB * db)
int VirtualNetwork::drop(SqlDB * db)
{
ostringstream oss;
int rc;
int rc;
if ( leases != 0 )
rc = PoolObjectSQL::drop(db);
if ( rc == 0 && leases != 0 )
{
leases->drop(db);
}
oss << "DELETE FROM " << table << " WHERE OID=" << oid;
rc = db->exec(oss);
if ( rc == 0 )
{
set_valid(false);
rc += leases->drop(db);
}
return rc;
}
/* ************************************************************************** */
/* Virtual Network :: Misc */
/* ************************************************************************** */
@ -556,7 +464,7 @@ ostream& operator<<(ostream& os, VirtualNetwork& vn)
{
string vnet_xml;
os << vn.to_xml(vnet_xml);
os << vn.to_xml_extended(vnet_xml,true);
return os;
};
@ -565,25 +473,45 @@ ostream& operator<<(ostream& os, VirtualNetwork& vn)
/* -------------------------------------------------------------------------- */
string& VirtualNetwork::to_xml(string& xml) const
{
return to_xml_extended(xml,false);
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
string& VirtualNetwork::to_xml_extended(string& xml, bool extended) const
{
ostringstream os;
string template_xml;
string leases_xml;
// Total leases is the number of used leases.
int total_leases = 0;
if (leases != 0)
{
total_leases = leases->n_used;
}
os <<
"<VNET>" <<
"<ID>" << oid << "</ID>" <<
"<UID>" << uid << "</UID>" <<
"<NAME>" << name << "</NAME>" <<
"<TYPE>" << type << "</TYPE>" <<
"<BRIDGE>" << bridge << "</BRIDGE>" <<
"<PUBLIC>" << public_vnet << "</PUBLIC>" <<
"<ID>" << oid << "</ID>" <<
"<UID>" << uid << "</UID>" <<
"<USERNAME>" << user_name << "</USERNAME>" <<
"<NAME>" << name << "</NAME>" <<
"<TYPE>" << type << "</TYPE>" <<
"<BRIDGE>" << bridge << "</BRIDGE>" <<
"<PUBLIC>" << public_vnet << "</PUBLIC>" <<
"<TOTAL_LEASES>"<< total_leases << "</TOTAL_LEASES>"<<
vn_template->to_xml(template_xml);
if (leases)
if (extended && leases != 0)
{
os << leases->to_xml(leases_xml);
}
os << "</VNET>";
xml = os.str();
@ -594,40 +522,44 @@ string& VirtualNetwork::to_xml(string& xml) const
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
string& VirtualNetwork::to_str(string& str) const
int VirtualNetwork::from_xml(const string &xml_str)
{
ostringstream os;
vector<xmlNodePtr> content;
string template_str;
string leases_str;
int rc = 0;
int int_type;
os << "ID : " << oid << endl;
os << "UID : " << uid << endl;
os << "NAME : " << name << endl;
os << "Type : ";
if ( type==VirtualNetwork::RANGED )
// Initialize the internal XML object
update_from_str(xml_str);
// Get class base attributes
rc += xpath(oid, "/VNET/ID", -1);
rc += xpath(uid, "/VNET/UID", -1);
rc += xpath(user_name, "/VNET/USERNAME", "not_found");
rc += xpath(name, "/VNET/NAME", "not_found");
rc += xpath(int_type, "/VNET/TYPE", -1);
rc += xpath(bridge, "/VNET/BRIDGE", "not_found");
rc += xpath(public_vnet,"/VNET/PUBLIC", 0);
type = static_cast<NetworkType>( int_type );
// Get associated classes
ObjectXML::get_nodes("/VNET/TEMPLATE", content);
if( content.size() < 1 )
{
os << "Ranged" << endl;
}
else
{
os << "Fixed" << endl;
return -1;
}
os << "Bridge : " << bridge << endl;
os << "Public : " << public_vnet << endl << endl;
// Virtual Network template
rc += vn_template->from_xml_node( content[0] );
os << "....: Template :...." << vn_template->to_str(template_str) << endl <<
endl;
if (leases)
if (rc != 0)
{
os << "....: Leases :...." << endl << leases->to_str(leases_str) << endl;
return -1;
}
str = os.str();
return str;
return 0;
}
/* -------------------------------------------------------------------------- */

Some files were not shown because too many files have changed in this diff Show More