AMBARI-3661. Resource Management. Support adding parameters, beside the configurations. (Andrew Onischuk via dlysnichenko)
Project: http://git-wip-us.apache.org/repos/asf/incubator-ambari/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-ambari/commit/0e7b987b Tree: http://git-wip-us.apache.org/repos/asf/incubator-ambari/tree/0e7b987b Diff: http://git-wip-us.apache.org/repos/asf/incubator-ambari/diff/0e7b987b Branch: refs/heads/trunk Commit: 0e7b987bcdfda2bde0fcc5fc174d9a758cba3cc2 Parents: 87ef3f4 Author: Lisnichenko Dmitro <[email protected]> Authored: Fri Nov 1 17:19:37 2013 +0200 Committer: Lisnichenko Dmitro <[email protected]> Committed: Fri Nov 1 17:19:37 2013 +0200 ---------------------------------------------------------------------- .../resource_management/core/environment.py | 79 ++++---------------- .../python/resource_management/core/script.py | 1 - .../python/resource_management/core/utils.py | 13 +++- 3 files changed, 22 insertions(+), 71 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ambari/blob/0e7b987b/ambari-agent/src/main/python/resource_management/core/environment.py ---------------------------------------------------------------------- diff --git a/ambari-agent/src/main/python/resource_management/core/environment.py b/ambari-agent/src/main/python/resource_management/core/environment.py index 8746ce8..d6557b0 100644 --- a/ambari-agent/src/main/python/resource_management/core/environment.py +++ b/ambari-agent/src/main/python/resource_management/core/environment.py @@ -33,7 +33,6 @@ class Environment(object): self.config = AttributeDictionary() self.resources = {} self.resource_list = [] - Substitutor.default_prefixes = [] self.delayed_actions = set() self.update_config({ # current time @@ -45,7 +44,7 @@ class Environment(object): # dir where templates,failes dirs are 'basedir': basedir, # variables, which can be used in templates - 'params': ParamsAttributeDictionary(Substitutor, params), + 'params': ParamsAttributeDictionary(Substitutor, params.copy()), }) def backup_file(self, path): @@ -67,7 +66,17 @@ class Environment(object): attr = attr[pth] if overwrite or path[-1] not in attr: attr[path[-1]] = value - + + def add_params(self, params): + variables = [item for item in dir(params) if not item.startswith("__")] + + for variable in variables: + value = getattr(params, variable) + if not hasattr(value, '__call__'): + if variable in self.config.params: + raise Fail("Variable %s already exists in the resource management parameters" % variable) + self.config.params[variable] = value + def run_action(self, resource, action): self.log.debug("Performing action %s on %s" % (action, resource)) @@ -89,9 +98,6 @@ class Environment(object): self.log.info( "%s sending %s action to %s (delayed)" % (resource, action, res)) self.delayed_actions |= resource.subscriptions['delayed'] - - def set_default_prefixes(self, dict): - Substitutor.default_prefixes = dict def _check_condition(self, cond): if hasattr(cond, '__call__'): @@ -166,72 +172,13 @@ class Environment(object): class Substitutor(): log = logging.getLogger("resource_management.resource") - default_prefixes = [] - - class ExtendedTemplate(Template): - """ - This is done to support substitution of dictionaries in dictionaries - ( ':' sign) - - default is: - idpattern = r'[_a-z][_a-z0-9]*' - """ - idpattern = r'[_a-z][_a-z0-9:]*' - - @staticmethod - def _get_subdict(name, dic): - """ - "a:b:c" => a[b][c] - - doesn't use prefixes - """ - name_parts = name.split(':') - curr = dic - - for x in name_parts: - curr = curr[x] - return curr - - @staticmethod - def get_subdict(name, dic): - """ - "a:b:c" => a[b][c] - - can use prefixes - """ - prefixes = list(Substitutor.default_prefixes) - prefixes.insert(0, None) # for not prefixed case - name_parts = name.split(':') - is_found = False - result = None - - for prefix in prefixes: - curr = Substitutor._get_subdict(prefix,dic) if prefix else dic - - try: - for x in name_parts: - curr = curr[x] - except (KeyError, TypeError): - continue - - if is_found: - raise Fail("Variable ${%s} found more than one time, please check your default prefixes!" % name) - - is_found = True - result = curr - - if not result: - raise Fail("Configuration on ${%s} cannot be resolved" % name) - - return result - @staticmethod def substitute(val): env = Environment.get_instance() dic = env.config.params if dic and isinstance(val, str): - result = Substitutor.ExtendedTemplate(val).substitute(dic) + result = Template(val).substitute(dic) if '$' in val: Substitutor.log.debug("%s after substitution is %s", val, result) return result http://git-wip-us.apache.org/repos/asf/incubator-ambari/blob/0e7b987b/ambari-agent/src/main/python/resource_management/core/script.py ---------------------------------------------------------------------- diff --git a/ambari-agent/src/main/python/resource_management/core/script.py b/ambari-agent/src/main/python/resource_management/core/script.py index d64010a..44bdbcd 100644 --- a/ambari-agent/src/main/python/resource_management/core/script.py +++ b/ambari-agent/src/main/python/resource_management/core/script.py @@ -37,7 +37,6 @@ class Script(): def start(self, env, params): # TODO: just for test runs; remove - env.set_prefixes("ddd") print "Start!" pass http://git-wip-us.apache.org/repos/asf/incubator-ambari/blob/0e7b987b/ambari-agent/src/main/python/resource_management/core/utils.py ---------------------------------------------------------------------- diff --git a/ambari-agent/src/main/python/resource_management/core/utils.py b/ambari-agent/src/main/python/resource_management/core/utils.py index 856c2a6..4cd5207 100644 --- a/ambari-agent/src/main/python/resource_management/core/utils.py +++ b/ambari-agent/src/main/python/resource_management/core/utils.py @@ -1,3 +1,5 @@ +from resource_management.core.exceptions import Fail + class AttributeDictionary(object): def __init__(self, *args, **kwargs): d = kwargs @@ -76,11 +78,14 @@ class ParamsAttributeDictionary(AttributeDictionary): super(AttributeDictionary, self).__setattr__("substitutor", substitutor) def __getitem__(self, name): - val = self.substitutor.get_subdict(name, self._dict) - return self._convert_value(val) - + try: + return self._convert_value(self._dict[name]) + except KeyError as ex: + raise Fail("Configuration $%s not found!" % str(ex).strip("'")) + def copy(self): - return ParamsAttributeDictionary(self.substitutor, self._dict) + # don't allow real copying to be able to change params passed to jinja2 + return self def __unicode__(self): if isinstance(self._dict, str):
