From 35fc3914691636970b2959ff1b5b0c6fb999e03c Mon Sep 17 00:00:00 2001 From: Markus Frei Date: Mon, 16 Nov 2026 08:55:21 +0100 Subject: [PATCH] feat(plugin:dashboard): show the progress and ETA of the play, count ok results per host The progress bar numbers the tasks the way the PlayIterator sees them: play.compile() with the tags filter applied, without meta tasks (they never fire v2_playbook_on_task_start) and without rescue sections. A task of an include_tasks or include_role is found through its parents, which lead to the include, so the include is one step. Verified against ansible-core 2.16, 2.18 and 2.22. The task counter shows "ok 13/22/35" (ok results, hosts still at work, hosts) instead of a separate pending counter. The counter columns are aligned to the widest value, so large run totals no longer run into the next label. The host list gets an O column, and LAST is renamed to LAST WORST. --- CHANGELOG.md | 4 +- README.md | 19 +- plugins/callback/dashboard.py | 259 ++++++++++++++---- tests/unit/plugins/callback/test_dashboard.py | 114 +++++++- 4 files changed, 326 insertions(+), 70 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b589accf5..7a38b32fb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] -**Highlights:** Apache Solr runs on Debian, Ubuntu and RHEL 10 and supports Solr 9.11. Hosts that deploy the OWASP Core Rule Set with apache_httpd need `apache_httpd__mod_security_coreruleset_version` in their inventory, since the role no longer picks a release on its own and verifies the download against a checksum. The `systemd_unit` role also manages socket units, so a host can forward a TCP port with `systemd-socket-proxyd` without installing a proxy server. Hosts set up with `setup_basic` follow more of the CIS Benchmarks out of the box: root can no longer log in over SSH, USB storage is blocked, a sysctl hardening baseline is applied, and the cron configuration is readable by root only. Before updating, make sure Ansible logs in with an unprivileged user, and allow USB storage on hosts that need it. Nextcloud keeps the apps and app settings that the admin changed in the web interface, and JPEG previews work again. Large runs are easier to follow with the `linuxfabrik.lfops.compact` stdout callback, which shows one block per task, a progress bar per host, and only what changed. The `linuxfabrik.lfops.dashboard` stdout callback shows a run on one fixed screen, with the hosts sorted by severity. `duba` backups no longer fail on a small `/tmp`, `duba` throttles itself instead. +**Highlights:** Apache Solr runs on Debian, Ubuntu and RHEL 10 and supports Solr 9.11. Hosts that deploy the OWASP Core Rule Set with apache_httpd need `apache_httpd__mod_security_coreruleset_version` in their inventory, since the role no longer picks a release on its own and verifies the download against a checksum. The `systemd_unit` role also manages socket units, so a host can forward a TCP port with `systemd-socket-proxyd` without installing a proxy server. Hosts set up with `setup_basic` follow more of the CIS Benchmarks out of the box: root can no longer log in over SSH, USB storage is blocked, a sysctl hardening baseline is applied, and the cron configuration is readable by root only. Before updating, make sure Ansible logs in with an unprivileged user, and allow USB storage on hosts that need it. Nextcloud keeps the apps and app settings that the admin changed in the web interface, and JPEG previews work again. Large runs are easier to follow with the `linuxfabrik.lfops.compact` stdout callback, which shows one block per task, a progress bar per host, and only what changed. The `linuxfabrik.lfops.dashboard` stdout callback shows a run on one fixed screen, with a progress bar, the estimated remaining time and the hosts sorted by severity. `duba` backups no longer fail on a small `/tmp`, `duba` throttles itself instead. ### Breaking Changes @@ -93,7 +93,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * **role:freeipa_server**: The `freeipa_server__healthcheck_*` variables set the certificate warning period, the timeout per check and the excludes of `ipa-healthcheck`, by default with 30 instead of 10 seconds per check. * **role:freeipa_server**: Installs `ipa-healthcheck` and runs it right after the installation and then daily at 06:xx, at a fixed minute per host or after the next boot if the host was down at that time, so its report in `/var/log/ipa/healthcheck/healthcheck.log` is ready for monitoring. * **execution-environment**: The image carries its LFOps version in the `LFOPS_EE_VERSION` environment variable and the `org.opencontainers.image.version` label, and the dashboard callback shows it. -* **plugin:dashboard**: The `linuxfabrik.lfops.dashboard` stdout callback shows a run on one fixed screen with counters and the hosts sorted by severity, and the details of the tasks with changes, warnings or failures at the end. +* **plugin:dashboard**: The `linuxfabrik.lfops.dashboard` stdout callback shows a run on one fixed screen with a progress bar, counters and the hosts sorted by severity, and the details of the tasks with changes, warnings or failures at the end. * **plugin:compact**: The `linuxfabrik.lfops.compact` stdout callback shortens the output of large runs to one block per task, with a progress bar per host, one line each for the ok and the skipped hosts, the diff or module output of the changed hosts and the warnings, each grouped by identical text, and a report of the distinct changes, warnings and errors by severity at the end. * **role:rpm, playbook:rpm, playbook:setup_basic**: Checks the RPM database on RHEL 8, 9 and 10 before the first package task, aborts on a damaged one instead of failing later with `rpmdb open failed`, and on RHEL 8 repairs a database that a killed rpm process left unusable, keeping all packages. * **role:dnf, playbook:dnf**: Sets options in `/etc/dnf/dnf.conf` on RHEL 8, 9 and 10, by default `minrate=200k` and `timeout=15`, so DNF drops slow mirrors sooner. Hosts without DNF 4 are left alone. diff --git a/README.md b/README.md index e5e0ec637..ffc5cceda 100644 --- a/README.md +++ b/README.md @@ -382,24 +382,27 @@ Enable it with `stdout_callback = linuxfabrik.lfops.compact` in the `ansible.cfg The goal of the `linuxfabrik.lfops.dashboard` stdout callback is a live view of a run, similar to how glances shows the load of a system: always visible, without scrolling, and adapting to the size of the terminal. -It shows the run on one fixed screen that is updated in place instead of scrolling: the current role and task, counters of the results of the current task and of the whole run, and the hosts, sorted by their most severe result (fatal, warning, changed, ignored, ok) and then by name, each with its counts of fatal (F), warning (W) and changed (C) results and its last notable result, marked with the letter of its kind. A host with only ok results shows its last ok result, hosts with only skipped results are counted, not listed: +It shows the run on one fixed screen that is updated in place instead of scrolling: the current role and task, a progress bar of the play, counters of the results of the current task and of the whole run, with the ok results of the task followed by the hosts still at work on it and the number of hosts (`ok 20/5/35`), and the hosts, sorted by their most severe result (fatal, warning, changed, ignored, ok) and then by name. Each host comes with its counts of fatal (F), warning (W), changed (C) and ok (O) results and its last notable result (LAST WORST), marked with the letter of its kind. A rising O count shows that a host is still at work while LAST WORST stays the same. A host with only ok results shows its last ok result, hosts with only skipped results are counted, not listed: ```text linuxfabrik.lfops.setup_basic 35 hosts 00:12:34 LFOps main@3e8a7b75, 2 uncommitted ansible-core 2.16.19 mounted from /h/u/lfops Role: linuxfabrik.lfops.firewall Task: Deploy the firewall rules + step 140/421 (33%) ETA 00:15:12 ────────────────────────────────────────────────────────────────────────────────── -Task ok 20 changed 3 skipped 5 fatal 1 unreachable 0 pending 6 -Run ok 1840 changed 61 warning 12 fatal 1 unreachable 0 +Task ok 20/5/35 changed 3 skipped 5 fatal 1 unreachable 0 ignored 0 +Run ok 1840 changed 61 warning 12 fatal 1 unreachable 0 ────────────────────────────────────────────────────────────────────────────────── -HOST F W C LAST -web-02 1 0 2 F firewall - Deploy the firewall rules: ... -web-01 0 2 5 W lvm - pvcreate / pvresize: Rescan path does not exist ... -db-01 0 0 7 C firewall - Deploy the firewall rules -app-01 0 0 0 O firewall - Deploy the firewall rules +HOST F W C O LAST WORST +web-02 1 0 2 48 F firewall - Deploy the firewall rules: ... +web-01 0 2 5 61 W lvm - pvcreate / pvresize: Rescan path does not exist ... +db-01 0 0 7 59 C firewall - Deploy the firewall rules +app-01 0 0 0 66 O firewall - Deploy the firewall rules ``` +The progress bar counts the tasks of the play that are known when it starts, after `--tags` and `--skip-tags` are applied, and is drawn in reverse video on a color terminal. The estimated remaining time (ETA) assumes that the steps left take as long on average as the steps so far, so it gets more accurate towards the end of the play. A task included with `include_tasks` or `include_role` is one step, so the bar stands still while the tasks it includes run. Handlers and `rescue` sections are not counted. The bar measures tasks, not time, and starts over with every batch of `serial`. + At the end of the run, the details follow in the format of the [compact output](#compact-output), but only for the tasks with changes, warnings or failures, then the recap and the report. Without a terminal (log file, pipe, CI), with `--verbose`, or on a terminal smaller than 65 columns and 12 lines, it behaves exactly like `linuxfabrik.lfops.compact`. Enable it like the compact output, with `linuxfabrik.lfops.dashboard` as the stdout callback. See `ansible-doc --type callback linuxfabrik.lfops.dashboard` for details. ### LFOps-wide Variables diff --git a/plugins/callback/dashboard.py b/plugins/callback/dashboard.py index 32d783125..2dbf8a621 100644 --- a/plugins/callback/dashboard.py +++ b/plugins/callback/dashboard.py @@ -20,6 +20,7 @@ from collections import Counter from ansible import constants as C +from ansible.playbook.block import Block from ansible.playbook.task_include import TaskInclude from ansible.release import __version__ as ANSIBLE_CORE_VERSION from ansible.utils.color import ANSIBLE_COLOR, stringc @@ -43,9 +44,10 @@ version_added: "10.0.0" short_description: Fixed full-screen view of a run, details at the end description: - - Shows the run on one fixed screen that is updated in place instead of scrolling. At the top the playbook, the number of hosts and the elapsed time, the current role and task, then counters of the results of the current task and of the whole run. + - Shows the run on one fixed screen that is updated in place instead of scrolling. At the top the playbook, the number of hosts and the elapsed time, the current role and task, a progress bar of the play with the estimated remaining time, then counters of the results of the current task and of the whole run. The ok counter of the task is followed by the number of hosts still at work on the task and the number of hosts, for example C(ok 13/22/35). - Below the title, where LFOps comes from - its version, or for a git checkout the branch, the commit and the number of uncommitted files, and the source path, shortened to the initials of its parent directories, when the checkout is mounted into an execution environment - and the ansible-core version. An C(LFOPS_EE_VERSION) environment variable is shown as the EE version. - - Below, the hosts are listed, sorted by their most severe result (fatal, warning, changed, ignored, ok), then by name, with their counters and their last notable result, or the last ok result for a host without one. Hosts with only skipped results are counted, not listed. + - The progress bar counts the tasks of the play that are known when it starts, after C(--tags) and C(--skip-tags) are applied. A task included with C(include_tasks) or C(include_role) is one step, the bar stands still while the tasks it includes run. Tasks in C(rescue) sections and handlers are not counted. The bar counts tasks, not time, and starts over with every batch of C(serial). The estimated remaining time assumes that the steps left take as long on average as the steps so far. + - Below, the hosts are listed, sorted by their most severe result (fatal, warning, changed, ignored, ok), then by name, with their counts of fatal (F), warning (W), changed (C) and ok (O) results and their last notable result (LAST WORST), or the last ok result for a host without one. Hosts with only skipped results are counted, not listed. - At the end of the run, the screen is left and the details follow in the format of C(linuxfabrik.lfops.compact), but only for the tasks with changes, warnings or failures. Then the recap and the report of the distinct messages by severity. - Without a terminal (log file, pipe, CI) or with C(--verbose), the plugin behaves exactly like C(linuxfabrik.lfops.compact). author: @@ -157,12 +159,75 @@ def lfops_origin(root, mountinfo='/proc/self/mountinfo'): return ', '.join(parts), source +def play_steps(play): + """Number the tasks of a play that ansible-core will start, the way its + PlayIterator sees them: play.compile() with --tags and --skip-tags applied. + Return a dict of task UUID -> step, starting at 1. + + Left out are the meta tasks, which never fire v2_playbook_on_task_start, and the + rescue sections, which only run on a failure. Included tasks are only known at + run time; the include itself is one step. A role listed twice with the same + parameters runs once, its tasks keep the step of their first occurrence. + Verified against ansible-core 2.16, 2.18 and 2.22 (executor/play_iterator.py, + plugins/strategy/linear.py). + """ + all_vars = play.get_variable_manager().get_vars(play=play) + steps = {} + + def walk(tasks): + for task in tasks or []: + if isinstance(task, Block): + walk(task.block) + walk(task.always) + elif task.action not in C._ACTION_META: + steps.setdefault(task._uuid, len(steps) + 1) + + for block in play.compile(): + walk([block.filter_tagged_tasks(all_vars)]) + return steps + + +def hms(seconds): + """Seconds as "hh:mm:ss".""" + seconds = int(seconds) + return f'{seconds // 3600:02d}:{seconds % 3600 // 60:02d}:{seconds % 60:02d}' + + +def eta(step, total, play_start, step_start, now): + """The remaining time of a play, or None before the first step is done. The + time the finished steps took per step, times the steps left, less the time the + running step already took. It gets more accurate towards the end of the play. + """ + done = step - 1 + if done < 1: + return None + per_step = (step_start - play_start) / done + return max(per_step * (total - done) - (now - step_start), 0) + + +def progress_bar(step, total, width, color=True, remaining=None): + """A bar of `width` characters starting with "step 7/21 (33%)" and the + remaining time, if known. With color, the done part is shown in reverse video, + else filled with "#". + """ + label = f' step {step}/{total} ({step * 100 // total}%)' + if remaining is not None: + label += f' ETA {hms(remaining)}' + label += ' ' + done = width * step // total + if not color: + bar = '#' * done + ' ' * (width - done) + return (label + bar[len(label) :])[:width] + line = label.ljust(width)[:width] + return f'\033[7m{line[:done]}\033[0m{line[done:]}' + + class CallbackModule(Compact): CALLBACK_VERSION = 2.0 CALLBACK_TYPE = 'stdout' CALLBACK_NAME = 'linuxfabrik.lfops.dashboard' - # kind -> letter and color in the LAST column of the host list + # kind -> letter and color in the LAST WORST column of the host list _KIND_LETTERS = { # noqa: RUF012 'changed': ('C', C.COLOR_CHANGED), 'failed': ('F', C.COLOR_ERROR), @@ -182,6 +247,9 @@ class CallbackModule(Compact): ('ok', C.COLOR_OK), ) + # a mid gray of the 256 color palette, readable on dark and light backgrounds + _GRAY = 'gray12' + # at most this many redraws per second, the last state is always drawn at the # end of a task _REDRAW_INTERVAL = 0.1 @@ -208,6 +276,12 @@ def __init__(self): self._play_hosts = set() self._batch_hosts = set() self._dead_hosts = set() + # task UUID -> step of the running play, see play_steps(), the step of the + # running task, and when the play and that step started, for the ETA + self._steps = {} + self._step = 0 + self._play_start = time.monotonic() + self._step_start = self._play_start # where the plugin, and with it LFOps, comes from, see lfops_origin() self._origin = None # output of the running task and the details for the end of the run @@ -285,13 +359,47 @@ def _fit(segments, width): out.append(text) return ''.join(out) - def _counter_segments(self, label, counts, kinds): - segments = [(f'{label:<7}', None, True)] - for kind, color in kinds: - value = counts.get(kind, 0) - segments.append((f'{kind} ', None, False)) - segments.append((f'{value:<5}', color if value else None, bool(value))) - return segments + @staticmethod + def _counter_rows(rows): + """Lay out rows of counters like "Task ok 3/32 changed 0 ...". A row is a + label and a list of (kind, value) cells, a value a list of (text, color, + bold) segments. The values of a column are padded to the widest of them + plus one space, so the columns line up, also with numbers of any length. + """ + widths = {} + for _, cells in rows: + for column, (_, value) in enumerate(cells): + length = sum(len(text) for text, _, _ in value) + widths[column] = max(widths.get(column, 4), length) + lines = [] + for label, cells in rows: + segments = [(f'{label:<7}', None, True)] + for column, (kind, value) in enumerate(cells): + length = sum(len(text) for text, _, _ in value) + segments.append((f'{kind} ', None, False)) + segments.extend(value) + segments.append((' ' * (widths[column] - length + 1), None, False)) + lines.append(segments) + return lines + + @staticmethod + def _counter_cells(counts, kinds): + """(kind, value) cells for _counter_rows(), a count in the color of its + kind and in bold, a zero plain. + """ + return [ + ( + kind, + [ + ( + str(counts.get(kind, 0)), + color if counts.get(kind) else None, + bool(counts.get(kind)), + ) + ], + ) + for kind, color in kinds + ] def _draw(self, force=False): if not self._tui: @@ -307,8 +415,7 @@ def _draw(self, force=False): width = columns - 1 screen = [] - elapsed = int(now - self._start) - elapsed = f'{elapsed // 3600:02d}:{elapsed % 3600 // 60:02d}:{elapsed % 60:02d}' + elapsed = hms(now - self._start) count = len(self._play_hosts) right = f'{count} host{"" if count == 1 else "s"} {elapsed}' title = self._play_line.split(': ', 1)[-1] @@ -355,47 +462,60 @@ def _draw(self, force=False): ) else: screen.append('') + if self._steps: + screen.append( + progress_bar( + self._step, + len(self._steps), + width, + ANSIBLE_COLOR, + eta( + self._step, + len(self._steps), + self._play_start, + self._step_start, + now, + ), + ) + ) + else: + screen.append('') screen.append('─' * width) - task_counts = Counter(self._task_counts) + task_cells = self._counter_cells( + self._task_counts, + ( + ('ok', C.COLOR_OK), + ('changed', C.COLOR_CHANGED), + ('skipped', C.COLOR_SKIP), + ('fatal', C.COLOR_ERROR), + ('unreachable', C.COLOR_UNREACHABLE), + ('ignored', C.COLOR_WARN), + ), + ) if self._batch_hosts: + # "ok 13/22/35": the ok results, the hosts still at work on the task, + # and the hosts of the play or of the batch with "serial" alive = len(self._batch_hosts - self._dead_hosts) - # hosts that are still at work on the task - task_counts['pending'] = max(alive - len(self._task_hosts), 0) - screen.append( - self._fit( - self._counter_segments( - 'Task', - task_counts, - ( - ('ok', C.COLOR_OK), - ('changed', C.COLOR_CHANGED), - ('skipped', C.COLOR_SKIP), - ('fatal', C.COLOR_ERROR), - ('unreachable', C.COLOR_UNREACHABLE), - ('ignored', C.COLOR_WARN), - ('pending', None), - ), - ), - width, - ) - ) - screen.append( - self._fit( - self._counter_segments( - 'Run', - self._run_counts, - ( - ('ok', C.COLOR_OK), - ('changed', C.COLOR_CHANGED), - ('warning', C.COLOR_WARN), - ('fatal', C.COLOR_ERROR), - ('unreachable', C.COLOR_UNREACHABLE), - ), - ), - width, + pending = max(alive - len(self._task_hosts), 0) + task_cells[0][1].extend( + [ + (f'/{pending}', self._GRAY, False), + (f'/{len(self._batch_hosts)}', self._GRAY, True), + ] ) + run_cells = self._counter_cells( + self._run_counts, + ( + ('ok', C.COLOR_OK), + ('changed', C.COLOR_CHANGED), + ('warning', C.COLOR_WARN), + ('fatal', C.COLOR_ERROR), + ('unreachable', C.COLOR_UNREACHABLE), + ), ) + for segments in self._counter_rows([('Task', task_cells), ('Run', run_cells)]): + screen.append(self._fit(segments, width)) screen.append('─' * width) hosts = sorted(self._host_counts, key=lambda h: (self._host_severity(h), h)) @@ -404,7 +524,8 @@ def _draw(self, force=False): self._fit( [ ( - f'{"HOST":<{name_width}} {"F":>3} {"W":>3} {"C":>3} LAST', + f'{"HOST":<{name_width}} {"F":>3} {"W":>3} {"C":>3}' + f' {"O":>4} LAST WORST', None, True, ) @@ -422,12 +543,21 @@ def _draw(self, force=False): color = self._HOST_SEVERITY[self._host_severity(host)][1] # every count in the color of its own kind, zeros plain segments = [(f'{host[:name_width]:<{name_width}} ', color, bool(fatal))] - for value, value_color in ( - (fatal, C.COLOR_ERROR), - (counts['warning'], C.COLOR_WARN), - (counts['changed'], C.COLOR_CHANGED), + # the ok count shows that a host is still at work while LAST WORST + # stays the same + for value, value_color, value_width in ( + (fatal, C.COLOR_ERROR, 3), + (counts['warning'], C.COLOR_WARN, 3), + (counts['changed'], C.COLOR_CHANGED, 3), + (counts['ok'], C.COLOR_OK, 4), ): - segments.append((f' {value:>3}', value_color if value else None, False)) + segments.append( + ( + f' {value:>{value_width}}', + value_color if value else None, + False, + ) + ) segments.append((' ', None, False)) kind, text = self._host_last.get(host) or self._host_last_ok.get( host, (None, '') @@ -511,6 +641,18 @@ def _print_task_banner(self, task): return self._header = self._banner_parts(task) self._last_task_banner = task._uuid + # a task of an include_tasks or include_role is found through its parents, + # which lead to the include; handlers are not found and keep the step + node = task + while node is not None: + step = self._steps.get(node._uuid) + if step: + # only forward, e.g. when a failure skips to an "always" section + if step > self._step: + self._step = step + self._step_start = time.monotonic() + break + node = getattr(node, '_parent', None) self._draw(force=True) def _flush_hosts(self): @@ -536,7 +678,7 @@ def _host_severity(self, host): return len(self._HOST_SEVERITY) - 1 def _last_text(self, task, message=None): - """The LAST column of the host list: " - : ".""" + """The LAST WORST column of the host list: " - : ".""" source, name = self._source_and_name(task) # "namespace.collection.role" -> "role", the screen is narrow and the report # at the end names it in full @@ -581,6 +723,15 @@ def v2_playbook_on_play_start(self, play): self._batch_hosts = set() self._dead_hosts = set() self._start_tui() + self._steps = {} + self._step = 0 + self._play_start = time.monotonic() + self._step_start = self._play_start + if self._tui: + try: + self._steps = play_steps(play) + except Exception: # only for the progress bar, which is then left out + pass # the play line is part of the screen header, not of the details, and was # printed above the screen already self._capture = [] diff --git a/tests/unit/plugins/callback/test_dashboard.py b/tests/unit/plugins/callback/test_dashboard.py index 598275253..ff228060c 100644 --- a/tests/unit/plugins/callback/test_dashboard.py +++ b/tests/unit/plugins/callback/test_dashboard.py @@ -20,6 +20,7 @@ import importlib.util import os import pty +import re import shutil import struct import subprocess # nosec B404 @@ -49,6 +50,7 @@ _module = importlib.util.module_from_spec(_spec) _spec.loader.exec_module(_module) lfops_origin = _module.lfops_origin +progress_bar = _module.progress_bar shorten_path = _module.shorten_path _SCREEN_ON = '\x1b[?1049h' @@ -69,9 +71,10 @@ """ -def _run(columns=None, playbook=_PLAYBOOK): - """Run the playbook, on a pseudo-terminal with `columns` if given. Return the - exit code and the output. +def _run(columns=None, playbook=_PLAYBOOK, files=None): + """Run the playbook, on a pseudo-terminal with `columns` if given, with the + additional `files` (name -> content) next to it. Return the exit code and the + output. """ assert _ANSIBLE_PLAYBOOK, 'ansible-playbook not found' # nosec B101 tmp = tempfile.mkdtemp(prefix='lfops_dashboard_') @@ -86,6 +89,7 @@ def _run(columns=None, playbook=_PLAYBOOK): + f" ansible_python_interpreter: '{sys.executable}'\n" ), 'playbook.yml': playbook, + **(files or {}), } for name, content in files.items(): with open(os.path.join(tmp, name), 'w') as f: @@ -149,20 +153,25 @@ def test_screen_shows_counters_and_the_hosts_needing_attention(self): self.assertIn('HOST', screen) self.assertIn('Task ok', screen) self.assertIn('Run ok', screen) - self.assertIn('pending', screen) + # the ok results of the task, the hosts still at work on it, all hosts + self.assertRegex(screen, r'Task ok \d/\d/4 +changed ') + self.assertIn('Task ok 0/4/4 ', screen) self.assertIn('ansible-core ', screen) self.assertIn('LFOps ', screen) self.assertIn('t-h01', screen) - self.assertIn('F W C LAST', screen) + self.assertIn('F W C O LAST WORST', screen) # the last notable result with the letter of its kind, the playbook # "linuxfabrik.lfops.test" shortened to "test" self.assertIn('C test - Some changes', screen) # hosts with only ok results are listed with their last ok result self.assertIn('O test - Some changes', screen) + # F, W, C and O counts: one changed and one ok, or two ok results + self.assertRegex(screen, r't-h01\s+0\s+0\s+1\s+1 C test - Some changes') + self.assertRegex(screen, r't-h03\s+0\s+0\s+0\s+2 O test - Some changes') def test_hosts_sorted_by_severity_then_name(self): # the last drawn host list, rows start with the host name - screen = self.raw[self.raw.rindex('LAST') : self.raw.rindex(_SCREEN_OFF)] + screen = self.raw[self.raw.rindex('LAST WORST') : self.raw.rindex(_SCREEN_OFF)] order = sorted( ('t-h01', 't-h02', 't-h03', 't-h04'), key=lambda host: screen.index(host) ) @@ -191,6 +200,99 @@ def test_header_counts_all_hosts_of_the_play(self): self.assertNotIn('1 host ', raw) +class TestProgress(unittest.TestCase): + def test_includes_are_one_step_rescue_not_counted(self): + playbook = _PLAYBOOK.replace( + " - name: 'Some changes'\n", + " - name: 'Include'\n" + " ansible.builtin.include_tasks: 'included.yml'\n" + '\n' + ' - block:\n' + " - name: 'In a block'\n" + " ansible.builtin.command: 'true'\n" + ' changed_when: false\n' + ' rescue:\n' + " - name: 'Rescue'\n" + " ansible.builtin.command: 'true'\n" + '\n' + " - name: 'Some changes'\n", + ) + included = ( + "- name: 'Included 1'\n" + " ansible.builtin.command: 'true'\n" + ' changed_when: false\n' + "- name: 'Included 2'\n" + " ansible.builtin.command: 'true'\n" + ' changed_when: false\n' + ) + returncode, raw = _run( + columns=100, playbook=playbook, files={'included.yml': included} + ) + self.assertEqual(returncode, 0, raw) + screen = raw[raw.index(_SCREEN_ON) : raw.rindex(_SCREEN_OFF)] + steps = list(dict.fromkeys(re.findall(r'step (\d+)/(\d+)', screen))) + # All ok, Include (with its two tasks), In a block, Some changes + self.assertEqual( + steps, [('0', '4'), ('1', '4'), ('2', '4'), ('3', '4'), ('4', '4')] + ) + self.assertIn(' step 4/4 (100%) ', screen) + + def test_bar_without_color(self): + bar = progress_bar(7, 21, 80, color=False) + self.assertEqual(bar, ' step 7/21 (33%) ' + '#' * 9 + ' ' * 54) + + def test_bar_with_color_reverses_the_done_part(self): + bar = progress_bar(7, 21, 40, color=True) + self.assertTrue(bar.startswith('\033[7m')) + self.assertEqual( + bar.replace('\033[7m', '').replace('\033[0m', ''), + ' step 7/21 (33%) '.ljust(40), + ) + + def test_bar_with_remaining_time(self): + bar = progress_bar(7, 21, 60, color=False, remaining=3725) + self.assertTrue(bar.startswith(' step 7/21 (33%) ETA 01:02:05 ')) + + def test_bar_cut_to_width(self): + self.assertEqual(len(progress_bar(1, 3, 10, color=False)), 10) + + def test_eta_from_the_finished_steps(self): + # 6 steps done in 60 s, 15 left at 10 s each, 4 s into step 7 + self.assertEqual(_module.eta(7, 21, 100, 160, 164), 146) + # no step done yet + self.assertIsNone(_module.eta(1, 21, 100, 100, 130)) + # a step that takes longer than estimated does not go below zero + self.assertEqual(_module.eta(21, 21, 0, 200, 900), 0) + + +class TestCounterRows(unittest.TestCase): + def test_columns_aligned_and_apart_for_long_numbers(self): + rows = _module.CallbackModule._counter_rows( + [ + ( + 'Task', + [ + ( + 'ok', + [ + ('3', None, True), + ('/2', None, False), + ('/32', None, True), + ], + ) + ], + ), + ( + 'Run', + [('ok', [('11044', None, True)]), ('changed', [('5', None, True)])], + ), + ] + ) + task, run = (''.join(text for text, _, _ in row) for row in rows) + self.assertEqual(run, 'Run ok 11044 changed 5 ') + self.assertEqual(task, 'Task ok 3/2/32 ') + + class TestShortenPath(unittest.TestCase): def test_parents_reduced_to_their_initial(self): self.assertEqual(