-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.rb
144 lines (119 loc) · 2.9 KB
/
app.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
require 'docker_registry2'
require 'json'
require 'net/http'
require 'redis'
require 'sinatra'
require 'uri'
require 'yaml'
require './github'
[
'GITHUB_TOKEN',
'REDIS_URL'
].each do |var|
if ENV[var].nil? || ENV[var].empty?
puts "Missing env var: #{var}"
exit -1
end
end
set :ndk_info, YAML.load_file('ndk_info.yml')
set :icon_cache, Hash.new
set :github_client, Github.new(ENV['GITHUB_TOKEN'])
set :dhub_client, DockerRegistry2.connect()
set :redis_client, Redis.new(url: ENV['REDIS_URL'])
helpers do
def current_page?(path = '')
request.path_info == "/#{path}"
end
def partial(page, options={})
erb page, options.merge!(:layout => false)
end
def render_flash(type)
partial(:"partials/#{type}")
end
def tags_data
data = settings.redis_client.get('tags')
if data.nil?
begin
tags = settings.dhub_client.tags('rhardih/stand')
rescue RestClient::NotFound => e
tags = { "tags" => [] }
end
settings.redis_client.set('tags', tags.to_json)
settings.redis_client.expire('tags', 300)
return tags
end
JSON.parse(data)
end
def tag_built?(ndk, platform, toolchain)
tags_data["tags"].include?([ndk, platform, toolchain].join('--'))
end
def production?
ENV['RACK_ENV'] == 'production'
end
def method_missing(id, *args, &block)
if id =~ /svg_/
# memoize file reads
settings.icon_cache[args.first] ||=
File.read(File.join('public', 'images', args.first))
else
super
end
end
end
get '/' do
locals = {
github: {
busy: settings.github_client.active_workflow_runs?
},
twidth: 7
}
erb :index, locals: locals
end
get '/build/:ndk/:platform/:toolchain' do
locals = {
github: {
busy: settings.github_client.active_workflow_runs?
}
}
if settings.github_client.active_workflow_runs?
locals[:flash] = :github_busy_warning
end
erb :build, locals: locals
end
post '/build/:ndk/:platform/:toolchain' do
locals = {
github: {
busy: settings.github_client.active_workflow_runs?
}
}
if settings.github_client.active_workflow_runs?
locals[:flash] = :github_busy_error
else
begin
# TODO: Sanitize this
res = settings.github_client.trigger_build(
ndk: params[:ndk],
platform: params[:platform],
toolchain: params[:toolchain],
tag: [
params[:ndk],
params[:platform],
params[:toolchain]
].join("--")
)
locals[:github][:busy] = true
case res
when Net::HTTPSuccess, Net::HTTPRedirection
locals[:flash] = :github_build_started
when Net::HTTPTooManyRequests
locals[:flash] = :github_rate_limit
else
locals[:flash] = :unknown_error
end
rescue Net::HTTPServerException, Net::HTTPError => e
puts e
locals[:flash] = :unknown_error
end
end
erb :build, locals: locals
end