Merge pull request #19741 from basepi/saltssh.jinja.newconvention.19681

Fix FunctionWrapper to allow for jinja salt.cmd.run() syntax
This commit is contained in:
Thomas S Hatch 2015-01-14 16:21:41 -07:00
commit 4158b17208

View File

@ -7,6 +7,7 @@ as ZeroMQ salt, but via ssh.
'''
# Import python libs
import json
import copy
# Import salt libs
import salt.loader
@ -27,8 +28,10 @@ class FunctionWrapper(object):
wfuncs=None,
mods=None,
fsclient=None,
cmd_prefix=None,
**kwargs):
super(FunctionWrapper, self).__init__()
self.cmd_prefix = cmd_prefix
self.wfuncs = wfuncs if isinstance(wfuncs, dict) else {}
self.opts = opts
self.mods = mods if isinstance(mods, dict) else {}
@ -41,6 +44,29 @@ class FunctionWrapper(object):
'''
Return the function call to simulate the salt local lookup system
'''
if '.' not in cmd and not self.cmd_prefix:
# Form of salt.cmd.run in Jinja -- it's expecting a subdictionary
# containing only 'cmd' module calls, in that case. Create a new
# FunctionWrapper which contains the prefix 'cmd' (again, for the
# salt.cmd.run example)
kwargs = copy.deepcopy(self.kwargs)
id_ = kwargs.pop('id_')
host = kwargs.pop('host')
return FunctionWrapper(self.opts,
id_,
host,
wfuncs=self.wfuncs,
mods=self.mods,
fsclient=self.fsclient,
cmd_prefix=cmd,
**kwargs)
if self.cmd_prefix:
# We're in an inner FunctionWrapper as created by the code block
# above. Reconstruct the original cmd in the form 'cmd.run' and
# then evaluate as normal
cmd = '{0}.{1}'.format(self.cmd_prefix, cmd)
if cmd in self.wfuncs:
return self.wfuncs[cmd]