Merge pull request #27725 from basepi/states.cross.call.27481

Fix global injection for state cross calls
This commit is contained in:
Colton Myers 2015-10-06 15:02:14 -06:00
commit d67e8c5c2c
2 changed files with 36 additions and 5 deletions

View file

@ -16,6 +16,7 @@ import time
import logging
import inspect
import tempfile
import functools
from collections import MutableMapping
from zipimport import zipimporter
@ -23,6 +24,7 @@ from zipimport import zipimporter
from salt.exceptions import LoaderError
from salt.template import check_render_pipe_str
from salt.utils.decorators import Depends
from salt.utils import context
import salt.utils.lazy
import salt.utils.odict
import salt.utils.event
@ -846,6 +848,7 @@ class LazyLoader(salt.utils.lazy.LazyDict):
virtual_enable=True,
): # pylint: disable=W0231
self.inject_globals = {}
self.opts = self.__prep_mod_opts(opts)
self.module_dirs = module_dirs
@ -879,6 +882,17 @@ class LazyLoader(salt.utils.lazy.LazyDict):
_generate_module('{0}.ext'.format(self.loaded_base_name))
_generate_module('{0}.ext.{1}'.format(self.loaded_base_name, tag))
def __getitem__(self, item):
'''
Override the __getitem__ in order to decorate the returned function if we need
to last-minute inject globals
'''
func = super(LazyLoader, self).__getitem__(item)
if self.inject_globals:
return global_injector_decorator(self.inject_globals)(func)
else:
return func
def __getattr__(self, mod_name):
'''
Allow for "direct" attribute access-- this allows jinja templates to
@ -1452,3 +1466,20 @@ class LazyLoader(salt.utils.lazy.LazyDict):
return (False, module_name, error_reason)
return (True, module_name, None)
def global_injector_decorator(inject_globals):
'''
Decorator used by the LazyLoader to inject globals into a function at
execute time.
globals
Dictionary with global variables to inject
'''
def inner_decorator(f):
@functools.wraps(f)
def wrapper(*args, **kwargs):
with context.func_globals_inject(f, **inject_globals):
return f(*args, **kwargs)
return wrapper
return inner_decorator

View file

@ -33,7 +33,7 @@ import salt.fileclient
import salt.utils.event
import salt.utils.url
import salt.syspaths as syspaths
from salt.utils import context, immutabletypes
from salt.utils import immutabletypes
from salt.template import compile_template, compile_template_str
from salt.exceptions import SaltRenderError, SaltReqTimeoutError, SaltException
from salt.utils.odict import OrderedDict, DefaultOrderedDict
@ -1585,10 +1585,10 @@ class State(object):
inject_globals['__env__'] = 'base'
if 'result' not in ret or ret['result'] is False:
with context.func_globals_inject(self.states[cdata['full']],
**inject_globals):
ret = self.states[cdata['full']](*cdata['args'],
**cdata['kwargs'])
self.states.inject_globals = inject_globals
ret = self.states[cdata['full']](*cdata['args'],
**cdata['kwargs'])
self.states.inject_globals = {}
if 'check_cmd' in low and '{0[state]}.mod_run_check_cmd'.format(low) not in self.states:
ret.update(self._run_check_cmd(low))
self.verify_ret(ret)