From 683e2cc502062a929633bf4fff72ecdc2f5efe54 Mon Sep 17 00:00:00 2001 From: Erik Johnson Date: Wed, 29 Nov 2017 22:09:09 -0600 Subject: [PATCH] Rename 'environment' config option to 'saltenv' 'environment' predates the 'saltenv' nomenclature, and since we have been using saltenv on the CLI for years, and now also have pillarenv, it makes sense to make this change. --- salt/client/ssh/__init__.py | 2 +- salt/config/__init__.py | 47 ++++++++++++++++++++++++++++++++++--- salt/daemons/flo/core.py | 2 +- salt/minion.py | 14 +++++------ salt/modules/cmdmod.py | 2 +- salt/modules/cp.py | 2 +- salt/modules/dockermod.py | 2 +- salt/modules/event.py | 2 +- salt/modules/pillar.py | 4 ++-- salt/modules/saltcheck.py | 2 +- salt/modules/state.py | 32 ++++++++++++------------- salt/pillar/__init__.py | 12 +++++----- salt/pillar/git_pillar.py | 2 +- salt/state.py | 18 +++++++------- salt/utils/gitfs.py | 2 +- tests/saltsh.py | 2 +- tests/unit/test_pillar.py | 8 +++---- 17 files changed, 98 insertions(+), 57 deletions(-) diff --git a/salt/client/ssh/__init__.py b/salt/client/ssh/__init__.py index 4c38a8b1cef..92d8328d6c9 100644 --- a/salt/client/ssh/__init__.py +++ b/salt/client/ssh/__init__.py @@ -1050,7 +1050,7 @@ class Single(object): popts, opts_pkg[u'grains'], opts_pkg[u'id'], - opts_pkg.get(u'environment', u'base') + opts_pkg.get(u'saltenv', u'base') ) pillar_data = pillar.compile_pillar() diff --git a/salt/config/__init__.py b/salt/config/__init__.py index 5680f727b94..5084426e268 100644 --- a/salt/config/__init__.py +++ b/salt/config/__init__.py @@ -242,7 +242,10 @@ VALID_OPTS = { 'autoload_dynamic_modules': bool, # Force the minion into a single environment when it fetches files from the master - 'environment': str, + 'saltenv': str, + + # Prevent saltenv from being overriden on the command line + 'lock_saltenv': bool, # Force the minion into a single pillar root when it fetches pillar data from the master 'pillarenv': str, @@ -1177,7 +1180,8 @@ DEFAULT_MINION_OPTS = { 'random_startup_delay': 0, 'failhard': False, 'autoload_dynamic_modules': True, - 'environment': None, + 'saltenv': None, + 'lock_saltenv': False, 'pillarenv': None, 'pillarenv_from_saltenv': False, 'pillar_opts': False, @@ -1454,7 +1458,8 @@ DEFAULT_MASTER_OPTS = { }, 'top_file_merging_strategy': 'merge', 'env_order': [], - 'environment': None, + 'saltenv': None, + 'lock_saltenv': False, 'default_top': 'base', 'file_client': 'local', 'git_pillar_base': 'master', @@ -3590,6 +3595,24 @@ def apply_minion_config(overrides=None, if overrides: opts.update(overrides) + if u'environment' in opts: + if u'saltenv' in opts: + log.warning( + u'The \'saltenv\' and \'environment\' minion config options ' + u'cannot both be used. Ignoring \'environment\' in favor of ' + u'\'saltenv\'.', + ) + # Set environment to saltenv in case someone's custom module is + # refrencing __opts__['environment'] + opts[u'environment'] = opts[u'saltenv'] + else: + log.warning( + u'The \'environment\' minion config option has been renamed ' + u'to \'saltenv\'. Using %s as the \'saltenv\' config value.', + opts[u'environment'] + ) + opts[u'saltenv'] = opts[u'environment'] + opts['__cli'] = os.path.basename(sys.argv[0]) # No ID provided. Will getfqdn save us? @@ -3742,6 +3765,24 @@ def apply_master_config(overrides=None, defaults=None): if overrides: opts.update(overrides) + if u'environment' in opts: + if u'saltenv' in opts: + log.warning( + u'The \'saltenv\' and \'environment\' master config options ' + u'cannot both be used. Ignoring \'environment\' in favor of ' + u'\'saltenv\'.', + ) + # Set environment to saltenv in case someone's custom runner is + # refrencing __opts__['environment'] + opts[u'environment'] = opts[u'saltenv'] + else: + log.warning( + u'The \'environment\' master config option has been renamed ' + u'to \'saltenv\'. Using %s as the \'saltenv\' config value.', + opts[u'environment'] + ) + opts[u'saltenv'] = opts[u'environment'] + if len(opts['sock_dir']) > len(opts['cachedir']) + 10: opts['sock_dir'] = os.path.join(opts['cachedir'], '.salt-unix') diff --git a/salt/daemons/flo/core.py b/salt/daemons/flo/core.py index 1a11e08aedb..dad36d32a4e 100644 --- a/salt/daemons/flo/core.py +++ b/salt/daemons/flo/core.py @@ -737,7 +737,7 @@ class SaltLoadPillar(ioflo.base.deeding.Deed): 'dst': (master.name, None, 'remote_cmd')} load = {'id': self.opts.value['id'], 'grains': self.grains.value, - 'saltenv': self.opts.value['environment'], + 'saltenv': self.opts.value['saltenv'], 'ver': '2', 'cmd': '_pillar'} self.road_stack.value.transmit({'route': route, 'load': load}, diff --git a/salt/minion.py b/salt/minion.py index bd4cfcc01b2..71b856ac783 100644 --- a/salt/minion.py +++ b/salt/minion.py @@ -736,8 +736,8 @@ class SMinion(MinionBase): if not os.path.isdir(pdir): os.makedirs(pdir, 0o700) ptop = os.path.join(pdir, u'top.sls') - if self.opts[u'environment'] is not None: - penv = self.opts[u'environment'] + if self.opts[u'saltenv'] is not None: + penv = self.opts[u'saltenv'] else: penv = u'base' cache_top = {penv: {self.opts[u'id']: [u'cache']}} @@ -773,7 +773,7 @@ class SMinion(MinionBase): self.opts, self.opts[u'grains'], self.opts[u'id'], - self.opts[u'environment'], + self.opts[u'saltenv'], pillarenv=self.opts.get(u'pillarenv'), ).compile_pillar() @@ -1144,7 +1144,7 @@ class Minion(MinionBase): self.opts, self.opts[u'grains'], self.opts[u'id'], - self.opts[u'environment'], + self.opts[u'saltenv'], pillarenv=self.opts.get(u'pillarenv') ).compile_pillar() @@ -2032,7 +2032,7 @@ class Minion(MinionBase): self.opts, self.opts[u'grains'], self.opts[u'id'], - self.opts[u'environment'], + self.opts[u'saltenv'], pillarenv=self.opts.get(u'pillarenv'), ).compile_pillar() except SaltClientError: @@ -3349,7 +3349,7 @@ class ProxyMinion(Minion): self.opts, self.opts[u'grains'], self.opts[u'id'], - saltenv=self.opts[u'environment'], + saltenv=self.opts[u'saltenv'], pillarenv=self.opts.get(u'pillarenv'), ).compile_pillar() @@ -3391,7 +3391,7 @@ class ProxyMinion(Minion): # we can then sync any proxymodules down from the master # we do a sync_all here in case proxy code was installed by # SPM or was manually placed in /srv/salt/_modules etc. - self.functions[u'saltutil.sync_all'](saltenv=self.opts[u'environment']) + self.functions[u'saltutil.sync_all'](saltenv=self.opts[u'saltenv']) # Pull in the utils self.utils = salt.loader.utils(self.opts) diff --git a/salt/modules/cmdmod.py b/salt/modules/cmdmod.py index 3ae58905c37..d0489285eaa 100644 --- a/salt/modules/cmdmod.py +++ b/salt/modules/cmdmod.py @@ -219,7 +219,7 @@ def _gather_pillar(pillarenv, pillar_override): __opts__, __grains__, __opts__['id'], - __opts__['environment'], + __opts__['saltenv'], pillar_override=pillar_override, pillarenv=pillarenv ) diff --git a/salt/modules/cp.py b/salt/modules/cp.py index cd894bea58b..5d95d2a31c3 100644 --- a/salt/modules/cp.py +++ b/salt/modules/cp.py @@ -49,7 +49,7 @@ def _gather_pillar(pillarenv, pillar_override): __opts__, __grains__, __opts__['id'], - __opts__['environment'], + __opts__['saltenv'], pillar_override=pillar_override, pillarenv=pillarenv ) diff --git a/salt/modules/dockermod.py b/salt/modules/dockermod.py index dadb3c8c17f..261d0f5b3b2 100644 --- a/salt/modules/dockermod.py +++ b/salt/modules/dockermod.py @@ -5290,7 +5290,7 @@ def _gather_pillar(pillarenv, pillar_override, **grains): grains, # Not sure if these two are correct __opts__['id'], - __opts__['environment'], + __opts__['saltenv'], pillar_override=pillar_override, pillarenv=pillarenv ) diff --git a/salt/modules/event.py b/salt/modules/event.py index 25c9153ab7b..d75e9be1ecf 100644 --- a/salt/modules/event.py +++ b/salt/modules/event.py @@ -225,7 +225,7 @@ def send(tag, data_dict['pillar'] = __pillar__ if with_env_opts: - data_dict['saltenv'] = __opts__.get('environment', 'base') + data_dict['saltenv'] = __opts__.get('saltenv', 'base') data_dict['pillarenv'] = __opts__.get('pillarenv') if kwargs: diff --git a/salt/modules/pillar.py b/salt/modules/pillar.py index 035e082a40f..a3dc6f57c1d 100644 --- a/salt/modules/pillar.py +++ b/salt/modules/pillar.py @@ -237,7 +237,7 @@ def items(*args, **kwargs): pillarenv = kwargs.get('pillarenv') if pillarenv is None: if __opts__.get('pillarenv_from_saltenv', False): - pillarenv = kwargs.get('saltenv') or __opts__['environment'] + pillarenv = kwargs.get('saltenv') or __opts__['saltenv'] else: pillarenv = __opts__['pillarenv'] @@ -468,7 +468,7 @@ def ext(external, pillar=None): __opts__, __grains__, __opts__['id'], - __opts__['environment'], + __opts__['saltenv'], ext=external, pillar_override=pillar) diff --git a/salt/modules/saltcheck.py b/salt/modules/saltcheck.py index f59d2d7e91f..a6d2fe1c693 100644 --- a/salt/modules/saltcheck.py +++ b/salt/modules/saltcheck.py @@ -509,7 +509,7 @@ class SaltCheck(object): # state cache should be updated before running this method search_list = [] cachedir = __opts__.get('cachedir', None) - environment = __opts__['environment'] + environment = __opts__['saltenv'] if environment: path = cachedir + os.sep + "files" + os.sep + environment search_list.append(path) diff --git a/salt/modules/state.py b/salt/modules/state.py index af7ce0fe38f..107332c2fd2 100644 --- a/salt/modules/state.py +++ b/salt/modules/state.py @@ -271,9 +271,9 @@ def _get_opts(**kwargs): if 'saltenv' in kwargs: saltenv = kwargs['saltenv'] if saltenv is not None and not isinstance(saltenv, six.string_types): - opts['environment'] = str(kwargs['saltenv']) + opts['saltenv'] = str(kwargs['saltenv']) else: - opts['environment'] = kwargs['saltenv'] + opts['saltenv'] = kwargs['saltenv'] if 'pillarenv' in kwargs or opts.get('pillarenv_from_saltenv', False): pillarenv = kwargs.get('pillarenv') or kwargs.get('saltenv') @@ -840,7 +840,7 @@ def highstate(test=None, queue=False, **kwargs): kwargs.pop('env') if 'saltenv' in kwargs: - opts['environment'] = kwargs['saltenv'] + opts['saltenv'] = kwargs['saltenv'] if 'pillarenv' in kwargs: opts['pillarenv'] = kwargs['pillarenv'] @@ -1032,8 +1032,8 @@ def sls(mods, test=None, exclude=None, queue=False, **kwargs): # Since this is running a specific SLS file (or files), fall back to the # 'base' saltenv if none is configured and none was passed. - if opts['environment'] is None: - opts['environment'] = 'base' + if opts['saltenv'] is None: + opts['saltenv'] = 'base' pillar_override = kwargs.get('pillar') pillar_enc = kwargs.get('pillar_enc') @@ -1089,7 +1089,7 @@ def sls(mods, test=None, exclude=None, queue=False, **kwargs): st_.push_active() ret = {} try: - high_, errors = st_.render_highstate({opts['environment']: mods}) + high_, errors = st_.render_highstate({opts['saltenv']: mods}) if errors: __context__['retcode'] = 1 @@ -1411,8 +1411,8 @@ def sls_id(id_, mods, test=None, queue=False, **kwargs): # Since this is running a specific ID within a specific SLS file, fall back # to the 'base' saltenv if none is configured and none was passed. - if opts['environment'] is None: - opts['environment'] = 'base' + if opts['saltenv'] is None: + opts['saltenv'] = 'base' pillar_override = kwargs.get('pillar') pillar_enc = kwargs.get('pillar_enc') @@ -1446,7 +1446,7 @@ def sls_id(id_, mods, test=None, queue=False, **kwargs): split_mods = mods.split(',') st_.push_active() try: - high_, errors = st_.render_highstate({opts['environment']: split_mods}) + high_, errors = st_.render_highstate({opts['saltenv']: split_mods}) finally: st_.pop_active() errors += st_.state.verify_high(high_) @@ -1472,7 +1472,7 @@ def sls_id(id_, mods, test=None, queue=False, **kwargs): if not ret: raise SaltInvocationError( 'No matches for ID \'{0}\' found in SLS \'{1}\' within saltenv ' - '\'{2}\''.format(id_, mods, opts['environment']) + '\'{2}\''.format(id_, mods, opts['saltenv']) ) return ret @@ -1523,8 +1523,8 @@ def show_low_sls(mods, test=None, queue=False, **kwargs): # Since this is dealing with a specific SLS file (or files), fall back to # the 'base' saltenv if none is configured and none was passed. - if opts['environment'] is None: - opts['environment'] = 'base' + if opts['saltenv'] is None: + opts['saltenv'] = 'base' pillar_override = kwargs.get('pillar') pillar_enc = kwargs.get('pillar_enc') @@ -1555,7 +1555,7 @@ def show_low_sls(mods, test=None, queue=False, **kwargs): mods = mods.split(',') st_.push_active() try: - high_, errors = st_.render_highstate({opts['environment']: mods}) + high_, errors = st_.render_highstate({opts['saltenv']: mods}) finally: st_.pop_active() errors += st_.state.verify_high(high_) @@ -1610,8 +1610,8 @@ def show_sls(mods, test=None, queue=False, **kwargs): # Since this is dealing with a specific SLS file (or files), fall back to # the 'base' saltenv if none is configured and none was passed. - if opts['environment'] is None: - opts['environment'] = 'base' + if opts['saltenv'] is None: + opts['saltenv'] = 'base' pillar_override = kwargs.get('pillar') pillar_enc = kwargs.get('pillar_enc') @@ -1644,7 +1644,7 @@ def show_sls(mods, test=None, queue=False, **kwargs): mods = mods.split(',') st_.push_active() try: - high_, errors = st_.render_highstate({opts['environment']: mods}) + high_, errors = st_.render_highstate({opts['saltenv']: mods}) finally: st_.pop_active() errors += st_.state.verify_high(high_) diff --git a/salt/pillar/__init__.py b/salt/pillar/__init__.py index 24c7c485e36..903d0f5b1c4 100644 --- a/salt/pillar/__init__.py +++ b/salt/pillar/__init__.py @@ -138,7 +138,7 @@ class AsyncRemotePillar(RemotePillarMixin): def __init__(self, opts, grains, minion_id, saltenv, ext=None, functions=None, pillar_override=None, pillarenv=None, extra_minion_data=None): self.opts = opts - self.opts['environment'] = saltenv + self.opts['saltenv'] = saltenv self.ext = ext self.grains = grains self.minion_id = minion_id @@ -165,7 +165,7 @@ class AsyncRemotePillar(RemotePillarMixin): ''' load = {'id': self.minion_id, 'grains': self.grains, - 'saltenv': self.opts['environment'], + 'saltenv': self.opts['saltenv'], 'pillarenv': self.opts['pillarenv'], 'pillar_override': self.pillar_override, 'extra_minion_data': self.extra_minion_data, @@ -198,7 +198,7 @@ class RemotePillar(RemotePillarMixin): def __init__(self, opts, grains, minion_id, saltenv, ext=None, functions=None, pillar_override=None, pillarenv=None, extra_minion_data=None): self.opts = opts - self.opts['environment'] = saltenv + self.opts['saltenv'] = saltenv self.ext = ext self.grains = grains self.minion_id = minion_id @@ -224,7 +224,7 @@ class RemotePillar(RemotePillarMixin): ''' load = {'id': self.minion_id, 'grains': self.grains, - 'saltenv': self.opts['environment'], + 'saltenv': self.opts['saltenv'], 'pillarenv': self.opts['pillarenv'], 'pillar_override': self.pillar_override, 'extra_minion_data': self.extra_minion_data, @@ -445,9 +445,9 @@ class Pillar(object): else: opts['grains'] = grains # Allow minion/CLI saltenv/pillarenv to take precedence over master - opts['environment'] = saltenv \ + opts['saltenv'] = saltenv \ if saltenv is not None \ - else opts.get('environment') + else opts.get('saltenv') opts['pillarenv'] = pillarenv \ if pillarenv is not None \ else opts.get('pillarenv') diff --git a/salt/pillar/git_pillar.py b/salt/pillar/git_pillar.py index 732183a0890..5450de5ed94 100644 --- a/salt/pillar/git_pillar.py +++ b/salt/pillar/git_pillar.py @@ -404,7 +404,7 @@ def ext_pillar(minion_id, pillar, *repos): # pylint: disable=unused-argument # Map env if env == '__env__' before checking the env value if env == '__env__': env = opts.get('pillarenv') \ - or opts.get('environment') \ + or opts.get('saltenv') \ or opts.get('git_pillar_base') log.debug('__env__ maps to %s', env) diff --git a/salt/state.py b/salt/state.py index f0e405e77d0..cbc34bede94 100644 --- a/salt/state.py +++ b/salt/state.py @@ -763,7 +763,7 @@ class State(object): self.opts, self.opts[u'grains'], self.opts[u'id'], - self.opts[u'environment'], + self.opts[u'saltenv'], pillar_override=self._pillar_override, pillarenv=self.opts.get(u'pillarenv')) return pillar.compile_pillar() @@ -2900,32 +2900,32 @@ class BaseHighState(object): found = 0 # did we find any contents in the top files? # Gather initial top files merging_strategy = self.opts[u'top_file_merging_strategy'] - if merging_strategy == u'same' and not self.opts[u'environment']: + if merging_strategy == u'same' and not self.opts[u'saltenv']: if not self.opts[u'default_top']: raise SaltRenderError( u'top_file_merging_strategy set to \'same\', but no ' u'default_top configuration option was set' ) - if self.opts[u'environment']: + if self.opts[u'saltenv']: contents = self.client.cache_file( self.opts[u'state_top'], - self.opts[u'environment'] + self.opts[u'saltenv'] ) if contents: found = 1 - tops[self.opts[u'environment']] = [ + tops[self.opts[u'saltenv']] = [ compile_template( contents, self.state.rend, self.state.opts[u'renderer'], self.state.opts[u'renderer_blacklist'], self.state.opts[u'renderer_whitelist'], - saltenv=self.opts[u'environment'] + saltenv=self.opts[u'saltenv'] ) ] else: - tops[self.opts[u'environment']] = [{}] + tops[self.opts[u'saltenv']] = [{}] else: found = 0 @@ -3257,8 +3257,8 @@ class BaseHighState(object): matches = DefaultOrderedDict(OrderedDict) # pylint: disable=cell-var-from-loop for saltenv, body in six.iteritems(top): - if self.opts[u'environment']: - if saltenv != self.opts[u'environment']: + if self.opts[u'saltenv']: + if saltenv != self.opts[u'saltenv']: continue for match, data in six.iteritems(body): def _filter_matches(_match, _data, _opts): diff --git a/salt/utils/gitfs.py b/salt/utils/gitfs.py index f619ec4b761..38687d54c43 100644 --- a/salt/utils/gitfs.py +++ b/salt/utils/gitfs.py @@ -910,7 +910,7 @@ class GitProvider(object): ''' if self.branch == '__env__': target = self.opts.get('pillarenv') \ - or self.opts.get('environment') \ + or self.opts.get('saltenv') \ or 'base' return self.opts['{0}_base'.format(self.role)] \ if target == 'base' \ diff --git a/tests/saltsh.py b/tests/saltsh.py index 4167cebc924..6b70f87fcb6 100644 --- a/tests/saltsh.py +++ b/tests/saltsh.py @@ -90,7 +90,7 @@ def get_salt_vars(): __opts__, __grains__, __opts__.get('id'), - __opts__.get('environment'), + __opts__.get('saltenv'), ).compile_pillar() else: __pillar__ = {} diff --git a/tests/unit/test_pillar.py b/tests/unit/test_pillar.py index 476941f8f4e..f4ad72a9956 100644 --- a/tests/unit/test_pillar.py +++ b/tests/unit/test_pillar.py @@ -55,7 +55,7 @@ class PillarTestCase(TestCase): 'os': 'Ubuntu', } pillar = salt.pillar.Pillar(opts, grains, 'mocked-minion', 'dev') - self.assertEqual(pillar.opts['environment'], 'dev') + self.assertEqual(pillar.opts['saltenv'], 'dev') self.assertEqual(pillar.opts['pillarenv'], 'dev') def test_ext_pillar_no_extra_minion_data_val_dict(self): @@ -416,7 +416,7 @@ class PillarTestCase(TestCase): 'state_top': '', 'pillar_roots': [], 'extension_modules': '', - 'environment': 'base', + 'saltenv': 'base', 'file_roots': [], } grains = { @@ -584,7 +584,7 @@ class RemotePillarTestCase(TestCase): salt.pillar.RemotePillar({}, self.grains, 'mocked-minion', 'dev') mock_get_extra_minion_data.assert_called_once_with( - {'environment': 'dev'}) + {'saltenv': 'dev'}) def test_multiple_keys_in_opts_added_to_pillar(self): opts = { @@ -702,7 +702,7 @@ class AsyncRemotePillarTestCase(TestCase): salt.pillar.RemotePillar({}, self.grains, 'mocked-minion', 'dev') mock_get_extra_minion_data.assert_called_once_with( - {'environment': 'dev'}) + {'saltenv': 'dev'}) def test_pillar_send_extra_minion_data_from_config(self): opts = {