-
Notifications
You must be signed in to change notification settings - Fork 2
/
baza.rb
337 lines (311 loc) · 8.57 KB
/
baza.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# frozen_string_literal: true
# MIT License
#
# Copyright (c) 2009-2024 Zerocracy
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
$stdout.sync = true
require 'always'
require 'cgi'
require 'fileutils'
require 'glogin'
require 'glogin/codec'
require 'haml'
require 'htmlcompressor'
require 'iri'
require 'json'
require 'loog'
require 'pgtk'
require 'pgtk/pool'
require 'securerandom'
require 'sinatra'
require 'sinatra/cookies'
require 'socket'
require 'time'
require 'total'
require 'truncate'
require 'yaml'
require 'zache'
require_relative 'version'
require_relative 'objects/baza/features'
# see https://stackoverflow.com/questions/78547207
disable :method_override
use Rack::RewindableInput::Middleware
use Rack::MethodOverride
use Rack::Deflater
use HtmlCompressor::Rack, {
enabled: true,
remove_spaces_inside_tags: true,
remove_multi_spaces: true,
remove_comments: true,
remove_intertag_spaces: false,
remove_quotes: false
}
Haml::Template.options[:escape_html] = true
Haml::Template.options[:format] = :xhtml
unless Baza::Features::TESTS
require 'rack/ssl'
use Rack::SSL
end
# Sinatra configs:
configure do
set :bind, '0.0.0.0'
set :server_settings, timeout: 25
end
# Global config data:
configure do
config = {
'sentry' => '',
'ipgeolocation' => '',
'tg' => {
'token' => '',
'admin_chat' => ''
},
's3' => {
'key' => '', # AWS authentication key
'secret' => '', # AWS secret
'region' => '', # S3 region
'bucket' => ''
},
'sqs' => {
'key' => '', # AWS authentication key
'secret' => '', # AWS secret
'region' => '', # SQS region
'url' => ''
},
'lambda' => {
'account' => '42424242',
'key' => 'FAKEFAKEFAKEFAKEFAKE',
'secret' => 'fakefakefakefakefakefakefakefakefakefake',
'region' => 'us-east-1',
'sgroup' => 'sg-42',
'subnet' => 'sn-42',
'image' => 'ami-42',
'id_rsa' => '',
'bucket' => 'foo'
},
'github' => {
'id' => '',
'secret' => '',
'encryption_secret' => ''
}
}
unless Baza::Features::TESTS
f = File.join(File.dirname(__FILE__), 'config.yml')
if File.exist?(f)
config = YAML.safe_load(File.open(f))
File.delete(f)
elsif ENV['YAML_CONFIG']
config = YAML.safe_load(ENV['YAML_CONFIG'])
else
raise \
"The config file #{f} is absent, can't start the app. " \
'Also, there is not YAML_CONFIG environment variable. ' \
'If you are running in a staging/testing mode, set RACK_ENV ' \
"envirornemt variable to 'test' and run again:\n" \
'RACK_ENV=test ruby baza.app -p 8888'
end
end
set :config, config
end
# Logging:
configure do
set :logging, false # to disable default Sinatra logging and use Loog
if Baza::Features::TESTS
set :loog, Loog::NULL
else
set :loog, Loog::VERBOSE
end
end
# PostgreSQL:
configure do
if File.exist?('target/pgsql-config.yml')
set :pgsql, Pgtk::Pool.new(
Pgtk::Wire::Yaml.new(File.join(__dir__, 'target/pgsql-config.yml')),
log: settings.loog
)
else
set :pgsql, Pgtk::Pool.new(
Pgtk::Wire::Env.new('DATABASE_URL'),
log: settings.loog
)
end
settings.pgsql.start(4)
end
# Telegram client:
configure do
require_relative 'objects/baza/tbot'
set :tbot, Baza::Tbot::Spy.new(
Baza::Tbot.new(
settings.pgsql,
settings.config['tg']['token'],
loog: settings.loog
),
settings.config['tg']['admin_chat']
)
set(:tg, Always.new(1).on_error { |e, _| settings.loog.error(Backtrace.new(e)) })
unless Baza::Features::TESTS
settings.tg.start do
settings.tbot.start
end
end
set :telegramers, {}
end
# Humans:
configure do
require_relative 'objects/baza/humans'
set :humans, Baza::Humans.new(settings.pgsql, tbot: settings.tbot, loog: settings.loog)
settings.humans.ensure('yegor256').notify(
"🍓 New version `#{ENV.fetch('HEROKU_RELEASE_VERSION', 'n/a')}:#{Baza::VERSION}` released",
"and started running at [#{Socket.ip_address_list.detect(&:ipv4_private?)&.ip_address}](//dash).",
"Ruby version is `#{RUBY_VERSION}`.",
"PostgreSQL version is `#{settings.pgsql.version}`.",
"Total memory on the server: #{Total::Mem.new.bytes / (1024 * 1024 * 1024)}Gb.",
"Features: #{Baza::Features.summary}."
)
end
# Trails:
configure do
require_relative 'objects/baza/trails'
set :trails, Baza::Trails.new(settings.pgsql)
end
# Factbases:
configure do
require_relative 'objects/baza/factbases'
set :fbs, Baza::Factbases.new(
settings.config['s3']['key'],
settings.config['s3']['secret'],
settings.config['s3']['region'],
settings.config['s3']['bucket'],
loog: settings.loog
)
end
# Amazon SQS:
configure do
require_relative 'objects/baza/sqs'
set :sqs, Baza::SQS.new(
settings.config['sqs']['key'],
settings.config['sqs']['secret'],
settings.config['sqs']['url'],
settings.config['sqs']['region'],
loog: settings.loog
)
end
# Ops with swarms:
configure do
require_relative 'objects/baza/ec2'
cfg = settings.config['lambda']
ec2 = Baza::EC2.new(
cfg['key'],
cfg['secret'],
cfg['region'],
cfg['sgroup'],
cfg['subnet'],
type: 't2.xlarge',
loog: settings.loog
)
require_relative 'objects/baza/ops'
set :ops, Baza::Ops.new(ec2, cfg['account'], cfg['id_rsa'], cfg['bucket'])
end
# Pipeline:
configure do
set(:pipeline, Always.new(1).on_error { |e, _| settings.loog.error(Backtrace.new(e)) })
unless Baza::Features::TESTS || !Baza::Features::PIPELINE
settings.pipeline.start(5) do
load('always/always_pipeline.rb', true)
end
end
end
# IPGeolocation client:
configure do
token = settings.config['ipgeolocation']
require_relative 'objects/baza/ipgeolocation'
set :ipgeolocation, Baza::IpGeolocation.new(
token:,
connection: Baza::Features::TESTS && token.empty? ? Baza::IpGeolocation::FakeConnection : Faraday
)
end
# Garbage collection:
configure do
set :gc, Always.new(1)
set :expiration_days, 14
unless Baza::Features::TESTS
settings.gc.start(30) do
load('always/always_gc.rb', true)
end
end
end
# Verify jobs:
configure do
set :verify, Always.new(1)
unless Baza::Features::TESTS
settings.verify.start(60) do
load('always/always_verify.rb', true)
end
end
end
# Donations:
configure do
set :donations, Always.new(1)
set :donation_amount, 8 * 100_000
set :donation_period, 30
unless Baza::Features::TESTS
settings.donations.start(60) do
load('always/always_donations.rb', true)
end
end
end
# Release all swarms:
configure do
set :release, Always.new(1)
unless Baza::Features::TESTS
settings.release.start(60) do
load('always/always_release.rb', true)
end
end
end
# Global in-memory cache.
configure do
set :zache, Zache.new
end
get '/' do
flash(iri.cut('/dash')) if @locals[:human]
assemble(:index, :front, title: '/')
end
get '/dash' do
assemble(:dash, :default, title: '/dash')
end
require_relative 'front/front_misc'
require_relative 'front/front_errors'
require_relative 'front/front_login'
require_relative 'front/front_admin'
require_relative 'front/front_tokens'
require_relative 'front/front_jobs'
require_relative 'front/front_account'
require_relative 'front/front_valves'
require_relative 'front/front_locks'
require_relative 'front/front_trails'
require_relative 'front/front_telegram'
require_relative 'front/front_secrets'
require_relative 'front/front_durables'
require_relative 'front/front_alterations'
require_relative 'front/front_swarms'
require_relative 'front/front_push'
require_relative 'front/front_assets'
require_relative 'front/front_helpers'