Class: DynportTools::Jenkins

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

Defined Under Namespace

Classes: Project, RemoteProject

Constant Summary collapse

CONFIGURED_PROJECTS_HASH =
:configured_projects_hash

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



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

def cache
  @cache ||= {}
end

#clear_cacheObject



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

def clear_cache
  cache.clear
end

#configured_projectsObject



91
92
93
# File 'lib/dynport_tools/jenkins.rb', line 91

def configured_projects
  configured_projects_hash.values
end

#configured_projects_hashObject



87
88
89
# File 'lib/dynport_tools/jenkins.rb', line 87

def configured_projects_hash
  @configured_projects_hash ||= {}
end

#configured_projects_hash=(new_hash) ⇒ Object



83
84
85
# File 'lib/dynport_tools/jenkins.rb', line 83

def configured_projects_hash=(new_hash)
  @configured_projects_hash = new_hash
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



53
54
55
# File 'lib/dynport_tools/jenkins.rb', line 53

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

#exists_remotely?(project) ⇒ Boolean

Returns:

  • (Boolean)


102
103
104
# File 'lib/dynport_tools/jenkins.rb', line 102

def exists_remotely?(project)
  remote_projects.keys.include?(project.name)
end

#hydraObject



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

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

#not_configured_projectsObject



118
119
120
# File 'lib/dynport_tools/jenkins.rb', line 118

def not_configured_projects
  remote_projects.values.select { |project| !configured_projects_hash.keys.include?(project.name) }
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)
  clear_cache
  Typhoeus::Request.post(*["#{url}/#{path}", options].compact)
end

#project_detailsObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/dynport_tools/jenkins.rb', line 66

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



57
58
59
60
61
62
63
64
# File 'lib/dynport_tools/jenkins.rb', line 57

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

#projects_to_createObject



110
111
112
# File 'lib/dynport_tools/jenkins.rb', line 110

def projects_to_create
  configured_projects.select { |project| !project.deleted? && !exists_remotely?(project) }
end

#projects_to_deleteObject



106
107
108
# File 'lib/dynport_tools/jenkins.rb', line 106

def projects_to_delete
  configured_projects.select { |project| project.deleted? && exists_remotely?(project) }
end

#projects_to_updateObject



114
115
116
# File 'lib/dynport_tools/jenkins.rb', line 114

def projects_to_update
  configured_projects.select { | project| exists_remotely?(project) && !project.deleted? && (project.md5 != remote_projects[project.name].md5) }
end

#remote_projectsObject



95
96
97
98
99
100
# File 'lib/dynport_tools/jenkins.rb', line 95

def remote_projects
  project_details.inject({}) do |hash, (url, project_hash)|
    hash[project_hash[:name]] = RemoteProject.from_details_hash(project_hash)
    hash
  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