Skip to content

Commit

Permalink
Properly handle failed git commands when redirect_stderr=True (#33203)
Browse files Browse the repository at this point in the history
* Properly handle redirected stderr

When running subprocess.Popen.communicate() on a command run with stderr
redirected to subprocess.STDOUT, the 2nd element of the return tuple
(representing the stderr) will be None, since all of the standard error
was sent to stdout.

Functions interpreting the return data from cmd.run_all will be
expecting the ``stderr`` key in the return dict to contain a string, not
a NoneType, so this commit forces the stderr to be set to an empty
string when redirect_stderr is True.

* Look at stdout instead of stderr for error text when redirect_stderr is True
  • Loading branch information
Erik Johnson authored and Mike Place committed May 12, 2016
1 parent 8a0950d commit 86db5df
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
4 changes: 4 additions & 0 deletions salt/modules/cmdmod.py
Expand Up @@ -449,6 +449,10 @@ def _run(cmd,
return ret

out, err = proc.stdout, proc.stderr
if err is None:
# Will happen if redirect_stderr is True, since stderr was sent to
# stdout.
err = ''

if rstrip:
if out is not None:
Expand Down
11 changes: 6 additions & 5 deletions salt/modules/git.py
Expand Up @@ -238,9 +238,9 @@ def _git_run(command, cwd=None, runas=None, identity=None,
if result['retcode'] == 0:
return result
else:
stderr = \
salt.utils.url.redact_http_basic_auth(result['stderr'])
errors.append(stderr)
err = result['stdout' if redirect_stderr else 'stderr']
if err:
errors.append(salt.utils.url.redact_http_basic_auth(err))

# We've tried all IDs and still haven't passed, so error out
if failhard:
Expand Down Expand Up @@ -281,9 +281,10 @@ def _git_run(command, cwd=None, runas=None, identity=None,
msg = 'Command \'{0}\' failed'.format(
salt.utils.url.redact_http_basic_auth(gitcommand)
)
if result['stderr']:
err = result['stdout' if redirect_stderr else 'stderr']
if err:
msg += ': {0}'.format(
salt.utils.url.redact_http_basic_auth(result['stderr'])
salt.utils.url.redact_http_basic_auth(err)
)
raise CommandExecutionError(msg)
return result
Expand Down

0 comments on commit 86db5df

Please sign in to comment.