mirror of
https://github.com/saltstack/salt.git
synced 2025-04-15 09:10:20 +00:00
fixes saltstack/salt#61708 add grains_refresh_pre_exec option for minions
This commit is contained in:
parent
7e86d9e341
commit
a5fcf442a3
6 changed files with 78 additions and 0 deletions
1
changelog/61708.added
Normal file
1
changelog/61708.added
Normal file
|
@ -0,0 +1 @@
|
|||
Add grains_refresh_pre_exec option to allow grains to be refreshed before any operation
|
|
@ -370,6 +370,12 @@
|
|||
# If the value is set to zero, this check is disabled.
|
||||
#grains_refresh_every: 1
|
||||
|
||||
# The grains_refresh_pre_exec setting allows for a minion to check its grains
|
||||
# prior to the execution of any operation to see if they have changed and, if
|
||||
# so, to inform the master of the new grains. This operation is moderately
|
||||
# expensive, therefore care should be taken before enabling this behavior.
|
||||
#grains_refresh_pre_exec: False
|
||||
|
||||
# Cache grains on the minion. Default is False.
|
||||
#grains_cache: False
|
||||
|
||||
|
|
|
@ -934,6 +934,24 @@ A value of 10 minutes is a reasonable default.
|
|||
|
||||
grains_refresh_every: 0
|
||||
|
||||
.. conf_minion:: grains_refresh_pre_exec
|
||||
|
||||
``grains_refresh_pre_exec``
|
||||
---------------------------
|
||||
|
||||
.. versionadded:: 3005
|
||||
|
||||
Default: ``False``
|
||||
|
||||
The ``grains_refresh_pre_exec`` setting allows for a minion to check its grains
|
||||
prior to the execution of any operation to see if they have changed and, if
|
||||
so, to inform the master of the new grains. This operation is moderately
|
||||
expensive, therefore care should be taken before enabling this behavior.
|
||||
|
||||
.. code-block:: yaml
|
||||
|
||||
grains_refresh_pre_exec: True
|
||||
|
||||
.. conf_minion:: metadata_server_grains
|
||||
|
||||
``metadata_server_grains``
|
||||
|
|
|
@ -749,6 +749,8 @@ VALID_OPTS = immutabletypes.freeze(
|
|||
"grains_blacklist": list,
|
||||
# The number of minutes between the minion refreshing its cache of grains
|
||||
"grains_refresh_every": int,
|
||||
# Enable grains refresh prior to any operation
|
||||
"grains_refresh_pre_exec": bool,
|
||||
# Use lspci to gather system data for grains on a minion
|
||||
"enable_lspci": bool,
|
||||
# The number of seconds for the salt client to wait for additional syndics to
|
||||
|
|
|
@ -1744,6 +1744,9 @@ class Minion(MinionBase):
|
|||
self.schedule.functions = self.functions
|
||||
self.schedule.returners = self.returners
|
||||
|
||||
if self.opts["grains_refresh_pre_exec"]:
|
||||
self.opts["grains"] = salt.loader.grains(self.opts, force_refresh=True)
|
||||
|
||||
process_count_max = self.opts.get("process_count_max")
|
||||
if process_count_max > 0:
|
||||
process_count = len(salt.utils.minion.running(self.opts))
|
||||
|
|
|
@ -969,3 +969,51 @@ def test_config_cache_path_overrides():
|
|||
|
||||
mminion = salt.minion.MasterMinion(opts)
|
||||
assert mminion.opts["cachedir"] == cachedir
|
||||
|
||||
|
||||
def test_minion_grains_refresh_pre_exec_false():
|
||||
"""
|
||||
Minion does not refresh grains when grains_refresh_pre_exec is False
|
||||
"""
|
||||
mock_opts = salt.config.DEFAULT_MINION_OPTS.copy()
|
||||
mock_opts["multiprocessing"] = False
|
||||
mock_opts["grains_refresh_pre_exec"] = False
|
||||
mock_data = {"fun": "foo.bar", "jid": 123}
|
||||
with patch("salt.loader.grains") as grainsfunc, patch(
|
||||
"salt.minion.Minion._target", MagicMock(return_value=True)
|
||||
):
|
||||
minion = salt.minion.Minion(
|
||||
mock_opts,
|
||||
jid_queue=None,
|
||||
io_loop=salt.ext.tornado.ioloop.IOLoop(),
|
||||
load_grains=False,
|
||||
)
|
||||
try:
|
||||
ret = minion._handle_decoded_payload(mock_data).result()
|
||||
grainsfunc.assert_not_called()
|
||||
finally:
|
||||
minion.destroy()
|
||||
|
||||
|
||||
def test_minion_grains_refresh_pre_exec_true():
|
||||
"""
|
||||
Minion refreshes grains when grains_refresh_pre_exec is True
|
||||
"""
|
||||
mock_opts = salt.config.DEFAULT_MINION_OPTS.copy()
|
||||
mock_opts["multiprocessing"] = False
|
||||
mock_opts["grains_refresh_pre_exec"] = True
|
||||
mock_data = {"fun": "foo.bar", "jid": 123}
|
||||
with patch("salt.loader.grains") as grainsfunc, patch(
|
||||
"salt.minion.Minion._target", MagicMock(return_value=True)
|
||||
):
|
||||
minion = salt.minion.Minion(
|
||||
mock_opts,
|
||||
jid_queue=None,
|
||||
io_loop=salt.ext.tornado.ioloop.IOLoop(),
|
||||
load_grains=False,
|
||||
)
|
||||
try:
|
||||
ret = minion._handle_decoded_payload(mock_data).result()
|
||||
grainsfunc.assert_called()
|
||||
finally:
|
||||
minion.destroy()
|
||||
|
|
Loading…
Add table
Reference in a new issue