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 secret = os.environ.get('DISCOURSE_SECRET', None) | 6 secret = os.environ.get('DISCOURSE_SECRET', None) |
7 airbrake_key = os.environ.get('AIRBRAKE_KEY', None) | 7 airbrake_key = os.environ.get('AIRBRAKE_KEY', None) |
8 | 8 |
9 # HOME environment variable isn't reliable when called via sudo | 9 # HOME environment variable isn't reliable when called via sudo |
10 home_dir = os.path.expanduser('~' + pwd.getpwuid(os.getuid()).pw_name) | 10 home_dir = os.path.expanduser('~' + pwd.getpwuid(os.getuid()).pw_name) |
11 | 11 |
12 env = dict(os.environ) | 12 env = dict(os.environ) |
13 env['GEM_HOME'] = os.path.join(home_dir, '.gems') | 13 env['GEM_HOME'] = os.path.join(home_dir, '.gems') |
14 env['RAILS_ENV'] = 'production' | 14 env['RAILS_ENV'] = 'production' |
15 | 15 |
16 def preprocessFiles(): | 16 def preprocessFiles(): |
17 gemfile_path = os.path.join(app_dir, 'Gemfile') | |
18 handle = open(gemfile_path, 'rb') | |
19 if "gem 'fcgi'" not in map(str.strip, handle.readlines()): | |
20 handle.close() | |
21 handle = open(gemfile_path, 'ab') | |
22 print >>handle, "gem 'fcgi'" | |
23 print >>sys.stderr, 'Added fcgi gem to Gemfile' | |
24 handle.close() | |
25 | |
26 secret_path = os.path.join(app_dir, 'config', 'initializers', 'secret_token.rb
') | 17 secret_path = os.path.join(app_dir, 'config', 'initializers', 'secret_token.rb
') |
27 handle = open(secret_path, 'rb') | 18 handle = open(secret_path, 'rb') |
28 if handle.read().find('SET_SECRET_HERE') >= 0: | 19 if handle.read().find('SET_SECRET_HERE') >= 0: |
29 if secret != None: | 20 if secret != None: |
30 handle.close() | 21 handle.close() |
31 handle = open(secret_path, 'wb') | 22 handle = open(secret_path, 'wb') |
32 print >>handle, 'Discourse::Application.config.secret_token = "%s"' % secr
et | 23 print >>handle, 'Discourse::Application.config.secret_token = "%s"' % secr
et |
33 print >>sys.stderr, 'Defined our secret in config/initializers/secret_toke
n.rb' | 24 print >>sys.stderr, 'Defined our secret in config/initializers/secret_toke
n.rb' |
34 else: | 25 else: |
35 print >>sys.stderr, 'Skipping defining secret in config/initializers/secre
t_token.rb, please set DISCOURSE_SECRET environment variable' | 26 print >>sys.stderr, 'Skipping defining secret in config/initializers/secre
t_token.rb, please set DISCOURSE_SECRET environment variable' |
(...skipping 13 matching lines...) Expand all Loading... |
49 handle.close() | 40 handle.close() |
50 | 41 |
51 if data.find(airbrake_key) >= 0: | 42 if data.find(airbrake_key) >= 0: |
52 return | 43 return |
53 else: | 44 else: |
54 os.remove(path) | 45 os.remove(path) |
55 callRailsCommand([os.path.join(app_dir, 'script', 'rails'), 'generate', 'airbr
ake', '--api-key', airbrake_key]) | 46 callRailsCommand([os.path.join(app_dir, 'script', 'rails'), 'generate', 'airbr
ake', '--api-key', airbrake_key]) |
56 | 47 |
57 def runInitCommands(): | 48 def runInitCommands(): |
58 rake_path = os.path.join(env['GEM_HOME'], 'bin', 'rake') | 49 rake_path = os.path.join(env['GEM_HOME'], 'bin', 'rake') |
59 spawn_path = '/etc/init.d/spawn-fcgi' | 50 service_path = '/etc/init.d/discourse-thin' |
60 | 51 |
61 callRailsCommand(['bundle', 'install']) | 52 callRailsCommand(['bundle', 'install']) |
62 initAirBrake() | 53 initAirBrake() |
63 callRailsCommand([rake_path, 'assets:precompile']) | 54 callRailsCommand([rake_path, 'assets:precompile']) |
64 | 55 |
65 if os.path.exists(spawn_path): | 56 if os.path.exists(service_path): |
66 subprocess.call(['sudo', spawn_path, 'stop']) | 57 subprocess.call(['sudo', service_path, 'stop']) |
67 | 58 |
68 callRailsCommand([rake_path, 'db:migrate']) | 59 callRailsCommand([rake_path, 'db:migrate']) |
69 | 60 |
70 if os.path.exists(spawn_path): | 61 if os.path.exists(service_path): |
71 subprocess.call(['sudo', spawn_path, 'start']) | 62 subprocess.call(['sudo', service_path, 'start']) |
72 | 63 |
73 if __name__ == '__main__': | 64 if __name__ == '__main__': |
74 preprocessFiles() | 65 preprocessFiles() |
75 runInitCommands() | 66 runInitCommands() |
OLD | NEW |