Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3006.x] Provide execution module a regular file client durring pillar rendering #66128

Merged
merged 6 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/66124.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Execution modules have access to regular fileclient durring pillar rendering.
3 changes: 2 additions & 1 deletion salt/beacons/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import re
import sys

import salt.loader
import salt.utils.event
import salt.utils.minion

Expand All @@ -20,6 +19,8 @@ class Beacon:
"""

def __init__(self, opts, functions):
import salt.loader

self.opts = opts
self.functions = functions
self.beacons = salt.loader.beacons(opts, functions)
Expand Down
14 changes: 12 additions & 2 deletions salt/pillar/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,7 @@ def __init__(
self.opts = self.__gen_opts(opts, grains, saltenv=saltenv, pillarenv=pillarenv)
self.saltenv = saltenv
self.client = salt.fileclient.get_file_client(self.opts, True)
self.fileclient = salt.fileclient.get_file_client(self.opts, False)
self.avail = self.__gather_avail()

if opts.get("file_client", "") == "local" and not opts.get(
Expand All @@ -573,11 +574,15 @@ def __init__(
utils = salt.loader.utils(opts, file_client=self.client)
if opts.get("file_client", "") == "local":
self.functions = salt.loader.minion_mods(
opts, utils=utils, file_client=self.client
opts,
utils=utils,
file_client=salt.fileclient.ContextlessFileClient(self.fileclient),
)
else:
self.functions = salt.loader.minion_mods(
self.opts, utils=utils, file_client=self.client
self.opts,
utils=utils,
file_client=salt.fileclient.ContextlessFileClient(self.fileclient),
)
else:
self.functions = functions
Expand Down Expand Up @@ -1370,6 +1375,11 @@ def destroy(self):
self.client.destroy()
except AttributeError:
pass
if self.fileclient:
try:
self.fileclient.destroy()
except AttributeError:
pass

# pylint: disable=W1701
def __del__(self):
Expand Down
24 changes: 24 additions & 0 deletions tests/pytests/integration/pillar/test_fileclient.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
def test_pillar_using_cp_module(salt_master, salt_minion, salt_cli, tmp_path):
pillar_top = """
base:
"*":
- my_pillar
"""
my_pillar = """
{{% set file_content = salt.cp.get_file_str("{}") %}}
""".format(
str(tmp_path / "myfile.txt")
)
my_file = """
foobar
"""
(tmp_path / "myfile.txt").write_text(my_file)
with salt_master.pillar_tree.base.temp_file("top.sls", pillar_top):
with salt_master.pillar_tree.base.temp_file("my_pillar.sls", my_pillar):
with salt_master.pillar_tree.base.temp_file("my_pillar.sls", my_pillar):
ret = salt_cli.run("state.apply", minion_tgt=salt_minion.id)
assert ret.returncode == 1
assert (
ret.json["no_|-states_|-states_|-None"]["comment"]
== "No states found for this minion"
)
4 changes: 4 additions & 0 deletions tests/pytests/unit/pillar/test_pillar.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ def test_dynamic_pillarenv():
},
"file_roots": {"base": ["/srv/salt/base"], "__env__": ["/srv/salt/__env__"]},
"extension_modules": "",
"fileserver_backend": "roots",
"cachedir": "",
}
pillar = salt.pillar.Pillar(opts, {}, "mocked-minion", "base", pillarenv="dev")
assert pillar.opts["pillar_roots"] == {
Expand All @@ -87,6 +89,8 @@ def test_ignored_dynamic_pillarenv():
},
"file_roots": {"base": ["/srv/salt/base"], "dev": ["/svr/salt/dev"]},
"extension_modules": "",
"fileserver_backend": "roots",
"cachedir": "",
}
pillar = salt.pillar.Pillar(opts, {}, "mocked-minion", "base", pillarenv="base")
assert pillar.opts["pillar_roots"] == {"base": ["/srv/pillar/base"]}
Expand Down
Loading
Loading