1
0

dev.rst 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. conn = sqlite3.connect(__opts__["spm_db"], isolation_level=None)
  33. ...
  34. return conn
  35. SPM itself will not use this connection object; it will be passed in as-is to
  36. the other functions in the module. Therefore, when you set up this object, make
  37. sure to do so in a way that is easily usable throughout the module.
  38. info()
  39. ``````
  40. Return information for a package. This generally consists of the information
  41. that is stored in the ``FORMULA`` file in the package.
  42. The arguments that are passed in, in order, are ``package`` (required) and
  43. ``conn`` (optional).
  44. ``package`` is the name of the package, as specified in the ``FORMULA``.
  45. ``conn`` is the connection object returned from ``init()``.
  46. list_files()
  47. ````````````
  48. Return a list of files for an installed package. Only the filename should be
  49. returned, and no other information.
  50. The arguments that are passed in, in order, are ``package`` (required) and
  51. ``conn`` (optional).
  52. ``package`` is the name of the package, as specified in the ``FORMULA``.
  53. ``conn`` is the connection object returned from ``init()``.
  54. register_pkg()
  55. ``````````````
  56. Register a package in the package database. Nothing is expected to be returned
  57. from this function.
  58. The arguments that are passed in, in order, are ``name`` (required),
  59. ``formula_def`` (required), and ``conn`` (optional).
  60. ``name`` is the name of the package, as specified in the ``FORMULA``.
  61. ``formula_def`` is the contents of the ``FORMULA`` file, as a ``dict``. ``conn``
  62. is the connection object returned from ``init()``.
  63. register_file()
  64. ```````````````
  65. Register a file in the package database. Nothing is expected to be returned
  66. from this function.
  67. The arguments that are passed in are ``name`` (required), ``member`` (required),
  68. ``path`` (required), ``digest`` (optional), and ``conn`` (optional).
  69. ``name`` is the name of the package.
  70. ``member`` is a ``tarfile`` object for the
  71. package file. It is included, because it contains most of the information for
  72. the file.
  73. ``path`` is the location of the file on the local filesystem.
  74. ``digest`` is the SHA1 checksum of the file.
  75. ``conn`` is the connection object returned from ``init()``.
  76. unregister_pkg()
  77. ````````````````
  78. Unregister a package from the package database. This usually only involves
  79. removing the package's record from the database. Nothing is expected to be
  80. returned from this function.
  81. The arguments that are passed in, in order, are ``name`` (required) and
  82. ``conn`` (optional).
  83. ``name`` is the name of the package, as specified in the ``FORMULA``. ``conn``
  84. is the connection object returned from ``init()``.
  85. unregister_file()
  86. `````````````````
  87. Unregister a package from the package database. This usually only involves
  88. removing the package's record from the database. Nothing is expected to be
  89. returned from this function.
  90. The arguments that are passed in, in order, are ``name`` (required), ``pkg``
  91. (optional) and ``conn`` (optional).
  92. ``name`` is the path of the file, as it was installed on the filesystem.
  93. ``pkg`` is the name of the package that the file belongs to.
  94. ``conn`` is the connection object returned from ``init()``.
  95. db_exists()
  96. ```````````
  97. Check to see whether the package database already exists. This is the path to
  98. the package database file. This function will return ``True`` or ``False``.
  99. The only argument that is expected is ``db_``, which is the package database
  100. file.
  101. .. _spm-development-pkgfiles:
  102. Package Files
  103. -------------
  104. By default, package files are installed using the ``local`` module. This module
  105. applies files to the local filesystem, on the machine that the package is
  106. installed on.
  107. Modules for managing the package database are stored in the
  108. ``salt/spm/pkgfiles/`` directory. A number of functions must exist to support
  109. file management.
  110. init()
  111. ``````
  112. Initialize the installation location for the package files. Normally these will
  113. be directory paths, but other external destinations such as databases can be
  114. used. For this reason, this function will return a connection object, which can
  115. be a database object. However, in the default ``local`` module, this object is a
  116. dict containing the paths. This object will be passed into all other functions.
  117. Three directories are used for the destinations: ``formula_path``,
  118. ``pillar_path``, and ``reactor_path``.
  119. ``formula_path`` is the location of most of the files that will be installed.
  120. The default is specific to the operating system, but is normally ``/srv/salt/``.
  121. ``pillar_path`` is the location that the ``pillar.example`` file will be
  122. installed to. The default is specific to the operating system, but is normally
  123. ``/srv/pillar/``.
  124. ``reactor_path`` is the location that reactor files will be installed to. The
  125. default is specific to the operating system, but is normally ``/srv/reactor/``.
  126. check_existing()
  127. ````````````````
  128. Check the filesystem for existing files. All files for the package will be
  129. checked, and if any are existing, then this function will normally state that
  130. SPM will refuse to install the package.
  131. This function returns a list of the files that exist on the system.
  132. The arguments that are passed into this function are, in order: ``package``
  133. (required), ``pkg_files`` (required), ``formula_def`` (formula_def), and
  134. ``conn`` (optional).
  135. ``package`` is the name of the package that is to be installed.
  136. ``pkg_files`` is a list of the files to be checked.
  137. ``formula_def`` is a copy of the information that is stored in the ``FORMULA``
  138. file.
  139. ``conn`` is the file connection object.
  140. install_file()
  141. ``````````````
  142. Install a single file to the destination (normally on the filesystem). Nothing
  143. is expected to be returned from this function.
  144. This function returns the final location that the file was installed to.
  145. The arguments that are passed into this function are, in order, ``package``
  146. (required), ``formula_tar`` (required), ``member`` (required), ``formula_def``
  147. (required), and ``conn`` (optional).
  148. ``package`` is the name of the package that is to be installed.
  149. ``formula_tar`` is the tarfile object for the package. This is passed in so that
  150. the function can call ``formula_tar.extract()`` for the file.
  151. ``member`` is the tarfile object which represents the individual file. This may
  152. be modified as necessary, before being passed into ``formula_tar.extract()``.
  153. ``formula_def`` is a copy of the information from the ``FORMULA`` file.
  154. ``conn`` is the file connection object.
  155. remove_file()
  156. `````````````
  157. Remove a single file from file system. Normally this will be little more than an
  158. ``os.remove()``. Nothing is expected to be returned from this function.
  159. The arguments that are passed into this function are, in order, ``path``
  160. (required) and ``conn`` (optional).
  161. ``path`` is the absolute path to the file to be removed.
  162. ``conn`` is the file connection object.
  163. hash_file()
  164. ```````````
  165. Returns the hexdigest hash value of a file.
  166. The arguments that are passed into this function are, in order, ``path``
  167. (required), ``hashobj`` (required), and ``conn`` (optional).
  168. ``path`` is the absolute path to the file.
  169. ``hashobj`` is a reference to ``hashlib.sha1()``, which is used to pull the
  170. ``hexdigest()`` for the file.
  171. ``conn`` is the file connection object.
  172. This function will not generally be more complex than:
  173. .. code-block:: python
  174. def hash_file(path, hashobj, conn=None):
  175. with salt.utils.files.fopen(path, "r") as f:
  176. hashobj.update(f.read())
  177. return hashobj.hexdigest()
  178. path_exists()
  179. `````````````
  180. Check to see whether the file already exists on the filesystem. Returns ``True``
  181. or ``False``.
  182. This function expects a ``path`` argument, which is the absolute path to the
  183. file to be checked.
  184. path_isdir()
  185. ````````````
  186. Check to see whether the path specified is a directory. Returns ``True`` or
  187. ``False``.
  188. This function expects a ``path`` argument, which is the absolute path to be
  189. checked.