From 724e9fe2b067ad9eb622d6f26fac70411b06be11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Wed, 16 Mar 2016 11:34:45 +0100 Subject: [PATCH] Feature #4369: Create a default cluster on bootstrap --- include/ClusterPool.h | 10 ++ include/RequestManagerAllocate.h | 27 +++- src/cluster/ClusterPool.cc | 41 +++++- src/datastore/DatastorePool.cc | 10 +- src/oca/ruby/opennebula/datastore.rb | 2 +- src/oca/ruby/opennebula/host.rb | 2 +- src/oca/ruby/opennebula/virtual_network.rb | 2 +- src/rm/RequestManagerCluster.cc | 150 ++++++++++----------- 8 files changed, 151 insertions(+), 93 deletions(-) diff --git a/include/ClusterPool.h b/include/ClusterPool.h index 0c15913dda..4b3d16e604 100644 --- a/include/ClusterPool.h +++ b/include/ClusterPool.h @@ -44,6 +44,16 @@ public: */ static const int NONE_CLUSTER_ID; + /** + * Name for the default cluster + */ + static const string DEFAULT_CLUSTER_NAME; + + /** + * Identifier for the default cluster + */ + static const int DEFAULT_CLUSTER_ID; + /* ---------------------------------------------------------------------- */ /* Methods for DB management */ /* ---------------------------------------------------------------------- */ diff --git a/include/RequestManagerAllocate.h b/include/RequestManagerAllocate.h index 8332b57d3d..faf34bc07a 100644 --- a/include/RequestManagerAllocate.h +++ b/include/RequestManagerAllocate.h @@ -177,7 +177,14 @@ public: int get_cluster_id(xmlrpc_c::paramList const& paramList) { - return xmlrpc_c::value_int(paramList.getInt(2)); + int cid = xmlrpc_c::value_int(paramList.getInt(2)); + + if (cid == -1) + { + cid = ClusterPool::DEFAULT_CLUSTER_ID; + } + + return cid; }; int add_to_cluster( @@ -280,7 +287,14 @@ public: int get_cluster_id(xmlrpc_c::paramList const& paramList) { - return xmlrpc_c::value_int(paramList.getInt(5)); + int cid = xmlrpc_c::value_int(paramList.getInt(5)); + + if (cid == -1) + { + cid = ClusterPool::DEFAULT_CLUSTER_ID; + } + + return cid; }; int add_to_cluster( @@ -384,7 +398,14 @@ public: int get_cluster_id(xmlrpc_c::paramList const& paramList) { - return xmlrpc_c::value_int(paramList.getInt(2)); + int cid = xmlrpc_c::value_int(paramList.getInt(2)); + + if (cid == -1) + { + cid = ClusterPool::DEFAULT_CLUSTER_ID; + } + + return cid; }; int add_to_cluster( diff --git a/src/cluster/ClusterPool.cc b/src/cluster/ClusterPool.cc index bef9345fa2..d9ddeb179c 100644 --- a/src/cluster/ClusterPool.cc +++ b/src/cluster/ClusterPool.cc @@ -21,7 +21,7 @@ #include /* -------------------------------------------------------------------------- */ -/* There is a default cluster boostrapped by the core: */ +/* There is a default cluster boostrapped by the core: 0, default */ /* The first 100 cluster IDs are reserved for system clusters. */ /* Regular ones start from ID 100 */ /* -------------------------------------------------------------------------- */ @@ -29,6 +29,9 @@ const string ClusterPool::NONE_CLUSTER_NAME = ""; const int ClusterPool::NONE_CLUSTER_ID = -1; +const string ClusterPool::DEFAULT_CLUSTER_NAME = "default"; +const int ClusterPool::DEFAULT_CLUSTER_ID = 0; + /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ @@ -37,12 +40,48 @@ ClusterPool::ClusterPool(SqlDB * db):PoolSQL(db, Cluster::table, true, true) ostringstream oss; string error_str; + // --------------------------------------------------------------------- + // Create the default cluster + // --------------------------------------------------------------------- + if (get_lastOID() == -1) //lastOID is set in PoolSQL::init_cb { + int rc; + + allocate(DEFAULT_CLUSTER_NAME, &rc, error_str); + + if( rc != DEFAULT_CLUSTER_ID ) + { + goto error_bootstrap; + } + + Cluster* cluster = get(DEFAULT_CLUSTER_ID, true); + + if (cluster == 0) + { + goto error_bootstrap; + } + + cluster->add_datastore(DatastorePool::SYSTEM_DS_ID, error_str); + cluster->add_datastore(DatastorePool::DEFAULT_DS_ID, error_str); + cluster->add_datastore(DatastorePool::FILE_DS_ID, error_str); + + update(cluster); + + cluster->unlock(); + + // User created clusters will start from ID 100 set_update_lastOID(99); } return; + +error_bootstrap: + oss.str(""); + oss << "Error trying to create default cluster: " << error_str; + NebulaLog::log("CLUSTER",Log::ERROR,oss); + + throw runtime_error(oss.str()); } /* -------------------------------------------------------------------------- */ diff --git a/src/datastore/DatastorePool.cc b/src/datastore/DatastorePool.cc index 37b1b5158b..204b175d57 100644 --- a/src/datastore/DatastorePool.cc +++ b/src/datastore/DatastorePool.cc @@ -58,7 +58,9 @@ DatastorePool::DatastorePool( { DatastoreTemplate * ds_tmpl; int rc; - set empty; + set cluster_ids; + + cluster_ids.insert(ClusterPool::DEFAULT_CLUSTER_ID); // --------------------------------------------------------------------- // Create the system datastore @@ -83,7 +85,7 @@ DatastorePool::DatastorePool( 0137, ds_tmpl, &rc, - empty, + cluster_ids, error_str); if( rc < 0 ) @@ -116,7 +118,7 @@ DatastorePool::DatastorePool( 0137, ds_tmpl, &rc, - empty, + cluster_ids, error_str); if( rc < 0 ) @@ -149,7 +151,7 @@ DatastorePool::DatastorePool( 0137, ds_tmpl, &rc, - empty, + cluster_ids, error_str); if( rc < 0 ) diff --git a/src/oca/ruby/opennebula/datastore.rb b/src/oca/ruby/opennebula/datastore.rb index 26326fe508..8417b5a1f0 100644 --- a/src/oca/ruby/opennebula/datastore.rb +++ b/src/oca/ruby/opennebula/datastore.rb @@ -85,7 +85,7 @@ module OpenNebula # Allocates a new Datastore in OpenNebula # # @param description [String] The template of the Datastore. - # @param cluster_id [Integer] Id of the cluster + # @param cluster_id [Integer] Id of the cluster, -1 to use default # # @return [Integer, OpenNebula::Error] the new ID in case of # success, error otherwise diff --git a/src/oca/ruby/opennebula/host.rb b/src/oca/ruby/opennebula/host.rb index ba7459dc91..23f61e4a9d 100644 --- a/src/oca/ruby/opennebula/host.rb +++ b/src/oca/ruby/opennebula/host.rb @@ -90,7 +90,7 @@ module OpenNebula # @param im [String] Name of the im_driver (information/monitoring) # @param vmm [String] Name of the vmm_driver (hypervisor) # @param vnm [String] Name of the vnm_driver (networking) - # @param cluster_id [String] Id of the cluster + # @param cluster_id [String] Id of the cluster, -1 to use default # # @return [Integer, OpenNebula::Error] the new ID in case of # success, error otherwise diff --git a/src/oca/ruby/opennebula/virtual_network.rb b/src/oca/ruby/opennebula/virtual_network.rb index 463e372eba..436e4d5e6c 100644 --- a/src/oca/ruby/opennebula/virtual_network.rb +++ b/src/oca/ruby/opennebula/virtual_network.rb @@ -76,7 +76,7 @@ module OpenNebula # Allocates a new VirtualNetwork in OpenNebula # # @param description [String] The template of the VirtualNetwork. - # @param cluster_id [Integer] Id of the cluster + # @param cluster_id [Integer] Id of the cluster, -1 to use default # # @return [Integer, OpenNebula::Error] the new ID in case of # success, error otherwise diff --git a/src/rm/RequestManagerCluster.cc b/src/rm/RequestManagerCluster.cc index c43b986ed1..ec86042ec6 100644 --- a/src/rm/RequestManagerCluster.cc +++ b/src/rm/RequestManagerCluster.cc @@ -42,19 +42,12 @@ void RequestManagerCluster::action_generic( PoolObjectAuth c_perms; PoolObjectAuth obj_perms; - if ( cluster_id != ClusterPool::NONE_CLUSTER_ID ) - { - rc = get_info(clpool, cluster_id, PoolObjectSQL::CLUSTER, att, c_perms, - cluster_name, true); + rc = get_info(clpool, cluster_id, PoolObjectSQL::CLUSTER, att, c_perms, + cluster_name, true); - if ( rc == -1 ) - { - return; - } - } - else + if ( rc == -1 ) { - cluster_name = ClusterPool::NONE_CLUSTER_NAME; + return; } rc = get_info(pool, object_id, type, att, obj_perms, obj_name, true); @@ -68,11 +61,7 @@ void RequestManagerCluster::action_generic( { AuthRequest ar(att.uid, att.group_ids); - if ( cluster_id != ClusterPool::NONE_CLUSTER_ID ) - { - ar.add_auth(auth_op, c_perms); // ADMIN CLUSTER - } - + ar.add_auth(auth_op, c_perms); // ADMIN CLUSTER ar.add_auth(AuthRequest::ADMIN, obj_perms); // ADMIN OBJECT if (UserPool::authorize(ar) == -1) @@ -116,87 +105,84 @@ void RequestManagerCluster::action_generic( object->unlock(); // ------------- Add/del object to new cluster --------------------- - if ( cluster_id != ClusterPool::NONE_CLUSTER_ID ) + cluster = clpool->get(cluster_id, true); + + if ( cluster == 0 ) { - cluster = clpool->get(cluster_id, true); + att.resp_obj = PoolObjectSQL::CLUSTER; + att.resp_id = cluster_id; + failure_response(NO_EXISTS, att); - if ( cluster == 0 ) + // Rollback + get(object_id, true, &object, &cluster_obj); + + if ( object != 0 ) { - att.resp_obj = PoolObjectSQL::CLUSTER; - att.resp_id = cluster_id; - failure_response(NO_EXISTS, att); - - // Rollback - get(object_id, true, &object, &cluster_obj); - - if ( object != 0 ) + if (add) { - if (add) - { - cluster_obj->del_cluster(cluster_id); - } - else - { - cluster_obj->add_cluster(cluster_id); - } - - pool->update(object); - - object->unlock(); + cluster_obj->del_cluster(cluster_id); + } + else + { + cluster_obj->add_cluster(cluster_id); } - return; + pool->update(object); + + object->unlock(); } - if (add) - { - rc = add_object(cluster, object_id, att.resp_msg); - } - else - { - rc = del_object(cluster, object_id, att.resp_msg); - } - - if ( rc < 0 ) - { - cluster->unlock(); - - failure_response(INTERNAL, att); - - // Rollback - get(object_id, true, &object, &cluster_obj); - - if ( object != 0 ) - { - if (add) - { - cluster_obj->del_cluster(cluster_id); - } - else - { - cluster_obj->add_cluster(cluster_id); - } - - pool->update(object); - - object->unlock(); - } - - return; - } - - clpool->update(cluster); - - cluster->unlock(); + return; } + if (add) + { + rc = add_object(cluster, object_id, att.resp_msg); + } + else + { + rc = del_object(cluster, object_id, att.resp_msg); + } + + if ( rc < 0 ) + { + cluster->unlock(); + + failure_response(ACTION, att); + + // Rollback + get(object_id, true, &object, &cluster_obj); + + if ( object != 0 ) + { + if (add) + { + cluster_obj->del_cluster(cluster_id); + } + else + { + cluster_obj->add_cluster(cluster_id); + } + + pool->update(object); + + object->unlock(); + } + + return; + } + + clpool->update(cluster); + + cluster->unlock(); + success_response(cluster_id, att); return; } -/* ------------------------------------------------------------------------- */ -/* ------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- */ void RequestManagerClusterHost::add_generic( int cluster_id,