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

feature-#200: Finishing image pool

This commit is contained in:
Constantino Vázquez Blanco and Carlos Martín 2010-05-25 18:19:22 +02:00 committed by Constantino Vázquez Blanco
parent 484013d9b1
commit 6b320815b4
6 changed files with 211 additions and 75 deletions

View File

@ -52,6 +52,7 @@ main_env.Append(LIBPATH=[
cwd+'/src/tm', cwd+'/src/tm',
cwd+'/src/dm', cwd+'/src/dm',
cwd+'/src/im', cwd+'/src/im',
cwd+'/src/image',
cwd+'/src/rm', cwd+'/src/rm',
cwd+'/src/vnm', cwd+'/src/vnm',
cwd+'/src/hm', cwd+'/src/hm',
@ -154,6 +155,7 @@ build_scripts=[
'src/rm/SConstruct', 'src/rm/SConstruct',
'src/tm/SConstruct', 'src/tm/SConstruct',
'src/im/SConstruct', 'src/im/SConstruct',
'src/image/SConstruct',
'src/dm/SConstruct', 'src/dm/SConstruct',
'src/scheduler/SConstruct', 'src/scheduler/SConstruct',
'src/vnm/SConstruct', 'src/vnm/SConstruct',

View File

@ -108,6 +108,58 @@ public:
return name; return name;
}; };
/**
* Set enum type
* @return 0 on success, -1 otherwise
*/
int set_type(string _type)
{
int rc = 0;
if ( _type == "OS" )
{
type = OS;
}
else if ( _type == "CDROM" )
{
type = CDROM;
}
else if ( _type == "DATABLOCK" )
{
type = DATABLOCK;
}
else
{
rc = -1;
}
return rc;
}
/**
* Set enum bus
* @return 0 on success, -1 otherwise
*/
int set_bus(string _bus)
{
int rc = 0;
if ( _bus == "IDE" )
{
bus = IDE;
}
else if ( _bus == "SCSI" )
{
bus = SCSI;
}
else
{
rc = -1;
}
return rc;
}
/** /**
* Get an image to be used in a VM * Get an image to be used in a VM
* @param overwrite true if the image is going to be overwritten * @param overwrite true if the image is going to be overwritten
@ -190,9 +242,9 @@ public:
{ {
Attribute * single_attr; Attribute * single_attr;
single_attr = new SongleAttribute(name,value); single_attr = new SingleAttribute(name,value);
image_template->set(single_attr); image_template.set(single_attr);
} }
/** /**
@ -208,7 +260,7 @@ public:
vector_attr = new VectorAttribute(name,value); vector_attr = new VectorAttribute(name,value);
image_template->set(vector_attr); image_template.set(vector_attr);
} }
/** /**
@ -220,10 +272,17 @@ public:
{ {
int rc; int rc;
vector<Attribute *> values; vector<Attribute *> values;
Attribute * value;
rc = image_template->remove(name, values); rc = image_template.remove(name, values);
delete[] values; // Delete all pointers in the vector
while (!values.empty())
{
value = values.front(); // Gets the pointer
values.erase(values.begin()); // Removes it from the vector
delete value; // Frees memory
}
return rc; return rc;
} }
@ -263,7 +322,7 @@ private:
/** /**
* Registration time * Registration time
*/ */
time_t registration_time; time_t regtime;
/** /**
* Path to the image * Path to the image
@ -396,16 +455,7 @@ protected:
* @param db pointer to the db * @param db pointer to the db
* @return 0 on success * @return 0 on success
*/ */
int drop(SqlDB *db) virtual 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 * Function to output an Image object in to an stream in XML format

View File

@ -19,12 +19,12 @@
#include "PoolSQL.h" #include "PoolSQL.h"
#include "Image.h" #include "Image.h"
#include "NebulaLog.h"
#include <time.h> #include <time.h>
#include <sstream> #include <sstream>
#include <iostream> #include <iostream>
#include <vector> #include <vector>
using namespace std; using namespace std;
@ -36,7 +36,38 @@ class ImagePool : public PoolSQL
{ {
public: public:
ImagePool(SqlDB * db):PoolSQL(db,Image::table){}; ImagePool(SqlDB * db,
const string& _source_prefix,
const string& _default_type,
const string& _default_bus):
PoolSQL(db,Image::table),
source_prefix(_source_prefix)
{
if (_default_type != "OS" ||
_default_type != "CDROM" ||
_default_type != "DATABLOCK")
{
NebulaLog::log("IMG", Log::ERROR,
"Bad default for image type, setting OS");
default_type = "OS";
}
else
{
default_type = _default_type;
}
if (_default_bus != "IDE" ||
_default_bus != "SCSI")
{
NebulaLog::log("IMG", Log::ERROR,
"Bad default for bus type, setting IDE");
default_bus = "IDE";
}
else
{
default_bus = _default_bus;
}
};
~ImagePool(){}; ~ImagePool(){};
@ -137,6 +168,21 @@ private:
*/ */
map<string, int> image_names; map<string, int> image_names;
/**
* Path to the image repository
**/
string source_prefix;
/**
* Default image type
**/
string default_type;
/**
* Default bus type
**/
string default_bus;
/** /**
* Factory method to produce Image objects * Factory method to produce Image objects
* @return a pointer to the new Image * @return a pointer to the new Image
@ -155,6 +201,13 @@ private:
* @return 0 on success * @return 0 on success
*/ */
int dump_cb(void * _oss, int num, char **values, char **names); int dump_cb(void * _oss, int num, char **values, char **names);
/**
* "Encrypts" the password with SHA1 digest
* @param password
* @return sha1 encrypted password
*/
string sha1_digest(const string& pass);
}; };
#endif /*IMAGE_POOL_H_*/ #endif /*IMAGE_POOL_H_*/

View File

@ -21,6 +21,7 @@
#include "PoolSQL.h" #include "PoolSQL.h"
#include "History.h" #include "History.h"
#include "Log.h" #include "Log.h"
#include "NebulaLog.h"
#include <time.h> #include <time.h>
#include <sstream> #include <sstream>
@ -1041,24 +1042,14 @@ protected:
virtual int update(SqlDB * db); virtual int update(SqlDB * db);
/** /**
* Deletes a VM from the database and all its associated information: * Deletes a VM from the database and all its associated information
* - History records
* - VM template
* @param db pointer to the db * @param db pointer to the db
* @return 0 on success * @return -1
*/ */
virtual int drop(SqlDB * db) virtual int drop(SqlDB * db)
{ {
int rc; NebulaLog::log("ONE",Log::ERROR, "VM Drop not implemented!");
return -1;
rc = vm_template.drop(db);
if ( history != 0 )
{
rc += history->drop(db);
}
return rc;
} }
/** /**

View File

@ -31,11 +31,11 @@ Image::Image(int _uid):
uid(_uid), uid(_uid),
name(""), name(""),
description(""), description(""),
type(0), type(OS),
registration_time(time(0)), regtime(time(0)),
source(""), source(""),
target(""), target(""),
bus(0), bus(IDE),
state(INIT) state(INIT)
{}; {};
@ -421,8 +421,8 @@ string& Image::to_str(string& str) const
/* ------------------------------------------------------------------------ */ /* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */ /* ------------------------------------------------------------------------ */
// TODO update?
bool get_image(bool overwrite) bool Image::get_image(bool overwrite)
{ {
if ( state == READY || state == USED ) if ( state == READY || state == USED )
{ {
@ -446,8 +446,8 @@ bool get_image(bool overwrite)
/* ------------------------------------------------------------------------ */ /* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */ /* ------------------------------------------------------------------------ */
// TODO update?
void release_image() void Image::release_image()
{ {
running_vms--; running_vms--;

View File

@ -19,6 +19,8 @@
/* ************************************************************************** */ /* ************************************************************************** */
#include "ImagePool.h" #include "ImagePool.h"
#include <openssl/evp.h>
#include <iomanip>
int ImagePool::allocate ( int ImagePool::allocate (
int uid, int uid,
@ -26,11 +28,16 @@ int ImagePool::allocate (
int * oid) int * oid)
{ {
Image * img; Image * img;
string name; string name = "";
string type; string description = "";
string original_path; string source = "";
string target; string type = "";
string bus; string original_path = "";
string target = "";
string bus = "";
ostringstream tmp_hashstream;
ostringstream tmp_sourcestream;
char * error_msg; char * error_msg;
int rc; int rc;
@ -60,46 +67,52 @@ int ImagePool::allocate (
goto error_name; goto error_name;
} }
img->get_template_attribute("DESCRIPTION", description);
img->get_template_attribute("TYPE", type); img->get_template_attribute("TYPE", type);
if ( type.empty() == true ) if ( type.empty() == true )
{ {
goto error_type; type = default_type;
} }
img->get_template_attribute("ORIGINAL_PATH", original_path); img->get_template_attribute("ORIGINAL_PATH", original_path);
if ( type == "OS" || type == "CDROM" ) if ( (type == "OS" || type == "CDROM") &&
{ original_path.empty() == true )
if ( original_path.empty() == true )
{ {
goto error_original_path; goto error_original_path;
} }
}
img->get_template_attribute("TARGET", target);
if ( target.empty() == true )
{
target = "hda"; // TODO add to oned.configuration
}
img->get_template_attribute("BUS", bus); img->get_template_attribute("BUS", bus);
if ( bus.empty() == true ) if ( bus.empty() == true )
{ {
bus = "IDE"; // TODO add to oned.configuration bus = default_bus;
} }
img->running_vms = 0;
// generatesource // Generate path to store the image
tmp_hashstream << oid << ":" << uid << ":" << name;
tmp_hashstream.str(sha1_digest(tmp_hashstream.str()));
tmp_sourcestream << source_prefix << "/" << tmp_hashstream;
img->name = name; img->name = name;
img->type = type; img->description = description;
img->target = target; img->source = tmp_sourcestream.str();
img->bus = bus;
if (img->set_type(type) != 0)
{
goto error_type;
}
if (img->set_bus(bus) != 0)
{
goto error_bus;
}
// --------------------------------------------------------------------- // ---------------------------------------------------------------------
// Insert the Object in the pool // Insert the Object in the pool
@ -121,7 +134,10 @@ error_name:
NebulaLog::log("IMG", Log::ERROR, "NAME not present in image template"); NebulaLog::log("IMG", Log::ERROR, "NAME not present in image template");
goto error_common; goto error_common;
error_type: error_type:
NebulaLog::log("IMG", Log::ERROR, "TYPE not present in image template"); NebulaLog::log("IMG", Log::ERROR, "Incorrect TYPE in image template");
goto error_common;
error_bus:
NebulaLog::log("IMG", Log::ERROR, "Incorrect BUS in image template");
goto error_common; goto error_common;
error_original_path: error_original_path:
NebulaLog::log("IMG", Log::ERROR, NebulaLog::log("IMG", Log::ERROR,
@ -129,18 +145,15 @@ error_original_path:
goto error_common; goto error_common;
error_common: error_common:
delete img; delete img;
*oid = -1; *oid = -1;
return -1; return -1;
error_parse: error_parse:
ostringstream oss; ostringstream oss;
oss << "ImagePool template parse error: " << error_msg; oss << "ImagePool template parse error: " << error_msg;
NebulaLog::log("IMG", Log::ERROR, oss); NebulaLog::log("IMG", Log::ERROR, oss);
free(error_msg); free(error_msg);
delete img; delete img;
*oid = -2; *oid = -2;
return -2; return -2;
} }
@ -183,3 +196,30 @@ int ImagePool::dump(ostringstream& oss, const string& where)
return rc; return rc;
} }
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
string ImagePool::sha1_digest(const string& pass)
{
EVP_MD_CTX mdctx;
unsigned char md_value[EVP_MAX_MD_SIZE];
unsigned int md_len;
ostringstream oss;
EVP_MD_CTX_init(&mdctx);
EVP_DigestInit_ex(&mdctx, EVP_sha1(), NULL);
EVP_DigestUpdate(&mdctx, pass.c_str(), pass.length());
EVP_DigestFinal_ex(&mdctx,md_value,&md_len);
EVP_MD_CTX_cleanup(&mdctx);
for(unsigned int i = 0; i<md_len; i++)
{
oss << setfill('0') << setw(2) << hex << nouppercase
<< (unsigned short) md_value[i];
}
return oss.str();
}