diff --git a/include/Host.h b/include/Host.h index ad0ee87265..ab2561edc8 100644 --- a/include/Host.h +++ b/include/Host.h @@ -18,8 +18,8 @@ #define HOST_H_ #include "PoolSQL.h" -#include "HostShare.h" #include "HostTemplate.h" +#include "HostShare.h" #include "ClusterPool.h" using namespace std; @@ -454,11 +454,9 @@ private: { ostringstream oss_host(Host::db_bootstrap); ostringstream oss_share(HostShare::db_bootstrap); - ostringstream oss_templ(HostTemplate::db_bootstrap); db->exec(oss_host); db->exec(oss_share); - db->exec(oss_templ); }; protected: @@ -489,7 +487,8 @@ protected: TM_MAD = 5, LAST_MON_TIME = 6, CLUSTER = 7, - LIMIT = 8 + TEMPLATE = 8, + LIMIT = 9 }; static const char * db_names; diff --git a/include/HostTemplate.h b/include/HostTemplate.h index 0121ce889d..b478c08437 100644 --- a/include/HostTemplate.h +++ b/include/HostTemplate.h @@ -17,30 +17,22 @@ #ifndef HOST_TEMPLATE_H_ #define HOST_TEMPLATE_H_ -#include "TemplateSQL.h" +#include "Template.h" using namespace std; /** * Host Template class, it represents the attributes of a Host */ -class HostTemplate : public TemplateSQL +class HostTemplate : public Template { public: - HostTemplate(int tid = -1, - const char separator = '='): - TemplateSQL(table,tid,true,separator,"TEMPLATE"){}; + HostTemplate() : Template(true,'=',"TEMPLATE"){}; ~HostTemplate(){}; -private: - friend class Host; - - static const char * table; - - static const char * db_bootstrap; }; - + /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ diff --git a/include/Image.h b/include/Image.h index 1fba0dafe5..2109f6f03c 100644 --- a/include/Image.h +++ b/include/Image.h @@ -273,9 +273,30 @@ public: * Removes an Image attribute * @param name of the attribute */ - int remove_template_attribute(SqlDB * db, const string& name) + int remove_template_attribute(const string& name) { - return image_template->remove_attribute(db, name); + return image_template->erase(name); + } + + /** + * Adds a new attribute to the template (replacing it if + * already defined), the image's mutex SHOULD be locked + * @param name of the new attribute + * @param value of the new attribute + * @return 0 on success + */ + int replace_template_attribute( + const string& name, + const string& value) + { + SingleAttribute * sattr; + + image_template->erase(name); + + sattr = new SingleAttribute(name,value); + image_template->set(sattr); + + return 0; } private: @@ -367,10 +388,8 @@ private: static void bootstrap(SqlDB * db) { ostringstream oss_image(Image::db_bootstrap); - ostringstream oss_templ(ImageTemplate::db_bootstrap); db->exec(oss_image); - db->exec(oss_templ); }; @@ -407,7 +426,8 @@ protected: STATE = 7, /* 0) INIT 1) ALLOCATED */ /* 2) READY 3) USED */ RUNNING_VMS = 8, /* Number of VMs using the img */ - LIMIT = 9 + TEMPLATE = 9, /* Image template xml data */ + LIMIT = 10 }; static const char * db_names; diff --git a/include/ImagePool.h b/include/ImagePool.h index d7fd446ec0..1b5ec7de35 100644 --- a/include/ImagePool.h +++ b/include/ImagePool.h @@ -120,34 +120,6 @@ public: return rc; }; - /** Modify an image attribute in the template (Image MUST be locked) - * @param image pointer to Image - * @param name of the attribute to be changed - * @param new value for the attribute - * @return 0 on success, -1 otherwise - */ - int replace_attribute( - Image * image, - const string& name, - const string& value) - { - SingleAttribute * sattr = new SingleAttribute(name,value); - - return image->image_template->replace_attribute(db,sattr); - } - - /** Delete an image attribute in the template (Image MUST be locked) - * @param image pointer to Image - * @param name of the attribute to be removed - * @return 0 on success, -1 otherwise - */ - int remove_attribute( - Image * image, - const string& name) - { - return image->image_template->remove_attribute(db, name); - } - /** * Bootstraps the database table(s) associated to the Image pool */ diff --git a/include/ImageTemplate.h b/include/ImageTemplate.h index 8a999aa9f4..17b8502dbd 100644 --- a/include/ImageTemplate.h +++ b/include/ImageTemplate.h @@ -17,29 +17,19 @@ #ifndef IMAGE_TEMPLATE_H_ #define IMAGE_TEMPLATE_H_ -#include "TemplateSQL.h" +#include "Template.h" using namespace std; /** * Image Template class, it represents the attributes of a Host */ -class ImageTemplate : public TemplateSQL +class ImageTemplate : public Template { public: - ImageTemplate(int tid = -1, - const char separator = '='): - TemplateSQL(table,tid,true,separator,"TEMPLATE"){}; + ImageTemplate() : Template(true,'=',"TEMPLATE"){}; ~ImageTemplate(){}; - -private: - friend class Image; - friend class ImagePool; - - static const char * table; - - static const char * db_bootstrap; }; /* -------------------------------------------------------------------------- */ diff --git a/include/TemplateSQL.h b/include/TemplateSQL.h deleted file mode 100644 index 3c219475ae..0000000000 --- a/include/TemplateSQL.h +++ /dev/null @@ -1,130 +0,0 @@ -/* ------------------------------------------------------------------------ */ -/* 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 TEMPLATE_SQL_H_ -#define TEMPLATE_SQL_H_ - -#include -#include -#include - -#include "Template.h" -#include "SqlDB.h" -#include "ObjectSQL.h" - -using namespace std; - -/** - * SQL Template class, it provides DB support for template objects - */ -class TemplateSQL : public Template, public ObjectSQL -{ -public: - TemplateSQL( - const char * _table, - int template_id = -1, - bool replace = false, - const char separator = '=', - const char * xml_root = "TEMPLATE"): - Template(replace,separator,xml_root),table(_table),id(template_id) - {}; - - virtual ~TemplateSQL(){}; - -protected: - - //Database implementation variables - - const char * table; - - static const char * db_names; - - //Template attributes - - /** - * Template unique identification. - */ - int id; - - /** - * Writes the template in the DB - * @param db pointer to the database. - * @return 0 on success. - */ - int insert(SqlDB * db); - - /** - * Updates the template in the DB - * @param db pointer to the database. - * @return 0 on success. - */ - int update(SqlDB *db); - - /** - * Reads the template (identified by its id) from the DB - * @param db pointer to the database. - * @return 0 on success. - */ - int select(SqlDB *db); - - /** - * Removes the template from the DB - * @param db pointer to the database. - */ - 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); - - /** - * Removes a template attribute from the DB. If there are multiple - * attributes with the same name, only one will be replaced. The - * attribute MUST be allocated in the heap. - * @param db pointer to the database. - * @param attribute pointer to the new attribute. - */ - int replace_attribute(SqlDB * db, Attribute * attribute); - - /** - * Insert a given attribute (MUST be allocated in the heap) in the template - * and updates the DB. - * @param db pointer to the database. - * @param attribute pointer to the new attribute - */ - int insert_attribute(SqlDB * db, Attribute * attribute); - - /** - * Remove a given attribute from the template and the DB. - * @param db pointer to the database. - * @param name name of the attribute - */ - int remove_attribute(SqlDB * db, const string& name); - - /** - * Callback to recover template attributes (TemplateSQL::select) - */ - int select_cb(void *nil, int num, char **values, char **names); -}; - -/* ------------------------------------------------------------------------ */ -/* ------------------------------------------------------------------------ */ - -#endif /*TEMPLATE_SQL_H_*/ diff --git a/include/VirtualMachine.h b/include/VirtualMachine.h index 8330967428..3b22e54194 100644 --- a/include/VirtualMachine.h +++ b/include/VirtualMachine.h @@ -598,6 +598,28 @@ public: vm_template->get(str,value); } + /** + * Adds a new attribute to the template (replacing it if + * already defined), the vm's mutex SHOULD be locked + * @param name of the new attribute + * @param value of the new attribute + * @return 0 on success + */ + int replace_template_attribute( + string& name, + string& value) + { + SingleAttribute * sattr; + + vm_template->erase(name); + + sattr = new SingleAttribute(name,value); + vm_template->set(sattr); + + return 0; + } + + /** * Generates a XML string for the template of the VM * @param xml the string to store the XML description. @@ -869,11 +891,9 @@ private: static void bootstrap(SqlDB * db) { ostringstream oss_vm(VirtualMachine::db_bootstrap); - ostringstream oss_tmpl(VirtualMachineTemplate::db_bootstrap); ostringstream oss_hist(History::db_bootstrap); db->exec(oss_vm); - db->exec(oss_tmpl); db->exec(oss_hist); }; @@ -925,44 +945,6 @@ private: return -1; }; - /** - * Updates the template of a VM, adding a new attribute (replacing it if - * already defined), the vm's mutex SHOULD be locked - * @param db pointer to the database - * @param name of the new attribute - * @param value of the new attribute - * @return 0 on success - */ - int update_template_attribute( - SqlDB * db, - string& name, - string& value) - { - SingleAttribute * sattr; - int rc; - - sattr = new SingleAttribute(name,value); - rc = vm_template->replace_attribute(db,sattr); - - if (rc != 0) - { - delete sattr; - } - - return rc; - } - - /** - * Inserts a new attribute in the template of a VM, also the DB is - * updated. The vm's mutex SHOULD be locked - * @param db pointer to the database - * @param attribute the new attribute for the template - * @return 0 on success - */ - int insert_template_attribute(SqlDB * db, Attribute * attribute) - { - return vm_template->insert_attribute(db,attribute); - } // ------------------------------------------------------------------------- // Attribute Parser @@ -1023,7 +1005,8 @@ protected: NET_TX = 11, NET_RX = 12, LAST_SEQ = 13, - LIMIT = 14 + TEMPLATE = 14, + LIMIT = 15 }; static const char * table; diff --git a/include/VirtualMachinePool.h b/include/VirtualMachinePool.h index fe8ca25bae..378f7460d0 100644 --- a/include/VirtualMachinePool.h +++ b/include/VirtualMachinePool.h @@ -90,22 +90,6 @@ public: // Virtual Machine DB access functions //-------------------------------------------------------------------------- - /** - * Updates the template of a VM, adding a new attribute (replacing it if - * already defined), the vm's mutex SHOULD be locked - * @param vm pointer to the virtual machine object - * @param name of the new attribute - * @param value of the new attribute - * @return 0 on success - */ - int update_template_attribute( - VirtualMachine * vm, - string& name, - string& value) - { - return vm->update_template_attribute(db,name,value); - } - /** * Updates the history record of a VM, the vm's mutex SHOULD be locked * @param vm pointer to the virtual machine object diff --git a/include/VirtualMachineTemplate.h b/include/VirtualMachineTemplate.h index c661663dd2..62053e61b4 100644 --- a/include/VirtualMachineTemplate.h +++ b/include/VirtualMachineTemplate.h @@ -17,29 +17,25 @@ #ifndef VIRTUAL_MACHINE_TEMPLATE_H_ #define VIRTUAL_MACHINE_TEMPLATE_H_ -#include "TemplateSQL.h" +#include "Template.h" using namespace std; /** * Virtual Machine Template class, it represents a VM configuration file. */ -class VirtualMachineTemplate : public TemplateSQL +class VirtualMachineTemplate : public Template { public: - VirtualMachineTemplate(int tid = -1): - TemplateSQL(table,tid,false,'=',"TEMPLATE"){}; + VirtualMachineTemplate(): + Template(false,'=',"TEMPLATE"){}; ~VirtualMachineTemplate(){}; - + private: friend class VirtualMachine; - - static const char * table; - - static const char * db_bootstrap; }; - + /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ diff --git a/include/VirtualNetwork.h b/include/VirtualNetwork.h index cf37a96753..10aaa47017 100644 --- a/include/VirtualNetwork.h +++ b/include/VirtualNetwork.h @@ -313,11 +313,9 @@ private: static void bootstrap(SqlDB * db) { ostringstream oss_vnet(VirtualNetwork::db_bootstrap); - ostringstream oss_templ(VirtualNetworkTemplate::db_bootstrap); ostringstream oss_lease(Leases::db_bootstrap); db->exec(oss_vnet); - db->exec(oss_templ); db->exec(oss_lease); }; @@ -337,34 +335,6 @@ private: int vn_drop(SqlDB * db); - - /** - * Updates the template of a VNW, adding a new attribute (replacing it if - * already defined), the VN's mutex SHOULD be locked - * @param db pointer to the DB - * @param name of the new attribute - * @param value of the new attribute - * @return 0 on success - */ - int update_template_attribute( - SqlDB * db, - string& name, - string& value) - { - SingleAttribute * sattr; - int rc; - - sattr = new SingleAttribute(name,value); - rc = vn_template->replace_attribute(db,sattr); - - if (rc != 0) - { - delete sattr; - } - - return rc; - } - protected: //************************************************************************** @@ -387,7 +357,8 @@ protected: TYPE = 3, BRIDGE = 4, PUBLIC = 5, - LIMIT = 6 + TEMPLATE = 6, + LIMIT = 7 }; static const char * table; @@ -428,9 +399,7 @@ protected: { int rc; - rc = vn_template->drop(db); - - rc += leases->drop(db); + rc = leases->drop(db); rc += vn_drop(db); diff --git a/include/VirtualNetworkPool.h b/include/VirtualNetworkPool.h index f57c53037b..03a63bb51e 100644 --- a/include/VirtualNetworkPool.h +++ b/include/VirtualNetworkPool.h @@ -96,22 +96,6 @@ public: */ void authorize_nic(VectorAttribute * nic, AuthRequest * ar); - /** - * Updates the template of a VN, adding a new attribute (replacing it if - * already defined), the VN's mutex SHOULD be locked - * @param vn pointer to the virtual network object - * @param name of the new attribute - * @param value of the new attribute - * @return 0 on success - */ - int update_template_attribute( - VirtualNetwork * vn, - string& name, - string& value) - { - return vn->update_template_attribute(db,name,value); - }; - /** * Bootstraps the database table(s) associated to the VirtualNetwork pool */ diff --git a/include/VirtualNetworkTemplate.h b/include/VirtualNetworkTemplate.h index 82cb99473d..c611345996 100644 --- a/include/VirtualNetworkTemplate.h +++ b/include/VirtualNetworkTemplate.h @@ -17,29 +17,22 @@ #ifndef VIRTUAL_NETWORK_TEMPLATE_H_ #define VIRTUAL_NETWORK_TEMPLATE_H_ -#include "TemplateSQL.h" +#include "Template.h" using namespace std; /** * Virtual Network Template class, it represents a VN configuration file. */ -class VirtualNetworkTemplate : public TemplateSQL +class VirtualNetworkTemplate : public Template { public: - VirtualNetworkTemplate(int tid = -1): - TemplateSQL(table,tid,false,'=',"TEMPLATE"){}; + VirtualNetworkTemplate(): + Template(false,'=',"TEMPLATE"){}; ~VirtualNetworkTemplate(){}; - -private: - friend class VirtualNetwork; - - static const char * table; - - static const char * db_bootstrap; }; - + /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ diff --git a/src/authm/test/SConstruct b/src/authm/test/SConstruct index 827237c72b..d254d0d1a4 100644 --- a/src/authm/test/SConstruct +++ b/src/authm/test/SConstruct @@ -73,6 +73,9 @@ main_env.Append(CPPFLAGS=[ main_env.Append(LIBPATH=["/usr/lib/mysql"]) main_env.Append(CPPPATH=["/usr/include/mysql"]) +# libxml2 +main_env.ParseConfig('xml2-config --libs --cflags') + sqlite=ARGUMENTS.get('sqlite', 'yes') if sqlite=='yes': main_env.Append(CPPFLAGS=["-DSQLITE_DB"]) diff --git a/src/host/Host.cc b/src/host/Host.cc index 3fc1087bf8..0fe22ece3b 100644 --- a/src/host/Host.cc +++ b/src/host/Host.cc @@ -41,7 +41,7 @@ Host::Host( tm_mad_name(_tm_mad_name), last_monitored(0), cluster(ClusterPool::DEFAULT_CLUSTER_NAME), - host_template(id) + host_template() {}; @@ -54,12 +54,12 @@ 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)"; + "tm_mad,last_mon_time, cluster, template)"; const char * Host::db_bootstrap = "CREATE TABLE IF NOT EXISTS host_pool (" "oid INTEGER PRIMARY KEY,host_name VARCHAR(512), state INTEGER," "im_mad VARCHAR(128),vm_mad VARCHAR(128),tm_mad VARCHAR(128)," - "last_mon_time INTEGER, cluster VARCHAR(128), " + "last_mon_time INTEGER, cluster VARCHAR(128), template TEXT, " "UNIQUE(host_name, im_mad, vm_mad, tm_mad) )"; /* ------------------------------------------------------------------------ */ @@ -75,6 +75,7 @@ int Host::select_cb(void * nil, int num, char **values, char ** names) (!values[TM_MAD]) || (!values[LAST_MON_TIME]) || (!values[CLUSTER]) || + (!values[TEMPLATE]) || (num != LIMIT )) { return -1; @@ -92,7 +93,8 @@ int Host::select_cb(void * nil, int num, char **values, char ** names) cluster = values[CLUSTER]; - host_template.id = oid; + host_template.from_xml(values[TEMPLATE]); + host_share.hsid = oid; return 0; @@ -122,9 +124,6 @@ int Host::select(SqlDB *db) return -1; } - // Get the template - rc = host_template.select(db); - if ( rc != 0 ) { return -1; @@ -149,32 +148,17 @@ int Host::insert(SqlDB *db) int rc; map::iterator iter; - // Set up the template ID, to insert it - if ( host_template.id == -1 ) - { - host_template.id = oid; - } - // Set up the share ID, to insert it if ( host_share.hsid == -1 ) { host_share.hsid = oid; } - // Update the Template - rc = host_template.insert(db); - - if ( rc != 0 ) - { - return rc; - } - // Update the HostShare rc = host_share.insert(db); if ( rc != 0 ) { - host_template.drop(db); return rc; } @@ -183,7 +167,6 @@ int Host::insert(SqlDB *db) if ( rc != 0 ) { - host_template.drop(db); host_share.drop(db); return rc; @@ -199,14 +182,6 @@ int Host::update(SqlDB *db) { int rc; - // Update the Template needed by the monitoring action from IM - rc = host_template.update(db); - - if ( rc != 0 ) - { - return rc; - } - // Update the HostShare rc = host_share.update(db); @@ -235,12 +210,14 @@ int Host::insert_replace(SqlDB *db, bool replace) ostringstream oss; int rc; + string xml_template; char * sql_hostname; char * sql_im_mad_name; char * sql_tm_mad_name; char * sql_vmm_mad_name; char * sql_cluster; + char * sql_template; // Update the Host @@ -279,6 +256,14 @@ 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()); + + if ( sql_template == 0 ) + { + goto error_template; + } + if(replace) { oss << "REPLACE"; @@ -291,14 +276,15 @@ int Host::insert_replace(SqlDB *db, bool replace) // Construct the SQL statement to Insert or Replace oss <<" INTO "<< table <<" "<< db_names <<" VALUES (" - << oid << "," - << "'" << sql_hostname << "'," - << state << "," - << "'" << sql_im_mad_name << "'," - << "'" << sql_vmm_mad_name << "'," - << "'" << sql_tm_mad_name << "'," - << last_monitored << "," - << "'" << sql_cluster << "')"; + << oid << "," + << "'" << sql_hostname << "'," + << state << "," + << "'" << sql_im_mad_name << "'," + << "'" << sql_vmm_mad_name << "'," + << "'" << sql_tm_mad_name << "'," + << last_monitored << "," + << "'" << sql_cluster << "'," + << "'" << sql_template << "')"; rc = db->exec(oss); @@ -307,9 +293,12 @@ int Host::insert_replace(SqlDB *db, bool replace) db->free_str(sql_tm_mad_name); db->free_str(sql_vmm_mad_name); db->free_str(sql_cluster); + db->free_str(sql_template); return rc; +error_template: + db->free_str(sql_cluster); error_cluster: db->free_str(sql_vmm_mad_name); error_vmm: @@ -335,6 +324,7 @@ int Host::dump(ostringstream& oss, int num, char **values, char **names) (!values[TM_MAD]) || (!values[LAST_MON_TIME]) || (!values[CLUSTER]) || + (!values[TEMPLATE]) || (num != LIMIT + HostShare::LIMIT )) { return -1; @@ -349,7 +339,8 @@ int Host::dump(ostringstream& oss, int num, char **values, char **names) "" << values[VM_MAD] <<"" << "" << values[TM_MAD] <<"" << ""<< values[LAST_MON_TIME]<<""<< - "" << values[CLUSTER] <<""; + "" << values[CLUSTER] <<"" << + values[TEMPLATE]; HostShare::dump(oss,num - LIMIT, values + LIMIT, names + LIMIT); @@ -366,8 +357,6 @@ int Host::drop(SqlDB * db) ostringstream oss; int rc; - host_template.drop(db); - host_share.drop(db); oss << "DELETE FROM " << table << " WHERE oid=" << oid; diff --git a/src/host/HostTemplate.cc b/src/host/HostTemplate.cc deleted file mode 100644 index 0f5c6ccfa1..0000000000 --- a/src/host/HostTemplate.cc +++ /dev/null @@ -1,24 +0,0 @@ -/* -------------------------------------------------------------------------- */ -/* 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 "HostTemplate.h" - -const char * HostTemplate::table = "host_attributes"; - -const char * HostTemplate::db_bootstrap = "CREATE TABLE IF NOT EXISTS " - "host_attributes (id INTEGER, name VARCHAR(256), type INTEGER, value TEXT, " - "PRIMARY KEY(id,name))"; - diff --git a/src/host/SConstruct b/src/host/SConstruct index 4e0ef053f8..c87e054d76 100644 --- a/src/host/SConstruct +++ b/src/host/SConstruct @@ -25,7 +25,6 @@ source_files=[ 'Host.cc', 'HostShare.cc', 'HostPool.cc', - 'HostTemplate.cc', 'ClusterPool.cc', ] diff --git a/src/host/test/HostPoolTest.cc b/src/host/test/HostPoolTest.cc index 5d72de6f9b..1378854cc7 100644 --- a/src/host/test/HostPoolTest.cc +++ b/src/host/test/HostPoolTest.cc @@ -54,34 +54,34 @@ const string xmls[] = const string xml_dump = "0a0im_madvmm_madtm_mad0" - "default00default00000000000000" "1a name0im_madvmm_madtm_mad0default100default100000" "000000002a_name0im_madvmm_madtm_mad0defaulttm_mad0default200000000000003another " "name0im_madvmm_mad" - "tm_mad0defaulttm_mad0default300000000000004host0im_madvmm_madtm_mad" - "0default4" + "0default4" "000" "0000" "000vmm_madtm_mad0" - "default00default00000000000000" "1a name0im_madvmm_madtm_mad0default100default100000" "000000002a_name0im_madvmm_madtm_mad0defaulttm_mad0default200000000000003another " "name0im_madvmm_mad" - "tm_mad0defaulttm_mad0default30000000000000"; +const string host0_updated = + "0Host one0im_madvmm_madtm_mad0default00000000000000"; const string cluster_default = "0default"; @@ -149,6 +151,7 @@ class HostPoolTest : public PoolTest CPPUNIT_TEST (cluster_dump); CPPUNIT_TEST (set_cluster); CPPUNIT_TEST (remove_cluster); + CPPUNIT_TEST (update_info); CPPUNIT_TEST_SUITE_END (); @@ -486,8 +489,10 @@ public: // 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"); CPPUNIT_ASSERT( rc == 1 ); @@ -518,8 +523,10 @@ public: // 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"); CPPUNIT_ASSERT( rc == 1 ); @@ -540,6 +547,40 @@ public: host->to_xml(xml_str); check(0, host); } + + /* ********************************************************************* */ + + void update_info() + { + int rc; + int oid_1; + HostPool * hp = static_cast(pool); + Host* host; + string str; + + oid_1 = allocate(0); + + host = hp->get(oid_1, false); + CPPUNIT_ASSERT( host != 0 ); + + string info = "ATT_A=VALUE_A ATT_B=VALUE_B"; + rc = host->update_info(info); + + CPPUNIT_ASSERT(rc == 0); + + pool->update(host); + + host = hp->get(oid_1,false); + CPPUNIT_ASSERT( host != 0 ); + CPPUNIT_ASSERT( host->to_xml(str) == host0_updated ); + + //Now force access to DB + pool->clean(); + host = hp->get(oid_1,false); + + CPPUNIT_ASSERT( host != 0 ); + CPPUNIT_ASSERT( host->to_xml(str) == host0_updated ); + } }; diff --git a/src/host/test/SConstruct b/src/host/test/SConstruct index 26a0c4d5b6..295c230080 100644 --- a/src/host/test/SConstruct +++ b/src/host/test/SConstruct @@ -67,6 +67,10 @@ main_env.Append(LIBPATH=["/usr/lib/mysql"]) main_env.Append(CPPPATH=["/usr/include/mysql"]) +# libxml2 +main_env.ParseConfig('xml2-config --libs --cflags') + + sqlite=ARGUMENTS.get('sqlite', 'yes') if sqlite=='yes': main_env.Append(CPPFLAGS=["-DSQLITE_DB"]) diff --git a/src/image/Image.cc b/src/image/Image.cc index fbe3bd6b5d..c7ce568b1a 100644 --- a/src/image/Image.cc +++ b/src/image/Image.cc @@ -67,27 +67,28 @@ Image::~Image() const char * Image::table = "image_pool"; const char * Image::db_names = "(oid, uid, name, type, public, regtime, " - "source, state, running_vms)"; + "source, state, running_vms, template)"; 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, regtime INTEGER, source TEXT, state INTEGER, " - "running_vms INTEGER, UNIQUE(name) )"; + "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[REGTIME]) || - (!values[SOURCE]) || - (!values[STATE]) || - (!values[RUNNING_VMS]) || + if ((!values[OID]) || + (!values[UID]) || + (!values[NAME]) || + (!values[TYPE]) || + (!values[PUBLIC]) || + (!values[REGTIME]) || + (!values[SOURCE]) || + (!values[STATE]) || + (!values[RUNNING_VMS]) || + (!values[TEMPLATE]) || (num != LIMIT )) { return -1; @@ -108,7 +109,7 @@ int Image::select_cb(void * nil, int num, char **values, char ** names) running_vms = atoi(values[RUNNING_VMS]); - image_template->id = oid; + image_template->from_xml(values[TEMPLATE]); return 0; } @@ -136,15 +137,6 @@ int Image::select(SqlDB *db) return -1; } - // Get the template - - rc = image_template->select(db); - - if ( rc != 0 ) - { - return -1; - } - return 0; } @@ -225,36 +217,16 @@ int Image::insert(SqlDB *db) source = tmp_sourcestream.str(); - // ------------ INSERT THE TEMPLATE -------------------- - - if ( image_template->id == -1 ) - { - image_template->id = oid; - } state = DISABLED; - rc = image_template->insert(db); - - if ( rc != 0 ) - { - return rc; - } - //-------------------------------------------------------------------------- // Insert the Image //-------------------------------------------------------------------------- rc = insert_replace(db, false); - if ( rc != 0 ) - { - image_template->drop(db); - - return rc; - } - - return 0; + return rc; error_name: NebulaLog::log("IMG", Log::ERROR, "NAME not present in image template"); @@ -285,10 +257,13 @@ int Image::insert_replace(SqlDB *db, bool replace) int rc; + string xml_template; + char * sql_name; char * sql_source; + char * sql_template; - // Update the Image + // Update the Image sql_name = db->escape_str(name.c_str()); @@ -304,6 +279,14 @@ int Image::insert_replace(SqlDB *db, bool replace) 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; + } + if(replace) { oss << "REPLACE"; @@ -324,15 +307,19 @@ int Image::insert_replace(SqlDB *db, bool replace) << regtime << "," << "'" << sql_source << "'," << state << "," - << running_vms << ")"; + << running_vms << "," + << "'" << sql_template << "')"; rc = db->exec(oss); db->free_str(sql_name); db->free_str(sql_source); + db->free_str(sql_template); return rc; +error_template: + db->free_str(sql_source); error_source: db->free_str(sql_name); error_name: @@ -344,15 +331,16 @@ error_name: int Image::dump(ostringstream& oss, int num, char **values, char **names) { - if ((!values[OID]) || - (!values[UID]) || - (!values[NAME]) || - (!values[TYPE]) || - (!values[PUBLIC]) || - (!values[REGTIME]) || - (!values[SOURCE]) || - (!values[STATE]) || - (!values[RUNNING_VMS]) || + if ((!values[OID]) || + (!values[UID]) || + (!values[NAME]) || + (!values[TYPE]) || + (!values[PUBLIC]) || + (!values[REGTIME]) || + (!values[SOURCE]) || + (!values[STATE]) || + (!values[RUNNING_VMS]) || + (!values[TEMPLATE]) || (num != LIMIT + 1)) { return -1; @@ -370,6 +358,7 @@ int Image::dump(ostringstream& oss, int num, char **values, char **names) "" << values[SOURCE] << "" << "" << values[STATE] << "" << "" << values[RUNNING_VMS] << "" << + values[TEMPLATE] << ""; return 0; @@ -389,8 +378,6 @@ int Image::drop(SqlDB * db) return -1; } - image_template->drop(db); - oss << "DELETE FROM " << table << " WHERE oid=" << oid; rc = db->exec(oss); diff --git a/src/image/ImageTemplate.cc b/src/image/ImageTemplate.cc deleted file mode 100644 index 2daa1d7bba..0000000000 --- a/src/image/ImageTemplate.cc +++ /dev/null @@ -1,22 +0,0 @@ -/* -------------------------------------------------------------------------- */ -/* 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 "ImageTemplate.h" - -const char * ImageTemplate::table = "image_attributes"; - -const char * ImageTemplate::db_bootstrap = "CREATE TABLE IF NOT EXISTS" - " image_attributes (id INTEGER, name TEXT, type INTEGER, value TEXT)"; diff --git a/src/image/SConstruct b/src/image/SConstruct index 94de312801..f98729c3c0 100644 --- a/src/image/SConstruct +++ b/src/image/SConstruct @@ -22,7 +22,6 @@ lib_name='nebula_image' # Sources to generate the library source_files=[ - 'ImageTemplate.cc', 'Image.cc', 'ImagePool.cc' ] diff --git a/src/image/test/ImagePoolTest.cc b/src/image/test/ImagePoolTest.cc index 04300e573a..628a8066be 100644 --- a/src/image/test/ImagePoolTest.cc +++ b/src/image/test/ImagePoolTest.cc @@ -48,20 +48,20 @@ const string templates[] = const string xmls[] = { - "00Image one000000000000source_prefix/9ab4a4e021ee2883f57e3aeecc9e2aed7c3fa19840", + "00Image one000000000000source_prefix/9ab4a4e021ee2883f57e3aeecc9e2aed7c3fa19830", - "11Second Image010000000000source_prefix/c9d51800847467911c755e5e4c13dfe28c3a79f340", + "11Second Image010000000000source_prefix/c9d51800847467911c755e5e4c13dfe28c3a79f330", - "02The third image000000000000source_prefix/e50b0c738be9d431475bf5859629e5580301a7d640" + "02The third image000000000000source_prefix/e50b0c738be9d431475bf5859629e5580301a7d630" }; // This xml dump result has the STIMEs modified to 0000000000 const string xml_dump = -"00one_user_testImage one000000000000source_prefix/9ab4a4e021ee2883f57e3aeecc9e2aed7c3fa1984011A userSecond Image010000000000source_prefix/c9d51800847467911c755e5e4c13dfe28c3a79f34022B userThe third image000000000000source_prefix/e50b0c738be9d431475bf5859629e5580301a7d640"; +"00one_user_testImage one000000000000source_prefix/9ab4a4e021ee2883f57e3aeecc9e2aed7c3fa1983011A userSecond Image010000000000source_prefix/c9d51800847467911c755e5e4c13dfe28c3a79f33022B userThe third image000000000000source_prefix/e50b0c738be9d431475bf5859629e5580301a7d630"; const string xml_dump_where = -"00one_user_testImage one000000000000source_prefix/9ab4a4e021ee2883f57e3aeecc9e2aed7c3fa1984011A userSecond Image010000000000source_prefix/c9d51800847467911c755e5e4c13dfe28c3a79f340"; +"00one_user_testImage one000000000000source_prefix/9ab4a4e021ee2883f57e3aeecc9e2aed7c3fa1983011A userSecond Image010000000000source_prefix/c9d51800847467911c755e5e4c13dfe28c3a79f330"; const string replacement = "0000000000"; @@ -119,7 +119,6 @@ class ImagePoolTest : public PoolTest CPPUNIT_TEST ( target_generation ); CPPUNIT_TEST ( bus_source_assignment ); CPPUNIT_TEST ( public_attribute ); - CPPUNIT_TEST ( disk_overwrite ); CPPUNIT_TEST ( imagepool_disk_attribute ); CPPUNIT_TEST ( dump ); CPPUNIT_TEST ( dump_where ); @@ -255,9 +254,11 @@ public: CPPUNIT_ASSERT( img != 0 ); // Image object should be cached. Let's change some template attributes - ip->replace_attribute(img, description_name, new_description); - ip->replace_attribute(img, attr_name, new_attr_value); - ip->remove_attribute(img, "ORIGINAL_PATH"); + img->replace_template_attribute(description_name, new_description); + img->replace_template_attribute(attr_name, new_attr_value); + img->remove_template_attribute("ORIGINAL_PATH"); + + ip->update(img); img->unlock(); @@ -466,7 +467,7 @@ public: CPPUNIT_ASSERT( oid == 0 ); img->enable(true); - img->disk_attribute(disk, &index, img_type); + img->disk_attribute(disk, &index, &img_type); value = disk->vector_value("TARGET"); @@ -488,7 +489,7 @@ public: img = imp->get(oid, false); img->enable(true); - img->disk_attribute(disk, &index, img_type); + img->disk_attribute(disk, &index, &img_type); value = disk->vector_value("TARGET"); CPPUNIT_ASSERT(value == "hdc"); @@ -509,7 +510,7 @@ public: img = imp->get(oid, false); img->enable(true); - img->disk_attribute(disk, &index, img_type); + img->disk_attribute(disk, &index, &img_type); value = disk->vector_value("TARGET"); CPPUNIT_ASSERT(value == "hde"); @@ -530,7 +531,7 @@ public: img = imp->get(oid, false); img->enable(true); - img->disk_attribute(disk, &index, img_type); + img->disk_attribute(disk, &index, &img_type); value = disk->vector_value("TARGET"); CPPUNIT_ASSERT(value == "sdf"); @@ -562,7 +563,7 @@ public: disk = new VectorAttribute("DISK"); img->enable(true); - img->disk_attribute(disk, &index, img_type); + img->disk_attribute(disk, &index, &img_type); value = ""; value = disk->vector_value("BUS"); @@ -582,7 +583,7 @@ public: disk->replace("BUS", "SCSI"); img->enable(true); - img->disk_attribute(disk, &index, img_type); + img->disk_attribute(disk, &index, &img_type); value = disk->vector_value("BUS"); CPPUNIT_ASSERT( value == "SCSI" ); @@ -596,137 +597,6 @@ public: delete disk; } -/* -------------------------------------------------------------------------- */ -/* -------------------------------------------------------------------------- */ - - void disk_overwrite() - { - ImagePool * imp = static_cast(pool); - Image * img; - - VectorAttribute * disk; - int oid, rc; - string value; - int index = 0; - Image::ImageType img_type; - - // --------------------------------------------------------------------- - // Allocate an OS type image - oid = allocate(0); - CPPUNIT_ASSERT( oid > -1 ); - img = imp->get(oid, false); - - // Disk with overwrite=yes, save_as empty - disk = new VectorAttribute("DISK"); - - disk->replace("OVERWRITE", "yes"); - - img->enable(true); - rc = img->disk_attribute(disk, &index, img_type); - CPPUNIT_ASSERT( rc == 0 ); - - - value = disk->vector_value("OVERWRITE"); - CPPUNIT_ASSERT( value == "YES" ); - - value = ""; - value = disk->vector_value("SAVE_AS"); - CPPUNIT_ASSERT( value == "" ); - - value = ""; - value = disk->vector_value("CLONE"); - CPPUNIT_ASSERT( value == "NO" ); - - value = ""; - value = disk->vector_value("SAVE"); - CPPUNIT_ASSERT( value == "YES" ); - - value = ""; - value = disk->vector_value("READONLY"); - CPPUNIT_ASSERT( value == "NO" ); - - // clean up - delete disk; - - - // --------------------------------------------------------------------- - // Allocate an OS type image - oid = allocate(1); - CPPUNIT_ASSERT( oid > -1 ); - img = imp->get(oid, false); - - // Disk with overwrite=no, save_as not empty - disk = new VectorAttribute("DISK"); - - disk->replace("OVERWRITE", "NO"); - disk->replace("SAVE_AS", "path_to_save"); - - img->enable(true); - rc = img->disk_attribute(disk, &index, img_type); - CPPUNIT_ASSERT( rc == 0 ); - - value = ""; - value = disk->vector_value("OVERWRITE"); - CPPUNIT_ASSERT( value == "NO" ); - - value = ""; - value = disk->vector_value("SAVE_AS"); - CPPUNIT_ASSERT( value == "path_to_save" ); - - value = ""; - value = disk->vector_value("CLONE"); - CPPUNIT_ASSERT( value == "YES" ); - - value = ""; - value = disk->vector_value("SAVE"); - CPPUNIT_ASSERT( value == "YES" ); - - value = ""; - value = disk->vector_value("READONLY"); - CPPUNIT_ASSERT( value == "NO" ); - - // clean up - delete disk; - - // --------------------------------------------------------------------- - // Allocate an OS type image - oid = allocate(2); - CPPUNIT_ASSERT( oid > -1 ); - img = imp->get(oid, false); - - // Disk with overwrite=no, save_as not present - disk = new VectorAttribute("DISK"); - - disk->replace("OVERWRITE", "NO"); - - img->enable(true); - rc = img->disk_attribute(disk, &index, img_type); - CPPUNIT_ASSERT( rc == 0 ); - - value = ""; - value = disk->vector_value("OVERWRITE"); - CPPUNIT_ASSERT( value == "NO" ); - - value = ""; - value = disk->vector_value("SAVE_AS"); - CPPUNIT_ASSERT( value == "" ); - - value = ""; - value = disk->vector_value("CLONE"); - CPPUNIT_ASSERT( value == "YES" ); - - value = ""; - value = disk->vector_value("SAVE"); - CPPUNIT_ASSERT( value == "NO" ); - - value = ""; - value = disk->vector_value("READONLY"); - CPPUNIT_ASSERT( value == "NO" ); - - // clean up - delete disk; - } - /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ @@ -736,7 +606,7 @@ public: Image * img; VectorAttribute * disk; - int oid_0, oid_1; + int oid_0, oid_1, index; string value; Image::ImageType img_type; @@ -770,7 +640,7 @@ public: disk = new VectorAttribute("DISK"); disk->replace("IMAGE", "Image 0"); - ((ImagePool*)imp)->disk_attribute(disk, 0, img_type); + ((ImagePool*)imp)->disk_attribute(disk, 0, &index, &img_type); value = ""; value = disk->vector_value("TARGET"); @@ -787,7 +657,7 @@ public: disk = new VectorAttribute("DISK"); disk->replace("IMAGE_ID", "1"); - ((ImagePool*)imp)->disk_attribute(disk, 0, img_type); + ((ImagePool*)imp)->disk_attribute(disk, 0, &index, &img_type); value = ""; value = disk->vector_value("TARGET"); @@ -902,8 +772,8 @@ public: string result = oss.str(); result.replace(138, 10, replacement); - result.replace(403, 10, replacement); - result.replace(671, 10, replacement); + result.replace(1102, 10, replacement); + result.replace(1606, 10, replacement); CPPUNIT_ASSERT( result == xml_dump ); } @@ -931,9 +801,8 @@ public: CPPUNIT_ASSERT(rc == 0); string result = oss.str(); - - result.replace(138, 10, replacement); - result.replace(403, 10, replacement); + result.replace(138, 10, replacement); + result.replace(1102, 10, replacement); CPPUNIT_ASSERT( result == xml_dump_where ); } diff --git a/src/image/test/SConstruct b/src/image/test/SConstruct index 34f67f44a3..ac843f7345 100644 --- a/src/image/test/SConstruct +++ b/src/image/test/SConstruct @@ -80,6 +80,9 @@ main_env.Append(LIBS=[ main_env.Append(LIBPATH=["/usr/lib/mysql"]) main_env.Append(CPPPATH=["/usr/include/mysql"]) +# libxml2 +main_env.ParseConfig('xml2-config --libs --cflags') + sqlite=ARGUMENTS.get('sqlite', 'yes') if sqlite=='yes': main_env.Append(CPPFLAGS=["-DSQLITE_DB"]) diff --git a/src/nebula/Nebula.cc b/src/nebula/Nebula.cc index 2bcf9557aa..8b28763267 100644 --- a/src/nebula/Nebula.cc +++ b/src/nebula/Nebula.cc @@ -22,6 +22,7 @@ #include #include +#include #include #include @@ -115,6 +116,11 @@ void Nebula::start() NebulaLog::log("ONE",Log::INFO,os); + // ----------------------------------------------------------- + // Initialize the XML library + // ----------------------------------------------------------- + xmlInitParser(); + // ----------------------------------------------------------- // Pools // ----------------------------------------------------------- @@ -526,6 +532,9 @@ void Nebula::start() pthread_join(rm->get_thread_id(),0); pthread_join(hm->get_thread_id(),0); + //XML Library + xmlCleanupParser(); + NebulaLog::log("ONE", Log::INFO, "All modules finalized, exiting.\n"); } diff --git a/src/rm/RequestManagerImageRemoveAttribute.cc b/src/rm/RequestManagerImageRemoveAttribute.cc index a86d05882b..75bd493dcf 100644 --- a/src/rm/RequestManagerImageRemoveAttribute.cc +++ b/src/rm/RequestManagerImageRemoveAttribute.cc @@ -100,7 +100,12 @@ void RequestManager::ImageRemoveAttribute::execute( goto error_image_get; } - rc = ImageRemoveAttribute::ipool->remove_attribute(image, name); + rc = image->remove_template_attribute(name); + + if(rc == 0) + { + rc = ImageRemoveAttribute::ipool->update(image); + } if ( rc < 0 ) { diff --git a/src/rm/RequestManagerImageUpdate.cc b/src/rm/RequestManagerImageUpdate.cc index acc6eef587..a4ff3843bb 100644 --- a/src/rm/RequestManagerImageUpdate.cc +++ b/src/rm/RequestManagerImageUpdate.cc @@ -101,9 +101,12 @@ void RequestManager::ImageUpdate::execute( goto error_image_get; } - // This will perform the update on the DB as well, - // so no need to do it manually - rc = ImageUpdate::ipool->replace_attribute(image, name, value); + rc = image->replace_template_attribute(name, value); + + if(rc == 0) + { + rc = ImageUpdate::ipool->update(image); + } if ( rc < 0 ) { diff --git a/src/template/SConstruct b/src/template/SConstruct index aa0dfbfa5e..1352b341f3 100644 --- a/src/template/SConstruct +++ b/src/template/SConstruct @@ -37,7 +37,6 @@ if env['parsers']=='yes': # Sources to generate the library source_files=[ 'Template.cc', - 'TemplateSQL.cc', 'template_parser.c', 'template_syntax.cc' ] diff --git a/src/template/Template.cc b/src/template/Template.cc index 6cd0be3723..0da76c8965 100644 --- a/src/template/Template.cc +++ b/src/template/Template.cc @@ -460,6 +460,19 @@ int Template::from_xml(const string &xml_str) return -1; } + //Clear the template if not empty + if (!attributes.empty()) + { + multimap::iterator it; + + for ( it = attributes.begin(); it != attributes.end(); it++) + { + delete it->second; + } + + attributes.clear(); + } + // Get the