source: branches/locker-dev/locker/deploy/bin/rails @ 2277

Last change on this file since 2277 was 2277, checked in by glasgall, 9 years ago
remove cargo-culted creation of .failed files
  • Property svn:executable set to *
File size: 7.6 KB
Line 
1#!/usr/bin/perl
2use strict;
3use FindBin qw($Bin);
4use lib $Bin;
5use onserver;
6use Tie::File;
7use Cwd;
8
9setup();
10
11sub make_db {
12    my($type) = @_;
13    print "\nCreating $type SQL database for $sname...\n";
14    open GETPWD, '-|', "/mit/scripts/sql/bin$scriptsdev/get-password";
15    ($sqlhost, $sqluser, $sqlpass) = split(/\s/, <GETPWD>);
16    close GETPWD;
17    open SQLDB, '-|', "/mit/scripts/sql/bin$scriptsdev/get-next-database", "${addrlast}_${type}";
18    $sqldb = <SQLDB>;
19    close SQLDB;
20    open SQLDB, '-|', "/mit/scripts/sql/bin$scriptsdev/create-database", $sqldb;
21    $sqldb = <SQLDB>;
22    close SQLDB;
23    if($sqldb eq "") {
24        print "\nERROR:\n";
25        print "Your SQL account failed to create a SQL database.\n";
26        print "You should log in at http://sql.mit.edu to check whether\n";
27        print "your SQL account is at its database limit or its storage limit.\n";
28        print "If you cannot determine the cause of the problem, please\n";
29        print "feel free to contact sql\@mit.edu for assistance.\n";
30        exit 1;
31    }
32    return $sqldb;
33}
34
35my $dev_db = make_db("development");
36my $test_db = make_db("test");
37my $prod_db = make_db("production");
38
39my $cwd = getcwd;
40system("rails", "new", $cwd ,"-d", "mysql");
41my $appdir = `basename $cwd`;
42chomp $appdir;
43
44open APPLICATION_RB, "config/application.rb";
45my $appclass;
46while(<APPLICATION_RB>) {
47    if (/module (\w+)\n/) {
48        $appclass = $1;
49        last;
50    }
51}
52close APPLICATION_RB;
53if (!$appclass) {
54    die "Couldn't find application class name - plase email scripts\@mit.edu with the names of your locker and the application you tried to create. Sorry!";
55}
56
57open PUBLIC_HTACCESS, ">public/.htaccess";
58print PUBLIC_HTACCESS <<EOF;
59# General Apache options
60Options +FollowSymLinks +ExecCGI
61
62# If you don't want Rails to look in certain directories,
63# use the following rewrite rules so that Apache won't rewrite certain requests
64#
65# Example:
66#   RewriteCond %{REQUEST_URI} ^/notrails.*
67#   RewriteRule .* - [L]
68
69# Redirect all requests not available on the filesystem to Rails
70# By default the cgi dispatcher is used which is very slow
71#
72# For better performance replace the dispatcher with the fastcgi one
73#
74# Example:
75#   RewriteRule ^(.*)\$ dispatch.fcgi [QSA,L]
76RewriteEngine On
77
78# If your Rails application is accessed via an Alias directive,
79# then you MUST also set the RewriteBase in this htaccess file.
80#
81# Example:
82#   Alias /myrailsapp /path/to/myrailsapp/public
83#   RewriteBase /myrailsapp
84
85RewriteRule ^\$ index.html [QSA]
86RewriteRule ^([^.]+)\$ \$1.html [QSA]
87RewriteCond %{REQUEST_FILENAME} !-f
88RewriteRule ^(.*)\$ dispatch.fcgi [QSA,L]
89
90# In case Rails experiences terminal errors
91# Instead of displaying this message you can supply a file here which will be rendered instead
92#
93# Example:
94#   ErrorDocument 500 /500.html
95
96EOF
97
98open HTACCESS, ">.htaccess";
99print HTACCESS <<EOF;
100RewriteEngine On
101RewriteRule ^(.*)\$ public/\$1 [QSA,L]
102
103EOF
104
105tie my @railsenv, 'Tie::File', 'config/environment.rb';
106unshift @railsenv, "# ENV['RAILS_ENV'] ||= 'production'";
107unshift @railsenv, "# Uncomment below to put Rails into production mode";
108unshift @railsenv, "";
109unshift @railsenv, "ENV['RAILS_RELATIVE_URL_ROOT'] = \"/$addrend\"";
110untie @railsenv;
111
112tie my @railsdb, 'Tie::File', 'config/database.yml';
113for (@railsdb) {
114    s/username:.*$/username: $sqluser/;
115    s/password:.*$/password: $sqlpass/;
116    s/host:.*$/host: $sqlhost/;
117    s/database:.*_development.*/database: $dev_db/;
118    s/database:.*_test.*/database: $test_db/;
119    s/database:.*_production.*/database: $prod_db/;
120}
121untie @railsdb;
122
123tie my @railswelcome, 'Tie::File', 'public/index.html';
124for (@railswelcome) {
125    s/Create your database/Sync your database/;
126    s/to create your database\..*/to create tables in your database.<\/p>/;
127}
128untie @railswelcome;
129
130tie my @railsfcgi, 'Tie::File', 'public/dispatch.fcgi';
131for (@railsfcgi) {
132    s/^[^#]*RailsFCGIHandler/## Commented out by scripts.mit.edu autoinstaller\n## RailsFCGIHandler/;
133}
134untie @railsfcgi;
135open RAILSFCGI, ">>public/dispatch.fcgi";
136print RAILSFCGI "#!/usr/bin/ruby\n";
137print RAILSFCGI <<EOF;
138require File.join(File.dirname(__FILE__), '../config/environment')       
139require 'rack'
140
141## Added by scripts.mit.edu autoinstaller to reload when app code changes
142Thread.abort_on_exception = true
143
144class Rack::PathInfoRewriter
145  def initialize(app)
146    \@app = app
147  end
148
149  def call(env)
150    env["SCRIPT_NAME"] = ""
151    parts = env['REQUEST_URI'].split('?')
152    env['PATH_INFO'] = parts[0]
153    env['QUERY_STRING'] = parts[1].to_s
154    \@app.call(env)
155  end
156end
157
158
159t1 = Thread.new do
160  dispatch_logger = Logger.new(File.join(Rails.root,'log/dispatcher.log'))
161
162  begin
163    Rack::Handler::FastCGI.run Rack::PathInfoRewriter.new(Rack::URLMap.new("/$appdir" => ${appclass}::Application,
164                                                                           "/" => ${appclass}::Application))
165  rescue => e
166   dispatch_logger.error(e)
167   raise e
168  end
169end
170t2 = Thread.new do
171   # List of directories to watch for changes before reload.
172   # You may want to also watch public or vendor, depending on your needs.
173   Thread.current[:watched_dirs] = ['app', 'config', 'db', 'lib']
174
175   # List of specific files to watch for changes.
176   Thread.current[:watched_files] = ['public/dispatch.fcgi',
177                                     'public/.htaccess']
178   # Sample filter: /(\.rb|\.erb)\$/.  Default filter: watch all files
179   Thread.current[:watched_extensions] = //
180   # Iterations since last reload
181   Thread.current[:iterations] = 0
182
183   def modified(file)
184     begin
185       mtime = File.stat(file).mtime
186     rescue
187       false
188     else
189       if Thread.current[:iterations] == 0
190         Thread.current[:modifications][file] = mtime
191       end
192       Thread.current[:modifications][file] != mtime
193     end
194   end
195
196   # Don't symlink yourself into a loop.  Please.  Things will still work
197   # (Linux limits your symlink depth) but you will be sad
198   def modified_dir(dir)
199     Dir.new(dir).each do |file|
200       absfile = File.join(dir, file)
201       if FileTest.directory? absfile
202         next if file == '.' or file == '..'
203         return true if modified_dir(absfile)
204       else
205         return true if Thread.current[:watched_extensions] =~ absfile &&
206           modified(absfile)
207       end
208     end
209     false
210   end
211
212   def reload
213     Thread.current[:modifications] = {}
214     Thread.current[:iterations] = 0
215     # This is a kludge, but at the same time it works.
216     # Will kill the current FCGI process so that it is reloaded
217     # at next request.
218     raise RuntimeError
219   end
220
221   Thread.current[:modifications] = {}
222   # Wait until the modify time changes, then reload.
223   while true
224     dir_modified = Thread.current[:watched_dirs].inject(false) {|z, dir| z || modified_dir(File.join(File.dirname(__FILE__), '..', dir))}
225     file_modified = Thread.current[:watched_files].inject(false) {|z, file| z || modified(File.join(File.dirname(__FILE__), '..', file))}
226     reload if dir_modified || file_modified
227     Thread.current[:iterations] += 1
228     sleep 1
229   end
230end
231
232t1.join
233t2.join
234## End of scripts.mit.edu autoinstaller additions
235EOF
236chmod 0755,'public/dispatch.fcgi';
237
238# static-cat doesn't whitelist .txt files
239chmod 0777, 'public/robots.txt'; 
240
241# have to explicitly take a dependency on fcgi
242# ruby1.9 means we need to take a dependency on minitest
243# for rails console to work
244open GEMFILE, ">>Gemfile";
245print GEMFILE "gem 'fcgi'\n";
246print GEMFILE "gem 'minitest'\n";
247close GEMFILE;
248
249print "Your application is located in:\n";
250print "  /mit/$USER/web_scripts/$addrend/\n";
251print "To run programs like rake or rails generate, run\n";
252print "  'ssh -k $USER\@scripts' and cd to the above directory.\n\n";
253press_enter;
254
255exit 0;
Note: See TracBrowser for help on using the repository browser.