Skip to content

Commit

Permalink
[AAP-9299] Fixes stack trace when a playbook is not found (#373)
Browse files Browse the repository at this point in the history
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.

https://issues.redhat.com/browse/AAP-9299

- Add a log info message to always report the **rc** and **status**
- If rc is not 0, read the stderr or stdout in the private_data_dir and
report it as an error
- If the playbook has syntax error it would be visible in the error log.
Previously this was getting suppressed
- if the rc is not 0 then we should not post_event to set_fact
  • Loading branch information
mkanoor authored Feb 16, 2023
2 parents 3e0d9ff + 149caca commit 617f2bf
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
22 changes: 17 additions & 5 deletions ansible_rulebook/builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)

Expand All @@ -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",
Expand All @@ -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
Expand Down Expand Up @@ -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()
Expand Down
10 changes: 10 additions & 0 deletions ansible_rulebook/exception.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,13 @@ class ConditionParsingException(Exception):
class InvalidTypeException(Exception):

pass


class PlaybookStatusNotFoundException(Exception):

pass


class PlaybookNotFoundException(Exception):

pass

0 comments on commit 617f2bf

Please sign in to comment.