Skip to content

Commit

Permalink
use the cache runner in the cache roster file to get grains
Browse files Browse the repository at this point in the history
Instead of having to query each file individitually.
  • Loading branch information
Daniel Wallace committed Nov 1, 2016
1 parent 4c2d415 commit c3e33c3
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 24 deletions.
1 change: 1 addition & 0 deletions .testing.pylintrc
Expand Up @@ -227,6 +227,7 @@ additional-builtins=__opts__,
__pillar__,
__grains__,
__context__,
__runner__,
__ret__,
__env__,
__low__,
Expand Down
3 changes: 2 additions & 1 deletion salt/loader.py
Expand Up @@ -432,7 +432,7 @@ def fileserver(opts, backends):
pack={'__utils__': utils(opts)})


def roster(opts, whitelist=None):
def roster(opts, runner, whitelist=None):
'''
Returns the roster modules
'''
Expand All @@ -441,6 +441,7 @@ def roster(opts, whitelist=None):
opts,
tag='roster',
whitelist=whitelist,
pack={'__runner__': runner},
)


Expand Down
2 changes: 1 addition & 1 deletion salt/roster/__init__.py
Expand Up @@ -50,7 +50,7 @@ def __init__(self, opts, backends='flat'):
self.backends = backends
if not backends:
self.backends = ['flat']
self.rosters = salt.loader.roster(opts)
self.rosters = salt.loader.roster(opts, runner=salt.loader.runner(self.opts))

def _gen_back(self):
'''
Expand Down
27 changes: 5 additions & 22 deletions salt/roster/cache.py
Expand Up @@ -18,45 +18,28 @@
'''
from __future__ import absolute_import

# Import python libs
import os.path
import msgpack

# Import Salt libs
import salt.loader
import salt.utils
import salt.utils.cloud
import salt.utils.validate.net
from salt import syspaths


def targets(tgt, tgt_type='glob', **kwargs): # pylint: disable=W0613
'''
Return the targets from the flat yaml file, checks opts for location but
defaults to /etc/salt/roster
'''
cache = os.path.join(syspaths.CACHE_DIR, 'master', 'minions', tgt, 'data.p')

if not os.path.exists(cache):
return {}

roster_order = __opts__.get('roster_order', (
'public', 'private', 'local'
))

with salt.utils.fopen(cache, 'r') as fh_:
cache_data = msgpack.load(fh_)

ipv4 = cache_data.get('grains', {}).get('ipv4', [])
preferred_ip = extract_ipv4(roster_order, ipv4)
if preferred_ip is None:
return {}

return {
tgt: {
'host': preferred_ip,
}
}
cached_data = __runner__['cache.grains'](tgt=tgt, expr_form=tgt_type)
ret = {}
for server, grains in cached_data.items():
ret[server] = {'host': extract_ipv4(roster_order, grains.get('ipv4', []))}
return ret


def extract_ipv4(roster_order, ipv4):
Expand Down

0 comments on commit c3e33c3

Please sign in to comment.