1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-01-27 14:03:40 +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

@ -33,9 +33,9 @@ public:
*/ */
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 */
}; };
/** /**
@ -43,10 +43,11 @@ public:
*/ */
enum ImageState enum ImageState
{ {
INIT = 0, INIT = 0, /** < Initialization state */
LOCKED = 1, LOCKED = 1, /** < FS operation on the image in progress, don't use */
READY = 2, READY = 2, /** < Image ready to use */
USED = 3 USED = 3, /** < Image in use */
DISABLED = 4 /** < Image can not be instantiated by a VM */
}; };
/** /**
@ -105,14 +106,14 @@ 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;
@ -137,11 +138,11 @@ public:
} }
/** /**
* 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

View File

@ -388,14 +388,17 @@ 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)
{ {
case READY:
running_vms++; running_vms++;
if(overwrite) if ( overwrite == true)
{ {
state = LOCKED; state = LOCKED;
} }
@ -403,26 +406,51 @@ bool Image::get_image(bool overwrite)
{ {
state = USED; state = USED;
} }
return true; break;
case USED:
if ( overwrite == true)
{
rc = -1;
} }
else else
{ {
return false; running_vms++;
} }
break;
case DISABLED:
case LOCKED:
default:
rc = -1;
break;
}
return rc;
} }
/* ------------------------------------------------------------------------ */ /* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */ /* ------------------------------------------------------------------------ */
// TODO update?
void Image::release_image() void Image::release_image()
{ {
switch (state)
{
case USED:
case LOCKED:
running_vms--; running_vms--;
if ( state == USED && running_vms == 0 ) if ( running_vms == 0)
{ {
state = READY; state = READY;
} }
break;
case DISABLED:
case READY:
default:
break;
}
} }
/* ------------------------------------------------------------------------ */ /* ------------------------------------------------------------------------ */