diff --git a/salt/renderers/json_wempy.py b/salt/renderers/json_wempy.py new file mode 100644 index 0000000000..4728e89b9c --- /dev/null +++ b/salt/renderers/json_wempy.py @@ -0,0 +1,35 @@ +''' +Process json with the Wempy templating engine + +This renderer will take a json file with the Wempy template and render it to a +high data format for salt states. +''' +# Import python libs +import json +import os + +# Import salt modules +from salt.exceptions import SaltRenderError +import salt.utils.templates + + +def render(template_file, env='', sls=''): + ''' + Render the data passing the functions and grains into the rendering system + ''' + if not os.path.isfile(template_file): + return {} + + tmp_data = salt.utils.templates.wempy( + template_file, + True, + salt=__salt__, + grains=__grains__, + opts=__opts__, + pillar=__pillar__, + env=env, + sls=sls) + if not tmp_data.get('result', False): + raise SaltRenderError(tmp_data.get('data', + 'Unknown render error in yaml_wempy renderer')) + return json.loads(tmp_data['data']) diff --git a/salt/renderers/yaml_wempy.py b/salt/renderers/yaml_wempy.py new file mode 100644 index 0000000000..bc7ce0ada1 --- /dev/null +++ b/salt/renderers/yaml_wempy.py @@ -0,0 +1,47 @@ +''' +Process yaml with the Wempy templating engine + +This renderer will take a yaml file within a wempy template and render it to a +high data format for salt states. +''' + +# Import Python Modules +import os +import logging +import warnings + +# Import Salt libs +from salt.utils.yaml import CustomLoader, load +from salt.exceptions import SaltRenderError +import salt.utils.templates + +log = logging.getLogger(__name__) + + +def render(template_file, env='', sls=''): + ''' + Render the data passing the functions and grains into the rendering system + ''' + if not os.path.isfile(template_file): + return {} + + tmp_data = salt.utils.templates.wempy( + template_file, + True, + salt=__salt__, + grains=__grains__, + opts=__opts__, + pillar=__pillar__, + env=env, + sls=sls) + if not tmp_data.get('result', False): + raise SaltRenderError(tmp_data.get('data', + 'Unknown render error in yaml_wempy renderer')) + yaml_data = tmp_data['data'] + with warnings.catch_warnings(record=True) as warn_list: + data = load(yaml_data, Loader=CustomLoader) + if len(warn_list) > 0: + for item in warn_list: + log.warn("{warn} found in {file_}".format( + warn=item.message, file_=template_file)) + return data diff --git a/salt/utils/templates.py b/salt/utils/templates.py index 0c10788689..75ea2ab526 100644 --- a/salt/utils/templates.py +++ b/salt/utils/templates.py @@ -156,8 +156,50 @@ def py(sfn, string=False, **kwargs): return {'result': False, 'data': trb} +def wempy(sfn, string=False, **kwargs): + ''' + Render a wempy template, returns the location of the rendered file, + return False if render fails. + Returns:: + + {'result': bool, + 'data': } + ''' + try: + from wemplate.wemplate import TemplateParser as Template + except ImportError: + return {'result': False, + 'data': 'Failed to import wempy'} + try: + passthrough = {} + fd_, tgt = tempfile.mkstemp() + os.close(fd_) + if 'context' in kwargs: + passthrough = kwargs['context'] if isinstance(kwargs['context'], dict) else {} + for kwarg in kwargs: + if kwarg == 'context': + continue + passthrough[kwarg] = kwargs[kwarg] + data = '' + with open(sfn, 'r') as src: + template = Template(src.read()) + data = template.render(**passthrough) + if string: + salt.utils.safe_rm(tgt) + return {'result': True, + 'data': data} + with open(tgt, 'w+') as target: + target.write(data) + return {'result': True, + 'data': tgt} + except Exception: + trb = traceback.format_exc() + return {'result': False, + 'data': trb} + template_registry = { 'jinja': jinja, 'mako': mako, 'py': py, + 'wempy': wempy, }