1
0

dev.rst 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. .. _spm-development:
  2. =====================
  3. SPM Development Guide
  4. =====================
  5. This document discusses developing additional code for SPM.
  6. SPM-Specific Loader Modules
  7. ===========================
  8. SPM was designed to behave like traditional package managers, which apply files
  9. to the filesystem and store package metadata in a local database. However,
  10. because modern infrastructures often extend beyond those use cases, certain
  11. parts of SPM have been broken out into their own set of modules.
  12. Each function that accepts arguments has a set of required and optional
  13. arguments. Take note that SPM will pass all arguments in, and therefore each
  14. function must accept each of those arguments. However, arguments that are
  15. marked as required are crucial to SPM's core functionality, while arguments that
  16. are marked as optional are provided as a benefit to the module, if it needs to
  17. use them.
  18. .. _spm-development-pkgdb:
  19. Package Database
  20. ----------------
  21. By default, the package database is stored using the ``sqlite3`` module. This
  22. module was chosen because support for SQLite3 is built into Python itself.
  23. Modules for managing the package database are stored in the ``salt/spm/pkgdb/``
  24. directory. A number of functions must exist to support database management.
  25. init()
  26. ``````
  27. Get a database connection, and initialize the package database if necessary.
  28. This function accepts no arguments. If a database is used which supports a
  29. connection object, then that connection object is returned. For instance, the
  30. ``sqlite3`` module returns a ``connect()`` object from the ``sqlite3`` library:
  31. .. code-block:: python
  32. def myfunc():
  33. conn = sqlite3.connect(__opts__["spm_db"], isolation_level=None)
  34. ...
  35. return conn
  36. SPM itself will not use this connection object; it will be passed in as-is to
  37. the other functions in the module. Therefore, when you set up this object, make
  38. sure to do so in a way that is easily usable throughout the module.
  39. info()
  40. ``````
  41. Return information for a package. This generally consists of the information
  42. that is stored in the ``FORMULA`` file in the package.
  43. The arguments that are passed in, in order, are ``package`` (required) and
  44. ``conn`` (optional).
  45. ``package`` is the name of the package, as specified in the ``FORMULA``.
  46. ``conn`` is the connection object returned from ``init()``.
  47. list_files()
  48. ````````````
  49. Return a list of files for an installed package. Only the filename should be
  50. returned, and no other information.
  51. The arguments that are passed in, in order, are ``package`` (required) and
  52. ``conn`` (optional).
  53. ``package`` is the name of the package, as specified in the ``FORMULA``.
  54. ``conn`` is the connection object returned from ``init()``.
  55. register_pkg()
  56. ``````````````
  57. Register a package in the package database. Nothing is expected to be returned
  58. from this function.
  59. The arguments that are passed in, in order, are ``name`` (required),
  60. ``formula_def`` (required), and ``conn`` (optional).
  61. ``name`` is the name of the package, as specified in the ``FORMULA``.
  62. ``formula_def`` is the contents of the ``FORMULA`` file, as a ``dict``. ``conn``
  63. is the connection object returned from ``init()``.
  64. register_file()
  65. ```````````````
  66. Register a file in the package database. Nothing is expected to be returned
  67. from this function.
  68. The arguments that are passed in are ``name`` (required), ``member`` (required),
  69. ``path`` (required), ``digest`` (optional), and ``conn`` (optional).
  70. ``name`` is the name of the package.
  71. ``member`` is a ``tarfile`` object for the
  72. package file. It is included, because it contains most of the information for
  73. the file.
  74. ``path`` is the location of the file on the local filesystem.
  75. ``digest`` is the SHA1 checksum of the file.
  76. ``conn`` is the connection object returned from ``init()``.
  77. unregister_pkg()
  78. ````````````````
  79. Unregister a package from the package database. This usually only involves
  80. removing the package's record from the database. Nothing is expected to be
  81. returned from this function.
  82. The arguments that are passed in, in order, are ``name`` (required) and
  83. ``conn`` (optional).
  84. ``name`` is the name of the package, as specified in the ``FORMULA``. ``conn``
  85. is the connection object returned from ``init()``.
  86. unregister_file()
  87. `````````````````
  88. Unregister a package from the package database. This usually only involves
  89. removing the package's record from the database. Nothing is expected to be
  90. returned from this function.
  91. The arguments that are passed in, in order, are ``name`` (required), ``pkg``
  92. (optional) and ``conn`` (optional).
  93. ``name`` is the path of the file, as it was installed on the filesystem.
  94. ``pkg`` is the name of the package that the file belongs to.
  95. ``conn`` is the connection object returned from ``init()``.
  96. db_exists()
  97. ```````````
  98. Check to see whether the package database already exists. This is the path to
  99. the package database file. This function will return ``True`` or ``False``.
  100. The only argument that is expected is ``db_``, which is the package database
  101. file.
  102. .. _spm-development-pkgfiles:
  103. Package Files
  104. -------------
  105. By default, package files are installed using the ``local`` module. This module
  106. applies files to the local filesystem, on the machine that the package is
  107. installed on.
  108. Modules for managing the package database are stored in the
  109. ``salt/spm/pkgfiles/`` directory. A number of functions must exist to support
  110. file management.
  111. init()
  112. ``````
  113. Initialize the installation location for the package files. Normally these will
  114. be directory paths, but other external destinations such as databases can be
  115. used. For this reason, this function will return a connection object, which can
  116. be a database object. However, in the default ``local`` module, this object is a
  117. dict containing the paths. This object will be passed into all other functions.
  118. Three directories are used for the destinations: ``formula_path``,
  119. ``pillar_path``, and ``reactor_path``.
  120. ``formula_path`` is the location of most of the files that will be installed.
  121. The default is specific to the operating system, but is normally ``/srv/salt/``.
  122. ``pillar_path`` is the location that the ``pillar.example`` file will be
  123. installed to. The default is specific to the operating system, but is normally
  124. ``/srv/pillar/``.
  125. ``reactor_path`` is the location that reactor files will be installed to. The
  126. default is specific to the operating system, but is normally ``/srv/reactor/``.
  127. check_existing()
  128. ````````````````
  129. Check the filesystem for existing files. All files for the package will be
  130. checked, and if any are existing, then this function will normally state that
  131. SPM will refuse to install the package.
  132. This function returns a list of the files that exist on the system.
  133. The arguments that are passed into this function are, in order: ``package``
  134. (required), ``pkg_files`` (required), ``formula_def`` (formula_def), and
  135. ``conn`` (optional).
  136. ``package`` is the name of the package that is to be installed.
  137. ``pkg_files`` is a list of the files to be checked.
  138. ``formula_def`` is a copy of the information that is stored in the ``FORMULA``
  139. file.
  140. ``conn`` is the file connection object.
  141. install_file()
  142. ``````````````
  143. Install a single file to the destination (normally on the filesystem). Nothing
  144. is expected to be returned from this function.
  145. This function returns the final location that the file was installed to.
  146. The arguments that are passed into this function are, in order, ``package``
  147. (required), ``formula_tar`` (required), ``member`` (required), ``formula_def``
  148. (required), and ``conn`` (optional).
  149. ``package`` is the name of the package that is to be installed.
  150. ``formula_tar`` is the tarfile object for the package. This is passed in so that
  151. the function can call ``formula_tar.extract()`` for the file.
  152. ``member`` is the tarfile object which represents the individual file. This may
  153. be modified as necessary, before being passed into ``formula_tar.extract()``.
  154. ``formula_def`` is a copy of the information from the ``FORMULA`` file.
  155. ``conn`` is the file connection object.
  156. remove_file()
  157. `````````````
  158. Remove a single file from file system. Normally this will be little more than an
  159. ``os.remove()``. Nothing is expected to be returned from this function.
  160. The arguments that are passed into this function are, in order, ``path``
  161. (required) and ``conn`` (optional).
  162. ``path`` is the absolute path to the file to be removed.
  163. ``conn`` is the file connection object.
  164. hash_file()
  165. ```````````
  166. Returns the hexdigest hash value of a file.
  167. The arguments that are passed into this function are, in order, ``path``
  168. (required), ``hashobj`` (required), and ``conn`` (optional).
  169. ``path`` is the absolute path to the file.
  170. ``hashobj`` is a reference to ``hashlib.sha1()``, which is used to pull the
  171. ``hexdigest()`` for the file.
  172. ``conn`` is the file connection object.
  173. This function will not generally be more complex than:
  174. .. code-block:: python
  175. def hash_file(path, hashobj, conn=None):
  176. with salt.utils.files.fopen(path, "r") as f:
  177. hashobj.update(f.read())
  178. return hashobj.hexdigest()
  179. path_exists()
  180. `````````````
  181. Check to see whether the file already exists on the filesystem. Returns ``True``
  182. or ``False``.
  183. This function expects a ``path`` argument, which is the absolute path to the
  184. file to be checked.
  185. path_isdir()
  186. ````````````
  187. Check to see whether the path specified is a directory. Returns ``True`` or
  188. ``False``.
  189. This function expects a ``path`` argument, which is the absolute path to be
  190. checked.