Skip to content

Commit

Permalink
Fixed last review finding.
Browse files Browse the repository at this point in the history
  • Loading branch information
ckunki committed Nov 17, 2023
1 parent 2aca7aa commit cc2b8ce
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 22 deletions.
9 changes: 3 additions & 6 deletions exasol/ds/sandbox/lib/ansible/ansible_access.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import ansible_runner
import json
import logging

from dataclasses import dataclass
Expand All @@ -24,7 +25,7 @@ class AnsibleAccess:
def run(
private_data_dir: str,
run_ctx: AnsibleRunContext,
event_logger: Callable[[AnsibleEvent], None],
event_logger: Callable[[str], None],
event_handler: Callable[[AnsibleEvent], bool] = None,
):
quiet = not get_status_logger(LogType.ANSIBLE).isEnabledFor(logging.INFO)
Expand All @@ -36,10 +37,6 @@ def run(
extravars=run_ctx.extra_vars,
)
for e in r.events:
event_logger(e)
event_logger(json.dumps(e, indent=2))
if r.rc != 0:
raise AnsibleException(r.rc)

if __name__ == "__main__":
a = _event_handler({"event_data": { "duration": 1}})
print(f'a = "{a}"')
38 changes: 22 additions & 16 deletions exasol/ds/sandbox/lib/ansible/ansible_runner.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import json
import logging

from pathlib import Path
Expand All @@ -20,19 +19,26 @@ class AnsibleRunner:
def __init__(self, ansible_access: AnsibleAccess, work_dir: Path):
self._ansible_access = ansible_access
self._work_dir = work_dir

@staticmethod
def event_logger(event: AnsibleEvent):
LOG.debug(json.dumps(event, indent=2))

@staticmethod
def event_handler(event: AnsibleEvent) -> bool:
try:
duration = event["event_data"]["duration"]
if duration is not None and duration > 0.5:
print(f"duration: {round(duration)} seconds")
except KeyError as ex:
pass
self._duration_logger = AnsibleRunner.duration_logger()

@classmethod
def duration_logger(cls) -> logging.Logger:
def handler():
handler = logging.StreamHandler()
handler.setFormatter(logging.Formatter('%(message)s'))
return handler
logger = logging.getLogger(f"{__name__}:{cls.__name__}")
logger.setLevel(logging.DEBUG)
logger.propagate = False
logger.addHandler(handler())
return logger

def event_handler(self, event: AnsibleEvent) -> bool:
if not "event_data" in event:
return True
duration = event["event_data"].get("duration", 0)
if duration > 0.5:
self._duration_logger.debug(f"duration: {round(duration)} seconds")
return True

def run(self, ansible_run_context: AnsibleRunContext, host_infos: Tuple[HostInfo]):
Expand All @@ -43,6 +49,6 @@ def run(self, ansible_run_context: AnsibleRunContext, host_infos: Tuple[HostInfo
self._ansible_access.run(
str(self._work_dir),
ansible_run_context,
self.event_logger,
self.event_handler,
event_logger=LOG.debug,
event_handler=self.event_handler,
)

0 comments on commit cc2b8ce

Please sign in to comment.