Revision 0fa481f5

b/lib/backend.py
60 60
from ganeti import serializer
61 61
from ganeti import netutils
62 62
from ganeti import runtime
63
from ganeti import mcpu
63 64

  
64 65

  
65 66
_BOOT_ID_PATH = "/proc/sys/kernel/random/boot_id"
......
249 250
      master_netmask)
250 251

  
251 252

  
253
def RunLocalHooks(hook_opcode, hooks_path, env_builder_fn):
254
  """Decorator that runs hooks before and after the decorated function.
255

  
256
  @type hook_opcode: string
257
  @param hook_opcode: opcode of the hook
258
  @type hooks_path: string
259
  @param hooks_path: path of the hooks
260
  @type env_builder_fn: function
261
  @param env_builder_fn: function that returns a dictionary containing the
262
    environment variables for the hooks.
263
  @raise RPCFail: in case of pre-hook failure
264

  
265
  """
266
  def decorator(fn):
267
    def wrapper(*args, **kwargs):
268
      _, myself = ssconf.GetMasterAndMyself()
269
      nodes = ([myself], [myself])  # these hooks run locally
270

  
271
      cfg = _GetConfig()
272
      hr = HooksRunner()
273
      hm = mcpu.HooksMaster(hook_opcode, hooks_path, nodes, hr.RunLocalHooks,
274
                            None, env_builder_fn, logging.warning,
275
                            cfg.GetClusterName(), cfg.GetMasterNode())
276

  
277
      hm.RunPhase(constants.HOOKS_PHASE_PRE)
278
      result = fn(*args, **kwargs)
279
      hm.RunPhase(constants.HOOKS_PHASE_POST)
280

  
281
      return result
282
    return wrapper
283
  return decorator
284

  
285

  
252 286
def ActivateMasterIp():
253 287
  """Activate the IP address of the master daemon.
254 288

  
......
3425 3459
    # constant
3426 3460
    self._BASE_DIR = hooks_base_dir # pylint: disable=C0103
3427 3461

  
3462
  def RunLocalHooks(self, node_list, hpath, phase, env):
3463
    """Check that the hooks will be run only locally and then run them.
3464

  
3465
    """
3466
    assert len(node_list) == 1
3467
    node = node_list[0]
3468
    _, myself = ssconf.GetMasterAndMyself()
3469
    assert node == myself
3470

  
3471
    results = self.RunHooks(hpath, phase, env)
3472

  
3473
    # Return values in the form expected by HooksMaster
3474
    return {node: (None, False, results)}
3475

  
3428 3476
  def RunHooks(self, hpath, phase, env):
3429 3477
    """Run the scripts in the hooks directory.
3430 3478

  

Also available in: Unified diff