2008-06-17 20:27:32 +04:00
/* -------------------------------------------------------------------------- */
2014-01-09 14:51:20 +04:00
/* Copyright 2002-2014, OpenNebula Project (OpenNebula.org), C12G Labs */
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 :
2012-05-16 18:09:27 +04:00
VirtualMachinePool ( SqlDB * db ,
vector < const Attribute * > hook_mads ,
const string & hook_location ,
const string & remotes_location ,
vector < const Attribute * > & restricted_attrs ,
2012-12-20 21:21:30 +04:00
time_t expire_time ,
bool on_hold ) ;
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 )
Feature #407: Add 'GID' attribute to some pool objects; change *pool.info XML-RPC flag meaning; update onedb migrator; fix tests.
* VM, VMTEMPLATE, VNET & IMAGE objects have a GID attribute, and a table column. The group id is inherited from the user creating the object, except for VMs created from Templates, that inherit the Template's group.
* The new flag meaning has been modified in src/rm sources and CLI commands for one.(vm,template,vnet,image)pool.info . It changes from
-2 all, -1 mine & public, >=0 UID
to
-3 mine, -2 all, -1 mine & group
* USER has a group, but not secondary ones. The user_pool table doesn't have a GID column, we'll deal with it later when the group-users relations are implemented.
* onedb migrator 1.rb: deleted USERNAME, and GID added.
2011-05-16 19:00:27 +04:00
* @ param gid the id of the group this object is assigned to
2013-01-18 21:34:51 +04:00
* @ param uname user name
* @ param gname group name
* @ param umask permissions umask
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 )
2011-03-30 21:03:49 +04:00
* @ param error_str Returns the error reason , if any
2008-06-17 20:27:32 +04:00
* @ param on_hold flag to submit on hold
2013-01-18 21:34:51 +04:00
*
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 (
2011-03-08 19:55:14 +03:00
int uid ,
Feature #407: Add 'GID' attribute to some pool objects; change *pool.info XML-RPC flag meaning; update onedb migrator; fix tests.
* VM, VMTEMPLATE, VNET & IMAGE objects have a GID attribute, and a table column. The group id is inherited from the user creating the object, except for VMs created from Templates, that inherit the Template's group.
* The new flag meaning has been modified in src/rm sources and CLI commands for one.(vm,template,vnet,image)pool.info . It changes from
-2 all, -1 mine & public, >=0 UID
to
-3 mine, -2 all, -1 mine & group
* USER has a group, but not secondary ones. The user_pool table doesn't have a GID column, we'll deal with it later when the group-users relations are implemented.
* onedb migrator 1.rb: deleted USERNAME, and GID added.
2011-05-16 19:00:27 +04:00
int gid ,
2011-06-30 13:31:00 +04:00
const string & uname ,
const string & gname ,
2013-01-18 21:34:51 +04:00
int umask ,
2011-03-08 19:55:14 +03:00
VirtualMachineTemplate * vm_template ,
int * oid ,
string & error_str ,
bool on_hold = false ) ;
2008-06-17 20:27:32 +04:00
/**
* 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 ) ) ;
} ;
2012-06-01 13:58:47 +04:00
/**
* Function to get a VM from the pool , string version for VM ID
*/
VirtualMachine * get (
const string & oid_s ,
bool lock )
{
istringstream iss ( oid_s ) ;
int oid ;
iss > > oid ;
2013-03-15 20:37:47 +04:00
2012-06-01 13:58:47 +04:00
if ( iss . fail ( ) )
{
return 0 ;
}
2013-03-15 20:37:47 +04:00
2012-06-01 13:58:47 +04:00
return static_cast < VirtualMachine * > ( PoolSQL : : get ( oid , lock ) ) ;
} ;
2008-06-17 20:27:32 +04:00
/**
* 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 ) ;
2014-09-11 12:34:18 +04:00
/**
* Gets the IDs of VMs matching the given SQL where string .
* @ param oids a vector that contains the IDs
* @ param where SQL clause
* @ return 0 on success
*/
int search ( vector < int > & oids , const string & where )
{
return PoolSQL : : search ( oids , VirtualMachine : : table , where ) ;
} ;
2008-06-17 20:27:32 +04:00
//--------------------------------------------------------------------------
// Virtual Machine DB access functions
//--------------------------------------------------------------------------
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
2012-05-16 18:09:27 +04:00
/**
* Inserts the last monitoring , and deletes old monitoring entries for this
* VM
*
* @ param vm pointer to the virtual machine object
* @ return 0 on success
*/
int update_monitoring (
VirtualMachine * vm )
{
2012-05-17 02:56:03 +04:00
if ( _monitor_expiration < = 0 )
2012-05-16 18:09:27 +04:00
{
return 0 ;
}
return vm - > update_monitoring ( db ) ;
2012-05-18 14:05:18 +04:00
} ;
2012-05-16 18:09:27 +04:00
/**
2012-05-18 14:05:18 +04:00
* Deletes the expired monitoring entries for all VMs
2012-05-16 18:09:27 +04:00
*
* @ return 0 on success
*/
2012-05-18 14:05:18 +04:00
int clean_expired_monitoring ( ) ;
2012-05-17 16:58:28 +04:00
/**
* Deletes all monitoring entries for all VMs
*
* @ return 0 on success
*/
2012-05-18 14:05:18 +04:00
int clean_all_monitoring ( ) ;
2012-05-16 18:09:27 +04:00
2008-06-17 20:27:32 +04:00
/**
* Bootstraps the database table ( s ) associated to the VirtualMachine pool
2011-10-10 17:14:46 +04:00
* @ return 0 on success
2008-06-17 20:27:32 +04:00
*/
2011-10-10 17:14:46 +04:00
static int bootstrap ( SqlDB * _db )
2008-06-17 20:27:32 +04:00
{
2011-10-10 17:14:46 +04:00
return 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
2014-01-13 19:30:43 +04:00
* @ param limit parameters used for pagination
2009-05-22 04:46:52 +04:00
*
* @ return 0 on success
*/
2014-01-13 19:30:43 +04:00
int dump ( ostringstream & oss , const string & where , const string & limit )
2010-08-24 17:51:15 +04:00
{
2014-01-13 19:30:43 +04:00
return PoolSQL : : dump ( oss , " VM_POOL " , VirtualMachine : : table , where ,
limit ) ;
2012-05-04 19:27:57 +04:00
} ;
2010-08-24 17:51:15 +04:00
2012-05-05 05:18:25 +04:00
/**
2013-03-15 20:37:47 +04:00
* Dumps the VM accounting information in XML format . A filter can be also
2012-05-05 05:18:25 +04:00
* added to the query as well as a time frame .
* @ param oss the output stream to dump the pool contents
* @ param where filter for the objects , defaults to all
*
* @ return 0 on success
*/
2013-03-15 20:37:47 +04:00
int dump_acct ( ostringstream & oss ,
const string & where ,
int time_start ,
2012-05-05 05:18:25 +04:00
int time_end ) ;
2012-05-16 18:09:27 +04:00
2014-10-30 19:45:32 +03:00
/**
* Dumps the VM showback information in XML format . A filter can be also
* added to the query as well as a time frame .
* @ param oss the output stream to dump the pool contents
* @ param where filter for the objects , defaults to all
*
* @ return 0 on success
*/
int dump_showback ( ostringstream & oss ,
const string & where ,
int time_start ,
int time_end ) ;
2012-05-16 18:09:27 +04:00
/**
* Dumps the VM monitoring information entries in XML format . A filter
* can be also added to the query .
*
* @ param oss the output stream to dump the pool contents
* @ param where filter for the objects , defaults to all
*
* @ return 0 on success
*/
int dump_monitoring ( ostringstream & oss ,
const string & where ) ;
2012-05-17 02:56:03 +04:00
/**
* Dumps the VM monitoring information for a single VM
*
* @ param oss the output stream to dump the pool contents
* @ param vmid id of the target VM
*
* @ return 0 on success
*/
int dump_monitoring ( ostringstream & oss ,
int vmid )
{
ostringstream filter ;
filter < < " oid = " < < vmid ;
return dump_monitoring ( oss , filter . str ( ) ) ;
}
2014-10-30 19:21:27 +03:00
/**
* Processes all the history records , and stores the monthly cost for each
* VM
*/
void calculate_showback ( ) ;
2008-06-17 20:27:32 +04:00
private :
/**
* Factory method to produce VM objects
* @ return a pointer to the new VM
*/
PoolObjectSQL * create ( )
{
2013-01-18 21:34:51 +04:00
return new VirtualMachine ( - 1 , - 1 , - 1 , " " , " " , 0 , 0 ) ;
2008-06-17 20:27:32 +04:00
} ;
2012-05-16 18:09:27 +04:00
/**
* Size , in seconds , of the historical monitoring information
*/
2012-05-17 02:56:03 +04:00
static time_t _monitor_expiration ;
2012-12-20 21:21:30 +04:00
/**
* True or false whether to submit new VM on HOLD or not
*/
static bool _submit_on_hold ;
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_*/