Skip to content

Commit 74b0ea9

Browse files
authored
Merge pull request #838 from jettero/4.0-centos6-fix
4.0 centos6 fix
2 parents 6d965a4 + 170c8be commit 74b0ea9

File tree

5 files changed

+3372
-2
lines changed

5 files changed

+3372
-2
lines changed
+194
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
# -*- coding: utf-8 -*-
2+
'''
3+
Git Fileserver Backend
4+
5+
With this backend, branches and tags in a remote git repository are exposed to
6+
salt as different environments.
7+
8+
To enable, add ``gitfs`` to the :conf_master:`fileserver_backend` option in the
9+
Master config file.
10+
11+
.. code-block:: yaml
12+
13+
fileserver_backend:
14+
- gitfs
15+
16+
.. note::
17+
``git`` also works here. Prior to the 2018.3.0 release, *only* ``git``
18+
would work.
19+
20+
The Git fileserver backend supports both pygit2_ and GitPython_, to provide the
21+
Python interface to git. If both are present, the order of preference for which
22+
one will be chosen is the same as the order in which they were listed: pygit2,
23+
then GitPython.
24+
25+
An optional master config parameter (:conf_master:`gitfs_provider`) can be used
26+
to specify which provider should be used, in the event that compatible versions
27+
of both pygit2_ and GitPython_ are installed.
28+
29+
More detailed information on how to use GitFS can be found in the :ref:`GitFS
30+
Walkthrough <tutorial-gitfs>`.
31+
32+
.. note:: Minimum requirements
33+
34+
To use pygit2_ for GitFS requires a minimum pygit2_ version of 0.20.3.
35+
pygit2_ 0.20.3 requires libgit2_ 0.20.0. pygit2_ and libgit2_ are developed
36+
alongside one another, so it is recommended to keep them both at the same
37+
major release to avoid unexpected behavior. For example, pygit2_ 0.21.x
38+
requires libgit2_ 0.21.x, pygit2_ 0.22.x will require libgit2_ 0.22.x, etc.
39+
40+
To use GitPython_ for GitFS requires a minimum GitPython version of 0.3.0,
41+
as well as the git CLI utility. Instructions for installing GitPython can
42+
be found :ref:`here <gitfs-dependencies>`.
43+
44+
To clear stale refs the git CLI utility must also be installed.
45+
46+
.. _pygit2: https://github.com/libgit2/pygit2
47+
.. _libgit2: https://libgit2.github.com/
48+
.. _GitPython: https://github.com/gitpython-developers/GitPython
49+
'''
50+
51+
# Import python libs
52+
from __future__ import absolute_import, print_function, unicode_literals
53+
import logging
54+
55+
PER_REMOTE_OVERRIDES = (
56+
'base', 'mountpoint', 'root', 'ssl_verify',
57+
'saltenv_whitelist', 'saltenv_blacklist',
58+
'env_whitelist', 'env_blacklist', 'refspecs',
59+
'disable_saltenv_mapping', 'ref_types', 'update_interval',
60+
)
61+
PER_REMOTE_ONLY = ('all_saltenvs', 'name', 'saltenv')
62+
63+
# Auth support (auth params can be global or per-remote, too)
64+
AUTH_PROVIDERS = ('pygit2',)
65+
AUTH_PARAMS = ('user', 'password', 'pubkey', 'privkey', 'passphrase',
66+
'insecure_auth')
67+
68+
from hubblestack.utils.signing import find_wrapf
69+
from hubblestack.extmods.utils.gitfs import GitFS
70+
from salt.exceptions import FileserverConfigError
71+
72+
log = logging.getLogger(__name__)
73+
74+
# Define the module's virtual name
75+
__virtualname__ = 'gitfs'
76+
77+
78+
def _gitfs(init_remotes=True):
79+
return GitFS( __opts__, __opts__['gitfs_remotes'],
80+
per_remote_overrides=PER_REMOTE_OVERRIDES,
81+
per_remote_only=PER_REMOTE_ONLY,
82+
init_remotes=init_remotes)
83+
84+
def __virtual__():
85+
'''
86+
Only load if the desired provider module is present and gitfs is enabled
87+
properly in the master config file.
88+
'''
89+
if __virtualname__ not in __opts__['fileserver_backend']:
90+
log.info("no fileserver_backend configs, skipping gitfs")
91+
return False
92+
try:
93+
_gitfs(init_remotes=False)
94+
# Initialization of the GitFS object did not fail, so we know we have
95+
# valid configuration syntax and that a valid provider was detected.
96+
log.error("have fileserver_backend configs and GitFS object loads: success loading %s", __virtualname__)
97+
return __virtualname__
98+
except FileserverConfigError:
99+
pass
100+
log.error("something went wrong loading GitFS object, claiming to not have gitfs")
101+
return False
102+
103+
def clear_cache():
104+
'''
105+
Completely clear gitfs cache
106+
'''
107+
return _gitfs(init_remotes=False).clear_cache()
108+
109+
def clear_lock(remote=None, lock_type='update'):
110+
'''
111+
Clear update.lk
112+
'''
113+
return _gitfs().clear_lock(remote=remote, lock_type=lock_type)
114+
115+
def lock(remote=None):
116+
'''
117+
Place an update.lk
118+
119+
``remote`` can either be a dictionary containing repo configuration
120+
information, or a pattern. If the latter, then remotes for which the URL
121+
matches the pattern will be locked.
122+
'''
123+
return _gitfs().lock(remote=remote)
124+
125+
def update(remotes=None):
126+
'''
127+
Execute a git fetch on all of the repos
128+
'''
129+
_gitfs().update(remotes)
130+
131+
def update_intervals():
132+
'''
133+
Returns the update intervals for each configured remote
134+
'''
135+
return _gitfs().update_intervals()
136+
137+
def envs(ignore_cache=False):
138+
'''
139+
Return a list of refs that can be used as environments
140+
'''
141+
return _gitfs().envs(ignore_cache=ignore_cache)
142+
143+
@find_wrapf(not_found={'rel': '', 'path': ''}, real_path='path')
144+
def find_file(path, tgt_env='base', **kwargs): # pylint: disable=W0613
145+
'''
146+
Find the first file to match the path and ref, read the file out of git
147+
and send the path to the newly cached file
148+
'''
149+
return _gitfs().find_file(path, tgt_env=tgt_env, **kwargs)
150+
151+
def init():
152+
'''
153+
Initialize remotes. This is only used by the master's pre-flight checks,
154+
and is not invoked by GitFS.
155+
'''
156+
_gitfs()
157+
158+
def serve_file(load, fnd):
159+
'''
160+
Return a chunk from a file based on the data received
161+
'''
162+
return _gitfs().serve_file(load, fnd)
163+
164+
def file_hash(load, fnd):
165+
'''
166+
Return a file hash, the hash type is set in the master config file
167+
'''
168+
return _gitfs().file_hash(load, fnd)
169+
170+
def file_list(load):
171+
'''
172+
Return a list of all files on the file server in a specified
173+
environment (specified as a key within the load dict).
174+
'''
175+
return _gitfs().file_list(load)
176+
177+
def file_list_emptydirs(load): # pylint: disable=W0613
178+
'''
179+
Return a list of all empty directories on the master
180+
'''
181+
# Cannot have empty dirs in git
182+
return []
183+
184+
def dir_list(load):
185+
'''
186+
Return a list of all directories on the master
187+
'''
188+
return _gitfs().dir_list(load)
189+
190+
def symlink_list(load):
191+
'''
192+
Return a dict of all symlinks based on a given path in the repo
193+
'''
194+
return _gitfs().symlink_list(load)

0 commit comments

Comments
 (0)