Add RunLocalHooks decorator
authorAndrea Spadaccini <spadaccio@google.com>
Tue, 25 Oct 2011 14:58:41 +0000 (15:58 +0100)
committerAndrea Spadaccini <spadaccio@google.com>
Tue, 1 Nov 2011 18:37:47 +0000 (18:37 +0000)
Add the RunLocalHooks decorator, that allows the execution of hooks
locally. Also, add a RunLocalHooks method to HooksRunner, to adapt the
signature of HooksRunner.RunHooks to the one expected by HooksMaster,
and also to check that the hooks are being executed locally.

Signed-off-by: Andrea Spadaccini <spadaccio@google.com>
Reviewed-by: Michael Hanselmann <hansmi@google.com>

lib/backend.py

index a32bc6c..6c52d20 100644 (file)
@@ -60,6 +60,7 @@ from ganeti import ssconf
 from ganeti import serializer
 from ganeti import netutils
 from ganeti import runtime
+from ganeti import mcpu
 
 
 _BOOT_ID_PATH = "/proc/sys/kernel/random/boot_id"
@@ -249,6 +250,39 @@ def GetMasterInfo():
       master_netmask)
 
 
+def RunLocalHooks(hook_opcode, hooks_path, env_builder_fn):
+  """Decorator that runs hooks before and after the decorated function.
+
+  @type hook_opcode: string
+  @param hook_opcode: opcode of the hook
+  @type hooks_path: string
+  @param hooks_path: path of the hooks
+  @type env_builder_fn: function
+  @param env_builder_fn: function that returns a dictionary containing the
+    environment variables for the hooks.
+  @raise RPCFail: in case of pre-hook failure
+
+  """
+  def decorator(fn):
+    def wrapper(*args, **kwargs):
+      _, myself = ssconf.GetMasterAndMyself()
+      nodes = ([myself], [myself])  # these hooks run locally
+
+      cfg = _GetConfig()
+      hr = HooksRunner()
+      hm = mcpu.HooksMaster(hook_opcode, hooks_path, nodes, hr.RunLocalHooks,
+                            None, env_builder_fn, logging.warning,
+                            cfg.GetClusterName(), cfg.GetMasterNode())
+
+      hm.RunPhase(constants.HOOKS_PHASE_PRE)
+      result = fn(*args, **kwargs)
+      hm.RunPhase(constants.HOOKS_PHASE_POST)
+
+      return result
+    return wrapper
+  return decorator
+
+
 def ActivateMasterIp():
   """Activate the IP address of the master daemon.
 
@@ -3425,6 +3459,20 @@ class HooksRunner(object):
     # constant
     self._BASE_DIR = hooks_base_dir # pylint: disable=C0103
 
+  def RunLocalHooks(self, node_list, hpath, phase, env):
+    """Check that the hooks will be run only locally and then run them.
+
+    """
+    assert len(node_list) == 1
+    node = node_list[0]
+    _, myself = ssconf.GetMasterAndMyself()
+    assert node == myself
+
+    results = self.RunHooks(hpath, phase, env)
+
+    # Return values in the form expected by HooksMaster
+    return {node: (None, False, results)}
+
   def RunHooks(self, hpath, phase, env):
     """Run the scripts in the hooks directory.