Skip to content

Commit

Permalink
feat(exec): Add regex output parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
MrKevinWeiss committed Sep 12, 2023
1 parent 5a983a4 commit 984ad53
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
10 changes: 9 additions & 1 deletion src/inet_nm/cli_exec.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,18 @@ def main():
default=None,
help="Wait until node available in seconds.",
)

parser.add_argument(
"--seq",
action="store_true",
help="Run commands sequentially instead of concurrently",
)
parser.add_argument(
"-r",
"--output-filter",
type=str,
default=None,
help="Filter all lines with regex.",
)

cfg.config_arg(parser)
chk.check_args(parser)
Expand All @@ -44,6 +50,7 @@ def main():
cmd = kwargs.pop("cmd")
seq = kwargs.pop("seq")
force = kwargs.pop("force")
output_filter = kwargs.pop("output_filter")
nodes = rh.sanity_check("/bin/bash", **kwargs)

extra_env = rh.node_env_vars(args.config)
Expand All @@ -54,6 +61,7 @@ def main():
nodes, default_timeout=timeout, seq=seq, extra_env=extra_env, force=force
) as runner:
runner.cmd = cmd
runner.output_filter = output_filter
runner.run()
except KeyboardInterrupt:
print()
Expand Down
22 changes: 18 additions & 4 deletions src/inet_nm/runner_apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"""

import os
import re
import subprocess
import time
from typing import Dict
Expand All @@ -29,15 +30,21 @@ class NmShellRunner(NmNodesRunner):

cmd = "echo $NM_IDX"
SETUP_WAIT = 0.1
output_filter = None
results = []

@staticmethod
def _run_command(cmd, prefix, env):
def _run_command(cmd, prefix, env, regex_str=None):
def get_output(process):
output = process.stdout.readline()

if output:
nm_print(f"{prefix}{output.decode().strip()}")
if regex_str is not None:
matched = re.findall(regex_str, output.decode().strip())
for data in matched:
nm_print(f"{prefix}{data}")
else:
nm_print(f"{prefix}{output.decode().strip()}")
else:
# Have a small sleep so we are not burning CPU waiting for output.
time.sleep(0.1)
Expand Down Expand Up @@ -82,8 +89,15 @@ def func(self, node: NmNode, idx: int, env: Dict[str, str]):
# Note that the run command exits after one command is executed.
# So if we want to loop with a default shell it will exit after the first loop.
cmd = f"/bin/bash -c '{self.cmd}'"
res = NmShellRunner._run_command(cmd, prefix=prefix, env=full_env)
self.results.append(f"RESULT:{prefix}{res}")
if self.output_filter is None:
regex_str = None
else:
regex_str = re.compile(self.output_filter)
res = NmShellRunner._run_command(
cmd, prefix=prefix, env=full_env, regex_str=regex_str
)
if self.output_filter is None:
self.results.append(f"RESULT:{prefix}{res}")

def post(self):
"""Run after the operations on nodes have completed.
Expand Down

0 comments on commit 984ad53

Please sign in to comment.