1
0

package_providers.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. =================
  2. Package Providers
  3. =================
  4. This page contains guidelines for writing package providers.
  5. Package Functions
  6. -----------------
  7. One of the most important features of Salt is package management. There is no
  8. shortage of package managers, so in the interest of providing a consistent
  9. experience in :mod:`pkg <salt.states.pkg>` states, there are certain functions
  10. that should be present in a package provider. Note that these are subject to
  11. change as new features are added or existing features are enhanced.
  12. list_pkgs
  13. ^^^^^^^^^
  14. This function should declare an empty dict, and then add packages to it by
  15. calling :mod:`pkg_resource.add_pkg <salt.modules.pkg_resource.add_pkg>`, like
  16. so:
  17. .. code-block:: python
  18. __salt__["pkg_resource.add_pkg"](ret, name, version)
  19. The last thing that should be done before returning is to execute
  20. :mod:`pkg_resource.sort_pkglist <salt.modules.pkg_resource.sort_pkglist>`. This
  21. function does not presently do anything to the return dict, but will be used in
  22. future versions of Salt.
  23. .. code-block:: python
  24. __salt__["pkg_resource.sort_pkglist"](ret)
  25. ``list_pkgs`` returns a dictionary of installed packages, with the keys being
  26. the package names and the values being the version installed. Example return
  27. data:
  28. .. code-block:: python
  29. {"foo": "1.2.3-4", "bar": "5.6.7-8"}
  30. latest_version
  31. ^^^^^^^^^^^^^^
  32. Accepts an arbitrary number of arguments. Each argument is a package name. The
  33. return value for a package will be an empty string if the package is not found
  34. or if the package is up-to-date. The only case in which a non-empty string is
  35. returned is if the package is available for new installation (i.e. not already
  36. installed) or if there is an upgrade available.
  37. If only one argument was passed, this function return a string, otherwise a
  38. dict of name/version pairs is returned.
  39. This function must also accept ``**kwargs``, in order to receive the
  40. ``fromrepo`` and ``repo`` keyword arguments from pkg states. Where supported,
  41. these arguments should be used to find the install/upgrade candidate in the
  42. specified repository. The ``fromrepo`` kwarg takes precedence over ``repo``, so
  43. if both of those kwargs are present, the repository specified in ``fromrepo``
  44. should be used. However, if ``repo`` is used instead of ``fromrepo``, it should
  45. still work, to preserve backwards compatibility with older versions of Salt.
  46. version
  47. ^^^^^^^
  48. Like ``latest_version``, accepts an arbitrary number of arguments and
  49. returns a string if a single package name was passed, or a dict of name/value
  50. pairs if more than one was passed. The only difference is that the return
  51. values are the currently-installed versions of whatever packages are passed. If
  52. the package is not installed, an empty string is returned for that package.
  53. upgrade_available
  54. ^^^^^^^^^^^^^^^^^
  55. Deprecated and destined to be removed. For now, should just do the following:
  56. .. code-block:: python
  57. return __salt__["pkg.latest_version"](name) != ""
  58. install
  59. ^^^^^^^
  60. The following arguments are required and should default to ``None``:
  61. #. name (for single-package pkg states)
  62. #. pkgs (for multiple-package pkg states)
  63. #. sources (for binary package file installation)
  64. The first thing that this function should do is call
  65. :mod:`pkg_resource.parse_targets <salt.modules.pkg_resource.parse_targets>`
  66. (see below). This function will convert the SLS input into a more easily parsed
  67. data structure.
  68. :mod:`pkg_resource.parse_targets <salt.modules.pkg_resource.parse_targets>` may
  69. need to be modified to support your new package provider, as it does things
  70. like parsing package metadata which cannot be done for every package management
  71. system.
  72. .. code-block:: python
  73. pkg_params, pkg_type = __salt__["pkg_resource.parse_targets"](name, pkgs, sources)
  74. Two values will be returned to the :strong:`install` function. The first of
  75. them will be a dictionary. The keys of this dictionary will be package names,
  76. though the values will differ depending on what kind of installation is being
  77. done:
  78. * If :strong:`name` was provided (and :strong:`pkgs` was not), then there will
  79. be a single key in the dictionary, and its value will be ``None``. Once the
  80. data has been returned, if the :strong:`version` keyword argument was
  81. provided, then it should replace the ``None`` value in the dictionary.
  82. * If :strong:`pkgs` was provided, then :strong:`name` is ignored, and the
  83. dictionary will contain one entry for each package in the :strong:`pkgs`
  84. list. The values in the dictionary will be ``None`` if a version was not
  85. specified for the package, and the desired version if specified. See the
  86. :strong:`Multiple Package Installation Options` section of the
  87. :mod:`pkg.installed <salt.states.pkg.installed>` state for more info.
  88. * If :strong:`sources` was provided, then :strong:`name` is ignored, and the
  89. dictionary values will be the path/URI for the package.
  90. The second return value will be a string with two possible values:
  91. ``repository`` or ``file``. The :strong:`install` function can use this value
  92. (if necessary) to build the proper command to install the targeted package(s).
  93. Both before and after the installing the target(s), you should run
  94. :strong:`list_pkgs` to obtain a list of the installed packages. You should then
  95. return the output of ``salt.utils.data.compare_dicts()``:
  96. .. code-block:: python
  97. return salt.utils.data.compare_dicts(old, new)
  98. remove
  99. ^^^^^^
  100. Removes the passed package and return a list of the packages removed.
  101. Package Repo Functions
  102. ----------------------
  103. There are some functions provided by ``pkg`` which are specific to package
  104. repositories, and not to packages themselves. When writing modules for new
  105. package managers, these functions should be made available as stated below, in
  106. order to provide compatibility with the ``pkgrepo`` state.
  107. All repo functions should accept a basedir option, which defines which
  108. directory repository configuration should be found in. The default for this
  109. is dictated by the repo manager that is being used, and rarely needs to be
  110. changed.
  111. .. code-block:: python
  112. basedir = "/etc/yum.repos.d"
  113. __salt__["pkg.list_repos"](basedir)
  114. list_repos
  115. ^^^^^^^^^^
  116. Lists the repositories that are currently configured on this system.
  117. .. code-block:: python
  118. __salt__["pkg.list_repos"]()
  119. Returns a dictionary, in the following format:
  120. .. code-block:: python
  121. {'reponame': 'config_key_1': 'config value 1',
  122. 'config_key_2': 'config value 2',
  123. 'config_key_3': ['list item 1 (when appropriate)',
  124. 'list item 2 (when appropriate)]}
  125. get_repo
  126. ^^^^^^^^
  127. Displays all local configuration for a specific repository.
  128. .. code-block:: python
  129. __salt__["pkg.get_repo"](repo="myrepo")
  130. The information is formatted in much the same way as list_repos, but is
  131. specific to only one repo.
  132. .. code-block:: python
  133. {'config_key_1': 'config value 1',
  134. 'config_key_2': 'config value 2',
  135. 'config_key_3': ['list item 1 (when appropriate)',
  136. 'list item 2 (when appropriate)]}
  137. del_repo
  138. ^^^^^^^^
  139. Removes the local configuration for a specific repository. Requires a `repo`
  140. argument, which must match the locally configured name. This function returns
  141. a string, which informs the user as to whether or not the operation was a
  142. success.
  143. .. code-block:: python
  144. __salt__["pkg.del_repo"](repo="myrepo")
  145. mod_repo
  146. ^^^^^^^^
  147. Modify the local configuration for one or more option for a configured repo.
  148. This is also the way to create new repository configuration on the local
  149. system; if a repo is specified which does not yet exist, it will be created.
  150. The options specified for this function are specific to the system; please
  151. refer to the documentation for your specific repo manager for specifics.
  152. .. code-block:: python
  153. __salt__["pkg.mod_repo"](repo="myrepo", url="http://myurl.com/repo")
  154. Low-Package Functions
  155. ---------------------
  156. In general, the standard package functions as describes above will meet your
  157. needs. These functions use the system's native repo manager (for instance,
  158. yum or the apt tools). In most cases, the repo manager is actually separate
  159. from the package manager. For instance, yum is usually a front-end for rpm, and
  160. apt is usually a front-end for dpkg. When possible, the package functions that
  161. use those package managers directly should do so through the low package
  162. functions.
  163. It is normal and sane for ``pkg`` to make calls to ``lowpkgs``, but ``lowpkg``
  164. must never make calls to ``pkg``. This is affects functions which are required
  165. by both ``pkg`` and ``lowpkg``, but the technique in ``pkg`` is more performant
  166. than what is available to ``lowpkg``. When this is the case, the ``lowpkg``
  167. function that requires that technique must still use the ``lowpkg`` version.
  168. list_pkgs
  169. ^^^^^^^^^
  170. Returns a dict of packages installed, including the package name and version.
  171. Can accept a list of packages; if none are specified, then all installed
  172. packages will be listed.
  173. .. code-block:: python
  174. installed = __salt__["lowpkg.list_pkgs"]("foo", "bar")
  175. Example output:
  176. .. code-block:: python
  177. {"foo": "1.2.3-4", "bar": "5.6.7-8"}
  178. verify
  179. ^^^^^^
  180. Many (but not all) package management systems provide a way to verify that the
  181. files installed by the package manager have or have not changed. This function
  182. accepts a list of packages; if none are specified, all packages will be
  183. included.
  184. .. code-block:: python
  185. installed = __salt__["lowpkg.verify"]("httpd")
  186. Example output:
  187. .. code-block:: python
  188. {
  189. "/etc/httpd/conf/httpd.conf": {
  190. "mismatch": ["size", "md5sum", "mtime"],
  191. "type": "config",
  192. }
  193. }
  194. file_list
  195. ^^^^^^^^^
  196. Lists all of the files installed by all packages specified. If not packages are
  197. specified, then all files for all known packages are returned.
  198. .. code-block:: python
  199. installed = __salt__["lowpkg.file_list"]("httpd", "apache")
  200. This function does not return which files belong to which packages; all files
  201. are returned as one giant list (hence the `file_list` function name. However,
  202. This information is still returned inside of a dict, so that it can provide
  203. any errors to the user in a sane manner.
  204. .. code-block:: python
  205. {
  206. "errors": ["package apache is not installed"],
  207. "files": ["/etc/httpd", "/etc/httpd/conf", "/etc/httpd/conf.d", "...SNIP..."],
  208. }
  209. file_dict
  210. ^^^^^^^^^
  211. Lists all of the files installed by all packages specified. If not packages are
  212. specified, then all files for all known packages are returned.
  213. .. code-block:: python
  214. installed = __salt__["lowpkg.file_dict"]("httpd", "apache", "kernel")
  215. Unlike `file_list`, this function will break down which files belong to which
  216. packages. It will also return errors in the same manner as `file_list`.
  217. .. code-block:: python
  218. {
  219. "errors": ["package apache is not installed"],
  220. "packages": {
  221. "httpd": ["/etc/httpd", "/etc/httpd/conf", "...SNIP..."],
  222. "kernel": [
  223. "/boot/.vmlinuz-2.6.32-279.el6.x86_64.hmac",
  224. "/boot/System.map-2.6.32-279.el6.x86_64",
  225. "...SNIP...",
  226. ],
  227. },
  228. }