Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 100 lines (85 sloc) 2.721 kB
8e460ca @martinjandrews Got enough working now to set up a build monitor of a hudson build.
martinjandrews authored
1 require 'rubygems'
1f48193 @rsutphin Bundlerify.
rsutphin authored
2 require 'bundler/setup'
3
8e460ca @martinjandrews Got enough working now to set up a build monitor of a hudson build.
martinjandrews authored
4 require 'sinatra'
5 require 'erb'
6 require 'rexml/document'
7 require 'hpricot'
8 require 'open-uri'
dc10389 @rsutphin Allow config.yml to include ERB blocks.
rsutphin authored
9 require 'yaml'
10 require 'erb'
0de5641 @rsutphin Rescue Timeout:Error separately since it isn't a subclass of Standard…
rsutphin authored
11 require 'timeout' # to catch error
8e460ca @martinjandrews Got enough working now to set up a build monitor of a hudson build.
martinjandrews authored
12
13 get '/' do
dc10389 @rsutphin Allow config.yml to include ERB blocks.
rsutphin authored
14 servers = load_servers
8e460ca @martinjandrews Got enough working now to set up a build monitor of a hudson build.
martinjandrews authored
15 return "Add the details of build server to the config.yml file to get started" unless servers
34ce1cd @rsutphin Whitespace
rsutphin authored
16
7d4911d @martinjandrews added support for explicitly listing jobs on a server
martinjandrews authored
17 @projects = []
18
8e460ca @martinjandrews Got enough working now to set up a build monitor of a hudson build.
martinjandrews authored
19 servers.each do |server|
a524150 @rsutphin Only try authentication if credentials are provided.
rsutphin authored
20 open_opts = {}
21 if server["username"] || server["password"]
22 open_opts[:http_basic_authentication] = [ server["username"], server["password"] ]
23 end
cc97f69 @rsutphin If loading a server's XML fails, report that that server is down on t…
rsutphin authored
24 begin
25 xml = REXML::Document.new(open(server["url"], open_opts))
26 @projects.push(*accumulate_projects(server, xml))
27 rescue => e
28 @projects.push(MonitoredProject.server_down(server, e))
0de5641 @rsutphin Rescue Timeout:Error separately since it isn't a subclass of Standard…
rsutphin authored
29 rescue Timeout::Error => e
30 @projects.push(MonitoredProject.server_down(server, e))
7d4911d @martinjandrews added support for explicitly listing jobs on a server
martinjandrews authored
31 end
8e460ca @martinjandrews Got enough working now to set up a build monitor of a hudson build.
martinjandrews authored
32 end
33
07576b3 @rsutphin Sort all projects across servers.
rsutphin authored
34 @projects = @projects.sort_by { |p| p.name.downcase }
52726e7 @dlindahl "building" or "failed" projects are displayed in 90% of the screen to…
authored
35 @successful_projects = @projects.select { |p| p.last_build_status == 'success' }
36 @unsuccessful_projects = @projects.select { |p| p.last_build_status != 'success' }
8e460ca @martinjandrews Got enough working now to set up a build monitor of a hudson build.
martinjandrews authored
37
38 erb :index
39 end
40
dc10389 @rsutphin Allow config.yml to include ERB blocks.
rsutphin authored
41 def load_servers
42 YAML.load(StringIO.new(ERB.new(File.read 'config.yml').result))
43 end
44
cc97f69 @rsutphin If loading a server's XML fails, report that that server is down on t…
rsutphin authored
45 def accumulate_projects(server, xml)
46 projects = xml.elements["//Projects"]
47
48 job_matchers =
49 if server["jobs"]
50 server["jobs"].collect do |j|
51 if j =~ %r{^/.*/$}
52 Regexp.new(j[1..(j.size-2)])
53 else
54 Regexp.new("^#{Regexp.escape(j)}$")
55 end
56 end
57 end
58
59 projects.collect do |project|
60 monitored_project = MonitoredProject.create(project)
61 if job_matchers
62 if job_matchers.detect { |matcher| monitored_project.name =~ matcher }
63 monitored_project
64 end
65 else
66 monitored_project
67 end
77e302f @rsutphin Remove nil entries in accumulated projects.
rsutphin authored
68 end.flatten.compact
cc97f69 @rsutphin If loading a server's XML fails, report that that server is down on t…
rsutphin authored
69 end
70
7d4911d @martinjandrews added support for explicitly listing jobs on a server
martinjandrews authored
71 class MonitoredProject
cc97f69 @rsutphin If loading a server's XML fails, report that that server is down on t…
rsutphin authored
72 attr_accessor :name, :last_build_status, :activity, :last_build_time, :web_url, :last_build_label
73
74 def self.create(project)
75 MonitoredProject.new.tap do |mp|
76 mp.activity = project.attributes["activity"]
77 mp.last_build_time = Time.parse(project.attributes["lastBuildTime"]).localtime
78 mp.web_url = project.attributes["webUrl"]
79 mp.last_build_label = project.attributes["lastBuildLabel"]
80 mp.last_build_status = project.attributes["lastBuildStatus"].downcase
81 mp.name = project.attributes["name"]
82 end
83 end
84
85 def self.server_down(server, e)
86 MonitoredProject.new.tap do |mp|
87 mp.name = e.to_s
88 mp.last_build_time = Time.now.localtime
89 mp.last_build_label = server["url"]
90 mp.web_url = server["url"]
91 mp.last_build_status = "Failure"
92 mp.activity = "Sleeping"
93 end
8e460ca @martinjandrews Got enough working now to set up a build monitor of a hudson build.
martinjandrews authored
94 end
bcb9310 @rsutphin Presentation refinements:
rsutphin authored
95
96 def building?
97 self.activity =~ /building/i
98 end
34ce1cd @rsutphin Whitespace
rsutphin authored
99 end
Something went wrong with that request. Please try again.