1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-01-22 22:03:39 +03:00

Implementation of the VM State Hooks. Get rid of check function for the Hook

base class.



git-svn-id: http://svn.opennebula.org/one/trunk@459 3034c82b-c49b-4eb3-8279-a7acafdc01c0
This commit is contained in:
Rubén S. Montero 2009-04-09 00:58:43 +00:00
parent 1c9b97535b
commit ea6a1050d6
4 changed files with 257 additions and 44 deletions

View File

@ -37,7 +37,7 @@ class Hook
public:
/**
* Defines the hook type, so a whole hook class can be masked
* Defines the hook type, so a whole hook class can be masked
*/
enum HookType
{
@ -50,31 +50,24 @@ public:
// Constructor and Destructor
//--------------------------------------------------------------------------
Hook(const string &_name,
const string &_cmd,
const string &_cmd,
const string &_args,
HookType _ht,
bool _remote):
name(_name), cmd(_cmd), args(_args), hook_type(_ht), remote(_remote){};
virtual ~Hook(){};
//--------------------------------------------------------------------------
// Hook methods
//--------------------------------------------------------------------------
/**
* Returns the hook_type
*/
HookType type()
HookType type() const
{
return hook_type;
}
/**
* Check if the object where we are attached should execute de hook or not
* @param arg arguments for the hook
* @return true if the hook has to be executed
*/
virtual bool check_hook(void *arg) = 0;
/**
* Executes the hook it self (usually with the aid of the ExecutionManager)
@ -87,7 +80,7 @@ protected:
* Name of the Hook
*/
string name;
/**
* The command to be executed
*/
@ -102,7 +95,7 @@ protected:
* The Hook Type
*/
HookType hook_type;
/**
* True if the command is to be executed remotely
*/
@ -150,7 +143,7 @@ public:
void clear_hooks()
{
int sz = static_cast<int>(hooks.size());
for (int i=0; i<sz ; i++)
{
delete hooks[i];
@ -170,13 +163,11 @@ public:
for (int i=0; i<sz ; i++)
{
if ((hooks[i]->type() & hook_mask) &&
(hooks[i]->check_hook(arg) == true))
if ( hooks[i]->type() & hook_mask )
{
hooks[i]->do_hook(arg);
}
}
};
private:

View File

@ -22,11 +22,12 @@
#include <string>
#include "Hook.h"
#include "VirtualMachine.h"
using namespace std;
/**
* This class is general VM Allocate Hook that executes a command locally
* This class is general VM Allocate Hook that executes a command locally
* when the VM is inserted in the database. The VirtualMachine object is
* looked
*/
@ -37,21 +38,126 @@ public:
// Init a LOCAL hook of ALLOCATE type
// -------------------------------------------------------------------------
VirtualMachineAllocateHook(const string& name,
const string& cmd,
const string& cmd,
const string& args):
Hook(name, cmd, args, Hook::ALLOCATE, false){};
virtual ~VirtualMachineAllocateHook(){};
~VirtualMachineAllocateHook(){};
// -------------------------------------------------------------------------
// Hook methods
// -------------------------------------------------------------------------
bool check_hook(void *arg)
{
return true;
}
void do_hook(void *arg);
};
/**
* This class provides basic functionality to store VM states for state Hooks.
* The state Map is shared by all the State hooks. A maintenance hook that
* updates the map should be added.
*/
class VirtualMachineStateMap
{
public:
static VirtualMachineStateMap& instance()
{
static VirtualMachineStateMap vm_sm;
return vm_sm;
};
// -------------------------------------------------------------------------
// Functions to handle the VM state map
// -------------------------------------------------------------------------
/**
* Gets the state associated to the VM
* @param id of the VM
* @param lcm_state (previous) of the VM
* @return 0 if the previous state for the VM has been recorded
*/
int get_state(int id, VirtualMachine::LcmState &lcm_state);
/**
* Updates the state associated to the VM
* @param id of the VM
* @param lcm_state (current) of the VM
*/
void update_state (int id,
VirtualMachine::LcmState lcm_state,
VirtualMachine::VmState vm_state);
private:
// -------------------------------------------------------------------------
// Init the Map
// -------------------------------------------------------------------------
VirtualMachineStateMap(){};
~VirtualMachineStateMap(){};
/**
* The state Map for the VMs
*/
map<int,VirtualMachine::LcmState> vm_states;
};
/**
* This class is a general VM State Hook that executes a command locally or
* remotelly when the VM gets into a given state (one shot). The VirtualMachine
* object is looked when the hook is invoked.
*/
class VirtualMachineStateHook : public Hook
{
public:
// -------------------------------------------------------------------------
// Init a LOCAL hook of ALLOCATE type
// -------------------------------------------------------------------------
/**
* Creates a VirtualMachineStateHook
* @param name of the hook
* @param cmd for the hook
* @param args for the hook
* @param _state the hook will be executed when the VM enters this state
* @param remote the hook will be executed on the target resource
*/
VirtualMachineStateHook(const string& name,
const string& cmd,
const string& args,
bool remote,
VirtualMachine::LcmState _state):
Hook(name, cmd,args,Hook::UPDATE,remote), state(_state){};
~VirtualMachineStateHook(){};
// -------------------------------------------------------------------------
// Hook methods
// -------------------------------------------------------------------------
void do_hook(void *arg);
private:
/**
* The target hook state
*/
VirtualMachine::LcmState state;
};
/**
* This class implements a state Map updater, one hook of this type should be
* added in order to mantain the VM state map.
*/
class VirtualMachineStateMapHook : public Hook
{
public:
// -------------------------------------------------------------------------
// -------------------------------------------------------------------------
VirtualMachineStateMapHook():Hook(name, cmd, args, Hook::UPDATE, remote){};
~VirtualMachineStateMapHook(){};
// -------------------------------------------------------------------------
// Hook methods
// -------------------------------------------------------------------------
void do_hook(void *arg);
};
#endif

View File

@ -448,10 +448,6 @@ void TransferManager::prolog_migr_action(int vid)
xfr << vm->get_previous_hostname() << ":" << vm->get_remote_dir() << " ";
xfr << vm->get_hostname() << ":" << vm->get_remote_dir() << endl;
// ------------------------------------------------------------------------
// TODO: Context commands
// ------------------------------------------------------------------------
xfr.close();
tm_md->transfer(vid,vm->get_transfer_file());
@ -537,10 +533,6 @@ void TransferManager::prolog_resume_action(int vid)
xfr << nd.get_nebula_hostname() << ":" << vm->get_local_dir() << "/images ";
xfr << vm->get_hostname() << ":" << vm->get_remote_dir() << endl;
// ------------------------------------------------------------------------
// TODO: Context commands
// ------------------------------------------------------------------------
xfr.close();
tm_md->transfer(vid,vm->get_transfer_file());
@ -744,10 +736,6 @@ void TransferManager::epilog_stop_action(int vid)
xfr << vm->get_hostname() << ":" << vm->get_remote_dir() << " ";
xfr << nd.get_nebula_hostname() << ":" << vm->get_local_dir() << endl;
// ------------------------------------------------------------------------
// TODO: Context commands
// ------------------------------------------------------------------------
xfr.close();
tm_md->transfer(vid,vm->get_transfer_file());

View File

@ -19,31 +19,159 @@
#include "VirtualMachine.h"
#include "Nebula.h"
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
void VirtualMachineAllocateHook::do_hook(void *arg)
{
{
VirtualMachine * vm;
int rc;
string parsed_args;
vm = static_cast<VirtualMachine *>(arg);
if ( vm == 0 )
{
return;
}
rc = vm->parse_template_attribute(args, parsed_args);
if ( rc == 0)
{
Nebula& ne = Nebula::instance();
HookManager * hm = ne.get_hm();
const HookManagerDriver * hmd = hm->get();
if ( hmd != 0 )
{
hmd->execute(vm->get_oid(),name,cmd,parsed_args);
}
}
}
}
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
int VirtualMachineStateMap::get_state(int id,
VirtualMachine::LcmState &lcm_state)
{
map<int,VirtualMachine::LcmState>::iterator it;
it = vm_states.find(id);
if ( it == vm_states.end() )
{
return -1;
}
lcm_state = it->second;
return 0;
}
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
void VirtualMachineStateMap::update_state (int id,
VirtualMachine::LcmState lcm_state,
VirtualMachine::VmState vm_state)
{
map<int,VirtualMachine::LcmState>::iterator it;
it = vm_states.find(id);
if ( it == vm_states.end() )
{
vm_states.insert(make_pair(id,lcm_state));
}
else
{
if ( vm_state == VirtualMachine::DONE )
{
vm_states.erase(it);
}
else
{
it->second = lcm_state;
}
}
}
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
void VirtualMachineStateHook::do_hook(void *arg)
{
VirtualMachine * vm;
int rc;
VirtualMachine::LcmState lcm_state;
VirtualMachineStateMap& vm_sm = VirtualMachineStateMap::instance();
vm = static_cast<VirtualMachine *>(arg);
if ( vm == 0 )
{
return;
}
rc = vm_sm.get_state(vm->get_oid(),lcm_state);
if ( rc != 0 )
{
return;
}
if ((lcm_state != state) && (vm->get_lcm_state() == state))
{
string parsed_args;
int rc = vm->parse_template_attribute(args, parsed_args);
if ( rc == 0)
{
Nebula& ne = Nebula::instance();
HookManager * hm = ne.get_hm();
const HookManagerDriver * hmd = hm->get();
if ( hmd != 0 )
{
if ( ! remote )
{
hmd->execute(vm->get_oid(),name,cmd,parsed_args);
}
else if ( vm->hasHistory() )
{
hmd->execute(vm->get_oid(),
name,
vm->get_hostname(),
cmd,
parsed_args);
}
}
}
}
}
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
void VirtualMachineStateMapHook::do_hook(void *arg)
{
VirtualMachine * vm = static_cast<VirtualMachine *>(arg);
if ( vm == 0 )
{
return;
}
VirtualMachineStateMap& vm_sm = VirtualMachineStateMap::instance();
vm_sm.update_state(vm->get_oid(), vm->get_lcm_state(), vm->get_state());
}
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------