2010-05-20 20:58:17 +04:00
/* ------------------------------------------------------------------------ */
2011-02-25 16:34:44 +03:00
/* Copyright 2002-2011, OpenNebula Project Leads (OpenNebula.org) */
2010-05-20 20:58:17 +04:00
/* */
/* 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 15:10:07 +04:00
# include "NebulaLog.h"
2010-05-20 20:58:17 +04:00
using namespace std ;
/**
* The Image class .
*/
class Image : public PoolObjectSQL
{
public :
/**
* Type of Images
*/
2010-06-24 18:12:45 +04: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 19:50:04 +04:00
READY = 1 , /** < Image ready to use */
USED = 2 , /** < Image in use */
2011-03-22 20:21:09 +03:00
DISABLED = 3 , /** < Image can not be instantiated by a VM */
LOCKED = 4 , /** < FS operation for the Image in process */
ERROR = 5 /** < Error state the operation FAILED*/
2010-06-24 18:12:45 +04:00
} ;
2010-05-20 20:58:17 +04:00
/**
* Function to write an Image on an output stream
*/
friend ostream & operator < < ( ostream & os , Image & i ) ;
2010-06-24 18:12:45 +04:00
2010-05-20 20:58:17 +04:00
// *************************************************************************
2010-05-28 20:56:35 +04:00
// Image Public Methods
2010-05-20 20:58:17 +04:00
// *************************************************************************
2010-05-28 20:56:35 +04: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 20:58:17 +04:00
/**
2011-02-25 21:02:29 +03:00
* Rebuilds the object from an xml formatted string
* @ param xml_str The xml - formatted string
*
* @ return 0 on success , - 1 otherwise
2010-05-20 20:58:17 +04:00
*/
2011-02-25 21:02:29 +03:00
int from_xml ( const string & xml_str ) ;
2010-06-24 18:12:45 +04:00
2010-06-08 17:30:15 +04:00
/**
2010-06-09 19:37:34 +04:00
* Returns true if the image is public
* @ return true if the image is public
2010-06-08 17:30:15 +04:00
*/
2010-07-09 14:10:05 +04:00
bool isPublic ( )
2010-06-08 17:30:15 +04:00
{
2010-06-24 18:12:45 +04:00
return ( public_img = = 1 ) ;
2010-06-08 17:30:15 +04:00
} ;
2010-05-28 20:56:35 +04:00
2010-08-03 21:22:13 +04:00
/**
* Returns true if the image is persistent
* @ return true if the image is persistent
*/
bool isPersistent ( )
{
return ( persistent_img = = 1 ) ;
} ;
2010-05-28 20:56:35 +04:00
2011-03-24 18:37:15 +03:00
/**
* Returns the source path of the image
* @ return source of image
*/
const string & get_source ( )
{
return source ;
}
2011-05-07 04:49:07 +04:00
/**
* Returns the source path of the image
* @ return source of image
*/
void set_source ( const string & _source )
{
source = _source ;
}
2011-03-24 20:27:19 +03:00
/**
* Returns the type of the image
* @ return type
*/
ImageType get_type ( )
{
return type ;
}
2010-05-25 20:19:22 +04:00
/**
2011-03-22 20:21:09 +03:00
* Returns the image state
* @ return state of image
2010-06-24 18:12:45 +04:00
*/
2011-03-22 20:21:09 +03:00
ImageState get_state ( )
2010-05-25 20:19:22 +04:00
{
2011-03-22 20:21:09 +03:00
return state ;
2010-05-25 20:19:22 +04:00
}
2010-05-20 20:58:17 +04:00
/**
2011-03-22 20:21:09 +03:00
* Sets the image state
* @ param state of image
2010-05-21 21:02:08 +04:00
*/
2011-03-22 20:21:09 +03:00
void set_state ( ImageState _state )
{
state = _state ;
}
2010-05-28 20:56:35 +04:00
2011-03-22 20:21:09 +03:00
/**
*
*/
int dec_running ( )
{
return - - running_vms ;
}
2010-05-21 21:02:08 +04:00
/**
2011-03-22 20:21:09 +03:00
*
2010-05-20 20:58:17 +04:00
*/
2011-03-22 20:21:09 +03:00
int inc_running ( )
{
return + + running_vms ;
}
2010-05-28 20:56:35 +04:00
2011-03-25 02:11:10 +03:00
/**
*
*/
int get_running ( )
{
return running_vms ;
}
2010-06-24 18:22:05 +04:00
/**
2011-03-22 20:21:09 +03:00
* Set enum type
* @ return 0 on success , - 1 otherwise
2010-06-24 18:22:05 +04:00
*/
2011-03-22 20:21:09 +03:00
int set_type ( const string & _type )
2010-06-24 18:22:05 +04:00
{
int rc = 0 ;
2011-03-22 20:21:09 +03:00
if ( _type = = " OS " )
2010-06-24 18:22:05 +04:00
{
2011-03-22 20:21:09 +03:00
type = OS ;
2010-06-24 18:22:05 +04:00
}
2011-03-22 20:21:09 +03:00
else if ( _type = = " CDROM " )
2010-06-24 18:47:39 +04:00
{
2011-03-22 20:21:09 +03:00
type = CDROM ;
}
else if ( _type = = " DATABLOCK " )
{
type = DATABLOCK ;
2010-06-24 18:47:39 +04:00
}
2010-06-24 18:22:05 +04:00
else
{
rc = - 1 ;
}
return rc ;
}
/**
2010-06-24 18:47:39 +04:00
* Publish or unpublish an image
* @ param pub true to publish the image
2010-08-05 21:28:28 +04:00
* @ return 0 on success
2010-06-24 18:22:05 +04:00
*/
2010-08-03 21:22:13 +04:00
bool publish ( bool pub )
2010-06-24 18:22:05 +04:00
{
2010-08-03 21:22:13 +04:00
bool success = false ;
2010-08-04 19:42:53 +04:00
2010-06-24 18:47:39 +04:00
if ( pub = = true )
2010-06-24 18:22:05 +04:00
{
2010-08-03 21:22:13 +04:00
if ( ! isPersistent ( ) )
{
public_img = 1 ;
success = true ;
}
2010-06-24 18:22:05 +04:00
}
else
{
2010-06-24 18:47:39 +04:00
public_img = 0 ;
2010-08-03 21:22:13 +04:00
success = true ;
2010-06-24 18:22:05 +04:00
}
2010-08-04 19:42:53 +04:00
2010-08-03 21:22:13 +04:00
return success ;
}
2010-08-04 19:42:53 +04:00
2010-08-03 21:22:13 +04:00
/**
2010-08-30 20:21:21 +04:00
* Set / Unset an image as persistent
* @ param persistent true to make an image persistent
2010-08-05 21:28:28 +04:00
* @ return 0 on success
2010-08-03 21:22:13 +04:00
*/
bool persistent ( bool persis )
{
bool success = false ;
2010-08-04 19:42:53 +04:00
2010-08-03 21:22:13 +04:00
if ( persis = = true )
{
if ( ! isPublic ( ) & & running_vms = = 0 )
{
persistent_img = 1 ;
success = true ;
}
2010-06-24 18:22:05 +04:00
}
2010-08-03 21:22:13 +04:00
else
{
persistent_img = 0 ;
success = true ;
}
2010-08-04 19:42:53 +04:00
2010-08-03 21:22:13 +04:00
return success ;
2010-06-24 18:22:05 +04:00
}
2010-05-28 20:56:35 +04:00
/**
2010-05-31 17:08:24 +04: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 20:35:18 +04:00
* @ param disk attribute for the VM template
2010-07-20 20:53:03 +04: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 20:56:35 +04:00
*/
2010-07-21 19:50:04 +04:00
int disk_attribute ( VectorAttribute * disk , int * index , ImageType * img_type ) ;
2010-05-20 20:58:17 +04:00
private :
// -------------------------------------------------------------------------
// Friends
// -------------------------------------------------------------------------
friend class ImagePool ;
// -------------------------------------------------------------------------
// Image Description
// -------------------------------------------------------------------------
/**
* Type of the Image
*/
2011-03-08 18:52:45 +03:00
ImageType type ;
2010-06-07 18:21:58 +04:00
/**
* Public scope of the Image
*/
2011-03-08 18:52:45 +03:00
int public_img ;
2010-06-07 18:21:58 +04:00
2010-08-03 21:22:13 +04:00
/**
* Persistency of the Image
*/
2011-03-08 18:52:45 +03:00
int persistent_img ;
2010-06-07 18:21:58 +04:00
2010-05-20 20:58:17 +04:00
/**
* Registration time
*/
2011-03-08 18:52:45 +03:00
time_t regtime ;
2010-06-24 18:12:45 +04:00
2010-05-20 20:58:17 +04:00
/**
* Path to the image
*/
string source ;
2010-05-26 14:38:21 +04:00
2010-05-21 21:02:08 +04:00
/**
* Image state
*/
ImageState state ;
2010-06-24 18:12:45 +04:00
2010-05-21 21:02:08 +04:00
/**
* Number of VMs using the image
*/
2010-06-24 18:12:45 +04:00
int running_vms ;
2010-05-20 20:58:17 +04: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 ) ;
/**
* 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 15:10:07 +04:00
/**
* " Encrypts " the password with SHA1 digest
* @ param password
* @ return sha1 encrypted password
*/
2011-04-07 04:01:40 +04:00
static string sha1_digest ( const string & pass ) ;
2010-06-30 15:10:07 +04:00
2010-05-20 20:58:17 +04:00
protected :
// *************************************************************************
// Constructor
// *************************************************************************
2011-05-12 19:10:35 +04:00
Image ( int uid ,
2011-03-08 18:52:45 +03:00
ImageTemplate * img_template ) ;
2010-05-20 20:58:17 +04:00
virtual ~ Image ( ) ;
// *************************************************************************
// DataBase implementation
// *************************************************************************
static const char * db_names ;
static const char * db_bootstrap ;
static const char * table ;
/**
* Writes the Image in the database .
* @ param db pointer to the db
* @ return 0 on success
*/
2010-08-05 21:28:28 +04:00
virtual int insert ( SqlDB * db , string & error_str ) ;
2010-05-20 20:58:17 +04: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 ) ;
} ;
# endif /*IMAGE_H_*/