1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-01-10 01:17:40 +03:00

Functions to generate the context file for a VM, and some minor

modifications and new functionality to deal with the Teamplates


git-svn-id: http://svn.opennebula.org/one/trunk@386 3034c82b-c49b-4eb3-8279-a7acafdc01c0
This commit is contained in:
Rubén S. Montero 2009-03-06 12:10:15 +00:00
parent 294950443c
commit e799c8b3ad
13 changed files with 802 additions and 412 deletions

View File

@ -72,7 +72,7 @@ public:
* by the calling function.
* @return a string (allocated in the heap) holding the attribute value.
*/
virtual string * marshall(const char * _sep = 0) = 0;
virtual string * marshall(const char * _sep = 0) const = 0;
/**
* Write the attribute using a simple XML format. The string MUST be freed
@ -84,7 +84,7 @@ public:
/**
* Builds a new attribute from a string.
*/
virtual void unmarshall(const string& sattr) = 0;
virtual void unmarshall(const string& sattr, const char * _sep = 0) = 0;
/**
* Returns the attribute type
@ -134,7 +134,7 @@ public:
* by the calling function.
* @return a string (allocated in the heap) holding the attribute value.
*/
string * marshall(const char * _sep = 0)
string * marshall(const char * _sep = 0) const
{
string * rs = new string;
@ -164,7 +164,7 @@ public:
/**
* Builds a new attribute from a string.
*/
void unmarshall(const string& sattr)
void unmarshall(const string& sattr, const char * _sep = 0)
{
attribute_value = sattr;
};
@ -228,7 +228,7 @@ public:
* "VAL_NAME_1=VAL_VALUE_1,...,VAL_NAME_N=VAL_VALUE_N".
* @return a string (allocated in the heap) holding the attribute value.
*/
string * marshall(const char * _sep = 0);
string * marshall(const char * _sep = 0) const;
/**
* Write the attribute using a simple XML format:
@ -248,7 +248,7 @@ public:
* Builds a new attribute from a string of the form:
* "VAL_NAME_1=VAL_VALUE_1,...,VAL_NAME_N=VAL_VALUE_N".
*/
void unmarshall(const string& sattr);
void unmarshall(const string& sattr, const char * _sep = 0);
/**
* Replace the value of the given attribute with the provided map

View File

@ -140,6 +140,7 @@ private:
string vm_lhome;
string transfer_file;
string deployment_file;
string context_file;
string vm_rhome;
string checkpoint_file;

View File

@ -107,6 +107,17 @@ public:
attributes.insert(make_pair(attr->name(),attr));
};
/**
* Removes an attribute from the template. The attributes are returned. The
* attributes MUST be freed by the calling funtion
* @param name of the attribute
* @param values a vector containing a pointer to the attributes
* @returns the number of attributes removed
*/
virtual int remove(
const string& name,
vector<Attribute *>& values);
/**
* Gets all the attributes with the given name.
* @param name the attribute name.

View File

@ -88,13 +88,21 @@ protected:
int drop(SqliteDB *db);
/**
* Removes a template attribute from the DB (ONLY SINGLE ATTRIBUTES)
* 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 name of the attribute.
* @param value of the new attribute.
* @param attribute pointer to the new attribute.
*/
int replace_attribute(SqliteDB * db, const string& name, const string& value);
int replace_attribute(SqliteDB * 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(SqliteDB * db, Attribute * attribute);
};
/* -------------------------------------------------------------------------- */

View File

@ -276,6 +276,19 @@ public:
return history->deployment_file;
};
/**
* Returns the context filename. The context file is in the form:
* $ONE_LOCATION/var/$VM_ID/context.sh
* or, in case that OpenNebula is installed in root
* /var/lib/one/$VM_ID/context.sh
* The hasHistory() function MUST be called before this one.
* @return the deployment filename
*/
const string & get_context_file() const
{
return history->context_file;
}
/**
* Returns the remote deployment filename. The file is in the form:
* $VM_DIR/$VM_ID/images/deployment.$SEQ
@ -639,6 +652,12 @@ public:
*/
void release_leases();
/**
* Writes the context file for this VM.
* @return 0 if success
*/
int write_context();
private:
// -------------------------------------------------------------------------
@ -808,7 +827,7 @@ private:
/**
* 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 db pointer to the database
* @param name of the new attribute
* @param value of the new attribute
* @return 0 on success
@ -818,9 +837,31 @@ private:
string& name,
string& value)
{
return vm_template.replace_attribute(db,name,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(SqliteDB * db, Attribute * attribute)
{
return vm_template.insert_attribute(db,attribute);
}
protected:
//**************************************************************************

View File

@ -124,6 +124,20 @@ public:
return vm->update_previous_history(db);
}
/**
* Parse a string and substitute variables (e.g. $NAME) using template
* values:
* @param vm_id, ID of the VM used to substitute the variables
* @param attribute, the string to be parsed
* @param parsed, the resulting parsed string
* @param error_msg, string describing the syntax error
* @return 0 on success.
*/
int parse_attribute(int vm_id,
string &attribute,
string &parsed,
char ** error_msg);
/**
* Bootstraps the database table(s) associated to the VirtualMachine pool
*/
@ -133,6 +147,18 @@ public:
};
private:
/**
* Mutex to perform just one attribute parse at a time
*/
static pthread_mutex_t lex_mutex;
/**
* Generate context file to be sourced upon VM booting
* @param vm_id, ID of the VM to generate context for
* @param attrs, the template CONTEXT attributes (only the first one will
* be used)
*/
void generate_context(int vm_id, vector<Attribute *> attrs);
/**
* Factory method to produce VM objects

View File

@ -285,7 +285,16 @@ private:
string& name,
string& value)
{
return vn_template.replace_attribute(db,name,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:

View File

@ -17,6 +17,7 @@
#include <string>
#include <sstream>
#include <cstring>
#include "Attribute.h"
@ -24,12 +25,13 @@
const char * VectorAttribute::magic_sep = "@^_^@";
const int VectorAttribute::magic_sep_size = 5;
string * VectorAttribute::marshall(const char * _sep)
string * VectorAttribute::marshall(const char * _sep) const
{
ostringstream os;
map<string,string>::iterator it;
string * rs;
const char * my_sep;
ostringstream os;
string * rs;
const char * my_sep;
map<string,string>::const_iterator it;
if ( _sep == 0 )
{
@ -91,15 +93,29 @@ string * VectorAttribute::to_xml() const
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
void VectorAttribute::unmarshall(const string& sattr)
void VectorAttribute::unmarshall(const string& sattr, const char * _sep)
{
size_t bpos=0,epos,mpos;
size_t bpos=0,epos,mpos;
string tmp;
bool cont = true;
bool cont = true;
const char * my_sep;
int my_sep_size;
if ( _sep == 0 )
{
my_sep = magic_sep;
my_sep_size = magic_sep_size;
}
else
{
my_sep = _sep;
my_sep_size = strlen(_sep);
}
while(cont)
{
epos=sattr.find(magic_sep,bpos);
epos=sattr.find(my_sep,bpos);
if (epos == string::npos)
{
@ -109,7 +125,7 @@ void VectorAttribute::unmarshall(const string& sattr)
else
{
tmp = sattr.substr(bpos,epos-bpos);
bpos = epos + magic_sep_size;
bpos = epos + my_sep_size;
}
mpos = tmp.find('=');

View File

@ -242,62 +242,84 @@ int TemplateSQL::drop(SqliteDB * db)
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int TemplateSQL::replace_attribute(
SqliteDB * db,
const string& name,
const string& value)
int TemplateSQL::replace_attribute(SqliteDB * db, Attribute * attribute)
{
ostringstream oss;
int rc;
ostringstream oss;
int rc;
string * astr;
multimap<string, Attribute *>::const_iterator i;
Attribute * attribute;
multimap<string, Attribute *>::iterator i;
if ( id == -1 || name.empty() || value.empty() )
if ( id == -1 || attribute == 0)
{
return -1;
}
i = attributes.find(name);
i = attributes.find(attribute->name());
if ( i != attributes.end() ) //attribute exists
if ( i != attributes.end() )
{
string * attr = i->second->marshall();
astr = i->second->marshall();
if ( attr != 0 )
{
oss << "DELETE FROM " << table << " WHERE id=" << id
<< " AND name='" << name << "' AND value='" << *attr << "'";
if ( astr == 0 )
{
return -1;
}
delete attr;
}
else
{
oss << "DELETE FROM " << table << " WHERE id=" << id
<< " AND name='" << name << "'";
}
oss << "DELETE FROM " << table << " WHERE id=" << id
<< " AND name='" << attribute->name() << "' AND value='"
<< *astr << "'";
rc = db->exec(oss);
delete astr;
if ( rc != 0 )
{
return rc;
}
if ((rc = db->exec(oss)) != 0 )
{
return rc;
}
attributes.erase(name);
delete i->second;
attributes.erase(i);
}
attribute = new SingleAttribute(name,value);
attributes.insert(make_pair(attribute->name(),attribute));
oss.str("");
oss << "INSERT INTO " << table << " " << db_names
<< " VALUES (" << id << ",'" << name << "'," <<
Attribute::SIMPLE <<",'" << value << "')";
return db->exec(oss);
return insert_attribute(db,attribute);
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int TemplateSQL::insert_attribute(SqliteDB * db, Attribute * attribute)
{
ostringstream oss;
int rc;
string * astr;
int atype;
if ( id == -1 || attribute == 0)
{
return -1;
}
astr = attribute->marshall();
atype = attribute->type();
if ( astr == 0 )
{
return -1;
}
oss << "INSERT INTO " << table << " " << db_names
<< " VALUES (" << id << ",'" << attribute->name() << "'," << atype
<< ",'" << *astr << "')";
delete astr;
if ((rc = db->exec(oss)) == 0)
{
attributes.insert(make_pair(attribute->name(),attribute));
}
return rc;
}
/* -------------------------------------------------------------------------- */

View File

@ -111,6 +111,11 @@ void History::non_persistent_data()
transfer_file = os.str();
os.str("");
os << vm_lhome << "/context.sh";
context_file = os.str();
// ----------- Remote Locations ------------
os.str("");
os << vm_dir << "/" << oid << "/images";

View File

@ -21,12 +21,27 @@ Import('env')
lib_name='nebula_vm'
if env['parsers']=='yes':
# LEX
parser=env.Lex(
source='vm_var_parser.l'
)
env.NoClean(parser)
# BISON
parser=env.Bison(
source='vm_var_syntax.y'
)
env.NoClean(parser)
# Sources to generate the library
source_files=[
'History.cc',
'VirtualMachine.cc',
'vm_var_parser.c',
'vm_var_syntax.cc',
'VirtualMachinePool.cc',
'VirtualMachineTemplate.cc',
'VirtualMachineTemplate.cc'
]
# Build library

View File

@ -266,26 +266,42 @@ error_previous_history:
int VirtualMachine::insert(SqliteDB * db)
{
int rc;
string name;
int rc;
string name;
//Set a name if the VM has not got one
SingleAttribute * attr;
string value;
ostringstream oss;
// ------------------------------------------------------------------------
// Set a name if the VM has not got one and VM_ID
// ------------------------------------------------------------------------
get_template_attribute("NAME",name);
if ( name.empty() == true )
{
SingleAttribute * name_attr;
ostringstream default_name;
oss << "one-" << oid;
value = oss.str();
default_name << "one-" << oid;
name = default_name.str();
attr = new SingleAttribute("NAME",value);
name_attr = new SingleAttribute("NAME",name);
vm_template.set(name_attr);
vm_template.set(attr);
}
oss.str("");
oss << oid;
value = oss.str();
attr = new SingleAttribute("VM_ID",value);
vm_template.set(attr);
// ------------------------------------------------------------------------
// Get network leases
// ------------------------------------------------------------------------
rc = get_leases();
if ( rc != 0 )
@ -293,20 +309,22 @@ int VirtualMachine::insert(SqliteDB * db)
goto error_leases;
}
// Insert the template first, so we get a valid template ID
// ------------------------------------------------------------------------
// Insert the template first, so we get a valid template ID. Then the VM
// ------------------------------------------------------------------------
rc = vm_template.insert(db);
if ( rc != 0 )
{
goto error_template;
goto error_template;
}
//Insert the VM
rc = update(db);
if ( rc != 0 )
{
goto error_update;
goto error_update;
}
return 0;
@ -521,7 +539,7 @@ int VirtualMachine::get_leases()
if ( vn == 0 )
{
continue;
return -1;
}
ip = nic->vector_value("IP");
@ -611,6 +629,60 @@ void VirtualMachine::release_leases()
}
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int VirtualMachine::write_context()
{
ofstream file;
vector<const Attribute*> attrs;
const VectorAttribute * context;
map<string, string>::const_iterator it;
if ( history == 0 )
return -1;
if ( get_template_attribute("CONTEXT",attrs) != 1 )
{
log("VM", Log::INFO, "Virtual Machine has no context");
return 0;
}
file.open(history->context_file.c_str(),ios::out);
if (file.fail() == true)
{
ostringstream oss;
oss << "Could not open context file: " << history->context_file;
log("VM", Log::ERROR, oss);
return -1;
}
context = dynamic_cast<const VectorAttribute *>(attrs[0]);
if (context == 0)
{
file.close();
return -1;
}
const map<string, string> values = context->value();
file << "# Context variables generated by OpenNebula\n";
for (it=values.begin(); it != values.end(); it++ )
{
file << it->first <<"=\""<< it->second << "\"" << endl;
}
file.close();
return 0;
}
/* ************************************************************************** */
/* Virtual Machine :: Misc */
/* ************************************************************************** */

View File

@ -17,6 +17,11 @@
#include "Nebula.h"
#include "VirtualMachinePool.h"
#include "vm_var_syntax.h"
extern "C"
{
#include "vm_var_parser.h"
}
#include <sstream>
int VirtualMachinePool::allocate (
@ -25,10 +30,12 @@ int VirtualMachinePool::allocate (
int * oid,
bool on_hold)
{
VirtualMachine * vm;
VirtualMachine * vm;
char * error_msg;
int rc;
char * error_msg;
int rc;
vector<Attribute *> attrs;
// Build a new Virtual Machine object
@ -58,6 +65,8 @@ int VirtualMachinePool::allocate (
return -2;
}
vm->vm_template.remove("CONTEXT",attrs);
// Insert the Object in the pool
*oid = PoolSQL::allocate(vm);
@ -67,6 +76,14 @@ int VirtualMachinePool::allocate (
return -1;
}
generate_context(*oid,attrs);
for (int i = 0; i < attrs.size() ; i++)
{
if (attrs[i] != 0)
delete attrs[i];
}
return 0;
}
@ -102,3 +119,150 @@ int VirtualMachinePool::get_pending(
return PoolSQL::search(oids,VirtualMachine::table,where);
};
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
void VirtualMachinePool::generate_context(int vm_id, vector<Attribute *> attrs)
{
VirtualMachine * vm;
VectorAttribute * context_parsed;
VectorAttribute * context;
string * str;
string parsed;
int rc;
char * error_msg;
if ( attrs.size() == 0 )
{
return;
}
context = dynamic_cast<VectorAttribute *>(attrs[0]);
if (context == 0)
{
return;
}
str = context->marshall(" @^_^@ ");
if (str == 0)
{
return;
}
rc = parse_attribute(vm_id,*str,parsed,&error_msg);
if ( rc != 0 )
{
if (error_msg != 0)
{
ostringstream oss;
oss << error_msg << ": " << *str;
free(error_msg);
Nebula::log("ONE", Log::ERROR, oss);
}
delete str;
return;
}
delete str;
context_parsed = new VectorAttribute("CONTEXT");
context_parsed->unmarshall(parsed," @^_^@ ");
vm = get(vm_id,true);
if ( vm == 0 )
{
return;
}
vm->insert_template_attribute(db,context_parsed);
vm->unlock();
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
pthread_mutex_t VirtualMachinePool::lex_mutex = PTHREAD_MUTEX_INITIALIZER;
extern "C"
{
int vm_var_parse (VirtualMachinePool * vmpool,
ostringstream * parsed,
VirtualMachine * vm,
char ** errmsg);
int vm_var_lex_destroy();
YY_BUFFER_STATE vm_var__scan_string(const char * str);
void vm_var__delete_buffer(YY_BUFFER_STATE);
}
int VirtualMachinePool::parse_attribute(int vm_id,
string &attribute,
string &parsed,
char ** error_msg)
{
YY_BUFFER_STATE str_buffer;
const char * str;
int rc;
VirtualMachine * vm;
ostringstream oss_parsed;
*error_msg = 0;
pthread_mutex_lock(&lex_mutex);
vm = get(vm_id,true);
if ( vm == 0 )
{
goto error_vm;
}
str = attribute.c_str();
str_buffer = vm_var__scan_string(str);
if (str_buffer == 0)
{
goto error_yy;
}
rc = vm_var_parse(this,&oss_parsed,vm,error_msg);
vm_var__delete_buffer(str_buffer);
vm_var_lex_destroy();
vm->unlock();
pthread_mutex_unlock(&lex_mutex);
parsed = oss_parsed.str();
return rc;
error_vm:
*error_msg=strdup("Could not find virtual machine!");
goto error_common;
error_yy:
*error_msg=strdup("Error setting scan buffer");
vm->unlock();
error_common:
pthread_mutex_unlock(&lex_mutex);
return -1;
}