Class: DynportTools::Jenkins

Inherits:
Object
  • Object
show all
Defined in:
lib/dynport_tools/jenkins.rb

Defined Under Namespace

Classes: Project, RemoteProject

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url = nil) ⇒ Jenkins

Returns a new instance of Jenkins.



4
5
6
# File 'lib/dynport_tools/jenkins.rb', line 4

def initialize(url = nil)
  self.url = url
end

Instance Attribute Details

#urlObject

Returns the value of attribute url.



2
3
4
# File 'lib/dynport_tools/jenkins.rb', line 2

def url
  @url
end

Instance Method Details

#build_project(name) ⇒ Object



24
25
26
# File 'lib/dynport_tools/jenkins.rb', line 24

def build_project(name)
  send_to_project(name, "build")
end

#cacheObject



45
46
47
# File 'lib/dynport_tools/jenkins.rb', line 45

def cache
  @cache ||= {}
end

#create_project(name, xml) ⇒ Object



12
13
14
# File 'lib/dynport_tools/jenkins.rb', line 12

def create_project(name, xml)
  post_request "createItem?name=#{escape_job_name(name)}", :headers => { "Content-Type" => "application/xml" }, :body => xml
end

#delete_project(name) ⇒ Object



20
21
22
# File 'lib/dynport_tools/jenkins.rb', line 20

def delete_project(name)
  send_to_project(name, "doDelete")
end

#disable_project(name) ⇒ Object



28
29
30
# File 'lib/dynport_tools/jenkins.rb', line 28

def disable_project(name)
  send_to_project(name, "disable")
end

#enable_project(name) ⇒ Object



32
33
34
# File 'lib/dynport_tools/jenkins.rb', line 32

def enable_project(name)
  send_to_project(name, "enable")
end

#escape_job_name(name) ⇒ Object



49
50
51
# File 'lib/dynport_tools/jenkins.rb', line 49

def escape_job_name(name)
  URI.escape(name)
end

#hydraObject



8
9
10
# File 'lib/dynport_tools/jenkins.rb', line 8

def hydra
  @hydra ||= Typhoeus::Hydra.new
end

#post_request(path, options = nil) ⇒ Object



40
41
42
43
# File 'lib/dynport_tools/jenkins.rb', line 40

def post_request(path, options = nil)
  @cache = {}
  Typhoeus::Request.post(*["#{url}/#{path}", options].compact)
end

#project_detailsObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/dynport_tools/jenkins.rb', line 62

def project_details
  return cache[:projects_details] if cache[:projects_details]
  jobs = {}
  projects_hash.each do |url, job|
    request = Typhoeus::Request.new("#{url}config.xml")
    request.on_complete do |response|
      xml = Nokogiri::XML(response.body).to_s
      jobs[url] = job.merge(:body => xml, :md5 => Digest::MD5.hexdigest(xml))
    end
    hydra.queue(request)
  end
  hydra.run
  cache[:projects_details] = jobs
end

#projects_hashObject



53
54
55
56
57
58
59
60
# File 'lib/dynport_tools/jenkins.rb', line 53

def projects_hash
  cache[:projects_hash] ||= Nokogiri::XML(Typhoeus::Request.get("#{url}/api/xml").body).search("job").inject({}) do |hash, node|
    url = node.at("url").inner_text.strip if node.at("url")
    name = node.at("name").inner_text.strip if node.at("name")
    hash[url] = { :url => url, :name => name }
    hash
  end
end

#remote_projectsObject



77
78
79
80
81
# File 'lib/dynport_tools/jenkins.rb', line 77

def remote_projects
  project_details.inject({}) do |hash, (url, project_hash)|
    hash.merge!(project_hash[:name] => RemoteProject.new(:url => project_hash[:url], :name => project_hash[:name], :xml => project_hash[:body]))
  end
end

#send_to_project(name, action) ⇒ Object



36
37
38
# File 'lib/dynport_tools/jenkins.rb', line 36

def send_to_project(name, action)
  post_request "job/#{escape_job_name(name)}/#{action}"
end

#update_project(name, xml) ⇒ Object



16
17
18
# File 'lib/dynport_tools/jenkins.rb', line 16

def update_project(name, xml)
  post_request "job/#{escape_job_name(name)}/config.xml", :headers => { "Content-Type" => "application/xml" }, :body => xml
end