Class: Hudkins

Inherits:
Object
  • Object
show all
Includes:
Common
Defined in:
lib/hudkins.rb,
lib/hudkins/jobs.rb,
lib/hudkins/mixin.rb,
lib/hudkins/common.rb,
lib/hudkins/errors.rb,
lib/hudkins/restclient.rb,
lib/hudkins/command/exec.rb,
lib/hudkins/command/irb_start.rb

Overview

attr_accessor_from_config :method_name, “search_path”[, type] attr_reader_from_config :method_name, “search_path”[, type] attr_writer_from_config :method_name, “search_path”[, type] ++

Defined Under Namespace

Modules: Common, HostLookup, Mixin Classes: ArgumentError, Command, Job, Jobs, Response

Constant Summary collapse

VERSION =
'0.0.1'

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Common

#check_host_availability, #host_available?, #hudkins, #inspect, #url_escape

Constructor Details

#initialize(host = "http://example.com", opts = {}) ⇒ Hudkins

Examples

Hudkins.new <host_name> [opts]

<host_name> will be URI parsed

Options

host_timeout

number of seconds to timeout trying to connect to the server. see Hudkins#host_available?



38
39
40
41
42
# File 'lib/hudkins.rb', line 38

def initialize(host = "http://example.com", opts = {})
  @host = URI.parse( ENV["hudkins_host"] || host )
  @options = opts
  @resource = RestClient::Resource.new @host.to_s
end

Instance Attribute Details

#hostObject

Returns the value of attribute host.



27
28
29
# File 'lib/hudkins.rb', line 27

def host
  @host
end

#resourceObject (readonly)

Returns the value of attribute resource.



27
28
29
# File 'lib/hudkins.rb', line 27

def resource
  @resource
end

Instance Method Details

#add_job(job_name, config_data = new_config) ⇒ Object

Description

TODO this needs cleaned up to be more like copy_job Use remote api to create a new job. Updates internal job list (Hudkins#jobs) afterwards

Example

hud.add_job :job_name, "<?xml..>"

Options

job_name

String or Symbol used as the name of the new job.

config_data

Uses provided template for bare-bones config, but optionally takes a string parameter (such as xml from another job’s config)

Notes

The remote api here is not fun. It uses HTTP#post instead of HTTP#create (which is normal) but the error messages are not very useful.



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/hudkins.rb', line 139

def add_job job_name, config_data = new_config
  # yuck..
  job = update_jobs.find_by_name( job_name )
  unless job
    response = post "/createItem?" + url_escape(:name => job_name), config_data, :content_type => "text/xml"
    if response.success?
      update_jobs.find_by_name job_name
    else
      case response.code
      when 400
        warn "the server returned an error. most likely the job name already exists."
        jobs.find_by_name( job_name ) || ( raise response.errors )
      else
        warn "there was a problem."
        raise response.errors
      end
    end
  else
    job
  end
end

#copy_job(job, new_job) ⇒ Object

Description

Copy a job

Examples

new_job = hud.copy_job "job-name", "new-job-name"

job = hud.find_by_name "job-name"
new_job = hud.copy_job job, "new-job-name"


170
171
172
173
# File 'lib/hudkins.rb', line 170

def copy_job job, new_job
  job = Hudkins::Job === job ? job : jobs.find_by_name( job )
  job.copy( new_job ) if job # find_by_name didn't return nil
end

#get(path = nil, opts = {}, &block) ⇒ Object

Description

Available to make arbitrary HTTP/get calls to the server. Returns an Hudkins::Response object. (see that class for reasoning.)

Parameters

path

“/path/to/resource”

opts

=> “text/plain” (default)

block

{ optional return block for RestClient#get }



86
87
88
# File 'lib/hudkins.rb', line 86

def get path = nil, opts = {}, &block
  use_resource :get, path, nil, opts, &block
end

#get_parsed(*args) ⇒ Object

Description

Same as #get but attempt to parse the response body. Raise unless Response#success?



107
108
109
# File 'lib/hudkins.rb', line 107

def get_parsed *args
  parse_response get(*args)
end

#jobsObject

Description

Access to internal list of jobs. see Hudkins::Jobs

One inital api call is made and then cached. See Hudkins#initialize_jobs



65
66
67
# File 'lib/hudkins.rb', line 65

def jobs
  @jobs ||= initialize_jobs
end

#parse_string(string, format) ⇒ Object



185
186
187
188
189
190
191
192
193
194
# File 'lib/hudkins.rb', line 185

def parse_string string, format
  case format
  when :xml then
    Nokogiri::XML string
  when :json then
    JSON.parse string
  else
    raise "unsupported type #{format.inspect}"
  end
end

#post(path = nil, data = "", opts = {}, &block) ⇒ Object

Description

Available to make arbitrary HTTP/post calls to the server.

Parameters

path

“/path/to/resource”

data

“<?xml…>” (any object that responds to to_s).

opts

=> “text/plain” (default)

block

{ optional return block for RestClient#get }



99
100
101
# File 'lib/hudkins.rb', line 99

def post path = nil, data = "", opts = {}, &block
  use_resource :post, path, data, opts, &block
end

#post_parsed(*args) ⇒ Object

Description

Same as #post but attempt to parse the response body. Raise unless Response#success?



115
116
117
# File 'lib/hudkins.rb', line 115

def post_parsed *args
  parse_response post(*args)
end

#reset_resource=(uri = host, opts = {}) ⇒ Object

Description

Update Hudkins#resource with new host name.



56
57
58
# File 'lib/hudkins.rb', line 56

def reset_resource= uri = host, opts = {}
  @resource = RestClient::Resource.new( uri.to_s, opts)
end

#server_versionObject

Description

Gets the hudson version the server is running

Examples

hud.server_version # => "1.37.0"


181
182
183
# File 'lib/hudkins.rb', line 181

def server_version
  get.response.headers[:x_hudson]
end

#update_jobsObject

Description

Reload jobs from the server



72
73
74
75
# File 'lib/hudkins.rb', line 72

def update_jobs
  # I might need to reinitiailze
  @jobs = initialize_jobs
end