forked from martinjandrews/greenscreen
-
Notifications
You must be signed in to change notification settings - Fork 5
/
greenscreen.rb
104 lines (88 loc) · 2.67 KB
/
greenscreen.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
require 'rubygems'
require 'bundler/setup'
require 'sinatra'
require 'erb'
require 'rexml/document'
require 'hpricot'
require 'open-uri'
require 'yaml'
require 'erb'
require 'timeout' # to catch error
get '/' do
servers = load_servers
return "Add the details of build server to the config.yml file to get started" unless servers
@projects = []
servers.each do |server|
open_opts = {}
if server["username"] || server["password"]
open_opts[:http_basic_authentication] = [ server["username"], server["password"] ]
end
begin
xml = REXML::Document.new(open(server["url"], open_opts))
@projects.push(*accumulate_projects(server, xml))
rescue => e
@projects.push(MonitoredProject.server_down(server, e))
rescue Timeout::Error => e
@projects.push(MonitoredProject.server_down(server, e))
end
end
@projects = @projects.sort_by { |p| p.name.downcase }
@columns = 1.0
@columns = 2.0 if @projects.size > 4
@columns = 3.0 if @projects.size > 10
@columns = 4.0 if @projects.size > 21
@rows = (@projects.size / @columns).ceil
erb :index
end
def load_servers
YAML.load(StringIO.new(ERB.new(File.read 'config.yml').result))
end
def accumulate_projects(server, xml)
projects = xml.elements["//Projects"]
job_matchers =
if server["jobs"]
server["jobs"].collect do |j|
if j =~ %r{^/.*/$}
Regexp.new(j[1..(j.size-2)])
else
Regexp.new("^#{Regexp.escape(j)}$")
end
end
end
projects.collect do |project|
monitored_project = MonitoredProject.create(project)
if job_matchers
if job_matchers.detect { |matcher| monitored_project.name =~ matcher }
monitored_project
end
else
monitored_project
end
end.flatten.compact
end
class MonitoredProject
attr_accessor :name, :last_build_status, :activity, :last_build_time, :web_url, :last_build_label
def self.create(project)
MonitoredProject.new.tap do |mp|
mp.activity = project.attributes["activity"]
mp.last_build_time = Time.parse(project.attributes["lastBuildTime"]).localtime
mp.web_url = project.attributes["webUrl"]
mp.last_build_label = project.attributes["lastBuildLabel"]
mp.last_build_status = project.attributes["lastBuildStatus"].downcase
mp.name = project.attributes["name"]
end
end
def self.server_down(server, e)
MonitoredProject.new.tap do |mp|
mp.name = e.to_s
mp.last_build_time = Time.now.localtime
mp.last_build_label = server["url"]
mp.web_url = server["url"]
mp.last_build_status = "Failure"
mp.activity = "Sleeping"
end
end
def building?
self.activity =~ /building/i
end
end