1
0
mirror of https://github.com/OpenNebula/one.git synced 2024-12-23 17:33:56 +03:00

Merge branch 'feature-2459' into one-4.4

This commit is contained in:
Ruben S. Montero 2013-11-15 13:33:12 +01:00
commit dc60d2089b
15 changed files with 270 additions and 48 deletions

View File

@ -146,9 +146,14 @@ public:
* attributes
*
* @param disk
* @param inherit_attrs Attributes to be inherited from the DS template
* into the disk
* @return 0 on success
*/
int disk_attribute(VectorAttribute * disk);
int disk_attribute(
VectorAttribute * disk,
const vector<string>& inherit_attrs);
/**
* Replace template for this object. Object should be updated

View File

@ -467,12 +467,15 @@ public:
* @param img_type will be set to the used image's type
* @param dev_prefix will be set to the defined dev_prefix,
* or the default one
* @param inherit_attrs Attributes to be inherited from the image template
* into the disk
*
* @return 0 on success, -1 otherwise
*/
int disk_attribute( VectorAttribute * disk,
ImageType& img_type,
string& dev_prefix);
int disk_attribute( VectorAttribute * disk,
ImageType& img_type,
string& dev_prefix,
const vector<string>& inherit_attrs);
/**
* Factory method for image templates

View File

@ -39,12 +39,15 @@ class ImagePool : public PoolSQL
{
public:
ImagePool(SqlDB * db,
const string& _default_type,
const string& _default_dev_prefix,
vector<const Attribute *>& restricted_attrs,
vector<const Attribute *> hook_mads,
const string& remotes_location);
ImagePool(
SqlDB * db,
const string& __default_type,
const string& __default_dev_prefix,
vector<const Attribute *>& restricted_attrs,
vector<const Attribute *> hook_mads,
const string& remotes_location,
const vector<const Attribute *>& _inherit_image_attrs,
const vector<const Attribute *>& _inherit_datastore_attrs);
~ImagePool(){};
@ -212,6 +215,16 @@ private:
**/
static string _default_dev_prefix;
/**
* Image attributes to be inherited into the VM disk
*/
vector<string> inherit_image_attrs;
/**
* Datastore attributes to be inherited into the VM disk
*/
vector<string> inherit_datastore_attrs;
//--------------------------------------------------------------------------
// Pool Attributes
// -------------------------------------------------------------------------

View File

@ -18,6 +18,7 @@
#define _NEBULA_UTIL_H_
#include <string>
#include <vector>
namespace one_util
{
@ -66,6 +67,23 @@ namespace one_util
* @return a new random password
*/
std::string random_password();
/**
* Splits a string, using the given delimiter
*
* @param st string to split
* @param delim delimiter character
* @param clean_empty true to clean empty split parts.
* Example for st "a::b:c"
* clean_empty true will return ["a", "b", "c"]
* clean_empty fase will return ["a", "", "b", "c"]
*
* @return a vector containing the resulting substrings
*/
std::vector<std::string> split(
const std::string& st,
char delim,
bool clean_empty=true);
};
#endif /* _NEBULA_UTIL_H_ */

View File

@ -201,9 +201,14 @@ public:
* * BRIDGE: for this virtual network
* @param nic attribute for the VM template
* @param vid of the VM getting the lease
* @param inherit_attrs Attributes to be inherited from the vnet template
* into the nic
* @return 0 on success
*/
int nic_attribute(VectorAttribute * nic, int vid);
int nic_attribute(
VectorAttribute * nic,
int vid,
const vector<string>& inherit_attrs);
private:

View File

@ -33,11 +33,12 @@ class VirtualNetworkPool : public PoolSQL
{
public:
VirtualNetworkPool(SqlDB * db,
const string& str_mac_prefix,
int default_size,
vector<const Attribute *> hook_mads,
const string& remotes_location);
VirtualNetworkPool(SqlDB * db,
const string& str_mac_prefix,
int default_size,
vector<const Attribute *> hook_mads,
const string& remotes_location,
const vector<const Attribute *>& _inherit_attrs);
~VirtualNetworkPool(){};
@ -174,6 +175,11 @@ private:
*/
static unsigned int _default_size;
/**
* VNet attributes to be injected into the VM nic
*/
vector<string> inherit_attrs;
/**
* Factory method to produce VN objects
* @return a pointer to the new VN

View File

@ -673,3 +673,27 @@ IMAGE_RESTRICTED_ATTR = "SOURCE"
#*******************************************************************************
#ONEGATE_ENDPOINT = "http://frontend:5030"
#*******************************************************************************
# Inherited Attributes Configuration
#*******************************************************************************
# The following attributes will be copied from the resource template to the
# instantiated VMs. More than one attribute can be defined.
#
# INHERIT_IMAGE_ATTR: Attribute to be copied from the Image template
# to each VM/DISK.
#
# INHERIT_DATASTORE_ATTR: Attribute to be copied from the Datastore template
# to each VM/DISK.
#
# INHERIT_VNET_ATTR: Attribute to be copied from the Network template
# to each VM/NIC.
#*******************************************************************************
#INHERIT_IMAGE_ATTR = "EXAMPLE"
#INHERIT_IMAGE_ATTR = "SECOND_EXAMPLE"
#INHERIT_DATASTORE_ATTR = "COLOR"
#INHERIT_VNET_ATTR = "BANDWIDTH_THROTTLING"
INHERIT_DATASTORE_ATTR = "CEPH_HOST"
INHERIT_DATASTORE_ATTR = "CEPH_SECRET"

View File

@ -217,3 +217,24 @@ string one_util::random_password()
return sha1_digest(sstr.str());
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
vector<string> one_util::split(const string& st, char delim, bool clean_empty)
{
vector<string> parts;
string part;
stringstream ss(st);
while (getline(ss, part, delim))
{
if (!(clean_empty && part.empty()))
{
parts.push_back(part);
}
}
return parts;
}

View File

@ -71,10 +71,15 @@ Datastore::Datastore(
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
int Datastore::disk_attribute(VectorAttribute * disk)
int Datastore::disk_attribute(
VectorAttribute * disk,
const vector<string>& inherit_attrs)
{
ostringstream oss;
string st;
string inherit_val;
vector<string>::const_iterator it;
oss << oid;
@ -104,6 +109,16 @@ int Datastore::disk_attribute(VectorAttribute * disk)
disk->replace("LN_TARGET", st);
}
for (it = inherit_attrs.begin(); it != inherit_attrs.end(); it++)
{
get_template_attribute((*it).c_str(), inherit_val);
if (!inherit_val.empty())
{
disk->replace(*it, inherit_val);
}
}
return 0;
}

View File

@ -467,18 +467,22 @@ int Image::from_xml(const string& xml)
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
int Image::disk_attribute( VectorAttribute * disk,
ImageType& img_type,
string& dev_prefix)
int Image::disk_attribute( VectorAttribute * disk,
ImageType& img_type,
string& dev_prefix,
const vector<string>& inherit_attrs)
{
string target;
string driver;
string disk_attr_type;
string inherit_val;
bool ro;
ostringstream iid;
vector<string>::const_iterator it;
img_type = type;
target = disk->vector_value("TARGET");
driver = disk->vector_value("DRIVER");
@ -614,6 +618,16 @@ int Image::disk_attribute( VectorAttribute * disk,
disk->replace("TARGET", template_target);
}
for (it = inherit_attrs.begin(); it != inherit_attrs.end(); it++)
{
get_template_attribute((*it).c_str(), inherit_val);
if (!inherit_val.empty())
{
disk->replace(*it, inherit_val);
}
}
return 0;
}

View File

@ -31,20 +31,38 @@ string ImagePool::_default_dev_prefix;
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
ImagePool::ImagePool(SqlDB * db,
const string& __default_type,
const string& __default_dev_prefix,
vector<const Attribute *>& restricted_attrs,
vector<const Attribute *> hook_mads,
const string& remotes_location)
ImagePool::ImagePool(
SqlDB * db,
const string& __default_type,
const string& __default_dev_prefix,
vector<const Attribute *>& restricted_attrs,
vector<const Attribute *> hook_mads,
const string& remotes_location,
const vector<const Attribute *>& _inherit_image_attrs,
const vector<const Attribute *>& _inherit_datastore_attrs)
:PoolSQL(db, Image::table, true)
{
ostringstream sql;
// Init static defaults
_default_type = __default_type;
_default_dev_prefix = __default_dev_prefix;
// Init inherit attributes
vector<const Attribute *>::const_iterator it;
for (it = _inherit_image_attrs.begin(); it != _inherit_image_attrs.end(); it++)
{
const SingleAttribute* sattr = static_cast<const SingleAttribute *>(*it);
inherit_image_attrs.push_back(sattr->value());
}
for (it = _inherit_datastore_attrs.begin(); it != _inherit_datastore_attrs.end(); it++)
{
const SingleAttribute* sattr = static_cast<const SingleAttribute *>(*it);
inherit_datastore_attrs.push_back(sattr->value());
}
// Set default type
if (_default_type != "OS" &&
_default_type != "CDROM" &&
@ -385,7 +403,7 @@ int ImagePool::disk_attribute(int vm_id,
Datastore * ds;
iid = img->get_oid();
rc = img->disk_attribute(disk, img_type, dev_prefix);
rc = img->disk_attribute(disk, img_type, dev_prefix, inherit_image_attrs);
image_id = img->get_oid();
datastore_id = img->get_ds_id();
@ -410,7 +428,7 @@ int ImagePool::disk_attribute(int vm_id,
return -1;
}
ds->disk_attribute(disk);
ds->disk_attribute(disk, inherit_datastore_attrs);
ds->unlock();
}

View File

@ -555,6 +555,10 @@ void Nebula::start(bool bootstrap_only)
vector<const Attribute *> vm_restricted_attrs;
vector<const Attribute *> img_restricted_attrs;
vector<const Attribute *> inherit_image_attrs;
vector<const Attribute *> inherit_datastore_attrs;
vector<const Attribute *> inherit_vnet_attrs;
clpool = new ClusterPool(db);
docpool = new DocumentPool(db);
@ -568,6 +572,10 @@ void Nebula::start(bool bootstrap_only)
nebula_configuration->get("VM_RESTRICTED_ATTR", vm_restricted_attrs);
nebula_configuration->get("IMAGE_RESTRICTED_ATTR", img_restricted_attrs);
nebula_configuration->get("INHERIT_IMAGE_ATTR", inherit_image_attrs);
nebula_configuration->get("INHERIT_DATASTORE_ATTR", inherit_datastore_attrs);
nebula_configuration->get("INHERIT_VNET_ATTR", inherit_vnet_attrs);
nebula_configuration->get("VM_MONITORING_EXPIRATION_TIME",vm_expiration);
nebula_configuration->get("HOST_MONITORING_EXPIRATION_TIME",host_expiration);
@ -593,7 +601,8 @@ void Nebula::start(bool bootstrap_only)
mac_prefix,
size,
vnet_hooks,
remotes_location);
remotes_location,
inherit_vnet_attrs);
gpool = new GroupPool(db, group_hooks, remotes_location);
@ -609,7 +618,9 @@ void Nebula::start(bool bootstrap_only)
default_device_prefix,
img_restricted_attrs,
image_hooks,
remotes_location);
remotes_location,
inherit_image_attrs,
inherit_datastore_attrs);
tpool = new VMTemplatePool(db);

View File

@ -59,6 +59,8 @@ int LibVirtDriver::deployment_description_kvm(
string disk_io = "";
string source = "";
string clone = "";
string ceph_host = "";
string ceph_secret= "";
int disk_id;
string default_driver = "";
@ -311,14 +313,16 @@ int LibVirtDriver::deployment_description_kvm(
continue;
}
type = disk->vector_value("TYPE");
target = disk->vector_value("TARGET");
ro = disk->vector_value("READONLY");
driver = disk->vector_value("DRIVER");
cache = disk->vector_value("CACHE");
disk_io= disk->vector_value("IO");
source = disk->vector_value("SOURCE");
clone = disk->vector_value("CLONE");
type = disk->vector_value("TYPE");
target = disk->vector_value("TARGET");
ro = disk->vector_value("READONLY");
driver = disk->vector_value("DRIVER");
cache = disk->vector_value("CACHE");
disk_io = disk->vector_value("IO");
source = disk->vector_value("SOURCE");
clone = disk->vector_value("CLONE");
ceph_host = disk->vector_value("CEPH_HOST");
ceph_secret = disk->vector_value("CEPH_SECRET");
disk->vector_value_str("DISK_ID", disk_id);
@ -360,7 +364,46 @@ int LibVirtDriver::deployment_description_kvm(
file << "-" << vm->get_oid() << "-" << disk_id;
}
file << "'/>" << endl;
if ( ceph_host.empty() )
{
file << "'/>" << endl;
}
else
{
vector<string>::const_iterator it;
vector<string> hosts = one_util::split(ceph_host, ' ');
file << "'>" << endl;
for (it = hosts.begin(); it != hosts.end(); it++)
{
vector<string> parts = one_util::split(*it, ':');
if (parts.empty())
{
continue;
}
file << "\t\t\t\t<host name='" << parts[0];
if (parts.size() > 1)
{
file << "' port='" << parts[1];
}
file << "'/>" << endl;
}
file << "\t\t\t</source>" << endl;
}
if ( !ceph_secret.empty() )
{
file << "\t\t\t<auth username='libvirt'>" << endl
<< "\t\t\t\t<secret type='ceph' uuid='"
<< ceph_secret <<"'/>" << endl
<< "\t\t\t</auth>" << endl;
}
}
else if ( type == "RBD_CDROM" )
{

View File

@ -706,18 +706,24 @@ int VirtualNetwork::from_xml(const string &xml_str)
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int VirtualNetwork::nic_attribute(VectorAttribute *nic, int vid)
int VirtualNetwork::nic_attribute(
VectorAttribute * nic,
int vid,
const vector<string>& inherit_attrs)
{
int rc;
string ip;
string mac;
string inherit_val;
unsigned int eui64[2];
unsigned int prefix[2] = {0, 0};
ostringstream oss;
vector<string>::const_iterator it;
ip = nic->vector_value("IP");
oss << oid;
@ -792,6 +798,16 @@ int VirtualNetwork::nic_attribute(VectorAttribute *nic, int vid)
nic->replace("CLUSTER_ID", oss.str());
}
for (it = inherit_attrs.begin(); it != inherit_attrs.end(); it++)
{
get_template_attribute((*it).c_str(), inherit_val);
if (!inherit_val.empty())
{
nic->replace(*it, inherit_val);
}
}
return 0;
}

View File

@ -32,11 +32,12 @@ unsigned int VirtualNetworkPool::_default_size;
/* -------------------------------------------------------------------------- */
VirtualNetworkPool::VirtualNetworkPool(
SqlDB * db,
const string& prefix,
int __default_size,
vector<const Attribute *> hook_mads,
const string& remotes_location):
SqlDB * db,
const string& prefix,
int __default_size,
vector<const Attribute *> hook_mads,
const string& remotes_location,
const vector<const Attribute *>& _inherit_attrs):
PoolSQL(db, VirtualNetwork::table, true)
{
istringstream iss;
@ -44,6 +45,8 @@ VirtualNetworkPool::VirtualNetworkPool(
int count = 0;
unsigned int tmp;
vector<const Attribute *>::const_iterator it;
string mac = prefix;
_mac_prefix = 0;
@ -71,6 +74,13 @@ VirtualNetworkPool::VirtualNetworkPool(
_mac_prefix += tmp;
register_hooks(hook_mads, remotes_location);
for (it = _inherit_attrs.begin(); it != _inherit_attrs.end(); it++)
{
const SingleAttribute* sattr = static_cast<const SingleAttribute *>(*it);
inherit_attrs.push_back(sattr->value());
}
}
/* -------------------------------------------------------------------------- */
@ -250,7 +260,7 @@ int VirtualNetworkPool::nic_attribute(VectorAttribute * nic,
return -1;
}
int rc = vnet->nic_attribute(nic,vid);
int rc = vnet->nic_attribute(nic, vid, inherit_attrs);
if ( rc == 0 )
{