From 6797120d853ffffbe30addbfcc32806239e8c614 Mon Sep 17 00:00:00 2001 From: dbasunag Date: Wed, 20 Nov 2024 09:20:59 -0500 Subject: [PATCH] use subprocess.PIPE for podman build commands --- webhook_server_container/libs/github_api.py | 8 ++++---- webhook_server_container/utils/helpers.py | 15 ++++++++++++--- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/webhook_server_container/libs/github_api.py b/webhook_server_container/libs/github_api.py index 1e6f3055..bb13c883 100644 --- a/webhook_server_container/libs/github_api.py +++ b/webhook_server_container/libs/github_api.py @@ -1497,7 +1497,7 @@ def _run_build_container( tag_name=tag, clone_repo_dir=clone_repo_dir, ): - build_rc, build_out, build_err = self.run_podman_command(command=podman_build_cmd) + build_rc, build_out, build_err = self.run_podman_command(command=podman_build_cmd, pipe=True) output: Dict[str, str] = { "title": "Build container", "summary": "", @@ -2065,13 +2065,13 @@ def fix_podman_bug(self) -> None: shutil.rmtree("/tmp/storage-run-1000/containers", ignore_errors=True) shutil.rmtree("/tmp/storage-run-1000/libpod/tmp", ignore_errors=True) - def run_podman_command(self, command: str) -> Tuple[bool, str, str]: - rc, out, err = run_command(command=command, log_prefix=self.log_prefix) + def run_podman_command(self, command: str, pipe: bool = False) -> Tuple[bool, str, str]: + rc, out, err = run_command(command=command, log_prefix=self.log_prefix, pipe=pipe) if rc: return rc, out, err if self.is_podman_bug(err=err): self.fix_podman_bug() - return run_command(command=command, log_prefix=self.log_prefix) + return run_command(command=command, log_prefix=self.log_prefix, pipe=pipe) return rc, out, err diff --git a/webhook_server_container/utils/helpers.py b/webhook_server_container/utils/helpers.py index 4a2f0833..958eecd3 100644 --- a/webhook_server_container/utils/helpers.py +++ b/webhook_server_container/utils/helpers.py @@ -69,6 +69,7 @@ def run_command( timeout: Optional[int] = None, capture_output: bool = True, check: bool = False, + pipe: bool = False, **kwargs: Any, ) -> Tuple[bool, str, str]: """ @@ -81,16 +82,24 @@ def run_command( shell (bool, default False): run subprocess with shell toggle timeout (int, optional): Command wait timeout capture_output (bool, default True): Capture command output - check (boot, default False): If check is True and the exit code was non-zero, it raises a + check (bool, default False): If check is True and the exit code was non-zero, it raises a CalledProcessError + pipe (bool, default False): If pipe is True, text and capture_output would be set to False. stdout and + stderr, would be set to subprocess.PIPE and passed to subprocess.run call + Returns: tuple: True, out if command succeeded, False, err otherwise. """ logger = get_logger_with_params(name="helpers") - + text = True out_decoded: str = "" err_decoded: str = "" + if pipe: + capture_output = False + text = False + kwargs["stdout"] = subprocess.PIPE + kwargs["stderr"] = subprocess.PIPE try: logger.debug(f"{log_prefix} Running '{command}' command") sub_process = subprocess.run( @@ -98,7 +107,7 @@ def run_command( capture_output=capture_output, check=check, shell=shell, - text=True, + text=text, timeout=timeout, **kwargs, )