diff --git a/salt/loader/__init__.py b/salt/loader/__init__.py index 2493f22cc48e..8f2a69dc6b6c 100644 --- a/salt/loader/__init__.py +++ b/salt/loader/__init__.py @@ -780,6 +780,7 @@ def states( proxy=None, context=None, loaded_base_name=None, + file_client=None, ): """ Returns the state modules @@ -817,6 +818,7 @@ def states( "__utils__": utils, "__serializers__": serializers, "__context__": context, + "__file_client__": file_client, }, whitelist=whitelist, extra_module_dirs=utils.module_dirs if utils else None, diff --git a/salt/modules/cp.py b/salt/modules/cp.py index 4898e4ca3c83..64666aefa5d8 100644 --- a/salt/modules/cp.py +++ b/salt/modules/cp.py @@ -160,13 +160,13 @@ def _error(msg): def _client(): """ - Return a client, hashed by the list of masters + Return a file client + + If the __file_client__ context is set return it, otherwize create a new + file client using __opts__. """ if __file_client__: - val = __file_client__.value() - fc = salt.fileclient.ContextlessFileClient(val) - log.error("Using context client %r %r", val, fc) - return fc + return __file_client__.value() return salt.fileclient.get_file_client(__opts__) diff --git a/salt/modules/dockermod.py b/salt/modules/dockermod.py index 415c03d24b7a..f9ffd2dda9e8 100644 --- a/salt/modules/dockermod.py +++ b/salt/modules/dockermod.py @@ -222,6 +222,7 @@ import salt.utils.json import salt.utils.path from salt.exceptions import CommandExecutionError, SaltInvocationError +from salt.loader.dunder import __file_client__ from salt.state import HighState __docformat__ = "restructuredtext en" @@ -325,6 +326,18 @@ def __virtual__(): return (False, "Could not import docker module, is docker-py installed?") +def _file_client(): + """ + Return a file client + + If the __file_client__ context is set return it, otherwize create a new + file client using __opts__. + """ + if __file_client__: + return __file_client__.value() + return salt.fileclient.get_file_client(__opts__) + + class DockerJSONDecoder(json.JSONDecoder): def decode(self, s, _w=None): objs = [] @@ -6633,7 +6646,7 @@ def _prepare_trans_tar(name, sls_opts, mods=None, pillar=None, extra_filerefs="" # reuse it from salt.ssh, however this function should # be somewhere else refs = salt.client.ssh.state.lowstate_file_refs(chunks, extra_filerefs) - with salt.fileclient.get_file_client(__opts__) as fileclient: + with _file_client() as fileclient: return salt.client.ssh.state.prep_trans_tar( fileclient, chunks, refs, pillar, name ) diff --git a/salt/state.py b/salt/state.py index d70e105013f1..dfa64adae0e7 100644 --- a/salt/state.py +++ b/salt/state.py @@ -1287,6 +1287,7 @@ def _load_states(self): self.serializers, context=self.state_con, proxy=self.proxy, + file_client=salt.fileclient.ContextlessFileClient(self.file_client), ) def load_modules(self, data=None, proxy=None): @@ -1300,7 +1301,7 @@ def load_modules(self, data=None, proxy=None): self.state_con, utils=self.utils, proxy=self.proxy, - file_client=self.file_client, + file_client=salt.fileclient.ContextlessFileClient(self.file_client), ) if isinstance(data, dict): if data.get("provider", False): diff --git a/salt/states/ansiblegate.py b/salt/states/ansiblegate.py index 9abd418c42c0..ec8913dee59a 100644 --- a/salt/states/ansiblegate.py +++ b/salt/states/ansiblegate.py @@ -38,12 +38,25 @@ import salt.fileclient import salt.utils.decorators.path +from salt.loader.dunder import __file_client__ from salt.utils.decorators import depends log = logging.getLogger(__name__) __virtualname__ = "ansible" +def _file_client(): + """ + Return a file client + + If the __file_client__ context is set return it, otherwize create a new + file client using __opts__. + """ + if __file_client__: + return __file_client__.value() + return salt.fileclient.get_file_client(__opts__) + + @depends("ansible") class AnsibleState: """ @@ -162,7 +175,7 @@ def playbooks(name, rundir=None, git_repo=None, git_kwargs=None, ansible_kwargs= } if git_repo: if not isinstance(rundir, str) or not os.path.isdir(rundir): - with salt.fileclient.get_file_client(__opts__) as client: + with _file_client() as client: rundir = client._extrn_path(git_repo, "base") log.trace("rundir set to %s", rundir) if not isinstance(git_kwargs, dict):