This repository has been archived by the owner on Jan 16, 2024. It is now read-only.
forked from rsutphin/greenscreen
-
Notifications
You must be signed in to change notification settings - Fork 24
/
application.rb
104 lines (89 loc) · 2.49 KB
/
application.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
configure {
set :root, File.dirname(__FILE__)
}
#
# GET /
#
# Load the static HTML file
#
get '/' do
send_file File.join(settings.public_folder, 'index.html')
end
#
# GET /projects.json
#
# Load the initial build screen
#
get '/projects.json' do
load_projects
content_type 'application/json'
render :rabl, :index, :format => 'json'
end
#
# GET '/:project_name'
#
# Returns a PNG corresponding to the current build status
#
get '/:project_name.png' do |project_name|
load_projects
project = @projects.find { |project| project.name == project_name }
unless project.nil?
# if the project is done, show the status, otherwise, show it's building
project_state = project.built? ? project.last_build_status : project.current_status
image = case project_state
when 'passed' then 'passing'
when 'failed' then 'failing'
else 'unknown'
end
content_type 'image/png'
File.read("./public/images/status/#{image}.png")
else
status 404
end
end
private
# Load the servers and projects from the YAML file
def load_projects
@servers ||= YAML::load(File.read('config/servers.yml'))
@projects = @servers.collect do |server|
Thread.new do
server_projects = Timeout::timeout(5) do
get_server(server).projects
end
# if server['projects'] is defined, we only want those projects
Thread.current['projects'] = if server['projects']
regexes = collect_regexes(server['projects'])
server_projects.select { |server_project|
regexes.any?{ |regex| server_project.name =~ regex }
}
elsif server['ignored_projects']
regexes = collect_regexes(server['ignored_projects'])
server_projects.reject { |server_project|
regexes.any?{ |regex| server_project.name =~ regex }
}
else
server_projects
end
end
end.map(&:join).collect{ |t| t['projects'] }.compact.flatten.sort
end
def collect_regexes(projects)
projects.collect do |j|
if j =~ %r{^/.*/$}
Regexp.new(j[1..(j.size-2)])
else
Regexp.new("^#{Regexp.escape(j)}$")
end
end
end
# Attemps to find the class associated with a given server type.
# Throws an exception if the class is not found
def get_server(server)
server_type = server['type'].camelize
begin
klass = "Stoplight::Providers::#{server_type}".constantize
rescue NameError => e
raise Stoplight::Exceptions::UnknownProviderError, "Could not load provider with class name `#{server_type}`"
end
klass.new(server)
end