From df8fed8e06f141750a9a89c4dd6e759d7475ba6a Mon Sep 17 00:00:00 2001 From: cpagravel Date: Fri, 3 Jun 2022 16:50:51 -0700 Subject: [PATCH] Chef - Preserve returncode in stateful_shell (#19144) --- examples/chef/stateful_shell.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/examples/chef/stateful_shell.py b/examples/chef/stateful_shell.py index 20615796c88ae4..0775933aabda93 100644 --- a/examples/chef/stateful_shell.py +++ b/examples/chef/stateful_shell.py @@ -92,8 +92,8 @@ def run_cmd( redirect = "" command_with_state = ( - f"OLDPWD={self.env.get('OLDPWD', '')}; {cmd} {redirect};" - f" env -0 > {self.envfile_path}") + f"OLDPWD={self.env.get('OLDPWD', '')}; {cmd} {redirect}; RETCODE=$?;" + f" env -0 > {self.envfile_path}; exit $RETCODE") with subprocess.Popen( [command_with_state], env=self.env, cwd=self.cwd, @@ -114,7 +114,9 @@ def run_cmd( if raise_on_returncode and returncode != 0: raise RuntimeError( - f"Error. Return code is not 0. It is: {returncode}") + "Error. Nonzero return code." + f"\nReturncode: {returncode}" + f"\nCmd: {cmd}") if return_cmd_output: with open(self.cmd_output_path, encoding="latin1") as f: