1 | #!/usr/bin/perl |
---|
2 | use strict; |
---|
3 | use FindBin qw($Bin); |
---|
4 | use lib $Bin; |
---|
5 | use onserver; |
---|
6 | use Tie::File; |
---|
7 | use Cwd; |
---|
8 | |
---|
9 | setup(); |
---|
10 | |
---|
11 | sub 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 | |
---|
35 | my $dev_db = make_db("development"); |
---|
36 | my $test_db = make_db("test"); |
---|
37 | my $prod_db = make_db("production"); |
---|
38 | |
---|
39 | my $cwd = getcwd; |
---|
40 | system("rails", "new", $cwd ,"-d", "mysql"); |
---|
41 | my $appdir = `basename $cwd`; |
---|
42 | chomp $appdir; |
---|
43 | |
---|
44 | open APPLICATION_RB, "config/application.rb"; |
---|
45 | my $appclass; |
---|
46 | while(<APPLICATION_RB>) { |
---|
47 | if (/module (\w+)\n/) { |
---|
48 | $appclass = $1; |
---|
49 | last; |
---|
50 | } |
---|
51 | } |
---|
52 | close APPLICATION_RB; |
---|
53 | if (!$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 | |
---|
57 | open PUBLIC_HTACCESS, ">public/.htaccess"; |
---|
58 | print PUBLIC_HTACCESS <<EOF; |
---|
59 | # General Apache options |
---|
60 | Options +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] |
---|
76 | RewriteEngine 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 | |
---|
85 | RewriteCond index.html -f |
---|
86 | RewriteRule ^\$ index.html [QSA] |
---|
87 | RewriteCond %{REQUEST_FILENAME} !-f |
---|
88 | RewriteRule ^(.*)\$ dispatch.fcgi/\$1 [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 | |
---|
96 | EOF |
---|
97 | |
---|
98 | open HTACCESS, ">.htaccess"; |
---|
99 | print HTACCESS <<EOF; |
---|
100 | RewriteEngine On |
---|
101 | RewriteRule ^(.*)\$ public/\$1 [QSA,L] |
---|
102 | |
---|
103 | EOF |
---|
104 | |
---|
105 | tie my @railsenv, 'Tie::File', 'config/environment.rb'; |
---|
106 | unshift @railsenv, "# ENV['RAILS_ENV'] ||= 'production'"; |
---|
107 | unshift @railsenv, "# Uncomment below to put Rails into production mode"; |
---|
108 | unshift @railsenv, ""; |
---|
109 | untie @railsenv; |
---|
110 | |
---|
111 | tie my @railsdb, 'Tie::File', 'config/database.yml'; |
---|
112 | for (@railsdb) { |
---|
113 | s/username:.*$/username: $sqluser/; |
---|
114 | s/password:.*$/password: $sqlpass/; |
---|
115 | s/host:.*$/host: $sqlhost/; |
---|
116 | s/database:.*_development.*/database: $dev_db/; |
---|
117 | s/database:.*_test.*/database: $test_db/; |
---|
118 | s/database:.*_production.*/database: $prod_db/; |
---|
119 | } |
---|
120 | untie @railsdb; |
---|
121 | |
---|
122 | tie my @railswelcome, 'Tie::File', 'public/index.html'; |
---|
123 | for (@railswelcome) { |
---|
124 | s/Create your database/Sync your database/; |
---|
125 | s/to create your database\..*/to create tables in your database.<\/p>/; |
---|
126 | } |
---|
127 | untie @railswelcome; |
---|
128 | |
---|
129 | # set config.action_controller.asset_host for all environments, |
---|
130 | # so urls to static assets are generated correctly |
---|
131 | # regardless of how the app is accessed |
---|
132 | my $rails_assethost = " config.action_controller.asset_host = \"//$USER.scripts.mit.edu/$appdir/public\""; |
---|
133 | my @environments = ('development', 'production', 'test'); |
---|
134 | |
---|
135 | for my $environment (@environments) { |
---|
136 | tie my @envfile, 'Tie::File', "config/environments/$environment.rb"; |
---|
137 | my $i = 0; |
---|
138 | for (@envfile) { |
---|
139 | if (/^end$/) { |
---|
140 | last; |
---|
141 | } |
---|
142 | ++$i; |
---|
143 | } |
---|
144 | splice @envfile, $i, 1, ($rails_assethost, 'end'); |
---|
145 | untie @envfile; |
---|
146 | } |
---|
147 | |
---|
148 | |
---|
149 | tie my @railsfcgi, 'Tie::File', 'public/dispatch.fcgi'; |
---|
150 | for (@railsfcgi) { |
---|
151 | s/^[^#]*RailsFCGIHandler/## Commented out by scripts.mit.edu autoinstaller\n## RailsFCGIHandler/; |
---|
152 | } |
---|
153 | untie @railsfcgi; |
---|
154 | open RAILSFCGI, ">>public/dispatch.fcgi"; |
---|
155 | print RAILSFCGI "#!/usr/bin/ruby\n"; |
---|
156 | print RAILSFCGI <<EOF; |
---|
157 | require File.join(File.expand_path(File.dirname(__FILE__)), '../config/environment') |
---|
158 | require 'rack' |
---|
159 | |
---|
160 | ## Added by scripts.mit.edu autoinstaller to reload when app code changes |
---|
161 | Thread.abort_on_exception = true |
---|
162 | |
---|
163 | # Strip public/dispatch.fcgi out of SCRIPT_NAME so Rails generates nicer URLs |
---|
164 | class ScriptNameRewriter |
---|
165 | |
---|
166 | def initialize(app) |
---|
167 | \@app = app |
---|
168 | end |
---|
169 | |
---|
170 | def call(env) |
---|
171 | if env['SCRIPT_NAME'] =~ /dispatch\.fcgi/ |
---|
172 | env['SCRIPT_NAME'].gsub!(/public\\/dispatch\.fcgi/,'') |
---|
173 | end |
---|
174 | \@app.call(env) |
---|
175 | end |
---|
176 | end |
---|
177 | |
---|
178 | |
---|
179 | t1 = Thread.new do |
---|
180 | dispatch_logger = Logger.new(File.join(Rails.root,'log/dispatcher.log')) |
---|
181 | |
---|
182 | begin |
---|
183 | Rack::Handler::FastCGI.run ScriptNameRewriter.new(Rack::URLMap.new("/" => ${appclass}::Application)) |
---|
184 | rescue => e |
---|
185 | dispatch_logger.error(e) |
---|
186 | raise e |
---|
187 | end |
---|
188 | end |
---|
189 | t2 = Thread.new do |
---|
190 | # List of directories to watch for changes before reload. |
---|
191 | # You may want to also watch public or vendor, depending on your needs. |
---|
192 | Thread.current[:watched_dirs] = ['app', 'config', 'db', 'lib'] |
---|
193 | |
---|
194 | # List of specific files to watch for changes. |
---|
195 | Thread.current[:watched_files] = ['public/dispatch.fcgi', |
---|
196 | 'public/.htaccess'] |
---|
197 | # Sample filter: /(\.rb|\.erb)\$/. Default filter: watch all files |
---|
198 | Thread.current[:watched_extensions] = // |
---|
199 | # Iterations since last reload |
---|
200 | Thread.current[:iterations] = 0 |
---|
201 | |
---|
202 | def modified(file) |
---|
203 | begin |
---|
204 | mtime = File.stat(file).mtime |
---|
205 | rescue |
---|
206 | false |
---|
207 | else |
---|
208 | if Thread.current[:iterations] == 0 |
---|
209 | Thread.current[:modifications][file] = mtime |
---|
210 | end |
---|
211 | Thread.current[:modifications][file] != mtime |
---|
212 | end |
---|
213 | end |
---|
214 | |
---|
215 | # Don't symlink yourself into a loop. Please. Things will still work |
---|
216 | # (Linux limits your symlink depth) but you will be sad |
---|
217 | def modified_dir(dir) |
---|
218 | Dir.new(dir).each do |file| |
---|
219 | absfile = File.join(dir, file) |
---|
220 | if FileTest.directory? absfile |
---|
221 | next if file == '.' or file == '..' |
---|
222 | return true if modified_dir(absfile) |
---|
223 | else |
---|
224 | return true if Thread.current[:watched_extensions] =~ absfile && |
---|
225 | modified(absfile) |
---|
226 | end |
---|
227 | end |
---|
228 | false |
---|
229 | end |
---|
230 | |
---|
231 | def reload |
---|
232 | Thread.current[:modifications] = {} |
---|
233 | Thread.current[:iterations] = 0 |
---|
234 | # This is a kludge, but at the same time it works. |
---|
235 | # Will kill the current FCGI process so that it is reloaded |
---|
236 | # at next request. |
---|
237 | raise RuntimeError |
---|
238 | end |
---|
239 | |
---|
240 | Thread.current[:modifications] = {} |
---|
241 | # Wait until the modify time changes, then reload. |
---|
242 | while true |
---|
243 | dir_modified = Thread.current[:watched_dirs].inject(false) {|z, dir| z || modified_dir(File.join(File.dirname(__FILE__), '..', dir))} |
---|
244 | file_modified = Thread.current[:watched_files].inject(false) {|z, file| z || modified(File.join(File.dirname(__FILE__), '..', file))} |
---|
245 | reload if dir_modified || file_modified |
---|
246 | Thread.current[:iterations] += 1 |
---|
247 | sleep 1 |
---|
248 | end |
---|
249 | end |
---|
250 | |
---|
251 | t1.join |
---|
252 | t2.join |
---|
253 | ## End of scripts.mit.edu autoinstaller additions |
---|
254 | EOF |
---|
255 | chmod 0755,'public/dispatch.fcgi'; |
---|
256 | |
---|
257 | # static-cat doesn't whitelist .txt files |
---|
258 | chmod 0777, 'public/robots.txt'; |
---|
259 | |
---|
260 | # have to explicitly take a dependency on fcgi |
---|
261 | # ruby1.9 means we need to take a dependency on minitest |
---|
262 | # for rails console to work |
---|
263 | open GEMFILE, ">>Gemfile"; |
---|
264 | print GEMFILE "gem 'fcgi'\n"; |
---|
265 | print GEMFILE "gem 'minitest'\n"; |
---|
266 | close GEMFILE; |
---|
267 | |
---|
268 | print "Your application is located in:\n"; |
---|
269 | print " /mit/$USER/web_scripts/$addrend/\n"; |
---|
270 | print "To run programs like rake or rails generate, run\n"; |
---|
271 | print " 'ssh -k $USER\@scripts' and cd to the above directory.\n\n"; |
---|
272 | press_enter; |
---|
273 | |
---|
274 | exit 0; |
---|