Created
June 4, 2015 19:14
-
-
Save iggy/c2a85f87ef2cfa7f51cd to your computer and use it in GitHub Desktop.
WIP defaults.get addition
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- /usr/lib/python2.7/dist-packages/salt/modules/defaults.py 2015-05-11 06:37:45.000000000 -0500 | |
+++ _modules/defaults.py 2015-06-04 14:09:18.402593449 -0500 | |
@@ -2,6 +2,7 @@ | |
from __future__ import absolute_import | |
import inspect | |
import json | |
+import logging | |
import os | |
import yaml | |
@@ -9,6 +10,8 @@ | |
import salt.utils | |
import salt.ext.six as six | |
+log = logging.getLogger(__name__) | |
+ | |
__virtualname__ = 'defaults' | |
@@ -33,6 +36,8 @@ | |
for ext in ('yaml', 'json'): | |
source_url = 'salt://{0}/{1}'.format(pillar_name, 'defaults.' + ext) | |
paths.append(source_url) | |
+ source_url = 'salt://{0}/{1}'.format(pillar_name, 'grainsmap.' + ext) | |
+ paths.append(source_url) | |
return __context__['cp.fileclient'].cache_files(paths) | |
@@ -51,6 +56,19 @@ | |
return defaults | |
+def _mapsload(defaults_path): | |
+ ''' | |
+ Given a pillar_name and the template cache location, attempt to load | |
+ the defaults.json from the cache location. If it does not exist, try | |
+ defaults.yaml. | |
+ ''' | |
+ for loader in json, yaml: | |
+ defaults_file = os.path.join(defaults_path, 'grainsmap.' + loader.__name__) | |
+ if os.path.exists(defaults_file): | |
+ with salt.utils.fopen(defaults_file) as fhr: | |
+ defaults = loader.load(fhr) | |
+ return defaults | |
+ | |
def get(key, default=''): | |
''' | |
defaults.get is used much like pillar.get except that it will read | |
@@ -87,6 +105,7 @@ | |
sls = None | |
tmplpath = None | |
+ value = None | |
for frame in inspect.stack(): | |
if sls is not None and tmplpath is not None: | |
@@ -121,8 +140,24 @@ | |
defaults = _load(defaults_path) | |
+ log.debug(">>>> {0}".format(key)) | |
+ log.debug(defaults_path) | |
+ log.debug('{0}:{1}'.format(pillar_name, key)) | |
value = __salt__['pillar.get']('{0}:{1}'.format(pillar_name, key), None) | |
+ # check each grain and lookup the grain value in the map | |
+ if value is None: | |
+ maps = _mapsload(defaults_path) | |
+ for grain, gmap in maps.iteritems(): | |
+ log.debug("defaults.get: {0}, {1}, {2}".format(grain, gmap, maps)) | |
+ grainv = __salt__['grains.get'](grain) | |
+ log.debug(grainv) | |
+ log.debug(gmap[grainv]) | |
+ if grainv in gmap: | |
+ value = salt.utils.traverse_dict_and_list(gmap[grainv], key, None) | |
+ #log.debug("setting value to grainsmap value: {0}", repr(value)) | |
+ | |
+ log.debug(value) | |
if value is None: | |
value = salt.utils.traverse_dict_and_list(defaults, key, None) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
os_family: | |
Debian: | |
test2: | |
test: "test2->test" | |
test3: | |
test: grainsmap value |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment