openshift_quick_installer.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. # This program is free software: you can redistribute it and/or modify
  2. # it under the terms of the GNU General Public License as published by
  3. # the Free Software Foundation, either version 3 of the License, or
  4. # (at your option) any later version.
  5. #
  6. # This program is distributed in the hope that it will be useful,
  7. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. # GNU General Public License for more details.
  10. #
  11. # You should have received a copy of the GNU General Public License
  12. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. """
  14. This file is a stdout callback plugin for the OpenShift Quick
  15. Installer. The purpose of this callback plugin is to reduce the amount
  16. of produced output for customers and enable simpler progress checking.
  17. What's different:
  18. * Playbook progress is expressed as: Play <current_play>/<total_plays> (Play Name)
  19. Ex: Play 3/30 (Initialize Megafrobber)
  20. * The Tasks and Handlers in each play (and included roles) are printed
  21. as a series of .'s following the play progress line.
  22. """
  23. from __future__ import (absolute_import, print_function)
  24. import imp
  25. import os
  26. import sys
  27. ANSIBLE_PATH = imp.find_module('ansible')[1]
  28. DEFAULT_PATH = os.path.join(ANSIBLE_PATH, 'plugins/callback/default.py')
  29. DEFAULT_MODULE = imp.load_source(
  30. 'ansible.plugins.callback.default',
  31. DEFAULT_PATH
  32. )
  33. try:
  34. from ansible.plugins.callback import CallbackBase
  35. BASECLASS = CallbackBase
  36. except ImportError: # < ansible 2.1
  37. BASECLASS = DEFAULT_MODULE.CallbackModule
  38. from ansible import constants as C
  39. reload(sys)
  40. sys.setdefaultencoding('utf-8')
  41. class CallbackModule(DEFAULT_MODULE.CallbackModule):
  42. """
  43. Ansible callback plugin
  44. """
  45. CALLBACK_VERSION = 2.2
  46. CALLBACK_TYPE = 'stdout'
  47. CALLBACK_NAME = 'openshift_quick_installer'
  48. CALLBACK_NEEDS_WHITELIST = False
  49. plays_count = 0
  50. plays_total_ran = 0
  51. def banner(self, msg, color=None):
  52. '''
  53. Prints a header-looking line with stars taking up to 80 columns
  54. of width (3 columns, minimum)
  55. '''
  56. msg = msg.strip()
  57. star_len = (79 - len(msg))
  58. if star_len < 0:
  59. star_len = 3
  60. stars = "*" * star_len
  61. self._display.display("\n%s %s" % (msg, stars), color=color, log_only=True)
  62. def v2_playbook_on_start(self, playbook):
  63. """This is basically the start of it all"""
  64. self.plays_count = len(playbook.get_plays())
  65. self.plays_total_ran = 0
  66. def v2_playbook_on_play_start(self, play):
  67. """Each play calls this once before running any tasks
  68. We could print the number of tasks here as well by using
  69. `play.get_tasks()` but that is not accurate when a play includes a
  70. role. Only the tasks directly assigned to a play are directly exposed
  71. in the `play` object.
  72. """
  73. self.plays_total_ran += 1
  74. print("")
  75. print("Play %s/%s (%s)" % (self.plays_total_ran, self.plays_count, play.get_name()))
  76. name = play.get_name().strip()
  77. if not name:
  78. msg = "PLAY"
  79. else:
  80. msg = "PLAY [%s]" % name
  81. self.banner(msg)
  82. # pylint: disable=unused-argument,no-self-use
  83. def v2_playbook_on_task_start(self, task, is_conditional):
  84. """This prints out the task header. For example:
  85. TASK [openshift_facts : Ensure PyYaml is installed] ***...
  86. Rather than print out all that for every task, we print a dot
  87. character to indicate a task has been started.
  88. """
  89. sys.stdout.write('.')
  90. # pylint: disable=unused-argument,no-self-use
  91. def v2_playbook_on_handler_task_start(self, task):
  92. """Print out task header for handlers
  93. Rather than print out a header for every handler, we print a dot
  94. character to indicate a handler task has been started.
  95. """
  96. sys.stdout.write('.')
  97. # pylint: disable=unused-argument,no-self-use
  98. def v2_playbook_on_cleanup_task_start(self, task):
  99. """Print out a task header for cleanup tasks
  100. Rather than print out a header for every handler, we print a dot
  101. character to indicate a handler task has been started.
  102. """
  103. sys.stdout.write('.')
  104. def v2_playbook_on_include(self, included_file):
  105. """Print out paths to statically included files"""
  106. pass
  107. def v2_runner_on_ok(self, result):
  108. """This prints out task results in a fancy format"""
  109. pass
  110. def v2_runner_item_on_ok(self, result):
  111. """Print out task results for you're iterating"""
  112. pass
  113. def v2_runner_item_on_skipped(self, result):
  114. """Print out task results when an item is skipped"""
  115. pass
  116. def v2_runner_on_skipped(self, result):
  117. """Print out task results when a task (or something else?) is skipped"""
  118. pass
  119. def v2_playbook_on_notify(self, res, handler):
  120. """What happens when a task result is 'changed' and the task has a
  121. 'notify' list attached.
  122. """
  123. pass