mirror of
https://github.com/OpenNebula/one.git
synced 2025-03-10 00:58:17 +03:00
Re-design of State Hooks. Added some pre-defined state hooks to VM Pool
git-svn-id: http://svn.opennebula.org/one/trunk@461 3034c82b-c49b-4eb3-8279-a7acafdc01c0
This commit is contained in:
parent
bf2f68ed93
commit
3ae441a176
@ -56,16 +56,22 @@ public:
|
||||
* The state Map is shared by all the State hooks. A maintenance hook that
|
||||
* updates the map should be added.
|
||||
*/
|
||||
class VirtualMachineStateMap
|
||||
class VirtualMachineStateMapHook: public Hook
|
||||
{
|
||||
public:
|
||||
virtual void do_hook(void *arg) = 0;
|
||||
|
||||
static VirtualMachineStateMap& instance()
|
||||
{
|
||||
static VirtualMachineStateMap vm_sm;
|
||||
protected:
|
||||
// -------------------------------------------------------------------------
|
||||
// Init the Map
|
||||
// -------------------------------------------------------------------------
|
||||
VirtualMachineStateMapHook(const string& name,
|
||||
const string& cmd,
|
||||
const string& args,
|
||||
bool remote):
|
||||
Hook(name, cmd, args, Hook::UPDATE, remote){};
|
||||
|
||||
return vm_sm;
|
||||
};
|
||||
virtual ~VirtualMachineStateMapHook(){};
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Functions to handle the VM state map
|
||||
@ -76,7 +82,9 @@ public:
|
||||
* @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);
|
||||
int get_state(int id,
|
||||
VirtualMachine::LcmState &lcm_state,
|
||||
VirtualMachine::VmState &vm_state);
|
||||
|
||||
/**
|
||||
* Updates the state associated to the VM
|
||||
@ -86,19 +94,21 @@ public:
|
||||
void update_state (int id,
|
||||
VirtualMachine::LcmState lcm_state,
|
||||
VirtualMachine::VmState vm_state);
|
||||
|
||||
private:
|
||||
// -------------------------------------------------------------------------
|
||||
// Init the Map
|
||||
// -------------------------------------------------------------------------
|
||||
VirtualMachineStateMap(){};
|
||||
|
||||
~VirtualMachineStateMap(){};
|
||||
struct VmStates
|
||||
{
|
||||
VmStates(VirtualMachine::LcmState _lcm, VirtualMachine::VmState _vm):
|
||||
lcm(_lcm), vm(_vm){};
|
||||
|
||||
VirtualMachine::LcmState lcm;
|
||||
VirtualMachine::VmState vm;
|
||||
};
|
||||
|
||||
/**
|
||||
* The state Map for the VMs
|
||||
*/
|
||||
map<int,VirtualMachine::LcmState> vm_states;
|
||||
static map<int,VmStates> vm_states;
|
||||
};
|
||||
|
||||
/**
|
||||
@ -106,11 +116,10 @@ private:
|
||||
* 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
|
||||
class VirtualMachineStateHook : public VirtualMachineStateMapHook
|
||||
{
|
||||
public:
|
||||
// -------------------------------------------------------------------------
|
||||
// Init a LOCAL hook of ALLOCATE type
|
||||
// -------------------------------------------------------------------------
|
||||
/**
|
||||
* Creates a VirtualMachineStateHook
|
||||
@ -124,8 +133,9 @@ public:
|
||||
const string& cmd,
|
||||
const string& args,
|
||||
bool remote,
|
||||
VirtualMachine::LcmState _state):
|
||||
Hook(name, cmd,args,Hook::UPDATE,remote), state(_state){};
|
||||
VirtualMachine::LcmState _lcm,
|
||||
VirtualMachine::VmState _vm):
|
||||
VirtualMachineStateMapHook(name,cmd,args,remote), lcm(_lcm), vm(_vm){};
|
||||
|
||||
~VirtualMachineStateHook(){};
|
||||
|
||||
@ -136,23 +146,29 @@ public:
|
||||
|
||||
private:
|
||||
/**
|
||||
* The target hook state
|
||||
* The target LCM state
|
||||
*/
|
||||
VirtualMachine::LcmState state;
|
||||
VirtualMachine::LcmState lcm;
|
||||
|
||||
/**
|
||||
* The target DM state
|
||||
*/
|
||||
VirtualMachine::VmState vm;
|
||||
};
|
||||
|
||||
/**
|
||||
* 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
|
||||
class VirtualMachineUpdateStateHook : public VirtualMachineStateMapHook
|
||||
{
|
||||
public:
|
||||
// -------------------------------------------------------------------------
|
||||
// -------------------------------------------------------------------------
|
||||
VirtualMachineStateMapHook():Hook("","","", Hook::UPDATE,false){};
|
||||
VirtualMachineUpdateStateHook():
|
||||
VirtualMachineStateMapHook("","","",false){};
|
||||
|
||||
~VirtualMachineStateMapHook(){};
|
||||
~VirtualMachineUpdateStateHook(){};
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Hook methods
|
||||
|
@ -53,10 +53,16 @@ void VirtualMachineAllocateHook::do_hook(void *arg)
|
||||
// -----------------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
int VirtualMachineStateMap::get_state(int id,
|
||||
VirtualMachine::LcmState &lcm_state)
|
||||
map<int,VirtualMachineStateMapHook::VmStates>
|
||||
VirtualMachineStateMapHook::vm_states;
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
int VirtualMachineStateMapHook::get_state(int id,
|
||||
VirtualMachine::LcmState &lcm_state,
|
||||
VirtualMachine::VmState &vm_state)
|
||||
{
|
||||
map<int,VirtualMachine::LcmState>::iterator it;
|
||||
map<int,VmStates>::iterator it;
|
||||
|
||||
it = vm_states.find(id);
|
||||
|
||||
@ -65,7 +71,8 @@ int VirtualMachineStateMap::get_state(int id,
|
||||
return -1;
|
||||
}
|
||||
|
||||
lcm_state = it->second;
|
||||
lcm_state = it->second.lcm;
|
||||
vm_state = it->second.vm;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@ -73,17 +80,19 @@ int VirtualMachineStateMap::get_state(int id,
|
||||
// -----------------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
void VirtualMachineStateMap::update_state (int id,
|
||||
void VirtualMachineStateMapHook::update_state (int id,
|
||||
VirtualMachine::LcmState lcm_state,
|
||||
VirtualMachine::VmState vm_state)
|
||||
VirtualMachine::VmState vm_state)
|
||||
{
|
||||
map<int,VirtualMachine::LcmState>::iterator it;
|
||||
map<int,VmStates>::iterator it;
|
||||
|
||||
it = vm_states.find(id);
|
||||
|
||||
if ( it == vm_states.end() )
|
||||
{
|
||||
vm_states.insert(make_pair(id,lcm_state));
|
||||
VmStates states(lcm_state, vm_state);
|
||||
|
||||
vm_states.insert(make_pair(id,states));
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -93,7 +102,8 @@ void VirtualMachineStateMap::update_state (int id,
|
||||
}
|
||||
else
|
||||
{
|
||||
it->second = lcm_state;
|
||||
it->second.lcm = lcm_state;
|
||||
it->second.vm = vm_state;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -107,8 +117,8 @@ void VirtualMachineStateHook::do_hook(void *arg)
|
||||
VirtualMachine * vm;
|
||||
int rc;
|
||||
|
||||
VirtualMachine::LcmState lcm_state;
|
||||
VirtualMachineStateMap& vm_sm = VirtualMachineStateMap::instance();
|
||||
VirtualMachine::LcmState prev_lcm, cur_lcm;
|
||||
VirtualMachine::VmState prev_vm, cur_vm;
|
||||
|
||||
vm = static_cast<VirtualMachine *>(arg);
|
||||
|
||||
@ -117,19 +127,26 @@ void VirtualMachineStateHook::do_hook(void *arg)
|
||||
return;
|
||||
}
|
||||
|
||||
rc = vm_sm.get_state(vm->get_oid(),lcm_state);
|
||||
rc = get_state(vm->get_oid(), prev_lcm, prev_vm);
|
||||
|
||||
if ( rc != 0 )
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if ((lcm_state != state) && (vm->get_lcm_state() == state))
|
||||
cur_lcm = vm->get_lcm_state();
|
||||
cur_vm = vm->get_state();
|
||||
|
||||
if ( prev_lcm == cur_lcm && prev_vm == cur_vm ) //Still in the same state
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if ( cur_lcm == lcm && cur_vm == this->vm )
|
||||
{
|
||||
string parsed_args;
|
||||
int rc = vm->parse_template_attribute(args, parsed_args);
|
||||
|
||||
if ( rc == 0)
|
||||
if ( vm->parse_template_attribute(args, parsed_args) == 0)
|
||||
{
|
||||
Nebula& ne = Nebula::instance();
|
||||
HookManager * hm = ne.get_hm();
|
||||
@ -158,7 +175,7 @@ void VirtualMachineStateHook::do_hook(void *arg)
|
||||
// -----------------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
void VirtualMachineStateMapHook::do_hook(void *arg)
|
||||
void VirtualMachineUpdateStateHook::do_hook(void *arg)
|
||||
{
|
||||
VirtualMachine * vm = static_cast<VirtualMachine *>(arg);
|
||||
|
||||
@ -167,9 +184,7 @@ void VirtualMachineStateMapHook::do_hook(void *arg)
|
||||
return;
|
||||
}
|
||||
|
||||
VirtualMachineStateMap& vm_sm = VirtualMachineStateMap::instance();
|
||||
|
||||
vm_sm.update_state(vm->get_oid(), vm->get_lcm_state(), vm->get_state());
|
||||
update_state(vm->get_oid(), vm->get_lcm_state(), vm->get_state());
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
@ -23,49 +23,116 @@
|
||||
/* -------------------------------------------------------------------------- */
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
||||
VirtualMachinePool::VirtualMachinePool(SqliteDB * db,
|
||||
VirtualMachinePool::VirtualMachinePool(SqliteDB * db,
|
||||
vector<const Attribute *> hook_mads)
|
||||
: PoolSQL(db,VirtualMachine::table)
|
||||
{
|
||||
const VectorAttribute * vattr;
|
||||
|
||||
string name;
|
||||
string on;
|
||||
string cmd;
|
||||
string arg;
|
||||
|
||||
|
||||
string name;
|
||||
string on;
|
||||
string cmd;
|
||||
string arg;
|
||||
string rmt;
|
||||
bool remote;
|
||||
|
||||
bool state_hook = false;
|
||||
|
||||
for (unsigned int i = 0 ; i < hook_mads.size() ; i++ )
|
||||
{
|
||||
vattr = static_cast<const VectorAttribute *>(hook_mads[i]);
|
||||
|
||||
|
||||
name = vattr->vector_value("NAME");
|
||||
on = vattr->vector_value("ON");
|
||||
cmd = vattr->vector_value("COMMAND");
|
||||
arg = vattr->vector_value("ARGUMENTS");
|
||||
|
||||
rmt = vattr->vector_value("REMOTE");
|
||||
|
||||
transform (on.begin(),on.end(),on.begin(),(int(*)(int))toupper);
|
||||
|
||||
if (name.empty())
|
||||
|
||||
if ( on.empty() || cmd.empty() )
|
||||
{
|
||||
ostringstream oss;
|
||||
|
||||
oss << "Empty ON or COMMAND attribute in VM_HOOK. Hook "
|
||||
<< "not registered!";
|
||||
Nebula::log("VM",Log::WARNING,oss);
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if ( name.empty() )
|
||||
{
|
||||
name = cmd;
|
||||
}
|
||||
|
||||
|
||||
remote = false;
|
||||
|
||||
if ( !rmt.empty() )
|
||||
{
|
||||
transform(rmt.begin(),rmt.end(),rmt.begin(),(int(*)(int))toupper);
|
||||
|
||||
if ( rmt == "YES" )
|
||||
{
|
||||
remote = true;
|
||||
}
|
||||
}
|
||||
|
||||
if ( on == "CREATE" )
|
||||
{
|
||||
VirtualMachineAllocateHook * hook;
|
||||
|
||||
|
||||
hook = new VirtualMachineAllocateHook(name,cmd,arg);
|
||||
|
||||
|
||||
add_hook(hook);
|
||||
}
|
||||
else if ( on == "RUNNING" )
|
||||
{
|
||||
VirtualMachineStateHook * hook;
|
||||
|
||||
hook = new VirtualMachineStateHook(name, cmd, arg, remote,
|
||||
VirtualMachine::RUNNING, VirtualMachine::ACTIVE);
|
||||
add_hook(hook);
|
||||
|
||||
state_hook = true;
|
||||
}
|
||||
else if ( on == "SHUTDOWN" )
|
||||
{
|
||||
VirtualMachineStateHook * hook;
|
||||
|
||||
hook = new VirtualMachineStateHook(name, cmd, arg, remote,
|
||||
VirtualMachine::LCM_INIT, VirtualMachine::DONE);
|
||||
add_hook(hook);
|
||||
|
||||
state_hook = true;
|
||||
}
|
||||
else if ( on == "STOP" )
|
||||
{
|
||||
VirtualMachineStateHook * hook;
|
||||
|
||||
hook = new VirtualMachineStateHook(name, cmd, arg, remote,
|
||||
VirtualMachine::LCM_INIT, VirtualMachine::STOPPED);
|
||||
add_hook(hook);
|
||||
|
||||
state_hook = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
ostringstream oss;
|
||||
|
||||
|
||||
oss << "Unkown VM_HOOK " << on << ". Hook not registered!";
|
||||
Nebula::log("VM",Log::WARNING,oss);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ( state_hook )
|
||||
{
|
||||
VirtualMachineUpdateStateHook * hook;
|
||||
|
||||
hook = new VirtualMachineUpdateStateHook();
|
||||
|
||||
add_hook(hook);
|
||||
}
|
||||
}
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
Loading…
x
Reference in New Issue
Block a user