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

feature #457: New Host Hook classes

This commit is contained in:
Ruben S. Montero 2010-12-26 18:53:42 +01:00
parent cd05b5a1ef
commit e2ed39e52a
3 changed files with 378 additions and 0 deletions

167
include/HostHook.h Normal file
View File

@ -0,0 +1,167 @@
/* -------------------------------------------------------------------------- */
/* Copyright 2002-2010, OpenNebula Project Leads (OpenNebula.org) */
/* */
/* 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 HOST_HOOK_H_
#define HOST_HOOK_H_
#include <vector>
#include <string>
#include "Hook.h"
#include "Host.h"
using namespace std;
/**
* This class is general Host Allocate Hook that executes a command when the
* Host is inserted in the database. The Host object is looked
*/
class HostAllocateHook : public Hook
{
public:
// -------------------------------------------------------------------------
// Init a LOCAL hook of ALLOCATE type
// -------------------------------------------------------------------------
HostAllocateHook(const string& name,
const string& cmd,
const string& args,
bool remote):
Hook(name, cmd, args, Hook::ALLOCATE, remote){};
~HostAllocateHook(){};
// -------------------------------------------------------------------------
// Hook methods
// -------------------------------------------------------------------------
void do_hook(void *arg);
};
/**
* This class provides basic functionality to store Hook 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 HostStateMapHook: public Hook
{
public:
virtual void do_hook(void *arg) = 0;
protected:
// -------------------------------------------------------------------------
// Init the Map
// -------------------------------------------------------------------------
HostStateMapHook(const string& name,
const string& cmd,
const string& args,
bool remote):
Hook(name, cmd, args, Hook::UPDATE | Hook::ALLOCATE, remote){};
virtual ~HostStateMapHook(){};
// -------------------------------------------------------------------------
// Functions to handle the VM state map
// -------------------------------------------------------------------------
/**
* Gets the state associated to the Host
* @param id of the Host
* @param state (previous) of the Host
* @return 0 if the previous state for the Host has been recorded
*/
int get_state(int id, Host::HostState &state);
/**
* Updates the state associated to the Host
* @param id of the Host
* @param state (current) of the Host
*/
void update_state (int id, Host::HostState state);
/**
* Frees the resources associated with a host
* @param id of the Host
*/
void remove_host (int id);
private:
/**
* The state Map for the Hosts
*/
static map<int,Host::HostState> host_states;
};
/**
* This class is a general Host State Hook that executes a command locally or
* remotelly when the Host gets into a given state (one shot). The Host
* object is looked when the hook is invoked.
*/
class HostStateHook : public HostStateMapHook
{
public:
// -------------------------------------------------------------------------
// -------------------------------------------------------------------------
/**
* Creates a HostStateHook
* @param name of the hook
* @param cmd for the hook
* @param args for the hook
* @param remote the hook will be executed on the target resource
* @param _state the hook will be executed when the Host enters this
* state
*/
HostStateHook(const string& name,
const string& cmd,
const string& args,
bool remote,
Host::HostState _state):
HostStateMapHook(name,cmd,args,remote), state(_state){};
~HostStateHook(){};
// -------------------------------------------------------------------------
// Hook methods
// -------------------------------------------------------------------------
void do_hook(void *arg);
private:
/**
* The target Host state
*/
Host::HostState 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 HostUpdateStateHook : public HostStateMapHook
{
public:
// -------------------------------------------------------------------------
// -------------------------------------------------------------------------
HostUpdateStateHook():
HostStateMapHook("","","",false){};
~HostUpdateStateHook(){};
// -------------------------------------------------------------------------
// Hook methods
// -------------------------------------------------------------------------
void do_hook(void *arg);
};
#endif

210
src/host/HostHook.cc Normal file
View File

@ -0,0 +1,210 @@
/* -------------------------------------------------------------------------- */
/* Copyright 2002-2010, OpenNebula Project Leads (OpenNebula.org) */
/* */
/* 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. */
/* -------------------------------------------------------------------------- */
#include "HostHook.h"
#include "Host.h"
#include "Nebula.h"
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
void HostAllocateHook::do_hook(void *arg)
{
Host * host;
string parsed_args = args;
size_t found;
host = static_cast<Host *>(arg);
if ( host == 0 )
{
return;
}
found = args.find("$HID");
if ( found !=string::npos )
{
ostringstream oss;
oss << host->get_oid();
parsed_args.replace(found,4,oss.str());
}
Nebula& ne = Nebula::instance();
HookManager * hm = ne.get_hm();
const HookManagerDriver * hmd = hm->get();
if ( hmd != 0 )
{
if ( remote == true )
{
hmd->execute(host->get_oid(),
name,
host->get_hostname(),
cmd,
parsed_args);
}
else
{
hmd->execute(host->get_oid(),name,cmd,parsed_args);
}
}
}
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
map<int,Host::HostState> HostStateMapHook::host_states;
// -----------------------------------------------------------------------------
int HostStateMapHook::get_state(int id, Host::HostState &state)
{
map<int,Host::HostState>::iterator it;
it = host_states.find(id);
if ( it == host_states.end() )
{
return -1;
}
state = it->second;
return 0;
}
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
void HostStateMapHook::update_state (int id, Host::HostState state)
{
map<int,Host::HostState>::iterator it;
it = host_states.find(id);
if ( it == host_states.end() )
{
host_states.insert(make_pair(id,state));
}
else
{
it->second = state;
}
}
// -----------------------------------------------------------------------------
void HostStateMapHook::remove_host (int id)
{
map<int,Host::HostState>::iterator it;
it = host_states.find(id);
if ( it != host_states.end() )
{
host_states.erase(it);
}
}
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
void HostStateHook::do_hook(void *arg)
{
Host * host;
int rc;
Host::HostState prev_state, cur_state;
host = static_cast<Host *>(arg);
if ( host == 0 )
{
return;
}
rc = get_state(host->get_oid(), prev_state);
if ( rc != 0 )
{
return;
}
cur_state = host->get_state();
if ( prev_state == cur_state ) //Still in the same state
{
return;
}
if ( cur_state == this->state )
{
string parsed_args = args;
size_t found;
found = args.find("$HID");
if ( found !=string::npos )
{
ostringstream oss;
oss << host->get_oid();
parsed_args.replace(found,4,oss.str());
}
Nebula& ne = Nebula::instance();
HookManager * hm = ne.get_hm();
const HookManagerDriver * hmd = hm->get();
if ( hmd != 0 )
{
if ( remote == true)
{
hmd->execute(host->get_oid(),
name,
host->get_hostname(),
cmd,
parsed_args);
}
else
{
hmd->execute(host->get_oid(),name,cmd,parsed_args);
}
}
}
}
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
void HostUpdateStateHook::do_hook(void *arg)
{
Host * host = static_cast<Host *>(arg);
if ( host == 0 )
{
return;
}
update_state(host->get_oid(), host->get_state());
}
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------

View File

@ -26,6 +26,7 @@ source_files=[
'HostShare.cc',
'HostPool.cc',
'ClusterPool.cc',
'HostHook.cc'
]
# Build library