AMBARI-3665. Resource Management. Implement ConfigGenerator (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/220294d5 Tree: http://git-wip-us.apache.org/repos/asf/incubator-ambari/tree/220294d5 Diff: http://git-wip-us.apache.org/repos/asf/incubator-ambari/diff/220294d5 Branch: refs/heads/trunk Commit: 220294d552b38555c3497b85b8356198fab7f23a Parents: 732d113 Author: Lisnichenko Dmitro <[email protected]> Authored: Fri Nov 1 17:26:09 2013 +0200 Committer: Lisnichenko Dmitro <[email protected]> Committed: Fri Nov 1 17:26:09 2013 +0200 ---------------------------------------------------------------------- .../python/resource_management/core/source.py | 20 +++++++------- .../libraries/providers/__init__.py | 3 ++- .../libraries/providers/config_file.py | 20 -------------- .../libraries/providers/template_config.py | 20 ++++++++++++++ .../libraries/providers/xml_config.py | 28 ++++++++++++++++++++ .../libraries/resources/__init__.py | 3 ++- .../libraries/resources/config_file.py | 12 --------- .../libraries/resources/template_config.py | 12 +++++++++ .../libraries/resources/xml_config.py | 15 +++++++++++ 9 files changed, 89 insertions(+), 44 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ambari/blob/220294d5/ambari-agent/src/main/python/resource_management/core/source.py ---------------------------------------------------------------------- diff --git a/ambari-agent/src/main/python/resource_management/core/source.py b/ambari-agent/src/main/python/resource_management/core/source.py index b27bacb..cb11bbc 100644 --- a/ambari-agent/src/main/python/resource_management/core/source.py +++ b/ambari-agent/src/main/python/resource_management/core/source.py @@ -74,33 +74,33 @@ else: return source, path, lambda: mtime == os.path.getmtime(path) class Template(Source): - def __init__(self, name, **kwargs): + def __init__(self, name, extra_imports=[], **kwargs): """ @param kwargs: Additional variables passed to template """ super(Template, self).__init__(name) params = self.env.config.params variables = checked_unite(params, kwargs) + self.imports_dict = dict((module.__name__, module) for module in extra_imports) self.context = variables.copy() if variables else {} if not hasattr(self, 'template_env'): self.template_env = JinjaEnvironment(loader=TemplateLoader(self.env), autoescape=False, undefined=StrictUndefined) + self.template = self.template_env.get_template(self.name) - + def get_content(self): - self.context.update( - env=self.env, - repr=repr, - str=str, - bool=bool, - ) + default_variables = { 'env':self.env, 'repr':repr, 'str':str, 'bool':bool } + variables = checked_unite(default_variables, self.imports_dict) + self.context.update(variables) + rendered = self.template.render(self.context) return rendered + "\n" if not rendered.endswith('\n') else rendered class InlineTemplate(Template): - def __init__(self, name, **kwargs): + def __init__(self, name, extra_imports=[], **kwargs): self.template_env = JinjaEnvironment(loader=FunctionLoader(lambda text: text)) - super(InlineTemplate, self).__init__(name, **kwargs) + super(InlineTemplate, self).__init__(name, extra_imports, **kwargs) class DownloadSource(Source): http://git-wip-us.apache.org/repos/asf/incubator-ambari/blob/220294d5/ambari-agent/src/main/python/resource_management/libraries/providers/__init__.py ---------------------------------------------------------------------- diff --git a/ambari-agent/src/main/python/resource_management/libraries/providers/__init__.py b/ambari-agent/src/main/python/resource_management/libraries/providers/__init__.py index ba4269e..19816c5 100644 --- a/ambari-agent/src/main/python/resource_management/libraries/providers/__init__.py +++ b/ambari-agent/src/main/python/resource_management/libraries/providers/__init__.py @@ -11,6 +11,7 @@ PROVIDERS = dict( ), default=dict( ExecuteHadoop="resource_management.libraries.providers.execute_hadoop.ExecuteHadoopProvider", - ConfigFile="resource_management.libraries.providers.config_file.ConfigFileProvider", + TemplateConfig="resource_management.libraries.providers.template_config.TemplateConfigProvider", + XmlConfig="resource_management.libraries.providers.xml_config.XmlConfigProvider" ), ) \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-ambari/blob/220294d5/ambari-agent/src/main/python/resource_management/libraries/providers/config_file.py ---------------------------------------------------------------------- diff --git a/ambari-agent/src/main/python/resource_management/libraries/providers/config_file.py b/ambari-agent/src/main/python/resource_management/libraries/providers/config_file.py deleted file mode 100644 index 8cc04e0..0000000 --- a/ambari-agent/src/main/python/resource_management/libraries/providers/config_file.py +++ /dev/null @@ -1,20 +0,0 @@ -import os -from resource_management import * - -class ConfigFileProvider(Provider): - def action_create(self): - template_tag = self.resource.template_tag - qualified_file_name = self.resource.name - file_name = os.path.basename(qualified_file_name) - - if not template_tag: - template_name = format("{file_name}.j2") - else: - template_name = format("{file_name}-{template_tag}.j2") - - File( qualified_file_name, - owner = self.resource.owner, - group = self.resource.group, - mode = self.resource.mode, - content = Template(template_name) - ) \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-ambari/blob/220294d5/ambari-agent/src/main/python/resource_management/libraries/providers/template_config.py ---------------------------------------------------------------------- diff --git a/ambari-agent/src/main/python/resource_management/libraries/providers/template_config.py b/ambari-agent/src/main/python/resource_management/libraries/providers/template_config.py new file mode 100644 index 0000000..6d75667 --- /dev/null +++ b/ambari-agent/src/main/python/resource_management/libraries/providers/template_config.py @@ -0,0 +1,20 @@ +import os +from resource_management import * + +class TemplateConfigProvider(Provider): + def action_create(self): + template_tag = self.resource.template_tag + qualified_file_name = self.resource.name + file_name = os.path.basename(qualified_file_name) + + if not template_tag: + template_name = format("{file_name}.j2") + else: + template_name = format("{file_name}-{template_tag}.j2") + + File( qualified_file_name, + owner = self.resource.owner, + group = self.resource.group, + mode = self.resource.mode, + content = Template(template_name) + ) \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-ambari/blob/220294d5/ambari-agent/src/main/python/resource_management/libraries/providers/xml_config.py ---------------------------------------------------------------------- diff --git a/ambari-agent/src/main/python/resource_management/libraries/providers/xml_config.py b/ambari-agent/src/main/python/resource_management/libraries/providers/xml_config.py new file mode 100644 index 0000000..9694244 --- /dev/null +++ b/ambari-agent/src/main/python/resource_management/libraries/providers/xml_config.py @@ -0,0 +1,28 @@ +import time +from resource_management import * + +class XmlConfigProvider(Provider): + def action_create(self): + filename = self.resource.filename + conf_dir = self.resource.conf_dir + + # |e - for html-like escaping of <,>,'," + config_content = InlineTemplate('''<!--{{time.asctime(time.localtime())}}--> + <configuration> + {% for key, value in configurations_dict.items() %} + <property> + <name>{{ key|e }}</name> + <value>{{ value|e }}</value> + </property> + {% endfor %} + </configuration>''', extra_imports=[time], configurations_dict=self.resource.configurations) + + + self.log.debug(format("Generating config: {conf_dir}/{filename}")) + + File (format("{conf_dir}/{filename}"), + content = config_content, + owner = self.resource.owner, + group = self.resource.group, + mode = self.resource.mode + ) \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-ambari/blob/220294d5/ambari-agent/src/main/python/resource_management/libraries/resources/__init__.py ---------------------------------------------------------------------- diff --git a/ambari-agent/src/main/python/resource_management/libraries/resources/__init__.py b/ambari-agent/src/main/python/resource_management/libraries/resources/__init__.py index 0001272..12f5455 100644 --- a/ambari-agent/src/main/python/resource_management/libraries/resources/__init__.py +++ b/ambari-agent/src/main/python/resource_management/libraries/resources/__init__.py @@ -1,2 +1,3 @@ from resource_management.libraries.resources.execute_hadoop import * -from resource_management.libraries.resources.config_file import * \ No newline at end of file +from resource_management.libraries.resources.template_config import * +from resource_management.libraries.resources.xml_config import * \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-ambari/blob/220294d5/ambari-agent/src/main/python/resource_management/libraries/resources/config_file.py ---------------------------------------------------------------------- diff --git a/ambari-agent/src/main/python/resource_management/libraries/resources/config_file.py b/ambari-agent/src/main/python/resource_management/libraries/resources/config_file.py deleted file mode 100644 index 3632c8b..0000000 --- a/ambari-agent/src/main/python/resource_management/libraries/resources/config_file.py +++ /dev/null @@ -1,12 +0,0 @@ -_all__ = ["ConfigFile"] -from resource_management.core.base import Resource, ForcedListArgument, ResourceArgument, BooleanArgument - -class ConfigFile(Resource): - action = ForcedListArgument(default="create") - path = ResourceArgument(default=lambda obj: obj.name) - mode = ResourceArgument() - owner = ResourceArgument() - group = ResourceArgument() - template_tag = ResourceArgument() - - actions = Resource.actions + ["create"] \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-ambari/blob/220294d5/ambari-agent/src/main/python/resource_management/libraries/resources/template_config.py ---------------------------------------------------------------------- diff --git a/ambari-agent/src/main/python/resource_management/libraries/resources/template_config.py b/ambari-agent/src/main/python/resource_management/libraries/resources/template_config.py new file mode 100644 index 0000000..2ff4440 --- /dev/null +++ b/ambari-agent/src/main/python/resource_management/libraries/resources/template_config.py @@ -0,0 +1,12 @@ +_all__ = ["TemplateConfig"] +from resource_management.core.base import Resource, ForcedListArgument, ResourceArgument, BooleanArgument + +class TemplateConfig(Resource): + action = ForcedListArgument(default="create") + path = ResourceArgument(default=lambda obj: obj.name) + mode = ResourceArgument() + owner = ResourceArgument() + group = ResourceArgument() + template_tag = ResourceArgument() + + actions = Resource.actions + ["create"] \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-ambari/blob/220294d5/ambari-agent/src/main/python/resource_management/libraries/resources/xml_config.py ---------------------------------------------------------------------- diff --git a/ambari-agent/src/main/python/resource_management/libraries/resources/xml_config.py b/ambari-agent/src/main/python/resource_management/libraries/resources/xml_config.py new file mode 100644 index 0000000..036329c --- /dev/null +++ b/ambari-agent/src/main/python/resource_management/libraries/resources/xml_config.py @@ -0,0 +1,15 @@ +_all__ = ["XmlConfig"] +from resource_management.core.base import Resource, ForcedListArgument, ResourceArgument, BooleanArgument + +class XmlConfig(Resource): + action = ForcedListArgument(default="create") + filename = ResourceArgument(default=lambda obj: obj.name) + + configurations = ResourceArgument() + conf_dir = ResourceArgument() + + mode = ResourceArgument() + owner = ResourceArgument() + group = ResourceArgument() + + actions = Resource.actions + ["create"] \ No newline at end of file
