developing.rst 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. ======================
  2. Developing New Modules
  3. ======================
  4. Interactive Debugging
  5. =====================
  6. Sometimes debugging with ``print()`` and extra logs sprinkled everywhere is not
  7. the best strategy.
  8. IPython is a helpful debug tool that has an interactive python environment
  9. which can be embedded in python programs.
  10. First the system will require IPython to be installed.
  11. .. code-block:: bash
  12. # Debian
  13. apt-get install ipython
  14. # Arch Linux
  15. pacman -Syu ipython2
  16. # RHEL/CentOS (via EPEL)
  17. yum install python-ipython
  18. Now, in the troubling python module, add the following line at a location where
  19. the debugger should be started:
  20. .. code-block:: python
  21. test = "test123"
  22. import IPython
  23. IPython.embed_kernel()
  24. After running a Salt command that hits that line, the following will show up in
  25. the log file:
  26. .. code-block:: text
  27. [CRITICAL] To connect another client to this kernel, use:
  28. [IPKernelApp] --existing kernel-31271.json
  29. Now on the system that invoked ``embed_kernel``, run the following command from
  30. a shell:
  31. .. code-block:: bash
  32. # NOTE: use ipython2 instead of ipython for Arch Linux
  33. ipython console --existing
  34. This provides a console that has access to all the vars and functions, and even
  35. supports tab-completion.
  36. .. code-block:: python
  37. print(test)
  38. test123
  39. To exit IPython and continue running Salt, press ``Ctrl-d`` to logout.
  40. Special Module Contents
  41. =======================
  42. These are things that may be defined by the module to influence various things.
  43. __virtual__
  44. -----------
  45. __virtual_aliases__
  46. -------------------
  47. __virtualname__
  48. ---------------
  49. __init__
  50. --------
  51. Called before ``__virtual__()``
  52. __proxyenabled__
  53. ----------------
  54. grains and proxy modules
  55. __proxyenabled__ as a list containing the names of the proxy types that the module supports.
  56. __load__
  57. --------
  58. __func_alias__
  59. --------------
  60. __outputter__
  61. -------------
  62. .. _dunder-dictionaries:
  63. Dunder Dictionaries
  64. ===================
  65. Salt provides several special "dunder" dictionaries as a convenience for Salt
  66. development. These include ``__opts__``, ``__context__``, ``__salt__``, and
  67. others. This document will describe each dictionary and detail where they exist
  68. and what information and/or functionality they provide.
  69. The following dunder dictionaries are always defined, but may be empty
  70. * ``__context__``
  71. * ``__grains__``
  72. * ``__pillar__``
  73. * ``__opts__``
  74. __opts__
  75. --------
  76. Defined in: All modules
  77. The ``__opts__`` dictionary contains all of the options passed in the
  78. configuration file for the master or minion.
  79. .. note::
  80. In many places in salt, instead of pulling raw data from the __opts__
  81. dict, configuration data should be pulled from the salt `get` functions
  82. such as config.get, aka - ``__salt__['config.get']('foo:bar')``
  83. The `get` functions also allow for dict traversal via the *:* delimiter.
  84. Consider using get functions whenever using ``__opts__`` or ``__pillar__``
  85. and ``__grains__`` (when using grains for configuration data)
  86. The configuration file data made available in the ``__opts__`` dictionary is the
  87. configuration data relative to the running daemon. If the modules are loaded and
  88. executed by the master, then the master configuration data is available, if the
  89. modules are executed by the minion, then the minion configuration is
  90. available. Any additional information passed into the respective configuration
  91. files is made available
  92. __salt__
  93. --------
  94. Defined in: Auth, Beacons, Engines, Execution, Executors, Outputters, Pillars,
  95. Proxies, Renderers, Returners, Runners, SDB, SSH Wrappers, State, Thorium
  96. ``__salt__`` contains the execution module functions. This allows for all
  97. functions to be called as they have been set up by the salt loader.
  98. .. code-block:: python
  99. __salt__["cmd.run"]("fdisk -l")
  100. __salt__["network.ip_addrs"]()
  101. .. note::
  102. When used in runners or outputters, ``__salt__`` references other
  103. runner/outputter modules, and not execution modules.
  104. __grains__
  105. ----------
  106. Filled in for: Execution, Pillar, Renderer, Returner, SSH Wrapper, State.
  107. The ``__grains__`` dictionary contains the grains data generated by the minion
  108. that is currently being worked with. In execution modules, state modules and
  109. returners this is the grains of the minion running the calls, when generating
  110. the external pillar the ``__grains__`` is the grains data from the minion that
  111. the pillar is being generated for.
  112. While ``__grains__`` is defined for every module, it's only filled in for some.
  113. __pillar__
  114. -----------
  115. Filled in for: Execution, Renderer, Returner, SSH Wrapper, State
  116. The ``__pillar__`` dictionary contains the pillar for the respective minion.
  117. While ``__pillar__`` is defined for every module, it's only filled in for some.
  118. __ext_pillar__
  119. --------------
  120. Filled in for: Pillar
  121. The ``__ext_pillar__`` dictionary contains the external pillar modules.
  122. .. _dunder-context:
  123. __context__
  124. -----------
  125. During a state run the ``__context__`` dictionary persists across all states
  126. that are run and then is destroyed when the state ends.
  127. When running an execution module ``__context__`` persists across all module
  128. executions until the modules are refreshed; such as when
  129. :py:func:`saltutil.sync_all <salt.modules.saltutil.sync_all>` or
  130. :py:func:`state.apply <salt.modules.state.apply_>` are executed.
  131. A great place to see how to use ``__context__`` is in the cp.py module in
  132. salt/modules/cp.py. The fileclient authenticates with the master when it is
  133. instantiated and then is used to copy files to the minion. Rather than create a
  134. new fileclient for each file that is to be copied down, one instance of the
  135. fileclient is instantiated in the ``__context__`` dictionary and is reused for
  136. each file. Here is an example from salt/modules/cp.py:
  137. .. code-block:: python
  138. if not "cp.fileclient" in __context__:
  139. __context__["cp.fileclient"] = salt.fileclient.get_file_client(__opts__)
  140. .. note:: Because __context__ may or may not have been destroyed, always be
  141. sure to check for the existence of the key in __context__ and
  142. generate the key before using it.
  143. __utils__
  144. ---------
  145. Defined in: Cloud, Engine, Execution, File Server, Grain, Pillar, Proxy, Roster, Runner, SDB, State
  146. __proxy__
  147. ---------
  148. Defined in: Beacon, Engine, Execution, Executor, Proxy, Renderer, Returner, State, Util
  149. __runner__
  150. -----------
  151. Defined in: Engine, Roster, Thorium
  152. .. note:: When used in engines, it should be called __runners__ (plural)
  153. __executors__
  154. -------------
  155. Defined in: Executor
  156. __ret__
  157. -------
  158. Defined in: Proxy
  159. __thorium__
  160. -----------
  161. Defined in: Thorium
  162. __states__
  163. ----------
  164. Defined in: Renderers, State
  165. __serializers__
  166. ---------------
  167. Defined in: State
  168. __sdb__
  169. -------
  170. Defined in: SDB
  171. Additional Globals
  172. ==================
  173. Defined for: Runners, Execution Modules, Wheels
  174. * ``__jid__``: The job ID
  175. * ``__user__``: The user
  176. * ``__tag__``: The jid tag
  177. * ``__jid_event__``: A :py:class:`salt.utils.event.NamespacedEvent`.
  178. :py:class:`NamespacedEvent <salt.utils.event.NamespacedEvent>` defines a single
  179. method :py:meth:`fire_event <salt.utils.event.NamespacedEvent.fire_event>`, that takes data and tag. The :ref:`Runner docs <runners>` has examples.