Skip to content

Commit

Permalink
control verbosity from the consumer of compat library
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Ziegenberg <[email protected]>
  • Loading branch information
ziegenberg committed Jul 28, 2023
1 parent a8ea023 commit af6e6ef
Showing 1 changed file with 28 additions and 4 deletions.
32 changes: 28 additions & 4 deletions src/ansible_compat/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ def __init__(
require_module: bool = False,
max_retries: int = 0,
environ: dict[str, str] | None = None,
verbosity: int = 0,
) -> None:
"""Initialize Ansible runtime environment.
Expand All @@ -184,12 +185,17 @@ def __init__(
Default is 0, no retries.
:param environ: Environment dictionary to use, if undefined
``os.environ`` will be copied and used.
:param verbosity: Verbosity level to use.
"""
self.project_dir = project_dir or Path.cwd()
self.isolated = isolated
self.max_retries = max_retries
self.environ = environ or os.environ.copy()
self.plugins = Plugins(runtime=self)
self.verbosity = verbosity

self.initialize_logger(level=self.verbosity)

# Reduce noise from paramiko, unless user already defined PYTHONWARNINGS
# paramiko/transport.py:236: CryptographyDeprecationWarning: Blowfish has been deprecated
# https://github.com/paramiko/paramiko/issues/2038
Expand Down Expand Up @@ -230,6 +236,21 @@ def warning(
# Monkey patch ansible warning in order to use warnings module.
Display.warning = warning

def initialize_logger(self, level: int = 0) -> None:
"""Set up the global logging level based on the verbosity number."""
verbosity_map = {
-2: logging.CRITICAL,
-1: logging.ERROR,
0: logging.WARNING,
1: logging.INFO,
2: logging.DEBUG,
}
# Unknown logging level is treated as DEBUG
logging_level = verbosity_map.get(level, logging.DEBUG)
_logger.setLevel(logging_level)
# Use module-level _logger instance to validate it
_logger.debug("Logging initialized to level %s", logging_level)

def load_collections(self) -> None:
"""Load collection data."""
self.collections = OrderedDict()
Expand Down Expand Up @@ -502,9 +523,11 @@ def install_requirements( # noqa: C901
"ansible-galaxy",
"role",
"install",
"-vvvr",
"-r",
f"{requirement}",
]
if self.verbosity > 0:
cmd.extend(["-" + ("v" * self.verbosity)])
if self.cache_dir:
cmd.extend(["--roles-path", f"{self.cache_dir}/roles"])

Expand All @@ -516,7 +539,7 @@ def install_requirements( # noqa: C901
_logger.info("Running %s", " ".join(cmd))

result = self.run(cmd, retry=retry)
_logger.info(result.stdout)
_logger.debug(result.stdout)
if result.returncode != 0:
_logger.error(result.stderr)
raise AnsibleCommandError(result)
Expand All @@ -527,8 +550,9 @@ def install_requirements( # noqa: C901
"ansible-galaxy",
"collection",
"install",
"-vvv",
]
if self.verbosity > 0:
cmd.extend(["-" + ("v" * self.verbosity)])
if offline:
_logger.warning(
"Skipped installing collection dependencies due to running in offline mode.",
Expand All @@ -549,7 +573,7 @@ def install_requirements( # noqa: C901
retry=retry,
env={**os.environ, "ANSIBLE_COLLECTIONS_PATH": ":".join(cpaths)},
)
_logger.info(result.stdout)
_logger.debug(result.stdout)
if result.returncode != 0:
_logger.error(result.stderr)
raise AnsibleCommandError(result)
Expand Down

0 comments on commit af6e6ef

Please sign in to comment.