From 28e18591dfc48f68af048da20c4528536e779c56 Mon Sep 17 00:00:00 2001 From: Gregory Hellings Date: Fri, 15 Sep 2017 23:57:39 -0500 Subject: [PATCH 1/2] Make jenkins_user_api.py more robust The jenkins_user_api module was not properly capturing errors and incorrect exit codes when the module was being executed improperly. Now, more of those cases should be handled properly and trigger an error state in Ansible. Fixes: #151 --- CHANGELOG | 3 +++ cinch/library/jenkins_user_api.py | 37 ++++++++++++++++++++++--------- 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index df2a988..e16f8c6 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,6 @@ +v 0.8.4 + - Capture errors from jenkins-cli.jar more robustly (GH #151) + v 0.8.3 (13 Sep 2017) - Clean up TravisCI tests - Clean up Docker UID variable for OpenShift support diff --git a/cinch/library/jenkins_user_api.py b/cinch/library/jenkins_user_api.py index 4d7e0f1..b973042 100644 --- a/cinch/library/jenkins_user_api.py +++ b/cinch/library/jenkins_user_api.py @@ -60,20 +60,35 @@ def main(): process = process_named_args + process_positional_args # The groovy code simply prints out the value of the API key, so we want # to be able to capture that output + err, output = None, None p = subprocess.Popen(process, stdout=subprocess.PIPE, stderr=subprocess.PIPE) - output, err = p.communicate() - os.unlink(groovy.name) - success = False - # It's possible the Popen process has an error code for a whole host of - # reasons - if p.returncode == 0: - success = True - module.exit_json(api_key=output.strip(), - err=err, - changed=False, - success=success) + try: + output, err = p.communicate() + os.unlink(groovy.name) + # It's possible the Popen process has an error code for a whole host of + # reasons + if p.returncode == 0: + module.exit_json(api_key=output.strip(), + err=err, + changed=False, + success=True) + else: + msg = "Error occurred while executing jenkins-cli.jar" + except subprocess.CalledProcessError: + msg = "Error received while attempting to execute Java" + # If err and output are some type of empty, but not the empty string + if not err and err != "": + err = "No stderr detected" + if not output and output != "": + output = "No stdout detected" + # There are lots of reasons to fall through to here. But if we have, then + # something has most definitely gone wrong. We should report on that + module.fail_json(msg=msg, + stderr=err, + stdout=output, + api_key='') main() From 09bc47a40401dbb45f845a726fc607eb0eec141a Mon Sep 17 00:00:00 2001 From: Gregory Hellings Date: Thu, 21 Sep 2017 08:09:42 -0500 Subject: [PATCH 2/2] More verbose comments on finding error message --- cinch/library/jenkins_user_api.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/cinch/library/jenkins_user_api.py b/cinch/library/jenkins_user_api.py index b973042..6acd393 100644 --- a/cinch/library/jenkins_user_api.py +++ b/cinch/library/jenkins_user_api.py @@ -78,7 +78,12 @@ def main(): msg = "Error occurred while executing jenkins-cli.jar" except subprocess.CalledProcessError: msg = "Error received while attempting to execute Java" - # If err and output are some type of empty, but not the empty string + # If err and output are some type of empty, but not the empty string, + # then we reached this point without any output. If they are the empty + # string, then we reached this point but the subprocess output nothing + # on the specified pipe. Providing this data, or a status message such + # as these defaults, provides a better way for users to diagnose the + # problems encountered if not err and err != "": err = "No stderr detected" if not output and output != "":