Guest User

Untitled

a guest
Jun 14th, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.58 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """Proof of concept to use saltstack's pure modules and states with fabric::
  3.  
  4. fab list_packages print_specs -H <ip> --user=<user>
  5.  
  6. On my vagrant environment:
  7.  
  8. fab list_packages print_specs -H <ip> --password=vagrant --user=vagrant
  9. """
  10.  
  11. import os
  12. import sys
  13. import subprocess
  14. from salt.modules.cmdmod import DEFAULT_SHELL
  15.  
  16. sys.path.append(os.path.dirname(__file__))
  17. from salt_python import salt
  18.  
  19.  
  20. try:
  21. import fabric
  22. except ImportError:
  23. raise Exception("""
  24. You must install the latest version of salt:
  25. pip install fabric
  26. """)
  27.  
  28.  
  29. def salt_run_fabric(cmd,
  30. cwd=None,
  31. stdin=None,
  32. stdout=subprocess.PIPE,
  33. stderr=subprocess.PIPE,
  34. output_loglevel='debug',
  35. quiet=False,
  36. runas=None,
  37. shell=DEFAULT_SHELL,
  38. python_shell=True,
  39. env=None,
  40. clean_env=False,
  41. rstrip=True,
  42. template=None,
  43. umask=None,
  44. timeout=None,
  45. with_communicate=True,
  46. reset_system_locale=True,
  47. ignore_retcode=False,
  48. saltenv='base',
  49. use_vt=False):
  50.  
  51. """Adapter to fit salt's argument signature into into fabric's
  52. subprocess wrapper."""
  53.  
  54. return fabric.operations.sudo(
  55. cmd,
  56. shell=shell,
  57. user=runas,
  58. timeout=timeout,
  59. warn_only=ignore_retcode,
  60. stdout=stdout,
  61. stderr=stderr,
  62. env=env
  63. )
  64.  
  65. def salt_run_fabric_quiet(cmd,
  66. cwd=None,
  67. stdin=None,
  68. runas=None,
  69. shell=DEFAULT_SHELL,
  70. python_shell=True,
  71. env=None,
  72. template=None,
  73. umask=None,
  74. timeout=None,
  75. reset_system_locale=True,
  76. saltenv='base'):
  77. '''
  78. Helper for running commands quietly for minion startup
  79. '''
  80. return salt_run_fabric(cmd,
  81. runas=runas,
  82. cwd=cwd,
  83. stdin=stdin,
  84. stderr=subprocess.STDOUT,
  85. output_loglevel='quiet',
  86. shell=shell,
  87. python_shell=python_shell,
  88. env=env,
  89. template=template,
  90. umask=umask,
  91. timeout=timeout,
  92. reset_system_locale=reset_system_locale,
  93. saltenv=saltenv)['stdout']
  94.  
  95. def bootstrap_fabric():
  96. from salt.config import DEFAULT_MASTER_OPTS
  97. from salt.grains.core import os_data
  98.  
  99. __builtins__['__salt__'] = {
  100. 'cmd.run': salt_run_fabric,
  101. 'cmd.run_all': salt.modules.cmdmod.run_all
  102. }
  103.  
  104. __builtins__['__context__'] = {}
  105. __builtins__['__env__'] = {}
  106. __builtins__['__opts__'] = DEFAULT_MASTER_OPTS
  107. __builtins__['__grains__'] = salt.loader.grains(__opts__)
  108.  
  109. __builtins__['__salt__'].update(
  110. salt.loader.minion_mods(
  111. __opts__
  112. )
  113. )
  114. print(__salt__.keys())
  115.  
  116.  
  117. bootstrap_fabric()
  118.  
  119.  
  120. @fabric.api.task
  121. def list_packages():
  122. # from salt.modules import aptpkg
  123. # if __grains__['os'] == 'MacOS' and sources:
  124. # causes a race condition
  125. #print(aptpkg.install('vim'))
  126.  
  127. from salt.modules import aptpkg
  128. print(aptpkg.list_pkgs())
  129.  
  130. @fabric.api.task
  131. def print_specs():
  132. from pprint import PrettyPrinter
  133. pp = PrettyPrinter(indent=4).pprint
  134. pp(__grains__)
Add Comment
Please, Sign In to add comment