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