Merge pull request #37625 from cachedout/issue_37492

Return with proper retcodes in batch mode
This commit is contained in:
Mike Place 2016-11-12 20:29:09 +00:00 committed by GitHub
commit 305e51d1c0
3 changed files with 19 additions and 9 deletions

View file

@ -226,14 +226,9 @@ class Batch(object):
if self.opts.get('raw'):
ret[minion] = data
yield data
elif self.opts.get('failhard'):
# When failhard is passed, we need to return all data to include
# the retcode to use in salt/cli/salt.py later. See issue #24996.
else:
ret[minion] = data
yield {minion: data}
else:
ret[minion] = data['ret']
yield {minion: data['ret']}
if not self.quiet:
ret[minion] = data['ret']
data[minion] = data.pop('ret')

View file

@ -53,6 +53,10 @@ class SaltCMD(parsers.SaltCMDOptionParser):
return
if self.options.batch or self.options.static:
# _run_batch() will handle all output and
# exit with the appropriate error condition
# Execution will not continue past this point
# in batch mode.
self._run_batch()
else:
if self.options.timeout <= 0:
@ -234,10 +238,14 @@ class SaltCMD(parsers.SaltCMDOptionParser):
# We will print errors to the console further down the stack
sys.exit(1)
# Printing the output is already taken care of in run() itself
retcode = 0
for res in batch.run():
if self.options.failhard:
for ret in six.itervalues(res):
retcode = salt.utils.job.get_retcode(ret)
for ret in six.itervalues(res):
job_retcode = salt.utils.job.get_retcode(ret)
if job_retcode > retcode:
# Exit with the highest retcode we find
retcode = job_retcode
if self.options.failhard:
if retcode != 0:
sys.stderr.write(
'{0}\nERROR: Minions returned with non-zero exit code.\n'.format(
@ -245,6 +253,7 @@ class SaltCMD(parsers.SaltCMDOptionParser):
)
)
sys.exit(retcode)
sys.exit(retcode)
def _print_errors_summary(self, errors):
if errors:

View file

@ -89,6 +89,12 @@ class BatchTest(integration.ShellCase):
cmd = sorted(self.run_salt('-G \'os:{0}\' -b 25% test.ping'.format(os_grain)))
self.assertListEqual(cmd, sorted(ret))
def test_batch_exit_code(self):
'''
Test that a failed state returns a non-zero exit code in batch mode
'''
cmd = self.run_salt(' "*" state.single test.fail_without_changes name=test_me -b 25%', with_retcode=True)
self.assertEqual(cmd[-1], 2)
if __name__ == '__main__':
from integration import run_tests