From 5e961a36e40911139a00df2a1600b8e154dd770d Mon Sep 17 00:00:00 2001 From: Chris Gravel Date: Thu, 2 Jun 2022 21:51:33 -0700 Subject: [PATCH] Chef - Preserve returncode in stateful_shell --- 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: