1
0

writing.rst 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. ======================
  2. Writing netapi modules
  3. ======================
  4. :py:mod:`~salt.netapi` modules, put simply, bind a port and start a service.
  5. They are purposefully open-ended and can be used to present a variety of
  6. external interfaces to Salt, and even present multiple interfaces at once.
  7. .. seealso:: :ref:`The full list of netapi modules <all-netapi-modules>`
  8. Configuration
  9. =============
  10. All :py:mod:`~salt.netapi` configuration is done in the :ref:`Salt master
  11. config <configuration-salt-master>` and takes a form similar to the following:
  12. .. code-block:: yaml
  13. rest_cherrypy:
  14. port: 8000
  15. debug: True
  16. ssl_crt: /etc/pki/tls/certs/localhost.crt
  17. ssl_key: /etc/pki/tls/certs/localhost.key
  18. The ``__virtual__`` function
  19. ============================
  20. Like all module types in Salt, :py:mod:`~salt.netapi` modules go through
  21. Salt's loader interface to determine if they should be loaded into memory and
  22. then executed.
  23. The ``__virtual__`` function in the module makes this determination and should
  24. return ``False`` or a string that will serve as the name of the module. If the
  25. module raises an ``ImportError`` or any other errors, it will not be loaded.
  26. The ``start`` function
  27. ======================
  28. The ``start()`` function will be called for each :py:mod:`~salt.netapi`
  29. module that is loaded. This function should contain the server loop that
  30. actually starts the service. This is started in a multiprocess.
  31. Multiple instances
  32. ==================
  33. .. versionadded:: 2016.11.0
  34. :py:mod:`~salt.netapi.rest_cherrypy` and :py:mod:`~salt.netapi.rest_tornado`
  35. support running multiple instances by copying and renaming entire directory
  36. of those. To start the copied multiple :py:mod:`~salt.netapi` modules, add
  37. configuration blocks for the copied :py:mod:`~salt.netapi` modules in the
  38. Salt Master config. The name of each added configuration block must match
  39. with the name of each directory of the copied :py:mod:`~salt.netapi` module.
  40. Inline documentation
  41. ====================
  42. As with the rest of Salt, it is a best-practice to include liberal inline
  43. documentation in the form of a module docstring and docstrings on any classes,
  44. methods, and functions in your :py:mod:`~salt.netapi` module.
  45. Loader “magic” methods
  46. ======================
  47. The loader makes the ``__opts__`` data structure available to any function in
  48. a :py:mod:`~salt.netapi` module.