@@ -60,20 +60,40 @@ def main():
60
60
process = process_named_args + process_positional_args
61
61
# The groovy code simply prints out the value of the API key, so we want
62
62
# to be able to capture that output
63
+ err , output = None , None
63
64
p = subprocess .Popen (process ,
64
65
stdout = subprocess .PIPE ,
65
66
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 = '' )
77
97
78
98
79
99
main ()
0 commit comments