Skip to content

Commit

Permalink
Merge pull request #47372 from gtmanfred/grains
Browse files Browse the repository at this point in the history
Allow for passing in previously compiled grains to custom grain modules
  • Loading branch information
gtmanfred authored May 29, 2018
2 parents 5e13488 + 4d0bbc7 commit 45da45f
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 4 deletions.
5 changes: 5 additions & 0 deletions doc/topics/grains/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,11 @@ grain will override that core grain. Similarly, grains from
``/etc/salt/minion`` override both core grains and custom grain modules, and
grains in ``_grains`` will override *any* grains of the same name.

For custom grains, if the function takes an argument ``grains``, then the
previously rendered grains will be passed in. Because the rest of the grains
could be rendered in any order, the only grains that can be relied upon to be
passed in are ``core`` grains. This was added in the Fluorine release.


Examples of Grains
==================
Expand Down
10 changes: 10 additions & 0 deletions doc/topics/releases/fluorine.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@ Salt Release Notes - Codename Fluorine
======================================


Grains Dictionary Passed into Custom Grains
-------------------------------------------

Starting in this release, if a custom grains function accepts a variable named
``grains``, the Grains dictionary of the already compiled grains will be passed
in. Because of the non-deterministic order that grains are rendered in, the
only grains that can be relied upon to be passed in are ``core.py`` grains,
since those are compiled first.


"Virtual Package" Support Dropped for APT
-----------------------------------------

Expand Down
11 changes: 7 additions & 4 deletions salt/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -751,10 +751,13 @@ def grains(opts, force_refresh=False, proxy=None):
# proxymodule for retrieving information from the connected
# device.
log.trace('Loading %s grain', key)
if funcs[key].__code__.co_argcount == 1:
ret = funcs[key](proxy)
else:
ret = funcs[key]()
parameters = list(funcs[key].__code__.co_varnames)
kwargs = {}
if 'proxy' in parameters:
kwargs['proxy'] = proxy
if 'grains' in parameters:
kwargs['grains'] = grains_data
ret = funcs[key](**kwargs)
except Exception:
if salt.utils.platform.is_proxy():
log.info('The following CRITICAL message may not be an error; the proxy may not be completely established yet.')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
def test(grains):
if 'os' in grains:
return {'custom_grain_test': 'itworked'}
return {'custom_grain_test': 'itdidntwork'}
22 changes: 22 additions & 0 deletions tests/integration/grains/test_custom.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# -*- coding: utf-8 -*-
'''
Test the core grains
'''

# Import python libs
from __future__ import absolute_import, unicode_literals

# Import Salt Testing libs
from tests.support.case import ModuleCase


class TestGrainsCore(ModuleCase):
'''
Test the core grains grains
'''

def test_grains_passed_to_custom_grain(self):
'''
test if current grains are passed to grains module functions that have a grains argument
'''
self.assertEqual(self.run_function('grains.get', ['custom_grain_test']), 'itworked')

0 comments on commit 45da45f

Please sign in to comment.