unicorn_rails: cleanup path mapping usage
[unicorn.git] / bin / unicorn_rails
blobfa0a9b72ab1369acd97a8a691b15dc320cd22317
1 #!/home/ew/bin/ruby
2 require 'unicorn/launcher'
3 require 'optparse'
4 require 'fileutils'
6 rails_pid = File.join(Unicorn::HttpServer::DEFAULT_START_CTX[:cwd],
7 "/tmp/pids/unicorn.pid")
8 cmd = File.basename($0)
9 daemonize = false
10 listeners = []
11 options = { :listeners => listeners }
12 host, port = Unicorn::Const::DEFAULT_HOST, Unicorn::Const::DEFAULT_PORT
13 set_listener = false
14 ENV['RAILS_ENV'] ||= "development"
15 map_path = ENV['RAILS_RELATIVE_URL_ROOT']
17 opts = OptionParser.new("", 24, ' ') do |opts|
18 opts.banner = "Usage: #{cmd} " \
19 "[ruby options] [#{cmd} options] [rackup config file]"
20 opts.separator "Ruby options:"
22 lineno = 1
23 opts.on("-e", "--eval LINE", "evaluate a LINE of code") do |line|
24 eval line, TOPLEVEL_BINDING, "-e", lineno
25 lineno += 1
26 end
28 opts.on("-d", "--debug", "set debugging flags (set $DEBUG to true)") do
29 $DEBUG = true
30 end
32 opts.on("-w", "--warn", "turn warnings on for your script") do
33 $-w = true
34 end
36 opts.on("-I", "--include PATH",
37 "specify $LOAD_PATH (may be used more than once)") do |path|
38 $LOAD_PATH.unshift(*path.split(/:/))
39 end
41 opts.on("-r", "--require LIBRARY",
42 "require the library, before executing your script") do |library|
43 require library
44 end
46 opts.separator "#{cmd} options:"
48 # some of these switches exist for rackup command-line compatibility,
50 opts.on("-o", "--host HOST",
51 "listen on HOST (default: #{Unicorn::Const::DEFAULT_HOST})") do |h|
52 host = h
53 set_listener = true
54 end
56 opts.on("-p", "--port PORT", "use PORT (default: #{port})") do |p|
57 port = p.to_i
58 set_listener = true
59 end
61 opts.on("-E", "--env ENVIRONMENT",
62 "use ENVIRONMENT for defaults (default: development)") do |e|
63 ENV['RAILS_ENV'] = e
64 end
66 opts.on("-D", "--daemonize", "run daemonized in the background") do |d|
67 daemonize = d ? true : false
68 end
70 # Unicorn-specific stuff
71 opts.on("-l", "--listen {HOST:PORT|PATH}",
72 "listen on HOST:PORT or PATH",
73 "this may be specified multiple times",
74 "(default: #{Unicorn::Const::DEFAULT_LISTEN})") do |address|
75 listeners << address
76 end
78 opts.on("-c", "--config-file FILE", "Unicorn-specific config file") do |f|
79 options[:config_file] = File.expand_path(f)
80 end
82 opts.on("-P", "--path PATH", "Runs Rails app mounted at a specific path.",
83 "(default: /") do |v|
84 ENV['RAILS_RELATIVE_URL_ROOT'] = map_path = v
85 end
87 # I'm avoiding Unicorn-specific config options on the command-line.
88 # IMNSHO, config options on the command-line are redundant given
89 # config files and make things unnecessarily complicated with multiple
90 # places to look for a config option.
92 opts.separator "Common options:"
94 opts.on_tail("-h", "--help", "Show this message") do
95 puts opts
96 exit
97 end
99 opts.on_tail("-v", "--version", "Show version") do
100 puts " v#{Unicorn::Const::UNICORN_VERSION}"
101 exit
104 opts.parse! ARGV
107 config = ARGV[0] || (File.exist?('config.ru') ? 'config.ru' : nil)
109 if config && config =~ /\.ru$/
110 # parse embedded command-line options in config.ru comments
111 if File.open(config, "rb") { |fp| fp.sysread(fp.stat.size) } =~ /^#\\(.*)/
112 opts.parse! $1.split(/\s+/)
116 require 'pp' if $DEBUG
118 # Loads Rails and the private version of Rack it bundles. Returns a
119 # lambda of arity==0 that will return *another* lambda of arity==1
120 # suitable for using inside Rack::Builder.new block.
121 rails_loader = lambda do ||
122 begin
123 require 'config/boot'
124 defined?(::RAILS_ROOT) or abort "RAILS_ROOT not defined by config/boot"
125 defined?(::RAILS_ENV) or abort "RAILS_ENV not defined by config/boot"
126 defined?(::Rails::VERSION::STRING) or
127 abort "Rails::VERSION::STRING not defined by config/boot"
128 rescue LoadError => err
129 abort "#$0 must be run inside RAILS_ROOT: #{err.inspect}"
131 defined?(::RAILS_ROOT) or abort "RAILS_ROOT not defined by config/boot"
132 defined?(::RAILS_ENV) or abort "RAILS_ENV not defined by config/boot"
133 defined?(::Rails::VERSION::STRING) or
134 abort "Rails::VERSION::STRING not defined by config/boot"
136 case config
137 when nil
138 lambda do ||
139 require 'config/environment'
141 # it seems Rails >=2.2 support Rack, but only >=2.3 requires it
142 old_rails = case ::Rails::VERSION::MAJOR
143 when 0, 1 then true
144 when 2 then Rails::VERSION::MINOR < 3 ? true : false
145 else
146 false
149 if old_rails
150 require 'rack'
151 require 'unicorn/app/old_rails'
152 Unicorn::App::OldRails.new
153 else
154 ActionController::Dispatcher.new
157 when /\.ru$/
158 raw = File.open(config, "rb") { |fp| fp.sysread(fp.stat.size) }
159 lambda { || eval("Rack::Builder.new {(#{raw}\n)}.to_app", nil, config) }
160 else
161 lambda do ||
162 require config
163 Object.const_get(File.basename(config, '.rb').capitalize)
168 # this won't run until after forking if preload_app is false
169 app = lambda do ||
170 inner_app = rails_loader.call
171 require 'active_support'
172 require 'action_controller'
173 map_path ||= '/'
174 inner_app = inner_app.call
175 Rack::Builder.new do
176 if inner_app.class.to_s == "Unicorn::App::OldRails"
177 if map_path != '/'
178 # patches + tests welcome, but I really cbf to deal with this
179 # since all apps I've ever dealt with just use "/" ...
180 $stderr.puts "relative URL roots may not work for older Rails"
182 $stderr.puts "LogTailer not available for Rails < 2.3" unless daemonize
183 $stderr.puts "Debugger not available" if $DEBUG
184 map(map_path) do
185 require 'unicorn/app/old_rails/static'
186 use Unicorn::App::OldRails::Static
187 run inner_app
189 else
190 use Rails::Rack::LogTailer unless daemonize
191 use Rails::Rack::Debugger if $DEBUG
192 map(map_path) do
193 use Rails::Rack::Static
194 run inner_app
197 end.to_app
200 listeners << "#{host}:#{port}" if set_listener
202 if $DEBUG
203 pp({
204 :unicorn_options => options,
205 :app => app,
206 :daemonize => daemonize,
210 # ensure Rails standard tmp paths exist
211 %w(cache pids sessions sockets).each do |dir|
212 FileUtils.mkdir_p("tmp/#{dir}")
215 if daemonize
216 options[:pid] = rails_pid
217 Unicorn::Launcher.daemonize!
219 Unicorn.run(app, options)