1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-01-12 09:17:41 +03:00

feature #200: New states for Images and associated life-cycle functions

This commit is contained in:
Tino Vázquez and Ruben S. Montero 2010-06-24 16:12:45 +02:00 committed by Ruben S. Montero
parent 4bd2506f00
commit 3fd53cc4c2
2 changed files with 94 additions and 82 deletions

View File

@ -31,29 +31,30 @@ public:
/** /**
* Type of Images * Type of Images
*/ */
enum ImageType enum ImageType
{ {
OS = 0, OS = 0, /** < Base OS image */
CDROM = 1, CDROM = 1, /** < An ISO9660 image */
DATABLOCK = 2 DATABLOCK = 2 /** < User persistent data device */
}; };
/**
* Image State
*/
enum ImageState
{
INIT = 0, /** < Initialization state */
LOCKED = 1, /** < FS operation on the image in progress, don't use */
READY = 2, /** < Image ready to use */
USED = 3, /** < Image in use */
DISABLED = 4 /** < Image can not be instantiated by a VM */
};
/**
* Image State
*/
enum ImageState
{
INIT = 0,
LOCKED = 1,
READY = 2,
USED = 3
};
/** /**
* Function to write an Image on an output stream * Function to write an Image on an output stream
*/ */
friend ostream& operator<<(ostream& os, Image& i); friend ostream& operator<<(ostream& os, Image& i);
// ************************************************************************* // *************************************************************************
// Image Public Methods // Image Public Methods
// ************************************************************************* // *************************************************************************
@ -98,24 +99,24 @@ public:
{ {
return name; return name;
}; };
/** /**
* Returns true if the image is public * Returns true if the image is public
* @return true if the image is public * @return true if the image is public
*/ */
bool is_public() bool is_public()
{ {
return public_img; return (public_img == 1);
}; };
/** /**
* Set enum type * Set enum type
* @return 0 on success, -1 otherwise * @return 0 on success, -1 otherwise
*/ */
int set_type(string _type) int set_type(const string& _type)
{ {
int rc = 0; int rc = 0;
if ( _type == "OS" ) if ( _type == "OS" )
{ {
type = OS; type = OS;
@ -132,16 +133,16 @@ public:
{ {
rc = -1; rc = -1;
} }
return rc; return rc;
} }
/** /**
* Get an image to be used in a VM * Get an image to be used in a VM, and updates its state.
* @param overwrite true if the image is going to be overwritten * @param overwrite true if the image is going to be overwritten
* @return boolean true if the image can be used * @return 0 if success
*/ */
bool get_image(bool overwrite); int acquire_image(bool overwrite);
/** /**
@ -240,26 +241,9 @@ public:
sattr = new SingleAttribute(name,value); sattr = new SingleAttribute(name,value);
rc = image_template.replace_attribute(db,sattr); rc = image_template.replace_attribute(db,sattr);
if (rc != 0)
{
delete sattr;
}
return rc; return rc;
} }
/**
* Inserts a new attribute in the template of an Image, also the DB is
* updated. The image'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 image_template.insert_attribute(db,attribute);
}
/** /**
* Removes an Image attribute * Removes an Image attribute
* @param name of the attribute * @param name of the attribute
@ -290,7 +274,7 @@ private:
* The name of the Image * The name of the Image
*/ */
string name; string name;
/** /**
* Type of the Image * Type of the Image
*/ */
@ -305,7 +289,7 @@ private:
* Registration time * Registration time
*/ */
time_t regtime; time_t regtime;
/** /**
* Path to the image * Path to the image
*/ */
@ -315,11 +299,11 @@ private:
* Image state * Image state
*/ */
ImageState state; ImageState state;
/** /**
* Number of VMs using the image * Number of VMs using the image
*/ */
int running_vms; int running_vms;
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
// Image Attributes // Image Attributes
@ -392,7 +376,7 @@ protected:
RUNNING_VMS = 8, /* Number of VMs using the img */ RUNNING_VMS = 8, /* Number of VMs using the img */
LIMIT = 9 LIMIT = 9
}; };
static const char * db_names; static const char * db_names;
static const char * db_bootstrap; static const char * db_bootstrap;

View File

@ -44,7 +44,7 @@ Image::~Image(){};
const char * Image::table = "image_pool"; const char * Image::table = "image_pool";
const char * Image::db_names = "(oid, uid, name, type, public, regtime, " const char * Image::db_names = "(oid, uid, name, type, public, regtime, "
"source, state, running_vms)"; "source, state, running_vms)";
const char * Image::db_bootstrap = "CREATE TABLE IF NOT EXISTS image_pool (" const char * Image::db_bootstrap = "CREATE TABLE IF NOT EXISTS image_pool ("
@ -73,18 +73,18 @@ int Image::select_cb(void * nil, int num, char **values, char ** names)
oid = atoi(values[OID]); oid = atoi(values[OID]);
uid = atoi(values[UID]); uid = atoi(values[UID]);
name = values[NAME]; name = values[NAME];
type = static_cast<ImageType>(atoi(values[TYPE])); type = static_cast<ImageType>(atoi(values[TYPE]));
public_img = atoi(values[PUBLIC]); public_img = atoi(values[PUBLIC]);
regtime = static_cast<time_t>(atoi(values[REGTIME])); regtime = static_cast<time_t>(atoi(values[REGTIME]));
source = values[SOURCE]; source = values[SOURCE];
state = static_cast<ImageState>(atoi(values[STATE])); state = static_cast<ImageState>(atoi(values[STATE]));
running_vms = atoi(values[RUNNING_VMS]); running_vms = atoi(values[RUNNING_VMS]);
image_template.id = oid; image_template.id = oid;
@ -122,7 +122,7 @@ int Image::select(SqlDB *db)
{ {
return -1; return -1;
} }
return 0; return 0;
} }
@ -292,7 +292,7 @@ int Image::drop(SqlDB * db)
{ {
ostringstream oss; ostringstream oss;
int rc; int rc;
// Only delete the VM // Only delete the VM
if (running_vms != 0) if (running_vms != 0)
{ {
@ -377,7 +377,7 @@ string& Image::to_str(string& str) const
"STATE = " << state << endl << "STATE = " << state << endl <<
"RUNNING_VMS = " << running_vms << endl << "RUNNING_VMS = " << running_vms << endl <<
"TEMPLATE" << endl "TEMPLATE" << endl
<< image_template.to_str(template_str) << image_template.to_str(template_str)
<< endl; << endl;
str = os.str(); str = os.str();
@ -388,40 +388,68 @@ string& Image::to_str(string& str) const
/* ------------------------------------------------------------------------ */ /* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */ /* ------------------------------------------------------------------------ */
// TODO update? int Image::acquire_image(bool overwrite)
bool Image::get_image(bool overwrite)
{ {
if ( state == READY || state == USED ) int rc = 0;
switch (state)
{ {
running_vms++; case READY:
running_vms++;
if(overwrite)
{ if ( overwrite == true)
state = LOCKED; {
} state = LOCKED;
else }
{ else
state = USED; {
} state = USED;
return true; }
} break;
else
{ case USED:
return false; if ( overwrite == true)
{
rc = -1;
}
else
{
running_vms++;
}
break;
case DISABLED:
case LOCKED:
default:
rc = -1;
break;
} }
return rc;
} }
/* ------------------------------------------------------------------------ */ /* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */ /* ------------------------------------------------------------------------ */
// TODO update?
void Image::release_image() void Image::release_image()
{ {
running_vms--; switch (state)
if ( state == USED && running_vms == 0 )
{ {
state = READY; case USED:
case LOCKED:
running_vms--;
if ( running_vms == 0)
{
state = READY;
}
break;
case DISABLED:
case READY:
default:
break;
} }
} }