| 1 |
# Copyright (c) 2024, kurokobo <[email protected]> |
| 2 |
# Copyright (c) 2014, Michael DeHaan <[email protected]> |
| 3 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 4 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 5 |
|
| 6 |
from __future__ import annotations |
| 7 |
|
| 8 |
DOCUMENTATION = r""" |
| 9 |
name: timestamp |
| 10 |
type: stdout |
| 11 |
short_description: Adds simple timestamp for each header |
| 12 |
version_added: 9.0.0 |
| 13 |
description: |
| 14 |
- This callback adds simple timestamp for each header. |
| 15 |
author: kurokobo (@kurokobo) |
| 16 |
options: |
| 17 |
timezone: |
| 18 |
description: |
| 19 |
- Timezone to use for the timestamp in IANA time zone format. |
| 20 |
- For example V(America/New_York), V(Asia/Tokyo)). Ignored on Python < 3.9. |
| 21 |
ini: |
| 22 |
- section: callback_timestamp |
| 23 |
key: timezone |
| 24 |
env: |
| 25 |
- name: ANSIBLE_CALLBACK_TIMESTAMP_TIMEZONE |
| 26 |
type: string |
| 27 |
format_string: |
| 28 |
description: |
| 29 |
- Format of the timestamp shown to user in 1989 C standard format. |
| 30 |
- Refer to L(the Python documentation,https://docs.python.org/3/library/datetime.html#strftime-and-strptime-format-codes) |
| 31 |
for the available format codes. |
| 32 |
ini: |
| 33 |
- section: callback_timestamp |
| 34 |
key: format_string |
| 35 |
env: |
| 36 |
- name: ANSIBLE_CALLBACK_TIMESTAMP_FORMAT_STRING |
| 37 |
default: "%H:%M:%S" |
| 38 |
type: string |
| 39 |
seealso: |
| 40 |
- plugin: ansible.posix.profile_tasks |
| 41 |
plugin_type: callback |
| 42 |
description: >- |
| 43 |
You can use P(ansible.posix.profile_tasks#callback) callback plugin to time individual tasks and overall execution time |
| 44 |
with detailed timestamps. |
| 45 |
extends_documentation_fragment: |
| 46 |
- ansible.builtin.default_callback |
| 47 |
- ansible.builtin.result_format_callback |
| 48 |
""" |
| 49 |
|
| 50 |
|
| 51 |
import sys |
| 52 |
import types |
| 53 |
from datetime import datetime |
| 54 |
|
| 55 |
from ansible.module_utils.common.text.converters import to_text |
| 56 |
from ansible.plugins.callback.default import CallbackModule as Default |
| 57 |
from ansible.utils.display import get_text_width |
| 58 |
|
| 59 |
# Store whether the zoneinfo module is available |
| 60 |
_ZONEINFO_AVAILABLE = sys.version_info >= (3, 9) |
| 61 |
|
| 62 |
|
| 63 |
def get_datetime_now(tz): |
| 64 |
""" |
| 65 |
Returns the current timestamp with the specified timezone |
| 66 |
""" |
| 67 |
return datetime.now(tz=tz) |
| 68 |
|
| 69 |
|
| 70 |
def banner(self, msg, color=None, cows=True): |
| 71 |
""" |
| 72 |
Prints a header-looking line with cowsay or stars with length depending on terminal width (3 minimum) with trailing timestamp |
| 73 |
|
| 74 |
Based on the banner method of Display class from ansible.utils.display |
| 75 |
|
| 76 |
https://github.com/ansible/ansible/blob/4403519afe89138042108e237aef317fd5f09c33/lib/ansible/utils/display.py#L511 |
| 77 |
""" |
| 78 |
timestamp = get_datetime_now(self.timestamp_tzinfo).strftime(self.timestamp_format_string) |
| 79 |
timestamp_len = get_text_width(timestamp) + 1 # +1 for leading space |
| 80 |
|
| 81 |
msg = to_text(msg) |
| 82 |
if self.b_cowsay and cows: |
| 83 |
try: |
| 84 |
self.banner_cowsay(f"{msg} @ {timestamp}") |
| 85 |
return |
| 86 |
except OSError: |
| 87 |
self.warning("somebody cleverly deleted cowsay or something during the PB run. heh.") |
| 88 |
|
| 89 |
msg = msg.strip() |
| 90 |
try: |
| 91 |
star_len = self.columns - get_text_width(msg) - timestamp_len |
| 92 |
except OSError: |
| 93 |
star_len = self.columns - len(msg) - timestamp_len |
| 94 |
if star_len <= 3: |
| 95 |
star_len = 3 |
| 96 |
stars = "*" * star_len |
| 97 |
self.display(f"\n{msg} {stars} {timestamp}", color=color) |
| 98 |
|
| 99 |
|
| 100 |
class CallbackModule(Default): |
| 101 |
CALLBACK_VERSION = 2.0 |
| 102 |
CALLBACK_TYPE = "stdout" |
| 103 |
CALLBACK_NAME = "community.general.timestamp" |
| 104 |
|
| 105 |
def __init__(self): |
| 106 |
super().__init__() |
| 107 |
|
| 108 |
# Replace the banner method of the display object with the custom one |
| 109 |
self._display.banner = types.MethodType(banner, self._display) |
| 110 |
|
| 111 |
def set_options(self, task_keys=None, var_options=None, direct=None): |
| 112 |
super().set_options(task_keys=task_keys, var_options=var_options, direct=direct) |
| 113 |
|
| 114 |
# Store zoneinfo for specified timezone if available |
| 115 |
tzinfo = None |
| 116 |
if _ZONEINFO_AVAILABLE and self.get_option("timezone"): |
| 117 |
from zoneinfo import ZoneInfo |
| 118 |
|
| 119 |
tzinfo = ZoneInfo(self.get_option("timezone")) |
| 120 |
|
| 121 |
# Inject options into the display object |
| 122 |
self._display.timestamp_tzinfo = tzinfo |
| 123 |
self._display.timestamp_format_string = self.get_option("format_string") |