|
|
@ -18,14 +18,21 @@ from __future__ import (absolute_import, division, print_function)
|
|
|
|
__metaclass__ = type
|
|
|
|
__metaclass__ = type
|
|
|
|
|
|
|
|
|
|
|
|
import datetime
|
|
|
|
import datetime
|
|
|
|
|
|
|
|
import signal
|
|
|
|
import sys
|
|
|
|
import sys
|
|
|
|
|
|
|
|
import termios
|
|
|
|
import time
|
|
|
|
import time
|
|
|
|
|
|
|
|
import tty
|
|
|
|
from termios import tcflush, TCIFLUSH
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from ansible.errors import *
|
|
|
|
from ansible.errors import *
|
|
|
|
from ansible.plugins.action import ActionBase
|
|
|
|
from ansible.plugins.action import ActionBase
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class AnsibleTimeoutExceeded(Exception):
|
|
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def timeout_handler(signum, frame):
|
|
|
|
|
|
|
|
raise AnsibleTimeoutExceeded
|
|
|
|
|
|
|
|
|
|
|
|
class ActionModule(ActionBase):
|
|
|
|
class ActionModule(ActionBase):
|
|
|
|
''' pauses execution for a length or time, or until input is received '''
|
|
|
|
''' pauses execution for a length or time, or until input is received '''
|
|
|
|
|
|
|
|
|
|
|
@ -48,13 +55,9 @@ class ActionModule(ActionBase):
|
|
|
|
delta = None,
|
|
|
|
delta = None,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
# FIXME: not sure if we can get this info directly like this anymore?
|
|
|
|
|
|
|
|
#hosts = ', '.join(self.runner.host_set)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Is 'args' empty, then this is the default prompted pause
|
|
|
|
# Is 'args' empty, then this is the default prompted pause
|
|
|
|
if self._task.args is None or len(self._task.args.keys()) == 0:
|
|
|
|
if self._task.args is None or len(self._task.args.keys()) == 0:
|
|
|
|
pause_type = 'prompt'
|
|
|
|
pause_type = 'prompt'
|
|
|
|
#prompt = "[%s]\nPress enter to continue:\n" % hosts
|
|
|
|
|
|
|
|
prompt = "[%s]\nPress enter to continue:\n" % self._task.get_name().strip()
|
|
|
|
prompt = "[%s]\nPress enter to continue:\n" % self._task.get_name().strip()
|
|
|
|
|
|
|
|
|
|
|
|
# Are 'minutes' or 'seconds' keys that exist in 'args'?
|
|
|
|
# Are 'minutes' or 'seconds' keys that exist in 'args'?
|
|
|
@ -76,51 +79,68 @@ class ActionModule(ActionBase):
|
|
|
|
# Is 'prompt' a key in 'args'?
|
|
|
|
# Is 'prompt' a key in 'args'?
|
|
|
|
elif 'prompt' in self._task.args:
|
|
|
|
elif 'prompt' in self._task.args:
|
|
|
|
pause_type = 'prompt'
|
|
|
|
pause_type = 'prompt'
|
|
|
|
#prompt = "[%s]\n%s:\n" % (hosts, self._task.args['prompt'])
|
|
|
|
|
|
|
|
prompt = "[%s]\n%s:\n" % (self._task.get_name().strip(), self._task.args['prompt'])
|
|
|
|
prompt = "[%s]\n%s:\n" % (self._task.get_name().strip(), self._task.args['prompt'])
|
|
|
|
|
|
|
|
|
|
|
|
# I have no idea what you're trying to do. But it's so wrong.
|
|
|
|
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
|
|
|
|
# I have no idea what you're trying to do. But it's so wrong.
|
|
|
|
return dict(failed=True, msg="invalid pause type given. must be one of: %s" % ", ".join(self.PAUSE_TYPES))
|
|
|
|
return dict(failed=True, msg="invalid pause type given. must be one of: %s" % ", ".join(self.PAUSE_TYPES))
|
|
|
|
|
|
|
|
|
|
|
|
#vv("created 'pause' ActionModule: pause_type=%s, duration_unit=%s, calculated_seconds=%s, prompt=%s" % \
|
|
|
|
|
|
|
|
# (self.pause_type, self.duration_unit, self.seconds, self.prompt))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
########################################################################
|
|
|
|
########################################################################
|
|
|
|
# Begin the hard work!
|
|
|
|
# Begin the hard work!
|
|
|
|
|
|
|
|
|
|
|
|
start = time.time()
|
|
|
|
start = time.time()
|
|
|
|
result['start'] = str(datetime.datetime.now())
|
|
|
|
result['start'] = str(datetime.datetime.now())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# FIXME: this is all very broken right now, as prompting from the worker side
|
|
|
|
|
|
|
|
# is not really going to be supported, and actions marked as BYPASS_HOST_LOOP
|
|
|
|
|
|
|
|
# probably should not be run through the executor engine at all. Also, ctrl+c
|
|
|
|
|
|
|
|
# is now captured on the parent thread, so it can't be caught here via the
|
|
|
|
|
|
|
|
# KeyboardInterrupt exception.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
if not pause_type == 'prompt':
|
|
|
|
if seconds is not None:
|
|
|
|
print("(^C-c = continue early, ^C-a = abort)")
|
|
|
|
# setup the alarm handler
|
|
|
|
#print("[%s]\nPausing for %s seconds" % (hosts, seconds))
|
|
|
|
signal.signal(signal.SIGALRM, timeout_handler)
|
|
|
|
print("[%s]\nPausing for %s seconds" % (self._task.get_name().strip(), seconds))
|
|
|
|
signal.alarm(seconds)
|
|
|
|
time.sleep(seconds)
|
|
|
|
# show the prompt
|
|
|
|
|
|
|
|
print("Pausing for %d seconds" % seconds)
|
|
|
|
|
|
|
|
print("(ctrl+C then 'C' = continue early, ctrl+C then 'A' = abort)\r"),
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
# Clear out any unflushed buffered input which would
|
|
|
|
print("(press enter to continue, ctrl+C to abort)")
|
|
|
|
# otherwise be consumed by raw_input() prematurely.
|
|
|
|
|
|
|
|
#tcflush(sys.stdin, TCIFLUSH)
|
|
|
|
# save the attributes on the existing (duped) stdin so
|
|
|
|
result['user_input'] = raw_input(prompt.encode(sys.stdout.encoding))
|
|
|
|
# that we can restore them later after we set raw mode
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
fd = self._connection._new_stdin.fileno()
|
|
|
|
|
|
|
|
old_settings = termios.tcgetattr(fd)
|
|
|
|
|
|
|
|
tty.setraw(fd)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# flush the buffer to make sure no previous key presses
|
|
|
|
|
|
|
|
# are read in below
|
|
|
|
|
|
|
|
termios.tcflush(self._connection._new_stdin, termios.TCIFLUSH)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# read key presses and act accordingly
|
|
|
|
while True:
|
|
|
|
while True:
|
|
|
|
print('\nAction? (a)bort/(c)ontinue: ')
|
|
|
|
key_pressed = self._connection._new_stdin.read(1)
|
|
|
|
c = getch()
|
|
|
|
if pause_type in ('minutes', 'seconds'):
|
|
|
|
if c == 'c':
|
|
|
|
if key_pressed == '\x03':
|
|
|
|
# continue playbook evaluation
|
|
|
|
key_pressed = self._connection._new_stdin.read(1)
|
|
|
|
break
|
|
|
|
if key_pressed == 'a':
|
|
|
|
elif c == 'a':
|
|
|
|
raise KeyboardInterrupt
|
|
|
|
# abort further playbook evaluation
|
|
|
|
elif key_pressed == 'c':
|
|
|
|
raise ae('user requested abort!')
|
|
|
|
break
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
if key_pressed == '\x03':
|
|
|
|
|
|
|
|
raise KeyboardInterrupt
|
|
|
|
|
|
|
|
elif key_pressed == '\r':
|
|
|
|
|
|
|
|
break
|
|
|
|
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
|
|
|
|
# cancel the previously set alarm signal
|
|
|
|
|
|
|
|
if seconds is not None:
|
|
|
|
|
|
|
|
signal.alarm(0)
|
|
|
|
|
|
|
|
raise AnsibleError('user requested abort!')
|
|
|
|
|
|
|
|
except AnsibleTimeoutExceeded:
|
|
|
|
|
|
|
|
# this is the exception we expect when the alarm signal
|
|
|
|
|
|
|
|
# fires, so we simply ignore it to move into the cleanup
|
|
|
|
|
|
|
|
pass
|
|
|
|
finally:
|
|
|
|
finally:
|
|
|
|
|
|
|
|
# cleanup and save some information
|
|
|
|
|
|
|
|
# restore the old settings for the duped stdin fd
|
|
|
|
|
|
|
|
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
|
|
|
|
|
|
|
|
|
|
|
|
duration = time.time() - start
|
|
|
|
duration = time.time() - start
|
|
|
|
result['stop'] = str(datetime.datetime.now())
|
|
|
|
result['stop'] = str(datetime.datetime.now())
|
|
|
|
result['delta'] = int(duration)
|
|
|
|
result['delta'] = int(duration)
|
|
|
|