developing.rst 6.8 KB

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