Pārlūkot izejas kodu

AMBARI-3665. Resource Management. Implement ConfigGenerator (Andrew Onischuk via dlysnichenko)

Lisnichenko Dmitro 11 gadi atpakaļ
vecāks
revīzija
220294d552

+ 10 - 10
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):

+ 2 - 1
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"
   ),
 )

+ 1 - 1
ambari-agent/src/main/python/resource_management/libraries/providers/config_file.py → ambari-agent/src/main/python/resource_management/libraries/providers/template_config.py

@@ -1,7 +1,7 @@
 import os
 from resource_management import *
 
-class ConfigFileProvider(Provider):
+class TemplateConfigProvider(Provider):
   def action_create(self):
     template_tag = self.resource.template_tag
     qualified_file_name = self.resource.name

+ 28 - 0
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
+    )

+ 2 - 1
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 *
+from resource_management.libraries.resources.template_config import *
+from resource_management.libraries.resources.xml_config import *

+ 2 - 2
ambari-agent/src/main/python/resource_management/libraries/resources/config_file.py → ambari-agent/src/main/python/resource_management/libraries/resources/template_config.py

@@ -1,7 +1,7 @@
-_all__ = ["ConfigFile"]
+_all__ = ["TemplateConfig"]
 from resource_management.core.base import Resource, ForcedListArgument, ResourceArgument, BooleanArgument
 
-class ConfigFile(Resource):
+class TemplateConfig(Resource):
   action = ForcedListArgument(default="create")
   path = ResourceArgument(default=lambda obj: obj.name)
   mode = ResourceArgument()

+ 15 - 0
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"]