Skip to content

Commit

Permalink
add saltenv directory to pillar_roots for dynamic environments and us…
Browse files Browse the repository at this point in the history
…e __env__ as tag to be replaced with saltenv
  • Loading branch information
root authored and root committed Dec 31, 2019
1 parent d4221bc commit 00da538
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
23 changes: 22 additions & 1 deletion salt/fileclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -816,7 +816,22 @@ def _find_file(self, path, saltenv='base'):
if salt.utils.url.is_escaped(path):
# The path arguments are escaped
path = salt.utils.url.unescape(path)
for root in self.opts['pillar_roots'].get(saltenv, []):
for item in self.opts['pillar_roots'].get(saltenv, []):
if isinstance(item, dict):
# get dict's first key like when this is a list entry.
root = item.keys()[0]
append_saltenv = item[root].get('append_saltenv')
if append_saltenv:
# append requested saltenv path to root
root = os.path.join(root,
saltenv if isinstance(append_saltenv, bool)
# replace __env__ with saltenv and append to root
else append_saltenv.replace("__env__", saltenv)
)
log.debug("PillarClient: pillar_roots path append with saltenv to %s", root)
else: # item is a list entry
root = item

full = os.path.join(root, path)
if os.path.isfile(full):
fnd['path'] = full
Expand Down Expand Up @@ -851,6 +866,8 @@ def file_list(self, saltenv='base', prefix=''):
ret = []
prefix = prefix.strip('/')
for path in self.opts['pillar_roots'].get(saltenv, []):
if isinstance(path, dict):
path = path.keys()[0]
for root, dirs, files in salt.utils.path.os_walk(
os.path.join(path, prefix), followlinks=True
):
Expand All @@ -869,6 +886,8 @@ def file_list_emptydirs(self, saltenv='base', prefix=''):
ret = []
prefix = prefix.strip('/')
for path in self.opts['pillar_roots'].get(saltenv, []):
if isinstance(path, dict):
path = path.keys()[0]
for root, dirs, files in salt.utils.path.os_walk(
os.path.join(path, prefix), followlinks=True
):
Expand All @@ -886,6 +905,8 @@ def dir_list(self, saltenv='base', prefix=''):
ret = []
prefix = prefix.strip('/')
for path in self.opts['pillar_roots'].get(saltenv, []):
if isinstance(path, dict):
path = path.keys()[0]
for root, dirs, files in salt.utils.path.os_walk(
os.path.join(path, prefix), followlinks=True
):
Expand Down
7 changes: 4 additions & 3 deletions salt/fileserver/roots.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,10 @@ def _add_file_stat(fnd):
# append requested saltenv to root directory.
log.debug("file_roots append_saltenv: %s", append_saltenv)
root = os.path.join(root,
requested_saltenv if isinstance(append_saltenv, bool)
# format string in append_saltenv
else append_saltenv % requested_saltenv
requested_saltenv \
if isinstance(append_saltenv, bool)
# replace __env__ with saltenv and append to root
else append_saltenv.replace("__env__", requested_saltenv)
)
log.debug("file_roots path append with saltenv to %s", root)
else: # item is a list entry
Expand Down
1 change: 1 addition & 0 deletions salt/pillar/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,7 @@ def __gen_opts(self, opts_in, grains, saltenv=None, ext=None, pillarenv=None):
env = opts.get('pillarenv') or opts.get('saltenv') or 'base'
if env not in opts['pillar_roots']:
log.debug("pillar environment '%s' maps to __env__ pillar_roots directory", env)
# remove __env__ and add requested environment
opts['pillar_roots'][env] = opts['pillar_roots'].pop('__env__')
else:
log.debug("pillar_roots __env__ ignored (environment '%s' found in pillar_roots)",
Expand Down

0 comments on commit 00da538

Please sign in to comment.