From 8faeadaf72140d5613867600a66b67400d6143d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Tue, 3 Aug 2010 16:22:48 +0200 Subject: [PATCH] feature #282: ImageTemplate moved from its own table to a image_pool column. --- include/Image.h | 30 +++++++-- include/ImagePool.h | 8 +-- include/ImageTemplate.h | 16 +---- src/image/Image.cc | 97 +++++++++++++---------------- src/image/ImageTemplate.cc | 22 ------- src/image/SConstruct | 1 - src/rm/RequestManagerImageUpdate.cc | 9 ++- 7 files changed, 79 insertions(+), 104 deletions(-) delete mode 100644 src/image/ImageTemplate.cc diff --git a/include/Image.h b/include/Image.h index 1fba0dafe5..c51474d15f 100644 --- a/include/Image.h +++ b/include/Image.h @@ -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; diff --git a/include/ImagePool.h b/include/ImagePool.h index d7fd446ec0..505a43fb3b 100644 --- a/include/ImagePool.h +++ b/include/ImagePool.h @@ -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); } /** diff --git a/include/ImageTemplate.h b/include/ImageTemplate.h index 8a999aa9f4..17b8502dbd 100644 --- a/include/ImageTemplate.h +++ b/include/ImageTemplate.h @@ -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; }; /* -------------------------------------------------------------------------- */ diff --git a/src/image/Image.cc b/src/image/Image.cc index fbe3bd6b5d..d33e2ce895 100644 --- a/src/image/Image.cc +++ b/src/image/Image.cc @@ -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) "" << values[SOURCE] << "" << "" << values[STATE] << "" << "" << values[RUNNING_VMS] << "" << + "" << ""; 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); diff --git a/src/image/ImageTemplate.cc b/src/image/ImageTemplate.cc deleted file mode 100644 index 2daa1d7bba..0000000000 --- a/src/image/ImageTemplate.cc +++ /dev/null @@ -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)"; diff --git a/src/image/SConstruct b/src/image/SConstruct index 94de312801..f98729c3c0 100644 --- a/src/image/SConstruct +++ b/src/image/SConstruct @@ -22,7 +22,6 @@ lib_name='nebula_image' # Sources to generate the library source_files=[ - 'ImageTemplate.cc', 'Image.cc', 'ImagePool.cc' ] diff --git a/src/rm/RequestManagerImageUpdate.cc b/src/rm/RequestManagerImageUpdate.cc index acc6eef587..0c90b01a9b 100644 --- a/src/rm/RequestManagerImageUpdate.cc +++ b/src/rm/RequestManagerImageUpdate.cc @@ -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 ) {