1
0
mirror of https://github.com/OpenNebula/one.git synced 2024-12-22 13:33:52 +03:00

feature #282 & #192: VirtualMachineTemplate moved from its own table to a vm_pool column.

This commit is contained in:
Carlos Martín 2010-08-02 18:14:04 +02:00 committed by Ruben S. Montero
parent 8b9313699d
commit 7221ae061c
7 changed files with 64 additions and 103 deletions

View File

@ -869,11 +869,9 @@ private:
static void bootstrap(SqlDB * db)
{
ostringstream oss_vm(VirtualMachine::db_bootstrap);
ostringstream oss_tmpl(VirtualMachineTemplate::db_bootstrap);
ostringstream oss_hist(History::db_bootstrap);
db->exec(oss_vm);
db->exec(oss_tmpl);
db->exec(oss_hist);
};
@ -928,40 +926,39 @@ private:
/**
* Updates the template of a VM, adding a new attribute (replacing it if
* already defined), the vm's mutex SHOULD be locked
* @param db pointer to the database
* @param name of the new attribute
* @param value of the new attribute
* @return 0 on success
*/
int update_template_attribute(
SqlDB * db,
string& name,
string& value)
{
SingleAttribute * sattr;
int rc;
vm_template->erase(name);
sattr = new SingleAttribute(name,value);
rc = vm_template->replace_attribute(db,sattr);
vm_template->set(sattr);
if (rc != 0)
{
delete sattr;
}
return rc;
return 0;
}
/**
* 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
* Inserts a new attribute in the template of a VM.
* The vm's mutex SHOULD be locked.
* @param attribute the new attribute for the template
* @return 0 on success
*/
int insert_template_attribute(SqlDB * db, Attribute * attribute)
int insert_template_attribute(Attribute * attribute)
{
return vm_template->insert_attribute(db,attribute);
if( attribute == 0 )
{
return -1;
}
vm_template->set(attribute);
return 0;
}
// -------------------------------------------------------------------------
@ -1023,7 +1020,8 @@ protected:
NET_TX = 11,
NET_RX = 12,
LAST_SEQ = 13,
LIMIT = 14
TEMPLATE = 14,
LIMIT = 15
};
static const char * table;

View File

@ -103,7 +103,7 @@ public:
string& name,
string& value)
{
return vm->update_template_attribute(db,name,value);
return vm->update_template_attribute(name,value);
}
/**

View File

@ -17,29 +17,25 @@
#ifndef VIRTUAL_MACHINE_TEMPLATE_H_
#define VIRTUAL_MACHINE_TEMPLATE_H_
#include "TemplateSQL.h"
#include "Template.h"
using namespace std;
/**
* Virtual Machine Template class, it represents a VM configuration file.
*/
class VirtualMachineTemplate : public TemplateSQL
class VirtualMachineTemplate : public Template
{
public:
VirtualMachineTemplate(int tid = -1):
TemplateSQL(table,tid,false,'=',"TEMPLATE"){};
VirtualMachineTemplate():
Template(false,'=',"TEMPLATE"){};
~VirtualMachineTemplate(){};
private:
friend class VirtualMachine;
static const char * table;
static const char * db_bootstrap;
};
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */

View File

@ -40,7 +40,6 @@ source_files=[
'vm_var_parser.c',
'vm_var_syntax.cc',
'VirtualMachinePool.cc',
'VirtualMachineTemplate.cc',
'VirtualMachineHook.cc'
]

View File

@ -96,14 +96,14 @@ const char * VirtualMachine::table = "vm_pool";
const char * VirtualMachine::db_names =
"(oid,uid,name,last_poll, state,lcm_state,stime,etime,deploy_id"
",memory,cpu,net_tx,net_rx,last_seq)";
",memory,cpu,net_tx,net_rx,last_seq, template)";
const char * VirtualMachine::db_bootstrap = "CREATE TABLE IF NOT EXISTS "
"vm_pool ("
"oid INTEGER PRIMARY KEY,uid INTEGER,name TEXT,"
"last_poll INTEGER, state INTEGER,lcm_state INTEGER,"
"stime INTEGER,etime INTEGER,deploy_id TEXT,memory INTEGER,cpu INTEGER,"
"net_tx INTEGER,net_rx INTEGER, last_seq INTEGER)";
"net_tx INTEGER,net_rx INTEGER, last_seq INTEGER, template TEXT)";
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
@ -123,6 +123,7 @@ int VirtualMachine::select_cb(void *nil, int num, char **values, char **names)
(values[NET_TX] == 0) ||
(values[NET_RX] == 0) ||
(values[LAST_SEQ] == 0) ||
(values[TEMPLATE] == 0) ||
(num != LIMIT ))
{
return -1;
@ -151,8 +152,8 @@ int VirtualMachine::select_cb(void *nil, int num, char **values, char **names)
net_rx = atoi(values[NET_RX]);
last_seq = atoi(values[LAST_SEQ]);
// Virtual Machine template ID is the VM ID
vm_template->id = oid;
// Virtual Machine template
vm_template->from_xml(values[TEMPLATE]);
return 0;
}
@ -187,14 +188,6 @@ int VirtualMachine::select(SqlDB * db)
goto error_id;
}
//Get the template
rc = vm_template->select(db);
if (rc != 0)
{
goto error_template;
}
//Get the History Records
if ( last_seq != -1 )
@ -221,7 +214,7 @@ int VirtualMachine::select(SqlDB * db)
}
}
//Create support directory fo this VM
//Create support directory for this VM
oss.str("");
oss << nd.get_var_location() << oid;
@ -229,7 +222,7 @@ int VirtualMachine::select(SqlDB * db)
mkdir(oss.str().c_str(), 0777);
chmod(oss.str().c_str(), 0777);
//Create Log support fo this VM
//Create Log support for this VM
try
{
@ -250,11 +243,6 @@ error_id:
log("VMM", Log::ERROR, ose);
return -1;
error_template:
ose << "Can not get template for VM id: " << oid;
log("ONE", Log::ERROR, ose);
return -1;
error_history:
ose << "Can not get history for VM id: " << oid;
log("ONE", Log::ERROR, ose);
@ -279,13 +267,6 @@ int VirtualMachine::insert(SqlDB * db)
string value;
ostringstream oss;
// -----------------------------------------------------------------------
// Set a template ID if it wasn't already assigned
// ------------------------------------------------------------------------
if ( vm_template->id == -1 )
{
vm_template->id = oid;
}
// -----------------------------------------------------------------------
// Set a name if the VM has not got one and VM_ID
@ -354,16 +335,9 @@ int VirtualMachine::insert(SqlDB * db)
parse_graphics();
// ------------------------------------------------------------------------
// Insert the template first, so we get a valid template ID. Then the VM
// Insert the VM
// ------------------------------------------------------------------------
rc = vm_template->insert(db);
if ( rc != 0 )
{
goto error_template;
}
rc = insert_replace(db, false);
if ( rc != 0 )
@ -375,11 +349,6 @@ int VirtualMachine::insert(SqlDB * db)
error_update:
NebulaLog::log("ONE",Log::ERROR, "Can not update VM in the database");
vm_template->drop(db);
goto error_common;
error_template:
NebulaLog::log("ONE",Log::ERROR, "Can not insert template in the database");
goto error_common;
error_leases:
@ -590,24 +559,34 @@ int VirtualMachine::insert_replace(SqlDB *db, bool replace)
ostringstream oss;
int rc;
string xml_template;
char * sql_deploy_id;
char * sql_name;
char * sql_template;
sql_deploy_id = db->escape_str(deploy_id.c_str());
if ( sql_deploy_id == 0 )
{
return -1;
goto error_deploy;
}
sql_name = db->escape_str(name.c_str());
if ( sql_name == 0 )
{
db->free_str(sql_deploy_id);
return -1;
goto error_name;
}
vm_template->to_xml(xml_template);
sql_template = db->escape_str(xml_template.c_str());
if ( sql_template == 0 )
{
goto error_template;
}
if(replace)
{
oss << "REPLACE";
@ -631,14 +610,23 @@ int VirtualMachine::insert_replace(SqlDB *db, bool replace)
<< cpu << ","
<< net_tx << ","
<< net_rx << ","
<< last_seq << ")";
<< last_seq << ","
<< "'" << sql_template << "')";
db->free_str(sql_deploy_id);
db->free_str(sql_name);
db->free_str(sql_template);
rc = db->exec(oss);
return rc;
error_template:
db->free_str(sql_name);
error_name:
db->free_str(sql_deploy_id);
error_deploy:
return -1;
}
/* -------------------------------------------------------------------------- */
@ -660,6 +648,7 @@ int VirtualMachine::dump(ostringstream& oss,int num,char **values,char **names)
(!values[NET_TX])||
(!values[NET_RX])||
(!values[LAST_SEQ])||
(!values[TEMPLATE])||
(num != (LIMIT + History::LIMIT + 1)))
{
return -1;
@ -681,7 +670,8 @@ int VirtualMachine::dump(ostringstream& oss,int num,char **values,char **names)
"<CPU>" << values[CPU] << "</CPU>" <<
"<NET_TX>" << values[NET_TX] << "</NET_TX>" <<
"<NET_RX>" << values[NET_RX] << "</NET_RX>" <<
"<LAST_SEQ>" << values[LAST_SEQ] << "</LAST_SEQ>";
"<LAST_SEQ>" << values[LAST_SEQ] << "</LAST_SEQ>" <<
values[TEMPLATE];
History::dump(oss, num-LIMIT-1, values+LIMIT+1, names+LIMIT+1);

View File

@ -1,23 +0,0 @@
/* -------------------------------------------------------------------------- */
/* Copyright 2002-2010, OpenNebula Project Leads (OpenNebula.org) */
/* */
/* Licensed under the Apache License, Version 2.0 (the "License"); you may */
/* not use this file except in compliance with the License. You may obtain */
/* a copy of the License at */
/* */
/* http://www.apache.org/licenses/LICENSE-2.0 */
/* */
/* Unless required by applicable law or agreed to in writing, software */
/* distributed under the License is distributed on an "AS IS" BASIS, */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
/* See the License for the specific language governing permissions and */
/* limitations under the License. */
/* -------------------------------------------------------------------------- */
#include "VirtualMachineTemplate.h"
const char * VirtualMachineTemplate::table = "vm_attributes";
const char * VirtualMachineTemplate::db_bootstrap = "CREATE TABLE IF NOT EXISTS"
" vm_attributes (id INTEGER, name TEXT, type INTEGER, value TEXT)";

View File

@ -70,13 +70,14 @@ const string xmls[] =
// This xml dump result has the STIMEs modified to 0000000000
const string xml_dump =
"<VM_POOL><VM><ID>0</ID><UID>1</UID><USERNAME>A user</USERNAME><NAME>VM one</NAME><LAST_POLL>0</LAST_POLL><STATE>1</STATE><LCM_STATE>0</LCM_STATE><STIME>0000000000</STIME><ETIME>0</ETIME><DEPLOY_ID></DEPLOY_ID><MEMORY>0</MEMORY><CPU>0</CPU><NET_TX>0</NET_TX><NET_RX>0</NET_RX><LAST_SEQ>-1</LAST_SEQ></VM><VM><ID>1</ID><UID>2</UID><USERNAME>B user</USERNAME><NAME>Second VM</NAME><LAST_POLL>0</LAST_POLL><STATE>2</STATE><LCM_STATE>0</LCM_STATE><STIME>0000000000</STIME><ETIME>0</ETIME><DEPLOY_ID></DEPLOY_ID><MEMORY>0</MEMORY><CPU>0</CPU><NET_TX>0</NET_TX><NET_RX>0</NET_RX><LAST_SEQ>-1</LAST_SEQ></VM></VM_POOL>";
"<VM_POOL><VM><ID>0</ID><UID>1</UID><USERNAME>A user</USERNAME><NAME>VM one</NAME><LAST_POLL>0</LAST_POLL><STATE>1</STATE><LCM_STATE>0</LCM_STATE><STIME>0000000000</STIME><ETIME>0</ETIME><DEPLOY_ID></DEPLOY_ID><MEMORY>0</MEMORY><CPU>0</CPU><NET_TX>0</NET_TX><NET_RX>0</NET_RX><LAST_SEQ>-1</LAST_SEQ><TEMPLATE><CPU><![CDATA[1]]></CPU><MEMORY><![CDATA[128]]></MEMORY><NAME><![CDATA[VM one]]></NAME><VMID><![CDATA[0]]></VMID></TEMPLATE></VM><VM><ID>1</ID><UID>2</UID><USERNAME>B user</USERNAME><NAME>Second VM</NAME><LAST_POLL>0</LAST_POLL><STATE>2</STATE><LCM_STATE>0</LCM_STATE><STIME>0000000000</STIME><ETIME>0</ETIME><DEPLOY_ID></DEPLOY_ID><MEMORY>0</MEMORY><CPU>0</CPU><NET_TX>0</NET_TX><NET_RX>0</NET_RX><LAST_SEQ>-1</LAST_SEQ><TEMPLATE><CPU><![CDATA[2]]></CPU><MEMORY><![CDATA[256]]></MEMORY><NAME><![CDATA[Second VM]]></NAME><VMID><![CDATA[1]]></VMID></TEMPLATE></VM></VM_POOL>";
const string xml_dump_where =
"<VM_POOL><VM><ID>0</ID><UID>1</UID><USERNAME>A user</USERNAME><NAME>VM one</NAME><LAST_POLL>0</LAST_POLL><STATE>1</STATE><LCM_STATE>0</LCM_STATE><STIME>0000000000</STIME><ETIME>0</ETIME><DEPLOY_ID></DEPLOY_ID><MEMORY>0</MEMORY><CPU>0</CPU><NET_TX>0</NET_TX><NET_RX>0</NET_RX><LAST_SEQ>-1</LAST_SEQ></VM></VM_POOL>";
"<VM_POOL><VM><ID>0</ID><UID>1</UID><USERNAME>A user</USERNAME><NAME>VM one</NAME><LAST_POLL>0</LAST_POLL><STATE>1</STATE><LCM_STATE>0</LCM_STATE><STIME>0000000000</STIME><ETIME>0</ETIME><DEPLOY_ID></DEPLOY_ID><MEMORY>0</MEMORY><CPU>0</CPU><NET_TX>0</NET_TX><NET_RX>0</NET_RX><LAST_SEQ>-1</LAST_SEQ><TEMPLATE><CPU><![CDATA[1]]></CPU><MEMORY><![CDATA[128]]></MEMORY><NAME><![CDATA[VM one]]></NAME><VMID><![CDATA[0]]></VMID></TEMPLATE></VM></VM_POOL>";
const string xml_history_dump =
"<VM_POOL><VM><ID>0</ID><UID>0</UID><USERNAME>one_user_test</USERNAME><NAME>VM one</NAME><LAST_POLL>0</LAST_POLL><STATE>1</STATE><LCM_STATE>0</LCM_STATE><STIME>0000000000</STIME><ETIME>0</ETIME><DEPLOY_ID></DEPLOY_ID><MEMORY>0</MEMORY><CPU>0</CPU><NET_TX>0</NET_TX><NET_RX>0</NET_RX><LAST_SEQ>-1</LAST_SEQ></VM><VM><ID>1</ID><UID>0</UID><USERNAME>one_user_test</USERNAME><NAME>Second VM</NAME><LAST_POLL>0</LAST_POLL><STATE>2</STATE><LCM_STATE>0</LCM_STATE><STIME>0000000000</STIME><ETIME>0</ETIME><DEPLOY_ID></DEPLOY_ID><MEMORY>0</MEMORY><CPU>0</CPU><NET_TX>0</NET_TX><NET_RX>0</NET_RX><LAST_SEQ>0</LAST_SEQ><HISTORY><SEQ>0</SEQ><HOSTNAME>A_hostname</HOSTNAME><HID>0</HID><STIME>0</STIME><ETIME>0</ETIME><PSTIME>0</PSTIME><PETIME>0</PETIME><RSTIME>0</RSTIME><RETIME>0</RETIME><ESTIME>0</ESTIME><EETIME>0</EETIME><REASON>0</REASON></HISTORY></VM><VM><ID>2</ID><UID>0</UID><USERNAME>one_user_test</USERNAME><NAME>VM one</NAME><LAST_POLL>0</LAST_POLL><STATE>2</STATE><LCM_STATE>0</LCM_STATE><STIME>0000000000</STIME><ETIME>0</ETIME><DEPLOY_ID></DEPLOY_ID><MEMORY>0</MEMORY><CPU>0</CPU><NET_TX>0</NET_TX><NET_RX>0</NET_RX><LAST_SEQ>1</LAST_SEQ><HISTORY><SEQ>1</SEQ><HOSTNAME>C_hostname</HOSTNAME><HID>2</HID><STIME>0</STIME><ETIME>0</ETIME><PSTIME>0</PSTIME><PETIME>0</PETIME><RSTIME>0</RSTIME><RETIME>0</RETIME><ESTIME>0</ESTIME><EETIME>0</EETIME><REASON>0</REASON></HISTORY></VM></VM_POOL>";
"<VM_POOL><VM><ID>0</ID><UID>0</UID><USERNAME>one_user_test</USERNAME><NAME>VM one</NAME><LAST_POLL>0</LAST_POLL><STATE>1</STATE><LCM_STATE>0</LCM_STATE><STIME>0000000000</STIME><ETIME>0</ETIME><DEPLOY_ID></DEPLOY_ID><MEMORY>0</MEMORY><CPU>0</CPU><NET_TX>0</NET_TX><NET_RX>0</NET_RX><LAST_SEQ>-1</LAST_SEQ><TEMPLATE><CPU><![CDATA[1]]></CPU><MEMORY><![CDATA[128]]></MEMORY><NAME><![CDATA[VM one]]></NAME><VMID><![CDATA[0]]></VMID></TEMPLATE></VM><VM><ID>1</ID><UID>0</UID><USERNAME>one_user_test</USERNAME><NAME>Second VM</NAME><LAST_POLL>0</LAST_POLL><STATE>2</STATE><LCM_STATE>0</LCM_STATE><STIME>0000000000</STIME><ETIME>0</ETIME><DEPLOY_ID></DEPLOY_ID><MEMORY>0</MEMORY><CPU>0</CPU><NET_TX>0</NET_TX><NET_RX>0</NET_RX><LAST_SEQ>0</LAST_SEQ><TEMPLATE><CPU><![CDATA[2]]></CPU><MEMORY><![CDATA[256]]></MEMORY><NAME><![CDATA[Second VM]]></NAME><VMID><![CDATA[1]]></VMID></TEMPLATE><HISTORY><SEQ>0</SEQ><HOSTNAME>A_hostname</HOSTNAME><HID>0</HID><STIME>0</STIME><ETIME>0</ETIME><PSTIME>0</PSTIME><PETIME>0</PETIME><RSTIME>0</RSTIME><RETIME>0</RETIME><ESTIME>0</ESTIME><EETIME>0</EETIME><REASON>0</REASON></HISTORY></VM><VM><ID>2</ID><UID>0</UID><USERNAME>one_user_test</USERNAME><NAME>VM one</NAME><LAST_POLL>0</LAST_POLL><STATE>2</STATE><LCM_STATE>0</LCM_STATE><STIME>0000000000</STIME><ETIME>0</ETIME><DEPLOY_ID></DEPLOY_ID><MEMORY>0</MEMORY><CPU>0</CPU><NET_TX>0</NET_TX><NET_RX>0</NET_RX><LAST_SEQ>1</LAST_SEQ><TEMPLATE><CPU><![CDATA[1]]></CPU><MEMORY><![CDATA[1024]]></MEMORY><NAME><![CDATA[VM one]]></NAME><VMID><![CDATA[2]]></VMID></TEMPLATE><HISTORY><SEQ>1</SEQ><HOSTNAME>C_hostname</HOSTNAME><HID>2</HID><STIME>0</STIME><ETIME>0</ETIME><PSTIME>0</PSTIME><PETIME>0</PETIME><RSTIME>0</RSTIME><RETIME>0</RETIME><ESTIME>0</ESTIME><EETIME>0</EETIME><REASON>0</REASON></HISTORY></VM></VM_POOL>";
const string replacement = "0000000000";
@ -281,7 +282,7 @@ public:
string result = oss.str();
result.replace(152, 10, replacement);
result.replace(449, 10, replacement);
result.replace(583, 10, replacement);
CPPUNIT_ASSERT( result == xml_dump );
}
@ -403,8 +404,8 @@ public:
string result = oss.str();
result.replace(159, 10, replacement);
result.replace(463, 10, replacement);
result.replace(995, 10, replacement);
result.replace(597, 10, replacement);
result.replace(1266,10, replacement);
CPPUNIT_ASSERT( result == xml_history_dump );
}