unicorn 0.2.0
[unicorn.git] / bin / unicorn
blobebf57c3c323c2295bc18c1d6af9bb63df47e973c
1 #!/home/ew/bin/ruby
2 $stdin.sync = $stdout.sync = $stderr.sync = true
3 require 'unicorn' # require this first to populate Unicorn::DEFAULT_START_CTX
4 require 'optparse'
6 env = "development"
7 daemonize = false
8 listeners = []
9 options = { :listeners => listeners }
10 host = Unicorn::Const::DEFAULT_HOST
11 port = Unicorn::Const::DEFAULT_PORT
13 opts = OptionParser.new("", 24, ' ') do |opts|
14 opts.banner = "Usage: #{File.basename($0)} " \
15 "[ruby options] [unicorn options] [rackup config file]"
17 opts.separator "Ruby options:"
19 lineno = 1
20 opts.on("-e", "--eval LINE", "evaluate a LINE of code") do |line|
21 eval line, TOPLEVEL_BINDING, "-e", lineno
22 lineno += 1
23 end
25 opts.on("-d", "--debug", "set debugging flags (set $DEBUG to true)") do
26 $DEBUG = true
27 end
29 opts.on("-w", "--warn", "turn warnings on for your script") do
30 $-w = true
31 end
33 opts.on("-I", "--include PATH",
34 "specify $LOAD_PATH (may be used more than once)") do |path|
35 $LOAD_PATH.unshift(*path.split(/:/))
36 end
38 opts.on("-r", "--require LIBRARY",
39 "require the library, before executing your script") do |library|
40 require library
41 end
43 opts.separator "Unicorn options:"
45 # some of these switches exist for rackup command-line compatibility,
47 opts.on("-o", "--host HOST",
48 "listen on HOST (default: #{Unicorn::Const::DEFAULT_HOST})") do |h|
49 host = h
50 end
52 opts.on("-p", "--port PORT",
53 "use PORT (default: #{Unicorn::Const::DEFAULT_PORT})") do |p|
54 port = p.to_i
55 end
57 opts.on("-E", "--env ENVIRONMENT",
58 "use ENVIRONMENT for defaults (default: development)") do |e|
59 env = e
60 end
62 opts.on("-D", "--daemonize", "run daemonized in the background") do |d|
63 daemonize = d ? true : false
64 end
66 opts.on("-P", "--pid FILE", "file to store PID (default: none)") do |f|
67 options[:pid] = File.expand_path(f)
68 end
70 opts.on("-s", "--server SERVER",
71 "this flag only exists for compatibility") do |s|
72 warn "-s/--server only exists for compatibility with rackup"
73 end
75 # Unicorn-specific stuff
76 opts.on("-l", "--listen {HOST:PORT|PATH}",
77 "listen on HOST:PORT or PATH",
78 "this may be specified multiple times",
79 "(default: #{Unicorn::Const::DEFAULT_LISTEN})") do |address|
80 listeners << address
81 end
83 opts.on("-c", "--config-file FILE", "Unicorn-specific config file") do |f|
84 options[:config_file] = File.expand_path(f)
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 "unicorn v#{Unicorn::Const::UNICORN_VERSION}"
101 exit
104 opts.parse! ARGV
107 require 'pp' if $DEBUG
109 # require Rack as late as possible in case $LOAD_PATH is modified
110 # in config.ru or command-line
111 require 'rack'
113 config = ARGV[0] || "config.ru"
114 abort "configuration file #{config} not found" unless File.exist?(config)
116 inner_app = case config
117 when /\.ru$/
118 raw = File.open(config, "rb") { |fp| fp.sysread(fp.stat.size) }
119 # parse embedded command-line options in config.ru comments
120 if raw[/^#\\(.*)/]
121 opts.parse! $1.split(/\s+/)
123 lambda { || eval("Rack::Builder.new {(#{raw}\n)}.to_app", nil, config) }
124 else
125 lambda do ||
126 require config
127 Object.const_get(File.basename(config, '.rb').capitalize)
131 app = case env
132 when "development"
133 lambda do ||
134 Rack::Builder.new do
135 use Rack::CommonLogger, $stderr
136 use Rack::ShowExceptions
137 use Rack::Lint
138 run inner_app.call
139 end.to_app
141 when "deployment"
142 lambda do ||
143 Rack::Builder.new do
144 use Rack::CommonLogger, $stderr
145 run inner_app.call
146 end.to_app
148 else
149 inner_app
152 if listeners.empty?
153 listener = "#{host}:#{port}"
154 listeners << listener
157 if $DEBUG
158 pp({
159 :unicorn_options => options,
160 :app => app,
161 :inner_app => inner_app,
162 :daemonize => daemonize,
166 # only daemonize if we're not inheriting file descriptors from our parent
167 if daemonize
169 $stdin.reopen("/dev/null")
170 unless ENV['UNICORN_FD']
171 exit if fork
172 Process.setsid
173 exit if fork
176 # We don't do a lot of standard daemonization stuff:
177 # * $stderr/$stderr can/will be redirected separately
178 # * umask is whatever was set by the parent process at startup
179 # and can be set in config.ru and config_file, so making it
180 # 0000 and potentially exposing sensitive log data can be bad
181 # policy.
182 # * Don't bother to chdir here since Unicorn is designed to
183 # run inside APP_ROOT. Unicorn will also re-chdir() to
184 # the directory it was started in when being re-executed
185 # to pickup code changes if the original deployment directory
186 # is a symlink or otherwise got replaced.
189 Unicorn.run(app, options)