mirror of
https://github.com/valitydev/salt.git
synced 2024-11-09 01:36:48 +00:00
Merge pull request #1603 from gcw/develop
initial wempy template support (json and yaml); minimal testing
This commit is contained in:
commit
a100eb9406
35
salt/renderers/json_wempy.py
Normal file
35
salt/renderers/json_wempy.py
Normal file
@ -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'])
|
47
salt/renderers/yaml_wempy.py
Normal file
47
salt/renderers/yaml_wempy.py
Normal file
@ -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
|
@ -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': <Error data or rendered file path>}
|
||||
'''
|
||||
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,
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user