2 $stdin.sync
= $stdout.sync
= $stderr.sync
= true
3 require 'unicorn' # require this first to populate Unicorn::DEFAULT_START_CTX
10 options
= { :listeners => listeners
}
11 host
= Unicorn
::Const::DEFAULT_HOST
12 port
= Unicorn
::Const::DEFAULT_PORT
14 opts
= OptionParser
.new("", 24, ' ') do |opts
|
15 opts
.banner
= "Usage: #{File.basename($0)} " \
16 "[ruby options] [unicorn options] [rackup config file]"
18 opts
.separator
"Ruby options:"
21 opts
.on("-e", "--eval LINE", "evaluate a LINE of code") do |line
|
22 eval line
, TOPLEVEL_BINDING
, "-e", lineno
26 opts
.on("-d", "--debug", "set debugging flags (set $DEBUG to true)") do
30 opts
.on("-w", "--warn", "turn warnings on for your script") do
34 opts
.on("-I", "--include PATH",
35 "specify $LOAD_PATH (may be used more than once)") do |path
|
36 $LOAD_PATH.unshift(*path
.split(/:/))
39 opts
.on("-r", "--require LIBRARY",
40 "require the library, before executing your script") do |library
|
44 opts
.separator
"Unicorn options:"
46 # some of these switches exist for rackup command-line compatibility,
48 opts
.on("-o", "--host HOST",
49 "listen on HOST (default: #{Unicorn::Const::DEFAULT_HOST})") do |h
|
53 opts
.on("-p", "--port PORT",
54 "use PORT (default: #{Unicorn::Const::DEFAULT_PORT})") do |p
|
58 opts
.on("-E", "--env ENVIRONMENT",
59 "use ENVIRONMENT for defaults (default: development)") do |e
|
63 opts
.on("-D", "--daemonize", "run daemonized in the background") do |d
|
64 daemonize
= d
? true : false
67 # Unicorn-specific stuff
68 opts
.on("-l", "--listen {HOST:PORT|PATH}",
69 "listen on HOST:PORT or PATH",
70 "this may be specified multiple times",
71 "(default: #{Unicorn::Const::DEFAULT_LISTEN})") do |address
|
75 opts
.on("-C", "--directory PATH", "run in this directory") do |d
|
76 options
[:directory] = d
79 opts
.on("-c", "--config-file FILE", "Unicorn-specific config file") do |f
|
80 options
[:config_file] = File
.expand_path(f
)
83 # I'm avoiding Unicorn-specific config options on the command-line.
84 # IMNSHO, config options on the command-line are redundant given
85 # config files and make things unnecessarily complicated with multiple
86 # places to look for a config option.
88 opts
.separator
"Common options:"
90 opts
.on_tail("-h", "--help", "Show this message") do
95 opts
.on_tail("-v", "--version", "Show version") do
96 puts
"unicorn v#{Unicorn::Const::UNICORN_VERSION}"
103 require 'pp' if $DEBUG
105 config
= ARGV[0] || "config.ru"
106 abort
"configuration file #{config} not found" unless File
.exist
?(config
)
108 inner_app
= case config
110 raw
= File
.open(config
, "rb") { |fp
| fp
.sysread(fp
.stat
.size
) }
112 warn
%(not parsing embedded command-line options: "#$1")
114 lambda { || eval("Rack::Builder.new {(#{raw}\n)}.to_app", nil, config) }
118 Object.const_get(File.basename(config, '.rb').capitalize)
126 use Rack::CommonLogger, $stderr
127 use Rack::ShowExceptions
135 use Rack::CommonLogger, $stderr
144 listener = "#{host}:#{port}"
145 listeners << listener if listener != Unicorn::Const::DEFAULT_LISTEN
150 :unicorn_options => options,
152 :inner_app => inner_app,
153 :daemonize => daemonize,
157 # only daemonize if we're not inheriting file descriptors from our parent
160 $stdin.reopen("/dev/null")
161 unless ENV['UNICORN_FD']
167 # We don't do a lot of standard daemonization stuff:
168 # * $stderr/$stderr can/will be redirected separately
169 # * umask is whatever was set by the parent process at startup
170 # and can be set in config.ru and config_file, so making it
171 # 0000 and potentially exposing sensitive log data can be bad
173 # * Don't bother to chdir here since Unicorn is designed to
174 # run inside APP_ROOT (or whatever the "directory" directive
175 # specifies). Unicorn will also re-chdir() to the directory
176 # it was started in when being re-executed to pickup code
177 # changes if the original deployment directory is a symlink
178 # or otherwise got replaced.
181 Unicorn.run(app, options)