OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 | 2 |
3 import sys, os, pwd, subprocess | 3 import sys, os, pwd, subprocess |
4 | 4 |
5 app_dir = '/opt/discourse' | 5 app_dir = '/opt/discourse' |
6 airbrake_key = os.environ.get('AIRBRAKE_KEY', None) | |
7 | 6 |
8 # HOME environment variable isn't reliable when called via sudo | 7 # HOME environment variable isn't reliable when called via sudo |
9 home_dir = os.path.expanduser('~' + pwd.getpwuid(os.getuid()).pw_name) | 8 home_dir = os.path.expanduser('~' + pwd.getpwuid(os.getuid()).pw_name) |
10 | 9 |
11 env = dict(os.environ) | 10 env = dict(os.environ) |
12 env['RAILS_ENV'] = 'production' | 11 env['RAILS_ENV'] = 'production' |
13 | 12 |
14 # See http://meta.discourse.org/t/tuning-ruby-and-rails-for-discourse/4126 | 13 # See http://meta.discourse.org/t/tuning-ruby-and-rails-for-discourse/4126 |
15 env['RUBY_GC_MALLOC_LIMIT'] = '90000000' | 14 env['RUBY_GC_MALLOC_LIMIT'] = '90000000' |
16 | 15 |
17 def callRailsCommand(command): | 16 def callRailsCommand(command): |
18 subprocess.check_call(command, env=env, cwd=app_dir) | 17 subprocess.check_call(command, env=env, cwd=app_dir) |
19 | 18 |
20 def initAirBrake(): | |
21 if airbrake_key == None: | |
22 return | |
23 | |
24 path = os.path.join(app_dir, 'config', 'initializers', 'airbrake.rb') | |
25 if os.path.exists(path): | |
26 with open(path, 'rb') as handle: | |
27 data = handle.read() | |
28 | |
29 if data.find(airbrake_key) >= 0: | |
30 return | |
31 else: | |
32 os.remove(path) | |
33 callRailsCommand([os.path.join(app_dir, 'script', 'rails'), 'generate', 'airbr
ake', '--api-key', airbrake_key]) | |
34 | |
35 with open(path, 'rb+') as handle: | |
36 # Prepend file with require 'airbrake', won't happen by default | |
37 data = handle.read() | |
38 handle.seek(0) | |
39 print >>handle, "require 'airbrake'" | |
40 handle.write(data) | |
41 | |
42 def runInitCommands(): | 19 def runInitCommands(): |
43 service_path = '/etc/init.d/discourse' | 20 service_path = '/etc/init.d/discourse' |
44 | 21 |
45 callRailsCommand(['bundle', 'install', '--deployment', '--without', 'test', '-
-without', 'development']) | 22 callRailsCommand(['bundle', 'install', '--deployment', '--without', 'test', '-
-without', 'development']) |
46 initAirBrake() | |
47 callRailsCommand(['bundle', 'exec', 'rake', 'db:migrate']) | 23 callRailsCommand(['bundle', 'exec', 'rake', 'db:migrate']) |
48 callRailsCommand(['bundle', 'exec', 'rake', 'assets:precompile']) | 24 callRailsCommand(['bundle', 'exec', 'rake', 'assets:precompile']) |
49 | 25 |
50 if os.path.exists(service_path): | 26 if os.path.exists(service_path): |
51 subprocess.check_call(['sudo', service_path, 'stop']) | 27 subprocess.check_call(['sudo', service_path, 'stop']) |
52 | 28 |
53 if os.path.exists(service_path): | 29 if os.path.exists(service_path): |
54 subprocess.check_call(['sudo', service_path, 'start']) | 30 subprocess.check_call(['sudo', service_path, 'start']) |
55 | 31 |
56 if __name__ == '__main__': | 32 if __name__ == '__main__': |
57 runInitCommands() | 33 runInitCommands() |
OLD | NEW |