forked from code-dot-org/code-dot-org
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdeployment.rb
306 lines (253 loc) · 10.1 KB
/
deployment.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
$:.unshift File.expand_path('../lib', __FILE__)
$:.unshift File.expand_path('../shared/middleware', __FILE__)
# Set up gems listed in the Gemfile.
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __FILE__)
require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE'])
require 'csv'
require 'yaml'
require 'cdo/erb'
require 'cdo/slog'
require 'os'
def load_yaml_file(path)
return nil unless File.file?(path)
YAML.load(IO.read(path))
end
def load_languages(path)
[].tap do |results|
CSV.foreach(path, headers: true) do |row|
results << row['code_s!']
end
end
end
def load_configuration()
root_dir = File.expand_path('..', __FILE__)
root_dir = '/home/ubuntu/website-ci' if root_dir == '/home/ubuntu/Dropbox (Code.org)'
hostname = `hostname`.strip
global_config = load_yaml_file(File.join(root_dir, 'globals.yml')) || {}
local_config = load_yaml_file(File.join(root_dir, 'locals.yml')) || {}
env = local_config['env'] || global_config['env'] || ENV['RACK_ENV'] || ENV['RAILS_ENV'] || 'development'
rack_env = env.to_sym
{
'app_servers' => {},
'aws_region' => 'us-east-1',
'build_apps' => false,
'build_blockly_core' => false,
'build_dashboard' => true,
'build_pegasus' => true,
'build_shared_js' => [:development, :adhoc, :staging].include?(rack_env),
'dcdo_table_name' => "dcdo_#{rack_env}",
'dashboard_db_name' => "dashboard_#{rack_env}",
'dashboard_devise_pepper' => 'not a pepper!',
'dashboard_secret_key_base' => 'not a secret',
'dashboard_honeybadger_api_key' =>'00000000',
'dashboard_host' => 'localhost',
'dashboard_port' => 3000,
'dashboard_unicorn_name' => 'dashboard',
'dashboard_enable_pegasus' => rack_env == :development,
'dashboard_workers' => 8,
'db_reader' => 'mysql://root@localhost/',
'db_writer' => 'mysql://root@localhost/',
'reporting_db_reader' => 'mysql://root@localhost/',
'reporting_db_writer' => 'mysql://root@localhost/',
'gatekeeper_table_name' => "gatekeeper_#{rack_env}",
'hip_chat_log_room' => rack_env.to_s,
'hip_chat_logging' => false,
'home_dir' => File.expand_path('~'),
'languages' => load_languages(File.join(root_dir, 'pegasus', 'data', 'cdo-languages.csv')),
'localize_apps' => false,
'name' => hostname,
'newrelic_logging' => rack_env == :production,
'netsim_max_routers' => 20,
'netsim_shard_expiry_seconds' => 7200,
'npm_use_sudo' => ((rack_env != :development) && OS.linux?),
'partners' => %w(al ar br eu italia ro sg uk za),
'pdf_port_collate' => 8081,
'pdf_port_markdown' => 8081,
'pegasus_db_name' => rack_env == :production ? 'pegasus' : "pegasus_#{rack_env}",
'pegasus_honeybadger_api_key' =>'00000000',
'pegasus_port' => 3000,
'pegasus_unicorn_name' => 'pegasus',
'pegasus_workers' => 8,
'poste_host' => 'localhost.code.org:3000',
'poste_secret' => 'not a real secret',
'proxy' => false, # If true, generated URLs will not include explicit port numbers in development
'rack_env' => rack_env,
'rack_envs' => [:development, :production, :adhoc, :staging, :test, :levelbuilder, :integration],
'read_only' => false,
'ruby_installer' => rack_env == :development ? 'rbenv' : 'system',
'root_dir' => root_dir,
'use_dynamo_tables' => [:staging, :adhoc, :test, :production].include?(rack_env),
#'use_dynamo_properties' => [:staging, :adhoc, :test, :production].include?(rack_env),
'dynamo_tables_table' => "#{rack_env}_tables",
'dynamo_tables_index' => "channel_id-table_name-index",
'use_dynamo_properties' => false,
'dynamo_properties_table' => "#{rack_env}_properties",
'lint' => rack_env == :adhoc || rack_env == :staging || rack_env == :development,
'assets_s3_bucket' => 'cdo-v3-assets',
'assets_s3_directory' => rack_env == :production ? 'assets' : "assets_#{rack_env}",
'sources_s3_bucket' => 'cdo-v3-sources',
'sources_s3_directory' => rack_env == :production ? 'sources' : "sources_#{rack_env}",
'use_pusher' => false,
'pusher_app_id' => 'fake_app_id',
'pusher_application_key' => 'fake_application_key',
'pusher_application_secret' => 'fake_application_secret',
'videos_s3_bucket' => 'videos.code.org',
'videos_url' => '//videos.code.org'
}.tap do |config|
raise "'#{rack_env}' is not known environment." unless config['rack_envs'].include?(rack_env)
ENV['RACK_ENV'] = rack_env.to_s unless ENV['RACK_ENV']
#raise "RACK_ENV ('#{ENV['RACK_ENV']}') does not match configuration ('#{rack_env}')" unless ENV['RACK_ENV'] == rack_env.to_s
config['bundler_use_sudo'] = config['ruby_installer'] == 'system'
config.merge! global_config
config.merge! local_config
config['channels_api_secret'] ||= config['poste_secret']
config['daemon'] ||= [:development, :levelbuilder, :staging, :test].include?(rack_env) || config['name'] == 'production-daemon'
config['dashboard_db_reader'] ||= config['db_reader'] + config['dashboard_db_name']
config['dashboard_db_writer'] ||= config['db_writer'] + config['dashboard_db_name']
config['dashboard_reporting_db_reader'] ||= config['reporting_db_reader'] + config['dashboard_db_name']
config['dashboard_reporting_db_writer'] ||= config['reporting_db_writer'] + config['dashboard_db_name']
config['pegasus_db_reader'] ||= config['db_reader'] + config['pegasus_db_name']
config['pegasus_db_writer'] ||= config['db_writer'] + config['pegasus_db_name']
config['pegasus_reporting_db_reader'] ||= config['reporting_db_reader'] + config['pegasus_db_name']
config['pegasus_reporting_db_writer'] ||= config['reporting_db_writer'] + config['pegasus_db_name']
# Set AWS SDK environment variables from provided config.
ENV['AWS_ACCESS_KEY_ID'] ||= config['aws_access_key'] || config['s3_access_key_id']
ENV['AWS_SECRET_ACCESS_KEY'] ||= config['aws_secret_key'] || config['s3_secret_access_key']
ENV['AWS_DEFAULT_REGION'] ||= config['aws_region']
end
end
####################################################################################################
##
## CDO - A singleton that contains our settings and integration helpers.
##
##########
class CDOImpl < OpenStruct
@slog = nil
def initialize()
super load_configuration
end
def canonical_hostname(domain)
return "#{self.name}.#{domain}" if ['console', 'hoc-levels'].include?(self.name)
return domain if rack_env?(:production)
# our HTTPS wildcard certificate only supports *.code.org
# 'env', 'studio.code.org' over https must resolve to 'env-studio.code.org' for non-prod environments
sep = (domain.include?('.code.org')) ? '-' : '.'
return "localhost#{sep}#{domain}" if rack_env?(:development)
return "translate#{sep}#{domain}" if self.name == 'crowdin'
"#{rack_env}#{sep}#{domain}"
end
def dashboard_hostname
canonical_hostname('studio.code.org')
end
def pegasus_hostname
canonical_hostname('code.org')
end
def site_url(domain, path = '', scheme = '')
host = canonical_hostname(domain)
if rack_env?(:development) && !CDO.https_development
port = ['studio.code.org'].include?(domain) ? CDO.dashboard_port : CDO.pegasus_port
host += ":#{port}"
end
path = '/' + path unless path.empty? || path[0] == '/'
"#{scheme}//#{host}#{path}"
end
def studio_url(path = '', scheme = '')
site_url('studio.code.org', path, scheme)
end
def code_org_url(path = '', scheme = '')
site_url('code.org', path, scheme)
end
def dir(*dirs)
File.join(root_dir, *dirs)
end
def hosts_by_env(env)
hosts = []
GlobalConfig['hosts'].each_pair do |key, i|
hosts << i if i['env'] == env.to_s
end
hosts
end
def hostnames_by_env(env)
hosts_by_env(env).map{|i| i['name']}
end
def rack_env?(env)
rack_env == env
end
def slog(params)
return unless slog_token
@slog ||= Slog::Writer.new(secret: slog_token)
@slog.write params
end
def shared_image_url(path)
"/shared/images/#{path}"
end
# Default logger implementation
attr_writer :log
def log
@log ||= Logger.new(STDOUT).tap do |l|
l.level = Logger::INFO
l.formatter = proc do |severity, _, _, msg|
"#{severity != 'INFO' ? "#{severity}: " : ''}#{msg}\n"
end
end
end
# Simple backtrace filter
FILTER_GEMS = %w(rake)
def backtrace(exception)
filter_backtrace exception.backtrace
end
def filter_backtrace(backtrace)
FILTER_GEMS.map do |gem|
backtrace.reject!{|b| b =~ /gems\/#{gem}/}
end
backtrace.each do |b|
b.gsub!(CDO.dir, '[CDO]')
Gem.path.each do |gem|
b.gsub!(gem, '[GEM]')
end
end
backtrace.join("\n")
end
end
CDO ||= CDOImpl.new
####################################################################################################
##
## Helpers
##
##########
def rack_env()
CDO.rack_env
end
def rack_env?(env)
rack_env == env
end
def deploy_dir(*dirs)
CDO.dir(*dirs)
end
def aws_dir(*dirs)
deploy_dir('aws', *dirs)
end
def apps_dir(*dirs)
deploy_dir('apps', *dirs)
end
def blockly_core_dir(*dirs)
deploy_dir('blockly-core', *dirs)
end
def cookbooks_dir(*dirs)
deploy_dir('cookbooks', *dirs)
end
def dashboard_dir(*dirs)
deploy_dir('dashboard', *dirs)
end
def home_dir(*paths)
File.join(CDO.home_dir, *paths)
end
def pegasus_dir(*paths)
deploy_dir('pegasus', *paths)
end
def shared_dir(*dirs)
deploy_dir('shared', *dirs)
end
def shared_js_dir(*dirs)
deploy_dir('shared/js', *dirs)
end