examples/init.sh: update to reduce upgrade raciness
[unicorn.git] / bin / unicorn_rails
blobea4f822fb0556801cddd568f6b7608f5b854aa87
1 #!/this/will/be/overwritten/or/wrapped/anyways/do/not/worry/ruby
2 # -*- encoding: binary -*-
3 require 'unicorn/launcher'
4 require 'optparse'
5 require 'fileutils'
7 ENV['RAILS_ENV'] ||= "development"
8 rackup_opts = Unicorn::Configurator::RACKUP
9 options = rackup_opts[:options]
11 op = OptionParser.new("", 24, ' ') do |opts|
12 cmd = File.basename($0)
13 opts.banner = "Usage: #{cmd} " \
14 "[ruby options] [#{cmd} options] [rackup config file]"
15 opts.separator "Ruby options:"
17 lineno = 1
18 opts.on("-e", "--eval LINE", "evaluate a LINE of code") do |line|
19 eval line, TOPLEVEL_BINDING, "-e", lineno
20 lineno += 1
21 end
23 opts.on("-d", "--debug", "set debugging flags (set $DEBUG to true)") do
24 $DEBUG = true
25 end
27 opts.on("-w", "--warn", "turn warnings on for your script") do
28 $-w = true
29 end
31 opts.on("-I", "--include PATH",
32 "specify $LOAD_PATH (may be used more than once)") do |path|
33 $LOAD_PATH.unshift(*path.split(':'))
34 end
36 opts.on("-r", "--require LIBRARY",
37 "require the library, before executing your script") do |library|
38 require library
39 end
41 opts.separator "#{cmd} options:"
43 # some of these switches exist for rackup command-line compatibility,
45 opts.on("-o", "--host HOST",
46 "listen on HOST (default: #{Unicorn::Const::DEFAULT_HOST})") do |h|
47 rackup_opts[:host] = h
48 rackup_opts[:set_listener] = true
49 end
51 opts.on("-p", "--port PORT", Integer,
52 "use PORT (default: #{Unicorn::Const::DEFAULT_PORT})") do |port|
53 rackup_opts[:port] = port
54 rackup_opts[:set_listener] = true
55 end
57 opts.on("-E", "--env RAILS_ENV",
58 "use RAILS_ENV for defaults (default: development)") do |e|
59 ENV['RAILS_ENV'] = e
60 end
62 opts.on("-D", "--daemonize", "run daemonized in the background") do |d|
63 rackup_opts[:daemonize] = !!d
64 end
66 # Unicorn-specific stuff
67 opts.on("-l", "--listen {HOST:PORT|PATH}",
68 "listen on HOST:PORT or PATH",
69 "this may be specified multiple times",
70 "(default: #{Unicorn::Const::DEFAULT_LISTEN})") do |address|
71 options[:listeners] << address
72 end
74 opts.on("-c", "--config-file FILE", "Unicorn-specific config file") do |f|
75 options[:config_file] = f
76 end
78 opts.on("-P PATH", "DEPRECATED") do |v|
79 warn %q{Use of -P is ambiguous and discouraged}
80 warn %q{Use --path or RAILS_RELATIVE_URL_ROOT instead}
81 ENV['RAILS_RELATIVE_URL_ROOT'] = v
82 end
84 opts.on("--path PATH", "Runs Rails app mounted at a specific path.",
85 "(default: /)") do |v|
86 ENV['RAILS_RELATIVE_URL_ROOT'] = v
87 end
89 # I'm avoiding Unicorn-specific config options on the command-line.
90 # IMNSHO, config options on the command-line are redundant given
91 # config files and make things unnecessarily complicated with multiple
92 # places to look for a config option.
94 opts.separator "Common options:"
96 opts.on_tail("-h", "--help", "Show this message") do
97 puts opts.to_s.gsub(/^.*DEPRECATED.*$/s, '')
98 exit
99 end
101 opts.on_tail("-v", "--version", "Show version") do
102 puts "#{cmd} v#{Unicorn::Const::UNICORN_VERSION}"
103 exit
106 opts.parse! ARGV
109 def rails_dispatcher
110 if ::Rails::VERSION::MAJOR >= 3 && ::File.exist?('config/application.rb')
111 if ::File.read('config/application.rb') =~ /^module\s+([\w:]+)\s*$/
112 app_module = Object.const_get($1)
113 begin
114 result = app_module::Application
115 rescue NameError
120 if result.nil? && defined?(ActionController::Dispatcher)
121 result = ActionController::Dispatcher.new
124 result || abort("Unable to locate the application dispatcher class")
127 def rails_builder(ru, op, daemonize)
128 return Unicorn.builder(ru, op) if ru
130 # allow Configurator to parse cli switches embedded in the ru file
131 Unicorn::Configurator::RACKUP.update(:file => :rails, :optparse => op)
133 # this lambda won't run until after forking if preload_app is false
134 # this runs after config file reloading
135 lambda do ||
136 # Rails 3 includes a config.ru, use it if we find it after
137 # working_directory is bound.
138 ::File.exist?('config.ru') and
139 return Unicorn.builder('config.ru', op).call
141 # Load Rails and (possibly) the private version of Rack it bundles.
142 begin
143 require ::File.expand_path('config/boot')
144 require ::File.expand_path('config/environment')
145 rescue LoadError => err
146 abort "#$0 must be run inside RAILS_ROOT: #{err.inspect}"
149 defined?(::Rails::VERSION::STRING) or
150 abort "Rails::VERSION::STRING not defined by config/{boot,environment}"
151 # it seems Rails >=2.2 support Rack, but only >=2.3 requires it
152 old_rails = case ::Rails::VERSION::MAJOR
153 when 0, 1 then true
154 when 2 then Rails::VERSION::MINOR < 3 ? true : false
155 else
156 false
159 Rack::Builder.new do
160 map_path = ENV['RAILS_RELATIVE_URL_ROOT'] || '/'
161 if old_rails
162 if map_path != '/'
163 # patches + tests welcome, but I really cbf to deal with this
164 # since all apps I've ever dealt with just use "/" ...
165 warn "relative URL roots may not work for older Rails"
167 warn "LogTailer not available for Rails < 2.3" unless daemonize
168 warn "Debugger not available" if $DEBUG
169 require 'unicorn/app/old_rails'
170 map(map_path) do
171 use Unicorn::App::OldRails::Static
172 run Unicorn::App::OldRails.new
174 else
175 use Rails::Rack::LogTailer unless daemonize
176 use Rails::Rack::Debugger if $DEBUG
177 map(map_path) do
178 unless defined?(ActionDispatch::Static)
179 use Rails::Rack::Static
181 run rails_dispatcher
184 end.to_app
188 app = rails_builder(ARGV[0], op, rackup_opts[:daemonize])
189 op = nil
191 if $DEBUG
192 require 'pp'
193 pp({
194 :unicorn_options => options,
195 :app => app,
196 :daemonize => rackup_opts[:daemonize],
200 # ensure Rails standard tmp paths exist
201 options[:after_reload] = lambda do
202 FileUtils.mkdir_p(%w(cache pids sessions sockets).map! { |d| "tmp/#{d}" })
205 if rackup_opts[:daemonize]
206 options[:pid] = "tmp/pids/unicorn.pid"
207 Unicorn::Launcher.daemonize!(options)
209 Unicorn::HttpServer.new(app, options).start.join