mirror of
https://github.com/OpenNebula/one.git
synced 2025-03-26 06:50:09 +03:00
Initial commit of the Image pool
This commit is contained in:
parent
e86c0f07eb
commit
8dd2d1a031
390
include/Image.h
Normal file
390
include/Image.h
Normal file
@ -0,0 +1,390 @@
|
||||
/* ------------------------------------------------------------------------ */
|
||||
/* 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 IMAGE_H_
|
||||
#define IMAGE_H_
|
||||
|
||||
#include "PoolSQL.h"
|
||||
#include "ImageTemplate.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
/**
|
||||
* The Image class.
|
||||
*/
|
||||
class Image : public PoolObjectSQL
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* Type of Images
|
||||
*/
|
||||
enum ImageType
|
||||
{
|
||||
OS = 0,
|
||||
CDROM = 1,
|
||||
DATABLOCK = 2
|
||||
};
|
||||
|
||||
/**
|
||||
* Bus type
|
||||
*/
|
||||
enum BusType
|
||||
{
|
||||
IDE = 0,
|
||||
SCSI = 1
|
||||
};
|
||||
|
||||
/**
|
||||
* Function to write an Image on an output stream
|
||||
*/
|
||||
friend ostream& operator<<(ostream& os, Image& i);
|
||||
|
||||
// *************************************************************************
|
||||
// Virtual Network Public Methods
|
||||
// *************************************************************************
|
||||
|
||||
/**
|
||||
* Function to print the Image object into a string in plain text
|
||||
* @param str the resulting string
|
||||
* @return a reference to the generated string
|
||||
*/
|
||||
string& to_str(string& str) const;
|
||||
|
||||
/**
|
||||
* Function to print the Image object into a string in XML format
|
||||
* @param xml the resulting XML string
|
||||
* @return a reference to the generated string
|
||||
*/
|
||||
string& to_xml(string& xml) const;
|
||||
|
||||
/**
|
||||
* Get the Image unique identifier IID, that matches the OID of the object
|
||||
* @return IID Image identifier
|
||||
*/
|
||||
int get_iid() const
|
||||
{
|
||||
return oid;
|
||||
};
|
||||
|
||||
/**
|
||||
* Gets the uid of the owner of the Image
|
||||
* @return uid
|
||||
**/
|
||||
int get_uid()
|
||||
{
|
||||
return uid;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns Image's name
|
||||
* @return name Image's name
|
||||
*/
|
||||
const string& get_name() const
|
||||
{
|
||||
return name;
|
||||
};
|
||||
|
||||
/**
|
||||
* Function to drop an Image entry in image_pool
|
||||
* @return 0 on success
|
||||
*/
|
||||
int image_drop(SqlDB * db);
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
// Template
|
||||
// ------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Gets the values of a template attribute
|
||||
* @param name of the attribute
|
||||
* @param values of the attribute
|
||||
* @return the number of values
|
||||
*/
|
||||
int get_template_attribute(
|
||||
string& name,
|
||||
vector<const Attribute*>& values) const
|
||||
{
|
||||
return image_template.get(name,values);
|
||||
};
|
||||
|
||||
/**
|
||||
* Gets the values of a template attribute
|
||||
* @param name of the attribute
|
||||
* @param values of the attribute
|
||||
* @return the number of values
|
||||
*/
|
||||
int get_template_attribute(
|
||||
const char *name,
|
||||
vector<const Attribute*>& values) const
|
||||
{
|
||||
string str=name;
|
||||
return image_template.get(str,values);
|
||||
};
|
||||
|
||||
/**
|
||||
* Gets a string based Image attribute
|
||||
* @param name of the attribute
|
||||
* @param value of the attribute (a string), will be "" if not defined
|
||||
*/
|
||||
void get_template_attribute(
|
||||
const char * name,
|
||||
string& value) const
|
||||
{
|
||||
string str=name;
|
||||
image_template.get(str,value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a string based Image attribute
|
||||
* @param name of the attribute
|
||||
* @param value of the attribute (an int), will be 0 if not defined
|
||||
*/
|
||||
void get_template_attribute(
|
||||
const char * name,
|
||||
int& value) const
|
||||
{
|
||||
string str=name;
|
||||
image_template.get(str,value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets an Image single attribute
|
||||
* @param name of the attribute
|
||||
* @param value of the attribute (string)
|
||||
*/
|
||||
void set_template_attribute(
|
||||
const string& name,
|
||||
string value)
|
||||
{
|
||||
Attribute * single_attr;
|
||||
|
||||
single_attr = new SongleAttribute(name,value);
|
||||
|
||||
image_template->set(single_attr);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a string based Image attribute
|
||||
* @param name of the attribute
|
||||
* @param values of the attribute (map)
|
||||
*/
|
||||
void set_template_attribute(
|
||||
const string& name,
|
||||
map<string,string> value)
|
||||
{
|
||||
Attribute * vector_attr;
|
||||
|
||||
vector_attr = new VectorAttribute(name,value);
|
||||
|
||||
image_template->set(vector_attr);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes an Image attribute
|
||||
* @param name of the attribute
|
||||
*/
|
||||
int remove_template_attribute(
|
||||
const string& name)
|
||||
{
|
||||
int rc;
|
||||
vector<Attribute *> values;
|
||||
|
||||
rc = image_template->remove(name, values);
|
||||
|
||||
delete[] values;
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Friends
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
friend class ImagePool;
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Image Description
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Owner if the image
|
||||
*/
|
||||
int uid;
|
||||
|
||||
/**
|
||||
* The name of the Image
|
||||
*/
|
||||
string name;
|
||||
|
||||
/**
|
||||
* The description of the Image
|
||||
*/
|
||||
string description;
|
||||
|
||||
/**
|
||||
* Type of the Image
|
||||
*/
|
||||
ImageType type;
|
||||
|
||||
/**
|
||||
* Registration time
|
||||
*/
|
||||
time_t registration_time;
|
||||
|
||||
/**
|
||||
* Path to the image
|
||||
*/
|
||||
string source;
|
||||
|
||||
/**
|
||||
* Device for the image to be attached into
|
||||
*/
|
||||
string target;
|
||||
|
||||
/**
|
||||
* IDE or SCSI
|
||||
*/
|
||||
BusType bus;
|
||||
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Image Attributes
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* The Image template, holds the Image attributes.
|
||||
*/
|
||||
ImageTemplate image_template;
|
||||
|
||||
|
||||
// *************************************************************************
|
||||
// DataBase implementation (Private)
|
||||
// *************************************************************************
|
||||
|
||||
/**
|
||||
* Execute an INSERT or REPLACE Sql query.
|
||||
* @param db The SQL DB
|
||||
* @param replace Execute an INSERT or a REPLACE
|
||||
* @return 0 on success
|
||||
*/
|
||||
int insert_replace(SqlDB *db, bool replace);
|
||||
|
||||
/**
|
||||
* Callback function to unmarshall a Image object (Image::select)
|
||||
* @param num the number of columns read from the DB
|
||||
* @param names the column names
|
||||
* @param values the column values
|
||||
* @return 0 on success
|
||||
*/
|
||||
int select_cb(void *nil, int num, char **values, char **names);
|
||||
|
||||
/**
|
||||
* Bootstraps the database table(s) associated to the Image
|
||||
*/
|
||||
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);
|
||||
};
|
||||
|
||||
protected:
|
||||
|
||||
// *************************************************************************
|
||||
// Constructor
|
||||
// *************************************************************************
|
||||
|
||||
Image(int id=-1);
|
||||
|
||||
virtual ~Image();
|
||||
|
||||
// *************************************************************************
|
||||
// DataBase implementation
|
||||
// *************************************************************************
|
||||
|
||||
enum ColNames
|
||||
{
|
||||
OID = 0, /* Image identifier (IID) */
|
||||
UID = 1, /* Image owner id */
|
||||
NAME = 2, /* Image name */
|
||||
DESCRIPTION = 3, /* Image description */
|
||||
TYPE = 4, /* 0) OS 1) CDROM 2) DATABLOCK */
|
||||
REGTIME = 5, /* Time of registration */
|
||||
SOURCE = 6, /* Path to the image */
|
||||
TARGET = 7, /* Device to be plugged into */
|
||||
BUS = 8, /* 0) IDE 1) SCSI */
|
||||
LIMIT = 9
|
||||
};
|
||||
|
||||
static const char * db_names;
|
||||
|
||||
static const char * db_bootstrap;
|
||||
|
||||
static const char * table;
|
||||
|
||||
/**
|
||||
* Reads the Image (identified with its OID=IID) from the database.
|
||||
* @param db pointer to the db
|
||||
* @return 0 on success
|
||||
*/
|
||||
virtual int select(SqlDB *db);
|
||||
|
||||
/**
|
||||
* Writes the Image in the database.
|
||||
* @param db pointer to the db
|
||||
* @return 0 on success
|
||||
*/
|
||||
virtual int insert(SqlDB *db);
|
||||
|
||||
/**
|
||||
* Writes/updates the Images data fields in the database.
|
||||
* @param db pointer to the db
|
||||
* @return 0 on success
|
||||
*/
|
||||
virtual int update(SqlDB *db);
|
||||
|
||||
/**
|
||||
* Drops Image and associated template from the database
|
||||
* @param db pointer to the db
|
||||
* @return 0 on success
|
||||
*/
|
||||
int drop(SqlDB *db)
|
||||
{
|
||||
int rc;
|
||||
|
||||
rc = image_template.drop(db);
|
||||
|
||||
rc += image_drop(db);
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
/**
|
||||
* Function to output an Image object in to an stream in XML format
|
||||
* @param oss the output stream
|
||||
* @param num the number of columns read from the DB
|
||||
* @param names the column names
|
||||
* @param vaues the column values
|
||||
* @return 0 on success
|
||||
*/
|
||||
static int dump(ostringstream& oss, int num, char **values, char **names);
|
||||
};
|
||||
|
||||
#endif /*IMAGE_H_*/
|
160
include/ImagePool.h
Normal file
160
include/ImagePool.h
Normal file
@ -0,0 +1,160 @@
|
||||
/* -------------------------------------------------------------------------- */
|
||||
/* 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 IMAGE_POOL_H_
|
||||
#define IMAGE_POOL_H_
|
||||
|
||||
#include "PoolSQL.h"
|
||||
#include "Image.h"
|
||||
|
||||
#include <time.h>
|
||||
#include <sstream>
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
/**
|
||||
* The Image Pool class.
|
||||
*/
|
||||
class ImagePool : public PoolSQL
|
||||
{
|
||||
public:
|
||||
|
||||
ImagePool(SqlDB * db):PoolSQL(db,Image::table){};
|
||||
|
||||
~ImagePool(){};
|
||||
|
||||
/**
|
||||
* Function to allocate a new Image object
|
||||
* @param uid the user id of the image's owner
|
||||
* @param stemplate template associated with the image
|
||||
* @param oid the id assigned to the Image
|
||||
* @return the oid assigned to the object,
|
||||
* -1 in case of failure
|
||||
* -2 in case of template parse failure
|
||||
*/
|
||||
int allocate (
|
||||
int uid,
|
||||
const string& stemplate,
|
||||
int * oid);
|
||||
|
||||
/**
|
||||
* Function to get a Image from the pool, if the object is not in memory
|
||||
* it is loaded from the DB
|
||||
* @param oid Image unique id
|
||||
* @param lock locks the Image mutex
|
||||
* @return a pointer to the Image, 0 if the Image could not be loaded
|
||||
*/
|
||||
Image * get(
|
||||
int oid,
|
||||
bool lock)
|
||||
{
|
||||
return static_cast<Image *>(PoolSQL::get(oid,lock));
|
||||
};
|
||||
|
||||
/**
|
||||
* Function to get an Image from the pool using the image name
|
||||
* @param name of the image
|
||||
* @param lock locks the User mutex
|
||||
* @return a pointer to the Image, 0 if the User could not be loaded
|
||||
*/
|
||||
Image * get(
|
||||
string name,
|
||||
bool lock)
|
||||
{
|
||||
map<string, int>::iterator index;
|
||||
|
||||
index = known_users.find(username);
|
||||
|
||||
if ( index != known_users.end() )
|
||||
{
|
||||
return get((int)index->second,lock);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/** Update a particular Image
|
||||
* @param image pointer to Image
|
||||
* @return 0 on success
|
||||
*/
|
||||
int update(Image * image)
|
||||
{
|
||||
return image->update(db);
|
||||
};
|
||||
|
||||
/** Drops an image from the DB, the image mutex MUST BE locked
|
||||
* @param image pointer to Image
|
||||
*/
|
||||
int drop(Image * image)
|
||||
{
|
||||
int rc = PoolSQL::drop(image);
|
||||
|
||||
if ( rc == 0)
|
||||
{
|
||||
image_names.erase(image->get_name());
|
||||
}
|
||||
|
||||
return rc;
|
||||
};
|
||||
|
||||
/**
|
||||
* Bootstraps the database table(s) associated to the Image pool
|
||||
*/
|
||||
static void bootstrap(SqlDB *_db)
|
||||
{
|
||||
Image::bootstrap(_db);
|
||||
};
|
||||
|
||||
/**
|
||||
* Dumps the Image pool in XML format. A filter can be also added to the
|
||||
* query
|
||||
* @param oss the output stream to dump the pool contents
|
||||
* @param where filter for the objects, defaults to all
|
||||
* @return 0 on success
|
||||
*/
|
||||
int dump(ostringstream& oss, const string& where);
|
||||
|
||||
private:
|
||||
/**
|
||||
* This map stores the association between IIDs and Image names
|
||||
*/
|
||||
map<string, int> image_names;
|
||||
|
||||
/**
|
||||
* Factory method to produce Image objects
|
||||
* @return a pointer to the new Image
|
||||
*/
|
||||
PoolObjectSQL * create()
|
||||
{
|
||||
return new Image;
|
||||
};
|
||||
|
||||
/**
|
||||
* Callback function to get output the image pool in XML format
|
||||
* (Image::dump)
|
||||
* @param num the number of columns read from the DB
|
||||
* @param names the column names
|
||||
* @param vaues the column values
|
||||
* @return 0 on success
|
||||
*/
|
||||
int dump_cb(void * _oss, int num, char **values, char **names);
|
||||
};
|
||||
|
||||
#endif /*IMAGE_POOL_H_*/
|
47
include/ImageTemplate.h
Normal file
47
include/ImageTemplate.h
Normal file
@ -0,0 +1,47 @@
|
||||
/* -------------------------------------------------------------------------- */
|
||||
/* 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 IMAGE_TEMPLATE_H_
|
||||
#define IMAGE_TEMPLATE_H_
|
||||
|
||||
#include "TemplateSQL.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
/**
|
||||
* Image Template class, it represents the attributes of a Host
|
||||
*/
|
||||
class ImageTemplate : public TemplateSQL
|
||||
{
|
||||
public:
|
||||
ImageTemplate(int tid = -1,
|
||||
const char separator = '='):
|
||||
TemplateSQL(table,tid,true,separator,"TEMPLATE"){};
|
||||
|
||||
~ImageTemplate(){};
|
||||
|
||||
private:
|
||||
friend class Image;
|
||||
|
||||
static const char * table;
|
||||
|
||||
static const char * db_bootstrap;
|
||||
};
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
||||
#endif /*IMAGE_TEMPLATE_H_*/
|
452
src/image/Image.cc
Normal file
452
src/image/Image.cc
Normal file
@ -0,0 +1,452 @@
|
||||
/* ------------------------------------------------------------------------ */
|
||||
/* 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 <limits.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
|
||||
#include "Image.h"
|
||||
|
||||
/* ************************************************************************ */
|
||||
/* Image :: Constructor/Destructor */
|
||||
/* ************************************************************************ */
|
||||
|
||||
Image::Image(int _uid):
|
||||
PoolObjectSQL(-1),
|
||||
uid(_uid),
|
||||
name(""),
|
||||
description(""),
|
||||
type(0),
|
||||
registration_time(time(0)),
|
||||
source(""),
|
||||
target(""),
|
||||
bus(0)
|
||||
{};
|
||||
|
||||
Image::~Image(){};
|
||||
|
||||
/* ************************************************************************ */
|
||||
/* Image :: Database Access Functions */
|
||||
/* ************************************************************************ */
|
||||
|
||||
const char * Image::table = "image_pool";
|
||||
|
||||
const char * Image::db_names = "(oid, uid, name, description, type, regtime,"
|
||||
"source, target, bus)";
|
||||
|
||||
const char * Image::db_bootstrap = "CREATE TABLE IF NOT EXISTS image_pool ("
|
||||
"oid INTEGER PRIMARY KEY, uid INTEGER, name VARCHAR(128), "
|
||||
"description TEXT, type INTEGER, regtime INTEGER, "
|
||||
"source VARCHAR, target VARCHAR, bus INTEGER, UNIQUE(name) )";
|
||||
|
||||
/* ------------------------------------------------------------------------ */
|
||||
/* ------------------------------------------------------------------------ */
|
||||
|
||||
int Image::select_cb(void * nil, int num, char **values, char ** names)
|
||||
{
|
||||
if ((!values[OID]) ||
|
||||
(!values[UID]) ||
|
||||
(!values[NAME]) ||
|
||||
(!values[DESCRIPTION]) ||
|
||||
(!values[TYPE]) ||
|
||||
(!values[REGTIME]) ||
|
||||
(!values[SOURCE]) ||
|
||||
(!values[TARGET]) ||
|
||||
(!values[BUS]) ||
|
||||
(num != LIMIT ))
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
oid = atoi(values[OID]);
|
||||
uid = atoi(values[UID]);
|
||||
|
||||
name = values[NAME];
|
||||
description = values[DESCRIPTION];
|
||||
|
||||
type = static_cast<ImageType>(atoi(values[TYPE]));
|
||||
regtime = static_cast<time_t>(atoi(values[REGTIME]));
|
||||
|
||||
source = values[SOURCE];
|
||||
target = values[TARGET];
|
||||
|
||||
bus = static_cast<BusType>(atoi(values[BUS]));
|
||||
|
||||
image_template.id = oid;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------ */
|
||||
|
||||
int Image::select(SqlDB *db)
|
||||
{
|
||||
ostringstream oss;
|
||||
int rc;
|
||||
int boid;
|
||||
|
||||
set_callback(static_cast<Callbackable::Callback>(&Image::select_cb));
|
||||
|
||||
oss << "SELECT * FROM " << table << " WHERE oid = " << oid;
|
||||
|
||||
boid = oid;
|
||||
oid = -1;
|
||||
|
||||
rc = db->exec(oss, this);
|
||||
|
||||
if ((rc != 0) || (oid != boid ))
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Get the template
|
||||
|
||||
rc = image_template.select(db);
|
||||
|
||||
if ( rc != 0 )
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/* ------------------------------------------------------------------------ */
|
||||
/* ------------------------------------------------------------------------ */
|
||||
|
||||
int Image::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;
|
||||
}
|
||||
|
||||
//Insert the Host
|
||||
rc = insert_replace(db, false);
|
||||
|
||||
if ( rc != 0 )
|
||||
{
|
||||
host_template.drop(db);
|
||||
host_share.drop(db);
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------ */
|
||||
/* ------------------------------------------------------------------------ */
|
||||
|
||||
int Host::update(SqlDB *db)
|
||||
{
|
||||
int rc;
|
||||
|
||||
// Update the Template
|
||||
rc = host_template.update(db);
|
||||
|
||||
if ( rc != 0 )
|
||||
{
|
||||
return rc;
|
||||
}
|
||||
|
||||
// Update the HostShare
|
||||
rc = host_share.update(db);
|
||||
|
||||
if ( rc != 0 )
|
||||
{
|
||||
return rc;
|
||||
}
|
||||
|
||||
rc = insert_replace(db, true);
|
||||
|
||||
if ( rc != 0 )
|
||||
{
|
||||
return rc;
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------ */
|
||||
/* ------------------------------------------------------------------------ */
|
||||
|
||||
int Host::insert_replace(SqlDB *db, bool replace)
|
||||
{
|
||||
ostringstream oss;
|
||||
|
||||
int rc;
|
||||
|
||||
char * sql_hostname;
|
||||
char * sql_im_mad_name;
|
||||
char * sql_tm_mad_name;
|
||||
char * sql_vmm_mad_name;
|
||||
|
||||
// Update the Host
|
||||
|
||||
sql_hostname = db->escape_str(hostname.c_str());
|
||||
|
||||
if ( sql_hostname == 0 )
|
||||
{
|
||||
goto error_hostname;
|
||||
}
|
||||
|
||||
sql_im_mad_name = db->escape_str(im_mad_name.c_str());
|
||||
|
||||
if ( sql_im_mad_name == 0 )
|
||||
{
|
||||
goto error_im;
|
||||
}
|
||||
|
||||
sql_tm_mad_name = db->escape_str(tm_mad_name.c_str());
|
||||
|
||||
if ( sql_tm_mad_name == 0 )
|
||||
{
|
||||
goto error_tm;
|
||||
}
|
||||
|
||||
sql_vmm_mad_name = db->escape_str(vmm_mad_name.c_str());
|
||||
|
||||
if ( sql_vmm_mad_name == 0 )
|
||||
{
|
||||
goto error_vmm;
|
||||
}
|
||||
|
||||
if(replace)
|
||||
{
|
||||
oss << "REPLACE";
|
||||
}
|
||||
else
|
||||
{
|
||||
oss << "INSERT";
|
||||
}
|
||||
|
||||
// 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 << ")";
|
||||
|
||||
rc = db->exec(oss);
|
||||
|
||||
db->free_str(sql_hostname);
|
||||
db->free_str(sql_im_mad_name);
|
||||
db->free_str(sql_tm_mad_name);
|
||||
db->free_str(sql_vmm_mad_name);
|
||||
|
||||
return rc;
|
||||
|
||||
error_vmm:
|
||||
db->free_str(sql_tm_mad_name);
|
||||
error_tm:
|
||||
db->free_str(sql_im_mad_name);
|
||||
error_im:
|
||||
db->free_str(sql_hostname);
|
||||
error_hostname:
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------ */
|
||||
/* ------------------------------------------------------------------------ */
|
||||
|
||||
int Host::dump(ostringstream& oss, int num, char **values, char **names)
|
||||
{
|
||||
if ((!values[OID]) ||
|
||||
(!values[HOST_NAME]) ||
|
||||
(!values[STATE]) ||
|
||||
(!values[IM_MAD]) ||
|
||||
(!values[VM_MAD]) ||
|
||||
(!values[TM_MAD]) ||
|
||||
(!values[LAST_MON_TIME]) ||
|
||||
(num != LIMIT + HostShare::LIMIT ))
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
oss <<
|
||||
"<HOST>" <<
|
||||
"<ID>" << values[OID] <<"</ID>" <<
|
||||
"<NAME>" << values[HOST_NAME] <<"</NAME>" <<
|
||||
"<STATE>" << values[STATE] <<"</STATE>" <<
|
||||
"<IM_MAD>" << values[IM_MAD] <<"</IM_MAD>" <<
|
||||
"<VM_MAD>" << values[VM_MAD] <<"</VM_MAD>" <<
|
||||
"<TM_MAD>" << values[TM_MAD] <<"</TM_MAD>" <<
|
||||
"<LAST_MON_TIME>"<< values[LAST_MON_TIME]<<"</LAST_MON_TIME>";
|
||||
|
||||
HostShare::dump(oss,num - LIMIT, values + LIMIT, names + LIMIT);
|
||||
|
||||
oss << "</HOST>";
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------ */
|
||||
/* ------------------------------------------------------------------------ */
|
||||
|
||||
int Host::drop(SqlDB * db)
|
||||
{
|
||||
ostringstream oss;
|
||||
int rc;
|
||||
|
||||
host_template.drop(db);
|
||||
|
||||
host_share.drop(db);
|
||||
|
||||
oss << "DELETE FROM " << table << " WHERE oid=" << oid;
|
||||
|
||||
rc = db->exec(oss);
|
||||
|
||||
if ( rc == 0 )
|
||||
{
|
||||
set_valid(false);
|
||||
}
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------ */
|
||||
/* ------------------------------------------------------------------------ */
|
||||
|
||||
int Host::update_info(string &parse_str)
|
||||
{
|
||||
char * error_msg;
|
||||
int rc;
|
||||
|
||||
rc = host_template.parse(parse_str, &error_msg);
|
||||
|
||||
if ( rc != 0 )
|
||||
{
|
||||
NebulaLog::log("ONE", Log::ERROR, error_msg);
|
||||
|
||||
free(error_msg);
|
||||
return -1;
|
||||
}
|
||||
|
||||
get_template_attribute("TOTALCPU",host_share.max_cpu);
|
||||
get_template_attribute("TOTALMEMORY",host_share.max_mem);
|
||||
|
||||
get_template_attribute("FREECPU",host_share.free_cpu);
|
||||
get_template_attribute("FREEMEMORY",host_share.free_mem);
|
||||
|
||||
get_template_attribute("USEDCPU",host_share.used_cpu);
|
||||
get_template_attribute("USEDMEMORY",host_share.used_mem);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* ************************************************************************ */
|
||||
/* Host :: Misc */
|
||||
/* ************************************************************************ */
|
||||
|
||||
ostream& operator<<(ostream& os, Host& host)
|
||||
{
|
||||
string host_str;
|
||||
|
||||
os << host.to_xml(host_str);
|
||||
|
||||
return os;
|
||||
};
|
||||
|
||||
|
||||
/* ------------------------------------------------------------------------ */
|
||||
/* ------------------------------------------------------------------------ */
|
||||
|
||||
string& Host::to_xml(string& xml) const
|
||||
{
|
||||
string template_xml;
|
||||
string share_xml;
|
||||
ostringstream oss;
|
||||
|
||||
oss <<
|
||||
"<HOST>"
|
||||
"<ID>" << oid << "</ID>" <<
|
||||
"<NAME>" << hostname << "</NAME>" <<
|
||||
"<STATE>" << state << "</STATE>" <<
|
||||
"<IM_MAD>" << im_mad_name << "</IM_MAD>" <<
|
||||
"<VM_MAD>" << vmm_mad_name << "</VM_MAD>" <<
|
||||
"<TM_MAD>" << tm_mad_name << "</TM_MAD>" <<
|
||||
"<LAST_MON_TIME>" << last_monitored << "</LAST_MON_TIME>" <<
|
||||
host_share.to_xml(share_xml) <<
|
||||
host_template.to_xml(template_xml) <<
|
||||
"</HOST>";
|
||||
|
||||
xml = oss.str();
|
||||
|
||||
return xml;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------ */
|
||||
/* ------------------------------------------------------------------------ */
|
||||
|
||||
string& Host::to_str(string& str) const
|
||||
{
|
||||
string template_str;
|
||||
string share_str;
|
||||
|
||||
ostringstream os;
|
||||
|
||||
os <<
|
||||
"ID = " << oid << endl <<
|
||||
"NAME = " << hostname << endl <<
|
||||
"STATE = " << state << endl <<
|
||||
"IM MAD = " << im_mad_name << endl <<
|
||||
"VMM MAD = " << vmm_mad_name << endl <<
|
||||
"TM MAD = " << tm_mad_name << endl <<
|
||||
"LAST_MON = " << last_monitored << endl <<
|
||||
"ATTRIBUTES" << endl << host_template.to_str(template_str) << endl <<
|
||||
"HOST SHARES" << endl << host_share.to_str(share_str) <<endl;
|
||||
|
||||
str = os.str();
|
||||
|
||||
return str;
|
||||
}
|
122
src/image/ImagePool.cc
Normal file
122
src/image/ImagePool.cc
Normal file
@ -0,0 +1,122 @@
|
||||
/* -------------------------------------------------------------------------- */
|
||||
/* 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. */
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
||||
/* ************************************************************************** */
|
||||
/* Image Pool */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "ImagePool.h"
|
||||
|
||||
int ImagePool::allocate (
|
||||
int uid,
|
||||
const string& stemplate,
|
||||
int * oid)
|
||||
{
|
||||
Image * img;
|
||||
string name;
|
||||
|
||||
char * error_msg;
|
||||
int rc;
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
// Build a new Image object
|
||||
// ---------------------------------------------------------------------
|
||||
img = new Image(uid);
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
// Parse template
|
||||
// ---------------------------------------------------------------------
|
||||
rc = img->image_template.parse(stemplate,&error_msg);
|
||||
|
||||
if ( rc != 0 )
|
||||
{
|
||||
ostringstream oss;
|
||||
|
||||
oss << "ImagePool template parse error: " << error_msg;
|
||||
NebulaLog::log("ONE", Log::ERROR, oss);
|
||||
free(error_msg);
|
||||
|
||||
delete img;
|
||||
|
||||
return -2;
|
||||
}
|
||||
|
||||
img->get_template_attribute("NAME",name);
|
||||
|
||||
if ( name.empty() == true )
|
||||
{
|
||||
oss.str("");
|
||||
oss << "image-" << oid;
|
||||
name = oss.str();
|
||||
img->set_template_attribute("NAME", name);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
// Insert the Object in the pool
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
*oid = PoolSQL::allocate(img);
|
||||
|
||||
if ( *oid == -1 )
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Add the image name to the map of image_names
|
||||
image_names.insert(make_pair(name,*oid));
|
||||
|
||||
|
||||
return *oid;
|
||||
}
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
||||
int ImagePool::dump_cb(void * _oss, int num, char **values, char **names)
|
||||
{
|
||||
ostringstream * oss;
|
||||
|
||||
oss = static_cast<ostringstream *>(_oss);
|
||||
|
||||
return Image::dump(*oss, num, values, names);
|
||||
}
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
||||
int ImagePool::dump(ostringstream& oss, const string& where)
|
||||
{
|
||||
int rc;
|
||||
ostringstream cmd;
|
||||
|
||||
oss << "<IMAGE_POOL>";
|
||||
|
||||
set_callback(static_cast<Callbackable::Callback>(&HostPool::dump_cb),
|
||||
static_cast<void *>(&oss));
|
||||
|
||||
cmd << "SELECT * FROM " << Image::table;
|
||||
|
||||
if ( !where.empty() )
|
||||
{
|
||||
cmd << " WHERE " << where;
|
||||
}
|
||||
|
||||
rc = db->exec(cmd, this);
|
||||
|
||||
oss << "</IMAGE_POOL>";
|
||||
|
||||
return rc;
|
||||
}
|
22
src/image/ImageTemplate.cc
Normal file
22
src/image/ImageTemplate.cc
Normal file
@ -0,0 +1,22 @@
|
||||
/* -------------------------------------------------------------------------- */
|
||||
/* 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)";
|
Loading…
x
Reference in New Issue
Block a user