diff --git a/CHANGELOG b/CHANGELOG index a9f2e31..029fc46 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,6 @@ v 0.8.4 - Bump to linchpin 1.0.3 and Ansible >= 2.3.2 because of syntax errors (GH #176) +- Capture errors from jenkins-cli.jar more robustly (GH #151) - Streamline installation of the Python pip module (GH #147) v 0.8.3 (13 Sep 2017) diff --git a/cinch/library/jenkins_user_api.py b/cinch/library/jenkins_user_api.py index 4d7e0f1..6acd393 100644 --- a/cinch/library/jenkins_user_api.py +++ b/cinch/library/jenkins_user_api.py @@ -60,20 +60,40 @@ 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, + # 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 != "": + 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()