Skip to content

Commit

Permalink
Capture sanity check warnings and display them
Browse files Browse the repository at this point in the history
Captures runtime warnings raise by driver.sanity_check() and
display them at the end in case of failure.

Related: ansible-community/molecule-podman#76
  • Loading branch information
ssbarnea committed Aug 27, 2021
1 parent b351ea0 commit 230c372
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
11 changes: 9 additions & 2 deletions src/molecule/provisioner/ansible_playbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"""Ansible-Playbook Provisioner Module."""

import logging
import warnings

from molecule import util

Expand Down Expand Up @@ -103,12 +104,18 @@ def execute(self):
LOG.warning("Skipping, %s action has no playbook." % self._config.action)
return

self._config.driver.sanity_checks()
result = util.run_command(self._ansible_command, debug=self._config.debug)
# breakpoint()
with warnings.catch_warnings(record=True) as warns:
# warnings.simplefilter("ignore")
self._config.driver.sanity_checks()
result = util.run_command(self._ansible_command, debug=self._config.debug)

# print(warns)
if result.returncode != 0:
util.sysexit_with_message(
f"Ansible return code was {result.returncode}, command was: {result.args}",
result.returncode,
warns=warns,
)

return result.stdout
Expand Down
8 changes: 7 additions & 1 deletion src/molecule/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,10 @@ def sysexit(code: int = 1) -> NoReturn:


def sysexit_with_message(
msg: str, code: int = 1, detail: Optional[MutableMapping] = None
msg: str,
code: int = 1,
detail: Optional[MutableMapping] = None,
warns: Optional[List[Warning]] = None,
) -> None:
"""Exit with an error message."""
# detail is usually a multi-line string which is not suitable for normal
Expand All @@ -108,6 +111,9 @@ def sysexit_with_message(
detail_str = str(detail)
print(detail_str)
LOG.critical(msg)
if warns:
for warn in warns:
LOG.warning(warn.__dict__['message'].args[0])
sysexit(code)


Expand Down

0 comments on commit 230c372

Please sign in to comment.