1
0

gitfs.rst 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179
  1. .. _tutorial-gitfs:
  2. ==================================
  3. Git Fileserver Backend Walkthrough
  4. ==================================
  5. .. note::
  6. This walkthrough assumes basic knowledge of Salt. To get up to speed, check
  7. out the :ref:`Salt Walkthrough <tutorial-salt-walk-through>`.
  8. The gitfs backend allows Salt to serve files from git repositories. It can be
  9. enabled by adding ``git`` to the :conf_master:`fileserver_backend` list, and
  10. configuring one or more repositories in :conf_master:`gitfs_remotes`.
  11. Branches and tags become Salt fileserver environments.
  12. .. note::
  13. Branching and tagging can result in a lot of potentially-conflicting
  14. :ref:`top files <states-top>`, for this reason it may be useful to set
  15. :conf_minion:`top_file_merging_strategy` to ``same`` in the minions' config
  16. files if the top files are being managed in a GitFS repo.
  17. .. _gitfs-dependencies:
  18. Installing Dependencies
  19. =======================
  20. Both pygit2_ and GitPython_ are supported Python interfaces to git. If
  21. compatible versions of both are installed, pygit2_ will be preferred. In these
  22. cases, GitPython_ can be forced using the :conf_master:`gitfs_provider`
  23. parameter in the master config file.
  24. .. note::
  25. It is recommended to always run the most recent version of any the below
  26. dependencies. Certain features of GitFS may not be available without
  27. the most recent version of the chosen library.
  28. .. _pygit2: https://github.com/libgit2/pygit2
  29. .. _GitPython: https://github.com/gitpython-developers/GitPython
  30. pygit2
  31. ------
  32. The minimum supported version of pygit2_ is 0.20.3. Availability for this
  33. version of pygit2_ is still limited, though the SaltStack team is working to
  34. get compatible versions available for as many platforms as possible.
  35. For the Fedora/EPEL versions which have a new enough version packaged, the
  36. following command would be used to install pygit2_:
  37. .. code-block:: bash
  38. # yum install python-pygit2
  39. Provided a valid version is packaged for Debian/Ubuntu (which is not currently
  40. the case), the package name would be the same, and the following command would
  41. be used to install it:
  42. .. code-block:: bash
  43. # apt-get install python-pygit2
  44. If pygit2_ is not packaged for the platform on which the Master is running, the
  45. pygit2_ website has installation instructions
  46. `here <pygit2-install-instructions>`_. Keep in mind however that
  47. following these instructions will install libgit2_ and pygit2_ without system
  48. packages. Additionally, keep in mind that :ref:`SSH authentication in pygit2
  49. <pygit2-authentication-ssh>` requires libssh2_ (*not* libssh) development
  50. libraries to be present before libgit2_ is built. On some Debian-based distros
  51. ``pkg-config`` is also required to link libgit2_ with libssh2.
  52. .. note::
  53. If you are receiving the error "Unsupported URL Protocol" in the Salt Master
  54. log when making a connection using SSH, review the libssh2 details listed
  55. above.
  56. Additionally, version 0.21.0 of pygit2 introduced a dependency on python-cffi_,
  57. which in turn depends on newer releases of libffi_. Upgrading libffi_ is not
  58. advisable as several other applications depend on it, so on older LTS linux
  59. releases pygit2_ 0.20.3 and libgit2_ 0.20.0 is the recommended combination.
  60. .. warning::
  61. pygit2_ is actively developed and `frequently makes
  62. non-backwards-compatible API changes <pygit2-version-policy>`_, even in
  63. minor releases. It is not uncommon for pygit2_ upgrades to result in errors
  64. in Salt. Please take care when upgrading pygit2_, and pay close attention
  65. to the changelog_, keeping an eye out for API changes. Errors can be
  66. reported on the `SaltStack issue tracker <saltstack-issue-tracker>`_.
  67. .. _pygit2-version-policy: http://www.pygit2.org/install.html#version-numbers
  68. .. _changelog: https://github.com/libgit2/pygit2#changelog
  69. .. _saltstack-issue-tracker: https://github.com/saltstack/salt/issues
  70. .. _pygit2-install-instructions: http://www.pygit2.org/install.html
  71. .. _libgit2: https://libgit2.github.com/
  72. .. _libssh2: http://www.libssh2.org/
  73. .. _python-cffi: https://pypi.python.org/pypi/cffi
  74. .. _libffi: http://sourceware.org/libffi/
  75. RedHat Pygit2 Issues
  76. ~~~~~~~~~~~~~~~~~~~~
  77. The release of RedHat/CentOS 7.3 upgraded both ``python-cffi`` and
  78. ``http-parser``, both of which are dependencies for pygit2_/libgit2_. Both
  79. ``pygit2`` and ``libgit2`` packages (which are from the EPEL repository) should
  80. be upgraded to the most recent versions, at least to ``0.24.2``.
  81. The below errors will show up in the master log if an incompatible
  82. ``python-pygit2`` package is installed:
  83. .. code-block:: text
  84. 2017-02-10 09:07:34,892 [salt.utils.gitfs ][ERROR ][11211] Import pygit2 failed: CompileError: command 'gcc' failed with exit status 1
  85. 2017-02-10 09:07:34,907 [salt.utils.gitfs ][ERROR ][11211] gitfs is configured but could not be loaded, are pygit2 and libgit2 installed?
  86. 2017-02-10 09:07:34,907 [salt.utils.gitfs ][CRITICAL][11211] No suitable gitfs provider module is installed.
  87. 2017-02-10 09:07:34,912 [salt.master ][CRITICAL][11211] Master failed pre flight checks, exiting
  88. The below errors will show up in the master log if an incompatible ``libgit2``
  89. package is installed:
  90. .. code-block:: text
  91. 2017-02-15 18:04:45,211 [salt.utils.gitfs ][ERROR ][6211] Error occurred fetching gitfs remote 'https://foo.com/bar.git': No Content-Type header in response
  92. A restart of the ``salt-master`` daemon and gitfs cache directory clean up may
  93. be required to allow http(s) repositories to continue to be fetched.
  94. GitPython
  95. ---------
  96. GitPython_ 0.3.0 or newer is required to use GitPython for gitfs. For
  97. RHEL-based Linux distros, a compatible version is available in EPEL, and can be
  98. easily installed on the master using yum:
  99. .. code-block:: bash
  100. # yum install GitPython
  101. Ubuntu 14.04 LTS and Debian Wheezy (7.x) also have a compatible version packaged:
  102. .. code-block:: bash
  103. # apt-get install python-git
  104. GitPython_ requires the ``git`` CLI utility to work. If installed from a system
  105. package, then git should already be installed, but if installed via pip_ then
  106. it may still be necessary to install git separately. For MacOS users,
  107. GitPython_ comes bundled in with the Salt installer, but git must still be
  108. installed for it to work properly. Git can be installed in several ways,
  109. including by installing XCode_.
  110. .. _pip: http://www.pip-installer.org/
  111. .. _XCode: https://developer.apple.com/xcode/
  112. .. warning::
  113. Keep in mind that if GitPython has been previously installed on the master
  114. using pip (even if it was subsequently uninstalled), then it may still
  115. exist in the build cache (typically ``/tmp/pip-build-root/GitPython``) if
  116. the cache is not cleared after installation. The package in the build cache
  117. will override any requirement specifiers, so if you try upgrading to
  118. version 0.3.2.RC1 by running ``pip install 'GitPython==0.3.2.RC1'`` then it
  119. will ignore this and simply install the version from the cache directory.
  120. Therefore, it may be necessary to delete the GitPython directory from the
  121. build cache in order to ensure that the specified version is installed.
  122. .. warning::
  123. GitPython_ 2.0.9 and newer is not compatible with Python 2.6. If installing
  124. GitPython_ using pip on a machine running Python 2.6, make sure that a
  125. version earlier than 2.0.9 is installed. This can be done on the CLI by
  126. running ``pip install 'GitPython<2.0.9'``, or in a :py:func:`pip.installed
  127. <salt.states.pip_state.installed>` state using the following SLS:
  128. .. code-block:: yaml
  129. GitPython:
  130. pip.installed:
  131. - name: 'GitPython < 2.0.9'
  132. Simple Configuration
  133. ====================
  134. To use the gitfs backend, only two configuration changes are required on the
  135. master:
  136. 1. Include ``gitfs`` in the :conf_master:`fileserver_backend` list in the
  137. master config file:
  138. .. code-block:: yaml
  139. fileserver_backend:
  140. - gitfs
  141. .. note::
  142. ``git`` also works here. Prior to the 2018.3.0 release, *only* ``git``
  143. would work.
  144. 2. Specify one or more ``git://``, ``https://``, ``file://``, or ``ssh://``
  145. URLs in :conf_master:`gitfs_remotes` to configure which repositories to
  146. cache and search for requested files:
  147. .. code-block:: yaml
  148. gitfs_remotes:
  149. - https://github.com/saltstack-formulas/salt-formula.git
  150. SSH remotes can also be configured using scp-like syntax:
  151. .. code-block:: yaml
  152. gitfs_remotes:
  153. - git@github.com:user/repo.git
  154. - ssh://user@domain.tld/path/to/repo.git
  155. Information on how to authenticate to SSH remotes can be found :ref:`here
  156. <gitfs-authentication>`.
  157. 3. Restart the master to load the new configuration.
  158. .. note::
  159. In a master/minion setup, files from a gitfs remote are cached once by the
  160. master, so minions do not need direct access to the git repository.
  161. Multiple Remotes
  162. ================
  163. The ``gitfs_remotes`` option accepts an ordered list of git remotes to
  164. cache and search, in listed order, for requested files.
  165. A simple scenario illustrates this cascading lookup behavior:
  166. If the ``gitfs_remotes`` option specifies three remotes:
  167. .. code-block:: yaml
  168. gitfs_remotes:
  169. - git://github.com/example/first.git
  170. - https://github.com/example/second.git
  171. - file:///root/third
  172. And each repository contains some files:
  173. .. code-block:: yaml
  174. first.git:
  175. top.sls
  176. edit/vim.sls
  177. edit/vimrc
  178. nginx/init.sls
  179. second.git:
  180. edit/dev_vimrc
  181. haproxy/init.sls
  182. third:
  183. haproxy/haproxy.conf
  184. edit/dev_vimrc
  185. Salt will attempt to lookup the requested file from each gitfs remote
  186. repository in the order in which they are defined in the configuration. The
  187. :strong:`git://github.com/example/first.git` remote will be searched first.
  188. If the requested file is found, then it is served and no further searching
  189. is executed. For example:
  190. * A request for the file :strong:`salt://haproxy/init.sls` will be served from
  191. the :strong:`https://github.com/example/second.git` git repo.
  192. * A request for the file :strong:`salt://haproxy/haproxy.conf` will be served from the
  193. :strong:`file:///root/third` repo.
  194. .. note::
  195. This example is purposefully contrived to illustrate the behavior of the
  196. gitfs backend. This example should not be read as a recommended way to lay
  197. out files and git repos.
  198. The :strong:`file://` prefix denotes a git repository in a local directory.
  199. However, it will still use the given :strong:`file://` URL as a remote,
  200. rather than copying the git repo to the salt cache. This means that any
  201. refs you want accessible must exist as *local* refs in the specified repo.
  202. .. warning::
  203. Salt versions prior to 2014.1.0 are not tolerant of changing the
  204. order of remotes or modifying the URI of existing remotes. In those
  205. versions, when modifying remotes it is a good idea to remove the gitfs
  206. cache directory (``/var/cache/salt/master/gitfs``) before restarting the
  207. salt-master service.
  208. .. _gitfs-per-remote-config:
  209. Per-remote Configuration Parameters
  210. ===================================
  211. .. versionadded:: 2014.7.0
  212. The following master config parameters are global (that is, they apply to all
  213. configured gitfs remotes):
  214. * :conf_master:`gitfs_base`
  215. * :conf_master:`gitfs_root`
  216. * :conf_master:`gitfs_ssl_verify`
  217. * :conf_master:`gitfs_mountpoint` (new in 2014.7.0)
  218. * :conf_master:`gitfs_user` (**pygit2 only**, new in 2014.7.0)
  219. * :conf_master:`gitfs_password` (**pygit2 only**, new in 2014.7.0)
  220. * :conf_master:`gitfs_insecure_auth` (**pygit2 only**, new in 2014.7.0)
  221. * :conf_master:`gitfs_pubkey` (**pygit2 only**, new in 2014.7.0)
  222. * :conf_master:`gitfs_privkey` (**pygit2 only**, new in 2014.7.0)
  223. * :conf_master:`gitfs_passphrase` (**pygit2 only**, new in 2014.7.0)
  224. * :conf_master:`gitfs_refspecs` (new in 2017.7.0)
  225. * :conf_master:`gitfs_disable_saltenv_mapping` (new in 2018.3.0)
  226. * :conf_master:`gitfs_ref_types` (new in 2018.3.0)
  227. * :conf_master:`gitfs_update_interval` (new in 2018.3.0)
  228. .. note::
  229. pygit2 only supports disabling SSL verification in versions 0.23.2 and
  230. newer.
  231. These parameters can now be overridden on a per-remote basis. This allows for a
  232. tremendous amount of customization. Here's some example usage:
  233. .. code-block:: yaml
  234. gitfs_provider: pygit2
  235. gitfs_base: develop
  236. gitfs_remotes:
  237. - https://foo.com/foo.git
  238. - https://foo.com/bar.git:
  239. - root: salt
  240. - mountpoint: salt://bar
  241. - base: salt-base
  242. - ssl_verify: False
  243. - update_interval: 120
  244. - https://foo.com/bar.git:
  245. - name: second_bar_repo
  246. - root: other/salt
  247. - mountpoint: salt://other/bar
  248. - base: salt-base
  249. - ref_types:
  250. - branch
  251. - http://foo.com/baz.git:
  252. - root: salt/states
  253. - user: joe
  254. - password: mysupersecretpassword
  255. - insecure_auth: True
  256. - disable_saltenv_mapping: True
  257. - saltenv:
  258. - foo:
  259. - ref: foo
  260. - http://foo.com/quux.git:
  261. - all_saltenvs: master
  262. .. important::
  263. There are two important distinctions which should be noted for per-remote
  264. configuration:
  265. 1. The URL of a remote which has per-remote configuration must be suffixed
  266. with a colon.
  267. 2. Per-remote configuration parameters are named like the global versions,
  268. with the ``gitfs_`` removed from the beginning. The exception being the
  269. ``name``, ``saltenv``, and ``all_saltenvs`` parameters, which are only
  270. available to per-remote configurations.
  271. The ``all_saltenvs`` parameter is new in the 2018.3.0 release.
  272. In the example configuration above, the following is true:
  273. 1. The first and fourth gitfs remotes will use the ``develop`` branch/tag as the
  274. ``base`` environment, while the second and third will use the ``salt-base``
  275. branch/tag as the ``base`` environment.
  276. 2. The first remote will serve all files in the repository. The second
  277. remote will only serve files from the ``salt`` directory (and its
  278. subdirectories). The third remote will only server files from the
  279. ``other/salt`` directory (and its subdirectories), while the fourth remote
  280. will only serve files from the ``salt/states`` directory (and its
  281. subdirectories).
  282. 3. The third remote will only serve files from branches, and not from tags or
  283. SHAs.
  284. 4. The fourth remote will only have two saltenvs available: ``base`` (pointed
  285. at ``develop``), and ``foo`` (pointed at ``foo``).
  286. 5. The first and fourth remotes will have files located under the root of the
  287. Salt fileserver namespace (``salt://``). The files from the second remote
  288. will be located under ``salt://bar``, while the files from the third remote
  289. will be located under ``salt://other/bar``.
  290. 6. The second and third remotes reference the same repository and unique names
  291. need to be declared for duplicate gitfs remotes.
  292. 7. The fourth remote overrides the default behavior of :ref:`not authenticating
  293. to insecure (non-HTTPS) remotes <gitfs-insecure-auth>`.
  294. 8. Because ``all_saltenvs`` is configured for the fifth remote, files from the
  295. branch/tag ``master`` will appear in every fileserver environment.
  296. .. note::
  297. The use of ``http://`` (instead of ``https://``) is permitted here
  298. *only* because authentication is not being used. Otherwise, the
  299. ``insecure_auth`` parameter must be used (as in the fourth remote) to
  300. force Salt to authenticate to an ``http://`` remote.
  301. 9. The first remote will wait 120 seconds between updates instead of 60.
  302. .. _gitfs-per-saltenv-config:
  303. Per-Saltenv Configuration Parameters
  304. ====================================
  305. .. versionadded:: 2016.11.0
  306. For more granular control, Salt allows the following three things to be
  307. overridden for individual saltenvs within a given repo:
  308. - The :ref:`mountpoint <gitfs-walkthrough-mountpoint>`
  309. - The :ref:`root <gitfs-walkthrough-root>`
  310. - The branch/tag to be used for a given saltenv
  311. Here is an example:
  312. .. code-block:: yaml
  313. gitfs_root: salt
  314. gitfs_saltenv:
  315. - dev:
  316. - mountpoint: salt://gitfs-dev
  317. - ref: develop
  318. gitfs_remotes:
  319. - https://foo.com/bar.git:
  320. - saltenv:
  321. - staging:
  322. - ref: qa
  323. - mountpoint: salt://bar-staging
  324. - dev:
  325. - ref: development
  326. - https://foo.com/baz.git:
  327. - saltenv:
  328. - staging:
  329. - mountpoint: salt://baz-staging
  330. Given the above configuration, the following is true:
  331. 1. For all gitfs remotes, files for the ``dev`` saltenv will be located under
  332. ``salt://gitfs-dev``.
  333. 2. For the ``dev`` saltenv, files from the first remote will be sourced from
  334. the ``development`` branch, while files from the second remote will be
  335. sourced from the ``develop`` branch.
  336. 3. For the ``staging`` saltenv, files from the first remote will be located
  337. under ``salt://bar-staging``, while files from the second remote will be
  338. located under ``salt://baz-staging``.
  339. 4. For all gitfs remotes, and in all saltenvs, files will be served from the
  340. ``salt`` directory (and its subdirectories).
  341. .. _gitfs-custom-refspecs:
  342. Custom Refspecs
  343. ===============
  344. .. versionadded:: 2017.7.0
  345. GitFS will by default fetch remote branches and tags. However, sometimes it can
  346. be useful to fetch custom refs (such as those created for `GitHub pull
  347. requests`__). To change the refspecs GitFS fetches, use the
  348. :conf_master:`gitfs_refspecs` config option:
  349. .. __: https://help.github.com/articles/checking-out-pull-requests-locally/
  350. .. code-block:: yaml
  351. gitfs_refspecs:
  352. - '+refs/heads/*:refs/remotes/origin/*'
  353. - '+refs/tags/*:refs/tags/*'
  354. - '+refs/pull/*/head:refs/remotes/origin/pr/*'
  355. - '+refs/pull/*/merge:refs/remotes/origin/merge/*'
  356. In the above example, in addition to fetching remote branches and tags,
  357. GitHub's custom refs for pull requests and merged pull requests will also be
  358. fetched. These special ``head`` refs represent the head of the branch which is
  359. requesting to be merged, and the ``merge`` refs represent the result of the
  360. base branch after the merge.
  361. .. important::
  362. When using custom refspecs, the destination of the fetched refs *must* be
  363. under ``refs/remotes/origin/``, preferably in a subdirectory like in the
  364. example above. These custom refspecs will map as environment names using
  365. their relative path underneath ``refs/remotes/origin/``. For example,
  366. assuming the configuration above, the head branch for pull request 12345
  367. would map to fileserver environment ``pr/12345`` (slash included).
  368. Refspecs can be configured on a :ref:`per-remote basis
  369. <gitfs-per-remote-config>`. For example, the below configuration would only
  370. alter the default refspecs for the *second* GitFS remote. The first remote
  371. would only fetch branches and tags (the default).
  372. .. code-block:: yaml
  373. gitfs_remotes:
  374. - https://domain.tld/foo.git
  375. - https://domain.tld/bar.git:
  376. - refspecs:
  377. - '+refs/heads/*:refs/remotes/origin/*'
  378. - '+refs/tags/*:refs/tags/*'
  379. - '+refs/pull/*/head:refs/remotes/origin/pr/*'
  380. - '+refs/pull/*/merge:refs/remotes/origin/merge/*'
  381. .. _gitfs-global-remotes:
  382. Global Remotes
  383. ==============
  384. .. versionadded:: 2018.3.0 for all_saltenvs, neon for fallback
  385. The ``all_saltenvs`` per-remote configuration parameter overrides the logic
  386. Salt uses to map branches/tags to fileserver environments (i.e. saltenvs). This
  387. allows a single branch/tag to appear in *all* GitFS saltenvs.
  388. .. note::
  389. ``all_saltenvs`` only works *within* GitFS. That is, files in a branch
  390. configured using ``all_saltenvs`` will *not* show up in a fileserver
  391. environment defined via some other fileserver backend (e.g.
  392. :conf_master:`file_roots`).
  393. The ``fallback`` global or per-remote configuration can also be used.
  394. Those are very useful in particular when working with :ref:`salt formulas
  395. <conventions-formula>`. Prior to the addition of this feature, it was necessary
  396. to push a branch/tag to the remote repo for each saltenv in which that formula
  397. was to be used. If the formula needed to be updated, this update would need to
  398. be reflected in all of the other branches/tags. This is both inconvenient and
  399. not scalable.
  400. With ``all_saltenvs``, it is now possible to define your formula once, in a
  401. single branch.
  402. .. code-block:: yaml
  403. gitfs_remotes:
  404. - http://foo.com/quux.git:
  405. - all_saltenvs: anything
  406. If you want to also test working branches of the formula repository, use
  407. ``fallback``:
  408. .. code-block:: yaml
  409. gitfs_remotes:
  410. - http://foo.com/quux.git:
  411. - fallback: anything
  412. .. _gitfs-update-intervals:
  413. Update Intervals
  414. ================
  415. Prior to the 2018.3.0 release, GitFS would update its fileserver backends as part
  416. of a dedicated "maintenance" process, in which various routine maintenance
  417. tasks were performed. This tied the update interval to the
  418. :conf_master:`loop_interval` config option, and also forced all fileservers to
  419. update at the same interval.
  420. Now it is possible to make GitFS update at its own interval, using
  421. :conf_master:`gitfs_update_interval`:
  422. .. code-block:: yaml
  423. gitfs_update_interval: 180
  424. gitfs_remotes:
  425. - https://foo.com/foo.git
  426. - https://foo.com/bar.git:
  427. - update_interval: 120
  428. Using the above configuration, the first remote would update every three
  429. minutes, while the second remote would update every two minutes.
  430. Configuration Order of Precedence
  431. =================================
  432. The order of precedence for GitFS configuration is as follows (each level
  433. overrides all levels below it):
  434. 1. Per-saltenv configuration (defined under a per-remote ``saltenv``
  435. param)
  436. .. code-block:: yaml
  437. gitfs_remotes:
  438. - https://foo.com/bar.git:
  439. - saltenv:
  440. - dev:
  441. - mountpoint: salt://bar
  442. 2. Global per-saltenv configuration (defined in :conf_master:`gitfs_saltenv`)
  443. .. code-block:: yaml
  444. gitfs_saltenv:
  445. - dev:
  446. - mountpoint: salt://bar
  447. 3. Per-remote configuration parameter
  448. .. code-block:: yaml
  449. gitfs_remotes:
  450. - https://foo.com/bar.git:
  451. - mountpoint: salt://bar
  452. 4. Global configuration parameter
  453. .. code-block:: yaml
  454. gitfs_mountpoint: salt://bar
  455. .. note::
  456. The one exception to the above is when :ref:`all_saltenvs
  457. <gitfs-global-remotes>` is used. This value overrides all logic for mapping
  458. branches/tags to fileserver environments. So, even if
  459. :conf_master:`gitfs_saltenv` is used to globally override the mapping for a
  460. given saltenv, :ref:`all_saltenvs <gitfs-global-remotes>` would take
  461. precedence for any remote which uses it.
  462. It's important to note however that any ``root`` and ``mountpoint`` values
  463. configured in :conf_master:`gitfs_saltenv` (or :ref:`per-saltenv
  464. configuration <gitfs-per-saltenv-config>`) would be unaffected by this.
  465. .. _gitfs-walkthrough-root:
  466. Serving from a Subdirectory
  467. ===========================
  468. The :conf_master:`gitfs_root` parameter allows files to be served from a
  469. subdirectory within the repository. This allows for only part of a repository
  470. to be exposed to the Salt fileserver.
  471. Assume the below layout:
  472. .. code-block:: text
  473. .gitignore
  474. README.txt
  475. foo/
  476. foo/bar/
  477. foo/bar/one.txt
  478. foo/bar/two.txt
  479. foo/bar/three.txt
  480. foo/baz/
  481. foo/baz/top.sls
  482. foo/baz/edit/vim.sls
  483. foo/baz/edit/vimrc
  484. foo/baz/nginx/init.sls
  485. The below configuration would serve only the files under ``foo/baz``, ignoring
  486. the other files in the repository:
  487. .. code-block:: yaml
  488. gitfs_remotes:
  489. - git://mydomain.com/stuff.git
  490. gitfs_root: foo/baz
  491. The root can also be configured on a :ref:`per-remote basis
  492. <gitfs-per-remote-config>`.
  493. .. _gitfs-walkthrough-mountpoint:
  494. Mountpoints
  495. ===========
  496. .. versionadded:: 2014.7.0
  497. The :conf_master:`gitfs_mountpoint` parameter will prepend the specified path
  498. to the files served from gitfs. This allows an existing repository to be used,
  499. rather than needing to reorganize a repository or design it around the layout
  500. of the Salt fileserver.
  501. Before the addition of this feature, if a file being served up via gitfs was
  502. deeply nested within the root directory (for example,
  503. ``salt://webapps/foo/files/foo.conf``, it would be necessary to ensure that the
  504. file was properly located in the remote repository, and that all of the
  505. parent directories were present (for example, the directories
  506. ``webapps/foo/files/`` would need to exist at the root of the repository).
  507. The below example would allow for a file ``foo.conf`` at the root of the
  508. repository to be served up from the Salt fileserver path
  509. ``salt://webapps/foo/files/foo.conf``.
  510. .. code-block:: yaml
  511. gitfs_remotes:
  512. - https://mydomain.com/stuff.git
  513. gitfs_mountpoint: salt://webapps/foo/files
  514. Mountpoints can also be configured on a :ref:`per-remote basis
  515. <gitfs-per-remote-config>`.
  516. Using gitfs in Masterless Mode
  517. ==============================
  518. Since 2014.7.0, gitfs can be used in masterless mode. To do so, simply add the
  519. gitfs configuration parameters (and set :conf_master:`fileserver_backend`) in
  520. the _minion_ config file instead of the master config file.
  521. Using gitfs Alongside Other Backends
  522. ====================================
  523. Sometimes it may make sense to use multiple backends; for instance, if ``sls``
  524. files are stored in git but larger files are stored directly on the master.
  525. The cascading lookup logic used for multiple remotes is also used with multiple
  526. backends. If the :conf_master:`fileserver_backend` option contains multiple
  527. backends:
  528. .. code-block:: yaml
  529. fileserver_backend:
  530. - roots
  531. - git
  532. Then the ``roots`` backend (the default backend of files in ``/srv/salt``) will
  533. be searched first for the requested file; then, if it is not found on the
  534. master, each configured git remote will be searched.
  535. .. note::
  536. This can be used together with `file_roots` accepting `__env__` as a catch-all
  537. environment, since 2018.3.5 and 2019.2.1:
  538. .. code-block:: yaml
  539. file_roots:
  540. base:
  541. - /srv/salt
  542. __env__:
  543. - /srv/salt
  544. Branches, Environments, and Top Files
  545. =====================================
  546. When using the GitFS backend, branches, and tags will be mapped to environments
  547. using the branch/tag name as an identifier.
  548. There is one exception to this rule: the ``master`` branch is implicitly mapped
  549. to the ``base`` environment.
  550. So, for a typical ``base``, ``qa``, ``dev`` setup, the following branches could
  551. be used:
  552. .. code-block:: yaml
  553. master
  554. qa
  555. dev
  556. ``top.sls`` files from different branches will be merged into one at runtime.
  557. Since this can lead to overly complex configurations, the recommended setup is
  558. to have a separate repository, containing only the ``top.sls`` file with just
  559. one single ``master`` branch.
  560. To map a branch other than ``master`` as the ``base`` environment, use the
  561. :conf_master:`gitfs_base` parameter.
  562. .. code-block:: yaml
  563. gitfs_base: salt-base
  564. The base can also be configured on a :ref:`per-remote basis
  565. <gitfs-per-remote-config>`.
  566. .. _gitfs-whitelist-blacklist:
  567. Environment Whitelist/Blacklist
  568. ===============================
  569. .. versionadded:: 2014.7.0
  570. The :conf_master:`gitfs_saltenv_whitelist` and
  571. :conf_master:`gitfs_saltenv_blacklist` parameters allow for greater control
  572. over which branches/tags are exposed as fileserver environments. Exact matches,
  573. globs, and regular expressions are supported, and are evaluated in that order.
  574. If using a regular expression, ``^`` and ``$`` must be omitted, and the
  575. expression must match the entire branch/tag.
  576. .. code-block:: yaml
  577. gitfs_saltenv_whitelist:
  578. - base
  579. - v1.*
  580. - 'mybranch\d+'
  581. .. note::
  582. ``v1.*``, in this example, will match as both a glob and a regular
  583. expression (though it will have been matched as a glob, since globs are
  584. evaluated before regular expressions).
  585. The behavior of the blacklist/whitelist will differ depending on which
  586. combination of the two options is used:
  587. * If only :conf_master:`gitfs_saltenv_whitelist` is used, then **only**
  588. branches/tags which match the whitelist will be available as environments
  589. * If only :conf_master:`gitfs_saltenv_blacklist` is used, then the
  590. branches/tags which match the blacklist will **not** be available as
  591. environments
  592. * If both are used, then the branches/tags which match the whitelist, but do
  593. **not** match the blacklist, will be available as environments.
  594. .. _gitfs-authentication:
  595. Authentication
  596. ==============
  597. pygit2
  598. ------
  599. .. versionadded:: 2014.7.0
  600. Both HTTPS and SSH authentication are supported as of version 0.20.3, which is
  601. the earliest version of pygit2_ supported by Salt for gitfs.
  602. .. note::
  603. The examples below make use of per-remote configuration parameters, a
  604. feature new to Salt 2014.7.0. More information on these can be found
  605. :ref:`here <gitfs-per-remote-config>`.
  606. HTTPS
  607. ~~~~~
  608. For HTTPS repositories which require authentication, the username and password
  609. can be provided like so:
  610. .. code-block:: yaml
  611. gitfs_remotes:
  612. - https://domain.tld/myrepo.git:
  613. - user: git
  614. - password: mypassword
  615. .. _gitfs-insecure-auth:
  616. If the repository is served over HTTP instead of HTTPS, then Salt will by
  617. default refuse to authenticate to it. This behavior can be overridden by adding
  618. an ``insecure_auth`` parameter:
  619. .. code-block:: yaml
  620. gitfs_remotes:
  621. - http://domain.tld/insecure_repo.git:
  622. - user: git
  623. - password: mypassword
  624. - insecure_auth: True
  625. .. _pygit2-authentication-ssh:
  626. SSH
  627. ~~~
  628. SSH repositories can be configured using the ``ssh://`` protocol designation,
  629. or using scp-like syntax. So, the following two configurations are equivalent:
  630. * ``ssh://git@github.com/user/repo.git``
  631. * ``git@github.com:user/repo.git``
  632. Both :conf_master:`gitfs_pubkey` and :conf_master:`gitfs_privkey` (or their
  633. :ref:`per-remote counterparts <gitfs-per-remote-config>`) must be configured in
  634. order to authenticate to SSH-based repos. If the private key is protected with
  635. a passphrase, it can be configured using :conf_master:`gitfs_passphrase` (or
  636. simply ``passphrase`` if being configured :ref:`per-remote
  637. <gitfs-per-remote-config>`). For example:
  638. .. code-block:: yaml
  639. gitfs_remotes:
  640. - git@github.com:user/repo.git:
  641. - pubkey: /root/.ssh/id_rsa.pub
  642. - privkey: /root/.ssh/id_rsa
  643. - passphrase: myawesomepassphrase
  644. Finally, the SSH host key must be :ref:`added to the known_hosts file
  645. <gitfs-ssh-fingerprint>`.
  646. .. note::
  647. There is a known issue with public-key SSH authentication to Microsoft
  648. Visual Studio (VSTS) with pygit2. This is due to a bug or lack of support
  649. for VSTS in older libssh2 releases. Known working releases include libssh2
  650. 1.7.0 and later, and known incompatible releases include 1.5.0 and older.
  651. At the time of this writing, 1.6.0 has not been tested.
  652. Since upgrading libssh2 would require rebuilding many other packages (curl,
  653. etc.), followed by a rebuild of libgit2 and a reinstall of pygit2, an
  654. easier workaround for systems with older libssh2 is to use GitPython with a
  655. passphraseless key for authentication.
  656. GitPython
  657. ---------
  658. HTTPS
  659. ~~~~~
  660. For HTTPS repositories which require authentication, the username and password
  661. can be configured in one of two ways. The first way is to include them in the
  662. URL using the format ``https://<user>:<password>@<url>``, like so:
  663. .. code-block:: yaml
  664. gitfs_remotes:
  665. - https://git:mypassword@domain.tld/myrepo.git
  666. The other way would be to configure the authentication in ``~/.netrc``:
  667. .. code-block:: text
  668. machine domain.tld
  669. login git
  670. password mypassword
  671. If the repository is served over HTTP instead of HTTPS, then Salt will by
  672. default refuse to authenticate to it. This behavior can be overridden by adding
  673. an ``insecure_auth`` parameter:
  674. .. code-block:: yaml
  675. gitfs_remotes:
  676. - http://git:mypassword@domain.tld/insecure_repo.git:
  677. - insecure_auth: True
  678. SSH
  679. ~~~
  680. Only passphrase-less SSH public key authentication is supported using
  681. GitPython. **The auth parameters (pubkey, privkey, etc.) shown in the pygit2
  682. authentication examples above do not work with GitPython.**
  683. .. code-block:: yaml
  684. gitfs_remotes:
  685. - ssh://git@github.com/example/salt-states.git
  686. Since GitPython_ wraps the git CLI, the private key must be located in
  687. ``~/.ssh/id_rsa`` for the user under which the Master is running, and should
  688. have permissions of ``0600``. Also, in the absence of a user in the repo URL,
  689. GitPython_ will (just as SSH does) attempt to login as the current user (in
  690. other words, the user under which the Master is running, usually ``root``).
  691. If a key needs to be used, then ``~/.ssh/config`` can be configured to use
  692. the desired key. Information on how to do this can be found by viewing the
  693. manpage for ``ssh_config``. Here's an example entry which can be added to the
  694. ``~/.ssh/config`` to use an alternate key for gitfs:
  695. .. code-block:: text
  696. Host github.com
  697. IdentityFile /root/.ssh/id_rsa_gitfs
  698. The ``Host`` parameter should be a hostname (or hostname glob) that matches the
  699. domain name of the git repository.
  700. It is also necessary to :ref:`add the SSH host key to the known_hosts file
  701. <gitfs-ssh-fingerprint>`. The exception to this would be if strict host key
  702. checking is disabled, which can be done by adding ``StrictHostKeyChecking no``
  703. to the entry in ``~/.ssh/config``
  704. .. code-block:: text
  705. Host github.com
  706. IdentityFile /root/.ssh/id_rsa_gitfs
  707. StrictHostKeyChecking no
  708. However, this is generally regarded as insecure, and is not recommended.
  709. .. _gitfs-ssh-fingerprint:
  710. Adding the SSH Host Key to the known_hosts File
  711. -----------------------------------------------
  712. To use SSH authentication, it is necessary to have the remote repository's SSH
  713. host key in the ``~/.ssh/known_hosts`` file. If the master is also a minion,
  714. this can be done using the :mod:`ssh.set_known_host
  715. <salt.modules.ssh.set_known_host>` function:
  716. .. code-block:: bash
  717. # salt mymaster ssh.set_known_host user=root hostname=github.com
  718. mymaster:
  719. ----------
  720. new:
  721. ----------
  722. enc:
  723. ssh-rsa
  724. fingerprint:
  725. 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48
  726. hostname:
  727. |1|OiefWWqOD4kwO3BhoIGa0loR5AA=|BIXVtmcTbPER+68HvXmceodDcfI=
  728. key:
  729. AAAAB3NzaC1yc2EAAAABIwAAAQEAq2A7hRGmdnm9tUDbO9IDSwBK6TbQa+PXYPCPy6rbTrTtw7PHkccKrpp0yVhp5HdEIcKr6pLlVDBfOLX9QUsyCOV0wzfjIJNlGEYsdlLJizHhbn2mUjvSAHQqZETYP81eFzLQNnPHt4EVVUh7VfDESU84KezmD5QlWpXLmvU31/yMf+Se8xhHTvKSCZIFImWwoG6mbUoWf9nzpIoaSjB+weqqUUmpaaasXVal72J+UX2B+2RPW3RcT0eOzQgqlJL3RKrTJvdsjE3JEAvGq3lGHSZXy28G3skua2SmVi/w4yCE6gbODqnTWlg7+wC604ydGXA8VJiS5ap43JXiUFFAaQ==
  730. old:
  731. None
  732. status:
  733. updated
  734. If not, then the easiest way to add the key is to su to the user (usually
  735. ``root``) under which the salt-master runs and attempt to login to the
  736. server via SSH:
  737. .. code-block:: text
  738. $ su -
  739. Password:
  740. # ssh github.com
  741. The authenticity of host 'github.com (192.30.252.128)' can't be established.
  742. RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.
  743. Are you sure you want to continue connecting (yes/no)? yes
  744. Warning: Permanently added 'github.com,192.30.252.128' (RSA) to the list of known hosts.
  745. Permission denied (publickey).
  746. It doesn't matter if the login was successful, as answering ``yes`` will write
  747. the fingerprint to the known_hosts file.
  748. Verifying the Fingerprint
  749. ~~~~~~~~~~~~~~~~~~~~~~~~~
  750. To verify that the correct fingerprint was added, it is a good idea to look it
  751. up. One way to do this is to use ``nmap``:
  752. .. code-block:: bash
  753. $ nmap -p 22 github.com --script ssh-hostkey
  754. Starting Nmap 5.51 ( http://nmap.org ) at 2014-08-18 17:47 CDT
  755. Nmap scan report for github.com (192.30.252.129)
  756. Host is up (0.17s latency).
  757. Not shown: 996 filtered ports
  758. PORT STATE SERVICE
  759. 22/tcp open ssh
  760. | ssh-hostkey: 1024 ad:1c:08:a4:40:e3:6f:9c:f5:66:26:5d:4b:33:5d:8c (DSA)
  761. |_2048 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48 (RSA)
  762. 80/tcp open http
  763. 443/tcp open https
  764. 9418/tcp open git
  765. Nmap done: 1 IP address (1 host up) scanned in 28.78 seconds
  766. Another way is to check one's own ``known_hosts`` file, using this one-liner:
  767. .. code-block:: bash
  768. $ ssh-keygen -l -f /dev/stdin <<<`ssh-keyscan github.com 2>/dev/null` | awk '{print $2}'
  769. 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48
  770. .. warning::
  771. AWS tracks usage of nmap and may flag it as abuse. On AWS hosts, the
  772. ``ssh-keygen`` method is recommended for host key verification.
  773. .. note::
  774. As of `OpenSSH 6.8`_ the SSH fingerprint is now shown as a base64-encoded
  775. SHA256 checksum of the host key. So, instead of the fingerprint looking
  776. like ``16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48``, it would look
  777. like ``SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8``.
  778. .. _`OpenSSH 6.8`: http://www.openssh.com/txt/release-6.8
  779. Refreshing gitfs Upon Push
  780. ==========================
  781. By default, Salt updates the remote fileserver backends every 60 seconds.
  782. However, if it is desirable to refresh quicker than that, the :ref:`Reactor
  783. System <reactor>` can be used to signal the master to update the fileserver on
  784. each push, provided that the git server is also a Salt minion. There are three
  785. steps to this process:
  786. 1. On the master, create a file **/srv/reactor/update_fileserver.sls**, with
  787. the following contents:
  788. .. code-block:: yaml
  789. update_fileserver:
  790. runner.fileserver.update
  791. 2. Add the following reactor configuration to the master config file:
  792. .. code-block:: yaml
  793. reactor:
  794. - 'salt/fileserver/gitfs/update':
  795. - /srv/reactor/update_fileserver.sls
  796. 3. On the git server, add a `post-receive hook`_
  797. a. If the user executing `git push` is the same as the minion user, use the following hook:
  798. .. code-block:: bash
  799. #!/usr/bin/env sh
  800. salt-call event.fire_master update salt/fileserver/gitfs/update
  801. b. To enable other git users to run the hook after a `push`, use sudo in the hook script:
  802. .. code-block:: bash
  803. #!/usr/bin/env sh
  804. sudo -u root salt-call event.fire_master update salt/fileserver/gitfs/update
  805. 4. If using sudo in the git hook (above), the policy must be changed to permit
  806. all users to fire the event. Add the following policy to the sudoers file
  807. on the git server.
  808. .. code-block:: bash
  809. Cmnd_Alias SALT_GIT_HOOK = /bin/salt-call event.fire_master update salt/fileserver/gitfs/update
  810. Defaults!SALT_GIT_HOOK !requiretty
  811. ALL ALL=(root) NOPASSWD: SALT_GIT_HOOK
  812. The ``update`` argument right after :mod:`event.fire_master
  813. <salt.modules.event.fire_master>` in this example can really be anything, as it
  814. represents the data being passed in the event, and the passed data is ignored
  815. by this reactor.
  816. Similarly, the tag name ``salt/fileserver/gitfs/update`` can be replaced by
  817. anything, so long as the usage is consistent.
  818. The ``root`` user name in the hook script and sudo policy should be changed to
  819. match the user under which the minion is running.
  820. .. _`post-receive hook`: http://www.git-scm.com/book/en/Customizing-Git-Git-Hooks#Server-Side-Hooks
  821. .. _git-as-ext_pillar:
  822. Using Git as an External Pillar Source
  823. ======================================
  824. The git external pillar (a.k.a. git_pillar) has been rewritten for the 2015.8.0
  825. release. This rewrite brings with it pygit2_ support (allowing for access to
  826. authenticated repositories), as well as more granular support for per-remote
  827. configuration. This configuration schema is detailed :ref:`here
  828. <git-pillar-configuration>`.
  829. .. _faq-gitfs-bug:
  830. Why aren't my custom modules/states/etc. syncing to my Minions?
  831. ===============================================================
  832. In versions 0.16.3 and older, when using the :mod:`git fileserver backend
  833. <salt.fileserver.gitfs>`, certain versions of GitPython may generate errors
  834. when fetching, which Salt fails to catch. While not fatal to the fetch process,
  835. these interrupt the fileserver update that takes place before custom types are
  836. synced, and thus interrupt the sync itself. Try disabling the git fileserver
  837. backend in the master config, restarting the master, and attempting the sync
  838. again.
  839. This issue is worked around in Salt 0.16.4 and newer.