Revision 20d38e8a lib/backend.py

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"
......
244 245
  return (master_netdev, master_ip, master_node, primary_ip_family)
245 246

  
246 247

  
248
def RunLocalHooks(hook_opcode, hooks_path, env_builder_fn):
249
  """Decorator that runs hooks before and after the decorated function.
250

  
251
  @type hook_opcode: string
252
  @param hook_opcode: opcode of the hook
253
  @type hooks_path: string
254
  @param hooks_path: path of the hooks
255
  @type env_builder_fn: function
256
  @param env_builder_fn: function that returns a dictionary containing the
257
    environment variables for the hooks.
258
  @raise RPCFail: in case of pre-hook failure
259

  
260
  """
261
  def decorator(fn):
262
    def wrapper(*args, **kwargs):
263
      _, myself = ssconf.GetMasterAndMyself()
264
      nodes = ([myself], [myself])  # these hooks run locally
265

  
266
      cfg = _GetConfig()
267
      hr = HooksRunner()
268
      hm = mcpu.HooksMaster(hook_opcode, hooks_path, nodes, hr.RunLocalHooks,
269
                            None, env_builder_fn, logging.warning,
270
                            cfg.GetClusterName(), cfg.GetMasterNode())
271

  
272
      hm.RunPhase(constants.HOOKS_PHASE_PRE)
273
      result = fn(*args, **kwargs)
274
      hm.RunPhase(constants.HOOKS_PHASE_POST)
275

  
276
      return result
277
    return wrapper
278
  return decorator
279

  
280

  
247 281
def ActivateMasterIp():
248 282
  """Activate the IP address of the master daemon.
249 283

  
......
3363 3397
    # constant
3364 3398
    self._BASE_DIR = hooks_base_dir # pylint: disable=C0103
3365 3399

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

  
3403
    """
3404
    assert len(node_list) == 1
3405
    node = node_list[0]
3406
    _, myself = ssconf.GetMasterAndMyself()
3407
    assert node == myself
3408

  
3409
    results = self.RunHooks(hpath, phase, env)
3410

  
3411
    # Return values in the form expected by HooksMaster
3412
    return {node: (None, False, results)}
3413

  
3366 3414
  def RunHooks(self, hpath, phase, env):
3367 3415
    """Run the scripts in the hooks directory.
3368 3416

  

Also available in: Unified diff