Skip to content

Commit a0b0243

Browse files
Merge pull request #179 from greg-hellings/issue151
Make jenkins_user_api.py more robust
2 parents cbb2c75 + e3b5b46 commit a0b0243

File tree

2 files changed

+32
-11
lines changed

2 files changed

+32
-11
lines changed

CHANGELOG

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
v 0.8.4
2+
- Capture errors from jenkins-cli.jar more robustly (GH #151)
23
- Streamline installation of the Python pip module (GH #147)
34

45
v 0.8.3 (13 Sep 2017)

cinch/library/jenkins_user_api.py

+31-11
Original file line numberDiff line numberDiff line change
@@ -60,20 +60,40 @@ def main():
6060
process = process_named_args + process_positional_args
6161
# The groovy code simply prints out the value of the API key, so we want
6262
# to be able to capture that output
63+
err, output = None, None
6364
p = subprocess.Popen(process,
6465
stdout=subprocess.PIPE,
6566
stderr=subprocess.PIPE)
66-
output, err = p.communicate()
67-
os.unlink(groovy.name)
68-
success = False
69-
# It's possible the Popen process has an error code for a whole host of
70-
# reasons
71-
if p.returncode == 0:
72-
success = True
73-
module.exit_json(api_key=output.strip(),
74-
err=err,
75-
changed=False,
76-
success=success)
67+
try:
68+
output, err = p.communicate()
69+
os.unlink(groovy.name)
70+
# It's possible the Popen process has an error code for a whole host of
71+
# reasons
72+
if p.returncode == 0:
73+
module.exit_json(api_key=output.strip(),
74+
err=err,
75+
changed=False,
76+
success=True)
77+
else:
78+
msg = "Error occurred while executing jenkins-cli.jar"
79+
except subprocess.CalledProcessError:
80+
msg = "Error received while attempting to execute Java"
81+
# If err and output are some type of empty, but not the empty string,
82+
# then we reached this point without any output. If they are the empty
83+
# string, then we reached this point but the subprocess output nothing
84+
# on the specified pipe. Providing this data, or a status message such
85+
# as these defaults, provides a better way for users to diagnose the
86+
# problems encountered
87+
if not err and err != "":
88+
err = "No stderr detected"
89+
if not output and output != "":
90+
output = "No stdout detected"
91+
# There are lots of reasons to fall through to here. But if we have, then
92+
# something has most definitely gone wrong. We should report on that
93+
module.fail_json(msg=msg,
94+
stderr=err,
95+
stdout=output,
96+
api_key='')
7797

7898

7999
main()

0 commit comments

Comments
 (0)