2008-06-17 20:27:32 +04:00
/* -------------------------------------------------------------------------- */
2010-02-22 20:00:30 +03:00
/* Copyright 2002-2010, OpenNebula Project Leads (OpenNebula.org) */
2008-06-17 20:27:32 +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 VIRTUAL_MACHINE_POOL_H_
# define VIRTUAL_MACHINE_POOL_H_
# include "PoolSQL.h"
# include "VirtualMachine.h"
# include <time.h>
using namespace std ;
/**
* The Virtual Machine Pool class . . . .
*/
class VirtualMachinePool : public PoolSQL
{
public :
2010-04-11 00:16:47 +04:00
VirtualMachinePool ( SqlDB * db , vector < const Attribute * > hook_mads ) ;
2008-06-17 20:27:32 +04:00
~ VirtualMachinePool ( ) { } ;
/**
* Function to allocate a new VM object
* @ param uid user id ( the owner of the VM )
2010-07-14 20:11:29 +04:00
* @ param vm_template a VM Template object describing the VM
2008-06-17 20:27:32 +04:00
* @ param oid the id assigned to the VM ( output )
* @ param on_hold flag to submit on hold
2010-05-03 19:21:35 +04:00
* @ return oid on success , - 1 error inserting in DB or - 2 error parsing
2008-06-17 20:27:32 +04:00
* the template
*/
int allocate (
int uid ,
2010-07-14 20:11:29 +04:00
VirtualMachineTemplate * vm_template ,
2008-06-17 20:27:32 +04:00
int * oid ,
bool on_hold = false ) ;
/**
* Function to get a VM from the pool , if the object is not in memory
* it is loade from the DB
* @ param oid VM unique id
* @ param lock locks the VM mutex
* @ return a pointer to the VM , 0 if the VM could not be loaded
*/
VirtualMachine * get (
int oid ,
bool lock )
{
return static_cast < VirtualMachine * > ( PoolSQL : : get ( oid , lock ) ) ;
} ;
/**
* Function to get the IDs of running VMs
* @ param oids a vector that contains the IDs
2010-06-14 18:18:30 +04:00
* @ param vm_limit Max . number of VMs returned
* @ param last_poll Return only VMs which last_poll is less than or equal
* to this value .
2008-06-17 20:27:32 +04:00
* @ return 0 on success
*/
int get_running (
2010-06-14 18:18:30 +04:00
vector < int > & oids ,
int vm_limit ,
time_t last_poll ) ;
2008-06-17 20:27:32 +04:00
/**
* Function to get the IDs of pending VMs
* @ param oids a vector that contains the IDs
* @ return 0 on success
*/
int get_pending (
vector < int > & oids ) ;
//--------------------------------------------------------------------------
// Virtual Machine DB access functions
//--------------------------------------------------------------------------
2009-03-06 15:10:15 +03:00
2008-07-21 02:41:20 +04:00
/**
2009-03-06 15:10:15 +03:00
* Updates the template of a VM , adding a new attribute ( replacing it if
2008-07-21 02:41:20 +04:00
* already defined ) , the vm ' s mutex SHOULD be locked
* @ param vm pointer to the virtual machine object
* @ param name of the new attribute
* @ param value of the new attribute
* @ return 0 on success
*/
int update_template_attribute (
2010-07-14 20:11:29 +04:00
VirtualMachine * vm ,
string & name ,
string & value )
2008-07-21 02:41:20 +04:00
{
return vm - > update_template_attribute ( db , name , value ) ;
}
2009-03-06 15:10:15 +03:00
2008-06-17 20:27:32 +04:00
/**
2008-06-22 05:51:49 +04:00
* Updates the history record of a VM , the vm ' s mutex SHOULD be locked
2008-06-17 20:27:32 +04:00
* @ param vm pointer to the virtual machine object
* @ return 0 on success
*/
int update_history (
VirtualMachine * vm )
{
return vm - > update_history ( db ) ;
}
2008-06-22 05:51:49 +04:00
2008-06-17 20:27:32 +04:00
/**
2008-06-22 05:51:49 +04:00
* Updates the previous history record , the vm ' s mutex SHOULD be locked
2008-06-17 20:27:32 +04:00
* @ param vm pointer to the virtual machine object
2008-06-22 05:51:49 +04:00
* @ return 0 on success
2008-06-17 20:27:32 +04:00
*/
2008-06-22 05:51:49 +04:00
int update_previous_history (
VirtualMachine * vm )
2008-06-17 20:27:32 +04:00
{
2008-06-22 05:51:49 +04:00
return vm - > update_previous_history ( db ) ;
}
2009-03-06 15:10:15 +03:00
2008-06-17 20:27:32 +04:00
/**
* Bootstraps the database table ( s ) associated to the VirtualMachine pool
*/
2010-04-11 00:16:47 +04:00
static void bootstrap ( SqlDB * _db )
2008-06-17 20:27:32 +04:00
{
2009-07-09 18:34:34 +04:00
VirtualMachine : : bootstrap ( _db ) ;
2008-06-17 20:27:32 +04:00
} ;
2010-04-11 00:16:47 +04:00
2009-05-22 04:46:52 +04:00
/**
* Dumps the VM pool in XML format . A filter can be also added to the query
* Also the hostname where the VirtualMachine is running is added to the
* pool
* @ param oss the output stream to dump the pool contents
* @ param where filter for the objects , defaults to all
*
* @ return 0 on success
*/
2010-04-11 00:16:47 +04:00
int dump ( ostringstream & oss , const string & where ) ;
2009-05-22 04:46:52 +04:00
2008-06-17 20:27:32 +04:00
private :
/**
* Factory method to produce VM objects
* @ return a pointer to the new VM
*/
PoolObjectSQL * create ( )
{
return new VirtualMachine ;
} ;
2010-04-11 00:16:47 +04:00
/**
* Callback function to get output the vm pool in XML format
* ( VirtualMachinePool : : dump )
* @ param num the number of columns read from the DB
* @ param names the column names
* @ param vaues the column values
* @ return 0 on success
*/
int dump_cb ( void * _oss , int num , char * * values , char * * names ) ;
2008-06-17 20:27:32 +04:00
} ;
2009-03-06 15:10:15 +03:00
2008-06-17 20:27:32 +04:00
# endif /*VIRTUAL_MACHINE_POOL_H_*/