From 6329d8b3fac165434850629b91f748bd57d9e7e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Tue, 17 May 2011 12:45:16 +0200 Subject: [PATCH] Feature #407: Use Cluster-ids instead of cluster names for Hosts. Update onedb 1.rb, tests and CLI for this change --- include/ClusterPool.h | 7 +++++- include/Host.h | 16 +++++------- src/cli/onehost | 8 +++++- src/cli/oneimage | 4 +-- src/cli/onetemplate | 4 +-- src/cli/oneuser | 2 +- src/cli/onevm | 4 +-- src/cli/onevnet | 4 +-- src/cluster/ClusterPool.cc | 13 ++++++---- src/cluster/test/ClusterPoolTest.cc | 10 ++++---- src/host/Host.cc | 30 +++++++---------------- src/host/HostPool.cc | 4 +-- src/host/test/HostPoolTest.cc | 24 +++++++++--------- src/oca/ruby/OpenNebula/Host.rb | 6 ++--- src/oca/ruby/OpenNebula/Image.rb | 6 +++++ src/oca/ruby/OpenNebula/Template.rb | 10 ++++++++ src/oca/ruby/OpenNebula/User.rb | 10 ++++++++ src/oca/ruby/OpenNebula/VirtualMachine.rb | 6 +++++ src/oca/ruby/OpenNebula/VirtualNetwork.rb | 10 ++++++++ src/onedb/1.rb | 10 ++++++-- src/rm/RequestManagerClusterAdd.cc | 2 +- 21 files changed, 118 insertions(+), 72 deletions(-) diff --git a/include/ClusterPool.h b/include/ClusterPool.h index 13b5ac7fab..e6a7df2700 100644 --- a/include/ClusterPool.h +++ b/include/ClusterPool.h @@ -42,7 +42,7 @@ public: */ int set_default_cluster(Host * host) { - return host->set_cluster(ClusterPool::DEFAULT_CLUSTER_NAME); + return host->set_cluster(ClusterPool::DEFAULT_CLUSTER_ID); }; /** @@ -50,6 +50,11 @@ public: */ static const string DEFAULT_CLUSTER_NAME; + /** + * Cluster id for the default cluster + */ + static const int DEFAULT_CLUSTER_ID; + /* ---------------------------------------------------------------------- */ /* Methods for DB management */ /* ---------------------------------------------------------------------- */ diff --git a/include/Host.h b/include/Host.h index 26726c35ae..a1c4943cae 100644 --- a/include/Host.h +++ b/include/Host.h @@ -173,11 +173,12 @@ public: /** * Sets the cluster for this host - * @return time_t last monitored time + * @param cluster_id Cluster's oid + * @return 0 on success */ - int set_cluster(const string& cluster_name) + int set_cluster(int cluster_id) { - cluster = cluster_name; + gid = cluster_id; return 0; }; @@ -329,11 +330,6 @@ private: */ time_t last_monitored; - /** - * Name of the cluster this host belongs to. - */ - string cluster; - // ------------------------------------------------------------------------- // Host Attributes // ------------------------------------------------------------------------- @@ -347,11 +343,11 @@ private: // ************************************************************************* Host(int id=-1, + int cluster_id=-1, const string& hostname="", const string& im_mad_name="", const string& vmm_mad_name="", - const string& tm_mad_name="", - const string& cluster=""); + const string& tm_mad_name=""); virtual ~Host(); diff --git a/src/cli/onehost b/src/cli/onehost index 281ce5335a..95aa68f357 100755 --- a/src/cli/onehost +++ b/src/cli/onehost @@ -46,12 +46,18 @@ ShowTableHost={ :left => true, :proc => lambda {|d,e| d.name } }, + :cid => { + :name => "CID", + :desc => "Cluster ID", + :size => 4, + :proc => lambda {|d,e| d.cluster_id } + }, :cluster => { :name => "CLUSTER", :desc => "Clustername", :size => 8, :left => true, - :proc => lambda {|d,e| d.cluster } + :proc => lambda { "TODO" } }, :rvm => { :name => "RVM", diff --git a/src/cli/oneimage b/src/cli/oneimage index 3664b6c47f..3cb31dd8f2 100755 --- a/src/cli/oneimage +++ b/src/cli/oneimage @@ -44,7 +44,7 @@ ShowTableImage={ :desc => "Name of the owner", :size => 8, :proc => lambda {|d,e| - d["USERNAME"] + "TODO" } }, :uid=> { @@ -60,7 +60,7 @@ ShowTableImage={ :desc => "Id of the group", :size => 4, :proc => lambda {|d,e| - d["GID"] + d.gid } }, :name => { diff --git a/src/cli/onetemplate b/src/cli/onetemplate index 92b9243d59..315ab1519f 100755 --- a/src/cli/onetemplate +++ b/src/cli/onetemplate @@ -44,7 +44,7 @@ ShowTableTemplate={ :desc => "Name of the owner", :size => 8, :proc => lambda {|d,e| - d["USERNAME"] + "TODO" } }, :uid=> { @@ -60,7 +60,7 @@ ShowTableTemplate={ :desc => "Id of the group", :size => 4, :proc => lambda {|d,e| - d["GID"] + d.gid } }, :name => { diff --git a/src/cli/oneuser b/src/cli/oneuser index f8b1f3be9c..23c212ac8e 100755 --- a/src/cli/oneuser +++ b/src/cli/oneuser @@ -44,7 +44,7 @@ ShowTableUP={ :desc => "Id of the group", :size => 4, :proc => lambda {|d,e| - d["GID"] + d.gid } }, :user => { diff --git a/src/cli/onevm b/src/cli/onevm index 231f0652dc..db12818e63 100755 --- a/src/cli/onevm +++ b/src/cli/onevm @@ -52,7 +52,7 @@ ShowTableVM={ :desc => "Id of the group", :size => 4, :proc => lambda {|d,e| - d["GID"] + d.gid } }, :name => { @@ -68,7 +68,7 @@ ShowTableVM={ :desc => "Name of the owner", :size => 8, :proc => lambda {|d,e| - d["USERNAME"] + "TODO" } }, :stat => { diff --git a/src/cli/onevnet b/src/cli/onevnet index ee5c2db9ea..6a2ef598a2 100755 --- a/src/cli/onevnet +++ b/src/cli/onevnet @@ -51,7 +51,7 @@ ShowTableVN={ :desc => "Username of the virtual network owner", :size => 8, :left => true, - :proc => lambda {|d,e| d["USERNAME"] } + :proc => lambda {|d,e| "TODO" } }, :uid=> { :name => "UID", @@ -66,7 +66,7 @@ ShowTableVN={ :desc => "Id of the group", :size => 4, :proc => lambda {|d,e| - d["GID"] + d.gid } }, :type => { diff --git a/src/cluster/ClusterPool.cc b/src/cluster/ClusterPool.cc index ef1280e8ef..ae5ff899c1 100644 --- a/src/cluster/ClusterPool.cc +++ b/src/cluster/ClusterPool.cc @@ -21,6 +21,7 @@ #include const string ClusterPool::DEFAULT_CLUSTER_NAME = "default"; +const int ClusterPool::DEFAULT_CLUSTER_ID = 0; /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ @@ -35,7 +36,7 @@ ClusterPool::ClusterPool(SqlDB * db):PoolSQL(db, Cluster::table) string error_str; // Build a new Cluster object - cluster = new Cluster(0, ClusterPool::DEFAULT_CLUSTER_NAME); + cluster = new Cluster(DEFAULT_CLUSTER_ID, DEFAULT_CLUSTER_NAME); // Insert the Object in the pool rc = PoolSQL::allocate(cluster, error_str); @@ -110,11 +111,13 @@ int ClusterPool::drop(Cluster * cluster) Nebula& nd = Nebula::instance(); HostPool * hpool = nd.get_hpool(); - string cluster_name = cluster->get_name(); - string where = "cluster = '" + cluster_name + "'"; + int cluster_id = cluster->get_oid(); + + ostringstream where; + where << "cid = " << cluster_id; // Return error if cluster is 'default' - if( cluster->get_oid() == 0 ) + if( cluster->get_oid() == DEFAULT_CLUSTER_ID ) { NebulaLog::log("CLUSTER",Log::WARNING, "Default cluster cannot be deleted."); @@ -127,7 +130,7 @@ int ClusterPool::drop(Cluster * cluster) // Move the hosts assigned to the deleted cluster to the default one if( rc == 0 ) { - hpool->search(hids, where); + hpool->search(hids, where.str()); for ( hid_it=hids.begin() ; hid_it < hids.end(); hid_it++ ) { diff --git a/src/cluster/test/ClusterPoolTest.cc b/src/cluster/test/ClusterPoolTest.cc index bdb102dd05..22b1724feb 100644 --- a/src/cluster/test/ClusterPoolTest.cc +++ b/src/cluster/test/ClusterPoolTest.cc @@ -38,12 +38,12 @@ const string cluster_xml_dump = "0default1cluster_a3cluster_c4cluster_d"; const string host_0_cluster = - "0Host one0im_madvmm_madtm_mad0cluster_a0000000000000"; + "0Host one0im_madvmm_madtm_mad010000000000000"; const string host_0_default = "0Host one0" "im_madvmm_madtm_mad" - "0default" + "00" "000" "000" "000" @@ -271,7 +271,7 @@ public: host = hpool->get(0, false); CPPUNIT_ASSERT(host != 0); - rc = host->set_cluster("cluster_a"); + rc = host->set_cluster(1); CPPUNIT_ASSERT( rc == 0 ); hpool->update(host); @@ -295,7 +295,7 @@ public: CPPUNIT_ASSERT(host != 0); // Set cluster - rc = host->set_cluster("cluster_a"); + rc = host->set_cluster(1); CPPUNIT_ASSERT( rc == 0 ); hpool->update(host); @@ -339,7 +339,7 @@ public: cluster = cpool->get(1, false); // Set cluster - rc = host->set_cluster(cluster->get_name()); + rc = host->set_cluster(cluster->get_oid()); CPPUNIT_ASSERT( rc == 0 ); hpool->update(host); diff --git a/src/host/Host.cc b/src/host/Host.cc index 1d82ddca70..c9abd04839 100644 --- a/src/host/Host.cc +++ b/src/host/Host.cc @@ -29,18 +29,17 @@ Host::Host( int id, + int cluster_id, 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,-1,table), + const string& _tm_mad_name): + PoolObjectSQL(id,_hostname,-1,cluster_id,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(_cluster) + last_monitored(0) { obj_template = new HostTemplate; } @@ -59,11 +58,11 @@ Host::~Host() const char * Host::table = "host_pool"; -const char * Host::db_names = "oid, name, body, state, last_mon_time, cluster"; +const char * Host::db_names = "oid, name, body, state, last_mon_time, cid"; const char * Host::db_bootstrap = "CREATE TABLE IF NOT EXISTS host_pool (" "oid INTEGER PRIMARY KEY, name VARCHAR(256), body TEXT, state INTEGER, " - "last_mon_time INTEGER, cluster VARCHAR(128), UNIQUE(name))"; + "last_mon_time INTEGER, cid INTEGER, UNIQUE(name))"; /* ------------------------------------------------------------------------ */ /* ------------------------------------------------------------------------ */ @@ -105,7 +104,6 @@ int Host::insert_replace(SqlDB *db, bool replace) string xml_body; char * sql_hostname; - char * sql_cluster; char * sql_xml; // Update the Host @@ -117,13 +115,6 @@ int Host::insert_replace(SqlDB *db, bool replace) goto error_hostname; } - sql_cluster = db->escape_str(cluster.c_str()); - - if ( sql_cluster == 0 ) - { - goto error_cluster; - } - sql_xml = db->escape_str(to_xml(xml_body).c_str()); if ( sql_xml == 0 ) @@ -148,19 +139,16 @@ int Host::insert_replace(SqlDB *db, bool replace) << "'" << sql_xml << "'," << state << "," << last_monitored << "," - << "'" << sql_cluster << "')"; + << gid << ")"; rc = db->exec(oss); db->free_str(sql_hostname); - db->free_str(sql_cluster); db->free_str(sql_xml); return rc; error_body: - db->free_str(sql_cluster); -error_cluster: db->free_str(sql_hostname); error_hostname: return -1; @@ -227,7 +215,7 @@ string& Host::to_xml(string& xml) const "" << vmm_mad_name << "" << "" << tm_mad_name << "" << "" << last_monitored << "" << - "" << cluster << "" << + "" << gid << "" << host_share.to_xml(share_xml) << obj_template->to_xml(template_xml) << ""; @@ -260,7 +248,7 @@ int Host::from_xml(const string& xml) rc += xpath(tm_mad_name, "/HOST/TM_MAD", "not_found"); rc += xpath(last_monitored, "/HOST/LAST_MON_TIME", 0); - rc += xpath(cluster, "/HOST/CLUSTER", "not_found"); + rc += xpath(gid, "/HOST/CID", 0); state = static_cast( int_state ); diff --git a/src/host/HostPool.cc b/src/host/HostPool.cc index 785facd3e3..8ec6376e07 100644 --- a/src/host/HostPool.cc +++ b/src/host/HostPool.cc @@ -175,11 +175,11 @@ int HostPool::allocate ( // Build a new Host object host = new Host(-1, + ClusterPool::DEFAULT_CLUSTER_ID, hostname, im_mad_name, vmm_mad_name, - tm_mad_name, - ClusterPool::DEFAULT_CLUSTER_NAME); + tm_mad_name); // Insert the Object in the pool diff --git a/src/host/test/HostPoolTest.cc b/src/host/test/HostPoolTest.cc index e2ce49be0f..47c13018be 100644 --- a/src/host/test/HostPoolTest.cc +++ b/src/host/test/HostPoolTest.cc @@ -33,7 +33,7 @@ const string xmls[] = { "0Host one0" "im_madvmm_madtm_mad" - "0default" + "00" "000" "000" "000" @@ -42,7 +42,7 @@ const string xmls[] = "1Second host0" "im_madvmm_madtm_mad" - "0default" + "00" "000" "000" "000" @@ -54,34 +54,34 @@ const string xmls[] = const string xml_dump = "0a0im_madvmm_madtm_mad0" - "default000000000000000" "1a name0im_madvmm_madtm_mad0default00000000" "000000002a_name0im_madvmm_madtm_mad0defaulttm_mad0000000000000003another " "name0im_madvmm_mad" - "tm_mad0defaulttm_mad0000000000000004host0im_madvmm_madtm_mad" - "0default" + "00" "000" "0000" "000vmm_madtm_mad0" - "default000000000000000" "1a name0im_madvmm_madtm_mad0default00000000" "000000002a_name0im_madvmm_madtm_mad0defaulttm_mad0000000000000003another " "name0im_madvmm_mad" - "tm_mad0defaulttm_mad000000000000Host one0im_madvmm_madtm_mad0default0000000000000"; + "0Host one0im_madvmm_madtm_mad000000000000000"; const string cluster_default = "0default"; diff --git a/src/oca/ruby/OpenNebula/Host.rb b/src/oca/ruby/OpenNebula/Host.rb index 40b8f36c1f..a6536187cf 100644 --- a/src/oca/ruby/OpenNebula/Host.rb +++ b/src/oca/ruby/OpenNebula/Host.rb @@ -121,9 +121,9 @@ module OpenNebula SHORT_HOST_STATES[state_str] end - # Returns the cluster of the Host - def cluster - self['CLUSTER'] + # Returns the cluster ID of the Host + def cluster_id + self['CID'] end diff --git a/src/oca/ruby/OpenNebula/Image.rb b/src/oca/ruby/OpenNebula/Image.rb index 224f7b2d27..491b1b92bd 100644 --- a/src/oca/ruby/OpenNebula/Image.rb +++ b/src/oca/ruby/OpenNebula/Image.rb @@ -178,6 +178,12 @@ module OpenNebula SHORT_IMAGE_TYPES[type_str] end + # Returns the group identifier + # [return] _Integer_ the element's group ID + def gid + self['GID'].to_i + end + private def set_enabled(enabled) diff --git a/src/oca/ruby/OpenNebula/Template.rb b/src/oca/ruby/OpenNebula/Template.rb index 5c8b758da6..84f7d6ff4f 100644 --- a/src/oca/ruby/OpenNebula/Template.rb +++ b/src/oca/ruby/OpenNebula/Template.rb @@ -103,6 +103,16 @@ module OpenNebula set_publish(false) end + # --------------------------------------------------------------------- + # Helpers to get Template information + # --------------------------------------------------------------------- + + # Returns the group identifier + # [return] _Integer_ the element's group ID + def gid + self['GID'].to_i + end + private def set_publish(published) diff --git a/src/oca/ruby/OpenNebula/User.rb b/src/oca/ruby/OpenNebula/User.rb index 071447c6e5..1a1700f4a6 100644 --- a/src/oca/ruby/OpenNebula/User.rb +++ b/src/oca/ruby/OpenNebula/User.rb @@ -89,5 +89,15 @@ module OpenNebula return rc end + # --------------------------------------------------------------------- + # Helpers to get User information + # --------------------------------------------------------------------- + + # Returns the group identifier + # [return] _Integer_ the element's group ID + def gid + self['GID'].to_i + end + end end diff --git a/src/oca/ruby/OpenNebula/VirtualMachine.rb b/src/oca/ruby/OpenNebula/VirtualMachine.rb index 29effeefc2..cc9d71f375 100644 --- a/src/oca/ruby/OpenNebula/VirtualMachine.rb +++ b/src/oca/ruby/OpenNebula/VirtualMachine.rb @@ -259,6 +259,12 @@ module OpenNebula short_state_str end + # Returns the group identifier + # [return] _Integer_ the element's group ID + def gid + self['GID'].to_i + end + private def action(name) return Error.new('ID not defined') if !@pe_id diff --git a/src/oca/ruby/OpenNebula/VirtualNetwork.rb b/src/oca/ruby/OpenNebula/VirtualNetwork.rb index b12a5d8d69..773aaf45d6 100644 --- a/src/oca/ruby/OpenNebula/VirtualNetwork.rb +++ b/src/oca/ruby/OpenNebula/VirtualNetwork.rb @@ -111,6 +111,16 @@ module OpenNebula return rc end + # --------------------------------------------------------------------- + # Helpers to get VirtualNetwork information + # --------------------------------------------------------------------- + + # Returns the group identifier + # [return] _Integer_ the element's group ID + def gid + self['GID'].to_i + end + private def set_publish(published) return Error.new('ID not defined') if !@pe_id diff --git a/src/onedb/1.rb b/src/onedb/1.rb index 96bd167e2c..17a8ef9961 100644 --- a/src/onedb/1.rb +++ b/src/onedb/1.rb @@ -88,7 +88,7 @@ class Migrator < MigratorBase @db.run "ALTER TABLE host_pool RENAME TO old_host_pool;" # Create new table - @db.run "CREATE TABLE host_pool (oid INTEGER PRIMARY KEY, name VARCHAR(256), body TEXT, state INTEGER, last_mon_time INTEGER, cluster VARCHAR(128), UNIQUE(name));" + @db.run "CREATE TABLE host_pool (oid INTEGER PRIMARY KEY, name VARCHAR(256), body TEXT, state INTEGER, last_mon_time INTEGER, cid INTEGER, UNIQUE(name));" # Read each entry in the old table, and insert into new table @db.fetch("SELECT * FROM old_host_pool") do |row| @@ -98,13 +98,19 @@ class Migrator < MigratorBase last_mon_time = row[:last_mon_time] cluster = row[:cluster] + # OpenNebula 2.X stored the cluster name, we need the cluster ID + cluster_id = 0 + @db.fetch("SELECT oid FROM cluster_pool WHERE name='#{cluster}'") do |cluster_row| + cluster_id = cluster_row[:oid] + end + # There is one host share for each host host_share = "" @db.fetch("SELECT * FROM host_shares WHERE hid=#{oid}") do |share| host_share = "#{share[:disk_usage]}#{share[:mem_usage]}#{share[:cpu_usage]}#{share[:max_disk]}#{share[:max_mem]}#{share[:max_cpu]}#{share[:free_disk]}#{share[:free_mem]}#{share[:free_cpu]}#{share[:used_disk]}#{share[:used_mem]}#{share[:used_cpu]}#{share[:running_vms]}" end - body = "#{oid}#{name}#{state}#{row[:im_mad]}#{row[:vm_mad]}#{row[:tm_mad]}#{last_mon_time}#{cluster}#{host_share}#{row[:template]}" + body = "#{oid}#{name}#{state}#{row[:im_mad]}#{row[:vm_mad]}#{row[:tm_mad]}#{last_mon_time}#{cluster_id}#{host_share}#{row[:template]}" @db.run "INSERT INTO host_pool VALUES(#{oid},'#{name}','#{body}', #{state}, #{last_mon_time}, '#{cluster}');" end diff --git a/src/rm/RequestManagerClusterAdd.cc b/src/rm/RequestManagerClusterAdd.cc index 0ffe63f389..2d0c180a3e 100644 --- a/src/rm/RequestManagerClusterAdd.cc +++ b/src/rm/RequestManagerClusterAdd.cc @@ -89,7 +89,7 @@ void RequestManager::ClusterAdd::execute( } // Set cluster - rc = host->set_cluster(cluster->get_name()); + rc = host->set_cluster(cluster->get_oid()); if ( rc != 0 ) {