1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-02-22 17:57:46 +03:00

Feature #1112: Associate Datastores to Clusters

This commit is contained in:
Carlos Martín 2012-02-28 12:17:33 +01:00
parent f9e18cb569
commit dd51ce11bf
11 changed files with 183 additions and 28 deletions

View File

@ -20,11 +20,12 @@
#include "PoolSQL.h"
#include "ObjectCollection.h"
#include "DatastoreTemplate.h"
#include "Clusterable.h"
/**
* The Datastore class.
*/
class Datastore : public PoolObjectSQL, ObjectCollection
class Datastore : public PoolObjectSQL, ObjectCollection, public Clusterable
{
public:
@ -121,7 +122,11 @@ private:
// Constructor
// *************************************************************************
Datastore(int id, DatastoreTemplate* ds_template);
Datastore(
int id,
DatastoreTemplate* ds_template,
int cluster_id,
const string& cluster_name);
virtual ~Datastore(){};

View File

@ -63,12 +63,16 @@ public:
* allocated for the object.
* @param ds_template Datastore definition template
* @param oid the id assigned to the Datastore
* @param cluster_id the id of the cluster this Datastore will belong to
* @param cluster_name the name of the cluster this Datastore will belong to
* @param error_str Returns the error reason, if any
*
* @return the oid assigned to the object, -1 in case of failure
*/
int allocate(DatastoreTemplate * ds_template,
int * oid,
int cluster_id,
const string& cluster_name,
string& error_str);
/**
@ -159,7 +163,7 @@ private:
*/
PoolObjectSQL * create()
{
return new Datastore(-1, 0);
return new Datastore(-1, 0, -1, "");
};
};

View File

@ -37,6 +37,7 @@ protected:
Nebula& nd = Nebula::instance();
clpool = nd.get_clpool();
hpool = nd.get_hpool();
dspool = nd.get_dspool();
auth_object = PoolObjectSQL::CLUSTER;
auth_op = AuthRequest::MANAGE;
@ -46,8 +47,9 @@ protected:
/* --------------------------------------------------------------------- */
ClusterPool * clpool;
HostPool * hpool;
ClusterPool * clpool;
HostPool * hpool;
DatastorePool * dspool;
/* --------------------------------------------------------------------- */
@ -112,6 +114,44 @@ public:
};
};
/* ------------------------------------------------------------------------- */
/* ------------------------------------------------------------------------- */
class ClusterAddDatastore : public RequestManagerCluster
{
public:
ClusterAddDatastore():
RequestManagerCluster("ClusterAddDatastore",
"Adds a datastore to the cluster",
"A:sii"){};
~ClusterAddDatastore(){};
void request_execute(xmlrpc_c::paramList const& _paramList,
RequestAttributes& att)
{
return add_generic(_paramList, att, dspool, PoolObjectSQL::DATASTORE);
}
virtual int add_object(Cluster* cluster, int id, string& error_msg)
{
return cluster->add_datastore(id, error_msg);
};
virtual int del_object(Cluster* cluster, int id, string& error_msg)
{
return cluster->del_datastore(id, error_msg);
};
virtual void get(int oid, bool lock, PoolObjectSQL ** object, Clusterable ** cluster_obj)
{
Datastore * ds = dspool->get(oid, lock);
*object = static_cast<PoolObjectSQL *>(ds);
*cluster_obj = static_cast<Clusterable *>(ds);
};
};
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */

View File

@ -67,10 +67,16 @@ class OneClusterHelper < OpenNebulaHelper::OneHelper
puts str % ["NAME", cluster.name]
puts
CLIHelper.print_header(str_h1 % "HOSTS", false)
CLIHelper.print_header("%-15s" % ["ID"])
CLIHelper.print_header("%-15s" % ["HOSTS"])
cluster.host_ids.each do |id|
puts "%-15s" % [id]
end
puts
CLIHelper.print_header("%-15s" % ["DATASTORES"])
cluster.datastore_ids.each do |id|
puts "%-15s" % [id]
end
end
end

View File

@ -107,4 +107,14 @@ cmd=CommandParser::CmdParser.new(ARGV) do
end
end
adddatastore_desc = <<-EOT.unindent
Adds a Datastore to the given Cluster
EOT
# TODO: allow the second param to be [:range, :datastoreid_list]
command :adddatastore, adddatastore_desc,:clusterid, :datastoreid do
helper.perform_actions(args[0],options,"updated") do |cluster|
cluster.adddatastore(args[1].to_i)
end
end
end

View File

@ -34,9 +34,12 @@ const char * Datastore::db_bootstrap =
/* ************************************************************************ */
Datastore::Datastore(int id,
DatastoreTemplate* ds_template):
DatastoreTemplate* ds_template,
int cluster_id,
const string& cluster_name):
PoolObjectSQL(id,DATASTORE,"",-1,-1,"","",table),
ObjectCollection("IMAGES"),
Clusterable(cluster_id, cluster_name),
type(""),
tm_mad(""),
base_path("")
@ -235,7 +238,9 @@ string& Datastore::to_xml(string& xml) const
"<NAME>" << name << "</NAME>" <<
"<TYPE>" << type << "</TYPE>" <<
"<TM_MAD>" << tm_mad << "</TM_MAD>" <<
"<BASE_PATH>" << base_path << "</BASE_PATH>" <<
"<BASE_PATH>" << base_path << "</BASE_PATH>" <<
"<CLUSTER_ID>" << cluster_id << "</CLUSTER_ID>" <<
"<CLUSTER>" << cluster << "</CLUSTER>" <<
collection_xml <<
"</DATASTORE>";
@ -262,6 +267,9 @@ int Datastore::from_xml(const string& xml)
rc += xpath(tm_mad, "/DATASTORE/TM_MAD", "not_found");
rc += xpath(base_path, "/DATASTORE/BASE_PATH", "not_found");
rc += xpath(cluster_id, "/DATASTORE/CLUSTER_ID", -1);
rc += xpath(cluster, "/DATASTORE/CLUSTER", "not_found");
// Set the owner and group to oneadmin
set_user(0, "");
set_group(GroupPool::ONEADMIN_ID, GroupPool::ONEADMIN_NAME);

View File

@ -43,8 +43,7 @@ DatastorePool::DatastorePool(SqlDB * db):
if (get_lastOID() == -1) //lastOID is set in PoolSQL::init_cb
{
int rc;
Datastore * ds;
int rc;
DatastoreTemplate * ds_tmpl;
// ---------------------------------------------------------------------
@ -64,9 +63,11 @@ DatastorePool::DatastorePool(SqlDB * db):
goto error_bootstrap;
}
ds = new Datastore(-1, ds_tmpl);
rc = PoolSQL::allocate(ds, error_str);
allocate(ds_tmpl,
&rc,
ClusterPool::DEFAULT_CLUSTER_ID,
ClusterPool::DEFAULT_CLUSTER_NAME,
error_str);
if( rc < 0 )
{
@ -91,9 +92,11 @@ DatastorePool::DatastorePool(SqlDB * db):
goto error_bootstrap;
}
ds = new Datastore(-1, ds_tmpl);
rc = PoolSQL::allocate(ds, error_str);
allocate(ds_tmpl,
&rc,
ClusterPool::DEFAULT_CLUSTER_ID,
ClusterPool::DEFAULT_CLUSTER_NAME,
error_str);
if( rc < 0 )
{
@ -118,6 +121,8 @@ error_bootstrap:
int DatastorePool::allocate(DatastoreTemplate * ds_template,
int * oid,
int cluster_id,
const string& cluster_name,
string& error_str)
{
Datastore * ds;
@ -125,7 +130,11 @@ int DatastorePool::allocate(DatastoreTemplate * ds_template,
string name;
ostringstream oss;
ds = new Datastore(-1, ds_template);
Nebula& nd = Nebula::instance();
ClusterPool * clpool;
Cluster * cluster;
ds = new Datastore(-1, ds_template, cluster_id, cluster_name);
// -------------------------------------------------------------------------
// Check name & duplicates
@ -152,6 +161,29 @@ int DatastorePool::allocate(DatastoreTemplate * ds_template,
*oid = PoolSQL::allocate(ds, error_str);
if ( *oid < 0 )
{
return *oid;
}
// Add to Cluster
clpool = nd.get_clpool();
cluster = clpool->get(cluster_id, true);
if( cluster == 0 )
{
return -1;
}
if ( cluster->add_datastore(*oid, error_str) < 0 )
{
return -1;
}
clpool->update(cluster);
cluster->unlock();
return *oid;
error_name:

View File

@ -281,6 +281,8 @@ void Nebula::start()
vector<const Attribute *> vm_restricted_attrs;
vector<const Attribute *> img_restricted_attrs;
clpool = new ClusterPool(db);
nebula_configuration->get("VM_HOOK", vm_hooks);
nebula_configuration->get("HOST_HOOK", host_hooks);
@ -315,8 +317,6 @@ void Nebula::start()
tpool = new VMTemplatePool(db);
dspool = new DatastorePool(db);
clpool = new ClusterPool(db);
}
catch (exception&)
{

View File

@ -24,10 +24,11 @@ module OpenNebula
#######################################################################
CLUSTER_METHODS = {
:info => "cluster.info",
:allocate => "cluster.allocate",
:delete => "cluster.delete",
:addhost => "cluster.addhost"
:info => "cluster.info",
:allocate => "cluster.allocate",
:delete => "cluster.delete",
:addhost => "cluster.addhost",
:adddatastore => "cluster.adddatastore"
}
# Creates a Cluster description with just its identifier
@ -75,6 +76,8 @@ module OpenNebula
# Adds a Host to this Cluster
# @param hid [Integer] Host ID
# @return [nil, OpenNebula::Error] nil in case of success, Error
# otherwise
def addhost(hid)
return Error.new('ID not defined') if !@pe_id
@ -84,20 +87,36 @@ module OpenNebula
return rc
end
# Adds a Datastore to this Cluster
# @param ds_id [Integer] Datastore ID
# @return [nil, OpenNebula::Error] nil in case of success, Error
# otherwise
def adddatastore(ds_id)
return Error.new('ID not defined') if !@pe_id
rc = @client.call(CLUSTER_METHODS[:adddatastore], @pe_id, ds_id)
rc = nil if !OpenNebula.is_error?(rc)
return rc
end
# ---------------------------------------------------------------------
# Helpers to get information
# ---------------------------------------------------------------------
# Returns whether or not the host with id 'uid' is part of this group
def contains(uid)
# Returns whether or not the host with 'id' is part of this cluster
# @param id [Integer] host ID
# @return [Boolean] true if found
def contains_host(id)
#This doesn't work in ruby 1.8.5
#return self["HOSTS/ID[.=#{uid}]"] != nil
id_array = retrieve_elements('HOSTS/ID')
return id_array != nil && id_array.include?(uid.to_s)
return id_array != nil && id_array.include?(id.to_s)
end
# Returns an array with the numeric host ids
# @return [Array<Integer>]
def host_ids
array = Array.new
@ -107,5 +126,28 @@ module OpenNebula
return array
end
# Returns whether or not the datastore with 'id' is part of this cluster
# @param id [Integer] datastore ID
# @return [Boolean] true if found
def contains_datastore(id)
#This doesn't work in ruby 1.8.5
#return self["DATASTORES/ID[.=#{uid}]"] != nil
id_array = retrieve_elements('DATASTORES/ID')
return id_array != nil && id_array.include?(id.to_s)
end
# Returns an array with the numeric datastore ids
# @return [Array<Integer>]
def datastore_ids
array = Array.new
self.each("DATASTORES/ID") do |id|
array << id.text.to_i
end
return array
end
end
end

View File

@ -327,6 +327,7 @@ void RequestManager::register_xml_methods()
// Cluster Methods
xmlrpc_c::methodPtr cluster_addhost(new ClusterAddHost());
xmlrpc_c::methodPtr cluster_addds(new ClusterAddDatastore());
/* VM related methods */
RequestManagerRegistry.addMethod("one.vm.deploy", vm_deploy);
@ -422,6 +423,7 @@ void RequestManager::register_xml_methods()
RequestManagerRegistry.addMethod("one.cluster.delete", cluster_delete);
RequestManagerRegistry.addMethod("one.cluster.info", cluster_info);
RequestManagerRegistry.addMethod("one.cluster.addhost", cluster_addhost);
RequestManagerRegistry.addMethod("one.cluster.adddatastore", cluster_addds);
RequestManagerRegistry.addMethod("one.clusterpool.info",clusterpool_info);
};

View File

@ -391,7 +391,13 @@ int DatastoreAllocate::pool_allocate(
DatastoreTemplate * ds_tmpl = static_cast<DatastoreTemplate *>(tmpl);
return dspool->allocate(ds_tmpl, &id, error_str);
// TODO: include another int parameter for the cluster?
int cluster_id = ClusterPool::DEFAULT_CLUSTER_ID;
string cluster_name = ClusterPool::DEFAULT_CLUSTER_NAME;
// TODO: Add to auth request CLUSTER MANAGE or ADMIN
return dspool->allocate(ds_tmpl, &id, cluster_id, cluster_name, error_str);
}
/* -------------------------------------------------------------------------- */