1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-03-23 22:50:09 +03:00

feature #282: ImageTemplate moved from its own table to a image_pool column.

This commit is contained in:
Carlos Martín 2010-08-03 16:22:48 +02:00 committed by Ruben S. Montero
parent 24987794c5
commit 8faeadaf72
7 changed files with 79 additions and 104 deletions

View File

@ -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);
}
/**
* Updates the template, adding a new attribute (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 update_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;

View File

@ -126,14 +126,12 @@ public:
* @param new value for the attribute
* @return 0 on success, -1 otherwise
*/
int replace_attribute(
int update_template_attribute(
Image * image,
const string& name,
const string& value)
{
SingleAttribute * sattr = new SingleAttribute(name,value);
return image->image_template->replace_attribute(db,sattr);
return image->update_template_attribute(name, value);
}
/** Delete an image attribute in the template (Image MUST be locked)
@ -145,7 +143,7 @@ public:
Image * image,
const string& name)
{
return image->image_template->remove_attribute(db, name);
return image->remove_template_attribute(name);
}
/**

View File

@ -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;
};
/* -------------------------------------------------------------------------- */

View File

@ -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>" <<
"<TEMPLATE>" << values[TEMPLATE] << "</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);

View File

@ -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)";

View File

@ -22,7 +22,6 @@ lib_name='nebula_image'
# Sources to generate the library
source_files=[
'ImageTemplate.cc',
'Image.cc',
'ImagePool.cc'
]

View File

@ -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 = ImageUpdate::ipool->update_template_attribute(image, name, value);
if(rc == 0)
{
rc = ImageUpdate::ipool->update(image);
}
if ( rc < 0 )
{