Updated Branches: refs/heads/trunk 3e1a60aa4 -> dac8ff8b1
AMBARI-3556. Resource management. Rewrite Templates and Static Files to declarative rather than cookbook-oriented (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/dac8ff8b Tree: http://git-wip-us.apache.org/repos/asf/incubator-ambari/tree/dac8ff8b Diff: http://git-wip-us.apache.org/repos/asf/incubator-ambari/diff/dac8ff8b Branch: refs/heads/trunk Commit: dac8ff8b1429890c77d6dc76cbaba701515dd47e Parents: 3e1a60a Author: Lisnichenko Dmitro <[email protected]> Authored: Tue Oct 22 18:31:09 2013 +0300 Committer: Lisnichenko Dmitro <[email protected]> Committed: Tue Oct 22 18:31:09 2013 +0300 ---------------------------------------------------------------------- .../python/resource_management/environment.py | 32 ++++++++++++++------ .../main/python/resource_management/source.py | 24 +++++---------- 2 files changed, 30 insertions(+), 26 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ambari/blob/dac8ff8b/ambari-agent/src/main/python/resource_management/environment.py ---------------------------------------------------------------------- diff --git a/ambari-agent/src/main/python/resource_management/environment.py b/ambari-agent/src/main/python/resource_management/environment.py index 550083e..89054a0 100644 --- a/ambari-agent/src/main/python/resource_management/environment.py +++ b/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) http://git-wip-us.apache.org/repos/asf/incubator-ambari/blob/dac8ff8b/ambari-agent/src/main/python/resource_management/source.py ---------------------------------------------------------------------- diff --git a/ambari-agent/src/main/python/resource_management/source.py b/ambari-agent/src/main/python/resource_management/source.py index bea4408..ea3a25f 100644 --- a/ambari-agent/src/main/python/resource_management/source.py +++ b/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)
