| [1295] | 1 | #!/usr/bin/perl | 
|---|
|  | 2 | use strict; | 
|---|
|  | 3 | use FindBin qw($Bin); | 
|---|
|  | 4 | use lib $Bin; | 
|---|
|  | 5 | use onserver; | 
|---|
|  | 6 | use Tie::File; | 
|---|
| [2149] | 7 | use Cwd; | 
|---|
| [1295] | 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 |  | 
|---|
| [2149] | 39 | my $cwd = getcwd; | 
|---|
|  | 40 | system("rails", "new", $cwd ,"-d", "mysql"); | 
|---|
|  | 41 | my $appdir = `basename $cwd`; | 
|---|
|  | 42 | chomp $appdir; | 
|---|
| [1295] | 43 |  | 
|---|
| [2261] | 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 |  | 
|---|
| [1295] | 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 |  | 
|---|
| [2278] | 85 | RewriteCond index.html -f | 
|---|
| [1295] | 86 | RewriteRule ^\$ index.html [QSA] | 
|---|
|  | 87 | RewriteCond %{REQUEST_FILENAME} !-f | 
|---|
| [2278] | 88 | RewriteRule ^(.*)\$ dispatch.fcgi/\$1 [QSA,L] | 
|---|
| [1295] | 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] | 
|---|
| [2260] | 102 |  | 
|---|
| [1295] | 103 | EOF | 
|---|
|  | 104 |  | 
|---|
|  | 105 | tie my @railsenv, 'Tie::File', 'config/environment.rb'; | 
|---|
| [1407] | 106 | unshift @railsenv, "# ENV['RAILS_ENV'] ||= 'production'"; | 
|---|
|  | 107 | unshift @railsenv, "# Uncomment below to put Rails into production mode"; | 
|---|
|  | 108 | unshift @railsenv, ""; | 
|---|
| [1295] | 109 | unshift @railsenv, "ENV['RAILS_RELATIVE_URL_ROOT'] = \"/$addrend\""; | 
|---|
|  | 110 | untie @railsenv; | 
|---|
|  | 111 |  | 
|---|
|  | 112 | tie my @railsdb, 'Tie::File', 'config/database.yml'; | 
|---|
|  | 113 | for (@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 | } | 
|---|
|  | 121 | untie @railsdb; | 
|---|
|  | 122 |  | 
|---|
| [1297] | 123 | tie my @railswelcome, 'Tie::File', 'public/index.html'; | 
|---|
|  | 124 | for (@railswelcome) { | 
|---|
|  | 125 | s/Create your database/Sync your database/; | 
|---|
|  | 126 | s/to create your database\..*/to create tables in your database.<\/p>/; | 
|---|
|  | 127 | } | 
|---|
|  | 128 | untie @railswelcome; | 
|---|
|  | 129 |  | 
|---|
| [1408] | 130 | tie my @railsfcgi, 'Tie::File', 'public/dispatch.fcgi'; | 
|---|
|  | 131 | for (@railsfcgi) { | 
|---|
|  | 132 | s/^[^#]*RailsFCGIHandler/## Commented out by scripts.mit.edu autoinstaller\n## RailsFCGIHandler/; | 
|---|
|  | 133 | } | 
|---|
|  | 134 | untie @railsfcgi; | 
|---|
|  | 135 | open RAILSFCGI, ">>public/dispatch.fcgi"; | 
|---|
| [2149] | 136 | print RAILSFCGI "#!/usr/bin/ruby\n"; | 
|---|
| [1408] | 137 | print RAILSFCGI <<EOF; | 
|---|
| [2149] | 138 | require File.join(File.dirname(__FILE__), '../config/environment') | 
|---|
|  | 139 | require 'rack' | 
|---|
| [1408] | 140 |  | 
|---|
|  | 141 | ## Added by scripts.mit.edu autoinstaller to reload when app code changes | 
|---|
|  | 142 | Thread.abort_on_exception = true | 
|---|
|  | 143 |  | 
|---|
| [2149] | 144 | class 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 | 
|---|
|  | 156 | end | 
|---|
|  | 157 |  | 
|---|
|  | 158 |  | 
|---|
| [1408] | 159 | t1 = Thread.new do | 
|---|
| [2149] | 160 | dispatch_logger = Logger.new(File.join(Rails.root,'log/dispatcher.log')) | 
|---|
|  | 161 |  | 
|---|
|  | 162 | begin | 
|---|
| [2260] | 163 | Rack::Handler::FastCGI.run Rack::PathInfoRewriter.new(Rack::URLMap.new("/$appdir" => ${appclass}::Application, | 
|---|
|  | 164 | "/" => ${appclass}::Application)) | 
|---|
| [2149] | 165 | rescue => e | 
|---|
|  | 166 | dispatch_logger.error(e) | 
|---|
|  | 167 | raise e | 
|---|
|  | 168 | end | 
|---|
| [1408] | 169 | end | 
|---|
|  | 170 | t2 = Thread.new do | 
|---|
| [1469] | 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'] | 
|---|
| [1484] | 174 |  | 
|---|
|  | 175 | # List of specific files to watch for changes. | 
|---|
|  | 176 | Thread.current[:watched_files] = ['public/dispatch.fcgi', | 
|---|
| [2262] | 177 | 'public/.htaccess'] | 
|---|
| [1411] | 178 | # Sample filter: /(\.rb|\.erb)\$/.  Default filter: watch all files | 
|---|
| [1408] | 179 | Thread.current[:watched_extensions] = // | 
|---|
| [1410] | 180 | # Iterations since last reload | 
|---|
|  | 181 | Thread.current[:iterations] = 0 | 
|---|
| [1408] | 182 |  | 
|---|
|  | 183 | def modified(file) | 
|---|
| [1410] | 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 | 
|---|
| [1408] | 194 | end | 
|---|
|  | 195 |  | 
|---|
| [1410] | 196 | # Don't symlink yourself into a loop.  Please.  Things will still work | 
|---|
|  | 197 | # (Linux limits your symlink depth) but you will be sad | 
|---|
| [1408] | 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 && | 
|---|
| [2262] | 206 | modified(absfile) | 
|---|
| [1408] | 207 | end | 
|---|
|  | 208 | end | 
|---|
|  | 209 | false | 
|---|
|  | 210 | end | 
|---|
|  | 211 |  | 
|---|
|  | 212 | def reload | 
|---|
|  | 213 | Thread.current[:modifications] = {} | 
|---|
| [1410] | 214 | Thread.current[:iterations] = 0 | 
|---|
| [1486] | 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 | 
|---|
| [1408] | 219 | end | 
|---|
|  | 220 |  | 
|---|
|  | 221 | Thread.current[:modifications] = {} | 
|---|
|  | 222 | # Wait until the modify time changes, then reload. | 
|---|
|  | 223 | while true | 
|---|
| [1484] | 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 | 
|---|
| [1410] | 227 | Thread.current[:iterations] += 1 | 
|---|
| [1408] | 228 | sleep 1 | 
|---|
|  | 229 | end | 
|---|
|  | 230 | end | 
|---|
|  | 231 |  | 
|---|
|  | 232 | t1.join | 
|---|
|  | 233 | t2.join | 
|---|
|  | 234 | ## End of scripts.mit.edu autoinstaller additions | 
|---|
|  | 235 | EOF | 
|---|
| [2149] | 236 | chmod 0755,'public/dispatch.fcgi'; | 
|---|
| [1408] | 237 |  | 
|---|
| [2275] | 238 | # static-cat doesn't whitelist .txt files | 
|---|
|  | 239 | chmod 0777, 'public/robots.txt'; | 
|---|
|  | 240 |  | 
|---|
| [2149] | 241 | # have to explicitly take a dependency on fcgi | 
|---|
| [2259] | 242 | # ruby1.9 means we need to take a dependency on minitest | 
|---|
|  | 243 | # for rails console to work | 
|---|
| [2149] | 244 | open GEMFILE, ">>Gemfile"; | 
|---|
|  | 245 | print GEMFILE "gem 'fcgi'\n"; | 
|---|
| [2259] | 246 | print GEMFILE "gem 'minitest'\n"; | 
|---|
| [2149] | 247 | close GEMFILE; | 
|---|
|  | 248 |  | 
|---|
| [1297] | 249 | print "Your application is located in:\n"; | 
|---|
|  | 250 | print "  /mit/$USER/web_scripts/$addrend/\n"; | 
|---|
| [2276] | 251 | print "To run programs like rake or rails generate, run\n"; | 
|---|
| [1297] | 252 | print "  'ssh -k $USER\@scripts' and cd to the above directory.\n\n"; | 
|---|
|  | 253 | press_enter; | 
|---|
|  | 254 |  | 
|---|
| [1295] | 255 | exit 0; | 
|---|