Disable formatting for command-line switches
[unicorn.git] / bin / unicorn_rails
blobb3fda7b92eb92c7b1185547df5f071b0cc651f86
1 #!/home/ew/bin/ruby
2 require 'unicorn/launcher'
3 require 'optparse'
4 require 'fileutils'
6 rails_pid = "#{Unicorn::HttpServer::START_CTX[:cwd]}/tmp/pids/unicorn.pid"
7 cmd = File.basename($0)
8 daemonize = false
9 listeners = []
10 options = { :listeners => listeners }
11 host, port = Unicorn::Const::DEFAULT_HOST, Unicorn::Const::DEFAULT_PORT
12 set_listener = false
13 ENV['RAILS_ENV'] ||= "development"
14 map_path = ENV['RAILS_RELATIVE_URL_ROOT']
16 opts = OptionParser.new("", 24, ' ') do |opts|
17 opts.banner = "Usage: #{cmd} " \
18 "[ruby options] [#{cmd} options] [rackup config file]"
19 opts.separator "Ruby options:"
21 lineno = 1
22 opts.on("-e", "--eval LINE", "evaluate a LINE of code") do |line|
23 eval line, TOPLEVEL_BINDING, "-e", lineno
24 lineno += 1
25 end
27 opts.on("-d", "--debug", "set debugging flags (set $DEBUG to true)") do
28 $DEBUG = true
29 end
31 opts.on("-w", "--warn", "turn warnings on for your script") do
32 $-w = true
33 end
35 opts.on("-I", "--include PATH",
36 "specify $LOAD_PATH (may be used more than once)") do |path|
37 $LOAD_PATH.unshift(*path.split(/:/))
38 end
40 opts.on("-r", "--require LIBRARY",
41 "require the library, before executing your script") do |library|
42 require library
43 end
45 opts.separator "#{cmd} options:"
47 # some of these switches exist for rackup command-line compatibility,
49 opts.on("-o", "--host HOST",
50 "listen on HOST (default: #{Unicorn::Const::DEFAULT_HOST})") do |h|
51 host = h
52 set_listener = true
53 end
55 opts.on("-p", "--port PORT", "use PORT (default: #{port})") do |p|
56 port = p.to_i
57 set_listener = true
58 end
60 opts.on("-E", "--env ENVIRONMENT",
61 "use ENVIRONMENT for defaults (default: development)") do |e|
62 ENV['RAILS_ENV'] = e
63 end
65 opts.on("-D", "--daemonize", "run daemonized in the background") do |d|
66 daemonize = d ? true : false
67 end
69 # Unicorn-specific stuff
70 opts.on("-l", "--listen {HOST:PORT|PATH}",
71 "listen on HOST:PORT or PATH",
72 "this may be specified multiple times",
73 "(default: #{Unicorn::Const::DEFAULT_LISTEN})") do |address|
74 listeners << address
75 end
77 opts.on("-c", "--config-file FILE", "Unicorn-specific config file") do |f|
78 options[:config_file] = File.expand_path(f)
79 end
81 opts.on("-P", "--path PATH", "Runs Rails app mounted at a specific path.",
82 "(default: /") do |v|
83 ENV['RAILS_RELATIVE_URL_ROOT'] = map_path = v
84 end
86 # I'm avoiding Unicorn-specific config options on the command-line.
87 # IMNSHO, config options on the command-line are redundant given
88 # config files and make things unnecessarily complicated with multiple
89 # places to look for a config option.
91 opts.separator "Common options:"
93 opts.on_tail("-h", "--help", "Show this message") do
94 puts opts
95 exit
96 end
98 opts.on_tail("-v", "--version", "Show version") do
99 puts " v#{Unicorn::Const::UNICORN_VERSION}"
100 exit
103 opts.parse! ARGV
106 config = ARGV[0] || (File.exist?('config.ru') ? 'config.ru' : nil)
108 if config && config =~ /\.ru$/
109 # parse embedded command-line options in config.ru comments
110 if File.open(config, "rb") { |fp| fp.sysread(fp.stat.size) } =~ /^#\\(.*)/
111 opts.parse! $1.split(/\s+/)
115 require 'pp' if $DEBUG
117 # this won't run until after forking if preload_app is false
118 app = lambda do ||
119 # Load Rails and the private version of Rack it bundles.
120 begin
121 require 'config/boot'
122 rescue LoadError => err
123 abort "#$0 must be run inside RAILS_ROOT: #{err.inspect}"
125 defined?(::RAILS_ROOT) or abort "RAILS_ROOT not defined by config/boot"
126 defined?(::RAILS_ENV) or abort "RAILS_ENV not defined by config/boot"
127 defined?(::Rails::VERSION::STRING) or
128 abort "Rails::VERSION::STRING not defined by config/boot"
130 inner_app = case config
131 when nil
132 require 'config/environment'
134 # it seems Rails >=2.2 support Rack, but only >=2.3 requires it
135 old_rails = case ::Rails::VERSION::MAJOR
136 when 0, 1 then true
137 when 2 then Rails::VERSION::MINOR < 3 ? true : false
138 else
139 false
142 if old_rails
143 require 'rack'
144 require 'unicorn/app/old_rails'
145 Unicorn::App::OldRails.new
146 else
147 ActionController::Dispatcher.new
149 when /\.ru$/
150 raw = File.open(config, "rb") { |fp| fp.sysread(fp.stat.size) }
151 eval("Rack::Builder.new {(#{raw}\n)}.to_app", nil, config)
152 else
153 require config
154 Object.const_get(File.basename(config, '.rb').capitalize)
157 map_path ||= '/'
158 Rack::Builder.new do
159 if inner_app.class.to_s == "Unicorn::App::OldRails"
160 if map_path != '/'
161 # patches + tests welcome, but I really cbf to deal with this
162 # since all apps I've ever dealt with just use "/" ...
163 $stderr.puts "relative URL roots may not work for older Rails"
165 $stderr.puts "LogTailer not available for Rails < 2.3" unless daemonize
166 $stderr.puts "Debugger not available" if $DEBUG
167 map(map_path) do
168 require 'unicorn/app/old_rails/static'
169 use Unicorn::App::OldRails::Static
170 run inner_app
172 else
173 use Rails::Rack::LogTailer unless daemonize
174 use Rails::Rack::Debugger if $DEBUG
175 map(map_path) do
176 use Rails::Rack::Static
177 run inner_app
180 end.to_app
183 listeners << "#{host}:#{port}" if set_listener
185 if $DEBUG
186 pp({
187 :unicorn_options => options,
188 :app => app,
189 :daemonize => daemonize,
193 # ensure Rails standard tmp paths exist
194 %w(cache pids sessions sockets).each do |dir|
195 FileUtils.mkdir_p("tmp/#{dir}")
198 if daemonize
199 options[:pid] = rails_pid
200 Unicorn::Launcher.daemonize!
202 Unicorn.run(app, options)