2008-06-17 16:27:32 +00:00
/* -------------------------------------------------------------------------- */
2022-04-07 19:49:58 +02:00
/* Copyright 2002-2022, OpenNebula Project, OpenNebula Systems */
2008-06-17 16:27:32 +00: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"
2020-03-04 16:05:57 +01:00
# include "OneDB.h"
2008-06-17 16:27:32 +00:00
# include <time.h>
/**
* The Virtual Machine Pool class . . . .
*/
class VirtualMachinePool : public PoolSQL
{
public :
2019-09-09 14:43:51 +02:00
VirtualMachinePool ( SqlDB * db ,
2020-07-02 22:42:10 +02:00
std : : vector < const SingleAttribute * > & restricted_attrs ,
std : : vector < const SingleAttribute * > & encrypted_attrs ,
2019-09-12 16:25:23 +02:00
bool on_hold ,
float default_cpu_cost ,
float default_mem_cost ,
2021-04-15 11:11:41 +02:00
float default_disk_cost ,
bool showback_only_running ) ;
2008-06-17 16:27:32 +00: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 17:00:27 +02:00
* @ param gid the id of the group this object is assigned to
2013-01-18 18:34:51 +01:00
* @ param uname user name
* @ param gname group name
* @ param umask permissions umask
2010-07-14 18:11:29 +02:00
* @ param vm_template a VM Template object describing the VM
2008-06-17 16:27:32 +00:00
* @ param oid the id assigned to the VM ( output )
2011-03-30 19:03:49 +02:00
* @ param error_str Returns the error reason , if any
2008-06-17 16:27:32 +00:00
* @ param on_hold flag to submit on hold
2013-01-18 18:34:51 +01:00
*
2010-05-03 17:21:35 +02:00
* @ return oid on success , - 1 error inserting in DB or - 2 error parsing
2008-06-17 16:27:32 +00:00
* the template
*/
int allocate (
2011-03-08 17:55:14 +01: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 17:00:27 +02:00
int gid ,
2020-07-02 22:42:10 +02:00
const std : : string & uname ,
const std : : string & gname ,
2013-01-18 18:34:51 +01:00
int umask ,
2020-09-15 11:16:00 +02:00
std : : unique_ptr < VirtualMachineTemplate > vm_template ,
2011-03-08 17:55:14 +01:00
int * oid ,
2020-07-02 22:42:10 +02:00
std : : string & error_str ,
2011-03-08 17:55:14 +01:00
bool on_hold = false ) ;
2008-06-17 16:27:32 +00:00
/**
2020-09-10 09:08:29 +02:00
* Gets an object from the pool ( if needed the object is loaded from the
* database ) . The object is locked , other threads can ' t access the same
* object . The lock is released by destructor .
* @ param oid the VM unique identifier
* @ return a pointer to the VM , nullptr in case of failure
2008-06-17 16:27:32 +00:00
*/
2020-09-10 09:08:29 +02:00
std : : unique_ptr < VirtualMachine > get ( int oid )
2008-06-17 16:27:32 +00:00
{
2020-09-10 09:08:29 +02:00
return PoolSQL : : get < VirtualMachine > ( oid ) ;
}
2008-06-17 16:27:32 +00:00
2018-10-09 11:05:08 +02:00
/**
2020-09-10 09:08:29 +02:00
* Gets a read only object from the pool ( if needed the object is loaded from the
* database ) . No object lock , other threads may work with the same object .
* @ param oid the VM unique identifier
* @ return a pointer to the VM , nullptr in case of failure
2018-10-09 11:05:08 +02:00
*/
2020-09-10 09:08:29 +02:00
std : : unique_ptr < VirtualMachine > get_ro ( int oid )
2018-10-09 11:05:08 +02:00
{
2020-09-10 09:08:29 +02:00
return PoolSQL : : get_ro < VirtualMachine > ( oid ) ;
}
2018-10-09 11:05:08 +02:00
2012-06-01 11:58:47 +02:00
/**
* Function to get a VM from the pool , string version for VM ID
*/
2020-09-10 09:08:29 +02:00
std : : unique_ptr < VirtualMachine > get ( const std : : string & oid_s )
2012-06-01 11:58:47 +02:00
{
2020-07-02 22:42:10 +02:00
std : : istringstream iss ( oid_s ) ;
int oid ;
2012-06-01 11:58:47 +02:00
iss > > oid ;
2013-03-15 17:37:47 +01:00
2012-06-01 11:58:47 +02:00
if ( iss . fail ( ) )
{
return 0 ;
}
2013-03-15 17:37:47 +01:00
2020-09-10 09:08:29 +02:00
return PoolSQL : : get < VirtualMachine > ( oid ) ;
}
2012-06-01 11:58:47 +02:00
2018-10-09 11:05:08 +02:00
/**
* Function to get a read only VM from the pool , string version for VM ID
*/
2020-09-10 09:08:29 +02:00
std : : unique_ptr < VirtualMachine > get_ro ( const std : : string & oid_s )
2018-10-09 11:05:08 +02:00
{
2020-07-02 22:42:10 +02:00
std : : istringstream iss ( oid_s ) ;
int oid ;
2018-10-09 11:05:08 +02:00
iss > > oid ;
if ( iss . fail ( ) )
{
return 0 ;
}
2020-09-10 09:08:29 +02:00
return PoolSQL : : get_ro < VirtualMachine > ( oid ) ;
}
2018-10-09 11:05:08 +02:00
2015-02-19 16:12:09 +01:00
/**
* Updates a VM in the data base . The VM SHOULD be locked . It also updates
* the previous state after executing the hooks .
* @ param objsql a pointer to the VM
*
* @ return 0 on success .
*/
2019-09-09 14:43:51 +02:00
virtual int update ( PoolObjectSQL * objsql ) ;
2015-02-19 16:12:09 +01:00
2015-02-05 17:56:21 +01:00
/**
* Gets a VM ID by its deploy_id , the dedploy_id - VM id mapping is keep
* in the import_table .
2015-05-13 10:39:30 +02:00
* @ param deploy_id to search the id for
2015-02-05 17:56:21 +01:00
* @ return - 1 if not found or VMID
2015-05-13 10:39:30 +02:00
*
2015-02-05 17:56:21 +01:00
*/
2020-07-02 22:42:10 +02:00
int get_vmid ( const std : : string & deploy_id ) ;
2015-02-05 17:56:21 +01:00
2008-06-17 16:27:32 +00:00
/**
* Function to get the IDs of running VMs
* @ param oids a vector that contains the IDs
2010-06-14 16:18:30 +02: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 16:27:32 +00:00
* @ return 0 on success
*/
int get_running (
2020-07-02 22:42:10 +02:00
std : : vector < int > & oids ,
2010-06-14 16:18:30 +02:00
int vm_limit ,
time_t last_poll ) ;
2008-06-17 16:27:32 +00:00
/**
* Function to get the IDs of pending VMs
* @ param oids a vector that contains the IDs
* @ return 0 on success
*/
int get_pending (
2020-07-02 22:42:10 +02:00
std : : vector < int > & oids ) ;
2008-06-17 16:27:32 +00:00
2014-09-11 10:34:18 +02: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
*/
2020-07-02 22:42:10 +02:00
int search ( std : : vector < int > & oids , const std : : string & where )
2014-09-11 10:34:18 +02:00
{
2020-03-04 16:05:57 +01:00
return PoolSQL : : search ( oids , one_db : : vm_table , where ) ;
2014-09-11 10:34:18 +02:00
} ;
2008-06-17 16:27:32 +00:00
//--------------------------------------------------------------------------
// Virtual Machine DB access functions
//--------------------------------------------------------------------------
2009-03-06 12:10:15 +00:00
2019-06-12 17:20:50 +02:00
/**
* Insert a new history record of a VM , the vm ' s mutex SHOULD be locked
* @ param vm pointer to the virtual machine object
* @ return 0 on success
*/
int insert_history (
VirtualMachine * vm )
{
return vm - > insert_history ( db ) ;
}
2008-06-17 16:27:32 +00:00
/**
2008-06-22 01:51:49 +00:00
* Updates the history record of a VM , the vm ' s mutex SHOULD be locked
2008-06-17 16:27:32 +00: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 01:51:49 +00:00
2008-06-17 16:27:32 +00:00
/**
2008-06-22 01:51:49 +00:00
* Updates the previous history record , the vm ' s mutex SHOULD be locked
2008-06-17 16:27:32 +00:00
* @ param vm pointer to the virtual machine object
2008-06-22 01:51:49 +00:00
* @ return 0 on success
2008-06-17 16:27:32 +00:00
*/
2008-06-22 01:51:49 +00:00
int update_previous_history (
VirtualMachine * vm )
2008-06-17 16:27:32 +00:00
{
2008-06-22 01:51:49 +00:00
return vm - > update_previous_history ( db ) ;
}
2009-03-06 12:10:15 +00:00
2019-06-12 17:20:50 +02:00
/**
* Updates the VM ' s search information
* @ param vm pointer to the virtual machine object
* @ return 0 on success
*/
int update_search (
VirtualMachine * vm )
{
return vm - > update_search ( db ) ;
}
2008-06-17 16:27:32 +00:00
/**
* Bootstraps the database table ( s ) associated to the VirtualMachine pool
2011-10-10 06:14:46 -07:00
* @ return 0 on success
2008-06-17 16:27:32 +00:00
*/
2011-10-10 06:14:46 -07:00
static int bootstrap ( SqlDB * _db )
2015-05-13 10:39:30 +02:00
{
2015-02-05 17:56:21 +01:00
int rc ;
2020-07-02 22:42:10 +02:00
std : : ostringstream oss_import ( one_db : : vm_import_db_bootstrap ) ;
2015-05-13 10:39:30 +02:00
2015-02-05 17:56:21 +01:00
rc = VirtualMachine : : bootstrap ( _db ) ;
2017-04-21 19:16:45 +02:00
rc + = _db - > exec_local_wr ( oss_import ) ;
2015-05-13 10:39:30 +02:00
return rc ;
2008-06-17 16:27:32 +00:00
} ;
2010-04-10 22:16:47 +02:00
2009-05-22 00:46:52 +00: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
2020-04-13 17:32:21 +02:00
* @ param sid first element used for pagination
* @ param eid last element used for pagination , - 1 to disable
2018-07-24 11:41:41 +02:00
* @ param desc descending order of pool elements
2009-05-22 00:46:52 +00:00
*
* @ return 0 on success
*/
2020-04-13 17:32:21 +02:00
int dump ( std : : string & oss , const std : : string & where , int sid , int eid ,
bool desc )
2010-08-24 15:51:15 +02:00
{
2020-03-04 16:05:57 +01:00
return PoolSQL : : dump ( oss , " VM_POOL " , " short_body " , one_db : : vm_table , where ,
2020-04-13 17:32:21 +02:00
sid , eid , desc ) ;
2012-05-04 17:27:57 +02:00
} ;
2010-08-24 15:51:15 +02:00
2019-03-29 12:43:59 +01:00
/**
* Dumps the VM pool in extended 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
* @ param limit parameters used for pagination
* @ param desc descending order of pool elements
*
* @ return 0 on success
*/
2020-07-02 22:42:10 +02:00
int dump_extended ( std : : string & oss , const std : : string & where ,
int sid , int eid ,
bool desc )
2019-03-29 12:43:59 +01:00
{
2020-03-04 16:05:57 +01:00
return PoolSQL : : dump ( oss , " VM_POOL " , " body " , one_db : : vm_table , where ,
2020-04-13 17:32:21 +02:00
sid , eid , desc ) ;
2019-03-29 12:43:59 +01:00
} ;
2012-05-05 03:18:25 +02:00
/**
2013-03-15 17:37:47 +01:00
* Dumps the VM accounting information in XML format . A filter can be also
2012-05-05 03:18:25 +02: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
2020-12-09 18:19:52 +01:00
* @ param time_start start date to include history record
* @ param time_end end date to include history record
2012-05-05 03:18:25 +02:00
*
* @ return 0 on success
*/
2020-07-02 22:42:10 +02:00
int dump_acct ( std : : string & oss ,
const std : : string & where ,
int time_start ,
int time_end ) ;
2012-05-16 16:09:27 +02:00
2020-12-09 18:19:52 +01:00
/**
* Dumps the VM accounting information in XML format . Faster version ,
* which doesn ' t allow filters , except time . Allows paging .
* @ param oss the output stream to dump the pool contents
* @ param time_start start date to include history record
* @ param time_end end date to include history record
* @ param sid first element used for pagination
* @ param rows number of records to retrieve , - 1 to disable
*
* @ return 0 on success
*/
int dump_acct ( std : : string & oss ,
int time_start ,
int time_end ,
int sid = 0 ,
int rows = - 1 ) ;
2014-10-30 17:45:32 +01: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
2014-11-06 13:21:21 +01:00
* @ param start_month First month ( + year ) to include . January is 1.
* Use - 1 to unset
* @ param start_year First year ( + month ) to include . e . g . 2014.
* Use - 1 to unset
* @ param end_month Last month ( + year ) to include . January is 1.
* Use - 1 to unset
* @ param end_year Last year ( + month ) to include . e . g . 2014.
* Use - 1 to unset
2014-10-30 17:45:32 +01:00
*
* @ return 0 on success
*/
2020-07-02 22:42:10 +02:00
int dump_showback ( std : : string & oss ,
const std : : string & where ,
int start_month ,
int start_year ,
int end_month ,
int end_year ) ;
2014-10-30 17:45:32 +01:00
2012-05-16 16:09:27 +02: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
2020-10-28 17:12:27 +01:00
* @ param seconds Retrieve monitor records in the last seconds
2012-05-16 16:09:27 +02:00
*
* @ return 0 on success
*/
2020-10-28 17:12:27 +01:00
int dump_monitoring ( std : : string & oss , const std : : string & where , const int seconds ) ;
2012-05-16 16:09:27 +02:00
2012-05-17 00:56:03 +02: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
*/
2020-07-02 22:42:10 +02:00
int dump_monitoring ( std : : string & oss , int vmid )
2012-05-17 00:56:03 +02:00
{
2020-07-02 22:42:10 +02:00
std : : ostringstream filter ;
2012-05-17 00:56:03 +02:00
filter < < " oid = " < < vmid ;
2020-10-28 17:12:27 +01:00
return dump_monitoring ( oss , filter . str ( ) , - 1 ) ;
2012-05-17 00:56:03 +02:00
}
2020-03-04 16:05:57 +01:00
/**
* Returns last monitoring info for a VM
* @ param vmid Virtual Machine id
*/
VirtualMachineMonitorInfo get_monitoring ( int vmid ) ;
2014-10-30 17:21:27 +01:00
/**
* Processes all the history records , and stores the monthly cost for each
* VM
2014-11-06 18:15:29 +01:00
* @ param start_month First month ( + year ) to process . January is 1.
* Use - 1 to unset
* @ param start_year First year ( + month ) to process . e . g . 2014.
* Use - 1 to unset
* @ param end_month Last month ( + year ) to process . January is 1.
* Use - 1 to unset
* @ param end_year Last year ( + month ) to process . e . g . 2014.
* Use - 1 to unset
2014-11-12 15:35:18 +01:00
* @ param error_str Returns the error reason , if any
*
* @ return 0 on success
2014-10-30 17:21:27 +01:00
*/
2014-11-12 15:35:18 +01:00
int calculate_showback (
2014-11-06 18:15:29 +01:00
int start_month ,
int start_year ,
int end_month ,
2014-11-12 15:35:18 +01:00
int end_year ,
2020-07-02 22:42:10 +02:00
std : : string & error_str ) ;
2014-10-30 17:21:27 +01:00
2015-03-18 16:28:43 +01:00
/**
* Deletes the DISK that was in the process of being attached . Releases
* Images and updates usage quotas
*
2020-09-10 09:08:29 +02:00
* @ param vm unique_ptr to VM , will be release in the method
2015-03-18 16:28:43 +01:00
*/
2020-09-10 09:08:29 +02:00
void delete_attach_disk ( std : : unique_ptr < VirtualMachine > vm ) ;
2015-03-18 16:28:43 +01:00
/**
2019-07-18 12:16:14 +02:00
* Deletes the NIC that was in the process of being attached / detached
2015-03-18 16:28:43 +01:00
*
2020-09-10 09:08:29 +02:00
* @ param vm unique_ptr to VM , will be release in the method
2015-03-18 16:28:43 +01:00
*/
2020-09-10 09:08:29 +02:00
void delete_attach_nic ( std : : unique_ptr < VirtualMachine > vm ) ;
2015-03-18 16:28:43 +01:00
2015-05-13 10:39:30 +02:00
/**
* Deletes an entry in the HV - 2 - vmid mapping table for imported VMs
* @ param deploy_id of the VM
*/
2020-07-02 22:42:10 +02:00
void drop_index ( const std : : string & deploy_id ) ;
2015-05-13 10:39:30 +02:00
2008-06-17 16:27:32 +00:00
private :
/**
* Factory method to produce VM objects
* @ return a pointer to the new VM
*/
PoolObjectSQL * create ( )
{
2013-01-18 18:34:51 +01:00
return new VirtualMachine ( - 1 , - 1 , - 1 , " " , " " , 0 , 0 ) ;
2008-06-17 16:27:32 +00:00
} ;
2012-05-16 16:09:27 +02:00
2012-12-20 18:21:30 +01:00
/**
* True or false whether to submit new VM on HOLD or not
*/
2016-04-05 12:47:21 +02:00
bool _submit_on_hold ;
2014-10-30 17:46:44 +01:00
2015-02-04 18:16:31 +01:00
/**
* Default values for cpu and memory cost
*/
2016-04-05 12:47:21 +02:00
float _default_cpu_cost ;
float _default_mem_cost ;
float _default_disk_cost ;
2015-02-04 18:16:31 +01:00
2021-04-15 11:11:41 +02:00
/**
* Switch for showback cpu and memory cost calculation
* - true : count only running VMs for CPU and MEMORY
* - false : include reservations for CPU and MEMORY
* this includes poweroff and suspended VM states
* note : datastore cost is always counted in poweroff and suspended state
*/
bool _showback_only_running ;
2014-10-30 17:46:44 +01:00
/**
2015-02-05 17:56:21 +01:00
* Callback used to get an int in the DB it is used by VM Pool in :
* - calculate_showback ( min_stime )
* - get_vmid ( vmid )
*/
int db_int_cb ( void * _min_stime , int num , char * * values , char * * names ) ;
/**
* Insert deploy_id - vmid index .
* @ param replace will replace and not insert
* @ return 0 on success
2014-10-30 17:46:44 +01:00
*/
2020-07-02 22:42:10 +02:00
int insert_index ( const std : : string & deploy_id , int vm_id , bool replace ) ;
2008-06-17 16:27:32 +00:00
} ;
2009-03-06 12:10:15 +00:00
2008-06-17 16:27:32 +00:00
# endif /*VIRTUAL_MACHINE_POOL_H_*/