-
Notifications
You must be signed in to change notification settings - Fork 3
/
updateserver.rb
executable file
·278 lines (222 loc) · 6.77 KB
/
updateserver.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#!/usr/bin/env ruby
require 'sinatra'
require 'sinatra/config_file'
require 'thin'
require 'json'
require 'date'
require 'geoip'
require_relative 'lib/models.rb'
require_relative 'lib/scraper.rb'
require_relative 'lib/stats.rb'
$CHANNELS = {
"16" => "stable",
"2" => "prerelease",
"4" => "beta",
}
geofile = "#{File.join(File.dirname(File.expand_path(__FILE__)),'geoip','GeoLiteCity.dat')}"
puts geofile
$GEOIP = GeoIP.new(geofile)
class UpdateHTTP < Sinatra::Base
# threaded - False: Will take requests on the reactor thread
# True: Will queue request for background thread
def initialize(job, settings)
puts job.nil?
@statsJob = job
@settings = settings
super()
end
set :public_folder, 'public'
configure do
set :threaded, false
end
# Send a response for pingdom
get '/' do
status 200
body "pong"
end
get '/stats' do
status 200
erb :stats
end
get '/json/stats' do
status 200
headers \
"Access-Control-Allow-Origin" => [ "http://www.rasplex.com", "https://www.rasplex.com"]
body @statsJob.cachedStats
end
# Callback once a client is done updating
get '/updated' do
status 200
current_time = DateTime.now
puts "Got #{params}"
saveUpdateComplete(current_time, params, request.ip)
body "Thanks for updating"
end
# Request to list available updates in channel
get '/update' do
status 200
current_time = DateTime.now
releases = selectReleases(current_time, params, request.ip, @settings.serials)
puts "Got #{params}"
erb :update, :locals => { :releases => releases }
end
# Request to list available install images
get '/install' do
status 200
body JSON.dump Release.all
begin
puts "Got #{params}"
current_time = DateTime.now
saveInstallRequest(current_time, params, request.ip)
candidates = Release.all(:channel => 'stable')
candidates += Release.all(:channel => 'prerelease')
body JSON.dump candidates
rescue
puts "Error saving install information..."
end
end
post "/crashes" do
if params.has_key? "dumpfileb64" and params.has_key? "serial" \
and params.has_key? "revision" and params.has_key? "submitter_version" \
and params.has_key? "version"
crash = Crash.new(
:serial => params[:serial],
:hwrev => params[:revision],
:version => params[:version],
:submitter_version => params[:submitter_version],
:ipaddr => request.ip,
:time => DateTime.now
)
if crash.save
puts "Created crash id #{crash.id}"
else
crash.errors.each do |e|
puts e
end
end
id = crash.id
crashpath = "crashdata/crash_#{id}.dump"
crash.crash_path = crashpath
crash.save
File.open( crashpath , "wb") do |f|
f.write(Base64.decode64(params[:dumpfileb64]))
end
puts "created crash #{id}"
return "#{id}"
else
return "Invalid crash report, missing params"
end
end
end
def selectReleases(current_time, params, source, settings)
saveUpdateRequest(current_time, params, source)
channel = "stable"
if params["channel"] and $CHANNELS.has_key? params["channel"]
channel = $CHANNELS[params["channel"]]
end
puts "Using channel #{channel}"
# Only whitelisted beta users can download
serials = settings.values().join(',').split(',')
puts "Whitelisted serials: "+serials.to_s
if channel != "beta" or serials.include? params["serial"]
candidates = Release.all(:channel => channel.downcase)
else
candidates = []
end
stable = Release.all(:channel => "stable")
if channel != "stable" and not stable.nil? and stable.length > 0
stable.each do | candidate |
candidates.push candidate
end
end
releases = []
candidates.each do | candidate |
# Do any filtering here
releases.push candidate unless candidate.update_url.nil?
end
return releases
end
class UpdateServer
def initialize(settings)
@server = self
@settings = settings
@stats = nil
if ENV['UPDATER_ENVIRONMENT'] == "production"
puts "#{Time.now.utc} Running as production"
db_url = "mysql://#{settings.db['user']}:#{settings.db['password']}@#{settings.db['hostname']}/#{settings.db['dbname']}"
else
db_path = "#{File.join(File.dirname(File.expand_path(__FILE__)),'db','development.db')}"
puts "Using DB at #{db_path}"
db_url = "sqlite3://#{db_path}"
end
DataMapper.setup :default, db_url
DataMapper.finalize
DataMapper.auto_upgrade!
# UpdateRequest.auto_migrate!
# Release.auto_migrate!
end
def getStatsJob()
return @stats
end
def start_scraping( interval )
scraper = ScraperJob.new( interval, @settings.github_api_token )
scraper.scrape()
end
def start_stats( interval )
@stats = StatsJob.new( interval, $GEOIP )
puts @stats.nil?
end
def run()
# Start the reactor
EM.run do
server = @settings.server # 'thin'
host = @settings.host # '0.0.0.0'
port = @settings.port # '9000'
interval = @settings.interval # '120'
sinatra = @server
start_stats( interval )
stats = @stats
settings = @settings
dispatch = Rack::Builder.app do
map '/' do
run UpdateHTTP.new(stats, settings)
end
end
# NOTE that we have to use an EM-compatible web-server. There
# might be more, but these are some that are currently available.
unless ['thin', 'hatetepe', 'goliath'].include? server
raise "Need an EM webserver, but #{server} isn't"
end
# Start the web server. Note that you are free to run other tasks
# within your EM instance.
Rack::Server.start({
app: dispatch,
server: server,
Host: host,
Port: port
})
init_sighandlers
start_scraping(interval)
end
end
# Needed during testing
def init_sighandlers
trap(:INT) {"Got interrupt"; EM.stop(); exit }
trap(:TERM) {"Got term"; EM.stop(); exit }
trap(:KILL) {"Got kill"; EM.stop(); exit }
end
end
# Load configs
config = "#{File.join(File.dirname(File.expand_path(__FILE__)),'config','config.yml')}"
database_config = "#{File.join(File.dirname(File.expand_path(__FILE__)),'config','database.yml')}"
secrets_config = "#{File.join(File.dirname(File.expand_path(__FILE__)),'config','secrets.yml')}"
whitelist = "#{File.join(File.dirname(File.expand_path(__FILE__)),'whitelist.yml')}"
config_file config
config_file database_config
config_file secrets_config
config_file whitelist
$stdout.sync = true
$stderr.sync = true
# start the application
updateServer = UpdateServer.new(settings)
updateServer.run