mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
Add __file_client__ for execution modules
This commit is contained in:
parent
5f80bd9f45
commit
989790cb90
6 changed files with 69 additions and 9 deletions
|
@ -1532,3 +1532,17 @@ class DumbAuth:
|
|||
|
||||
def gen_token(self, clear_tok):
|
||||
return clear_tok
|
||||
|
||||
|
||||
class ContextlessFileClient:
|
||||
def __init__(self, file_client):
|
||||
self.file_client = file_client
|
||||
|
||||
def __getattr__(self, key):
|
||||
return getattr(self.file_client, key)
|
||||
|
||||
def __exit__(self, *_):
|
||||
pass
|
||||
|
||||
def __enter__(self):
|
||||
return self
|
||||
|
|
|
@ -263,6 +263,7 @@ def minion_mods(
|
|||
notify=False,
|
||||
static_modules=None,
|
||||
proxy=None,
|
||||
file_client=None,
|
||||
):
|
||||
"""
|
||||
Load execution modules
|
||||
|
@ -314,6 +315,7 @@ def minion_mods(
|
|||
"__utils__": utils,
|
||||
"__proxy__": proxy,
|
||||
"__opts__": opts,
|
||||
"__file_client__": file_client,
|
||||
},
|
||||
whitelist=whitelist,
|
||||
loaded_base_name=loaded_base_name,
|
||||
|
|
|
@ -84,11 +84,7 @@ class NamedLoaderContext(collections.abc.MutableMapping):
|
|||
self.value()[item] = value
|
||||
|
||||
def __bool__(self):
|
||||
try:
|
||||
self.loader
|
||||
except LookupError:
|
||||
return False
|
||||
return True
|
||||
return bool(self.value())
|
||||
|
||||
def __len__(self):
|
||||
return self.value().__len__()
|
||||
|
|
9
salt/loader/dunder.py
Normal file
9
salt/loader/dunder.py
Normal file
|
@ -0,0 +1,9 @@
|
|||
"""
|
||||
Salt dunders.
|
||||
"""
|
||||
import salt.loader.context
|
||||
|
||||
loader_context = salt.loader.context.LoaderContext()
|
||||
|
||||
|
||||
__file_client__ = loader_context.named_context("__file_client__")
|
|
@ -20,6 +20,7 @@ import salt.utils.path
|
|||
import salt.utils.templates
|
||||
import salt.utils.url
|
||||
from salt.exceptions import CommandExecutionError
|
||||
from salt.loader.dunder import __file_client__
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
@ -161,6 +162,11 @@ def _client():
|
|||
"""
|
||||
Return a client, hashed by the list of masters
|
||||
"""
|
||||
if __file_client__:
|
||||
val = __file_client__.value()
|
||||
fc = salt.fileclient.ContextlessFileClient(val)
|
||||
log.error("Using context client %r %r", val, fc)
|
||||
return fc
|
||||
return salt.fileclient.get_file_client(__opts__)
|
||||
|
||||
|
||||
|
|
|
@ -758,6 +758,7 @@ class State:
|
|||
mocked=False,
|
||||
loader="states",
|
||||
initial_pillar=None,
|
||||
file_client=None,
|
||||
):
|
||||
self._init_kwargs = {
|
||||
"opts": opts,
|
||||
|
@ -774,6 +775,12 @@ class State:
|
|||
if "grains" not in opts:
|
||||
opts["grains"] = salt.loader.grains(opts)
|
||||
self.opts = opts
|
||||
if file_client:
|
||||
self.file_client = file_client
|
||||
self.preserve_file_client = True
|
||||
else:
|
||||
self.file_client = salt.fileclient.get_file_client(self.opts)
|
||||
self.preserve_file_client = False
|
||||
self.proxy = proxy
|
||||
self._pillar_override = pillar_override
|
||||
if pillar_enc is not None:
|
||||
|
@ -798,7 +805,11 @@ class State:
|
|||
self.opts.get("pillar_merge_lists", False),
|
||||
)
|
||||
log.debug("Finished gathering pillar data for state run")
|
||||
self.state_con = context or {}
|
||||
if context is None:
|
||||
self.state_con = {}
|
||||
else:
|
||||
self.state_con = context
|
||||
self.state_con["fileclient"] = self.file_client
|
||||
self.load_modules()
|
||||
self.active = set()
|
||||
self.mod_init = set()
|
||||
|
@ -1285,7 +1296,11 @@ class State:
|
|||
log.info("Loading fresh modules for state activity")
|
||||
self.utils = salt.loader.utils(self.opts)
|
||||
self.functions = salt.loader.minion_mods(
|
||||
self.opts, self.state_con, utils=self.utils, proxy=self.proxy
|
||||
self.opts,
|
||||
self.state_con,
|
||||
utils=self.utils,
|
||||
proxy=self.proxy,
|
||||
file_client=self.file_client,
|
||||
)
|
||||
if isinstance(data, dict):
|
||||
if data.get("provider", False):
|
||||
|
@ -3672,6 +3687,16 @@ class State:
|
|||
return errors
|
||||
return self.call_high(high)
|
||||
|
||||
def destroy(self):
|
||||
if not self.preserve_file_client:
|
||||
self.file_client.close()
|
||||
|
||||
def __enter__(self):
|
||||
return self
|
||||
|
||||
def __exit__(self, *_):
|
||||
self.destroy()
|
||||
|
||||
|
||||
class LazyAvailStates:
|
||||
"""
|
||||
|
@ -4917,9 +4942,15 @@ class HighState(BaseHighState):
|
|||
mocked=False,
|
||||
loader="states",
|
||||
initial_pillar=None,
|
||||
file_client=None,
|
||||
):
|
||||
self.opts = opts
|
||||
self.client = salt.fileclient.get_file_client(self.opts)
|
||||
if file_client:
|
||||
self.client = file_client
|
||||
self.preserve_client = True
|
||||
else:
|
||||
self.client = salt.fileclient.get_file_client(self.opts)
|
||||
self.preserve_client = False
|
||||
BaseHighState.__init__(self, opts)
|
||||
self.state = State(
|
||||
self.opts,
|
||||
|
@ -4931,6 +4962,7 @@ class HighState(BaseHighState):
|
|||
mocked=mocked,
|
||||
loader=loader,
|
||||
initial_pillar=initial_pillar,
|
||||
file_client=self.client,
|
||||
)
|
||||
self.matchers = salt.loader.matchers(self.opts)
|
||||
self.proxy = proxy
|
||||
|
@ -4965,7 +4997,8 @@ class HighState(BaseHighState):
|
|||
return None
|
||||
|
||||
def destroy(self):
|
||||
self.client.destroy()
|
||||
if not self.preserve_client:
|
||||
self.client.destroy()
|
||||
|
||||
def __enter__(self):
|
||||
return self
|
||||
|
|
Loading…
Add table
Reference in a new issue