2010-05-20 18:58:17 +02:00
/* ------------------------------------------------------------------------ */
/* 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"
2010-06-30 13:10:07 +02:00
# include "NebulaLog.h"
2010-05-20 18:58:17 +02:00
using namespace std ;
/**
* The Image class .
*/
class Image : public PoolObjectSQL
{
public :
/**
* Type of Images
*/
2010-06-24 16:12:45 +02:00
enum ImageType
{
OS = 0 , /** < Base OS image */
CDROM = 1 , /** < An ISO9660 image */
DATABLOCK = 2 /** < User persistent data device */
} ;
/**
* Image State
*/
enum ImageState
{
INIT = 0 , /** < Initialization state */
2010-07-21 17:50:04 +02:00
READY = 1 , /** < Image ready to use */
USED = 2 , /** < Image in use */
DISABLED = 3 /** < Image can not be instantiated by a VM */
2010-06-24 16:12:45 +02:00
} ;
2010-05-20 18:58:17 +02:00
/**
* Function to write an Image on an output stream
*/
friend ostream & operator < < ( ostream & os , Image & i ) ;
2010-06-24 16:12:45 +02:00
2010-05-20 18:58:17 +02:00
// *************************************************************************
2010-05-28 18:56:35 +02:00
// Image Public Methods
2010-05-20 18:58:17 +02:00
// *************************************************************************
2010-05-28 18:56:35 +02:00
/**
* 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 ;
2010-05-20 18:58:17 +02:00
2010-05-28 18:56:35 +02:00
/**
* 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 ;
2010-05-20 18:58:17 +02:00
/**
* 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 ;
} ;
2010-06-24 16:12:45 +02:00
2010-06-08 15:30:15 +02:00
/**
2010-06-09 17:37:34 +02:00
* Returns true if the image is public
* @ return true if the image is public
2010-06-08 15:30:15 +02:00
*/
2010-07-09 12:10:05 +02:00
bool isPublic ( )
2010-06-08 15:30:15 +02:00
{
2010-06-24 16:12:45 +02:00
return ( public_img = = 1 ) ;
2010-06-08 15:30:15 +02:00
} ;
2010-05-28 18:56:35 +02:00
2010-08-03 19:22:13 +02:00
/**
* Returns true if the image is persistent
* @ return true if the image is persistent
*/
bool isPersistent ( )
{
return ( persistent_img = = 1 ) ;
} ;
2010-05-28 18:56:35 +02:00
2010-05-25 18:19:22 +02:00
/**
* Set enum type
* @ return 0 on success , - 1 otherwise
2010-06-24 16:12:45 +02:00
*/
int set_type ( const string & _type )
2010-05-25 18:19:22 +02:00
{
int rc = 0 ;
2010-06-24 16:12:45 +02:00
2010-05-25 18:19:22 +02:00
if ( _type = = " OS " )
{
type = OS ;
}
else if ( _type = = " CDROM " )
{
type = CDROM ;
}
else if ( _type = = " DATABLOCK " )
{
type = DATABLOCK ;
}
else
{
rc = - 1 ;
}
2010-06-24 16:12:45 +02:00
2010-05-25 18:19:22 +02:00
return rc ;
}
2010-05-20 18:58:17 +02:00
/**
2010-06-24 16:12:45 +02:00
* Get an image to be used in a VM , and updates its state .
2010-07-21 15:19:27 +02:00
* @ return 0 if success
2010-05-21 19:02:08 +02:00
*/
2010-07-21 17:50:04 +02:00
int acquire_image ( ) ;
2010-05-28 18:56:35 +02:00
2010-05-21 19:02:08 +02:00
/**
* Releases an image being used by a VM
2010-07-21 15:19:27 +02:00
* @ return true if the image needs to be updated
2010-05-20 18:58:17 +02:00
*/
2010-07-21 15:19:27 +02:00
bool release_image ( ) ;
2010-05-28 18:56:35 +02:00
2010-06-24 16:22:05 +02:00
/**
* Enables the image
2010-06-24 16:47:39 +02:00
* @ param to_enable true will enable the image .
2010-06-24 16:22:05 +02:00
* @ return 0 on success
*/
2010-06-24 16:47:39 +02:00
int enable ( bool to_enable )
2010-06-24 16:22:05 +02:00
{
int rc = 0 ;
2010-07-23 16:06:25 +02:00
if ( to_enable = = true )
2010-06-24 16:22:05 +02:00
{
2010-07-23 16:06:25 +02:00
if ( state = = DISABLED )
{
state = READY ;
}
2010-06-24 16:22:05 +02:00
}
2010-07-23 16:06:25 +02:00
else if ( state ! = USED ) // to_enable == false
2010-06-24 16:47:39 +02:00
{
state = DISABLED ;
}
2010-06-24 16:22:05 +02:00
else
{
rc = - 1 ;
}
return rc ;
}
/**
2010-06-24 16:47:39 +02:00
* Publish or unpublish an image
* @ param pub true to publish the image
2010-08-05 19:28:28 +02:00
* @ return 0 on success
2010-06-24 16:22:05 +02:00
*/
2010-08-03 19:22:13 +02:00
bool publish ( bool pub )
2010-06-24 16:22:05 +02:00
{
2010-08-03 19:22:13 +02:00
bool success = false ;
2010-08-04 17:42:53 +02:00
2010-06-24 16:47:39 +02:00
if ( pub = = true )
2010-06-24 16:22:05 +02:00
{
2010-08-03 19:22:13 +02:00
if ( ! isPersistent ( ) )
{
public_img = 1 ;
success = true ;
}
2010-06-24 16:22:05 +02:00
}
else
{
2010-06-24 16:47:39 +02:00
public_img = 0 ;
2010-08-03 19:22:13 +02:00
success = true ;
2010-06-24 16:22:05 +02:00
}
2010-08-04 17:42:53 +02:00
2010-08-03 19:22:13 +02:00
return success ;
}
2010-08-04 17:42:53 +02:00
2010-08-03 19:22:13 +02:00
/**
2010-08-30 18:21:21 +02:00
* Set / Unset an image as persistent
* @ param persistent true to make an image persistent
2010-08-05 19:28:28 +02:00
* @ return 0 on success
2010-08-03 19:22:13 +02:00
*/
bool persistent ( bool persis )
{
bool success = false ;
2010-08-04 17:42:53 +02:00
2010-08-03 19:22:13 +02:00
if ( persis = = true )
{
if ( ! isPublic ( ) & & running_vms = = 0 )
{
persistent_img = 1 ;
success = true ;
}
2010-06-24 16:22:05 +02:00
}
2010-08-03 19:22:13 +02:00
else
{
persistent_img = 0 ;
success = true ;
}
2010-08-04 17:42:53 +02:00
2010-08-03 19:22:13 +02:00
return success ;
2010-06-24 16:22:05 +02:00
}
2010-05-28 18:56:35 +02:00
/**
2010-05-31 15:08:24 +02:00
* Modifies the given disk attribute adding the following attributes :
* * SOURCE : the file - path .
* * BUS : will only be set if the Image ' s definition includes it .
* * TARGET : the value set depends on :
* - OS images will be mounted at prefix + a : hda , sda .
* - Prefix + b is reserved for the contex cdrom .
* - CDROM images will be at prefix + c : hdc , sdc .
* - Several DATABLOCK images can be mounted , they will be set to
* prefix + ( d + index ) : hdd , hde , hdf . . .
2010-06-24 18:35:18 +02:00
* @ param disk attribute for the VM template
2010-07-20 18:53:03 +02:00
* @ param index number of datablock images used by the same VM . Will be
* automatically increased .
* @ param img_type will be set to the used image ' s type
2010-05-28 18:56:35 +02:00
*/
2010-07-21 17:50:04 +02:00
int disk_attribute ( VectorAttribute * disk , int * index , ImageType * img_type ) ;
2010-05-20 18:58:17 +02:00
// ------------------------------------------------------------------------
// 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
{
2010-07-14 19:37:43 +02:00
return image_template - > get ( name , values ) ;
2010-05-20 18:58:17 +02:00
} ;
/**
* 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 ;
2010-07-14 19:37:43 +02:00
return image_template - > get ( str , values ) ;
2010-05-20 18:58:17 +02:00
} ;
/**
* 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 ;
2010-07-14 19:37:43 +02:00
image_template - > get ( str , value ) ;
2010-05-20 18:58:17 +02:00
}
/**
* 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 ;
2010-07-14 19:37:43 +02:00
image_template - > get ( str , value ) ;
2010-05-20 18:58:17 +02:00
}
2010-05-26 18:42:00 +02:00
2010-05-20 18:58:17 +02:00
/**
* Removes an Image attribute
* @ param name of the attribute
*/
2010-08-03 16:22:48 +02:00
int remove_template_attribute ( const string & name )
2010-05-20 18:58:17 +02:00
{
2010-08-03 16:22:48 +02:00
return image_template - > erase ( name ) ;
}
/**
2010-08-04 15:14:53 +02:00
* Adds a new attribute to the template ( replacing it if
2010-08-03 16:22:48 +02:00
* 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
*/
2010-08-04 15:14:53 +02:00
int replace_template_attribute (
2010-08-03 16:22:48 +02:00
const string & name ,
const string & value )
{
SingleAttribute * sattr ;
image_template - > erase ( name ) ;
sattr = new SingleAttribute ( name , value ) ;
image_template - > set ( sattr ) ;
return 0 ;
2010-05-20 18:58:17 +02:00
}
private :
// -------------------------------------------------------------------------
// Friends
// -------------------------------------------------------------------------
friend class ImagePool ;
// -------------------------------------------------------------------------
// Image Description
// -------------------------------------------------------------------------
/**
* Owner if the image
*/
int uid ;
/**
* The name of the Image
*/
string name ;
2010-06-24 16:12:45 +02:00
2010-05-20 18:58:17 +02:00
/**
* Type of the Image
*/
ImageType type ;
2010-06-07 16:21:58 +02:00
/**
* Public scope of the Image
*/
2010-06-09 17:37:34 +02:00
int public_img ;
2010-06-07 16:21:58 +02:00
2010-08-03 19:22:13 +02:00
/**
* Persistency of the Image
*/
int persistent_img ;
2010-06-07 16:21:58 +02:00
2010-05-20 18:58:17 +02:00
/**
* Registration time
*/
2010-05-25 18:19:22 +02:00
time_t regtime ;
2010-06-24 16:12:45 +02:00
2010-05-20 18:58:17 +02:00
/**
* Path to the image
*/
string source ;
2010-05-26 12:38:21 +02:00
2010-05-21 19:02:08 +02:00
/**
* Image state
*/
ImageState state ;
2010-06-24 16:12:45 +02:00
2010-05-21 19:02:08 +02:00
/**
* Number of VMs using the image
*/
2010-06-24 16:12:45 +02:00
int running_vms ;
2010-05-20 18:58:17 +02:00
// -------------------------------------------------------------------------
// Image Attributes
// -------------------------------------------------------------------------
/**
* The Image template , holds the Image attributes .
*/
2010-07-14 19:37:43 +02:00
ImageTemplate * image_template ;
2010-05-20 18:58:17 +02:00
// *************************************************************************
// 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 ) ;
db - > exec ( oss_image ) ;
} ;
2010-06-30 13:10:07 +02:00
/**
* " Encrypts " the password with SHA1 digest
* @ param password
* @ return sha1 encrypted password
*/
string sha1_digest ( const string & pass ) ;
2010-05-20 18:58:17 +02:00
protected :
// *************************************************************************
// Constructor
// *************************************************************************
2010-07-14 19:37:43 +02:00
Image ( int uid = - 1 , ImageTemplate * img_template = 0 ) ;
2010-05-20 18:58:17 +02:00
virtual ~ Image ( ) ;
// *************************************************************************
// DataBase implementation
// *************************************************************************
enum ColNames
2010-05-26 13:15:10 +02:00
{
2010-05-20 18:58:17 +02:00
OID = 0 , /* Image identifier (IID) */
UID = 1 , /* Image owner id */
NAME = 2 , /* Image name */
2010-05-26 13:15:10 +02:00
TYPE = 3 , /* 0) OS 1) CDROM 2) DATABLOCK */
2010-06-07 16:21:58 +02:00
PUBLIC = 4 , /* Public scope (YES OR NO) */
2010-08-03 19:22:13 +02:00
PERSISTENT = 5 , /* Peristency (YES OR NO) */
REGTIME = 6 , /* Time of registration */
SOURCE = 7 , /* Path to the image */
STATE = 8 , /* 0) INIT 1) ALLOCATED */
RUNNING_VMS = 9 , /* Number of VMs using the img */
2010-08-04 17:42:53 +02:00
TEMPLATE = 10 , /* Image template xml data */
LIMIT = 11
2010-05-20 18:58:17 +02:00
} ;
2010-06-24 16:12:45 +02:00
2010-05-20 18:58:17 +02:00
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
*/
2010-08-05 19:28:28 +02:00
virtual int insert ( SqlDB * db , string & error_str ) ;
2010-05-20 18:58:17 +02:00
/**
* 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
*/
2010-05-25 18:19:22 +02:00
virtual int drop ( SqlDB * db ) ;
2010-05-20 18:58:17 +02:00
/**
* 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_*/