Merge pull request #25680 from basepi/jinja.alias.25049

[2015.5] Move cmd.run jinja aliasing to a wrapper class to prevent side effects
This commit is contained in:
Colton Myers 2015-07-24 13:52:10 -06:00
commit 18c9d5454d
2 changed files with 64 additions and 6 deletions

View file

@ -41,6 +41,68 @@ TEMPLATE_DIRNAME = os.path.join(saltpath[0], 'templates')
SLS_ENCODING = 'utf-8' # this one has no BOM.
SLS_ENCODER = codecs.getencoder(SLS_ENCODING)
ALIAS_WARN = (
'Starting in 2015.5, cmd.run uses python_shell=False by default, '
'which doesn\'t support shellisms (pipes, env variables, etc). '
'cmd.run is currently aliased to cmd.shell to prevent breakage. '
'Please switch to cmd.shell or set python_shell=True to avoid '
'breakage in the future, when this aliasing is removed.'
)
ALIASES = {
'cmd.run': 'cmd.shell',
'cmd': {'run': 'shell'},
}
class AliasedLoader(object):
'''
Light wrapper around the LazyLoader to redirect 'cmd.run' calls to
'cmd.shell', for easy use of shellisms during templating calls
Dotted aliases ('cmd.run') must resolve to another dotted alias
(e.g. 'cmd.shell')
Non-dotted aliases ('cmd') must resolve to a dictionary of function
aliases for that module (e.g. {'run': 'shell'})
'''
def __init__(self, wrapped):
self.wrapped = wrapped
def __getitem__(self, name):
if name in ALIASES:
salt.utils.warn_until('Nitrogen', ALIAS_WARN)
return self.wrapped[ALIASES[name]]
else:
return self.wrapped[name]
def __getattr__(self, name):
if name in ALIASES:
salt.utils.warn_until('Nitrogen', ALIAS_WARN)
return AliasedModule(getattr(self.wrapped, name), ALIASES[name])
else:
return getattr(self.wrapped, name)
class AliasedModule(object):
'''
Light wrapper around module objects returned by the LazyLoader's getattr
for the purposes of `salt.cmd.run()` syntax in templates
Allows for aliasing specific functions, such as `run` to `shell` for easy
use of shellisms during templating calls
'''
def __init__(self, wrapped, aliases):
self.aliases = aliases
self.wrapped = wrapped
def __getattr__(self, name):
if name in self.aliases:
salt.utils.warn_until('Nitrogen', ALIAS_WARN)
return getattr(self.wrapped, self.aliases[name])
else:
return getattr(self.wrapped, name)
def wrap_tmpl_func(render_str):
@ -57,11 +119,7 @@ def wrap_tmpl_func(render_str):
# Alias cmd.run to cmd.shell to make python_shell=True the default for
# templated calls
if 'salt' in kws:
if 'cmd.run' in kws['salt'] and 'cmd.shell' in kws['salt']:
kws['salt']['cmd.run'] = kws['salt']['cmd.shell']
if 'run' in kws['salt'].get('cmd', {}) \
and 'shell' in kws['salt'].get('cmd', {}):
kws['salt']['cmd']['run'] = kws['salt']['cmd']['shell']
kws['salt'] = AliasedLoader(kws['salt'])
# We want explicit context to overwrite the **kws
kws.update(context)

View file

@ -83,7 +83,7 @@ class SaltStackVersion(object):
'Beryllium' : (MAX_SIZE - 105, 0),
'Boron' : (MAX_SIZE - 104, 0),
'Carbon' : (MAX_SIZE - 103, 0),
#'Nitrogen' : (MAX_SIZE - 102, 0),
'Nitrogen' : (MAX_SIZE - 102, 0),
#'Oxygen' : (MAX_SIZE - 101, 0),
#'Fluorine' : (MAX_SIZE - 100, 0),
#'Neon' : (MAX_SIZE - 99 , 0),