| 1 | #!/usr/bin/perl | 
|---|
| 2 | use strict; | 
|---|
| 3 | use FindBin qw($Bin); | 
|---|
| 4 | use lib $Bin; | 
|---|
| 5 | use onserver; | 
|---|
| 6 | use Tie::File; | 
|---|
| 7 |  | 
|---|
| 8 | setup(); | 
|---|
| 9 |  | 
|---|
| 10 | sub make_db { | 
|---|
| 11 |     my($type) = @_; | 
|---|
| 12 |     print "\nCreating $type SQL database for $sname...\n"; | 
|---|
| 13 |     open GETPWD, '-|', "/mit/scripts/sql/bin$scriptsdev/get-password"; | 
|---|
| 14 |     ($sqlhost, $sqluser, $sqlpass) = split(/\s/, <GETPWD>); | 
|---|
| 15 |     close GETPWD; | 
|---|
| 16 |     open SQLDB, '-|', "/mit/scripts/sql/bin$scriptsdev/get-next-database", "${addrlast}_${type}"; | 
|---|
| 17 |     $sqldb = <SQLDB>; | 
|---|
| 18 |     close SQLDB; | 
|---|
| 19 |     open SQLDB, '-|', "/mit/scripts/sql/bin$scriptsdev/create-database", $sqldb; | 
|---|
| 20 |     $sqldb = <SQLDB>; | 
|---|
| 21 |     close SQLDB; | 
|---|
| 22 |     if($sqldb eq "") { | 
|---|
| 23 |         print "\nERROR:\n"; | 
|---|
| 24 |         print "Your SQL account failed to create a SQL database.\n"; | 
|---|
| 25 |         print "You should log in at http://sql.mit.edu to check whether\n"; | 
|---|
| 26 |         print "your SQL account is at its database limit or its storage limit.\n"; | 
|---|
| 27 |         print "If you cannot determine the cause of the problem, please\n"; | 
|---|
| 28 |         print "feel free to contact sql\@mit.edu for assistance.\n"; | 
|---|
| 29 |         open FAILED, ">.failed"; | 
|---|
| 30 |         close FAILED; | 
|---|
| 31 |         exit 1; | 
|---|
| 32 |     } | 
|---|
| 33 |     return $sqldb; | 
|---|
| 34 | } | 
|---|
| 35 |  | 
|---|
| 36 | my $dev_db = make_db("development"); | 
|---|
| 37 | my $test_db = make_db("test"); | 
|---|
| 38 | my $prod_db = make_db("production"); | 
|---|
| 39 |  | 
|---|
| 40 | system qw{rails -D -d mysql .}; | 
|---|
| 41 |  | 
|---|
| 42 | open PUBLIC_HTACCESS, ">public/.htaccess"; | 
|---|
| 43 | print PUBLIC_HTACCESS <<EOF; | 
|---|
| 44 | # General Apache options | 
|---|
| 45 | Options +FollowSymLinks +ExecCGI | 
|---|
| 46 |  | 
|---|
| 47 | # If you don't want Rails to look in certain directories, | 
|---|
| 48 | # use the following rewrite rules so that Apache won't rewrite certain requests | 
|---|
| 49 | # | 
|---|
| 50 | # Example: | 
|---|
| 51 | #   RewriteCond %{REQUEST_URI} ^/notrails.* | 
|---|
| 52 | #   RewriteRule .* - [L] | 
|---|
| 53 |  | 
|---|
| 54 | # Redirect all requests not available on the filesystem to Rails | 
|---|
| 55 | # By default the cgi dispatcher is used which is very slow | 
|---|
| 56 | # | 
|---|
| 57 | # For better performance replace the dispatcher with the fastcgi one | 
|---|
| 58 | # | 
|---|
| 59 | # Example: | 
|---|
| 60 | #   RewriteRule ^(.*)\$ dispatch.fcgi [QSA,L] | 
|---|
| 61 | RewriteEngine On | 
|---|
| 62 |  | 
|---|
| 63 | # If your Rails application is accessed via an Alias directive, | 
|---|
| 64 | # then you MUST also set the RewriteBase in this htaccess file. | 
|---|
| 65 | # | 
|---|
| 66 | # Example: | 
|---|
| 67 | #   Alias /myrailsapp /path/to/myrailsapp/public | 
|---|
| 68 | #   RewriteBase /myrailsapp | 
|---|
| 69 |  | 
|---|
| 70 | RewriteRule ^\$ index.html [QSA] | 
|---|
| 71 | RewriteRule ^([^.]+)\$ \$1.html [QSA] | 
|---|
| 72 | RewriteCond %{REQUEST_FILENAME} !-f | 
|---|
| 73 | RewriteRule ^(.*)\$ dispatch.fcgi [QSA,L] | 
|---|
| 74 |  | 
|---|
| 75 | # In case Rails experiences terminal errors | 
|---|
| 76 | # Instead of displaying this message you can supply a file here which will be rendered instead | 
|---|
| 77 | # | 
|---|
| 78 | # Example: | 
|---|
| 79 | #   ErrorDocument 500 /500.html | 
|---|
| 80 |  | 
|---|
| 81 | RewriteBase /$addrend/public/ | 
|---|
| 82 | EOF | 
|---|
| 83 |  | 
|---|
| 84 | open HTACCESS, ">.htaccess"; | 
|---|
| 85 | print HTACCESS <<EOF; | 
|---|
| 86 | RewriteEngine On | 
|---|
| 87 | RewriteRule ^(.*)\$ public/\$1 [QSA,L] | 
|---|
| 88 | RewriteBase /$addrend/ | 
|---|
| 89 | EOF | 
|---|
| 90 |  | 
|---|
| 91 | tie my @railsenv, 'Tie::File', 'config/environment.rb'; | 
|---|
| 92 | unshift @railsenv, "# ENV['RAILS_ENV'] ||= 'production'"; | 
|---|
| 93 | unshift @railsenv, "# Uncomment below to put Rails into production mode"; | 
|---|
| 94 | unshift @railsenv, ""; | 
|---|
| 95 | unshift @railsenv, "ENV['RAILS_RELATIVE_URL_ROOT'] = \"/$addrend\""; | 
|---|
| 96 | untie @railsenv; | 
|---|
| 97 |  | 
|---|
| 98 | tie my @railsdb, 'Tie::File', 'config/database.yml'; | 
|---|
| 99 | for (@railsdb) { | 
|---|
| 100 |     s/username:.*$/username: $sqluser/; | 
|---|
| 101 |     s/password:.*$/password: $sqlpass/; | 
|---|
| 102 |     s/host:.*$/host: $sqlhost/; | 
|---|
| 103 |     s/database:.*_development.*/database: $dev_db/; | 
|---|
| 104 |     s/database:.*_test.*/database: $test_db/; | 
|---|
| 105 |     s/database:.*_production.*/database: $prod_db/; | 
|---|
| 106 | } | 
|---|
| 107 | untie @railsdb; | 
|---|
| 108 |  | 
|---|
| 109 | tie my @railswelcome, 'Tie::File', 'public/index.html'; | 
|---|
| 110 | for (@railswelcome) { | 
|---|
| 111 |     s/Create your database/Sync your database/; | 
|---|
| 112 |     s/to create your database\..*/to create tables in your database.<\/p>/; | 
|---|
| 113 | } | 
|---|
| 114 | untie @railswelcome; | 
|---|
| 115 |  | 
|---|
| 116 | tie my @railsfcgi, 'Tie::File', 'public/dispatch.fcgi'; | 
|---|
| 117 | for (@railsfcgi) { | 
|---|
| 118 |     s/^[^#]*RailsFCGIHandler/## Commented out by scripts.mit.edu autoinstaller\n## RailsFCGIHandler/; | 
|---|
| 119 | } | 
|---|
| 120 | untie @railsfcgi; | 
|---|
| 121 | open RAILSFCGI, ">>public/dispatch.fcgi"; | 
|---|
| 122 | print RAILSFCGI <<EOF; | 
|---|
| 123 |  | 
|---|
| 124 | ## Added by scripts.mit.edu autoinstaller to reload when app code changes | 
|---|
| 125 | Thread.abort_on_exception = true | 
|---|
| 126 |  | 
|---|
| 127 | t1 = Thread.new do | 
|---|
| 128 |    RailsFCGIHandler.process! | 
|---|
| 129 | end | 
|---|
| 130 |  | 
|---|
| 131 | t2 = Thread.new do | 
|---|
| 132 |    # List of directories to watch for changes before reload. | 
|---|
| 133 |    # You may want to also watch public or vendor, depending on your needs. | 
|---|
| 134 |    Thread.current[:watched_dirs] = ['app', 'config', 'db', 'lib'] | 
|---|
| 135 |  | 
|---|
| 136 |    # List of specific files to watch for changes. | 
|---|
| 137 |    Thread.current[:watched_files] = ['public/dispatch.fcgi', | 
|---|
| 138 |                                      'public/.htaccess'] | 
|---|
| 139 |    # Sample filter: /(\.rb|\.erb)\$/.  Default filter: watch all files | 
|---|
| 140 |    Thread.current[:watched_extensions] = // | 
|---|
| 141 |    # Iterations since last reload | 
|---|
| 142 |    Thread.current[:iterations] = 0 | 
|---|
| 143 |  | 
|---|
| 144 |    def modified(file) | 
|---|
| 145 |      begin | 
|---|
| 146 |        mtime = File.stat(file).mtime | 
|---|
| 147 |      rescue | 
|---|
| 148 |        false | 
|---|
| 149 |      else | 
|---|
| 150 |        if Thread.current[:iterations] == 0 | 
|---|
| 151 |          Thread.current[:modifications][file] = mtime | 
|---|
| 152 |        end | 
|---|
| 153 |        Thread.current[:modifications][file] != mtime | 
|---|
| 154 |      end | 
|---|
| 155 |    end | 
|---|
| 156 |  | 
|---|
| 157 |    # Don't symlink yourself into a loop.  Please.  Things will still work | 
|---|
| 158 |    # (Linux limits your symlink depth) but you will be sad | 
|---|
| 159 |    def modified_dir(dir) | 
|---|
| 160 |      Dir.new(dir).each do |file| | 
|---|
| 161 |        absfile = File.join(dir, file) | 
|---|
| 162 |        if FileTest.directory? absfile | 
|---|
| 163 |          next if file == '.' or file == '..' | 
|---|
| 164 |          return true if modified_dir(absfile) | 
|---|
| 165 |        else | 
|---|
| 166 |          return true if Thread.current[:watched_extensions] =~ absfile && | 
|---|
| 167 |            modified(absfile) | 
|---|
| 168 |        end | 
|---|
| 169 |      end | 
|---|
| 170 |      false | 
|---|
| 171 |    end | 
|---|
| 172 |  | 
|---|
| 173 |    def reload | 
|---|
| 174 |      Thread.current[:modifications] = {} | 
|---|
| 175 |      Thread.current[:iterations] = 0 | 
|---|
| 176 |      # This is a kludge, but at the same time it works. | 
|---|
| 177 |      # Will kill the current FCGI process so that it is reloaded | 
|---|
| 178 |      # at next request. | 
|---|
| 179 |      raise RuntimeError | 
|---|
| 180 |    end | 
|---|
| 181 |  | 
|---|
| 182 |    Thread.current[:modifications] = {} | 
|---|
| 183 |    # Wait until the modify time changes, then reload. | 
|---|
| 184 |    while true | 
|---|
| 185 |      dir_modified = Thread.current[:watched_dirs].inject(false) {|z, dir| z || modified_dir(File.join(File.dirname(__FILE__), '..', dir))} | 
|---|
| 186 |      file_modified = Thread.current[:watched_files].inject(false) {|z, file| z || modified(File.join(File.dirname(__FILE__), '..', file))} | 
|---|
| 187 |      reload if dir_modified || file_modified | 
|---|
| 188 |      Thread.current[:iterations] += 1 | 
|---|
| 189 |      sleep 1 | 
|---|
| 190 |    end | 
|---|
| 191 | end | 
|---|
| 192 |  | 
|---|
| 193 | t1.join | 
|---|
| 194 | t2.join | 
|---|
| 195 | ## End of scripts.mit.edu autoinstaller additions | 
|---|
| 196 | EOF | 
|---|
| 197 |  | 
|---|
| 198 | print "Your application is located in:\n"; | 
|---|
| 199 | print "  /mit/$USER/web_scripts/$addrend/\n"; | 
|---|
| 200 | print "To run programs like rake or script/generate, run\n"; | 
|---|
| 201 | print "  'ssh -k $USER\@scripts' and cd to the above directory.\n\n"; | 
|---|
| 202 | press_enter; | 
|---|
| 203 |  | 
|---|
| 204 | exit 0; | 
|---|