2008-06-17 20:27:32 +04:00
/* -------------------------------------------------------------------------- */
2015-09-23 16:03:22 +03:00
/* Copyright 2002-2015, OpenNebula Project, OpenNebula Systems */
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 HISTORY_H_
# define HISTORY_H_
# include "ObjectSQL.h"
using namespace std ;
/**
* The History class , it represents an execution record of a Virtual Machine .
*/
2011-03-01 19:47:20 +03:00
class History : public ObjectSQL , public ObjectXML
2008-06-17 20:27:32 +04:00
{
public :
2013-04-03 19:38:06 +04:00
enum EndReason
2008-06-17 20:27:32 +04:00
{
2013-04-19 00:17:55 +04:00
NONE = 0 , /** < History record is not closed yet */
ERROR = 1 , /** < History record was closed because of an error */
USER = 2 /** < History record was closed because of a user action */
2008-06-17 20:27:32 +04:00
} ;
2013-04-03 18:18:50 +04:00
enum VMAction
{
2013-04-19 00:17:55 +04:00
NONE_ACTION = 0 ,
MIGRATE_ACTION = 1 ,
LIVE_MIGRATE_ACTION = 2 ,
SHUTDOWN_ACTION = 3 ,
SHUTDOWN_HARD_ACTION = 4 ,
UNDEPLOY_ACTION = 5 ,
UNDEPLOY_HARD_ACTION = 6 ,
HOLD_ACTION = 7 ,
RELEASE_ACTION = 8 ,
STOP_ACTION = 9 ,
SUSPEND_ACTION = 10 ,
RESUME_ACTION = 11 ,
BOOT_ACTION = 12 ,
DELETE_ACTION = 13 ,
DELETE_RECREATE_ACTION = 14 ,
REBOOT_ACTION = 15 ,
REBOOT_HARD_ACTION = 16 ,
RESCHED_ACTION = 17 ,
UNRESCHED_ACTION = 18 ,
POWEROFF_ACTION = 19 ,
2015-04-21 16:35:07 +03:00
POWEROFF_HARD_ACTION = 20 ,
DISK_ATTACH_ACTION = 21 ,
DISK_DETACH_ACTION = 22 ,
NIC_ATTACH_ACTION = 23 ,
2015-07-13 19:24:58 +03:00
NIC_DETACH_ACTION = 24 ,
DISK_SNAPSHOT_CREATE_ACTION = 25 ,
DISK_SNAPSHOT_DELETE_ACTION = 26
2013-04-03 18:18:50 +04:00
} ;
2013-04-03 18:43:02 +04:00
static string action_to_str ( VMAction action )
2013-04-03 18:18:50 +04:00
{
string st ;
switch ( action )
{
case MIGRATE_ACTION :
st = " migrate " ;
break ;
case LIVE_MIGRATE_ACTION :
2013-04-05 01:17:55 +04:00
st = " live-migrate " ;
2013-04-03 18:18:50 +04:00
break ;
case SHUTDOWN_ACTION :
st = " shutdown " ;
break ;
case SHUTDOWN_HARD_ACTION :
st = " shutdown-hard " ;
break ;
case UNDEPLOY_ACTION :
st = " undeploy " ;
break ;
case UNDEPLOY_HARD_ACTION :
st = " undeploy-hard " ;
break ;
case HOLD_ACTION :
st = " hold " ;
break ;
case RELEASE_ACTION :
st = " release " ;
break ;
case STOP_ACTION :
st = " stop " ;
break ;
case SUSPEND_ACTION :
st = " suspend " ;
break ;
case RESUME_ACTION :
st = " resume " ;
break ;
2013-04-11 18:39:55 +04:00
case DELETE_ACTION :
st = " delete " ;
2013-04-03 18:18:50 +04:00
break ;
2013-04-11 18:39:55 +04:00
case DELETE_RECREATE_ACTION :
st = " delete-recreate " ;
2013-04-03 18:18:50 +04:00
break ;
case REBOOT_ACTION :
st = " reboot " ;
break ;
case REBOOT_HARD_ACTION :
st = " reboot-hard " ;
break ;
case RESCHED_ACTION :
st = " resched " ;
break ;
case UNRESCHED_ACTION :
st = " unresched " ;
break ;
case POWEROFF_ACTION :
st = " poweroff " ;
break ;
2013-04-03 20:20:20 +04:00
case POWEROFF_HARD_ACTION :
st = " poweroff-hard " ;
break ;
2015-04-21 16:35:07 +03:00
case DISK_ATTACH_ACTION :
st = " disk-attach " ;
break ;
case DISK_DETACH_ACTION :
st = " disk-detach " ;
break ;
case NIC_ATTACH_ACTION :
st = " nic-attach " ;
break ;
case NIC_DETACH_ACTION :
st = " nic-detach " ;
break ;
2015-07-13 19:24:58 +03:00
case DISK_SNAPSHOT_CREATE_ACTION :
st = " snap-create " ;
break ;
case DISK_SNAPSHOT_DELETE_ACTION :
st = " snap-delete " ;
break ;
2013-04-03 18:18:50 +04:00
case NONE_ACTION :
2015-04-26 17:47:00 +03:00
case BOOT_ACTION :
2013-04-03 18:18:50 +04:00
st = " none " ;
break ;
}
return st ;
} ;
2013-04-03 18:43:02 +04:00
static int action_from_str ( string & st , VMAction & action )
2013-04-03 18:18:50 +04:00
{
if ( st = = " migrate " )
{
action = MIGRATE_ACTION ;
}
2013-04-05 01:17:55 +04:00
else if ( st = = " live-migrate " )
2013-04-03 18:18:50 +04:00
{
action = LIVE_MIGRATE_ACTION ;
}
else if ( st = = " shutdown " )
{
action = SHUTDOWN_ACTION ;
}
else if ( st = = " shutdown-hard " )
{
action = SHUTDOWN_HARD_ACTION ;
}
else if ( st = = " undeploy " )
{
action = UNDEPLOY_ACTION ;
}
else if ( st = = " undeploy-hard " )
{
action = UNDEPLOY_HARD_ACTION ;
}
else if ( st = = " hold " )
{
action = HOLD_ACTION ;
}
else if ( st = = " release " )
{
action = RELEASE_ACTION ;
}
else if ( st = = " stop " )
{
action = STOP_ACTION ;
}
else if ( st = = " suspend " )
{
action = SUSPEND_ACTION ;
}
else if ( st = = " resume " )
{
action = RESUME_ACTION ;
}
2013-04-11 18:39:55 +04:00
else if ( st = = " delete " )
2013-04-03 18:18:50 +04:00
{
2013-04-11 18:39:55 +04:00
action = DELETE_ACTION ;
2013-04-03 18:18:50 +04:00
}
2013-04-11 18:39:55 +04:00
else if ( st = = " delete-recreate " )
2013-04-03 18:18:50 +04:00
{
2013-04-11 18:39:55 +04:00
action = DELETE_RECREATE_ACTION ;
2013-04-03 18:18:50 +04:00
}
else if ( st = = " reboot " )
{
action = REBOOT_ACTION ;
}
else if ( st = = " reboot-hard " )
{
action = REBOOT_HARD_ACTION ;
}
else if ( st = = " resched " )
{
action = RESCHED_ACTION ;
}
else if ( st = = " unresched " )
{
action = UNRESCHED_ACTION ;
}
else if ( st = = " poweroff " )
{
action = POWEROFF_ACTION ;
}
2013-04-03 20:20:20 +04:00
else if ( st = = " poweroff-hard " )
{
action = POWEROFF_HARD_ACTION ;
}
2015-04-21 16:35:07 +03:00
else if ( st = = " disk-attach " )
{
action = DISK_ATTACH_ACTION ;
}
else if ( st = = " disk-detach " )
{
action = DISK_DETACH_ACTION ;
}
else if ( st = = " nic-attach " )
{
action = NIC_ATTACH_ACTION ;
}
else if ( st = = " nic-detach " )
{
action = NIC_DETACH_ACTION ;
}
2015-07-13 19:24:58 +03:00
else if ( st = = " snap-create " )
{
action = DISK_SNAPSHOT_CREATE_ACTION ;
}
else if ( st = = " snap-delete " )
{
action = DISK_SNAPSHOT_DELETE_ACTION ;
}
2015-04-26 17:47:00 +03:00
else //BOOT_ACTION and others
2013-04-03 18:18:50 +04:00
{
action = NONE_ACTION ;
return - 1 ;
}
return 0 ;
} ;
2008-06-22 05:51:49 +04:00
History ( int oid , int _seq = - 1 ) ;
History (
2011-03-08 19:55:14 +03:00
int oid ,
int seq ,
int hid ,
const string & hostname ,
2013-10-25 19:09:00 +04:00
int cid ,
2011-03-08 19:55:14 +03:00
const string & vmm ,
2012-06-28 17:32:52 +04:00
const string & tmm ,
2012-06-29 18:48:43 +04:00
int ds_id ,
2012-05-08 17:42:24 +04:00
const string & vm_info ) ;
2008-06-22 05:51:49 +04:00
~ History ( ) { } ;
2009-07-09 18:34:34 +04:00
/**
* Function to write the History Record in an output stream
*/
friend ostream & operator < < ( ostream & os , const History & history ) ;
/**
* Function to print the History 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-04-11 00:16:47 +04:00
2008-06-17 20:27:32 +04:00
private :
friend class VirtualMachine ;
2010-04-11 00:16:47 +04:00
friend class VirtualMachinePool ;
2009-03-06 15:10:15 +03:00
2008-06-17 20:27:32 +04:00
// ----------------------------------------
// DataBase implementation variables
// ----------------------------------------
2012-05-05 05:18:25 +04:00
static const char * table ;
2012-10-05 15:23:44 +04:00
2008-06-17 20:27:32 +04:00
static const char * db_names ;
2009-03-06 15:10:15 +03:00
2008-06-17 20:27:32 +04:00
static const char * db_bootstrap ;
void non_persistent_data ( ) ;
2009-03-06 15:10:15 +03:00
2008-06-17 20:27:32 +04:00
// ----------------------------------------
// History fields
// ----------------------------------------
int oid ;
int seq ;
int hid ;
2013-10-25 19:09:00 +04:00
string hostname ;
int cid ;
2008-06-17 20:27:32 +04:00
string vmm_mad_name ;
2012-06-28 17:32:52 +04:00
string tm_mad_name ;
2008-06-17 20:27:32 +04:00
2012-06-29 18:48:43 +04:00
int ds_id ;
2008-06-17 20:27:32 +04:00
time_t stime ;
time_t etime ;
time_t prolog_stime ;
time_t prolog_etime ;
time_t running_stime ;
time_t running_etime ;
time_t epilog_stime ;
time_t epilog_etime ;
2013-04-03 19:38:06 +04:00
EndReason reason ;
2008-06-17 20:27:32 +04:00
2013-04-03 18:18:50 +04:00
VMAction action ;
2012-05-04 19:29:36 +04:00
string vm_info ;
2012-02-26 02:31:44 +04:00
// -------------------------------------------------------------------------
// Non-persistent history fields
// -------------------------------------------------------------------------
// Local paths
2008-11-13 19:21:17 +03:00
string transfer_file ;
string deployment_file ;
2009-03-06 15:10:15 +03:00
string context_file ;
2013-07-05 03:31:30 +04:00
string token_file ;
2009-03-06 15:10:15 +03:00
2012-02-26 02:31:44 +04:00
// Remote paths
2008-06-17 20:27:32 +04:00
string checkpoint_file ;
2008-11-13 19:21:17 +03:00
string rdeployment_file ;
2016-04-10 23:39:21 +03:00
string system_dir ;
2009-03-06 15:10:15 +03:00
2008-06-17 20:27:32 +04:00
/**
* Writes the history record in the DB
* @ param db pointer to the database .
* @ return 0 on success .
*/
2011-03-08 19:55:14 +03:00
int insert ( SqlDB * db , string & error_str )
{
error_str . clear ( ) ;
return insert_replace ( db , false ) ;
}
2008-06-17 20:27:32 +04:00
/**
* Reads the history record from the DB
* @ param db pointer to the database .
* @ return 0 on success .
*/
2010-04-11 00:16:47 +04:00
int select ( SqlDB * db ) ;
2010-08-05 21:28:28 +04:00
2008-06-17 20:27:32 +04:00
/**
2010-05-03 21:13:37 +04:00
* Updates the history record
2008-06-17 20:27:32 +04:00
* @ param db pointer to the database .
* @ return 0 on success .
*/
2011-03-08 19:55:14 +03:00
int update ( SqlDB * db )
{
return insert_replace ( db , true ) ;
}
2009-03-06 15:10:15 +03:00
2008-06-17 20:27:32 +04:00
/**
2010-05-03 21:13:37 +04:00
* Removes the all history records from the DB
2008-06-17 20:27:32 +04:00
* @ param db pointer to the database .
* @ return 0 on success .
2009-03-06 15:10:15 +03:00
*/
2010-05-03 21:13:37 +04:00
int drop ( SqlDB * db ) ;
2010-08-05 21:28:28 +04:00
2010-05-03 21:13:37 +04:00
/**
2010-08-05 21:28:28 +04:00
* Execute an INSERT or REPLACE Sql query .
2010-05-03 21:13:37 +04:00
* @ param db The SQL DB
2010-08-05 21:28:28 +04:00
* @ param replace Execute an INSERT or a REPLACE
2010-05-03 21:13:37 +04:00
* @ return 0 on success
*/
int insert_replace ( SqlDB * db , bool replace ) ;
2008-06-17 20:27:32 +04:00
/**
2010-04-11 00:16:47 +04:00
* Callback function to unmarshall a history object ( History : : select )
2008-06-17 20:27:32 +04:00
* @ param num the number of columns read from the DB
* @ para names the column names
* @ para vaues the column values
* @ return 0 on success
*/
2010-04-11 00:16:47 +04:00
int select_cb ( void * nil , int num , char * * values , char * * names ) ;
2009-07-09 18:34:34 +04:00
2012-05-04 19:29:36 +04:00
/**
* Function to print the History object into a string in
* XML format , to be stored in the DB . It includes the VM template info
* @ param xml the resulting XML string
* @ return a reference to the generated string
*/
string & to_db_xml ( string & xml ) const ;
/**
* Function to print the History object into a string in
* XML format . The VM info can be optionally included
* @ param xml the resulting XML string
* @ param database If it is true , the TEMPLATE element will be included
* @ return a reference to the generated string
*/
string & to_xml ( string & xml , bool database ) const ;
2009-07-09 18:34:34 +04:00
/**
2011-03-01 19:47:20 +03:00
* Rebuilds the object from an xml node
* @ param node The xml node pointer
*
* @ return 0 on success , - 1 otherwise
*/
2011-03-08 19:55:14 +03:00
int from_xml_node ( const xmlNodePtr node )
{
ObjectXML : : update_from_node ( node ) ;
return rebuild_attributes ( ) ;
}
2011-03-01 19:47:20 +03:00
/**
* Rebuilds the object from an xml formatted string
* @ param xml_str The xml - formatted string
*
* @ return 0 on success , - 1 otherwise
*/
2011-03-08 19:55:14 +03:00
int from_xml ( const string & xml_str )
{
ObjectXML : : update_from_str ( xml_str ) ;
return rebuild_attributes ( ) ;
}
2011-03-01 19:47:20 +03:00
/**
* Rebuilds the internal attributes using xpath
2009-07-09 18:34:34 +04:00
*/
2011-03-01 19:47:20 +03:00
int rebuild_attributes ( ) ;
2008-06-17 20:27:32 +04:00
} ;
2010-09-02 22:44:14 +04:00
# endif /*HISTORY_H_*/