Module: Jenkins::Api

Includes:
HTTParty
Defined in:
lib/jenkins/api.rb

Constant Summary collapse

JobAlreadyExistsError =

http_proxy ‘localhost’, ‘8888’

Class.new(Exception)

Class Method Summary collapse

Class Method Details

.add_node(options = {}) ⇒ Object

Adds SSH nodes only, for now



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/jenkins/api.rb', line 166

def self.add_node(options = {})
  options = options.with_clean_keys
  default_options = Hash.new
  if options[:vagrant]
    default_options.merge!(
      :slave_port  => 2222,
      :slave_user  => 'vagrant',
      :master_key  => "/Library/Ruby/Gems/1.8/gems/vagrant-0.6.7/keys/vagrant", # FIXME - hardcoded master username assumption
      :slave_fs    => "/vagrant/tmp/jenkins-slave/",
      :description => "Automatically created by Jenkins.rb",
      :executors   => 2,
      :exclusive   => true
    )
  else
    default_options.merge!(
      :slave_port  => 22,
      :slave_user  => 'deploy',
      :master_key  => "/home/deploy/.ssh/id_rsa", # FIXME - hardcoded master username assumption
      :slave_fs    => "/data/jenkins-slave/",
      :description => "Automatically created by Jenkins.rb",
      :executors   => 2,
      :exclusive   => true
    )
  end
  options    = default_options.merge(options)

  slave_host = options[:slave_host]
  name       = options[:name] || slave_host
  labels     = options[:labels].split(/\s*,\s*/).join(' ') if options[:labels]

  type = "hudson.slaves.DumbSlave$DescriptorImpl"

  fields = {
    "name" => name,
    "type" => type,

    "json"                => {
      "name"              => name,
      "nodeDescription"   => options[:description],
      "numExecutors"      => options[:executors],
      "remoteFS"          => options[:slave_fs],
      "labelString"       => labels,
      "mode"              => options[:exclusive] ? "EXCLUSIVE" : "NORMAL",
      "type"              => type,
      "retentionStrategy" => { "stapler-class"  => "hudson.slaves.RetentionStrategy$Always" },
      "nodeProperties"    => { "stapler-class-bag" => "true" },
      "launcher"          => {
        "stapler-class" => "hudson.plugins.sshslaves.SSHLauncher",
        "host"          => slave_host,
        "port"          => options[:slave_port],
        "username"      => options[:slave_user],
        "privatekey"    => options[:master_key],
      }
    }.to_json
  }

  url = URI.parse("#{base_uri}/computer/doCreateItem")

  req = Net::HTTP::Post.new(url.path)
  req.set_form_data(fields)

  http = Net::HTTP.new(url.host, url.port)

  response = http.request(req)
  case response
  when Net::HTTPFound
    { :name => name, :slave_host => slave_host }
  else
    # error message looks like:
    # <td id="main-panel">
    # <h1>Error</h1><p>Slave called 'localhost' already exists</p>
    require "hpricot"
    error = Hpricot(response.body).search("td#main-panel p").text
    unless error.blank?
      puts error
    else
      puts response.body # so we can find other errors
    end
    false
  end
end

.build_details(job_name, build_number) ⇒ Object

Return a hash of information about a build.



139
140
141
142
143
144
145
146
147
# File 'lib/jenkins/api.rb', line 139

def self.build_details(job_name, build_number)
  begin
    json = get "/job/#{job_name}/#{build_number}/api/json"
    cache_configuration!
    json
  rescue Crack::ParseError
    false
  end
end

.build_job(name) ⇒ Object



112
113
114
115
# File 'lib/jenkins/api.rb', line 112

def self.build_job(name)
  res = get_plain "/job/#{name}/build"
  res.code.to_i == 302
end

.cache_configuration!Object



272
273
274
275
276
# File 'lib/jenkins/api.rb', line 272

def self.cache_configuration!
  Jenkins::Config.config["base_uri"] = base_uri
  Jenkins::Config.config["basic_auth"] = default_options[:basic_auth]
  Jenkins::Config.store!
end

.console(job_name, axe, build_number) ⇒ Object

Return the console log information about a build.



150
151
152
153
154
155
156
157
# File 'lib/jenkins/api.rb', line 150

def self.console(job_name, axe, build_number)
  path  = "/job/#{job_name}/#{build_number}/"
  path << "#{axe}/" if axe
  path << "consoleText"
  log   = get_plain path
  cache_configuration!
  log.body
end

.create_job(name, job_config, options = {}) ⇒ Object

returns true if successfully create a new job on Jenkins job_config is a Jenkins::JobConfigBuilder instance options are:

:override - true, will delete any existing job with same name, else error

returns true if successful, else false

TODO Exceptions?



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/jenkins/api.rb', line 67

def self.create_job(name, job_config, options = {})
  options = options.with_clean_keys
  delete_job(name) if options[:override]
  begin
    res = post "/createItem/api/xml?name=#{CGI.escape(name)}", {
      :body => job_config.to_xml, :format => :xml, :headers => { 'content-type' => 'application/xml' }
    }
    if res.code.to_i == 200
      cache_configuration!
      true
    else
      show_me_the_error(res)
      false
    end
  rescue REXML::ParseException => e
    # For some reason, if the job exists we get back half a page of HTML
    raise JobAlreadyExistsError.new(name)
  end
end

.delete_job(name) ⇒ Object

Attempts to delete a job name



107
108
109
110
# File 'lib/jenkins/api.rb', line 107

def self.delete_job(name)
  res = post_plain "#{job_url name}/doDelete"
  res.code.to_i == 302
end

.delete_node(name) ⇒ Object



248
249
250
# File 'lib/jenkins/api.rb', line 248

def self.delete_node(name)
  post_plain("#{base_uri}/computer/#{CGI::escape(name).gsub('+', '%20')}/doDelete/api/json")
end

.get_plain(path, options = {}) ⇒ Object

Helper for GET that don’t barf at Jenkins’s crappy API responses



266
267
268
269
270
# File 'lib/jenkins/api.rb', line 266

def self.get_plain(path, options = {})
  options = options.with_clean_keys
  uri = URI.parse base_uri
  res = Net::HTTP.start(uri.host, uri.port) { |http| http.get(path, options) }
end

.job(name) ⇒ Object

Return hash of job statuses



128
129
130
131
132
133
134
135
136
# File 'lib/jenkins/api.rb', line 128

def self.job(name)
  begin
    json = get "/job/#{name}/api/json"
    cache_configuration!
    json
  rescue Crack::ParseError
    false
  end
end

.job_namesObject



123
124
125
# File 'lib/jenkins/api.rb', line 123

def self.job_names
  summary["jobs"].map {|job| job["name"]}
end

.nodesObject



159
160
161
162
163
# File 'lib/jenkins/api.rb', line 159

def self.nodes
  json = get "/computer/api/json"
  cache_configuration!
  json
end

.post_plain(path, data = "", options = {}) ⇒ Object

Helper for POST that don’t barf at Jenkins’s crappy API responses



253
254
255
256
257
258
259
260
261
262
263
# File 'lib/jenkins/api.rb', line 253

def self.post_plain(path, data = "", options = {})
  options = options.with_clean_keys
  uri = URI.parse base_uri
  res = Net::HTTP.start(uri.host, uri.port) do |http|
    if RUBY_VERSION =~ /1.8/
      http.post(path, options)
    else
      http.post(path, data, options)
    end
  end
end

.setup_base_url(options = {}) ⇒ Object



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
# File 'lib/jenkins/api.rb', line 22

def self.setup_base_url(options = {})
  # Handle single strings
  options = { :host => options } if options.is_a? String
  options = options.with_clean_keys
  # Thor's HashWithIndifferentAccess is based on string keys which URI::HTTP.build ignores
  options = options.inject({}) { |mem, (key, val)| mem[key.to_sym] = val; mem }

  # Handle URL style hosts by parsing the URL
  if options.keys.length == 1 && options.key?(:host)
    parsed_uri = URI::parse(options[:host])
    options = {
        :host => parsed_uri.host,
        :port => parsed_uri.port,
        :path => parsed_uri.path,
        :ssl => parsed_uri.scheme == 'https'
        }
  end

  options = setup_authentication(options)
  options[:host] ||= ENV['JENKINS_HOST']
  options[:port] ||= ENV['JENKINS_PORT']
  options[:port] &&= options[:port].to_i

  if options[:host]
    uri_class = options.delete(:ssl) ? URI::HTTPS : URI::HTTP
    uri = uri_class.build(options)
  else
    if Jenkins::Config.config["base_uri"]
      uri = Jenkins::Config.config["base_uri"]
    else
      return false # Nothing to work with.
    end
  end
  base_uri uri.to_s
  uri
end

.summaryObject



117
118
119
120
121
# File 'lib/jenkins/api.rb', line 117

def self.summary
  json = get "/api/json"
  cache_configuration!
  json
end

.update_job(name, job_config) ⇒ Object

returns true if successfully updated a job on Jenkins job_config is a Jenkins::JobConfigBuilder instance

returns true if successful, else false

TODO Exceptions?



93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/jenkins/api.rb', line 93

def self.update_job(name, job_config)
  res = post "#{job_url name}/config.xml", {
    :body => job_config.to_xml, :format => :xml, :headers => { 'content-type' => 'application/xml' }
  }
  if res.code.to_i == 200
    cache_configuration!
    true
  else
    show_me_the_error(res)
    false
  end
end