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