From 3252b5973819e338a4cdfb08f1cee318122173be Mon Sep 17 00:00:00 2001 From: techdragon Date: Mon, 17 Feb 2014 21:10:17 +0800 Subject: [PATCH] added failure exit code functionality to highstate - new config variable to enable/disable the new functionality - added new function to determine if the highstate return data indicates an error or failure has occurred. - added new logic to the end of the salt cli call that if enabled using the config variable, determines if salt should exit with a non zero exit code and then will exit if any failures or errors are found in the highstate return data. --- salt/config.py | 2 ++ salt/output/__init__.py | 13 +++++++++++++ 2 files changed, 15 insertions(+) diff --git a/salt/config.py b/salt/config.py index c565620d7492..9e2a46018d65 100644 --- a/salt/config.py +++ b/salt/config.py @@ -187,6 +187,7 @@ 'gather_job_timeout': int, 'auth_timeout': int, 'random_master': bool, + 'salt_fails_if_highstate_fails': bool, } # default configurations @@ -397,6 +398,7 @@ 'salt_transport': 'zeromq', 'enumerate_proxy_minions': False, 'gather_job_timeout': 2, + 'salt_fails_if_highstate_fails': False } # ----- Salt Cloud Configuration Defaults -----------------------------------> diff --git a/salt/output/__init__.py b/salt/output/__init__.py index 1d57ba5f4ae3..8b78ae751676 100644 --- a/salt/output/__init__.py +++ b/salt/output/__init__.py @@ -16,6 +16,8 @@ import salt.loader import salt.utils +from salt.exceptions import SaltSystemExit + log = logging.getLogger(__name__) @@ -39,6 +41,7 @@ def display_output(data, out, opts=None): display_data = get_printout('nested', opts)(data).rstrip() output_filename = opts.get('output_file', None) + salt_fails_if_highstate_fails = opts.get('salt_fails_if_highstate_fails', False) try: if output_filename is not None: with salt.utils.fopen(output_filename, 'a') as ofh: @@ -47,11 +50,21 @@ def display_output(data, out, opts=None): return if display_data: print(display_data) + if salt_fails_if_highstate_fails: + if not get_highstate_success(highstate_data=data): + raise SaltSystemExit(code=1) + except IOError as exc: # Only raise if it's NOT a broken pipe if exc.errno != errno.EPIPE: raise exc +def get_highstate_success(highstate_data): + for i in highstate_data.keys(): + for j in highstate_data[i].keys(): + if not highstate_data[i][j]['result']: + return False + return True def get_printout(out, opts=None, **kwargs): '''