Scrap event-based approach for refreshing grains

The call to refresh_modules() in saltutil.refresh_grains was resulting
in a race condition with the event I added to refresh the grains. This
meant that, even though self.opts['grains'] was being changed by the new
event, before that process could finish the module refresh was itself
re-loading the grains and re-packing these new grains into the
__grains__ dunder, negating the refresh.

Since the module refresh loads the grains anyway, this commit changes
saltutil.refresh_grains to refresh the modules. It also removes the
refresh argument recently added to the grains module functions, as we're
no longer using it to conditionally refresh modules.
This commit is contained in:
Erik Johnson 2017-03-09 12:18:51 -06:00 committed by rallytime
parent 776a9431b9
commit a7e419e35f
2 changed files with 16 additions and 23 deletions

View file

@ -203,7 +203,7 @@ def item(*args, **kwargs):
return ret
def setvals(grains, destructive=False, refresh=True):
def setvals(grains, destructive=False):
'''
Set new grains values in the grains config file
@ -211,10 +211,6 @@ def setvals(grains, destructive=False, refresh=True):
If an operation results in a key being removed, delete the key, too.
Defaults to False.
refresh
Refresh modules and pillar after adding the new grains.
Defaults to True.
CLI Example:
.. code-block:: bash
@ -290,12 +286,12 @@ def setvals(grains, destructive=False, refresh=True):
log.error(msg.format(fn_))
if not __opts__.get('local', False):
# Refresh the grains
__salt__['saltutil.refresh_grains'](refresh=refresh)
__salt__['saltutil.refresh_grains']()
# Return the grains we just set to confirm everything was OK
return new_grains
def setval(key, val, destructive=False, refresh=True):
def setval(key, val, destructive=False):
'''
Set a grains value in the grains config file
@ -309,10 +305,6 @@ def setval(key, val, destructive=False, refresh=True):
If an operation results in a key being removed, delete the key, too.
Defaults to False.
refresh
Refresh modules and pillar after adding the new grain.
Defaults to True.
CLI Example:
.. code-block:: bash
@ -320,7 +312,7 @@ def setval(key, val, destructive=False, refresh=True):
salt '*' grains.setval key val
salt '*' grains.setval key "{'sub-key': 'val', 'sub-key2': 'val2'}"
'''
return setvals({key: val}, destructive, refresh)
return setvals({key: val}, destructive)
def append(key, val, convert=False, delimiter=DEFAULT_TARGET_DELIM):

View file

@ -310,16 +310,19 @@ def sync_states(saltenv=None, refresh=True):
return ret
def refresh_grains(refresh=True):
def refresh_grains(refresh_pillar=True):
'''
.. versionadded:: 2016.3.6,2016.11.4,Nitrogen
Refresh the minion's grains without syncing custom grains modules from
``salt://_grains``.
refresh : True
If ``True``, refresh the available execution modules and recompile
pillar data for the minion. Set to ``False`` to prevent this refresh.
.. note::
The available execution modules will be reloaded as part of this
proceess, as grains can affect which modules are available.
refresh_pillar : True
Set to ``False`` to keep pillar data from being refreshed.
CLI Examples:
@ -327,13 +330,11 @@ def refresh_grains(refresh=True):
salt '*' saltutil.refresh_grains
'''
try:
ret = __salt__['event.fire']({}, 'grains_refresh_manual')
except Exception:
log.error('Failed to refresh grains', exc_info=True)
return False
if refresh:
refresh_modules()
# Modules and pillar need to be refreshed in case grains changes affected
# them, and the module refresh process reloads the grains and assigns the
# newly-reloaded grains to each execution module's __grains__ dunder.
refresh_modules()
if refresh_pillar:
refresh_pillar()
return ret