+ return (master_netdev, master_ip, master_node, primary_ip_family,
+ 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. Will get all the parameters of the
+ decorated function.
+ @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
+
+ env_fn = compat.partial(env_builder_fn, *args, **kwargs)
+
+ cfg = _GetConfig()
+ hr = HooksRunner()
+ hm = mcpu.HooksMaster(hook_opcode, hooks_path, nodes, hr.RunLocalHooks,
+ None, env_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 _BuildMasterIpEnv(master_params, use_external_mip_script=None):
+ """Builds environment variables for master IP hooks.
+
+ @type master_params: L{objects.MasterNetworkParameters}
+ @param master_params: network parameters of the master
+ @type use_external_mip_script: boolean
+ @param use_external_mip_script: whether to use an external master IP
+ address setup script (unused, but necessary per the implementation of the
+ _RunLocalHooks decorator)
+
+ """
+ # pylint: disable=W0613
+ ver = netutils.IPAddress.GetVersionFromAddressFamily(master_params.ip_family)
+ env = {
+ "MASTER_NETDEV": master_params.netdev,
+ "MASTER_IP": master_params.ip,
+ "MASTER_NETMASK": str(master_params.netmask),
+ "CLUSTER_IP_VERSION": str(ver),
+ }
+
+ return env
+
+
+def _RunMasterSetupScript(master_params, action, use_external_mip_script):
+ """Execute the master IP address setup script.
+
+ @type master_params: L{objects.MasterNetworkParameters}
+ @param master_params: network parameters of the master
+ @type action: string
+ @param action: action to pass to the script. Must be one of
+ L{backend._MASTER_START} or L{backend._MASTER_STOP}
+ @type use_external_mip_script: boolean
+ @param use_external_mip_script: whether to use an external master IP
+ address setup script
+ @raise backend.RPCFail: if there are errors during the execution of the
+ script
+
+ """
+ env = _BuildMasterIpEnv(master_params)
+
+ if use_external_mip_script:
+ setup_script = constants.EXTERNAL_MASTER_SETUP_SCRIPT
+ else:
+ setup_script = constants.DEFAULT_MASTER_SETUP_SCRIPT
+
+ result = utils.RunCmd([setup_script, action], env=env, reset_env=True)
+
+ if result.failed:
+ _Fail("Failed to %s the master IP. Script return value: %s" %
+ (action, result.exit_code), log=True)
+
+
+@RunLocalHooks(constants.FAKE_OP_MASTER_TURNUP, "master-ip-turnup",
+ _BuildMasterIpEnv)
+def ActivateMasterIp(master_params, use_external_mip_script):
+ """Activate the IP address of the master daemon.