From e6961163ad3d18b21c75a35d5cdc757a5ac7ff0b Mon Sep 17 00:00:00 2001 From: Alex Schultz Date: Fri, 8 May 2020 15:58:28 -0600 Subject: [PATCH] Add tripleo_profile_tasks for tripleo_dense output If we switch to tripleo_dense, the upstream profile_tasks breaks the formatting. So we can extend the profile_tasks and override the functions that print out the other formatting. Change-Id: I19f2aace53b0806496477966680925bf298b677f --- .../callback/tripleo_profile_tasks.py | 111 ++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 tripleo_ansible/ansible_plugins/callback/tripleo_profile_tasks.py diff --git a/tripleo_ansible/ansible_plugins/callback/tripleo_profile_tasks.py b/tripleo_ansible/ansible_plugins/callback/tripleo_profile_tasks.py new file mode 100644 index 000000000..cbf925210 --- /dev/null +++ b/tripleo_ansible/ansible_plugins/callback/tripleo_profile_tasks.py @@ -0,0 +1,111 @@ +from __future__ import (absolute_import, division, print_function) +__metaclass__ = type + +import time + +from ansible import constants as C +from ansible.plugins.callback.profile_tasks import secondsToStr +from ansible.plugins.callback.profile_tasks import timestamp +from ansible.plugins.callback.profile_tasks import CallbackModule as PT + +DOCUMENTATION = ''' + callback: tripleo_profile_tasks + type: aggregate + short_description: adds time information to tasks + version_added: "2.9" + description: + - Based on upstream profile_tasks but formatted for tripleo_dense + requirements: + - whitelisting in configuration - see examples section below for details. + options: + output_limit: + description: Number of tasks to display in the summary + default: 20 + env: + - name: PROFILE_TASKS_TASK_OUTPUT_LIMIT + ini: + - section: callback_profile_tasks + key: task_output_limit + sort_order: + description: Adjust the sorting output of summary tasks + choices: ['descending', 'ascending', 'none'] + default: 'descending' + env: + - name: PROFILE_TASKS_SORT_ORDER + ini: + - section: callback_profile_tasks + key: sort_order +''' + + +class CallbackModule(PT): + + CALLBACK_VERSION = 2.0 + CALLBACK_TYPE = 'aggregate' + CALLBACK_NAME = 'tripleo_profile_tasks' + CALLBACK_NEEDS_WHITELIST = True + + def __init__(self): + self.start_time = time.time() + super(CallbackModule, self).__init__() + + def _output_previous_timings(self, uuid): + # no previous timing because uuid was null + if not uuid: + return + line = [ + uuid, + u'{:>10}'.format('TIMING'), + self.stats[uuid].get('name', 'NONAME'), + secondsToStr(time.time() - self.start_time), + u'{0:.02f}s'.format(self.stats[uuid].get('time', '-1')) + ] + self._display.display(' | '.join(line), color=C.COLOR_DEBUG) + + def _record_task(self, task): + timestamp(self) + self._output_previous_timings(self.current) + self.current = task._uuid + self.stats[self.current] = {'time': time.time(), + 'name': task.get_name()} + if self._display.verbosity >= 2: + self.stats[self.current]['path'] = task.get_path() + + def playbook_on_stats(self, stats): + timestamp(self) + self.current = None + results = self.stats.items() + # Sort the tasks by the specified sort + if self.sort_order is not None: + results = sorted( + self.stats.items(), + key=lambda x: x[1]['time'], + reverse=self.sort_order, + ) + results = results[:self.task_output_limit] + + self._display.display('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~' + ' Summary Information ' + '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~') + self._display.display( + '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~' + ' Elapsed Time: {} ' + '~~~~~~~~~~~~~~~~~~~~~~~~~~~~'.format( + secondsToStr(time.time() - self.start_time))) + + header = [ + '{:>36}'.format('UUID'), + '{:>10}'.format('Info'), + '{}'.format('Task Name'), + '{:>10}'.format('Run Time'), + ] + self._display.display(' | '.join(header)) + + for uuid, result in results: + line = [ + uuid, + u'{:>10}'.format('SUMMARY'), + result['name'], + u'{0:.02f}s'.format(result['time']) + ] + self._display.display(' | '.join(line))