From 43d552aea2b497ab960e4ad6b080d1e2801ba4cb Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Sun, 7 Oct 2012 23:20:28 +0200 Subject: [PATCH] feature #1013: Abstraction of current Allocate and Remove hooks --- include/Hook.h | 89 ++++++++++++++++++++++++++++++++++++ include/HostHook.h | 49 ++++++++++++++++---- include/VirtualMachineHook.h | 17 ++----- src/hm/SConstruct | 1 + src/host/HostHook.cc | 65 +------------------------- src/nebula/SConstruct | 2 +- src/vm/VirtualMachineHook.cc | 55 +--------------------- 7 files changed, 137 insertions(+), 141 deletions(-) diff --git a/include/Hook.h b/include/Hook.h index a080079e5c..33f144fc24 100644 --- a/include/Hook.h +++ b/include/Hook.h @@ -25,6 +25,8 @@ using namespace std; //Forward definition of Hookable class Hookable; +class PoolObjectSQL; + /** * This class is an abstract representation of a hook, provides a method to * check if the hook has to be executed, and a method to invoke the hook. The @@ -74,6 +76,17 @@ public: */ virtual void do_hook(void *arg) = 0; + /** + * Parses the arguments of the hook using a generic ID identifier, and + * the target object. $TEMPLATE will be the base64 encoding of the + * template and the id string the oid of the object + * @param obj pointer to the object executing the hook for + * @param id_str id for the object (e.g. $VMID, $HID...) + * @param the resulting parser arguments + */ + void parse_hook_arguments(PoolObjectSQL * obj, + const string& id_str, + string& parsed); protected: /** * Name of the Hook @@ -101,6 +114,82 @@ protected: bool remote; }; +/** + * This class is general ObjectSQL Hook for allocation and removal. + * The object is looked when the hook is executed + */ +class AllocateRemoveHook : public Hook +{ +protected: + AllocateRemoveHook(const string& name, + const string& cmd, + const string& args, + int hook_type, + bool remote, + const char * _id_name): + Hook(name, cmd, args, hook_type, remote), id_name(_id_name){}; + + virtual ~AllocateRemoveHook(){}; + + // ------------------------------------------------------------------------- + // Hook methods + // ------------------------------------------------------------------------- + + void do_hook(void *arg); + + virtual string& remote_host(PoolObjectSQL *obj, string& hostname) + { + hostname.clear(); + + return hostname; + }; + +private: + + string id_name; +}; + +/** + * This class is general ObjectSQL Allocate Hook that executes a command + * when the ObjectSQL is inserted in the database. The object is looked when + * the hook is executed + */ +class AllocateHook : public AllocateRemoveHook +{ +public: + // ------------------------------------------------------------------------- + // Init a hook of ALLOCATE type + // ------------------------------------------------------------------------- + AllocateHook(const string& name, + const string& cmd, + const string& args, + bool remote, + const char * id_name): + AllocateRemoveHook(name, cmd, args, Hook::ALLOCATE, remote, id_name){}; + + virtual ~AllocateHook(){}; +}; + +/** + * This class is general ObjectSQL Remove Hook that executes a command + * when the ObjectSQL is inserted in the database. + */ +class RemoveHook : public AllocateRemoveHook +{ +public: + // ------------------------------------------------------------------------- + // Init a hook of ALLOCATE type + // ------------------------------------------------------------------------- + RemoveHook(const string& name, + const string& cmd, + const string& args, + bool remote, + const char * id_name): + AllocateRemoveHook(name, cmd, args, Hook::REMOVE, remote, id_name){}; + + virtual ~RemoveHook(){}; +}; + /** * Objects that inherits from hookable will allow others to hook into it. The * Hookable interface handles the list of hooks and provide a method to invoke diff --git a/include/HostHook.h b/include/HostHook.h index cb48bead0c..51cda24057 100644 --- a/include/HostHook.h +++ b/include/HostHook.h @@ -25,29 +25,58 @@ using namespace std; +#define HID_HOOK_NAME "$HID" + /** * 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 +class HostAllocateHook : public AllocateHook { 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){}; + AllocateHook(name, cmd, args, remote, HID_HOOK_NAME){}; - ~HostAllocateHook(){}; + virtual ~HostAllocateHook(){}; - // ------------------------------------------------------------------------- - // Hook methods - // ------------------------------------------------------------------------- +private: - void do_hook(void *arg); + string& remote_host(PoolObjectSQL *obj, string& hostname) + { + Host * host = static_cast(obj); + hostname = host->get_name(); + + return hostname; + } +}; + +/** + * This class is general Host Remove Hook that executes a command when the + * Host is dropped from the database. The Host object is looked + */ +class HostRemoveHook : public RemoveHook +{ +public: + HostRemoveHook(const string& name, + const string& cmd, + const string& args, + bool remote): + RemoveHook(name, cmd, args, remote, HID_HOOK_NAME){}; + + virtual ~HostRemoveHook(){}; + +private: + + string& remote_host(PoolObjectSQL *obj, string& hostname) + { + Host * host = static_cast(obj); + hostname = host->get_name(); + + return hostname; + } }; /** diff --git a/include/VirtualMachineHook.h b/include/VirtualMachineHook.h index f8c142361d..48f78071e7 100644 --- a/include/VirtualMachineHook.h +++ b/include/VirtualMachineHook.h @@ -25,29 +25,22 @@ using namespace std; +#define VMID_HOOK_NAME "$VMID" + /** * 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 */ -class VirtualMachineAllocateHook : public Hook +class VirtualMachineAllocateHook : public AllocateHook { public: - // ------------------------------------------------------------------------- - // Init a LOCAL hook of ALLOCATE type - // ------------------------------------------------------------------------- VirtualMachineAllocateHook(const string& name, const string& cmd, const string& args): - Hook(name, cmd, args, Hook::ALLOCATE, false){}; + AllocateHook(name, cmd, args, false, VMID_HOOK_NAME){}; - ~VirtualMachineAllocateHook(){}; - - // ------------------------------------------------------------------------- - // Hook methods - // ------------------------------------------------------------------------- - - void do_hook(void *arg); + virtual ~VirtualMachineAllocateHook(){}; }; /** diff --git a/src/hm/SConstruct b/src/hm/SConstruct index 5c46da165f..b5d62c88f2 100755 --- a/src/hm/SConstruct +++ b/src/hm/SConstruct @@ -22,6 +22,7 @@ lib_name='nebula_hm' # Sources to generate the library source_files=[ + 'Hook.cc', 'HookManager.cc', 'HookManagerDriver.cc' ] diff --git a/src/host/HostHook.cc b/src/host/HostHook.cc index c3857406fd..7dc1e624b1 100644 --- a/src/host/HostHook.cc +++ b/src/host/HostHook.cc @@ -18,69 +18,6 @@ #include "Host.h" #include "Nebula.h" -// ----------------------------------------------------------------------------- -// ----------------------------------------------------------------------------- -static void parse_host_arguments(Host *host, string& parsed) -{ - size_t found; - - found = parsed.find("$HID"); - - if ( found !=string::npos ) - { - ostringstream oss; - oss << host->get_oid(); - - parsed.replace(found,4,oss.str()); - } - - found = parsed.find("$TEMPLATE"); - - if ( found != string::npos ) - { - string templ; - parsed.replace(found,9,host->to_xml64(templ)); - } -} -// ----------------------------------------------------------------------------- -// ----------------------------------------------------------------------------- - -void HostAllocateHook::do_hook(void *arg) -{ - Host * host; - - string parsed_args = args; - - host = static_cast(arg); - - if ( host == 0 ) - { - return; - } - - parse_host_arguments(host,parsed_args); - - 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_name(), - cmd, - parsed_args); - } - else - { - hmd->execute(host->get_oid(),name,cmd,parsed_args); - } - } -} - // ----------------------------------------------------------------------------- // ----------------------------------------------------------------------------- @@ -172,7 +109,7 @@ void HostStateHook::do_hook(void *arg) { string parsed_args = args; - parse_host_arguments(host,parsed_args); + parse_hook_arguments(host, HID_HOOK_NAME, parsed_args); Nebula& ne = Nebula::instance(); HookManager * hm = ne.get_hm(); diff --git a/src/nebula/SConstruct b/src/nebula/SConstruct index ccfcf1048c..ceafbc245e 100644 --- a/src/nebula/SConstruct +++ b/src/nebula/SConstruct @@ -36,7 +36,6 @@ env.Prepend(LIBS=[ 'nebula_vmm', 'nebula_lcm', 'nebula_im', - 'nebula_hm', 'nebula_rm', 'nebula_dm', 'nebula_tm', @@ -55,6 +54,7 @@ env.Prepend(LIBS=[ 'nebula_vm', 'nebula_vmtemplate', 'nebula_document', + 'nebula_hm', 'nebula_common', 'nebula_sql', 'nebula_log', diff --git a/src/vm/VirtualMachineHook.cc b/src/vm/VirtualMachineHook.cc index 1ee9d85378..47b0aaa9eb 100644 --- a/src/vm/VirtualMachineHook.cc +++ b/src/vm/VirtualMachineHook.cc @@ -21,59 +21,6 @@ // ----------------------------------------------------------------------------- // ----------------------------------------------------------------------------- -static void parse_vm_arguments(VirtualMachine *vm, string& parsed) -{ - size_t found; - - found = parsed.find("$VMID"); - - if ( found !=string::npos ) - { - ostringstream oss; - oss << vm->get_oid(); - - parsed.replace(found,5,oss.str()); - } - - found = parsed.find("$TEMPLATE"); - - if ( found != string::npos ) - { - string templ; - parsed.replace(found,9,vm->to_xml64(templ)); - } -} - -// ----------------------------------------------------------------------------- -// ----------------------------------------------------------------------------- - -void VirtualMachineAllocateHook::do_hook(void *arg) -{ - VirtualMachine * vm; - string parsed_args = args; - - vm = static_cast(arg); - - if ( vm == 0 ) - { - return; - } - - parse_vm_arguments(vm, parsed_args); - - 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); - } -} - -// ----------------------------------------------------------------------------- -// ----------------------------------------------------------------------------- - map VirtualMachineStateMapHook::vm_states; @@ -167,7 +114,7 @@ void VirtualMachineStateHook::do_hook(void *arg) { string parsed_args = args; - parse_vm_arguments(vm,parsed_args); + parse_hook_arguments(vm, VMID_HOOK_NAME, parsed_args); Nebula& ne = Nebula::instance(); HookManager * hm = ne.get_hm();