package_providers.rst 11 KB

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