Explorar el Código

AMBARI-3556. Resource management. Rewrite Templates and Static Files to declarative rather than cookbook-oriented (Andrew Onischuk via dlysnichenko)

Lisnichenko Dmitro hace 11 años
padre
commit
dac8ff8b14

+ 22 - 10
ambari-agent/src/main/python/resource_management/environment.py

@@ -17,28 +17,40 @@ from resource_management.system import System
 class Environment(object):
   _instances = []
 
-  def __init__(self):
+  def __init__(self, basedir=None, params=None):
+    """
+    @param basedir: basedir/files, basedir/templates are the places where templates / static files
+    are looked up
+    @param params: configurations dictionary (this will be accessible in the templates)
+    """
     self.log = logging.getLogger("resource_management")
-    self.reset()
+    self.reset(basedir, params)
 
-  def reset(self):
+  def reset(self, basedir, params):
     self.system = System.get_instance()
     self.config = AttributeDictionary()
     self.resources = {}
     self.resource_list = []
     self.delayed_actions = set()
     self.update_config({
+      # current time
       'date': datetime.now(),
-      'resource_management.backup.path': '/tmp/resource_management/backup',
-      'resource_management.backup.prefix': datetime.now().strftime("%Y%m%d%H%M%S"),
+      # backups here files which were rewritten while executing File resource
+      'backup.path': '/tmp/resource_management/backup',
+      # prefix for this files 
+      'backup.prefix': datetime.now().strftime("%Y%m%d%H%M%S"),
+      # dir where templates,failes dirs are 
+      'basedir': basedir, 
+      # variables, which can be used in templates
+      'params': params, 
     })
 
   def backup_file(self, path):
-    if self.config.kokki.backup:
-      if not os.path.exists(self.config.kokki.backup.path):
-        os.makedirs(self.config.kokki.backup.path, 0700)
-      new_name = self.config.kokki.backup.prefix + path.replace('/', '-')
-      backup_path = os.path.join(self.config.kokki.backup.path, new_name)
+    if self.config.backup:
+      if not os.path.exists(self.config.backup.path):
+        os.makedirs(self.config.backup.path, 0700)
+      new_name = self.config.backup.prefix + path.replace('/', '-')
+      backup_path = os.path.join(self.config.backup.path, new_name)
       self.log.info("backing up %s to %s" % (path, backup_path))
       shutil.copy(path, backup_path)
 

+ 8 - 16
ambari-agent/src/main/python/resource_management/source.py

@@ -27,13 +27,8 @@ class StaticFile(Source):
     self.env = env or environment.Environment.get_instance()
 
   def get_content(self):
-    try:
-      cookbook, name = self.name.split('/', 1)
-    except ValueError:
-      raise Fail(
-        "[StaticFile(%s)] Path must include cookbook name (e.g. 'nginx/nginx.conf')" % self.name)
-    cb = self.env.cookbooks[cookbook]
-    path = os.path.join(cb.path, "files", name)
+    basedir = self.env.config.basedir
+    path = os.path.join(basedir, "files", self.name)
     with open(path, "rb") as fp:
       return fp.read()
 
@@ -49,16 +44,11 @@ else:
     def __init__(self, env=None):
       self.env = env or environment.Environment.get_instance()
 
-    def get_source(self, environment, template):
-      try:
-        cookbook, name = template.split('/', 1)
-      except ValueError:
-        raise Fail(
-          "[Template(%s)] Path must include cookbook name (e.g. 'nginx/nginx.conf.j2')" % template)
-      cb = self.env.cookbooks[cookbook]
-      path = os.path.join(cb.path, "templates", name)
+    def get_source(self, environment, template_name):
+      basedir = self.env.config.basedir
+      path = os.path.join(basedir, "templates", template_name)
       if not os.path.exists(path):
-        raise TemplateNotFound("%s at %s" % (template, path))
+        raise TemplateNotFound("%s at %s" % (template_name, path))
       mtime = os.path.getmtime(path)
       with open(path, "rb") as fp:
         source = fp.read().decode('utf-8')
@@ -68,6 +58,8 @@ else:
     def __init__(self, name, variables=None, env=None):
       self.name = name
       self.env = env or environment.Environment.get_instance()
+      params = self.env.config.params
+      variables = params if params else variables
       self.context = variables.copy() if variables else {}
       self.template_env = Environment(loader=TemplateLoader(self.env),
                                       autoescape=False)