1
0
Fork 0
mirror of https://github.com/puma/puma.git synced 2022-11-09 13:48:40 -05:00
puma--puma/lib/puma/cli.rb

474 lines
12 KiB
Ruby
Raw Normal View History

2011-09-27 12:23:03 -04:00
require 'optparse'
2011-09-27 13:53:45 -04:00
require 'uri'
require 'puma/server'
2011-09-27 12:23:03 -04:00
require 'puma/const'
2011-12-07 16:42:53 -05:00
require 'puma/configuration'
2011-09-27 12:23:03 -04:00
require 'rack/commonlogger'
2012-01-13 19:29:50 -05:00
require 'rack/utils'
2011-09-27 12:23:03 -04:00
module Puma
2011-12-01 18:23:14 -05:00
# Handles invoke a Puma::Server in a command line style.
#
2011-09-27 12:23:03 -04:00
class CLI
IS_JRUBY = defined?(JRUBY_VERSION)
2011-12-01 18:23:14 -05:00
# Create a new CLI object using +argv+ as the command line
# arguments.
#
# +stdout+ and +stderr+ can be set to IO-like objects which
# this object will report status on.
#
def initialize(argv, stdout=STDOUT, stderr=STDERR)
2011-09-27 12:23:03 -04:00
@argv = argv
@stdout = stdout
@stderr = stderr
2011-09-27 12:23:03 -04:00
2011-09-27 17:33:17 -04:00
@events = Events.new @stdout, @stderr
@server = nil
@status = nil
@restart = false
@listeners = []
2011-09-27 12:23:03 -04:00
setup_options
generate_restart_data
@inherited_fds = {}
remove = []
ENV.each do |k,v|
if k =~ /PUMA_INHERIT_\d+/
fd, url = v.split(":", 2)
@inherited_fds[url] = fd.to_i
remove << k
end
end
remove.each do |k|
ENV.delete k
end
end
def restart_on_stop!
@restart = true
end
def generate_restart_data
# Use the same trick as unicorn, namely favor PWD because
# it will contain an unresolved symlink, useful for when
# the pwd is /data/releases/current.
if dir = ENV['PWD']
s_env = File.stat(dir)
s_pwd = File.stat(Dir.pwd)
if s_env.ino == s_pwd.ino and s_env.dev == s_pwd.dev
@restart_dir = dir
end
end
@restart_dir ||= Dir.pwd
@original_argv = ARGV.dup
if defined? Rubinius::OS_ARGV
@restart_argv = Rubinius::OS_ARGV
else
require 'rubygems'
# if $0 is a file in the current directory, then restart
# it the same, otherwise add -S on there because it was
# picked up in PATH.
#
if File.exists?($0)
arg0 = [Gem.ruby, $0]
else
arg0 = [Gem.ruby, "-S", $0]
end
@restart_argv = arg0 + ARGV
end
end
def restart!
@options[:on_restart].each do |blk|
blk.call self
end
if IS_JRUBY
@listeners.each_with_index do |(str,io),i|
io.close
# We have to unlink a unix socket path that's not being used
uri = URI.parse str
if uri.scheme == "unix"
path = "#{uri.host}#{uri.path}"
File.unlink path
end
end
require 'puma/jruby_restart'
JRubyRestart.chdir_exec(@restart_dir, Gem.ruby, *@restart_argv)
else
@listeners.each_with_index do |(l,io),i|
ENV["PUMA_INHERIT_#{i}"] = "#{io.to_i}:#{l}"
end
if cmd = @options[:restart_cmd]
argv = cmd.split(' ') + @original_argv
else
argv = @restart_argv
end
Dir.chdir @restart_dir
Kernel.exec(*argv)
end
2011-09-27 12:23:03 -04:00
end
2012-07-02 18:09:35 -04:00
# Delegate +log+ to +@events+
2011-12-01 18:23:14 -05:00
#
def log(str)
2012-07-02 18:09:35 -04:00
@events.log str
end
2012-07-02 18:09:35 -04:00
# Delegate +error+ to +@events+
2011-12-01 18:23:14 -05:00
#
def error(str)
2012-07-02 18:09:35 -04:00
@events.error str
end
2011-12-01 18:23:14 -05:00
# Build the OptionParser object to handle the available options.
#
2011-09-27 12:23:03 -04:00
def setup_options
2011-09-27 13:53:45 -04:00
@options = {
2011-09-27 17:33:17 -04:00
:min_threads => 0,
:max_threads => 16,
:quiet => false,
:binds => []
2011-09-27 13:53:45 -04:00
}
2011-09-27 12:23:03 -04:00
2011-09-27 13:53:45 -04:00
@parser = OptionParser.new do |o|
o.on "-b", "--bind URI", "URI to bind to (tcp:// and unix:// only)" do |arg|
@options[:binds] << arg
end
o.on "-C", "--config PATH", "Load PATH as a config file" do |arg|
@options[:config_file] = arg
end
o.on "-I", "--include PATH", "Specify $LOAD_PATH directories" do |arg|
$LOAD_PATH.unshift(*arg.split(':'))
end
2012-01-13 18:41:22 -05:00
o.on "-p", "--port PORT", "Define what port TCP port to bind to",
"Use -b for more advanced options" do |arg|
2012-02-22 13:19:03 -05:00
@options[:binds] << "tcp://#{Configuration::DefaultTCPHost}:#{arg}"
2012-01-13 18:41:22 -05:00
end
o.on "--pidfile PATH", "Use PATH as a pidfile" do |arg|
@options[:pidfile] = arg
end
o.on "-q", "--quiet", "Quiet down the output" do
@options[:quiet] = true
end
o.on "-S", "--state PATH", "Where to store the state details" do |arg|
@options[:state] = arg
end
o.on "--control URL", "The bind url to use for the control server",
"Use 'auto' to use temp unix server" do |arg|
if arg
@options[:control_url] = arg
elsif IS_JRUBY
raise NotImplementedError, "No default url available on JRuby"
end
end
o.on "--control-token TOKEN",
"The token to use as authentication for the control server" do |arg|
@options[:control_auth_token] = arg
end
2011-09-27 17:33:17 -04:00
o.on '-t', '--threads INT', "min:max threads to use (default 0:16)" do |arg|
min, max = arg.split(":")
if max
@options[:min_threads] = min.to_i
@options[:max_threads] = max.to_i
else
@options[:min_threads] = 0
@options[:max_threads] = arg.to_i
end
2011-09-27 13:53:45 -04:00
end
o.on "--restart-cmd CMD",
"The puma command to run during a hot restart",
"Default: inferred" do |cmd|
@options[:restart_cmd] = cmd
end
o.on "-e", "--environment ENVIRONMENT",
"The environment to run the Rack app on (default development)" do |arg|
@options[:environment] = arg
end
2011-09-27 12:23:03 -04:00
end
2011-09-27 13:53:45 -04:00
@parser.banner = "puma <options> <rackup file>"
2011-09-27 12:23:03 -04:00
2011-09-27 13:53:45 -04:00
@parser.on_tail "-h", "--help", "Show help" do
log @parser
2011-09-27 12:23:03 -04:00
exit 1
end
end
2011-12-01 18:23:14 -05:00
# If configured, write the pid of the current process out
# to a file.
#
2011-11-22 00:15:40 -05:00
def write_pid
if path = @options[:pidfile]
File.open(path, "w") do |f|
f.puts Process.pid
end
end
end
def set_rack_environment
2012-07-19 19:38:16 -04:00
# Try the user option first, then the environment variable,
# finally default to development
ENV['RACK_ENV'] = @options[:environment] ||
ENV['RACK_ENV'] ||
'development'
end
def delete_pidfile
if path = @options[:pidfile]
File.unlink path
end
end
def write_state
require 'yaml'
if path = @options[:state]
state = { "pid" => Process.pid }
cfg = @config.dup
cfg.options.delete :on_restart
state["config"] = cfg
File.open(path, "w") do |f|
f.write state.to_yaml
end
end
end
2011-12-01 18:23:14 -05:00
# :nodoc:
2011-11-22 00:15:40 -05:00
def parse_options
2011-09-27 13:53:45 -04:00
@parser.parse! @argv
2011-12-07 16:42:53 -05:00
if @argv.last
@options[:rackup] = @argv.shift
end
@config = Puma::Configuration.new @options
@config.load
2011-11-22 00:15:40 -05:00
end
def graceful_stop(server)
log " - Gracefully stopping, waiting for requests to finish"
server.stop(true)
delete_pidfile
log " - Goodbye!"
end
2011-12-01 18:23:14 -05:00
# Parse the options, load the rackup, start the server and wait
# for it to finish.
#
2011-11-22 00:15:40 -05:00
def run
parse_options
2011-09-27 12:23:03 -04:00
set_rack_environment
2011-12-07 16:42:53 -05:00
app = @config.app
2011-09-27 12:23:03 -04:00
2011-11-22 00:15:40 -05:00
write_pid
write_state
2011-09-27 13:53:45 -04:00
2011-09-27 17:33:17 -04:00
min_t = @options[:min_threads]
max_t = @options[:max_threads]
2011-12-07 16:53:36 -05:00
server = Puma::Server.new app, @events
2011-09-27 17:33:17 -04:00
server.min_threads = min_t
server.max_threads = max_t
2011-09-27 13:53:45 -04:00
log "Puma #{Puma::Const::PUMA_VERSION} starting..."
2011-09-27 17:33:17 -04:00
log "* Min threads: #{min_t}, max threads: #{max_t}"
log "* Environment: #{ENV['RACK_ENV']}"
2011-09-27 13:53:45 -04:00
@options[:binds].each do |str|
2011-09-27 13:53:45 -04:00
uri = URI.parse str
case uri.scheme
when "tcp"
if fd = @inherited_fds.delete(str)
log "* Inherited #{str}"
io = server.inherit_tcp_listener uri.host, uri.port, fd
else
log "* Listening on #{str}"
io = server.add_tcp_listener uri.host, uri.port
end
@listeners << [str, io]
2011-09-27 13:53:45 -04:00
when "unix"
if fd = @inherited_fds.delete(str)
log "* Inherited #{str}"
io = server.inherit_unix_listener uri.path, fd
else
log "* Listening on #{str}"
path = "#{uri.host}#{uri.path}"
2011-09-27 12:23:03 -04:00
umask = nil
if uri.query
params = Rack::Utils.parse_query uri.query
if u = params['umask']
# Use Integer() to respect the 0 prefix as octal
umask = Integer(u)
end
end
io = server.add_unix_listener path, umask
end
@listeners << [str, io]
2012-01-13 19:29:50 -05:00
when "ssl"
params = Rack::Utils.parse_query uri.query
require 'openssl'
ctx = OpenSSL::SSL::SSLContext.new
unless params['key']
error "Please specify the SSL key via 'key='"
end
ctx.key = OpenSSL::PKey::RSA.new File.read(params['key'])
unless params['cert']
error "Please specify the SSL cert via 'cert='"
end
ctx.cert = OpenSSL::X509::Certificate.new File.read(params['cert'])
ctx.verify_mode = OpenSSL::SSL::VERIFY_NONE
if fd = @inherited_fds.delete(str)
log "* Inherited #{str}"
io = server.inherited_ssl_listener fd, ctx
else
log "* Listening on #{str}"
io = server.add_ssl_listener uri.host, uri.port, ctx
end
@listeners << [str, io]
2011-09-27 13:53:45 -04:00
else
error "Invalid URI: #{str}"
2011-09-27 12:23:03 -04:00
end
end
# If we inherited fds but didn't use them (because of a
# configuration change), then be sure to close them.
@inherited_fds.each do |str, fd|
log "* Closing unused inherited connection: #{str}"
begin
IO.for_fd(fd).close
rescue SystemCallError
end
# We have to unlink a unix socket path that's not being used
uri = URI.parse str
if uri.scheme == "unix"
path = "#{uri.host}#{uri.path}"
File.unlink path
end
end
@server = server
if str = @options[:control_url]
require 'puma/app/status'
uri = URI.parse str
app = Puma::App::Status.new server, self
if token = @options[:control_auth_token]
app.auth_token = token unless token.empty? or token == :none
end
status = Puma::Server.new app, @events
status.min_threads = 0
status.max_threads = 1
case uri.scheme
when "tcp"
log "* Starting status server on #{str}"
status.add_tcp_listener uri.host, uri.port
when "unix"
log "* Starting status server on #{str}"
path = "#{uri.host}#{uri.path}"
status.add_unix_listener path
else
error "Invalid status URI: #{str}"
end
status.run
@status = status
end
2012-06-21 12:01:18 -04:00
begin
Signal.trap "SIGUSR2" do
@restart = true
server.begin_restart
end
rescue Exception
log "*** Sorry signal SIGUSR2 not implemented, restart feature disabled!"
end
2012-06-21 12:01:18 -04:00
begin
Signal.trap "SIGTERM" do
log " - Gracefully stopping, waiting for requests to finish"
server.stop false
end
rescue Exception
log "*** Sorry signal SIGTERM not implemented, gracefully stopping feature disabled!"
end
log "Use Ctrl-C to stop"
2011-09-27 12:23:03 -04:00
begin
server.run.join
rescue Interrupt
graceful_stop server
end
if @restart
log "* Restarting..."
@status.stop true if @status
restart!
end
2011-09-27 12:23:03 -04:00
end
def stop
@server.stop(true) if @server
delete_pidfile
end
2011-09-27 12:23:03 -04:00
end
end