| 1 |
# Copyright 2012, Tim Bielawa <[email protected]> |
| 2 |
# |
| 3 |
# This file is part of Ansible |
| 4 |
# |
| 5 |
# Ansible is free software: you can redistribute it and/or modify |
| 6 |
# it under the terms of the GNU General Public License as published by |
| 7 |
# the Free Software Foundation, either version 3 of the License, or |
| 8 |
# (at your option) any later version. |
| 9 |
# |
| 10 |
# Ansible is distributed in the hope that it will be useful, |
| 11 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
# GNU General Public License for more details. |
| 14 |
# |
| 15 |
# You should have received a copy of the GNU General Public License |
| 16 |
# along with Ansible. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
from __future__ import annotations |
| 18 |
|
| 19 |
import datetime |
| 20 |
import time |
| 21 |
|
| 22 |
from ansible.errors import AnsibleError, AnsiblePromptInterrupt, AnsiblePromptNoninteractive |
| 23 |
from ansible.module_utils.common.text.converters import to_text |
| 24 |
from ansible.module_utils.secrets import register_secret |
| 25 |
from ansible.plugins.action import ActionBase |
| 26 |
from ansible.utils.display import Display |
| 27 |
|
| 28 |
display = Display() |
| 29 |
|
| 30 |
|
| 31 |
class ActionModule(ActionBase): |
| 32 |
""" pauses execution for a length or time, or until input is received """ |
| 33 |
|
| 34 |
BYPASS_HOST_LOOP = True |
| 35 |
|
| 36 |
def run(self, tmp=None, task_vars=None): |
| 37 |
""" run the pause action module """ |
| 38 |
if task_vars is None: |
| 39 |
task_vars = dict() |
| 40 |
|
| 41 |
result = super(ActionModule, self).run(tmp, task_vars) |
| 42 |
del tmp # tmp no longer has any effect |
| 43 |
|
| 44 |
validation_result, new_module_args = self.validate_argument_spec( |
| 45 |
argument_spec={ |
| 46 |
'echo': {'type': 'bool', 'default': True}, |
| 47 |
'minutes': {'type': int}, # Don't break backwards compat, allow floats, by using int callable |
| 48 |
'seconds': {'type': int}, # Don't break backwards compat, allow floats, by using int callable |
| 49 |
'prompt': {'type': 'str'}, |
| 50 |
}, |
| 51 |
mutually_exclusive=( |
| 52 |
('minutes', 'seconds'), |
| 53 |
), |
| 54 |
) |
| 55 |
|
| 56 |
duration_unit = 'minutes' |
| 57 |
prompt = None |
| 58 |
seconds = None |
| 59 |
echo = new_module_args['echo'] |
| 60 |
echo_prompt = '' |
| 61 |
result.update(dict( |
| 62 |
changed=False, |
| 63 |
rc=0, |
| 64 |
stderr='', |
| 65 |
stdout='', |
| 66 |
start=None, |
| 67 |
stop=None, |
| 68 |
delta=None, |
| 69 |
echo=echo |
| 70 |
)) |
| 71 |
|
| 72 |
# Add a note saying the output is hidden if echo is disabled |
| 73 |
if not echo: |
| 74 |
echo_prompt = ' (output is hidden)' |
| 75 |
|
| 76 |
if new_module_args['prompt']: |
| 77 |
prompt = "[%s]\n%s%s:" % (self._task.get_name().strip(), new_module_args['prompt'], echo_prompt) |
| 78 |
else: |
| 79 |
# If no custom prompt is specified, set a default prompt |
| 80 |
prompt = "[%s]\n%s%s:" % (self._task.get_name().strip(), 'Press enter to continue, Ctrl+C to interrupt', echo_prompt) |
| 81 |
|
| 82 |
if new_module_args['minutes'] is not None: |
| 83 |
seconds = new_module_args['minutes'] * 60 |
| 84 |
elif new_module_args['seconds'] is not None: |
| 85 |
seconds = new_module_args['seconds'] |
| 86 |
duration_unit = 'seconds' |
| 87 |
|
| 88 |
######################################################################## |
| 89 |
# Begin the hard work! |
| 90 |
|
| 91 |
start = time.time() |
| 92 |
result['start'] = to_text(datetime.datetime.now()) |
| 93 |
result['user_input'] = b'' |
| 94 |
|
| 95 |
default_input_complete = None |
| 96 |
if seconds is not None: |
| 97 |
if seconds < 1: |
| 98 |
seconds = 1 |
| 99 |
|
| 100 |
# show the timer and control prompts |
| 101 |
display.display("Pausing for %d seconds%s" % (seconds, echo_prompt)) |
| 102 |
|
| 103 |
# show the prompt specified in the task |
| 104 |
if new_module_args['prompt']: |
| 105 |
display.display("(ctrl+C then 'C' = continue early, ctrl+C then 'A' = abort)\r") |
| 106 |
else: |
| 107 |
# corner case where enter does not continue, wait for timeout/interrupt only |
| 108 |
prompt = "(ctrl+C then 'C' = continue early, ctrl+C then 'A' = abort)\r" |
| 109 |
|
| 110 |
# don't complete on LF/CR; we expect a timeout/interrupt and ignore user input when a pause duration is specified |
| 111 |
default_input_complete = tuple() |
| 112 |
|
| 113 |
# Only echo input if no timeout is specified |
| 114 |
echo = seconds is None and echo |
| 115 |
|
| 116 |
user_input = b'' |
| 117 |
try: |
| 118 |
_user_input = display.prompt_until(prompt, private=not echo, seconds=seconds, complete_input=default_input_complete) |
| 119 |
except AnsiblePromptInterrupt: |
| 120 |
user_input = None |
| 121 |
except AnsiblePromptNoninteractive: |
| 122 |
if seconds is None: |
| 123 |
display.warning("Not waiting for response to prompt as stdin is not interactive") |
| 124 |
else: |
| 125 |
# wait specified duration |
| 126 |
time.sleep(seconds) |
| 127 |
else: |
| 128 |
if seconds is None: |
| 129 |
user_input = _user_input |
| 130 |
# user interrupt |
| 131 |
if user_input is None: |
| 132 |
prompt = "Press 'C' to continue the play or 'A' to abort \r" |
| 133 |
try: |
| 134 |
user_input = display.prompt_until(prompt, private=not echo, interrupt_input=(b'a',), complete_input=(b'c',)) |
| 135 |
except AnsiblePromptInterrupt: |
| 136 |
raise AnsibleError('user requested abort!') |
| 137 |
|
| 138 |
duration = time.time() - start |
| 139 |
result['stop'] = to_text(datetime.datetime.now()) |
| 140 |
result['delta'] = int(duration) |
| 141 |
|
| 142 |
if duration_unit == 'minutes': |
| 143 |
duration = round(duration / 60.0, 2) |
| 144 |
else: |
| 145 |
duration = round(duration, 2) |
| 146 |
result['stdout'] = "Paused for %s %s" % (duration, duration_unit) |
| 147 |
|
| 148 |
user_input = to_text(user_input, errors='surrogate_or_strict') |
| 149 |
|
| 150 |
if not echo and user_input: |
| 151 |
register_secret(user_input) |
| 152 |
|
| 153 |
result['user_input'] = user_input |
| 154 |
return result |