Skip to content

Commit

Permalink
Populate __file_client__ for states
Browse files Browse the repository at this point in the history
Also start using __file_client__ wherever we used to pull a client from
__context__>
  • Loading branch information
dwoz committed Dec 4, 2023
1 parent 3faee7d commit 8223a27
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 8 deletions.
2 changes: 2 additions & 0 deletions salt/loader/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -780,6 +780,7 @@ def states(
proxy=None,
context=None,
loaded_base_name=None,
file_client=None,
):
"""
Returns the state modules
Expand Down Expand Up @@ -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,
Expand Down
10 changes: 5 additions & 5 deletions salt/modules/cp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)


Expand Down
15 changes: 14 additions & 1 deletion salt/modules/dockermod.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 = []
Expand Down Expand Up @@ -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
)
Expand Down
3 changes: 2 additions & 1 deletion salt/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):
Expand Down
15 changes: 14 additions & 1 deletion salt/states/ansiblegate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
"""
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 8223a27

Please sign in to comment.