From 149caca86a48e1c6d8240d78592205a49ae9fb9b Mon Sep 17 00:00:00 2001 From: Madhu Kanoor Date: Tue, 14 Feb 2023 19:31:26 -0500 Subject: [PATCH] [AAP-9299] Fixes stack trace when a playbook is not found When a playbook cannot be found, we throw an index error ``` Traceback (most recent call last): File "/Users/madhukanoor/devsrc/ansible-rulebook/ansible_rulebook/engine.py", line 522, in execute await action_method(**action_args) File "/Users/madhukanoor/devsrc/ansible-rulebook/ansible_rulebook/builtin.py", line 328, in run_playbook await post_process_runner( File "/Users/madhukanoor/devsrc/ansible-rulebook/ansible_rulebook/builtin.py", line 635, in post_process_runner fact_folder = _get_latest_artifact( File "/Users/madhukanoor/devsrc/ansible-rulebook/ansible_rulebook/builtin.py", line 845, in _get_latest_artifact return files[0] IndexError: list index out of range ``` Throw an exception if the runner results file can't be found. Also prevent looking for the facts file when the runner has failed to execute the playbook. --- ansible_rulebook/builtin.py | 22 +++++++++++++++++----- ansible_rulebook/exception.py | 10 ++++++++++ 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/ansible_rulebook/builtin.py b/ansible_rulebook/builtin.py index b7a70d24..4dfd9653 100644 --- a/ansible_rulebook/builtin.py +++ b/ansible_rulebook/builtin.py @@ -35,7 +35,11 @@ from .collection import find_playbook, has_playbook, split_collection_name from .conf import settings -from .exception import ShutdownException +from .exception import ( + PlaybookNotFoundException, + PlaybookStatusNotFoundException, + ShutdownException, +) from .job_template_runner import job_template_runner from .util import get_horizontal_rule @@ -573,9 +577,9 @@ async def pre_process_runner( os.path.join(project_dir, name), ) else: - logger.error( - "Could not find a playbook for %s from %s", name, os.getcwd() - ) + msg = f"Could not find a playbook for {name} from {os.getcwd()}" + logger.error(msg) + raise PlaybookNotFoundException(msg) return (private_data_dir, playbook_name) @@ -597,6 +601,12 @@ async def post_process_runner( rc = int(_get_latest_artifact(private_data_dir, "rc")) status = _get_latest_artifact(private_data_dir, "status") + logger.info("Playbook rc: %d, status: %s", rc, status) + if rc != 0: + error_message = _get_latest_artifact(private_data_dir, "stderr") + if not error_message: + error_message = _get_latest_artifact(private_data_dir, "stdout") + logger.error(error_message) result = dict( type="Action", @@ -613,7 +623,7 @@ async def post_process_runner( ) await event_log.put(result) - if set_facts or post_events: + if rc == 0 and (set_facts or post_events): logger.debug("set_facts") fact_folder = _get_latest_artifact( private_data_dir, "fact_cache", False @@ -798,6 +808,8 @@ async def shutdown( def _get_latest_artifact(data_dir: str, artifact: str, content: bool = True): files = glob.glob(os.path.join(data_dir, "artifacts", "*", artifact)) files.sort(key=os.path.getmtime, reverse=True) + if not files: + raise PlaybookStatusNotFoundException(f"No {artifact} file found") if content: with open(files[0], "r") as f: content = f.read() diff --git a/ansible_rulebook/exception.py b/ansible_rulebook/exception.py index 01e68657..07aaacda 100644 --- a/ansible_rulebook/exception.py +++ b/ansible_rulebook/exception.py @@ -76,3 +76,13 @@ class ConditionParsingException(Exception): class InvalidTypeException(Exception): pass + + +class PlaybookStatusNotFoundException(Exception): + + pass + + +class PlaybookNotFoundException(Exception): + + pass