Merge pull request #50768 from sathieu/git_pillar_all_saltenvs

git_pillar: Add support for all_saltenvs parameter
This commit is contained in:
Mike Place 2018-12-10 09:39:36 -07:00 committed by GitHub
commit 3a71567f43
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 220 additions and 2 deletions

View file

@ -329,6 +329,27 @@ mountpoint to ``web/`` (and restart the ``salt-master`` daemon).
file in the same pillar environment.
- Salt versions prior to 2018.3.4 ignore the ``root`` parameter when
``mountpoint`` is set.
.. _git-pillar-all_saltenvs:
all_saltenvs
~~~~~~~~~~~~
.. versionadded:: 2018.3.4
When ``__env__`` is specified as the branch name, ``all_saltenvs`` per-remote configuration parameter overrides the logic Salt uses to map branches/tags to pillar environments (i.e. pillarenvs). This allows a single branch/tag to appear in all saltenvs. Example:
.. code-block:: yaml
ext_pillar:
- git:
- __env__ https://mydomain.tld/top.git
- all_saltenvs: master
- __env__ https://mydomain.tld/pillar-nginx.git:
- mountpoint: web/server/
- __env__ https://mydomain.tld/pillar-appdata.git:
- mountpoint: web/server/
'''
from __future__ import absolute_import, print_function, unicode_literals
@ -348,7 +369,7 @@ from salt.pillar import Pillar
from salt.ext import six
PER_REMOTE_OVERRIDES = ('env', 'root', 'ssl_verify', 'refspecs')
PER_REMOTE_ONLY = ('name', 'mountpoint')
PER_REMOTE_ONLY = ('name', 'mountpoint', 'all_saltenvs')
GLOBAL_ONLY = ('base', 'branch')
# Set up logging

View file

@ -984,6 +984,11 @@ class GitProvider(object):
Resolve dynamically-set branch
'''
if self.role == 'git_pillar' and self.branch == '__env__':
try:
return self.all_saltenvs
except AttributeError:
# all_saltenvs not configured for this remote
pass
target = self.opts.get('pillarenv') \
or self.opts.get('saltenv') \
or 'base'
@ -2971,7 +2976,11 @@ class GitPillar(GitBase):
cachedir = self.do_checkout(repo)
if cachedir is not None:
# Figure out which environment this remote should be assigned
if repo.env:
if repo.branch == '__env__' and hasattr(repo, 'all_saltenvs'):
env = self.opts.get('pillarenv') \
or self.opts.get('saltenv') \
or self.opts.get('git_pillar_base')
elif repo.env:
env = repo.env
else:
env = 'base' if repo.branch == repo.base else repo.get_checkout_target()

View file

@ -460,6 +460,34 @@ class GitPythonMixin(object):
''')
self.assertEqual(ret, expected)
def test_all_saltenvs(self):
'''
Test all_saltenvs parameter.
'''
ret = self.get_pillar('''\
file_ignore_regex: []
file_ignore_glob: []
git_pillar_provider: gitpython
cachedir: {cachedir}
extension_modules: {extmods}
pillarenv: dev
ext_pillar:
- git:
- __env__ {url_extra_repo}:
- all_saltenvs: master
- __env__ {url}:
- mountpoint: nowhere
''')
self.assertEqual(
ret,
{'branch': 'dev',
'motd': 'The force will be with you. Always.',
'mylist': ['dev'],
'mydict': {'dev': True,
'nested_list': ['dev'],
'nested_dict': {'dev': True}}}
)
@destructiveTest
@skipIf(NO_MOCK, NO_MOCK_REASON)
@ -1613,6 +1641,107 @@ class TestPygit2SSH(GitPillarSSHTestBase):
''')
self.assertEqual(ret, expected)
@requires_system_grains
def test_all_saltenvs(self, grains):
'''
Test all_saltenvs parameter.
'''
expected = {'branch': 'dev',
'motd': 'The force will be with you. Always.',
'mylist': ['dev'],
'mydict': {'dev': True,
'nested_list': ['dev'],
'nested_dict': {'dev': True}
}
}
# Test with passphraseless key and global credential options
ret = self.get_pillar('''\
file_ignore_regex: []
file_ignore_glob: []
git_pillar_provider: pygit2
git_pillar_pubkey: {pubkey_nopass}
git_pillar_privkey: {privkey_nopass}
cachedir: {cachedir}
extension_modules: {extmods}
pillarenv: dev
ext_pillar:
- git:
- __env__ {url_extra_repo}:
- all_saltenvs: master
- __env__ {url}:
- mountpoint: nowhere
''')
self.assertEqual(ret, expected)
# Test with passphraseless key and per-repo credential options
ret = self.get_pillar('''\
file_ignore_regex: []
file_ignore_glob: []
git_pillar_provider: pygit2
cachedir: {cachedir}
extension_modules: {extmods}
pillarenv: dev
ext_pillar:
- git:
- __env__ {url_extra_repo}:
- all_saltenvs: master
- pubkey: {pubkey_nopass}
- privkey: {privkey_nopass}
- __env__ {url}:
- mountpoint: nowhere
- pubkey: {pubkey_nopass}
- privkey: {privkey_nopass}
''')
self.assertEqual(ret, expected)
if grains['os_family'] == 'Debian':
# passphrase-protected currently does not work here
return
# Test with passphrase-protected key and global credential options
ret = self.get_pillar('''\
file_ignore_regex: []
file_ignore_glob: []
git_pillar_provider: pygit2
git_pillar_pubkey: {pubkey_withpass}
git_pillar_privkey: {privkey_withpass}
git_pillar_passphrase: {passphrase}
cachedir: {cachedir}
extension_modules: {extmods}
pillarenv: dev
ext_pillar:
- git:
- __env__ {url_extra_repo}:
- all_saltenvs: master
- __env__ {url}:
- mountpoint: nowhere
''')
self.assertEqual(ret, expected)
# Test with passphrase-protected key and per-repo credential options
ret = self.get_pillar('''\
file_ignore_regex: []
file_ignore_glob: []
git_pillar_provider: pygit2
cachedir: {cachedir}
extension_modules: {extmods}
pillarenv: dev
ext_pillar:
- git:
- __env__ {url_extra_repo}:
- all_saltenvs: master
- pubkey: {pubkey_nopass}
- privkey: {privkey_nopass}
- passphrase: {passphrase}
- __env__ {url}:
- mountpoint: nowhere
- pubkey: {pubkey_nopass}
- privkey: {privkey_nopass}
- passphrase: {passphrase}
''')
self.assertEqual(ret, expected)
@skipIf(NO_MOCK, NO_MOCK_REASON)
@skipIf(_windows_or_mac(), 'minion is windows or mac')
@ -1962,6 +2091,34 @@ class TestPygit2HTTP(GitPillarHTTPTestBase):
''')
self.assertEqual(ret, expected)
def test_all_saltenvs(self):
'''
Test all_saltenvs parameter.
'''
ret = self.get_pillar('''\
file_ignore_regex: []
file_ignore_glob: []
git_pillar_provider: pygit2
cachedir: {cachedir}
extension_modules: {extmods}
pillarenv: dev
ext_pillar:
- git:
- __env__ {url_extra_repo}:
- all_saltenvs: master
- __env__ {url}:
- mountpoint: nowhere
''')
self.assertEqual(
ret,
{'branch': 'dev',
'motd': 'The force will be with you. Always.',
'mylist': ['dev'],
'mydict': {'dev': True,
'nested_list': ['dev'],
'nested_dict': {'dev': True}}}
)
@skipIf(NO_MOCK, NO_MOCK_REASON)
@skipIf(_windows_or_mac(), 'minion is windows or mac')
@ -2531,3 +2688,34 @@ class TestPygit2AuthenticatedHTTP(GitPillarHTTPTestBase):
- env: base
''')
self.assertEqual(ret, expected)
def test_all_saltenvs(self):
'''
Test all_saltenvs parameter.
'''
ret = self.get_pillar('''\
file_ignore_regex: []
file_ignore_glob: []
git_pillar_provider: pygit2
git_pillar_user: {user}
git_pillar_password: {password}
git_pillar_insecure_auth: True
cachedir: {cachedir}
extension_modules: {extmods}
pillarenv: dev
ext_pillar:
- git:
- __env__ {url_extra_repo}:
- all_saltenvs: master
- __env__ {url}:
- mountpoint: nowhere
''')
self.assertEqual(
ret,
{'branch': 'dev',
'motd': 'The force will be with you. Always.',
'mylist': ['dev'],
'mydict': {'dev': True,
'nested_list': ['dev'],
'nested_dict': {'dev': True}}}
)