mirror of
https://github.com/OpenNebula/one.git
synced 2025-01-10 01:17:40 +03:00
Merge branch 'feature-282' into one-2.0
Conflicts: src/template/Template.cc src/template/test/SConstruct
This commit is contained in:
commit
1aa3052dfa
@ -18,8 +18,8 @@
|
||||
#define HOST_H_
|
||||
|
||||
#include "PoolSQL.h"
|
||||
#include "HostShare.h"
|
||||
#include "HostTemplate.h"
|
||||
#include "HostShare.h"
|
||||
#include "ClusterPool.h"
|
||||
|
||||
using namespace std;
|
||||
@ -454,11 +454,9 @@ private:
|
||||
{
|
||||
ostringstream oss_host(Host::db_bootstrap);
|
||||
ostringstream oss_share(HostShare::db_bootstrap);
|
||||
ostringstream oss_templ(HostTemplate::db_bootstrap);
|
||||
|
||||
db->exec(oss_host);
|
||||
db->exec(oss_share);
|
||||
db->exec(oss_templ);
|
||||
};
|
||||
|
||||
protected:
|
||||
@ -489,7 +487,8 @@ protected:
|
||||
TM_MAD = 5,
|
||||
LAST_MON_TIME = 6,
|
||||
CLUSTER = 7,
|
||||
LIMIT = 8
|
||||
TEMPLATE = 8,
|
||||
LIMIT = 9
|
||||
};
|
||||
|
||||
static const char * db_names;
|
||||
|
@ -17,30 +17,22 @@
|
||||
#ifndef HOST_TEMPLATE_H_
|
||||
#define HOST_TEMPLATE_H_
|
||||
|
||||
#include "TemplateSQL.h"
|
||||
#include "Template.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
/**
|
||||
* Host Template class, it represents the attributes of a Host
|
||||
*/
|
||||
class HostTemplate : public TemplateSQL
|
||||
class HostTemplate : public Template
|
||||
{
|
||||
public:
|
||||
HostTemplate(int tid = -1,
|
||||
const char separator = '='):
|
||||
TemplateSQL(table,tid,true,separator,"TEMPLATE"){};
|
||||
HostTemplate() : Template(true,'=',"TEMPLATE"){};
|
||||
|
||||
~HostTemplate(){};
|
||||
|
||||
private:
|
||||
friend class Host;
|
||||
|
||||
static const char * table;
|
||||
|
||||
static const char * db_bootstrap;
|
||||
};
|
||||
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
||||
|
@ -273,9 +273,30 @@ public:
|
||||
* Removes an Image attribute
|
||||
* @param name of the attribute
|
||||
*/
|
||||
int remove_template_attribute(SqlDB * db, const string& name)
|
||||
int remove_template_attribute(const string& name)
|
||||
{
|
||||
return image_template->remove_attribute(db, name);
|
||||
return image_template->erase(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a new attribute to the template (replacing it if
|
||||
* already defined), the image's mutex SHOULD be locked
|
||||
* @param name of the new attribute
|
||||
* @param value of the new attribute
|
||||
* @return 0 on success
|
||||
*/
|
||||
int replace_template_attribute(
|
||||
const string& name,
|
||||
const string& value)
|
||||
{
|
||||
SingleAttribute * sattr;
|
||||
|
||||
image_template->erase(name);
|
||||
|
||||
sattr = new SingleAttribute(name,value);
|
||||
image_template->set(sattr);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
private:
|
||||
@ -367,10 +388,8 @@ private:
|
||||
static void bootstrap(SqlDB * db)
|
||||
{
|
||||
ostringstream oss_image(Image::db_bootstrap);
|
||||
ostringstream oss_templ(ImageTemplate::db_bootstrap);
|
||||
|
||||
db->exec(oss_image);
|
||||
db->exec(oss_templ);
|
||||
};
|
||||
|
||||
|
||||
@ -407,7 +426,8 @@ protected:
|
||||
STATE = 7, /* 0) INIT 1) ALLOCATED */
|
||||
/* 2) READY 3) USED */
|
||||
RUNNING_VMS = 8, /* Number of VMs using the img */
|
||||
LIMIT = 9
|
||||
TEMPLATE = 9, /* Image template xml data */
|
||||
LIMIT = 10
|
||||
};
|
||||
|
||||
static const char * db_names;
|
||||
|
@ -120,34 +120,6 @@ public:
|
||||
return rc;
|
||||
};
|
||||
|
||||
/** Modify an image attribute in the template (Image MUST be locked)
|
||||
* @param image pointer to Image
|
||||
* @param name of the attribute to be changed
|
||||
* @param new value for the attribute
|
||||
* @return 0 on success, -1 otherwise
|
||||
*/
|
||||
int replace_attribute(
|
||||
Image * image,
|
||||
const string& name,
|
||||
const string& value)
|
||||
{
|
||||
SingleAttribute * sattr = new SingleAttribute(name,value);
|
||||
|
||||
return image->image_template->replace_attribute(db,sattr);
|
||||
}
|
||||
|
||||
/** Delete an image attribute in the template (Image MUST be locked)
|
||||
* @param image pointer to Image
|
||||
* @param name of the attribute to be removed
|
||||
* @return 0 on success, -1 otherwise
|
||||
*/
|
||||
int remove_attribute(
|
||||
Image * image,
|
||||
const string& name)
|
||||
{
|
||||
return image->image_template->remove_attribute(db, name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Bootstraps the database table(s) associated to the Image pool
|
||||
*/
|
||||
|
@ -17,29 +17,19 @@
|
||||
#ifndef IMAGE_TEMPLATE_H_
|
||||
#define IMAGE_TEMPLATE_H_
|
||||
|
||||
#include "TemplateSQL.h"
|
||||
#include "Template.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
/**
|
||||
* Image Template class, it represents the attributes of a Host
|
||||
*/
|
||||
class ImageTemplate : public TemplateSQL
|
||||
class ImageTemplate : public Template
|
||||
{
|
||||
public:
|
||||
ImageTemplate(int tid = -1,
|
||||
const char separator = '='):
|
||||
TemplateSQL(table,tid,true,separator,"TEMPLATE"){};
|
||||
ImageTemplate() : Template(true,'=',"TEMPLATE"){};
|
||||
|
||||
~ImageTemplate(){};
|
||||
|
||||
private:
|
||||
friend class Image;
|
||||
friend class ImagePool;
|
||||
|
||||
static const char * table;
|
||||
|
||||
static const char * db_bootstrap;
|
||||
};
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
@ -1,130 +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. */
|
||||
/* ------------------------------------------------------------------------ */
|
||||
|
||||
#ifndef TEMPLATE_SQL_H_
|
||||
#define TEMPLATE_SQL_H_
|
||||
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <vector>
|
||||
|
||||
#include "Template.h"
|
||||
#include "SqlDB.h"
|
||||
#include "ObjectSQL.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
/**
|
||||
* SQL Template class, it provides DB support for template objects
|
||||
*/
|
||||
class TemplateSQL : public Template, public ObjectSQL
|
||||
{
|
||||
public:
|
||||
TemplateSQL(
|
||||
const char * _table,
|
||||
int template_id = -1,
|
||||
bool replace = false,
|
||||
const char separator = '=',
|
||||
const char * xml_root = "TEMPLATE"):
|
||||
Template(replace,separator,xml_root),table(_table),id(template_id)
|
||||
{};
|
||||
|
||||
virtual ~TemplateSQL(){};
|
||||
|
||||
protected:
|
||||
|
||||
//Database implementation variables
|
||||
|
||||
const char * table;
|
||||
|
||||
static const char * db_names;
|
||||
|
||||
//Template attributes
|
||||
|
||||
/**
|
||||
* Template unique identification.
|
||||
*/
|
||||
int id;
|
||||
|
||||
/**
|
||||
* Writes the template in the DB
|
||||
* @param db pointer to the database.
|
||||
* @return 0 on success.
|
||||
*/
|
||||
int insert(SqlDB * db);
|
||||
|
||||
/**
|
||||
* Updates the template in the DB
|
||||
* @param db pointer to the database.
|
||||
* @return 0 on success.
|
||||
*/
|
||||
int update(SqlDB *db);
|
||||
|
||||
/**
|
||||
* Reads the template (identified by its id) from the DB
|
||||
* @param db pointer to the database.
|
||||
* @return 0 on success.
|
||||
*/
|
||||
int select(SqlDB *db);
|
||||
|
||||
/**
|
||||
* Removes the template from the DB
|
||||
* @param db pointer to the database.
|
||||
*/
|
||||
int drop(SqlDB *db);
|
||||
|
||||
/**
|
||||
* Execute an INSERT or REPLACE Sql query.
|
||||
* @param db The SQL DB
|
||||
* @param replace Execute an INSERT or a REPLACE
|
||||
* @return 0 one success
|
||||
*/
|
||||
int insert_replace(SqlDB *db, bool replace);
|
||||
|
||||
/**
|
||||
* 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 attribute pointer to the new attribute.
|
||||
*/
|
||||
int replace_attribute(SqlDB * 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(SqlDB * db, Attribute * attribute);
|
||||
|
||||
/**
|
||||
* Remove a given attribute from the template and the DB.
|
||||
* @param db pointer to the database.
|
||||
* @param name name of the attribute
|
||||
*/
|
||||
int remove_attribute(SqlDB * db, const string& name);
|
||||
|
||||
/**
|
||||
* Callback to recover template attributes (TemplateSQL::select)
|
||||
*/
|
||||
int select_cb(void *nil, int num, char **values, char **names);
|
||||
};
|
||||
|
||||
/* ------------------------------------------------------------------------ */
|
||||
/* ------------------------------------------------------------------------ */
|
||||
|
||||
#endif /*TEMPLATE_SQL_H_*/
|
@ -598,6 +598,28 @@ public:
|
||||
vm_template->get(str,value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a new attribute to the template (replacing it if
|
||||
* already defined), the vm's mutex SHOULD be locked
|
||||
* @param name of the new attribute
|
||||
* @param value of the new attribute
|
||||
* @return 0 on success
|
||||
*/
|
||||
int replace_template_attribute(
|
||||
string& name,
|
||||
string& value)
|
||||
{
|
||||
SingleAttribute * sattr;
|
||||
|
||||
vm_template->erase(name);
|
||||
|
||||
sattr = new SingleAttribute(name,value);
|
||||
vm_template->set(sattr);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Generates a XML string for the template of the VM
|
||||
* @param xml the string to store the XML description.
|
||||
@ -869,11 +891,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);
|
||||
};
|
||||
|
||||
@ -925,44 +945,6 @@ private:
|
||||
return -1;
|
||||
};
|
||||
|
||||
/**
|
||||
* 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;
|
||||
|
||||
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(SqlDB * db, Attribute * attribute)
|
||||
{
|
||||
return vm_template->insert_attribute(db,attribute);
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Attribute Parser
|
||||
@ -1023,7 +1005,8 @@ protected:
|
||||
NET_TX = 11,
|
||||
NET_RX = 12,
|
||||
LAST_SEQ = 13,
|
||||
LIMIT = 14
|
||||
TEMPLATE = 14,
|
||||
LIMIT = 15
|
||||
};
|
||||
|
||||
static const char * table;
|
||||
|
@ -90,22 +90,6 @@ public:
|
||||
// Virtual Machine DB access functions
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* 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 name of the new attribute
|
||||
* @param value of the new attribute
|
||||
* @return 0 on success
|
||||
*/
|
||||
int update_template_attribute(
|
||||
VirtualMachine * vm,
|
||||
string& name,
|
||||
string& value)
|
||||
{
|
||||
return vm->update_template_attribute(db,name,value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the history record of a VM, the vm's mutex SHOULD be locked
|
||||
* @param vm pointer to the virtual machine object
|
||||
|
@ -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;
|
||||
};
|
||||
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
||||
|
@ -313,11 +313,9 @@ private:
|
||||
static void bootstrap(SqlDB * db)
|
||||
{
|
||||
ostringstream oss_vnet(VirtualNetwork::db_bootstrap);
|
||||
ostringstream oss_templ(VirtualNetworkTemplate::db_bootstrap);
|
||||
ostringstream oss_lease(Leases::db_bootstrap);
|
||||
|
||||
db->exec(oss_vnet);
|
||||
db->exec(oss_templ);
|
||||
db->exec(oss_lease);
|
||||
};
|
||||
|
||||
@ -337,34 +335,6 @@ private:
|
||||
int vn_drop(SqlDB * db);
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Updates the template of a VNW, adding a new attribute (replacing it if
|
||||
* already defined), the VN's mutex SHOULD be locked
|
||||
* @param db pointer to the DB
|
||||
* @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;
|
||||
|
||||
sattr = new SingleAttribute(name,value);
|
||||
rc = vn_template->replace_attribute(db,sattr);
|
||||
|
||||
if (rc != 0)
|
||||
{
|
||||
delete sattr;
|
||||
}
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
protected:
|
||||
|
||||
//**************************************************************************
|
||||
@ -387,7 +357,8 @@ protected:
|
||||
TYPE = 3,
|
||||
BRIDGE = 4,
|
||||
PUBLIC = 5,
|
||||
LIMIT = 6
|
||||
TEMPLATE = 6,
|
||||
LIMIT = 7
|
||||
};
|
||||
|
||||
static const char * table;
|
||||
@ -428,9 +399,7 @@ protected:
|
||||
{
|
||||
int rc;
|
||||
|
||||
rc = vn_template->drop(db);
|
||||
|
||||
rc += leases->drop(db);
|
||||
rc = leases->drop(db);
|
||||
|
||||
rc += vn_drop(db);
|
||||
|
||||
|
@ -96,22 +96,6 @@ public:
|
||||
*/
|
||||
void authorize_nic(VectorAttribute * nic, AuthRequest * ar);
|
||||
|
||||
/**
|
||||
* Updates the template of a VN, adding a new attribute (replacing it if
|
||||
* already defined), the VN's mutex SHOULD be locked
|
||||
* @param vn pointer to the virtual network object
|
||||
* @param name of the new attribute
|
||||
* @param value of the new attribute
|
||||
* @return 0 on success
|
||||
*/
|
||||
int update_template_attribute(
|
||||
VirtualNetwork * vn,
|
||||
string& name,
|
||||
string& value)
|
||||
{
|
||||
return vn->update_template_attribute(db,name,value);
|
||||
};
|
||||
|
||||
/**
|
||||
* Bootstraps the database table(s) associated to the VirtualNetwork pool
|
||||
*/
|
||||
|
@ -17,29 +17,22 @@
|
||||
#ifndef VIRTUAL_NETWORK_TEMPLATE_H_
|
||||
#define VIRTUAL_NETWORK_TEMPLATE_H_
|
||||
|
||||
#include "TemplateSQL.h"
|
||||
#include "Template.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
/**
|
||||
* Virtual Network Template class, it represents a VN configuration file.
|
||||
*/
|
||||
class VirtualNetworkTemplate : public TemplateSQL
|
||||
class VirtualNetworkTemplate : public Template
|
||||
{
|
||||
public:
|
||||
VirtualNetworkTemplate(int tid = -1):
|
||||
TemplateSQL(table,tid,false,'=',"TEMPLATE"){};
|
||||
VirtualNetworkTemplate():
|
||||
Template(false,'=',"TEMPLATE"){};
|
||||
|
||||
~VirtualNetworkTemplate(){};
|
||||
|
||||
private:
|
||||
friend class VirtualNetwork;
|
||||
|
||||
static const char * table;
|
||||
|
||||
static const char * db_bootstrap;
|
||||
};
|
||||
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
||||
|
@ -73,6 +73,9 @@ main_env.Append(CPPFLAGS=[
|
||||
main_env.Append(LIBPATH=["/usr/lib/mysql"])
|
||||
main_env.Append(CPPPATH=["/usr/include/mysql"])
|
||||
|
||||
# libxml2
|
||||
main_env.ParseConfig('xml2-config --libs --cflags')
|
||||
|
||||
sqlite=ARGUMENTS.get('sqlite', 'yes')
|
||||
if sqlite=='yes':
|
||||
main_env.Append(CPPFLAGS=["-DSQLITE_DB"])
|
||||
|
@ -41,7 +41,7 @@ Host::Host(
|
||||
tm_mad_name(_tm_mad_name),
|
||||
last_monitored(0),
|
||||
cluster(ClusterPool::DEFAULT_CLUSTER_NAME),
|
||||
host_template(id)
|
||||
host_template()
|
||||
{};
|
||||
|
||||
|
||||
@ -54,12 +54,12 @@ Host::~Host(){};
|
||||
const char * Host::table = "host_pool";
|
||||
|
||||
const char * Host::db_names = "(oid,host_name,state,im_mad,vm_mad,"
|
||||
"tm_mad,last_mon_time, cluster)";
|
||||
"tm_mad,last_mon_time, cluster, template)";
|
||||
|
||||
const char * Host::db_bootstrap = "CREATE TABLE IF NOT EXISTS host_pool ("
|
||||
"oid INTEGER PRIMARY KEY,host_name VARCHAR(512), state INTEGER,"
|
||||
"im_mad VARCHAR(128),vm_mad VARCHAR(128),tm_mad VARCHAR(128),"
|
||||
"last_mon_time INTEGER, cluster VARCHAR(128), "
|
||||
"last_mon_time INTEGER, cluster VARCHAR(128), template TEXT, "
|
||||
"UNIQUE(host_name, im_mad, vm_mad, tm_mad) )";
|
||||
|
||||
/* ------------------------------------------------------------------------ */
|
||||
@ -75,6 +75,7 @@ int Host::select_cb(void * nil, int num, char **values, char ** names)
|
||||
(!values[TM_MAD]) ||
|
||||
(!values[LAST_MON_TIME]) ||
|
||||
(!values[CLUSTER]) ||
|
||||
(!values[TEMPLATE]) ||
|
||||
(num != LIMIT ))
|
||||
{
|
||||
return -1;
|
||||
@ -92,7 +93,8 @@ int Host::select_cb(void * nil, int num, char **values, char ** names)
|
||||
|
||||
cluster = values[CLUSTER];
|
||||
|
||||
host_template.id = oid;
|
||||
host_template.from_xml(values[TEMPLATE]);
|
||||
|
||||
host_share.hsid = oid;
|
||||
|
||||
return 0;
|
||||
@ -122,9 +124,6 @@ int Host::select(SqlDB *db)
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Get the template
|
||||
rc = host_template.select(db);
|
||||
|
||||
if ( rc != 0 )
|
||||
{
|
||||
return -1;
|
||||
@ -149,32 +148,17 @@ int Host::insert(SqlDB *db)
|
||||
int rc;
|
||||
map<int,HostShare *>::iterator iter;
|
||||
|
||||
// Set up the template ID, to insert it
|
||||
if ( host_template.id == -1 )
|
||||
{
|
||||
host_template.id = oid;
|
||||
}
|
||||
|
||||
// Set up the share ID, to insert it
|
||||
if ( host_share.hsid == -1 )
|
||||
{
|
||||
host_share.hsid = oid;
|
||||
}
|
||||
|
||||
// Update the Template
|
||||
rc = host_template.insert(db);
|
||||
|
||||
if ( rc != 0 )
|
||||
{
|
||||
return rc;
|
||||
}
|
||||
|
||||
// Update the HostShare
|
||||
rc = host_share.insert(db);
|
||||
|
||||
if ( rc != 0 )
|
||||
{
|
||||
host_template.drop(db);
|
||||
return rc;
|
||||
}
|
||||
|
||||
@ -183,7 +167,6 @@ int Host::insert(SqlDB *db)
|
||||
|
||||
if ( rc != 0 )
|
||||
{
|
||||
host_template.drop(db);
|
||||
host_share.drop(db);
|
||||
|
||||
return rc;
|
||||
@ -199,14 +182,6 @@ int Host::update(SqlDB *db)
|
||||
{
|
||||
int rc;
|
||||
|
||||
// Update the Template needed by the monitoring action from IM
|
||||
rc = host_template.update(db);
|
||||
|
||||
if ( rc != 0 )
|
||||
{
|
||||
return rc;
|
||||
}
|
||||
|
||||
// Update the HostShare
|
||||
rc = host_share.update(db);
|
||||
|
||||
@ -235,12 +210,14 @@ int Host::insert_replace(SqlDB *db, bool replace)
|
||||
ostringstream oss;
|
||||
|
||||
int rc;
|
||||
string xml_template;
|
||||
|
||||
char * sql_hostname;
|
||||
char * sql_im_mad_name;
|
||||
char * sql_tm_mad_name;
|
||||
char * sql_vmm_mad_name;
|
||||
char * sql_cluster;
|
||||
char * sql_template;
|
||||
|
||||
// Update the Host
|
||||
|
||||
@ -279,6 +256,14 @@ int Host::insert_replace(SqlDB *db, bool replace)
|
||||
goto error_cluster;
|
||||
}
|
||||
|
||||
host_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";
|
||||
@ -291,14 +276,15 @@ int Host::insert_replace(SqlDB *db, bool replace)
|
||||
// Construct the SQL statement to Insert or Replace
|
||||
|
||||
oss <<" INTO "<< table <<" "<< db_names <<" VALUES ("
|
||||
<< oid << ","
|
||||
<< "'" << sql_hostname << "',"
|
||||
<< state << ","
|
||||
<< "'" << sql_im_mad_name << "',"
|
||||
<< "'" << sql_vmm_mad_name << "',"
|
||||
<< "'" << sql_tm_mad_name << "',"
|
||||
<< last_monitored << ","
|
||||
<< "'" << sql_cluster << "')";
|
||||
<< oid << ","
|
||||
<< "'" << sql_hostname << "',"
|
||||
<< state << ","
|
||||
<< "'" << sql_im_mad_name << "',"
|
||||
<< "'" << sql_vmm_mad_name << "',"
|
||||
<< "'" << sql_tm_mad_name << "',"
|
||||
<< last_monitored << ","
|
||||
<< "'" << sql_cluster << "',"
|
||||
<< "'" << sql_template << "')";
|
||||
|
||||
rc = db->exec(oss);
|
||||
|
||||
@ -307,9 +293,12 @@ int Host::insert_replace(SqlDB *db, bool replace)
|
||||
db->free_str(sql_tm_mad_name);
|
||||
db->free_str(sql_vmm_mad_name);
|
||||
db->free_str(sql_cluster);
|
||||
db->free_str(sql_template);
|
||||
|
||||
return rc;
|
||||
|
||||
error_template:
|
||||
db->free_str(sql_cluster);
|
||||
error_cluster:
|
||||
db->free_str(sql_vmm_mad_name);
|
||||
error_vmm:
|
||||
@ -335,6 +324,7 @@ int Host::dump(ostringstream& oss, int num, char **values, char **names)
|
||||
(!values[TM_MAD]) ||
|
||||
(!values[LAST_MON_TIME]) ||
|
||||
(!values[CLUSTER]) ||
|
||||
(!values[TEMPLATE]) ||
|
||||
(num != LIMIT + HostShare::LIMIT ))
|
||||
{
|
||||
return -1;
|
||||
@ -349,7 +339,8 @@ int Host::dump(ostringstream& oss, int num, char **values, char **names)
|
||||
"<VM_MAD>" << values[VM_MAD] <<"</VM_MAD>" <<
|
||||
"<TM_MAD>" << values[TM_MAD] <<"</TM_MAD>" <<
|
||||
"<LAST_MON_TIME>"<< values[LAST_MON_TIME]<<"</LAST_MON_TIME>"<<
|
||||
"<CLUSTER>" << values[CLUSTER] <<"</CLUSTER>";
|
||||
"<CLUSTER>" << values[CLUSTER] <<"</CLUSTER>" <<
|
||||
values[TEMPLATE];
|
||||
|
||||
HostShare::dump(oss,num - LIMIT, values + LIMIT, names + LIMIT);
|
||||
|
||||
@ -366,8 +357,6 @@ int Host::drop(SqlDB * db)
|
||||
ostringstream oss;
|
||||
int rc;
|
||||
|
||||
host_template.drop(db);
|
||||
|
||||
host_share.drop(db);
|
||||
|
||||
oss << "DELETE FROM " << table << " WHERE oid=" << oid;
|
||||
|
@ -1,24 +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 "HostTemplate.h"
|
||||
|
||||
const char * HostTemplate::table = "host_attributes";
|
||||
|
||||
const char * HostTemplate::db_bootstrap = "CREATE TABLE IF NOT EXISTS "
|
||||
"host_attributes (id INTEGER, name VARCHAR(256), type INTEGER, value TEXT, "
|
||||
"PRIMARY KEY(id,name))";
|
||||
|
@ -25,7 +25,6 @@ source_files=[
|
||||
'Host.cc',
|
||||
'HostShare.cc',
|
||||
'HostPool.cc',
|
||||
'HostTemplate.cc',
|
||||
'ClusterPool.cc',
|
||||
]
|
||||
|
||||
|
@ -54,34 +54,34 @@ const string xmls[] =
|
||||
const string xml_dump =
|
||||
"<HOST_POOL><HOST><ID>0</ID><NAME>a</NAME><STATE>0</STATE><IM_MAD>im_mad</I"
|
||||
"M_MAD><VM_MAD>vmm_mad</VM_MAD><TM_MAD>tm_mad</TM_MAD><LAST_MON_TIME>0"
|
||||
"</LAST_MON_TIME><CLUSTER>default</CLUSTER><HOST_SHARE><HID>0</HID><DISK_USAGE>0</DISK_USAGE><MEM"
|
||||
"</LAST_MON_TIME><CLUSTER>default</CLUSTER><TEMPLATE></TEMPLATE><HOST_SHARE><HID>0</HID><DISK_USAGE>0</DISK_USAGE><MEM"
|
||||
"_USAGE>0</MEM_USAGE><CPU_USAGE>0</CPU_USAGE><MAX_DISK>0</MAX_DISK><MAX_MEM"
|
||||
">0</MAX_MEM><MAX_CPU>0</MAX_CPU><FREE_DISK>0</FREE_DISK><FREE_MEM>0</FREE_"
|
||||
"MEM><FREE_CPU>0</FREE_CPU><USED_DISK>0</USED_DISK><USED_MEM>0</USED_MEM><U"
|
||||
"SED_CPU>0</USED_CPU><RUNNING_VMS>0</RUNNING_VMS></HOST_SHARE></HOST><HOST>"
|
||||
"<ID>1</ID><NAME>a name</NAME><STATE>0</STATE><IM_MAD>im_mad</IM_MAD><VM_MA"
|
||||
"D>vmm_mad</VM_MAD><TM_MAD>tm_mad</TM_MAD><LAST_MON_TIME>0</LAST_M"
|
||||
"ON_TIME><CLUSTER>default</CLUSTER><HOST_SHARE><HID>1</HID><DISK_USAGE>0</DISK_USAGE><MEM_USAGE>0</ME"
|
||||
"ON_TIME><CLUSTER>default</CLUSTER><TEMPLATE></TEMPLATE><HOST_SHARE><HID>1</HID><DISK_USAGE>0</DISK_USAGE><MEM_USAGE>0</ME"
|
||||
"M_USAGE><CPU_USAGE>0</CPU_USAGE><MAX_DISK>0</MAX_DISK><MAX_MEM>0</MAX_MEM>"
|
||||
"<MAX_CPU>0</MAX_CPU><FREE_DISK>0</FREE_DISK><FREE_MEM>0</FREE_MEM><FREE_CP"
|
||||
"U>0</FREE_CPU><USED_DISK>0</USED_DISK><USED_MEM>0</USED_MEM><USED_CPU>0</U"
|
||||
"SED_CPU><RUNNING_VMS>0</RUNNING_VMS></HOST_SHARE></HOST><HOST><ID>2</ID><N"
|
||||
"AME>a_name</NAME><STATE>0</STATE><IM_MAD>im_mad</IM_MAD><VM_MAD>vmm_mad</V"
|
||||
"M_MAD><TM_MAD>tm_mad</TM_MAD><LAST_MON_TIME>0</LAST_MON_TIME><CLUSTER>default</CLUSTER><HOS"
|
||||
"M_MAD><TM_MAD>tm_mad</TM_MAD><LAST_MON_TIME>0</LAST_MON_TIME><CLUSTER>default</CLUSTER><TEMPLATE></TEMPLATE><HOS"
|
||||
"T_SHARE><HID>2</HID><DISK_USAGE>0</DISK_USAGE><MEM_USAGE>0</MEM_USAGE><CPU"
|
||||
"_USAGE>0</CPU_USAGE><MAX_DISK>0</MAX_DISK><MAX_MEM>0</MAX_MEM><MAX_CPU>0</"
|
||||
"MAX_CPU><FREE_DISK>0</FREE_DISK><FREE_MEM>0</FREE_MEM><FREE_CPU>0</FREE_CP"
|
||||
"U><USED_DISK>0</USED_DISK><USED_MEM>0</USED_MEM><USED_CPU>0</USED_CPU><RUN"
|
||||
"NING_VMS>0</RUNNING_VMS></HOST_SHARE></HOST><HOST><ID>3</ID><NAME>another "
|
||||
"name</NAME><STATE>0</STATE><IM_MAD>im_mad</IM_MAD><VM_MAD>vmm_mad</VM_MAD>"
|
||||
"<TM_MAD>tm_mad</TM_MAD><LAST_MON_TIME>0</LAST_MON_TIME><CLUSTER>default</CLUSTER><HOST_SHAR"
|
||||
"<TM_MAD>tm_mad</TM_MAD><LAST_MON_TIME>0</LAST_MON_TIME><CLUSTER>default</CLUSTER><TEMPLATE></TEMPLATE><HOST_SHAR"
|
||||
"E><HID>3</HID><DISK_USAGE>0</DISK_USAGE><MEM_USAGE>0</MEM_USAGE><CPU_USAGE"
|
||||
">0</CPU_USAGE><MAX_DISK>0</MAX_DISK><MAX_MEM>0</MAX_MEM><MAX_CPU>0</MAX_CP"
|
||||
"U><FREE_DISK>0</FREE_DISK><FREE_MEM>0</FREE_MEM><FREE_CPU>0</FREE_CPU><USE"
|
||||
"D_DISK>0</USED_DISK><USED_MEM>0</USED_MEM><USED_CPU>0</USED_CPU><RUNNING_V"
|
||||
"MS>0</RUNNING_VMS></HOST_SHARE></HOST><HOST><ID>4</ID><NAME>host</NAME><ST"
|
||||
"ATE>0</STATE><IM_MAD>im_mad</IM_MAD><VM_MAD>vmm_mad</VM_MAD><TM_MAD>tm_mad"
|
||||
"</TM_MAD><LAST_MON_TIME>0</LAST_MON_TIME><CLUSTER>default</CLUSTER><HOST_SHARE><HID>4</HID>"
|
||||
"</TM_MAD><LAST_MON_TIME>0</LAST_MON_TIME><CLUSTER>default</CLUSTER><TEMPLATE></TEMPLATE><HOST_SHARE><HID>4</HID>"
|
||||
"<DISK_USAGE>0</DISK_USAGE><MEM_USAGE>0</MEM_USAGE><CPU_USAGE>0</CPU_USAGE>"
|
||||
"<MAX_DISK>0</MAX_DISK><MAX_MEM>0</MAX_MEM><MAX_CPU>0</MAX_CPU><FREE_DISK>0"
|
||||
"</FREE_DISK><FREE_MEM>0</FREE_MEM><FREE_CPU>0</FREE_CPU><USED_DISK>0</USED"
|
||||
@ -91,33 +91,35 @@ const string xml_dump =
|
||||
const string xml_dump_like_a =
|
||||
"<HOST_POOL><HOST><ID>0</ID><NAME>a</NAME><STATE>0</STATE><IM_MAD>im_mad</I"
|
||||
"M_MAD><VM_MAD>vmm_mad</VM_MAD><TM_MAD>tm_mad</TM_MAD><LAST_MON_TIME>0"
|
||||
"</LAST_MON_TIME><CLUSTER>default</CLUSTER><HOST_SHARE><HID>0</HID><DISK_USAGE>0</DISK_USAGE><MEM"
|
||||
"</LAST_MON_TIME><CLUSTER>default</CLUSTER><TEMPLATE></TEMPLATE><HOST_SHARE><HID>0</HID><DISK_USAGE>0</DISK_USAGE><MEM"
|
||||
"_USAGE>0</MEM_USAGE><CPU_USAGE>0</CPU_USAGE><MAX_DISK>0</MAX_DISK><MAX_MEM"
|
||||
">0</MAX_MEM><MAX_CPU>0</MAX_CPU><FREE_DISK>0</FREE_DISK><FREE_MEM>0</FREE_"
|
||||
"MEM><FREE_CPU>0</FREE_CPU><USED_DISK>0</USED_DISK><USED_MEM>0</USED_MEM><U"
|
||||
"SED_CPU>0</USED_CPU><RUNNING_VMS>0</RUNNING_VMS></HOST_SHARE></HOST><HOST>"
|
||||
"<ID>1</ID><NAME>a name</NAME><STATE>0</STATE><IM_MAD>im_mad</IM_MAD><VM_MA"
|
||||
"D>vmm_mad</VM_MAD><TM_MAD>tm_mad</TM_MAD><LAST_MON_TIME>0</LAST_M"
|
||||
"ON_TIME><CLUSTER>default</CLUSTER><HOST_SHARE><HID>1</HID><DISK_USAGE>0</DISK_USAGE><MEM_USAGE>0</ME"
|
||||
"ON_TIME><CLUSTER>default</CLUSTER><TEMPLATE></TEMPLATE><HOST_SHARE><HID>1</HID><DISK_USAGE>0</DISK_USAGE><MEM_USAGE>0</ME"
|
||||
"M_USAGE><CPU_USAGE>0</CPU_USAGE><MAX_DISK>0</MAX_DISK><MAX_MEM>0</MAX_MEM>"
|
||||
"<MAX_CPU>0</MAX_CPU><FREE_DISK>0</FREE_DISK><FREE_MEM>0</FREE_MEM><FREE_CP"
|
||||
"U>0</FREE_CPU><USED_DISK>0</USED_DISK><USED_MEM>0</USED_MEM><USED_CPU>0</U"
|
||||
"SED_CPU><RUNNING_VMS>0</RUNNING_VMS></HOST_SHARE></HOST><HOST><ID>2</ID><N"
|
||||
"AME>a_name</NAME><STATE>0</STATE><IM_MAD>im_mad</IM_MAD><VM_MAD>vmm_mad</V"
|
||||
"M_MAD><TM_MAD>tm_mad</TM_MAD><LAST_MON_TIME>0</LAST_MON_TIME><CLUSTER>default</CLUSTER><HOS"
|
||||
"M_MAD><TM_MAD>tm_mad</TM_MAD><LAST_MON_TIME>0</LAST_MON_TIME><CLUSTER>default</CLUSTER><TEMPLATE></TEMPLATE><HOS"
|
||||
"T_SHARE><HID>2</HID><DISK_USAGE>0</DISK_USAGE><MEM_USAGE>0</MEM_USAGE><CPU"
|
||||
"_USAGE>0</CPU_USAGE><MAX_DISK>0</MAX_DISK><MAX_MEM>0</MAX_MEM><MAX_CPU>0</"
|
||||
"MAX_CPU><FREE_DISK>0</FREE_DISK><FREE_MEM>0</FREE_MEM><FREE_CPU>0</FREE_CP"
|
||||
"U><USED_DISK>0</USED_DISK><USED_MEM>0</USED_MEM><USED_CPU>0</USED_CPU><RUN"
|
||||
"NING_VMS>0</RUNNING_VMS></HOST_SHARE></HOST><HOST><ID>3</ID><NAME>another "
|
||||
"name</NAME><STATE>0</STATE><IM_MAD>im_mad</IM_MAD><VM_MAD>vmm_mad</VM_MAD>"
|
||||
"<TM_MAD>tm_mad</TM_MAD><LAST_MON_TIME>0</LAST_MON_TIME><CLUSTER>default</CLUSTER><HOST_SHAR"
|
||||
"<TM_MAD>tm_mad</TM_MAD><LAST_MON_TIME>0</LAST_MON_TIME><CLUSTER>default</CLUSTER><TEMPLATE></TEMPLATE><HOST_SHAR"
|
||||
"E><HID>3</HID><DISK_USAGE>0</DISK_USAGE><MEM_USAGE>0</MEM_USAGE><CPU_USAGE"
|
||||
">0</CPU_USAGE><MAX_DISK>0</MAX_DISK><MAX_MEM>0</MAX_MEM><MAX_CPU>0</MAX_CP"
|
||||
"U><FREE_DISK>0</FREE_DISK><FREE_MEM>0</FREE_MEM><FREE_CPU>0</FREE_CPU><USE"
|
||||
"D_DISK>0</USED_DISK><USED_MEM>0</USED_MEM><USED_CPU>0</USED_CPU><RUNNING_V"
|
||||
"MS>0</RUNNING_VMS></HOST_SHARE></HOST></HOST_POOL>";
|
||||
|
||||
const string host0_updated =
|
||||
"<HOST><ID>0</ID><NAME>Host one</NAME><STATE>0</STATE><IM_MAD>im_mad</IM_MAD><VM_MAD>vmm_mad</VM_MAD><TM_MAD>tm_mad</TM_MAD><LAST_MON_TIME>0</LAST_MON_TIME><CLUSTER>default</CLUSTER><HOST_SHARE><HID>0</HID><DISK_USAGE>0</DISK_USAGE><MEM_USAGE>0</MEM_USAGE><CPU_USAGE>0</CPU_USAGE><MAX_DISK>0</MAX_DISK><MAX_MEM>0</MAX_MEM><MAX_CPU>0</MAX_CPU><FREE_DISK>0</FREE_DISK><FREE_MEM>0</FREE_MEM><FREE_CPU>0</FREE_CPU><USED_DISK>0</USED_DISK><USED_MEM>0</USED_MEM><USED_CPU>0</USED_CPU><RUNNING_VMS>0</RUNNING_VMS></HOST_SHARE><TEMPLATE><ATT_A><![CDATA[VALUE_A]]></ATT_A><ATT_B><![CDATA[VALUE_B]]></ATT_B></TEMPLATE></HOST>";
|
||||
|
||||
const string cluster_default =
|
||||
"<CLUSTER><ID>0</ID><NAME>default</NAME></CLUSTER>";
|
||||
@ -149,6 +151,7 @@ class HostPoolTest : public PoolTest
|
||||
CPPUNIT_TEST (cluster_dump);
|
||||
CPPUNIT_TEST (set_cluster);
|
||||
CPPUNIT_TEST (remove_cluster);
|
||||
CPPUNIT_TEST (update_info);
|
||||
|
||||
CPPUNIT_TEST_SUITE_END ();
|
||||
|
||||
@ -486,8 +489,10 @@ public:
|
||||
|
||||
// Allocate a host
|
||||
oid = allocate(0);
|
||||
CPPUNIT_ASSERT(oid >= 0);
|
||||
|
||||
host = hp->get(0, false);
|
||||
CPPUNIT_ASSERT(host != 0);
|
||||
|
||||
rc = hp->allocate_cluster(&clid, "cluster_a");
|
||||
CPPUNIT_ASSERT( rc == 1 );
|
||||
@ -518,8 +523,10 @@ public:
|
||||
|
||||
// Allocate a host
|
||||
oid = allocate(0);
|
||||
CPPUNIT_ASSERT(oid >= 0);
|
||||
|
||||
host = hp->get(0, false);
|
||||
CPPUNIT_ASSERT(host != 0);
|
||||
|
||||
rc = hp->allocate_cluster(&clid, "cluster_a");
|
||||
CPPUNIT_ASSERT( rc == 1 );
|
||||
@ -540,6 +547,40 @@ public:
|
||||
host->to_xml(xml_str);
|
||||
check(0, host);
|
||||
}
|
||||
|
||||
/* ********************************************************************* */
|
||||
|
||||
void update_info()
|
||||
{
|
||||
int rc;
|
||||
int oid_1;
|
||||
HostPool * hp = static_cast<HostPool *>(pool);
|
||||
Host* host;
|
||||
string str;
|
||||
|
||||
oid_1 = allocate(0);
|
||||
|
||||
host = hp->get(oid_1, false);
|
||||
CPPUNIT_ASSERT( host != 0 );
|
||||
|
||||
string info = "ATT_A=VALUE_A ATT_B=VALUE_B";
|
||||
rc = host->update_info(info);
|
||||
|
||||
CPPUNIT_ASSERT(rc == 0);
|
||||
|
||||
pool->update(host);
|
||||
|
||||
host = hp->get(oid_1,false);
|
||||
CPPUNIT_ASSERT( host != 0 );
|
||||
CPPUNIT_ASSERT( host->to_xml(str) == host0_updated );
|
||||
|
||||
//Now force access to DB
|
||||
pool->clean();
|
||||
host = hp->get(oid_1,false);
|
||||
|
||||
CPPUNIT_ASSERT( host != 0 );
|
||||
CPPUNIT_ASSERT( host->to_xml(str) == host0_updated );
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
@ -67,6 +67,10 @@ main_env.Append(LIBPATH=["/usr/lib/mysql"])
|
||||
main_env.Append(CPPPATH=["/usr/include/mysql"])
|
||||
|
||||
|
||||
# libxml2
|
||||
main_env.ParseConfig('xml2-config --libs --cflags')
|
||||
|
||||
|
||||
sqlite=ARGUMENTS.get('sqlite', 'yes')
|
||||
if sqlite=='yes':
|
||||
main_env.Append(CPPFLAGS=["-DSQLITE_DB"])
|
||||
|
@ -67,27 +67,28 @@ Image::~Image()
|
||||
const char * Image::table = "image_pool";
|
||||
|
||||
const char * Image::db_names = "(oid, uid, name, type, public, regtime, "
|
||||
"source, state, running_vms)";
|
||||
"source, state, running_vms, template)";
|
||||
|
||||
const char * Image::db_bootstrap = "CREATE TABLE IF NOT EXISTS image_pool ("
|
||||
"oid INTEGER PRIMARY KEY, uid INTEGER, name VARCHAR(128), "
|
||||
"type INTEGER, public INTEGER, regtime INTEGER, source TEXT, state INTEGER, "
|
||||
"running_vms INTEGER, UNIQUE(name) )";
|
||||
"running_vms INTEGER, template TEXT, UNIQUE(name) )";
|
||||
|
||||
/* ------------------------------------------------------------------------ */
|
||||
/* ------------------------------------------------------------------------ */
|
||||
|
||||
int Image::select_cb(void * nil, int num, char **values, char ** names)
|
||||
{
|
||||
if ((!values[OID]) ||
|
||||
(!values[UID]) ||
|
||||
(!values[NAME]) ||
|
||||
(!values[TYPE]) ||
|
||||
(!values[PUBLIC]) ||
|
||||
(!values[REGTIME]) ||
|
||||
(!values[SOURCE]) ||
|
||||
(!values[STATE]) ||
|
||||
(!values[RUNNING_VMS]) ||
|
||||
if ((!values[OID]) ||
|
||||
(!values[UID]) ||
|
||||
(!values[NAME]) ||
|
||||
(!values[TYPE]) ||
|
||||
(!values[PUBLIC]) ||
|
||||
(!values[REGTIME]) ||
|
||||
(!values[SOURCE]) ||
|
||||
(!values[STATE]) ||
|
||||
(!values[RUNNING_VMS]) ||
|
||||
(!values[TEMPLATE]) ||
|
||||
(num != LIMIT ))
|
||||
{
|
||||
return -1;
|
||||
@ -108,7 +109,7 @@ int Image::select_cb(void * nil, int num, char **values, char ** names)
|
||||
|
||||
running_vms = atoi(values[RUNNING_VMS]);
|
||||
|
||||
image_template->id = oid;
|
||||
image_template->from_xml(values[TEMPLATE]);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@ -136,15 +137,6 @@ int Image::select(SqlDB *db)
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Get the template
|
||||
|
||||
rc = image_template->select(db);
|
||||
|
||||
if ( rc != 0 )
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -225,36 +217,16 @@ int Image::insert(SqlDB *db)
|
||||
|
||||
source = tmp_sourcestream.str();
|
||||
|
||||
// ------------ INSERT THE TEMPLATE --------------------
|
||||
|
||||
if ( image_template->id == -1 )
|
||||
{
|
||||
image_template->id = oid;
|
||||
}
|
||||
|
||||
state = DISABLED;
|
||||
|
||||
rc = image_template->insert(db);
|
||||
|
||||
if ( rc != 0 )
|
||||
{
|
||||
return rc;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
// Insert the Image
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
rc = insert_replace(db, false);
|
||||
|
||||
if ( rc != 0 )
|
||||
{
|
||||
image_template->drop(db);
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
return 0;
|
||||
return rc;
|
||||
|
||||
error_name:
|
||||
NebulaLog::log("IMG", Log::ERROR, "NAME not present in image template");
|
||||
@ -285,10 +257,13 @@ int Image::insert_replace(SqlDB *db, bool replace)
|
||||
|
||||
int rc;
|
||||
|
||||
string xml_template;
|
||||
|
||||
char * sql_name;
|
||||
char * sql_source;
|
||||
char * sql_template;
|
||||
|
||||
// Update the Image
|
||||
// Update the Image
|
||||
|
||||
sql_name = db->escape_str(name.c_str());
|
||||
|
||||
@ -304,6 +279,14 @@ int Image::insert_replace(SqlDB *db, bool replace)
|
||||
goto error_source;
|
||||
}
|
||||
|
||||
image_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";
|
||||
@ -324,15 +307,19 @@ int Image::insert_replace(SqlDB *db, bool replace)
|
||||
<< regtime << ","
|
||||
<< "'" << sql_source << "',"
|
||||
<< state << ","
|
||||
<< running_vms << ")";
|
||||
<< running_vms << ","
|
||||
<< "'" << sql_template << "')";
|
||||
|
||||
rc = db->exec(oss);
|
||||
|
||||
db->free_str(sql_name);
|
||||
db->free_str(sql_source);
|
||||
db->free_str(sql_template);
|
||||
|
||||
return rc;
|
||||
|
||||
error_template:
|
||||
db->free_str(sql_source);
|
||||
error_source:
|
||||
db->free_str(sql_name);
|
||||
error_name:
|
||||
@ -344,15 +331,16 @@ error_name:
|
||||
|
||||
int Image::dump(ostringstream& oss, int num, char **values, char **names)
|
||||
{
|
||||
if ((!values[OID]) ||
|
||||
(!values[UID]) ||
|
||||
(!values[NAME]) ||
|
||||
(!values[TYPE]) ||
|
||||
(!values[PUBLIC]) ||
|
||||
(!values[REGTIME]) ||
|
||||
(!values[SOURCE]) ||
|
||||
(!values[STATE]) ||
|
||||
(!values[RUNNING_VMS]) ||
|
||||
if ((!values[OID]) ||
|
||||
(!values[UID]) ||
|
||||
(!values[NAME]) ||
|
||||
(!values[TYPE]) ||
|
||||
(!values[PUBLIC]) ||
|
||||
(!values[REGTIME]) ||
|
||||
(!values[SOURCE]) ||
|
||||
(!values[STATE]) ||
|
||||
(!values[RUNNING_VMS]) ||
|
||||
(!values[TEMPLATE]) ||
|
||||
(num != LIMIT + 1))
|
||||
{
|
||||
return -1;
|
||||
@ -370,6 +358,7 @@ int Image::dump(ostringstream& oss, int num, char **values, char **names)
|
||||
"<SOURCE>" << values[SOURCE] << "</SOURCE>" <<
|
||||
"<STATE>" << values[STATE] << "</STATE>" <<
|
||||
"<RUNNING_VMS>" << values[RUNNING_VMS] << "</RUNNING_VMS>" <<
|
||||
values[TEMPLATE] <<
|
||||
"</IMAGE>";
|
||||
|
||||
return 0;
|
||||
@ -389,8 +378,6 @@ int Image::drop(SqlDB * db)
|
||||
return -1;
|
||||
}
|
||||
|
||||
image_template->drop(db);
|
||||
|
||||
oss << "DELETE FROM " << table << " WHERE oid=" << oid;
|
||||
|
||||
rc = db->exec(oss);
|
||||
|
@ -1,22 +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 "ImageTemplate.h"
|
||||
|
||||
const char * ImageTemplate::table = "image_attributes";
|
||||
|
||||
const char * ImageTemplate::db_bootstrap = "CREATE TABLE IF NOT EXISTS"
|
||||
" image_attributes (id INTEGER, name TEXT, type INTEGER, value TEXT)";
|
@ -22,7 +22,6 @@ lib_name='nebula_image'
|
||||
|
||||
# Sources to generate the library
|
||||
source_files=[
|
||||
'ImageTemplate.cc',
|
||||
'Image.cc',
|
||||
'ImagePool.cc'
|
||||
]
|
||||
|
@ -48,20 +48,20 @@ const string templates[] =
|
||||
|
||||
const string xmls[] =
|
||||
{
|
||||
"<IMAGE><ID>0</ID><UID>0</UID><NAME>Image one</NAME><TYPE>0</TYPE><PUBLIC>0</PUBLIC><REGTIME>0000000000</REGTIME><SOURCE>source_prefix/9ab4a4e021ee2883f57e3aeecc9e2aed7c3fa198</SOURCE><STATE>4</STATE><RUNNING_VMS>0</RUNNING_VMS><TEMPLATE><DESCRIPTION><![CDATA[This is a very long description of an image, and to achieve the longness I will copy this over. This is a very long description of an image, and to achieve the longness I will copy this over. And over. This is a very long description of an image, and to achieve the longness I will copy this over. And over. This is a very long description of an image, and to achieve the longness I will copy this over. And over.This is a very long description of an image, and to achieve the longness I will copy this over.]]></DESCRIPTION><DEV_PREFIX><![CDATA[hd]]></DEV_PREFIX><NAME><![CDATA[Image one]]></NAME><ORIGINAL_PATH><![CDATA[/tmp/image_test]]></ORIGINAL_PATH></TEMPLATE></IMAGE>",
|
||||
"<IMAGE><ID>0</ID><UID>0</UID><NAME>Image one</NAME><TYPE>0</TYPE><PUBLIC>0</PUBLIC><REGTIME>0000000000</REGTIME><SOURCE>source_prefix/9ab4a4e021ee2883f57e3aeecc9e2aed7c3fa198</SOURCE><STATE>3</STATE><RUNNING_VMS>0</RUNNING_VMS><TEMPLATE><DESCRIPTION><![CDATA[This is a very long description of an image, and to achieve the longness I will copy this over. This is a very long description of an image, and to achieve the longness I will copy this over. And over. This is a very long description of an image, and to achieve the longness I will copy this over. And over. This is a very long description of an image, and to achieve the longness I will copy this over. And over.This is a very long description of an image, and to achieve the longness I will copy this over.]]></DESCRIPTION><DEV_PREFIX><![CDATA[hd]]></DEV_PREFIX><NAME><![CDATA[Image one]]></NAME><ORIGINAL_PATH><![CDATA[/tmp/image_test]]></ORIGINAL_PATH></TEMPLATE></IMAGE>",
|
||||
|
||||
"<IMAGE><ID>1</ID><UID>1</UID><NAME>Second Image</NAME><TYPE>0</TYPE><PUBLIC>1</PUBLIC><REGTIME>0000000000</REGTIME><SOURCE>source_prefix/c9d51800847467911c755e5e4c13dfe28c3a79f3</SOURCE><STATE>4</STATE><RUNNING_VMS>0</RUNNING_VMS><TEMPLATE><DESCRIPTION><![CDATA[This is a rather short description.]]></DESCRIPTION><DEV_PREFIX><![CDATA[hd]]></DEV_PREFIX><NAME><![CDATA[Second Image]]></NAME><ORIGINAL_PATH><![CDATA[/tmp/image_second_test]]></ORIGINAL_PATH></TEMPLATE></IMAGE>",
|
||||
"<IMAGE><ID>1</ID><UID>1</UID><NAME>Second Image</NAME><TYPE>0</TYPE><PUBLIC>1</PUBLIC><REGTIME>0000000000</REGTIME><SOURCE>source_prefix/c9d51800847467911c755e5e4c13dfe28c3a79f3</SOURCE><STATE>3</STATE><RUNNING_VMS>0</RUNNING_VMS><TEMPLATE><DESCRIPTION><![CDATA[This is a rather short description.]]></DESCRIPTION><DEV_PREFIX><![CDATA[hd]]></DEV_PREFIX><NAME><![CDATA[Second Image]]></NAME><ORIGINAL_PATH><![CDATA[/tmp/image_second_test]]></ORIGINAL_PATH></TEMPLATE></IMAGE>",
|
||||
|
||||
"<IMAGE><ID>0</ID><UID>2</UID><NAME>The third image</NAME><TYPE>0</TYPE><PUBLIC>0</PUBLIC><REGTIME>0000000000</REGTIME><SOURCE>source_prefix/e50b0c738be9d431475bf5859629e5580301a7d6</SOURCE><STATE>4</STATE><RUNNING_VMS>0</RUNNING_VMS><TEMPLATE><BUS><![CDATA[SCSI]]></BUS><DEV_PREFIX><![CDATA[hd]]></DEV_PREFIX><NAME><![CDATA[The third image]]></NAME><ORIGINAL_PATH><![CDATA[/tmp/image_test]]></ORIGINAL_PATH><PROFILE><![CDATA[STUDENT]]></PROFILE></TEMPLATE></IMAGE>"
|
||||
"<IMAGE><ID>0</ID><UID>2</UID><NAME>The third image</NAME><TYPE>0</TYPE><PUBLIC>0</PUBLIC><REGTIME>0000000000</REGTIME><SOURCE>source_prefix/e50b0c738be9d431475bf5859629e5580301a7d6</SOURCE><STATE>3</STATE><RUNNING_VMS>0</RUNNING_VMS><TEMPLATE><BUS><![CDATA[SCSI]]></BUS><DEV_PREFIX><![CDATA[hd]]></DEV_PREFIX><NAME><![CDATA[The third image]]></NAME><ORIGINAL_PATH><![CDATA[/tmp/image_test]]></ORIGINAL_PATH><PROFILE><![CDATA[STUDENT]]></PROFILE></TEMPLATE></IMAGE>"
|
||||
};
|
||||
|
||||
|
||||
// This xml dump result has the STIMEs modified to 0000000000
|
||||
const string xml_dump =
|
||||
"<IMAGE_POOL><IMAGE><ID>0</ID><UID>0</UID><USERNAME>one_user_test</USERNAME><NAME>Image one</NAME><TYPE>0</TYPE><PUBLIC>0</PUBLIC><REGTIME>0000000000</REGTIME><SOURCE>source_prefix/9ab4a4e021ee2883f57e3aeecc9e2aed7c3fa198</SOURCE><STATE>4</STATE><RUNNING_VMS>0</RUNNING_VMS></IMAGE><IMAGE><ID>1</ID><UID>1</UID><USERNAME>A user</USERNAME><NAME>Second Image</NAME><TYPE>0</TYPE><PUBLIC>1</PUBLIC><REGTIME>0000000000</REGTIME><SOURCE>source_prefix/c9d51800847467911c755e5e4c13dfe28c3a79f3</SOURCE><STATE>4</STATE><RUNNING_VMS>0</RUNNING_VMS></IMAGE><IMAGE><ID>2</ID><UID>2</UID><USERNAME>B user</USERNAME><NAME>The third image</NAME><TYPE>0</TYPE><PUBLIC>0</PUBLIC><REGTIME>0000000000</REGTIME><SOURCE>source_prefix/e50b0c738be9d431475bf5859629e5580301a7d6</SOURCE><STATE>4</STATE><RUNNING_VMS>0</RUNNING_VMS></IMAGE></IMAGE_POOL>";
|
||||
"<IMAGE_POOL><IMAGE><ID>0</ID><UID>0</UID><USERNAME>one_user_test</USERNAME><NAME>Image one</NAME><TYPE>0</TYPE><PUBLIC>0</PUBLIC><REGTIME>0000000000</REGTIME><SOURCE>source_prefix/9ab4a4e021ee2883f57e3aeecc9e2aed7c3fa198</SOURCE><STATE>3</STATE><RUNNING_VMS>0</RUNNING_VMS><TEMPLATE><DESCRIPTION><![CDATA[This is a very long description of an image, and to achieve the longness I will copy this over. This is a very long description of an image, and to achieve the longness I will copy this over. And over. This is a very long description of an image, and to achieve the longness I will copy this over. And over. This is a very long description of an image, and to achieve the longness I will copy this over. And over.This is a very long description of an image, and to achieve the longness I will copy this over.]]></DESCRIPTION><DEV_PREFIX><![CDATA[hd]]></DEV_PREFIX><NAME><![CDATA[Image one]]></NAME><ORIGINAL_PATH><![CDATA[/tmp/image_test]]></ORIGINAL_PATH></TEMPLATE></IMAGE><IMAGE><ID>1</ID><UID>1</UID><USERNAME>A user</USERNAME><NAME>Second Image</NAME><TYPE>0</TYPE><PUBLIC>1</PUBLIC><REGTIME>0000000000</REGTIME><SOURCE>source_prefix/c9d51800847467911c755e5e4c13dfe28c3a79f3</SOURCE><STATE>3</STATE><RUNNING_VMS>0</RUNNING_VMS><TEMPLATE><DESCRIPTION><![CDATA[This is a rather short description.]]></DESCRIPTION><DEV_PREFIX><![CDATA[hd]]></DEV_PREFIX><NAME><![CDATA[Second Image]]></NAME><ORIGINAL_PATH><![CDATA[/tmp/image_second_test]]></ORIGINAL_PATH></TEMPLATE></IMAGE><IMAGE><ID>2</ID><UID>2</UID><USERNAME>B user</USERNAME><NAME>The third image</NAME><TYPE>0</TYPE><PUBLIC>0</PUBLIC><REGTIME>0000000000</REGTIME><SOURCE>source_prefix/e50b0c738be9d431475bf5859629e5580301a7d6</SOURCE><STATE>3</STATE><RUNNING_VMS>0</RUNNING_VMS><TEMPLATE><BUS><![CDATA[SCSI]]></BUS><DEV_PREFIX><![CDATA[hd]]></DEV_PREFIX><NAME><![CDATA[The third image]]></NAME><ORIGINAL_PATH><![CDATA[/tmp/image_test]]></ORIGINAL_PATH><PROFILE><![CDATA[STUDENT]]></PROFILE></TEMPLATE></IMAGE></IMAGE_POOL>";
|
||||
|
||||
const string xml_dump_where =
|
||||
"<IMAGE_POOL><IMAGE><ID>0</ID><UID>0</UID><USERNAME>one_user_test</USERNAME><NAME>Image one</NAME><TYPE>0</TYPE><PUBLIC>0</PUBLIC><REGTIME>0000000000</REGTIME><SOURCE>source_prefix/9ab4a4e021ee2883f57e3aeecc9e2aed7c3fa198</SOURCE><STATE>4</STATE><RUNNING_VMS>0</RUNNING_VMS></IMAGE><IMAGE><ID>1</ID><UID>1</UID><USERNAME>A user</USERNAME><NAME>Second Image</NAME><TYPE>0</TYPE><PUBLIC>1</PUBLIC><REGTIME>0000000000</REGTIME><SOURCE>source_prefix/c9d51800847467911c755e5e4c13dfe28c3a79f3</SOURCE><STATE>4</STATE><RUNNING_VMS>0</RUNNING_VMS></IMAGE></IMAGE_POOL>";
|
||||
"<IMAGE_POOL><IMAGE><ID>0</ID><UID>0</UID><USERNAME>one_user_test</USERNAME><NAME>Image one</NAME><TYPE>0</TYPE><PUBLIC>0</PUBLIC><REGTIME>0000000000</REGTIME><SOURCE>source_prefix/9ab4a4e021ee2883f57e3aeecc9e2aed7c3fa198</SOURCE><STATE>3</STATE><RUNNING_VMS>0</RUNNING_VMS><TEMPLATE><DESCRIPTION><![CDATA[This is a very long description of an image, and to achieve the longness I will copy this over. This is a very long description of an image, and to achieve the longness I will copy this over. And over. This is a very long description of an image, and to achieve the longness I will copy this over. And over. This is a very long description of an image, and to achieve the longness I will copy this over. And over.This is a very long description of an image, and to achieve the longness I will copy this over.]]></DESCRIPTION><DEV_PREFIX><![CDATA[hd]]></DEV_PREFIX><NAME><![CDATA[Image one]]></NAME><ORIGINAL_PATH><![CDATA[/tmp/image_test]]></ORIGINAL_PATH></TEMPLATE></IMAGE><IMAGE><ID>1</ID><UID>1</UID><USERNAME>A user</USERNAME><NAME>Second Image</NAME><TYPE>0</TYPE><PUBLIC>1</PUBLIC><REGTIME>0000000000</REGTIME><SOURCE>source_prefix/c9d51800847467911c755e5e4c13dfe28c3a79f3</SOURCE><STATE>3</STATE><RUNNING_VMS>0</RUNNING_VMS><TEMPLATE><DESCRIPTION><![CDATA[This is a rather short description.]]></DESCRIPTION><DEV_PREFIX><![CDATA[hd]]></DEV_PREFIX><NAME><![CDATA[Second Image]]></NAME><ORIGINAL_PATH><![CDATA[/tmp/image_second_test]]></ORIGINAL_PATH></TEMPLATE></IMAGE></IMAGE_POOL>";
|
||||
|
||||
const string replacement = "0000000000";
|
||||
|
||||
@ -119,7 +119,6 @@ class ImagePoolTest : public PoolTest
|
||||
CPPUNIT_TEST ( target_generation );
|
||||
CPPUNIT_TEST ( bus_source_assignment );
|
||||
CPPUNIT_TEST ( public_attribute );
|
||||
CPPUNIT_TEST ( disk_overwrite );
|
||||
CPPUNIT_TEST ( imagepool_disk_attribute );
|
||||
CPPUNIT_TEST ( dump );
|
||||
CPPUNIT_TEST ( dump_where );
|
||||
@ -255,9 +254,11 @@ public:
|
||||
CPPUNIT_ASSERT( img != 0 );
|
||||
|
||||
// Image object should be cached. Let's change some template attributes
|
||||
ip->replace_attribute(img, description_name, new_description);
|
||||
ip->replace_attribute(img, attr_name, new_attr_value);
|
||||
ip->remove_attribute(img, "ORIGINAL_PATH");
|
||||
img->replace_template_attribute(description_name, new_description);
|
||||
img->replace_template_attribute(attr_name, new_attr_value);
|
||||
img->remove_template_attribute("ORIGINAL_PATH");
|
||||
|
||||
ip->update(img);
|
||||
|
||||
img->unlock();
|
||||
|
||||
@ -466,7 +467,7 @@ public:
|
||||
CPPUNIT_ASSERT( oid == 0 );
|
||||
|
||||
img->enable(true);
|
||||
img->disk_attribute(disk, &index, img_type);
|
||||
img->disk_attribute(disk, &index, &img_type);
|
||||
|
||||
value = disk->vector_value("TARGET");
|
||||
|
||||
@ -488,7 +489,7 @@ public:
|
||||
img = imp->get(oid, false);
|
||||
|
||||
img->enable(true);
|
||||
img->disk_attribute(disk, &index, img_type);
|
||||
img->disk_attribute(disk, &index, &img_type);
|
||||
|
||||
value = disk->vector_value("TARGET");
|
||||
CPPUNIT_ASSERT(value == "hdc");
|
||||
@ -509,7 +510,7 @@ public:
|
||||
img = imp->get(oid, false);
|
||||
|
||||
img->enable(true);
|
||||
img->disk_attribute(disk, &index, img_type);
|
||||
img->disk_attribute(disk, &index, &img_type);
|
||||
|
||||
value = disk->vector_value("TARGET");
|
||||
CPPUNIT_ASSERT(value == "hde");
|
||||
@ -530,7 +531,7 @@ public:
|
||||
img = imp->get(oid, false);
|
||||
|
||||
img->enable(true);
|
||||
img->disk_attribute(disk, &index, img_type);
|
||||
img->disk_attribute(disk, &index, &img_type);
|
||||
|
||||
value = disk->vector_value("TARGET");
|
||||
CPPUNIT_ASSERT(value == "sdf");
|
||||
@ -562,7 +563,7 @@ public:
|
||||
disk = new VectorAttribute("DISK");
|
||||
|
||||
img->enable(true);
|
||||
img->disk_attribute(disk, &index, img_type);
|
||||
img->disk_attribute(disk, &index, &img_type);
|
||||
|
||||
value = "";
|
||||
value = disk->vector_value("BUS");
|
||||
@ -582,7 +583,7 @@ public:
|
||||
disk->replace("BUS", "SCSI");
|
||||
|
||||
img->enable(true);
|
||||
img->disk_attribute(disk, &index, img_type);
|
||||
img->disk_attribute(disk, &index, &img_type);
|
||||
|
||||
value = disk->vector_value("BUS");
|
||||
CPPUNIT_ASSERT( value == "SCSI" );
|
||||
@ -596,137 +597,6 @@ public:
|
||||
delete disk;
|
||||
}
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
||||
void disk_overwrite()
|
||||
{
|
||||
ImagePool * imp = static_cast<ImagePool *>(pool);
|
||||
Image * img;
|
||||
|
||||
VectorAttribute * disk;
|
||||
int oid, rc;
|
||||
string value;
|
||||
int index = 0;
|
||||
Image::ImageType img_type;
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
// Allocate an OS type image
|
||||
oid = allocate(0);
|
||||
CPPUNIT_ASSERT( oid > -1 );
|
||||
img = imp->get(oid, false);
|
||||
|
||||
// Disk with overwrite=yes, save_as empty
|
||||
disk = new VectorAttribute("DISK");
|
||||
|
||||
disk->replace("OVERWRITE", "yes");
|
||||
|
||||
img->enable(true);
|
||||
rc = img->disk_attribute(disk, &index, img_type);
|
||||
CPPUNIT_ASSERT( rc == 0 );
|
||||
|
||||
|
||||
value = disk->vector_value("OVERWRITE");
|
||||
CPPUNIT_ASSERT( value == "YES" );
|
||||
|
||||
value = "";
|
||||
value = disk->vector_value("SAVE_AS");
|
||||
CPPUNIT_ASSERT( value == "" );
|
||||
|
||||
value = "";
|
||||
value = disk->vector_value("CLONE");
|
||||
CPPUNIT_ASSERT( value == "NO" );
|
||||
|
||||
value = "";
|
||||
value = disk->vector_value("SAVE");
|
||||
CPPUNIT_ASSERT( value == "YES" );
|
||||
|
||||
value = "";
|
||||
value = disk->vector_value("READONLY");
|
||||
CPPUNIT_ASSERT( value == "NO" );
|
||||
|
||||
// clean up
|
||||
delete disk;
|
||||
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
// Allocate an OS type image
|
||||
oid = allocate(1);
|
||||
CPPUNIT_ASSERT( oid > -1 );
|
||||
img = imp->get(oid, false);
|
||||
|
||||
// Disk with overwrite=no, save_as not empty
|
||||
disk = new VectorAttribute("DISK");
|
||||
|
||||
disk->replace("OVERWRITE", "NO");
|
||||
disk->replace("SAVE_AS", "path_to_save");
|
||||
|
||||
img->enable(true);
|
||||
rc = img->disk_attribute(disk, &index, img_type);
|
||||
CPPUNIT_ASSERT( rc == 0 );
|
||||
|
||||
value = "";
|
||||
value = disk->vector_value("OVERWRITE");
|
||||
CPPUNIT_ASSERT( value == "NO" );
|
||||
|
||||
value = "";
|
||||
value = disk->vector_value("SAVE_AS");
|
||||
CPPUNIT_ASSERT( value == "path_to_save" );
|
||||
|
||||
value = "";
|
||||
value = disk->vector_value("CLONE");
|
||||
CPPUNIT_ASSERT( value == "YES" );
|
||||
|
||||
value = "";
|
||||
value = disk->vector_value("SAVE");
|
||||
CPPUNIT_ASSERT( value == "YES" );
|
||||
|
||||
value = "";
|
||||
value = disk->vector_value("READONLY");
|
||||
CPPUNIT_ASSERT( value == "NO" );
|
||||
|
||||
// clean up
|
||||
delete disk;
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
// Allocate an OS type image
|
||||
oid = allocate(2);
|
||||
CPPUNIT_ASSERT( oid > -1 );
|
||||
img = imp->get(oid, false);
|
||||
|
||||
// Disk with overwrite=no, save_as not present
|
||||
disk = new VectorAttribute("DISK");
|
||||
|
||||
disk->replace("OVERWRITE", "NO");
|
||||
|
||||
img->enable(true);
|
||||
rc = img->disk_attribute(disk, &index, img_type);
|
||||
CPPUNIT_ASSERT( rc == 0 );
|
||||
|
||||
value = "";
|
||||
value = disk->vector_value("OVERWRITE");
|
||||
CPPUNIT_ASSERT( value == "NO" );
|
||||
|
||||
value = "";
|
||||
value = disk->vector_value("SAVE_AS");
|
||||
CPPUNIT_ASSERT( value == "" );
|
||||
|
||||
value = "";
|
||||
value = disk->vector_value("CLONE");
|
||||
CPPUNIT_ASSERT( value == "YES" );
|
||||
|
||||
value = "";
|
||||
value = disk->vector_value("SAVE");
|
||||
CPPUNIT_ASSERT( value == "NO" );
|
||||
|
||||
value = "";
|
||||
value = disk->vector_value("READONLY");
|
||||
CPPUNIT_ASSERT( value == "NO" );
|
||||
|
||||
// clean up
|
||||
delete disk;
|
||||
}
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
||||
@ -736,7 +606,7 @@ public:
|
||||
Image * img;
|
||||
|
||||
VectorAttribute * disk;
|
||||
int oid_0, oid_1;
|
||||
int oid_0, oid_1, index;
|
||||
string value;
|
||||
Image::ImageType img_type;
|
||||
|
||||
@ -770,7 +640,7 @@ public:
|
||||
disk = new VectorAttribute("DISK");
|
||||
disk->replace("IMAGE", "Image 0");
|
||||
|
||||
((ImagePool*)imp)->disk_attribute(disk, 0, img_type);
|
||||
((ImagePool*)imp)->disk_attribute(disk, 0, &index, &img_type);
|
||||
|
||||
value = "";
|
||||
value = disk->vector_value("TARGET");
|
||||
@ -787,7 +657,7 @@ public:
|
||||
disk = new VectorAttribute("DISK");
|
||||
disk->replace("IMAGE_ID", "1");
|
||||
|
||||
((ImagePool*)imp)->disk_attribute(disk, 0, img_type);
|
||||
((ImagePool*)imp)->disk_attribute(disk, 0, &index, &img_type);
|
||||
|
||||
value = "";
|
||||
value = disk->vector_value("TARGET");
|
||||
@ -902,8 +772,8 @@ public:
|
||||
string result = oss.str();
|
||||
|
||||
result.replace(138, 10, replacement);
|
||||
result.replace(403, 10, replacement);
|
||||
result.replace(671, 10, replacement);
|
||||
result.replace(1102, 10, replacement);
|
||||
result.replace(1606, 10, replacement);
|
||||
|
||||
CPPUNIT_ASSERT( result == xml_dump );
|
||||
}
|
||||
@ -931,9 +801,8 @@ public:
|
||||
CPPUNIT_ASSERT(rc == 0);
|
||||
|
||||
string result = oss.str();
|
||||
|
||||
result.replace(138, 10, replacement);
|
||||
result.replace(403, 10, replacement);
|
||||
result.replace(138, 10, replacement);
|
||||
result.replace(1102, 10, replacement);
|
||||
|
||||
CPPUNIT_ASSERT( result == xml_dump_where );
|
||||
}
|
||||
|
@ -80,6 +80,9 @@ main_env.Append(LIBS=[
|
||||
main_env.Append(LIBPATH=["/usr/lib/mysql"])
|
||||
main_env.Append(CPPPATH=["/usr/include/mysql"])
|
||||
|
||||
# libxml2
|
||||
main_env.ParseConfig('xml2-config --libs --cflags')
|
||||
|
||||
sqlite=ARGUMENTS.get('sqlite', 'yes')
|
||||
if sqlite=='yes':
|
||||
main_env.Append(CPPFLAGS=["-DSQLITE_DB"])
|
||||
|
@ -22,6 +22,7 @@
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdexcept>
|
||||
#include <libxml/parser.h>
|
||||
|
||||
#include <signal.h>
|
||||
#include <unistd.h>
|
||||
@ -115,6 +116,11 @@ void Nebula::start()
|
||||
|
||||
NebulaLog::log("ONE",Log::INFO,os);
|
||||
|
||||
// -----------------------------------------------------------
|
||||
// Initialize the XML library
|
||||
// -----------------------------------------------------------
|
||||
xmlInitParser();
|
||||
|
||||
// -----------------------------------------------------------
|
||||
// Pools
|
||||
// -----------------------------------------------------------
|
||||
@ -526,6 +532,9 @@ void Nebula::start()
|
||||
pthread_join(rm->get_thread_id(),0);
|
||||
pthread_join(hm->get_thread_id(),0);
|
||||
|
||||
//XML Library
|
||||
xmlCleanupParser();
|
||||
|
||||
NebulaLog::log("ONE", Log::INFO, "All modules finalized, exiting.\n");
|
||||
}
|
||||
|
||||
|
@ -100,7 +100,12 @@ void RequestManager::ImageRemoveAttribute::execute(
|
||||
goto error_image_get;
|
||||
}
|
||||
|
||||
rc = ImageRemoveAttribute::ipool->remove_attribute(image, name);
|
||||
rc = image->remove_template_attribute(name);
|
||||
|
||||
if(rc == 0)
|
||||
{
|
||||
rc = ImageRemoveAttribute::ipool->update(image);
|
||||
}
|
||||
|
||||
if ( rc < 0 )
|
||||
{
|
||||
|
@ -101,9 +101,12 @@ void RequestManager::ImageUpdate::execute(
|
||||
goto error_image_get;
|
||||
}
|
||||
|
||||
// This will perform the update on the DB as well,
|
||||
// so no need to do it manually
|
||||
rc = ImageUpdate::ipool->replace_attribute(image, name, value);
|
||||
rc = image->replace_template_attribute(name, value);
|
||||
|
||||
if(rc == 0)
|
||||
{
|
||||
rc = ImageUpdate::ipool->update(image);
|
||||
}
|
||||
|
||||
if ( rc < 0 )
|
||||
{
|
||||
|
@ -37,7 +37,6 @@ if env['parsers']=='yes':
|
||||
# Sources to generate the library
|
||||
source_files=[
|
||||
'Template.cc',
|
||||
'TemplateSQL.cc',
|
||||
'template_parser.c',
|
||||
'template_syntax.cc'
|
||||
]
|
||||
|
@ -460,6 +460,19 @@ int Template::from_xml(const string &xml_str)
|
||||
return -1;
|
||||
}
|
||||
|
||||
//Clear the template if not empty
|
||||
if (!attributes.empty())
|
||||
{
|
||||
multimap<string,Attribute *>::iterator it;
|
||||
|
||||
for ( it = attributes.begin(); it != attributes.end(); it++)
|
||||
{
|
||||
delete it->second;
|
||||
}
|
||||
|
||||
attributes.clear();
|
||||
}
|
||||
|
||||
// Get the <TEMPLATE> element
|
||||
root_element = xmlDocGetRootElement(xml_doc);
|
||||
|
||||
@ -485,6 +498,8 @@ int Template::from_xml(const string &xml_str)
|
||||
}
|
||||
}
|
||||
|
||||
xmlFreeDoc(xml_doc);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -1,361 +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 "TemplateSQL.h"
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
|
||||
/* ************************************************************************ */
|
||||
/* SQL Template */
|
||||
/* ************************************************************************ */
|
||||
|
||||
const char * TemplateSQL::db_names = "(id,name,type,value)";
|
||||
|
||||
/* ------------------------------------------------------------------------ */
|
||||
/* ------------------------------------------------------------------------ */
|
||||
|
||||
int TemplateSQL::insert(SqlDB * db)
|
||||
{
|
||||
int rc;
|
||||
|
||||
if ( id == -1)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
rc = insert_replace(db, false);
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------ */
|
||||
|
||||
int TemplateSQL::update(SqlDB * db)
|
||||
{
|
||||
int rc;
|
||||
|
||||
rc = insert_replace(db, true);
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------ */
|
||||
|
||||
int TemplateSQL::insert_replace(SqlDB *db, bool replace)
|
||||
{
|
||||
multimap<string,Attribute *>::iterator it;
|
||||
ostringstream oss;
|
||||
int rc;
|
||||
string * attr;
|
||||
char * sql_attr;
|
||||
Attribute::AttributeType atype;
|
||||
|
||||
for(it=attributes.begin(),oss.str("");
|
||||
it!=attributes.end();
|
||||
it++,oss.str(""))
|
||||
{
|
||||
if ( it->second == 0 )
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
attr = it->second->marshall();
|
||||
atype = it->second->type();
|
||||
|
||||
if ( attr == 0 )
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
sql_attr = db->escape_str((*attr).c_str());
|
||||
|
||||
delete attr;
|
||||
|
||||
if ( sql_attr == 0 )
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if(replace)
|
||||
{
|
||||
oss << "REPLACE";
|
||||
}
|
||||
else
|
||||
{
|
||||
oss << "INSERT";
|
||||
}
|
||||
|
||||
oss << " INTO " << table << " " << db_names
|
||||
<< " VALUES (" << id << ",'" << it->first << "',"<< atype <<",'"
|
||||
<< sql_attr << "')";
|
||||
|
||||
rc = db->exec(oss);
|
||||
|
||||
db->free_str(sql_attr);
|
||||
|
||||
if ( rc != 0 )
|
||||
{
|
||||
goto error_sql;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
error_sql:
|
||||
drop(db);
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
/* ------------------------------------------------------------------------ */
|
||||
/* ------------------------------------------------------------------------ */
|
||||
|
||||
int TemplateSQL::select_cb(void *nil, int num, char **values, char **names)
|
||||
{
|
||||
Attribute * attr;
|
||||
|
||||
string name;
|
||||
string value;
|
||||
int atype;
|
||||
|
||||
if (num != 4)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
if ( values[1] != 0 )
|
||||
{
|
||||
name = values[1];
|
||||
}
|
||||
else
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
if ( values[3] != 0 )
|
||||
{
|
||||
value = values[3];
|
||||
}
|
||||
else
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
if ( values[2] != 0 )
|
||||
{
|
||||
atype = atoi(values[2]);
|
||||
|
||||
switch (atype)
|
||||
{
|
||||
case Attribute::SIMPLE:
|
||||
attr = new SingleAttribute(name);
|
||||
break;
|
||||
|
||||
case Attribute::VECTOR:
|
||||
attr = new VectorAttribute(name);
|
||||
break;
|
||||
|
||||
default:
|
||||
return -1;
|
||||
break;
|
||||
};
|
||||
}
|
||||
|
||||
attr->unmarshall(value);
|
||||
|
||||
set(attr);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------ */
|
||||
/* ------------------------------------------------------------------------ */
|
||||
|
||||
int TemplateSQL::select(SqlDB * db)
|
||||
{
|
||||
ostringstream oss;
|
||||
int rc;
|
||||
|
||||
if ( id == -1 )
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
set_callback(static_cast<Callbackable::Callback>(&TemplateSQL::select_cb));
|
||||
|
||||
oss << "SELECT * FROM " << table << " WHERE id=" << id;
|
||||
|
||||
rc = db->exec(oss,this);
|
||||
|
||||
unset_callback();
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------ */
|
||||
/* ------------------------------------------------------------------------ */
|
||||
|
||||
int TemplateSQL::drop(SqlDB * db)
|
||||
{
|
||||
ostringstream oss;
|
||||
|
||||
if ( id == -1 )
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
oss << "DELETE FROM " << table << " WHERE id=" << id;
|
||||
|
||||
return db->exec(oss);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------ */
|
||||
/* ------------------------------------------------------------------------ */
|
||||
|
||||
int TemplateSQL::replace_attribute(SqlDB * db, Attribute * attribute)
|
||||
{
|
||||
ostringstream oss;
|
||||
int rc;
|
||||
|
||||
multimap<string, Attribute *>::iterator i;
|
||||
|
||||
if ( id == -1 || attribute == 0)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
rc = remove_attribute(db, attribute->name());
|
||||
|
||||
if (rc == 0)
|
||||
{
|
||||
rc = insert_attribute(db,attribute);
|
||||
}
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------ */
|
||||
/* ------------------------------------------------------------------------ */
|
||||
|
||||
int TemplateSQL::insert_attribute(SqlDB * db, Attribute * attribute)
|
||||
{
|
||||
ostringstream oss;
|
||||
int rc;
|
||||
string * astr;
|
||||
int atype;
|
||||
|
||||
char * sql_attr;
|
||||
|
||||
if ( id == -1 || attribute == 0)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
astr = attribute->marshall();
|
||||
atype = attribute->type();
|
||||
|
||||
if ( astr == 0 )
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
sql_attr = db->escape_str((*astr).c_str());
|
||||
|
||||
delete astr;
|
||||
|
||||
if ( sql_attr == 0 )
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
oss << "INSERT INTO " << table << " " << db_names
|
||||
<< " VALUES (" << id << ",'" << attribute->name() << "'," << atype
|
||||
<< ",'" << sql_attr << "')";
|
||||
|
||||
rc = db->exec(oss);
|
||||
|
||||
db->free_str(sql_attr);
|
||||
|
||||
if (rc == 0)
|
||||
{
|
||||
attributes.insert(make_pair(attribute->name(),attribute));
|
||||
}
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------ */
|
||||
/* ------------------------------------------------------------------------ */
|
||||
|
||||
int TemplateSQL::remove_attribute(SqlDB * db, const string& name)
|
||||
{
|
||||
ostringstream oss;
|
||||
int rc;
|
||||
string * astr;
|
||||
char * sql_attr;
|
||||
|
||||
multimap<string, Attribute *>::iterator i;
|
||||
|
||||
if ( id == -1 )
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
i = attributes.find(name);
|
||||
|
||||
if ( i != attributes.end() )
|
||||
{
|
||||
astr = i->second->marshall();
|
||||
|
||||
if ( astr == 0 )
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
sql_attr = db->escape_str((*astr).c_str());
|
||||
|
||||
delete astr;
|
||||
|
||||
if ( sql_attr == 0 )
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
oss << "DELETE FROM " << table << " WHERE id=" << id
|
||||
<< " AND name='" << name << "' AND value='"
|
||||
<< sql_attr << "'";
|
||||
|
||||
rc = db->exec(oss);
|
||||
|
||||
db->free_str(sql_attr);
|
||||
|
||||
if (rc != 0 )
|
||||
{
|
||||
return rc;
|
||||
}
|
||||
|
||||
delete i->second;
|
||||
|
||||
attributes.erase(i);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------ */
|
||||
/* ------------------------------------------------------------------------ */
|
||||
|
@ -43,7 +43,6 @@ main_env.Append(LIBPATH=[
|
||||
cwd + '/src/common',
|
||||
cwd + '/src/template',
|
||||
cwd + '/src/nebula',
|
||||
cwd + '/src/sql',
|
||||
cwd + '/src/log',
|
||||
])
|
||||
|
||||
@ -51,7 +50,6 @@ main_env.Append(LIBS=[
|
||||
'nebula_template',
|
||||
'nebula_common',
|
||||
'nebula_core',
|
||||
'nebula_sql',
|
||||
'nebula_log',
|
||||
'cppunit',
|
||||
'dl',
|
||||
@ -83,5 +81,4 @@ if mysql=='yes':
|
||||
main_env.Append(LIBS=[ 'mysqlclient', ])
|
||||
|
||||
main_env.Program('test','template.cc')
|
||||
main_env.Program('test_sql','template_sql.cc')
|
||||
#
|
||||
|
@ -15,7 +15,7 @@ using namespace std;
|
||||
/* ************************************************************************* */
|
||||
/* ************************************************************************* */
|
||||
|
||||
class TemplateTest : public CppUnit::TestFixture
|
||||
class TemplateTest : public CppUnit::TestFixture
|
||||
{
|
||||
private:
|
||||
Template *t, *tr, *t1;
|
||||
@ -32,7 +32,7 @@ public:
|
||||
|
||||
TemplateTest()
|
||||
{
|
||||
test_ok =
|
||||
test_ok =
|
||||
"#This line is a comment\n"
|
||||
" # Other comment\n"
|
||||
"MEMORY=345 # more comments behind an attribute\n"
|
||||
@ -89,6 +89,8 @@ public:
|
||||
|
||||
t1= new Template();
|
||||
t1->parse(test_ok,&error);
|
||||
|
||||
xmlInitParser();
|
||||
}
|
||||
|
||||
void tearDown()
|
||||
@ -96,6 +98,8 @@ public:
|
||||
delete t;
|
||||
delete tr;
|
||||
delete t1;
|
||||
|
||||
xmlCleanupParser();
|
||||
}
|
||||
|
||||
/* ********************************************************************* */
|
||||
@ -108,7 +112,7 @@ public:
|
||||
string tmp;
|
||||
|
||||
rc = t->parse(test_ok,&error);
|
||||
|
||||
|
||||
if ( error != 0 )
|
||||
{
|
||||
cerr << error << endl;
|
||||
@ -139,7 +143,7 @@ public:
|
||||
|
||||
CPPUNIT_ASSERT(test_ok_xml == tmp);
|
||||
}
|
||||
|
||||
|
||||
/* --------------------------------------------------------------------- */
|
||||
|
||||
void test_str()
|
||||
@ -157,21 +161,21 @@ public:
|
||||
{
|
||||
vector<Attribute*> attrs;
|
||||
string *tmp;
|
||||
|
||||
|
||||
CPPUNIT_ASSERT(t1->get("DISK",attrs) == 2 );
|
||||
|
||||
CPPUNIT_ASSERT(attrs[0]->type() == Attribute::VECTOR);
|
||||
|
||||
tmp = attrs[0]->to_xml();
|
||||
CPPUNIT_ASSERT( *tmp ==
|
||||
CPPUNIT_ASSERT( *tmp ==
|
||||
"<DISK><EXTRA><![CDATA[disk attribute ]]></EXTRA><FILE>"
|
||||
"<![CDATA[path1]]></FILE></DISK>");
|
||||
delete tmp;
|
||||
|
||||
CPPUNIT_ASSERT(attrs[1]->type() == Attribute::VECTOR);
|
||||
|
||||
|
||||
tmp = attrs[1]->to_xml();
|
||||
CPPUNIT_ASSERT( *tmp ==
|
||||
CPPUNIT_ASSERT( *tmp ==
|
||||
"<DISK><EXTRA><![CDATA[str]]></EXTRA><FILE><![CDATA[path2]]>"
|
||||
"</FILE><TYPE><![CDATA[disk]]></TYPE></DISK>");
|
||||
delete tmp;
|
||||
@ -179,7 +183,7 @@ public:
|
||||
CPPUNIT_ASSERT(t1->get("CPU",attrs) == 1 );
|
||||
|
||||
CPPUNIT_ASSERT(attrs[2]->type() == Attribute::SIMPLE);
|
||||
|
||||
|
||||
tmp = attrs[2]->to_xml();
|
||||
CPPUNIT_ASSERT( *tmp == "<CPU><![CDATA[4]]></CPU>");
|
||||
delete tmp;
|
||||
@ -203,7 +207,7 @@ public:
|
||||
void test_remove()
|
||||
{
|
||||
vector<Attribute*> attrs;
|
||||
|
||||
|
||||
string t1_xml;
|
||||
string rm_xml="<TEMPLATE><CPU><![CDATA[4]]></CPU><EMPTY_VAR>"
|
||||
"<![CDATA[]]></EMPTY_VAR><GRAPHICS><PORT><![CDATA[12]]></PORT><VNC>"
|
||||
@ -235,7 +239,7 @@ public:
|
||||
"<![CDATA[HOSTNAME = \"host*.com\"]]></REQUIREMENTS><XTRA>"
|
||||
"<![CDATA[44]]></XTRA></TEMPLATE>";
|
||||
string xml;
|
||||
|
||||
|
||||
string nattr = "XTRA";
|
||||
string vattr = "44";
|
||||
|
||||
@ -351,7 +355,7 @@ public:
|
||||
ts->addTest(new CppUnit::TestCaller<TemplateTest>(
|
||||
"parse() Test",
|
||||
&TemplateTest::test_parser));
|
||||
|
||||
|
||||
ts->addTest(new CppUnit::TestCaller<TemplateTest>(
|
||||
"marshall() Test",
|
||||
&TemplateTest::test_marshall));
|
||||
@ -395,7 +399,7 @@ public:
|
||||
int main(int argc, char ** argv)
|
||||
{
|
||||
CppUnit::TextUi::TestRunner tr;
|
||||
|
||||
|
||||
tr.addTest(TemplateTest::suite());
|
||||
tr.run();
|
||||
|
||||
|
@ -1,470 +0,0 @@
|
||||
#include "TemplateSQL.h"
|
||||
#include "SqliteDB.h"
|
||||
#include "SqlDB.h"
|
||||
#include "MySqlDB.h"
|
||||
#include "Log.h"
|
||||
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <unistd.h>
|
||||
#include <getopt.h>
|
||||
|
||||
#include <TestFixture.h>
|
||||
#include <TestAssert.h>
|
||||
#include <TestSuite.h>
|
||||
#include <TestCaller.h>
|
||||
#include <ui/text/TestRunner.h>
|
||||
#include <cppunit/extensions/HelperMacros.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
/* ************************************************************************* */
|
||||
/* ************************************************************************* */
|
||||
/* ************************************************************************* */
|
||||
|
||||
/* --------------------------------------- */
|
||||
/* WRAPPER TO ACCESS TEMPLATESQL CLASS */
|
||||
/* --------------------------------------- */
|
||||
|
||||
class TSQL : public TemplateSQL
|
||||
{
|
||||
public:
|
||||
TSQL(const char * _table,
|
||||
int template_id = -1,
|
||||
bool replace = false,
|
||||
const char separator = '=',
|
||||
const char * xml_root = "TEMPLATE"):
|
||||
TemplateSQL(_table,template_id,replace,separator,xml_root){}
|
||||
|
||||
~TSQL(){}
|
||||
|
||||
/* --------------------------------------------------------------------- */
|
||||
int insert(SqlDB * db){return TemplateSQL::insert(db);}
|
||||
int update(SqlDB * db){return TemplateSQL::update(db);}
|
||||
int select(SqlDB * db){return TemplateSQL::select(db);}
|
||||
int drop (SqlDB * db){return TemplateSQL::drop(db);}
|
||||
|
||||
/* --------------------------------------------------------------------- */
|
||||
int replace_attribute(SqlDB * db, Attribute * attr)
|
||||
{return TemplateSQL::replace_attribute(db,attr);}
|
||||
int insert_attribute(SqlDB * db, Attribute * attr)
|
||||
{return TemplateSQL::insert_attribute(db,attr);}
|
||||
int remove_attribute(SqlDB * db, const string& name)
|
||||
{return TemplateSQL::remove_attribute(db,name);};
|
||||
/* --------------------------------------------------------------------- */
|
||||
int id(){return TemplateSQL::id;};
|
||||
void sid(int i){TemplateSQL::id = i;}
|
||||
};
|
||||
|
||||
/* ************************************************************************* */
|
||||
/* ************************************************************************* */
|
||||
/* ************************************************************************* */
|
||||
|
||||
class TemplateSQLTest : public CppUnit::TestFixture
|
||||
{
|
||||
|
||||
CPPUNIT_TEST_SUITE (TemplateSQLTest);
|
||||
|
||||
CPPUNIT_TEST (test_insert);
|
||||
CPPUNIT_TEST (test_update);
|
||||
CPPUNIT_TEST (test_select);
|
||||
CPPUNIT_TEST (test_drop);
|
||||
CPPUNIT_TEST (test_replace_attribute);
|
||||
CPPUNIT_TEST (test_insert_attribute);
|
||||
CPPUNIT_TEST (test_remove_attribute);
|
||||
|
||||
CPPUNIT_TEST_SUITE_END ();
|
||||
|
||||
private:
|
||||
SqlDB *db;
|
||||
|
||||
string db_name;
|
||||
|
||||
string template_ok;
|
||||
string template_xml;
|
||||
|
||||
public:
|
||||
// Global flag to use either Sqlite or MySQL
|
||||
static bool mysql;
|
||||
|
||||
/* --------------------------------------------------------------------- */
|
||||
/* --------------------------------------------------------------------- */
|
||||
|
||||
TemplateSQLTest()
|
||||
{
|
||||
db_name = "ONE_test_database";
|
||||
|
||||
template_ok =
|
||||
"#This line is a comment\n"
|
||||
" # Other comment\n"
|
||||
"MEMORY=345 # more comments behind an attribute\n"
|
||||
" CPU = 4\n"
|
||||
"#------------------------------\n"
|
||||
"#Comments in the middle\n"
|
||||
"#------------------------------\n"
|
||||
" empty_var = \n"
|
||||
"REQUIREMENTS = \"HOSTNAME = \\\"host*.com\\\"\"\n"
|
||||
" DISK= [file = path1, extra = \"disk attribute \"]\n"
|
||||
"DISK =[ FILE = \"path2\", EXTRA = str, TYPE=disk]\n"
|
||||
" graphics = [\n"
|
||||
" VNC = \"127.0.0.1\",\n"
|
||||
" PORT = 12\n"
|
||||
" ]\n";
|
||||
|
||||
template_xml =
|
||||
"<TEMPLATE><CPU><![CDATA[4]]></CPU><DISK><EXTRA><![CDATA[disk attribute ]]></EXTRA><FILE><![CDATA[path1]]></FILE></DISK><DISK><EXTRA><![CDATA[str]]></EXTRA><FILE><![CDATA[path2]]></FILE><TYPE><![CDATA[disk]]></TYPE></DISK><EMPTY_VAR><![CDATA[]]></EMPTY_VAR><GRAPHICS><PORT><![CDATA[12]]></PORT><VNC><![CDATA[127.0.0.1]]></VNC></GRAPHICS><MEMORY><![CDATA[345]]></MEMORY><REQUIREMENTS><![CDATA[HOSTNAME = \"host*.com\"]]></REQUIREMENTS></TEMPLATE>";
|
||||
}
|
||||
|
||||
~TemplateSQLTest(){};
|
||||
|
||||
/* --------------------------------------------------------------------- */
|
||||
/* --------------------------------------------------------------------- */
|
||||
|
||||
void setUp()
|
||||
{
|
||||
if (mysql)
|
||||
{
|
||||
db = new MySqlDB("localhost","oneadmin","oneadmin",NULL);
|
||||
|
||||
ostringstream oss1;
|
||||
oss1 << "DROP DATABASE IF EXISTS " << db_name;
|
||||
db->exec(oss1);
|
||||
|
||||
ostringstream oss;
|
||||
oss << "CREATE DATABASE " << db_name;
|
||||
db->exec(oss);
|
||||
|
||||
ostringstream oss2;
|
||||
oss2 << "use " << db_name;
|
||||
db->exec(oss2);
|
||||
}
|
||||
else
|
||||
{
|
||||
unlink(db_name.c_str());
|
||||
|
||||
db = new SqliteDB(db_name);
|
||||
}
|
||||
|
||||
ostringstream db_bs(
|
||||
"CREATE TABLE IF NOT EXISTS template (id INTEGER, "
|
||||
"name TEXT, type INTEGER, value TEXT)");
|
||||
|
||||
CPPUNIT_ASSERT(db->exec(db_bs) == 0);
|
||||
}
|
||||
|
||||
void tearDown()
|
||||
{
|
||||
if (mysql)
|
||||
{
|
||||
ostringstream oss;
|
||||
oss << "DROP DATABASE IF EXISTS " << db_name;
|
||||
db->exec(oss);
|
||||
}
|
||||
else
|
||||
{
|
||||
unlink(db_name.c_str());
|
||||
}
|
||||
|
||||
if( db != 0 )
|
||||
delete db;
|
||||
}
|
||||
|
||||
/* ********************************************************************* */
|
||||
/* ********************************************************************* */
|
||||
|
||||
void test_insert()
|
||||
{
|
||||
char * error = 0;
|
||||
int rc;
|
||||
string tmp;
|
||||
TSQL t("template");
|
||||
|
||||
rc = t.parse(template_ok,&error);
|
||||
|
||||
if ( error != 0 )
|
||||
{
|
||||
cerr << error << endl;
|
||||
free(error);
|
||||
}
|
||||
|
||||
CPPUNIT_ASSERT( rc == 0);
|
||||
|
||||
t.sid(0);
|
||||
CPPUNIT_ASSERT( t.insert(db) == 0 );
|
||||
CPPUNIT_ASSERT( t.id() == 0 );
|
||||
|
||||
t.sid(1);
|
||||
CPPUNIT_ASSERT( t.insert(db) == 0 );
|
||||
CPPUNIT_ASSERT( t.id() == 1 );
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------- */
|
||||
|
||||
void test_update()
|
||||
{
|
||||
int rc;
|
||||
string att_name = "NEW_ATT";
|
||||
string value = "";
|
||||
|
||||
ostringstream db_bs(
|
||||
"CREATE TABLE IF NOT EXISTS template_replace (id INTEGER, "
|
||||
"name VARCHAR(256), type INTEGER, value TEXT, PRIMARY KEY(id,name))");
|
||||
|
||||
CPPUNIT_ASSERT(db->exec(db_bs) == 0);
|
||||
|
||||
TSQL t ("template_replace",0);
|
||||
TSQL t2("template_replace",0);
|
||||
|
||||
// Insert template t into the DB
|
||||
t.insert(db);
|
||||
|
||||
// Add a new vector attribute to t, and insert it into the DB
|
||||
SingleAttribute * s_att = new SingleAttribute(att_name, "some value");
|
||||
t.insert_attribute(db, ((Attribute*)s_att));
|
||||
|
||||
|
||||
// Now replace the attribute's value
|
||||
s_att->replace("a new value");
|
||||
|
||||
// And update the DB
|
||||
t.update(db);
|
||||
|
||||
// Read from the DB to t2
|
||||
rc = t2.select(db);
|
||||
CPPUNIT_ASSERT( rc == 0 );
|
||||
|
||||
t2.get(att_name, value);
|
||||
CPPUNIT_ASSERT( value == "a new value" );
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------- */
|
||||
|
||||
void test_select()
|
||||
{
|
||||
char * error = 0;
|
||||
|
||||
TSQL t("template");
|
||||
TSQL t2("template",23);
|
||||
|
||||
string t2_xml;
|
||||
|
||||
t.sid(23);
|
||||
t.parse(template_ok,&error);
|
||||
|
||||
int rc = t.insert(db);
|
||||
CPPUNIT_ASSERT( rc == 0 );
|
||||
|
||||
if ( error != 0 )
|
||||
{
|
||||
free(error);
|
||||
}
|
||||
|
||||
CPPUNIT_ASSERT( t2.select(db) == 0 );
|
||||
|
||||
t2.to_xml(t2_xml);
|
||||
|
||||
CPPUNIT_ASSERT( t2_xml == template_xml );
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------- */
|
||||
|
||||
void test_drop()
|
||||
{
|
||||
char * error = 0;
|
||||
string str = "";
|
||||
|
||||
TSQL t ("template",0);
|
||||
TSQL t2("template",0);
|
||||
|
||||
t.parse(template_ok,&error);
|
||||
t.insert(db);
|
||||
|
||||
if ( error != 0 )
|
||||
{
|
||||
free(error);
|
||||
}
|
||||
|
||||
// Drop the template from the DB
|
||||
t.drop(db);
|
||||
|
||||
// Try to read it from the DB
|
||||
CPPUNIT_ASSERT( t2.select(db) == 0 );
|
||||
// It should be empty
|
||||
CPPUNIT_ASSERT( t2.to_str(str) == "" );
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------- */
|
||||
|
||||
void test_replace_attribute()
|
||||
{
|
||||
int rc;
|
||||
string att_name = "NEW_ATT";
|
||||
string value = "";
|
||||
|
||||
|
||||
TSQL t ("template",0);
|
||||
TSQL t2("template",0);
|
||||
|
||||
// Add a new vector attribute to t, and insert it into the DB
|
||||
VectorAttribute * v_att = new VectorAttribute(att_name);
|
||||
v_att->replace("A", "A value");
|
||||
v_att->replace("B", "B value");
|
||||
v_att->replace("C", "C value");
|
||||
|
||||
t.insert(db);
|
||||
t.insert_attribute(db, ((Attribute*)v_att));
|
||||
|
||||
|
||||
|
||||
// Now replace its value, with a single value attribute
|
||||
SingleAttribute * s_att = new SingleAttribute(att_name, "some value");
|
||||
|
||||
t.replace_attribute(db, ((Attribute*)s_att) );
|
||||
|
||||
|
||||
// Read from the DB to t2
|
||||
rc = t2.select(db);
|
||||
CPPUNIT_ASSERT( rc == 0 );
|
||||
|
||||
t2.get(att_name, value);
|
||||
CPPUNIT_ASSERT( value == "some value" );
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------- */
|
||||
|
||||
void test_insert_attribute()
|
||||
{
|
||||
Attribute * att;
|
||||
int rc;
|
||||
string att_name = "NEW_ATT";
|
||||
string value = "";
|
||||
|
||||
|
||||
TSQL t ("template",0);
|
||||
TSQL t2("template",0);
|
||||
|
||||
// Add a new attribute to t, and insert it into the DB
|
||||
att = new SingleAttribute(att_name, "some value");
|
||||
|
||||
t.insert(db);
|
||||
t.insert_attribute(db, att);
|
||||
|
||||
// Read from the DB to t2
|
||||
rc = t2.select(db);
|
||||
CPPUNIT_ASSERT( rc == 0 );
|
||||
|
||||
t2.get(att_name, value);
|
||||
CPPUNIT_ASSERT( value == "some value" );
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------- */
|
||||
|
||||
void test_remove_attribute()
|
||||
{
|
||||
Attribute * att;
|
||||
int rc;
|
||||
string att_name = "NEW_ATT";
|
||||
string value = "";
|
||||
|
||||
|
||||
TSQL t ("template",0);
|
||||
TSQL t2("template",0);
|
||||
|
||||
// Add a new attribute to t, and insert it into the DB
|
||||
att = new SingleAttribute(att_name, "some value");
|
||||
|
||||
t.insert(db);
|
||||
t.insert_attribute(db, att);
|
||||
|
||||
t.remove_attribute(db, att_name);
|
||||
|
||||
// Read from the DB to t2
|
||||
rc = t2.select(db);
|
||||
CPPUNIT_ASSERT( rc == 0 );
|
||||
|
||||
t2.get(att_name, value);
|
||||
CPPUNIT_ASSERT( value == "" );
|
||||
}
|
||||
};
|
||||
|
||||
/* ************************************************************************* */
|
||||
|
||||
bool TemplateSQLTest::mysql;
|
||||
|
||||
/* ************************************************************************* */
|
||||
/* ************************************************************************* */
|
||||
/* ************************************************************************* */
|
||||
|
||||
static void show_options ()
|
||||
{
|
||||
cout << "Options:\n";
|
||||
cout << " -h --help Show this help\n"
|
||||
" -s --sqlite Run Sqlite tests (default)\n"
|
||||
" -m --mysql Run MySQL tests\n"
|
||||
" -l --log Keep the log file, test.log\n";
|
||||
}
|
||||
|
||||
int main(int argc, char ** argv)
|
||||
{
|
||||
|
||||
// Option flags
|
||||
bool sqlite_flag = true;
|
||||
bool log_flag = false;
|
||||
|
||||
// Long options
|
||||
const struct option long_opt[] =
|
||||
{
|
||||
{ "sqlite", 0, NULL, 's'},
|
||||
{ "mysql", 0, NULL, 'm'},
|
||||
{ "log", 0, NULL, 'l'},
|
||||
{ "help", 0, NULL, 'h'}
|
||||
};
|
||||
|
||||
int c;
|
||||
while ((c = getopt_long (argc, argv, "smlh", long_opt, NULL)) != -1)
|
||||
switch (c)
|
||||
{
|
||||
case 'm':
|
||||
sqlite_flag = false;
|
||||
break;
|
||||
case 'l':
|
||||
log_flag = true;
|
||||
break;
|
||||
case 'h':
|
||||
show_options();
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
CppUnit::TextUi::TestRunner tr;
|
||||
|
||||
tr.addTest(TemplateSQLTest::suite());
|
||||
|
||||
NebulaLog::init_log_system(NebulaLog::FILE, Log::DEBUG, "test.log");
|
||||
NebulaLog::log("Test", Log::INFO, "Test started");
|
||||
|
||||
|
||||
if (sqlite_flag)
|
||||
{
|
||||
TemplateSQLTest::mysql = false;
|
||||
NebulaLog::log("Test", Log::INFO, "Running Sqlite tests...");
|
||||
cout << "\nRunning Sqlite tests...\n";
|
||||
}
|
||||
else
|
||||
{
|
||||
TemplateSQLTest::mysql = true;
|
||||
NebulaLog::log("Test", Log::INFO, "Running MySQL tests...");
|
||||
cout << "\nRunning MySQL tests...\n";
|
||||
}
|
||||
|
||||
|
||||
tr.run();
|
||||
|
||||
|
||||
if (!log_flag)
|
||||
remove("test.log");
|
||||
|
||||
NebulaLog::finalize_log_system();
|
||||
|
||||
return 0;
|
||||
}
|
@ -80,6 +80,9 @@ main_env.Program('test','UserPoolTest.cc')
|
||||
main_env.Append(LIBPATH=["/usr/lib/mysql"])
|
||||
main_env.Append(CPPPATH=["/usr/include/mysql"])
|
||||
|
||||
# libxml2
|
||||
main_env.ParseConfig('xml2-config --libs --cflags')
|
||||
|
||||
sqlite=ARGUMENTS.get('sqlite', 'yes')
|
||||
if sqlite=='yes':
|
||||
main_env.Append(CPPFLAGS=["-DSQLITE_DB"])
|
||||
|
@ -40,7 +40,6 @@ source_files=[
|
||||
'vm_var_parser.c',
|
||||
'vm_var_syntax.cc',
|
||||
'VirtualMachinePool.cc',
|
||||
'VirtualMachineTemplate.cc',
|
||||
'VirtualMachineHook.cc'
|
||||
]
|
||||
|
||||
|
@ -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);
|
||||
|
||||
|
@ -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)";
|
||||
|
@ -80,6 +80,9 @@ main_env.Append(LIBS=[
|
||||
main_env.Append(LIBPATH=["/usr/lib/mysql"])
|
||||
main_env.Append(CPPPATH=["/usr/include/mysql"])
|
||||
|
||||
# libxml2
|
||||
main_env.ParseConfig('xml2-config --libs --cflags')
|
||||
|
||||
sqlite=ARGUMENTS.get('sqlite', 'yes')
|
||||
if sqlite=='yes':
|
||||
main_env.Append(CPPFLAGS=["-DSQLITE_DB"])
|
||||
|
@ -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";
|
||||
|
||||
@ -236,7 +237,7 @@ public:
|
||||
vm->set_state(VirtualMachine::ACTIVE);
|
||||
|
||||
// VirtualMachine object should be cached. Let's update the DB
|
||||
vmp->update_template_attribute(vm, attribute, value);
|
||||
vm->replace_template_attribute(attribute, value);
|
||||
|
||||
vmp->update(vm);
|
||||
|
||||
@ -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 );
|
||||
}
|
||||
|
@ -519,7 +519,7 @@ void VirtualMachineManagerDriver::protocol(
|
||||
|
||||
tiss >> val;
|
||||
|
||||
vmpool->update_template_attribute(vm,var,val);
|
||||
vm->replace_template_attribute(var,val);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -26,7 +26,6 @@ source_files=[
|
||||
'FixedLeases.cc',
|
||||
'RangedLeases.cc',
|
||||
'VirtualNetwork.cc',
|
||||
'VirtualNetworkTemplate.cc',
|
||||
'VirtualNetworkPool.cc',
|
||||
]
|
||||
|
||||
|
@ -69,24 +69,26 @@ VirtualNetwork::~VirtualNetwork()
|
||||
|
||||
const char * VirtualNetwork::table = "network_pool";
|
||||
|
||||
const char * VirtualNetwork::db_names = "(oid,uid,name,type,bridge,public)";
|
||||
const char * VirtualNetwork::db_names =
|
||||
"(oid,uid,name,type,bridge,public,template)";
|
||||
|
||||
const char * VirtualNetwork::db_bootstrap = "CREATE TABLE IF NOT EXISTS"
|
||||
" network_pool ("
|
||||
"oid INTEGER PRIMARY KEY, uid INTEGER, name VARCHAR(256), type INTEGER, "
|
||||
"bridge TEXT, public INTEGER, UNIQUE(name))";
|
||||
"bridge TEXT, public INTEGER, template TEXT, UNIQUE(name))";
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
||||
int VirtualNetwork::select_cb(void * nil, int num, char **values, char **names)
|
||||
{
|
||||
if ((!values[OID]) ||
|
||||
(!values[UID]) ||
|
||||
(!values[NAME]) ||
|
||||
(!values[TYPE]) ||
|
||||
(!values[BRIDGE]) ||
|
||||
(!values[PUBLIC]) ||
|
||||
if ((!values[OID]) ||
|
||||
(!values[UID]) ||
|
||||
(!values[NAME]) ||
|
||||
(!values[TYPE]) ||
|
||||
(!values[BRIDGE]) ||
|
||||
(!values[PUBLIC]) ||
|
||||
(!values[TEMPLATE]) ||
|
||||
(num != LIMIT ))
|
||||
{
|
||||
return -1;
|
||||
@ -103,8 +105,8 @@ int VirtualNetwork::select_cb(void * nil, int num, char **values, char **names)
|
||||
|
||||
public_vnet = atoi(values[PUBLIC]);
|
||||
|
||||
// Virtual Network template ID is the Network ID
|
||||
vn_template->id = oid;
|
||||
// Virtual Network template
|
||||
vn_template->from_xml(values[TEMPLATE]);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@ -142,21 +144,13 @@ int VirtualNetwork::select(SqlDB * db)
|
||||
goto error_id;
|
||||
}
|
||||
|
||||
//Get the template
|
||||
rc = vn_template->select(db);
|
||||
|
||||
if (rc != 0)
|
||||
{
|
||||
goto error_template;
|
||||
}
|
||||
|
||||
//Get the leases
|
||||
if (type == RANGED)
|
||||
{
|
||||
string nclass = "";
|
||||
int size = 0;
|
||||
|
||||
// retrieve specific information from template
|
||||
// retrieve specific information from the template
|
||||
get_template_attribute("NETWORK_ADDRESS",network_address);
|
||||
|
||||
if (network_address.empty())
|
||||
@ -174,7 +168,7 @@ int VirtualNetwork::select(SqlDB * db)
|
||||
{
|
||||
size = 254;
|
||||
}
|
||||
else if (!nclass.empty()) //Assume its a number
|
||||
else if (!nclass.empty()) //Assume it's a number
|
||||
{
|
||||
istringstream iss(nclass);
|
||||
iss >> size;
|
||||
@ -213,10 +207,6 @@ error_id:
|
||||
ose << "Error getting Virtual Network nid: " << oid;
|
||||
goto error_common;
|
||||
|
||||
error_template:
|
||||
ose << "Can not get template for Virtual Network nid: " << oid;
|
||||
goto error_common;
|
||||
|
||||
error_leases:
|
||||
ose << "Error getting Virtual Network leases nid: " << oid;
|
||||
goto error_common;
|
||||
@ -241,13 +231,14 @@ int VirtualNetwork::dump(ostringstream& oss,
|
||||
char ** values,
|
||||
char ** names)
|
||||
{
|
||||
if ((!values[OID]) ||
|
||||
(!values[UID]) ||
|
||||
(!values[NAME]) ||
|
||||
(!values[TYPE]) ||
|
||||
(!values[BRIDGE])||
|
||||
(!values[PUBLIC])||
|
||||
(!values[LIMIT]) ||
|
||||
if ((!values[OID]) ||
|
||||
(!values[UID]) ||
|
||||
(!values[NAME]) ||
|
||||
(!values[TYPE]) ||
|
||||
(!values[BRIDGE]) ||
|
||||
(!values[PUBLIC]) ||
|
||||
(!values[TEMPLATE]) ||
|
||||
(!values[LIMIT]) ||
|
||||
(num != LIMIT + 2 ))
|
||||
{
|
||||
return -1;
|
||||
@ -255,14 +246,15 @@ int VirtualNetwork::dump(ostringstream& oss,
|
||||
|
||||
oss <<
|
||||
"<VNET>" <<
|
||||
"<ID>" << values[OID] << "</ID>" <<
|
||||
"<UID>" << values[UID] << "</UID>" <<
|
||||
"<USERNAME>" << values[LIMIT+1] << "</USERNAME>" <<
|
||||
"<NAME>" << values[NAME] << "</NAME>" <<
|
||||
"<TYPE>" << values[TYPE] << "</TYPE>" <<
|
||||
"<BRIDGE>" << values[BRIDGE] << "</BRIDGE>" <<
|
||||
"<PUBLIC>" << values[PUBLIC] << "</PUBLIC>" <<
|
||||
"<TOTAL_LEASES>" << values[LIMIT]<< "</TOTAL_LEASES>" <<
|
||||
"<ID>" << values[OID] << "</ID>" <<
|
||||
"<UID>" << values[UID] << "</UID>" <<
|
||||
"<USERNAME>" << values[LIMIT+1] << "</USERNAME>" <<
|
||||
"<NAME>" << values[NAME] << "</NAME>" <<
|
||||
"<TYPE>" << values[TYPE] << "</TYPE>" <<
|
||||
"<BRIDGE>" << values[BRIDGE] << "</BRIDGE>" <<
|
||||
"<PUBLIC>" << values[PUBLIC] << "</PUBLIC>" <<
|
||||
"<TOTAL_LEASES>"<< values[LIMIT] << "</TOTAL_LEASES>"
|
||||
<< values[TEMPLATE] <<
|
||||
"</VNET>";
|
||||
|
||||
return 0;
|
||||
@ -332,20 +324,6 @@ int VirtualNetwork::insert(SqlDB * db)
|
||||
|
||||
vn_template->erase("PUBLIC");
|
||||
|
||||
// ------------ INSERT THE TEMPLATE --------------------
|
||||
|
||||
if ( vn_template->id == -1 )
|
||||
{
|
||||
vn_template->id = oid;
|
||||
}
|
||||
|
||||
rc = vn_template->insert(db);
|
||||
|
||||
if ( rc != 0 )
|
||||
{
|
||||
goto error_template;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
// Insert the Virtual Network
|
||||
//--------------------------------------------------------------------------
|
||||
@ -434,13 +412,8 @@ error_bridge:
|
||||
ose << "No BRIDGE in template for Virtual Network id " << oid;
|
||||
goto error_common;
|
||||
|
||||
error_template:
|
||||
ose << "Can not insert in DB template for Virtual Network id " << oid;
|
||||
goto error_common;
|
||||
|
||||
error_update:
|
||||
ose << "Can not update Virtual Network id " << oid;
|
||||
vn_template->drop(db);
|
||||
goto error_common;
|
||||
|
||||
error_addr:
|
||||
@ -472,21 +445,37 @@ int VirtualNetwork::insert_replace(SqlDB *db, bool replace)
|
||||
ostringstream oss;
|
||||
int rc;
|
||||
|
||||
char * sql_name = db->escape_str(name.c_str());
|
||||
|
||||
string xml_template;
|
||||
|
||||
char * sql_name;
|
||||
char * sql_bridge;
|
||||
char * sql_template;
|
||||
|
||||
|
||||
sql_name = db->escape_str(name.c_str());
|
||||
|
||||
if ( sql_name == 0 )
|
||||
{
|
||||
return -1;
|
||||
goto error_name;
|
||||
}
|
||||
|
||||
char * sql_bridge = db->escape_str(bridge.c_str());
|
||||
sql_bridge = db->escape_str(bridge.c_str());
|
||||
|
||||
if ( sql_bridge == 0 )
|
||||
{
|
||||
db->free_str(sql_name);
|
||||
return -1;
|
||||
goto error_bridge;
|
||||
}
|
||||
|
||||
vn_template->to_xml(xml_template);
|
||||
sql_template = db->escape_str(xml_template.c_str());
|
||||
|
||||
if ( sql_template == 0 )
|
||||
{
|
||||
goto error_template;
|
||||
}
|
||||
|
||||
|
||||
// Construct the SQL statement to Insert or Replace
|
||||
if(replace)
|
||||
{
|
||||
@ -503,14 +492,23 @@ int VirtualNetwork::insert_replace(SqlDB *db, bool replace)
|
||||
<< "'" << sql_name << "',"
|
||||
<< type << ","
|
||||
<< "'" << sql_bridge << "',"
|
||||
<< public_vnet << ")";
|
||||
<< public_vnet << ","
|
||||
<< "'" << sql_template<< "')";
|
||||
|
||||
rc = db->exec(oss);
|
||||
|
||||
db->free_str(sql_name);
|
||||
db->free_str(sql_bridge);
|
||||
db->free_str(sql_template);
|
||||
|
||||
return rc;
|
||||
|
||||
error_template:
|
||||
db->free_str(sql_bridge);
|
||||
error_bridge:
|
||||
db->free_str(sql_name);
|
||||
error_name:
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
@ -521,8 +519,6 @@ int VirtualNetwork::vn_drop(SqlDB * db)
|
||||
ostringstream oss;
|
||||
int rc;
|
||||
|
||||
vn_template->drop(db);
|
||||
|
||||
if ( leases != 0 )
|
||||
{
|
||||
leases->drop(db);
|
||||
|
@ -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 "VirtualNetworkTemplate.h"
|
||||
|
||||
const char * VirtualNetworkTemplate::table = "vn_template";
|
||||
|
||||
const char * VirtualNetworkTemplate::db_bootstrap = "CREATE TABLE IF NOT EXISTS"
|
||||
" vn_template (id INTEGER, name TEXT, type INTEGER, value TEXT)";
|
||||
|
@ -73,6 +73,9 @@ main_env.Append(LIBS=[
|
||||
main_env.Append(LIBPATH=["/usr/lib/mysql"])
|
||||
main_env.Append(CPPPATH=["/usr/include/mysql"])
|
||||
|
||||
# libxml2
|
||||
main_env.ParseConfig('xml2-config --libs --cflags')
|
||||
|
||||
sqlite=ARGUMENTS.get('sqlite', 'yes')
|
||||
if sqlite=='yes':
|
||||
main_env.Append(CPPFLAGS=["-DSQLITE_DB"])
|
||||
|
@ -77,10 +77,10 @@ const string xmls[] =
|
||||
};
|
||||
|
||||
const string xml_dump =
|
||||
"<VNET_POOL><VNET><ID>0</ID><UID>1</UID><USERNAME>A user</USERNAME><NAME>Net number one</NAME><TYPE>1</TYPE><BRIDGE>br1</BRIDGE><PUBLIC>0</PUBLIC><TOTAL_LEASES>0</TOTAL_LEASES></VNET><VNET><ID>1</ID><UID>2</UID><USERNAME>B user</USERNAME><NAME>A virtual network</NAME><TYPE>0</TYPE><BRIDGE>br0</BRIDGE><PUBLIC>1</PUBLIC><TOTAL_LEASES>0</TOTAL_LEASES></VNET></VNET_POOL>";
|
||||
"<VNET_POOL><VNET><ID>0</ID><UID>1</UID><USERNAME>A user</USERNAME><NAME>Net number one</NAME><TYPE>1</TYPE><BRIDGE>br1</BRIDGE><PUBLIC>0</PUBLIC><TOTAL_LEASES>0</TOTAL_LEASES><TEMPLATE><BRIDGE><![CDATA[br1]]></BRIDGE><LEASES><IP><![CDATA[130.10.0.1]]></IP><MAC><![CDATA[50:20:20:20:20:20]]></MAC></LEASES><NAME><![CDATA[Net number one]]></NAME><TYPE><![CDATA[FIXED]]></TYPE></TEMPLATE></VNET><VNET><ID>1</ID><UID>2</UID><USERNAME>B user</USERNAME><NAME>A virtual network</NAME><TYPE>0</TYPE><BRIDGE>br0</BRIDGE><PUBLIC>1</PUBLIC><TOTAL_LEASES>0</TOTAL_LEASES><TEMPLATE><BRIDGE><![CDATA[br0]]></BRIDGE><NAME><![CDATA[A virtual network]]></NAME><NETWORK_ADDRESS><![CDATA[192.168.0.0]]></NETWORK_ADDRESS><NETWORK_SIZE><![CDATA[C]]></NETWORK_SIZE><TYPE><![CDATA[RANGED]]></TYPE></TEMPLATE></VNET></VNET_POOL>";
|
||||
|
||||
const string xml_dump_where =
|
||||
"<VNET_POOL><VNET><ID>1</ID><UID>2</UID><USERNAME>B user</USERNAME><NAME>A virtual network</NAME><TYPE>0</TYPE><BRIDGE>br0</BRIDGE><PUBLIC>1</PUBLIC><TOTAL_LEASES>0</TOTAL_LEASES></VNET></VNET_POOL>";
|
||||
"<VNET_POOL><VNET><ID>1</ID><UID>2</UID><USERNAME>B user</USERNAME><NAME>A virtual network</NAME><TYPE>0</TYPE><BRIDGE>br0</BRIDGE><PUBLIC>1</PUBLIC><TOTAL_LEASES>0</TOTAL_LEASES><TEMPLATE><BRIDGE><![CDATA[br0]]></BRIDGE><NAME><![CDATA[A virtual network]]></NAME><NETWORK_ADDRESS><![CDATA[192.168.0.0]]></NETWORK_ADDRESS><NETWORK_SIZE><![CDATA[C]]></NETWORK_SIZE><TYPE><![CDATA[RANGED]]></TYPE></TEMPLATE></VNET></VNET_POOL>";
|
||||
|
||||
/* ************************************************************************* */
|
||||
/* ************************************************************************* */
|
||||
|
Loading…
Reference in New Issue
Block a user