|
| 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 | +# Import salt libs |
| 69 | +import salt.utils.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 salt.utils.gitfs.GitFS( |
| 80 | + __opts__, |
| 81 | + __opts__['gitfs_remotes'], |
| 82 | + per_remote_overrides=PER_REMOTE_OVERRIDES, |
| 83 | + per_remote_only=PER_REMOTE_ONLY, |
| 84 | + init_remotes=init_remotes) |
| 85 | + |
| 86 | + |
| 87 | +def __virtual__(): |
| 88 | + ''' |
| 89 | + Only load if the desired provider module is present and gitfs is enabled |
| 90 | + properly in the master config file. |
| 91 | + ''' |
| 92 | + if __virtualname__ not in __opts__['fileserver_backend']: |
| 93 | + return False |
| 94 | + try: |
| 95 | + _gitfs(init_remotes=False) |
| 96 | + # Initialization of the GitFS object did not fail, so we know we have |
| 97 | + # valid configuration syntax and that a valid provider was detected. |
| 98 | + return __virtualname__ |
| 99 | + except FileserverConfigError: |
| 100 | + pass |
| 101 | + return False |
| 102 | + |
| 103 | + |
| 104 | +def clear_cache(): |
| 105 | + ''' |
| 106 | + Completely clear gitfs cache |
| 107 | + ''' |
| 108 | + return _gitfs(init_remotes=False).clear_cache() |
| 109 | + |
| 110 | + |
| 111 | +def clear_lock(remote=None, lock_type='update'): |
| 112 | + ''' |
| 113 | + Clear update.lk |
| 114 | + ''' |
| 115 | + return _gitfs().clear_lock(remote=remote, lock_type=lock_type) |
| 116 | + |
| 117 | + |
| 118 | +def lock(remote=None): |
| 119 | + ''' |
| 120 | + Place an update.lk |
| 121 | +
|
| 122 | + ``remote`` can either be a dictionary containing repo configuration |
| 123 | + information, or a pattern. If the latter, then remotes for which the URL |
| 124 | + matches the pattern will be locked. |
| 125 | + ''' |
| 126 | + return _gitfs().lock(remote=remote) |
| 127 | + |
| 128 | + |
| 129 | +def update(remotes=None): |
| 130 | + ''' |
| 131 | + Execute a git fetch on all of the repos |
| 132 | + ''' |
| 133 | + _gitfs().update(remotes) |
| 134 | + |
| 135 | + |
| 136 | +def update_intervals(): |
| 137 | + ''' |
| 138 | + Returns the update intervals for each configured remote |
| 139 | + ''' |
| 140 | + return _gitfs().update_intervals() |
| 141 | + |
| 142 | + |
| 143 | +def envs(ignore_cache=False): |
| 144 | + ''' |
| 145 | + Return a list of refs that can be used as environments |
| 146 | + ''' |
| 147 | + return _gitfs().envs(ignore_cache=ignore_cache) |
| 148 | + |
| 149 | + |
| 150 | +def find_file(path, tgt_env='base', **kwargs): # pylint: disable=W0613 |
| 151 | + ''' |
| 152 | + Find the first file to match the path and ref, read the file out of git |
| 153 | + and send the path to the newly cached file |
| 154 | + ''' |
| 155 | + return _gitfs().find_file(path, tgt_env=tgt_env, **kwargs) |
| 156 | + |
| 157 | + |
| 158 | +def init(): |
| 159 | + ''' |
| 160 | + Initialize remotes. This is only used by the master's pre-flight checks, |
| 161 | + and is not invoked by GitFS. |
| 162 | + ''' |
| 163 | + _gitfs() |
| 164 | + |
| 165 | + |
| 166 | +def serve_file(load, fnd): |
| 167 | + ''' |
| 168 | + Return a chunk from a file based on the data received |
| 169 | + ''' |
| 170 | + return _gitfs().serve_file(load, fnd) |
| 171 | + |
| 172 | + |
| 173 | +def file_hash(load, fnd): |
| 174 | + ''' |
| 175 | + Return a file hash, the hash type is set in the master config file |
| 176 | + ''' |
| 177 | + return _gitfs().file_hash(load, fnd) |
| 178 | + |
| 179 | + |
| 180 | +def file_list(load): |
| 181 | + ''' |
| 182 | + Return a list of all files on the file server in a specified |
| 183 | + environment (specified as a key within the load dict). |
| 184 | + ''' |
| 185 | + return _gitfs().file_list(load) |
| 186 | + |
| 187 | + |
| 188 | +def file_list_emptydirs(load): # pylint: disable=W0613 |
| 189 | + ''' |
| 190 | + Return a list of all empty directories on the master |
| 191 | + ''' |
| 192 | + # Cannot have empty dirs in git |
| 193 | + return [] |
| 194 | + |
| 195 | + |
| 196 | +def dir_list(load): |
| 197 | + ''' |
| 198 | + Return a list of all directories on the master |
| 199 | + ''' |
| 200 | + return _gitfs().dir_list(load) |
| 201 | + |
| 202 | + |
| 203 | +def symlink_list(load): |
| 204 | + ''' |
| 205 | + Return a dict of all symlinks based on a given path in the repo |
| 206 | + ''' |
| 207 | + return _gitfs().symlink_list(load) |
0 commit comments