ssh_wrapper.rst 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. .. _ssh-wrapper:
  2. ===========
  3. SSH Wrapper
  4. ===========
  5. Salt-SSH Background
  6. ===================
  7. Salt-SSH works by creating a tar ball of salt, a bunch of python modules, and a generated
  8. short minion config. It then copies this onto the destination host over ssh, then
  9. uses that host's local python install to run ``salt-client --local`` with any requested modules.
  10. It does not automatically copy over states or cache files and since it is uses a local file_client,
  11. modules that rely on :py:func:`cp.cache* <salt.modules.cp>` functionality do not work.
  12. SSH Wrapper modules
  13. ===================
  14. To support cp modules or other functionality which might not otherwise work in the remote environment,
  15. a wrapper module can be created. These modules are run from the salt-master initiating the salt-ssh
  16. command and can include logic to support the needed functionality. SSH Wrapper modules are located in
  17. /salt/client/ssh/wrapper/ and are named the same as the execution module being extended. Any functions
  18. defined inside of the wrapper module are called from the ``salt-ssh module.function argument``
  19. command rather than executing on the minion.
  20. State Module example
  21. --------------------
  22. Running salt states on an salt-ssh minion, obviously requires the state files themselves. To support this,
  23. a state module wrapper script exists at salt/client/ssh/wrapper/state.py, and includes standard state
  24. functions like :py:func:`apply <salt.modules.state.apply>`, :py:func:`sls <salt.modules.state.sls>`,
  25. and :py:func:`highstate <salt.modules.state.highstate>`. When executing ``salt-ssh minion state.highstate``,
  26. these wrapper functions are used and include the logic to walk the low_state output for that minion to
  27. determine files used, gather needed files, tar them together, transfer the tar file to the minion over
  28. ssh, and run a state on the ssh minion. This state then extracts the tar file, applies the needed states
  29. and data, and cleans up the transferred files.
  30. Wrapper Handling
  31. ----------------
  32. From the wrapper script any invocations of ``__salt__['some.module']()`` do not run on the master
  33. which is running the wrapper, but instead magically are invoked on the minion over ssh.
  34. Should the function being called exist in the wrapper, the wrapper function will be
  35. used instead.
  36. One way of supporting this workflow may be to create a wrapper function which performs the needed file
  37. copy operations. Now that files are resident on the ssh minion, the next step is to run the original
  38. execution module function. But since that function name was already overridden by the wrapper, a
  39. function alias can be created in the original execution module, which can then be called from the
  40. wrapper.
  41. Example
  42. ```````
  43. The saltcheck module needs sls and tst files on the minion to function. The invocation of
  44. :py:func:`saltcheck.run_state_tests <salt.modules.saltcheck.run_state_tests>` is run from
  45. the wrapper module, and is responsible for performing the needed file copy. The
  46. :py:func:`saltcheck <salt.modules.saltcheck>` execution module includes an alias line of
  47. ``run_state_tests_ssh = salt.utils.functools.alias_function(run_state_tests, 'run_state_tests_ssh')``
  48. which creates an alias of ``run_state_tests`` with the name ``run_state_tests_ssh``. At the end of
  49. the ``run_state_tests`` function in the wrapper module, it then calls
  50. ``__salt__['saltcheck.run_state_tests_ssh']()``. Since this function does not exist in the wrapper script,
  51. the call is made on the remote minion, which then having the needed files, runs as expected.