forked from colinbjohnson/ask-jenkins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathask-jenkins.rb
executable file
·168 lines (156 loc) · 6.9 KB
/
ask-jenkins.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/usr/bin/ruby
#a Job object is created for each job in the XML query list
class Job
attr_accessor :name,:url,:urllatest,:userid,:number,:timestamp,:changeNumber,:stableBuild
def initialize(name,url,urllatest,userid,number,timestamp,changeNumber,stableBuild)
@name = name
@url = url
@urllatest = urllatest
@userid = userid
@number = number
@timestamp = timestamp
@changeNumber = changeNumber
@stableBuild = stableBuild
end
end
#an XML object contains the XML response from a Jenkins query
class XML
attr_accessor :httpresponse, :httpresponsereturn, :xmlresponse, :xmlresponsereturn
def initialize(url,authentication)
uri = URI.parse(url)
request = Net::HTTP::Get.new(uri.path)
request.basic_auth(authentication[:username],authentication[:password])
connection = Net::HTTP.new(uri.host, uri.port)
@httpresponse = connection.start {|http| http.request(request) }
case @httpresponse
when Net::HTTPSuccess
@xmlresponse = REXML::Document.new(@httpresponse.body)
else
@httpresponsereturn = 'fail'
@xmlresponsereturn = 'fail'
end
end
end
#an Output object is created and is used to output a list of Job objects stored in the job_collection hash
class Output
def initialize(job_collection,output)
case output
when 'screen'
job_collection.each do |jobkey,jobvalue|
if jobvalue.stableBuild = 'true'
print 'Name,',jobvalue.name,',UserId,',jobvalue.userid,',Number,',jobvalue.number,',changeNumber,',jobvalue.changeNumber,"\n"
end
end
when 'table'
puts 'Table Output'
table_rows = Array.new
job_collection.each do |jobkey,jobvalue|
table_rows << [jobvalue.name,jobvalue.userid,jobvalue.number,jobvalue.changeNumber]
end
#prints to table
table = Terminal::Table.new :headings => ['Name', 'UserID','Number','ChangeList'] , :rows => table_rows
table.align_column(5, :right)
puts table
end
end
end
require 'optparse'
require 'net/http' #require 'uri' is included by net/http automatically
require 'rexml/document'
require 'rubygems'
require 'terminal-table'
program_name = File.basename($PROGRAM_NAME)
options = {:output => "table"}
jenkins_authentication = {:username => '', :password => ''};
jenkins_url_array = {:protocol => 'http', :hostname => '', :port => '8080', :path => '/jenkins/'};
job_collection = {};
optparse = OptionParser.new do |opts|
#sets options banner
opts.banner = "Usage: #{program_name} [options]"
#options processing: output
opts.on("-o","--output OUTPUT","Output method. Accepts values \"screen\" or \"table.\" Default value is \"table\".") do |output|
#forces option to lowercase - easier to evaluate variables when always lowercase
output.downcase!
if (output == "screen" || output == "table")
options[:output] = output
else
$stderr.print "You must specify an output method such as \"screen\" or \"table\". You specified \"", output, ".\"\n"
exit 64
end
end
opts.on("-u","--username USERNAME","Username to be utilized when connecting to the Jenkins API.") do |username|
jenkins_authentication[:username] = username
end
opts.on("-p","--password PASSWORD","Password or Token to be utilized when connecting to the Jenkins API.") do |password|
jenkins_authentication[:password] = password
end
opts.on("-h","--hostname HOSTNAME","Hostname to be utilized when connecting to the Jenkins API.") do |hostname|
jenkins_url_array[:hostname] = hostname
end
opts.on("--protocol PROTOCOL","Protocol to be utilized when connecting to the Jenkins API. Defaults to http.") do |protocol|
jenkins_url_array[:protocol] = protocol
end
opts.on("--port PORT","Port to be utilized when connecting to the Jenkins API. Defaults to 8080.") do |port|
jenkins_url_array[:port] = port
end
opts.on("--path PATH","Path to be utilized when connecting to the Jenkins API. defaults to /jenkins/.") do |path|
jenkins_url_array[:path] = path
end
end
optparse.parse!
#validation of options - any options here are required
if jenkins_authentication[:username].empty?
$stderr.print "You must specifiy a Username to be utilized with connecting to the Jenkins API. You can specify a Username using either -u <myusername> or --username <myusername>.""\n"
exit 64
end
if jenkins_authentication[:password].empty?
$stderr.print "You must specifiy a Password or API token to be utilized with connecting to the Jenkins API. You can specify a Password using either -p <mypassword> or --password <password>.""\n"
exit 64
end
if jenkins_url_array[:hostname].empty?
$stderr.print "You must specifiy a Hostname to be utilized with connecting to the Jenkins API. You can specify a Hostname using either -h <hostname> or --hostname <hostname>.""\n"
exit 64
end
#creates a single object to be provided to methods that use the Jenkins API
jenkins_url = jenkins_url_array[:protocol] + '://' + jenkins_url_array[:hostname] + ':' + jenkins_url_array[:port] + jenkins_url_array[:path]
#jobslist is a list of jobs returned from polling the Jenkins API
jobslist = XML.new( jenkins_url + '/api/xml',jenkins_authentication)
#shortcut - if XML response is nil, exit
if jobslist.xmlresponse.nil?
$stderr.print "There was an error retreiving data from the Jenkins instance at " , jenkins_url , ".\n"
exit 64
end
jobslist.xmlresponse.each_element('hudson/job') do |jobelement|
jobname = jobelement.elements['name'].text
joburl = jobelement.elements['url'].text
joburllateststable = jobelement.elements['url'].text + 'lastStableBuild/api/xml/'
jobxml = XML.new(joburllateststable,jenkins_authentication)
#jobxml can either be nil, in which can a success
if jobxml.xmlresponse.nil?
joburllastbuild = jobelement.elements['url'].text + 'lastBuild/api/xml/'
jobxml = XML.new(joburllastbuild,jenkins_authentication)
if jobxml.xmlresponse.nil?
jobstableBuild = 'false'
else
print 'failed to retreive data for', jobname, ' at ', joburl, "\n"
end
else
jobstableBuild = 'true'
unless jobxml.xmlresponse.elements['freeStyleBuild/action/cause/userId'].nil?
jobuserid = jobxml.xmlresponse.elements['freeStyleBuild/action/cause/userId'].text
end
unless jobxml.xmlresponse.elements['freeStyleBuild/number'].nil?
jobnumber = jobxml.xmlresponse.elements['freeStyleBuild/number'].text
end
unless jobxml.xmlresponse.elements['freeStyleBuild/timestamp'].nil?
jobtimestamp = jobxml.xmlresponse.elements['freeStyleBuild/timestamp'].text
end
unless jobxml.xmlresponse.elements['freeStyleBuild/changeSet/item/changeNumber'].nil?
jobchangeNumber = jobxml.xmlresponse.elements['freeStyleBuild/changeSet/item/changeNumber'].text
end
end
#puts jobname,joburl,joburllateststable
jobobject = Job.new(jobname,joburl,joburllateststable,jobuserid,jobnumber,jobtimestamp,jobchangeNumber,jobstableBuild)
job_collection[jobobject.name] = jobobject
end
output = Output.new(job_collection,options[:output])