mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-04-26 12:21:26 -07:00
Provide a way to explicitly invoke the debugger (#34006)
* Provide a way to explicitly invoke the debugger with in the debug strategy * Merge the debugger strategy into StrategyBase * Fix some logic, pin to a single result * Make redo also continue * Make sure that if the debug closure doesn't need to process the result, that we still return it * Fix failing tests for the strategy * Clean up messages from debugger and exit code to match bin/ansible * Move the FieldAttribute higher, to apply at different levels * make debugger a string, expand logic * Better host state rollbacks * More explicit debugger prompt * ENABLE_TASK_DEBUGGER should be boolean, and better docs * No bare except, add pprint, alias h, vars to task_vars * _validate_debugger can ignore non-string, that can be caught later * Address issue if there were no previous tasks/state, and use the correct key * Update docs for changes to the debugger * Guard against a stat going negative through use of decrement * Add a few notes about using the debugger on the free strategy * Add changelog entry for task debugger * Add a few versionadded indicators and a note about vars -> task_vars
This commit is contained in:
parent
e802b769e6
commit
d1846425db
9 changed files with 394 additions and 176 deletions
|
@ -28,153 +28,10 @@ import cmd
|
|||
import pprint
|
||||
import sys
|
||||
|
||||
from ansible.module_utils.six.moves import reduce
|
||||
from ansible.plugins.strategy.linear import StrategyModule as LinearStrategyModule
|
||||
|
||||
try:
|
||||
from __main__ import display
|
||||
except ImportError:
|
||||
from ansible.utils.display import Display
|
||||
display = Display()
|
||||
|
||||
|
||||
class NextAction(object):
|
||||
""" The next action after an interpreter's exit. """
|
||||
REDO = 1
|
||||
CONTINUE = 2
|
||||
EXIT = 3
|
||||
|
||||
def __init__(self, result=EXIT):
|
||||
self.result = result
|
||||
|
||||
|
||||
class StrategyModule(LinearStrategyModule):
|
||||
def __init__(self, tqm):
|
||||
self.curr_tqm = tqm
|
||||
super(StrategyModule, self).__init__(tqm)
|
||||
|
||||
def _queue_task(self, host, task, task_vars, play_context):
|
||||
self.curr_host = host
|
||||
self.curr_task = task
|
||||
self.curr_task_vars = task_vars
|
||||
self.curr_play_context = play_context
|
||||
|
||||
super(StrategyModule, self)._queue_task(host, task, task_vars, play_context)
|
||||
|
||||
def _process_pending_results(self, iterator, one_pass=False, max_passes=None):
|
||||
if not hasattr(self, "curr_host"):
|
||||
return super(StrategyModule, self)._process_pending_results(iterator, one_pass, max_passes)
|
||||
|
||||
prev_host_state = iterator.get_host_state(self.curr_host)
|
||||
results = super(StrategyModule, self)._process_pending_results(iterator, one_pass)
|
||||
|
||||
while self._need_debug(results):
|
||||
next_action = NextAction()
|
||||
dbg = Debugger(self, results, next_action)
|
||||
dbg.cmdloop()
|
||||
|
||||
if next_action.result == NextAction.REDO:
|
||||
# rollback host state
|
||||
self.curr_tqm.clear_failed_hosts()
|
||||
iterator._host_states[self.curr_host.name] = prev_host_state
|
||||
if reduce(lambda total, res: res.is_failed() or total, results, False):
|
||||
self._tqm._stats.failures[self.curr_host.name] -= 1
|
||||
elif reduce(lambda total, res: res.is_unreachable() or total, results, False):
|
||||
self._tqm._stats.dark[self.curr_host.name] -= 1
|
||||
|
||||
# redo
|
||||
super(StrategyModule, self)._queue_task(self.curr_host, self.curr_task, self.curr_task_vars, self.curr_play_context)
|
||||
results = super(StrategyModule, self)._process_pending_results(iterator, one_pass)
|
||||
elif next_action.result == NextAction.CONTINUE:
|
||||
break
|
||||
elif next_action.result == NextAction.EXIT:
|
||||
exit(1)
|
||||
|
||||
return results
|
||||
|
||||
def _need_debug(self, results):
|
||||
return reduce(lambda total, res: res.is_failed() or res.is_unreachable() or total, results, False)
|
||||
|
||||
|
||||
class Debugger(cmd.Cmd):
|
||||
prompt = '(debug) ' # debugger
|
||||
prompt_continuous = '> ' # multiple lines
|
||||
|
||||
def __init__(self, strategy_module, results, next_action):
|
||||
# cmd.Cmd is old-style class
|
||||
cmd.Cmd.__init__(self)
|
||||
|
||||
self.intro = "Debugger invoked"
|
||||
self.scope = {}
|
||||
self.scope['task'] = strategy_module.curr_task
|
||||
self.scope['vars'] = strategy_module.curr_task_vars
|
||||
self.scope['host'] = strategy_module.curr_host
|
||||
self.scope['result'] = results[0]._result
|
||||
self.scope['results'] = results # for debug of this debugger
|
||||
self.next_action = next_action
|
||||
|
||||
def cmdloop(self):
|
||||
try:
|
||||
cmd.Cmd.cmdloop(self)
|
||||
except KeyboardInterrupt:
|
||||
pass
|
||||
|
||||
def do_EOF(self, args):
|
||||
return self.do_quit(args)
|
||||
|
||||
def do_quit(self, args):
|
||||
display.display('aborted')
|
||||
self.next_action.result = NextAction.EXIT
|
||||
return True
|
||||
|
||||
do_q = do_quit
|
||||
|
||||
def do_continue(self, args):
|
||||
self.next_action.result = NextAction.CONTINUE
|
||||
return True
|
||||
|
||||
do_c = do_continue
|
||||
|
||||
def do_redo(self, args):
|
||||
self.next_action.result = NextAction.REDO
|
||||
return True
|
||||
|
||||
do_r = do_redo
|
||||
|
||||
def evaluate(self, args):
|
||||
try:
|
||||
return eval(args, globals(), self.scope)
|
||||
except:
|
||||
t, v = sys.exc_info()[:2]
|
||||
if isinstance(t, str):
|
||||
exc_type_name = t
|
||||
else:
|
||||
exc_type_name = t.__name__
|
||||
display.display('***%s:%s' % (exc_type_name, repr(v)))
|
||||
raise
|
||||
|
||||
def do_p(self, args):
|
||||
try:
|
||||
result = self.evaluate(args)
|
||||
display.display(pprint.pformat(result))
|
||||
except:
|
||||
pass
|
||||
|
||||
def execute(self, args):
|
||||
try:
|
||||
code = compile(args + '\n', '<stdin>', 'single')
|
||||
exec(code, globals(), self.scope)
|
||||
except:
|
||||
t, v = sys.exc_info()[:2]
|
||||
if isinstance(t, str):
|
||||
exc_type_name = t
|
||||
else:
|
||||
exc_type_name = t.__name__
|
||||
display.display('***%s:%s' % (exc_type_name, repr(v)))
|
||||
raise
|
||||
|
||||
def default(self, line):
|
||||
try:
|
||||
self.execute(line)
|
||||
except:
|
||||
pass
|
||||
self.debugger_active = True
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue