spm_formula.rst 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. .. _spm-formula:
  2. ============
  3. FORMULA File
  4. ============
  5. In addition to the formula itself, a ``FORMULA`` file must exist which
  6. describes the package. An example of this file is:
  7. .. code-block:: yaml
  8. name: apache
  9. os: RedHat, Debian, Ubuntu, SUSE, FreeBSD
  10. os_family: RedHat, Debian, Suse, FreeBSD
  11. version: 201506
  12. release: 2
  13. summary: Formula for installing Apache
  14. description: Formula for installing Apache
  15. Required Fields
  16. ```````````````
  17. This file must contain at least the following fields:
  18. name
  19. ~~~~
  20. The name of the package, as it will appear in the package filename, in the
  21. repository metadata, and the package database. Even if the source formula has
  22. ``-formula`` in its name, this name should probably not include that. For
  23. instance, when packaging the ``apache-formula``, the name should be set to
  24. ``apache``.
  25. os
  26. ~~
  27. The value of the ``os`` grain that this formula supports. This is used to
  28. help users know which operating systems can support this package.
  29. os_family
  30. ~~~~~~~~~
  31. The value of the ``os_family`` grain that this formula supports. This is used to
  32. help users know which operating system families can support this package.
  33. version
  34. ~~~~~~~
  35. The version of the package. While it is up to the organization that manages this
  36. package, it is suggested that this version is specified in a ``YYYYMM`` format.
  37. For instance, if this version was released in June 2015, the package version
  38. should be ``201506``. If multiple releases are made in a month, the ``release``
  39. field should be used.
  40. minimum_version
  41. ~~~~~~~~~~~~~~~
  42. Minimum recommended version of Salt to use this formula. Not currently enforced.
  43. release
  44. ~~~~~~~
  45. This field refers primarily to a release of a version, but also to multiple
  46. versions within a month. In general, if a version has been made public, and
  47. immediate updates need to be made to it, this field should also be updated.
  48. summary
  49. ~~~~~~~
  50. A one-line description of the package.
  51. description
  52. ~~~~~~~~~~~
  53. A more detailed description of the package which can contain more than one line.
  54. Optional Fields
  55. ```````````````
  56. The following fields may also be present.
  57. top_level_dir
  58. ~~~~~~~~~~~~~
  59. This field is optional, but highly recommended. If it is not specified, the
  60. package name will be used.
  61. Formula repositories typically do not store ``.sls`` files in the root of the
  62. repository; instead they are stored in a subdirectory. For instance, an
  63. ``apache-formula`` repository would contain a directory called ``apache``, which
  64. would contain an ``init.sls``, plus a number of other related files. In this
  65. instance, the ``top_level_dir`` should be set to ``apache``.
  66. Files outside the ``top_level_dir``, such as ``README.rst``, ``FORMULA``, and
  67. ``LICENSE`` will not be installed. The exceptions to this rule are files that
  68. are already treated specially, such as ``pillar.example`` and ``_modules/``.
  69. dependencies
  70. ~~~~~~~~~~~~
  71. A comma-separated list of packages that must be installed along with this
  72. package. When this package is installed, SPM will attempt to discover and
  73. install these packages as well. If it is unable to, then it will refuse to
  74. install this package.
  75. This is useful for creating packages which tie together other packages. For
  76. instance, a package called wordpress-mariadb-apache would depend upon
  77. wordpress, mariadb, and apache.
  78. optional
  79. ~~~~~~~~
  80. A comma-separated list of packages which are related to this package, but are
  81. neither required nor necessarily recommended. This list is displayed in an
  82. informational message when the package is installed to SPM.
  83. recommended
  84. ~~~~~~~~~~~
  85. A comma-separated list of optional packages that are recommended to be
  86. installed with the package. This list is displayed in an informational message
  87. when the package is installed to SPM.
  88. files
  89. ~~~~~
  90. A files section can be added, to specify a list of files to add to the SPM.
  91. Such a section might look like:
  92. .. code-block:: yaml
  93. files:
  94. - _pillar
  95. - FORMULA
  96. - _runners
  97. - d|mymodule/index.rst
  98. - r|README.rst
  99. When ``files`` are specified, then only those files will be added to the SPM,
  100. regardless of what other files exist in the directory. They will also be added
  101. in the order specified, which is useful if you have a need to lay down files in
  102. a specific order.
  103. As can be seen in the example above, you may also tag files as being a specific
  104. type. This is done by pre-pending a filename with its type, followed by a pipe
  105. (``|``) character. The above example contains a document file and a readme. The
  106. available file types are:
  107. * ``c``: config file
  108. * ``d``: documentation file
  109. * ``g``: ghost file (i.e. the file contents are not included in the package payload)
  110. * ``l``: license file
  111. * ``r``: readme file
  112. * ``s``: SLS file
  113. * ``m``: Salt module
  114. The first 5 of these types (``c``, ``d``, ``g``, ``l``, ``r``) will be placed in
  115. ``/usr/share/salt/spm/`` by default. This can be changed by setting an
  116. ``spm_share_dir`` value in your ``/etc/salt/spm`` configuration file.
  117. The last two types (``s`` and ``m``) are currently ignored, but they are
  118. reserved for future use.
  119. Pre and Post States
  120. -------------------
  121. It is possible to run Salt states before and after installing a package by
  122. using pre and post states. The following sections may be declared in a
  123. ``FORMULA``:
  124. * ``pre_local_state``
  125. * ``pre_tgt_state``
  126. * ``post_local_state``
  127. * ``post_tgt_state``
  128. Sections with ``pre`` in their name are evaluated before a package is installed
  129. and sections with ``post`` are evaluated after a package is installed. ``local``
  130. states are evaluated before ``tgt`` states.
  131. Each of these sections needs to be evaluated as text, rather than as YAML.
  132. Consider the following block:
  133. .. code-block:: yaml
  134. pre_local_state: >
  135. echo test > /tmp/spmtest:
  136. cmd:
  137. - run
  138. Note that this declaration uses ``>`` after ``pre_local_state``. This is a YAML
  139. marker that marks the next multi-line block as text, including newlines. It is
  140. important to use this marker whenever declaring ``pre`` or ``post`` states, so
  141. that the text following it can be evaluated properly.
  142. local States
  143. ~~~~~~~~~~~~
  144. ``local`` states are evaluated locally; this is analogous to issuing a state
  145. run using a ``salt-call --local`` command. These commands will be issued on the
  146. local machine running the ``spm`` command, whether that machine is a master or
  147. a minion.
  148. ``local`` states do not require any special arguments, but they must still use
  149. the ``>`` marker to denote that the state is evaluated as text, not a data
  150. structure.
  151. .. code-block:: yaml
  152. pre_local_state: >
  153. echo test > /tmp/spmtest:
  154. cmd:
  155. - run
  156. tgt States
  157. ~~~~~~~~~~
  158. ``tgt`` states are issued against a remote target. This is analogous to issuing
  159. a state using the ``salt`` command. As such it requires that the machine that
  160. the ``spm`` command is running on is a master.
  161. Because ``tgt`` states require that a target be specified, their code blocks
  162. are a little different. Consider the following state:
  163. .. code-block:: yaml
  164. pre_tgt_state:
  165. tgt: '*'
  166. data: >
  167. echo test > /tmp/spmtest:
  168. cmd:
  169. - run
  170. With ``tgt`` states, the state data is placed under a ``data`` section, inside
  171. the ``*_tgt_state`` code block. The target is of course specified as a ``tgt``
  172. and you may also optionally specify a ``tgt_type`` (the default is ``glob``).
  173. You still need to use the ``>`` marker, but this time it follows the ``data``
  174. line, rather than the ``*_tgt_state`` line.
  175. Templating States
  176. ~~~~~~~~~~~~~~~~~
  177. The reason that state data must be evaluated as text rather than a data
  178. structure is because that state data is first processed through the rendering
  179. engine, as it would be with a standard state run.
  180. This means that you can use Jinja or any other supported renderer inside of
  181. Salt. All formula variables are available to the renderer, so you can reference
  182. ``FORMULA`` data inside your state if you need to:
  183. .. code-block:: yaml
  184. pre_tgt_state:
  185. tgt: '*'
  186. data: >
  187. echo {{ name }} > /tmp/spmtest:
  188. cmd:
  189. - run
  190. You may also declare your own variables inside the ``FORMULA``. If SPM doesn't
  191. recognize them then it will ignore them, so there are no restrictions on
  192. variable names, outside of avoiding reserved words.
  193. By default the renderer is set to ``jinja|yaml``. You may change this by
  194. changing the ``renderer`` setting in the ``FORMULA`` itself.
  195. Building a Package
  196. ------------------
  197. Once a ``FORMULA`` file has been created, it is placed into the root of the
  198. formula that is to be turned into a package. The ``spm build`` command is
  199. used to turn that formula into a package:
  200. .. code-block:: bash
  201. spm build /path/to/saltstack-formulas/apache-formula
  202. The resulting file will be placed in the build directory. By default this
  203. directory is located at ``/srv/spm/``.
  204. Loader Modules
  205. ==============
  206. When an execution module is placed in ``<file_roots>/_modules/`` on the master,
  207. it will automatically be synced to minions, the next time a sync operation takes
  208. place. Other modules are also propagated this way: state modules can be placed
  209. in ``_states/``, and so on.
  210. When SPM detects a file in a package which resides in one of these directories,
  211. that directory will be placed in ``<file_roots>`` instead of in the formula
  212. directory with the rest of the files.
  213. Removing Packages
  214. =================
  215. Packages may be removed once they are installed using the ``spm remove``
  216. command.
  217. .. code-block:: bash
  218. spm remove apache
  219. If files have been modified, they will not be removed. Empty directories will
  220. also be removed.
  221. Technical Information
  222. =====================
  223. Packages are built using BZ2-compressed tarballs. By default, the package
  224. database is stored using the ``sqlite3`` driver (see Loader Modules below).
  225. Support for these are built into Python, and so no external dependencies are
  226. needed.
  227. All other files belonging to SPM use YAML, for portability and ease of use and
  228. maintainability.
  229. SPM-Specific Loader Modules
  230. ===========================
  231. SPM was designed to behave like traditional package managers, which apply files
  232. to the filesystem and store package metadata in a local database. However,
  233. because modern infrastructures often extend beyond those use cases, certain
  234. parts of SPM have been broken out into their own set of modules.
  235. Package Database
  236. ----------------
  237. By default, the package database is stored using the ``sqlite3`` module. This
  238. module was chosen because support for SQLite3 is built into Python itself.
  239. Please see the SPM Development Guide for information on creating new modules
  240. for package database management.
  241. Package Files
  242. -------------
  243. By default, package files are installed using the ``local`` module. This module
  244. applies files to the local filesystem, on the machine that the package is
  245. installed on.
  246. Please see the :ref:`SPM Development Guide <spm-development>` for information
  247. on creating new modules for package file management.
  248. Types of Packages
  249. =================
  250. SPM supports different types of formula packages. The function of each package
  251. is denoted by its name. For instance, packages which end in ``-formula`` are
  252. considered to be Salt States (the most common type of formula). Packages which
  253. end in ``-conf`` contain configuration which is to be placed in the
  254. ``/etc/salt/`` directory. Packages which do not contain one of these names are
  255. treated as if they have a ``-formula`` name.
  256. formula
  257. -------
  258. By default, most files from this type of package live in the ``/srv/spm/salt/``
  259. directory. The exception is the ``pillar.example`` file, which will be renamed
  260. to ``<package_name>.sls`` and placed in the pillar directory (``/srv/spm/pillar/``
  261. by default).
  262. reactor
  263. -------
  264. By default, files from this type of package live in the ``/srv/spm/reactor/``
  265. directory.
  266. conf
  267. ----
  268. The files in this type of package are configuration files for Salt, which
  269. normally live in the ``/etc/salt/`` directory. Configuration files for packages
  270. other than Salt can and should be handled with a Salt State (using a ``formula``
  271. type of package).