2013-08-07 19:52:32 +00:00
|
|
|
.. _tutorial-gitfs:
|
|
|
|
|
2013-03-13 17:20:36 +00:00
|
|
|
=========================
|
|
|
|
GitFS Backend Walkthrough
|
|
|
|
=========================
|
|
|
|
|
2014-04-22 10:42:10 +00:00
|
|
|
Salt can retrieve states and pillars from local and remote Git repositories
|
|
|
|
configured as GitFS remotes.
|
2013-03-13 17:20:36 +00:00
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
2013-08-12 03:17:47 +00:00
|
|
|
This walkthrough assumes basic knowledge of Salt. To get up to speed, check
|
|
|
|
out the :doc:`walkthrough </topics/tutorials/walkthrough>`.
|
2013-03-13 17:20:36 +00:00
|
|
|
|
2014-04-22 10:42:10 +00:00
|
|
|
By default, Salt state trees and pillars are served from
|
|
|
|
``/srv/salt`` and ``/srv/pillar``, as configured by the
|
|
|
|
``roots`` :conf_master:`fileserver_backend`, :conf_master:`file_roots`,
|
|
|
|
and :conf_master:`pillar_roots` configuration settings in
|
|
|
|
``/etc/salt/master`` or ``/etc/salt/minion``.
|
|
|
|
|
|
|
|
GitFS support is enabled by configuring the ``git``
|
|
|
|
:conf_master:`fileserver_backend`, :conf_master:`gitfs_remotes`,
|
|
|
|
and/or :conf_master:`ext_pillar` settings.
|
|
|
|
|
|
|
|
Git branches in GitFS remotes are mapped to Salt environments.
|
|
|
|
Merging a QA or staging branch up to a production branch
|
|
|
|
can be all that is required to make state and pillar changes available to Salt
|
|
|
|
minions.
|
2013-03-13 17:20:36 +00:00
|
|
|
|
2014-06-03 15:40:27 +00:00
|
|
|
.. _gitfs-dependencies:
|
2013-03-13 17:20:36 +00:00
|
|
|
|
2014-06-03 15:40:27 +00:00
|
|
|
Installing Python Dependencies
|
|
|
|
==============================
|
2013-03-13 17:20:36 +00:00
|
|
|
|
2014-06-03 15:40:27 +00:00
|
|
|
The GitFS backend requires GitPython_, version 0.3.0 or newer. For RHEL-based
|
|
|
|
Linux distros, a compatible versions is available in EPEL, and can be easily
|
|
|
|
installed on the master using yum:
|
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
|
|
|
# yum install GitPython
|
|
|
|
|
|
|
|
Ubuntu 14.04 LTS and Debian Wheezy (7.x) also have a compatible version packaged:
|
2014-02-24 06:55:48 +00:00
|
|
|
|
2014-06-03 15:40:27 +00:00
|
|
|
.. code-block:: bash
|
2014-02-24 06:55:48 +00:00
|
|
|
|
2014-06-03 15:40:27 +00:00
|
|
|
# apt-get install python-git
|
2014-02-24 06:55:48 +00:00
|
|
|
|
2014-06-03 15:40:27 +00:00
|
|
|
If your master is running an older version (such as Ubuntu 12.04 LTS or Debian
|
|
|
|
Squeeze), then you will need to install GitPython using either pip_ or
|
|
|
|
easy_install (it is recommended to use pip). Version 0.3.2.RC1 is now marked as
|
|
|
|
the stable release in PyPI, so it should be a simple matter of running ``pip
|
|
|
|
install GitPython`` (or ``easy_install GitPython``) as root.
|
2014-02-24 06:55:48 +00:00
|
|
|
|
|
|
|
.. _`pip`: http://www.pip-installer.org/
|
|
|
|
|
2014-06-03 15:40:27 +00:00
|
|
|
.. warning::
|
|
|
|
|
|
|
|
Keep in mind that if GitPython has been previously installed on the master
|
|
|
|
using pip (even if it was subsequently uninstalled), then it may still
|
|
|
|
exist in the build cache (typically ``/tmp/pip-build-root/GitPython``) if
|
|
|
|
the cache is not cleared after installation. The package in the build cache
|
|
|
|
will override any requirement specifiers, so if you try upgrading to
|
|
|
|
version 0.3.2.RC1 by running ``pip install 'GitPython==0.3.2.RC1'`` then it
|
|
|
|
will ignore this and simply install the version from the cache directory.
|
|
|
|
Therefore, it may be necessary to delete the GitPython directory from the
|
|
|
|
build cache in order to ensure that the specified version is installed.
|
|
|
|
|
|
|
|
|
|
|
|
Simple Configuration
|
|
|
|
====================
|
|
|
|
|
2014-04-22 10:42:10 +00:00
|
|
|
To use the gitfs backend, only two configuration changes are required on the
|
|
|
|
master:
|
|
|
|
|
|
|
|
1. Include ``git`` in the :conf_master:`fileserver_backend`
|
|
|
|
option to enable the GitFS backend:
|
2013-03-13 17:20:36 +00:00
|
|
|
|
|
|
|
.. code-block:: yaml
|
|
|
|
|
|
|
|
fileserver_backend:
|
|
|
|
- git
|
|
|
|
|
2014-04-22 10:42:10 +00:00
|
|
|
2. Specify one or more ``git://``, ``git+ssh://``, ``https://``, or ``file://``
|
|
|
|
URLs in :conf_master:`gitfs_remotes`
|
|
|
|
to configure which repositories to cache and search for requested files:
|
2013-03-13 17:20:36 +00:00
|
|
|
|
|
|
|
.. code-block:: yaml
|
|
|
|
|
|
|
|
gitfs_remotes:
|
2014-04-22 10:42:10 +00:00
|
|
|
- https://github.com/saltstack-formulas/salt-formula.git
|
2013-09-25 02:53:55 +00:00
|
|
|
|
2014-06-17 19:45:24 +00:00
|
|
|
3. Restart the master so that the git repository cache on the master is
|
|
|
|
updated, and new ``salt://`` requests will send the latest files from the
|
|
|
|
remote git repository. This step is not necessary with a standalone minion
|
|
|
|
configuration.
|
2013-03-13 17:20:36 +00:00
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
2014-04-22 10:42:10 +00:00
|
|
|
In a master/minion setup, files from a GitFS remote are cached once by
|
|
|
|
the master; so minions do not need direct access
|
|
|
|
to the git repository. In a standalone minion configuration, files from
|
|
|
|
each GitFS remote are cached by the minion.
|
2013-03-13 17:20:36 +00:00
|
|
|
|
2014-05-21 06:35:00 +00:00
|
|
|
|
2013-03-13 17:20:36 +00:00
|
|
|
Multiple Remotes
|
|
|
|
================
|
|
|
|
|
2014-04-22 10:42:10 +00:00
|
|
|
The ``gitfs_remotes`` option accepts an ordered list of git remotes to
|
|
|
|
cache and search, in listed order, for requested files.
|
2013-03-13 17:20:36 +00:00
|
|
|
|
2014-04-22 10:42:10 +00:00
|
|
|
A simple scenario illustrates this cascading lookup behavior:
|
|
|
|
|
|
|
|
If the ``gitfs_remotes`` option specifies three remotes:
|
2013-03-13 17:20:36 +00:00
|
|
|
|
|
|
|
.. code-block:: yaml
|
|
|
|
|
|
|
|
gitfs_remotes:
|
|
|
|
- git://github.com/example/first.git
|
2014-04-22 10:42:10 +00:00
|
|
|
- https://github.com/example/second.git
|
2013-05-07 17:21:26 +00:00
|
|
|
- file:///root/third
|
2013-03-13 17:20:36 +00:00
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
|
|
|
This example is purposefully contrived to illustrate the behavior of the
|
|
|
|
gitfs backend. This example should not be read as a recommended way to lay
|
|
|
|
out files and git repos.
|
|
|
|
|
2013-08-12 03:17:47 +00:00
|
|
|
The :strong:`file://` prefix denotes a git repository in a local directory.
|
|
|
|
However, it will still use the given :strong:`file://` URL as a remote,
|
|
|
|
rather than copying the git repo to the salt cache. This means that any
|
|
|
|
refs you want accessible must exist as *local* refs in the specified repo.
|
2013-05-07 17:21:26 +00:00
|
|
|
|
2014-02-08 21:36:45 +00:00
|
|
|
.. warning::
|
|
|
|
|
|
|
|
Salt versions prior to 2014.1.0 (Hydrogen) are not tolerant of changing the
|
2014-04-22 10:42:10 +00:00
|
|
|
order of remotes or modifying the URI of existing remotes. In those
|
2014-02-08 21:36:45 +00:00
|
|
|
versions, when modifying remotes it is a good idea to remove the gitfs
|
|
|
|
cache directory (``/var/cache/salt/master/gitfs``) before restarting the
|
|
|
|
salt-master service.
|
|
|
|
|
2014-04-22 10:42:10 +00:00
|
|
|
And each repository contains some files:
|
2013-03-13 17:20:36 +00:00
|
|
|
|
2013-08-12 03:17:47 +00:00
|
|
|
.. code-block:: yaml
|
|
|
|
|
|
|
|
first.git:
|
|
|
|
top.sls
|
|
|
|
edit/vim.sls
|
|
|
|
edit/vimrc
|
|
|
|
nginx/init.sls
|
2013-03-13 17:20:36 +00:00
|
|
|
|
2013-08-12 03:17:47 +00:00
|
|
|
second.git:
|
|
|
|
edit/dev_vimrc
|
|
|
|
haproxy/init.sls
|
2013-03-13 17:20:36 +00:00
|
|
|
|
2013-08-12 03:17:47 +00:00
|
|
|
third:
|
|
|
|
haproxy/haproxy.conf
|
|
|
|
edit/dev_vimrc
|
2013-05-07 17:21:26 +00:00
|
|
|
|
2014-04-22 10:42:10 +00:00
|
|
|
Salt will attempt to lookup the requested file from each GitFS remote
|
|
|
|
repository in the order in which they are defined in the configuration. The
|
|
|
|
:strong:`git://github.com/example/first.git` remote will be searched first.
|
|
|
|
If the requested file is found, then it is served and no further searching
|
|
|
|
is executed. For example:
|
|
|
|
|
|
|
|
* A request for :strong:`salt://haproxy/init.sls` will be pulled from the
|
|
|
|
:strong:`https://github.com/example/second.git` git repo.
|
|
|
|
* A request for :strong:`salt://haproxy/haproxy.conf` will be pulled from the
|
|
|
|
:strong:`file:///root/third` repo.
|
2013-03-13 17:20:36 +00:00
|
|
|
|
2014-05-21 06:35:00 +00:00
|
|
|
|
2013-08-18 01:14:57 +00:00
|
|
|
Serving from a Subdirectory
|
|
|
|
===========================
|
|
|
|
|
2014-05-21 06:35:00 +00:00
|
|
|
The :conf_master:`gitfs_root` parameter allows files to be served from a
|
|
|
|
subdirectory within the repository. This allows for only part of a repository
|
|
|
|
to be exposed to the Salt fileserver.
|
2013-08-18 01:14:57 +00:00
|
|
|
|
2014-05-21 06:35:00 +00:00
|
|
|
Assume the below layout::
|
2013-08-18 01:14:57 +00:00
|
|
|
|
2014-05-21 06:35:00 +00:00
|
|
|
.gitignore
|
|
|
|
README.txt
|
|
|
|
foo/
|
|
|
|
foo/bar/
|
|
|
|
foo/bar/one.txt
|
|
|
|
foo/bar/two.txt
|
|
|
|
foo/bar/three.txt
|
|
|
|
foo/baz/
|
|
|
|
foo/baz/top.sls
|
|
|
|
foo/baz/edit/vim.sls
|
|
|
|
foo/baz/edit/vimrc
|
|
|
|
foo/baz/nginx/init.sls
|
2013-08-18 01:14:57 +00:00
|
|
|
|
2014-05-21 06:35:00 +00:00
|
|
|
The below configuration would serve only the files from ``foo/baz``, ignoring
|
|
|
|
the other files in the repository:
|
2013-08-18 01:14:57 +00:00
|
|
|
|
|
|
|
.. code-block:: yaml
|
|
|
|
|
2014-04-22 10:42:10 +00:00
|
|
|
gitfs_remotes:
|
2014-05-21 06:35:00 +00:00
|
|
|
- git://mydomain.com/stuff.git
|
2014-04-22 10:42:10 +00:00
|
|
|
|
2014-05-21 06:35:00 +00:00
|
|
|
gitfs_root: foo/baz
|
2014-04-22 10:42:10 +00:00
|
|
|
|
2013-08-18 01:14:57 +00:00
|
|
|
|
2013-03-13 17:20:36 +00:00
|
|
|
Multiple Backends
|
|
|
|
=================
|
|
|
|
|
2014-04-22 10:42:10 +00:00
|
|
|
Sometimes it may make sense to use multiple backends; for instance, if ``sls``
|
|
|
|
files are stored in git but larger files are stored directly on the master.
|
2013-03-13 17:20:36 +00:00
|
|
|
|
2014-04-22 10:42:10 +00:00
|
|
|
The cascading lookup logic used for multiple remotes is also used with
|
|
|
|
multiple backends. If the ``fileserver_backend`` option contains
|
|
|
|
multiple backends:
|
2013-03-13 17:20:36 +00:00
|
|
|
|
|
|
|
.. code-block:: yaml
|
|
|
|
|
|
|
|
fileserver_backend:
|
|
|
|
- roots
|
|
|
|
- git
|
|
|
|
|
2013-08-12 03:17:47 +00:00
|
|
|
Then the ``roots`` backend (the default backend of files in ``/srv/salt``) will
|
2014-04-22 10:42:10 +00:00
|
|
|
be searched first for the requested file; then, if it is not found on the
|
|
|
|
master, each configured git remote will be searched.
|
2013-05-24 07:36:56 +00:00
|
|
|
|
2013-09-19 22:00:46 +00:00
|
|
|
|
2014-05-21 06:35:00 +00:00
|
|
|
Branches, Environments and Top Files
|
|
|
|
====================================
|
|
|
|
|
|
|
|
When using the ``gitfs`` backend, branches and tags will be mapped to
|
|
|
|
environments using the branch/tag name as an identifier.
|
2014-04-22 10:42:10 +00:00
|
|
|
|
2014-05-21 06:35:00 +00:00
|
|
|
There is one exception to this rule: the ``master`` branch is implicitly mapped
|
|
|
|
to the ``base`` environment.
|
2014-04-22 10:42:10 +00:00
|
|
|
|
2014-05-21 06:35:00 +00:00
|
|
|
So, for a typical ``base``, ``qa``, ``dev`` setup, the following branches could
|
|
|
|
be used:
|
2013-09-19 22:00:46 +00:00
|
|
|
|
|
|
|
.. code-block:: yaml
|
|
|
|
|
|
|
|
master
|
|
|
|
qa
|
|
|
|
dev
|
|
|
|
|
2014-05-21 06:35:00 +00:00
|
|
|
``top.sls`` files from different branches will be merged into one at runtime.
|
|
|
|
Since this can lead to overly complex configurations, the recommended setup is
|
|
|
|
to have the ``top.sls`` file only in the master branch and use
|
|
|
|
environment-specific branches for state definitions.
|
2014-04-22 10:42:10 +00:00
|
|
|
|
2014-05-21 06:35:00 +00:00
|
|
|
To map a branch other than ``master`` as the ``base`` environment, use the
|
|
|
|
:conf_master:`gitfs_base` parameter.
|
|
|
|
|
|
|
|
.. code-block:: yaml
|
|
|
|
|
|
|
|
gitfs_base: salt-base
|
2013-09-19 22:00:46 +00:00
|
|
|
|
|
|
|
|
2013-05-24 07:36:56 +00:00
|
|
|
GitFS Remotes over SSH
|
|
|
|
======================
|
|
|
|
|
2014-04-22 10:42:10 +00:00
|
|
|
To configure a ``gitfs_remotes`` repository over SSH transport, use the
|
|
|
|
``git+ssh`` URL form:
|
2013-05-24 07:36:56 +00:00
|
|
|
|
|
|
|
.. code-block:: yaml
|
2014-02-24 06:55:48 +00:00
|
|
|
|
2013-05-24 07:36:56 +00:00
|
|
|
gitfs_remotes:
|
|
|
|
- git+ssh://git@github.com/example/salt-states.git
|
2014-02-24 06:55:48 +00:00
|
|
|
|
2014-04-22 10:42:10 +00:00
|
|
|
The private key used to connect to the repository must be located in
|
|
|
|
``~/.ssh/id_rsa`` for the user running the salt-master.
|
2013-05-24 07:36:56 +00:00
|
|
|
|
2014-05-21 06:35:00 +00:00
|
|
|
|
|
|
|
Upcoming Features
|
|
|
|
=================
|
|
|
|
|
|
|
|
The upcoming feature release will bring a number of new features to gitfs:
|
|
|
|
|
|
|
|
1. **Environment Blacklist/Whitelist**
|
|
|
|
|
|
|
|
Two new configuration parameters, :conf_master:`gitfs_env_whitelist` and
|
|
|
|
:conf_master:`gitfs_env_blacklist`, allow greater control over which
|
|
|
|
branches/tags are exposed as fileserver environments.
|
|
|
|
|
|
|
|
2. **Mountpoint**
|
|
|
|
|
|
|
|
Prior to the addition of this feature, to serve a file from the URI
|
|
|
|
``salt://webapps/foo/files/foo.conf``, it was necessary to ensure that the
|
|
|
|
git repository contained the parent directories (i.e.
|
|
|
|
``webapps/foo/files/``). The :conf_master:`gitfs_mountpoint` parameter
|
|
|
|
will prepend the specified path to the files served from gitfs, allowing you
|
|
|
|
to use an existing repository rather than reorganizing it to fit your Salt
|
|
|
|
fileserver layout.
|
|
|
|
|
|
|
|
3. **Per-remote Configuration Parameters**
|
|
|
|
|
|
|
|
:conf_master:`gitfs_base`, :conf_master:`gitfs_root`, and
|
|
|
|
:conf_master:`gitfs_mountpoint` are all global parameters. That is, they
|
|
|
|
affect *all* of your gitfs remotes. The upcoming feature release allows for
|
|
|
|
these parameters to be overridden on a per-remote basis. This allows for a
|
|
|
|
tremendous amount of customization. See :conf_master:`here <gitfs_remotes>`
|
|
|
|
for an example of how use per-remote configuration.
|
|
|
|
|
2014-05-21 20:00:29 +00:00
|
|
|
4. **Support for pygit2 and dulwich**
|
|
|
|
|
|
|
|
GitPython_ is no longer being actively developed, so support has been added
|
|
|
|
for both pygit2_ and dulwich_ as a Python interface for git. Neither is yet
|
|
|
|
as full-featured as GitPython, for instance authentication via public key
|
|
|
|
is not yet supported. Salt will default to using GitPython, but the
|
|
|
|
:conf_master:`gitfs_provider` parameter can be used to specify one of the
|
|
|
|
other providers.
|
|
|
|
|
|
|
|
.. _GitPython: https://github.com/gitpython-developers/GitPython
|
|
|
|
.. _pygit2: https://github.com/libgit2/pygit2
|
|
|
|
.. _dulwich: https://www.samba.org/~jelmer/dulwich/
|
2014-05-21 06:35:00 +00:00
|
|
|
|
2013-10-28 22:09:38 +00:00
|
|
|
Using Git as an External Pillar Source
|
|
|
|
======================================
|
|
|
|
|
2014-05-21 06:35:00 +00:00
|
|
|
Git repositories can also be used to provide :doc:`Pillar
|
|
|
|
</topics/pillar/index>` data, using the :doc:`External Pillar
|
|
|
|
</topics/development/external_pillars>` system. To define a git external
|
|
|
|
pillar, add a section like the following to the salt master config file:
|
2013-10-28 22:09:38 +00:00
|
|
|
|
|
|
|
.. code-block:: yaml
|
|
|
|
|
|
|
|
ext_pillar:
|
2014-02-27 16:37:40 +00:00
|
|
|
- git: <branch> <repo> [root=<gitroot>]
|
2013-10-28 22:09:38 +00:00
|
|
|
|
2014-03-10 16:45:13 +00:00
|
|
|
.. versionchanged:: Helium
|
|
|
|
The optional ``root`` parameter will be added.
|
2013-10-28 22:09:38 +00:00
|
|
|
|
2014-04-22 10:42:10 +00:00
|
|
|
The ``<branch>`` param is the branch containing the pillar SLS tree. The
|
|
|
|
``<repo>`` param is the URI for the repository. To add the
|
2014-02-27 16:37:40 +00:00
|
|
|
``master`` branch of the specified repo as an external pillar source:
|
2013-10-28 22:09:38 +00:00
|
|
|
|
|
|
|
.. code-block:: yaml
|
|
|
|
|
|
|
|
ext_pillar:
|
|
|
|
- git: master https://domain.com/pillar.git
|
|
|
|
|
2014-04-22 10:42:10 +00:00
|
|
|
Use the ``root`` parameter to use pillars from a subdirectory of a git
|
2014-03-02 11:51:20 +00:00
|
|
|
repository:
|
2014-02-27 16:37:40 +00:00
|
|
|
|
|
|
|
.. code-block:: yaml
|
|
|
|
|
|
|
|
ext_pillar:
|
|
|
|
- git: master https://domain.com/pillar.git root=subdirectory
|
|
|
|
|
2014-04-22 10:42:10 +00:00
|
|
|
More information on the git external pillar can be found in the
|
|
|
|
:mod:`salt.pillar.get_pillar docs <salt.pillar.git_pillar>`.
|
2013-10-28 22:09:38 +00:00
|
|
|
|
|
|
|
|
2013-09-05 18:43:08 +00:00
|
|
|
.. _faq-gitfs-bug:
|
|
|
|
|
|
|
|
Why aren't my custom modules/states/etc. syncing to my Minions?
|
|
|
|
===============================================================
|
|
|
|
|
|
|
|
In versions 0.16.3 and older, when using the :doc:`git fileserver backend
|
|
|
|
</topics/tutorials/gitfs>`, certain versions of GitPython may generate errors
|
|
|
|
when fetching, which Salt fails to catch. While not fatal to the fetch process,
|
|
|
|
these interrupt the fileserver update that takes place before custom types are
|
|
|
|
synced, and thus interrupt the sync itself. Try disabling the git fileserver
|
|
|
|
backend in the master config, restarting the master, and attempting the sync
|
|
|
|
again.
|
|
|
|
|
2013-10-12 16:35:02 +00:00
|
|
|
This issue is worked around in Salt 0.16.4 and newer.
|