2009-07-07 00:42:54 -04:00
|
|
|
#!/usr/bin/env ruby
|
|
|
|
#
|
2011-09-23 23:46:33 -04:00
|
|
|
# Copyright (c) 2011 Evan Phoenix
|
2007-08-25 13:32:51 -04:00
|
|
|
# Copyright (c) 2005 Zed A. Shaw
|
2006-06-30 16:42:12 -04:00
|
|
|
#
|
|
|
|
|
2006-03-05 02:58:18 -05:00
|
|
|
require 'yaml'
|
2007-10-27 13:33:12 -04:00
|
|
|
require 'etc'
|
|
|
|
|
2011-09-22 22:24:43 -04:00
|
|
|
require 'puma'
|
2007-10-27 13:33:12 -04:00
|
|
|
|
2011-09-23 23:24:32 -04:00
|
|
|
Puma::Gems.require 'puma/gem_plugin'
|
2007-11-12 16:13:03 -05:00
|
|
|
|
2007-10-27 13:33:12 -04:00
|
|
|
# require 'ruby-debug'
|
|
|
|
# Debugger.start
|
2006-03-26 15:01:50 -05:00
|
|
|
|
2011-09-22 22:24:43 -04:00
|
|
|
module Puma
|
2006-09-22 04:16:54 -04:00
|
|
|
class Start < GemPlugin::Plugin "/commands"
|
2011-09-22 22:24:43 -04:00
|
|
|
include Puma::Command::Base
|
2006-09-22 04:16:54 -04:00
|
|
|
|
|
|
|
def configure
|
|
|
|
options [
|
|
|
|
['-p', '--port PORT', "Which port to bind to", :@port, 3000],
|
|
|
|
['-a', '--address ADDR', "Address to bind to", :@address, "0.0.0.0"],
|
2011-09-22 22:24:43 -04:00
|
|
|
['-l', '--log FILE', "Where to write log messages", :@log_file, "log/puma.log"],
|
|
|
|
['-P', '--pid FILE', "Where to write the PID", :@pid_file, "log/puma.pid"],
|
2011-09-23 23:24:32 -04:00
|
|
|
['-n', '--concurrency INT', "Number of concurrent threads to use",
|
|
|
|
:@concurrency, 16],
|
2006-09-22 04:16:54 -04:00
|
|
|
['-c', '--chdir PATH', "Change to dir before starting (will be expanded)", :@cwd, Dir.pwd],
|
|
|
|
['-C', '--config PATH', "Use a config file", :@config_file, nil],
|
2006-09-26 17:25:44 -04:00
|
|
|
['-S', '--script PATH', "Load the given file as an extra config script", :@config_script, nil],
|
|
|
|
['-G', '--generate PATH', "Generate a config file for use with -C", :@generate, nil],
|
2011-09-23 23:24:32 -04:00
|
|
|
['-r', '--rackup PATH', 'Load a specific rackup file', :@rackup_file, "config.ru"],
|
2006-10-24 19:00:29 -04:00
|
|
|
['', '--user USER', "User to run as", :@user, nil],
|
2011-09-23 23:24:32 -04:00
|
|
|
['', '--group GROUP', "Group to run as", :@group, nil]
|
2006-09-22 04:16:54 -04:00
|
|
|
]
|
|
|
|
end
|
2006-03-26 15:01:50 -05:00
|
|
|
|
2006-09-22 04:16:54 -04:00
|
|
|
def validate
|
2007-08-25 13:32:51 -04:00
|
|
|
if @config_file
|
|
|
|
valid_exists?(@config_file, "Config file not there: #@config_file")
|
|
|
|
return false unless @valid
|
|
|
|
@config_file = File.expand_path(@config_file)
|
|
|
|
load_config
|
|
|
|
return false unless @valid
|
|
|
|
end
|
|
|
|
|
2006-09-22 04:16:54 -04:00
|
|
|
@cwd = File.expand_path(@cwd)
|
|
|
|
valid_dir? @cwd, "Invalid path to change to during daemon mode: #@cwd"
|
2006-02-14 07:19:02 -05:00
|
|
|
|
2006-09-26 17:25:44 -04:00
|
|
|
# Change there to start, then we'll have to come back after daemonize
|
2006-09-22 04:16:54 -04:00
|
|
|
Dir.chdir(@cwd)
|
2006-02-14 07:19:02 -05:00
|
|
|
|
2006-09-22 04:16:54 -04:00
|
|
|
valid_dir? File.dirname(@log_file), "Path to log file not valid: #@log_file"
|
|
|
|
valid_dir? File.dirname(@pid_file), "Path to pid file not valid: #@pid_file"
|
|
|
|
valid_exists? @mime_map, "MIME mapping file does not exist: #@mime_map" if @mime_map
|
|
|
|
valid_exists? @config_file, "Config file not there: #@config_file" if @config_file
|
|
|
|
valid_dir? File.dirname(@generate), "Problem accessing directory to #@generate" if @generate
|
|
|
|
valid_user? @user if @user
|
|
|
|
valid_group? @group if @group
|
2006-06-05 04:54:06 -04:00
|
|
|
|
2011-09-23 23:24:32 -04:00
|
|
|
@rackup = ARGV.shift || "config.ru"
|
|
|
|
|
|
|
|
valid? File.exists?(@rackup), "Unable to find rackup file"
|
|
|
|
|
2006-09-22 04:16:54 -04:00
|
|
|
return @valid
|
2006-05-12 15:39:42 -04:00
|
|
|
end
|
|
|
|
|
2006-09-22 04:16:54 -04:00
|
|
|
def run
|
|
|
|
if @generate
|
2011-09-23 23:24:32 -04:00
|
|
|
@generate = File.expand_path(@generate)
|
2011-09-23 01:14:39 -04:00
|
|
|
@stderr.puts "** Writing config to \"#@generate\"."
|
2006-09-22 04:16:54 -04:00
|
|
|
open(@generate, "w") {|f| f.write(settings.to_yaml) }
|
2011-09-23 01:14:39 -04:00
|
|
|
@stderr.puts "** Finished. Run \"puma_rails start -C #@generate\" to use the config file."
|
2006-09-22 04:16:54 -04:00
|
|
|
exit 0
|
2006-03-26 15:01:50 -05:00
|
|
|
end
|
2006-05-19 22:56:30 -04:00
|
|
|
|
2011-09-23 23:24:32 -04:00
|
|
|
rackup = @rackup
|
2006-03-26 15:01:50 -05:00
|
|
|
|
2011-09-23 23:24:32 -04:00
|
|
|
config = Puma::Configurator.new(settings) do |c|
|
|
|
|
c.log "Starting Puma listening at #{c.defaults[:host]}:#{c.defaults[:port]}"
|
2006-04-01 03:43:30 -05:00
|
|
|
|
2011-09-23 23:24:32 -04:00
|
|
|
c.listener do |l|
|
|
|
|
l.load_rackup rackup
|
2006-03-05 02:58:18 -05:00
|
|
|
|
2011-09-23 23:24:32 -04:00
|
|
|
l.load_plugins
|
2006-09-22 04:16:54 -04:00
|
|
|
|
2011-09-23 23:24:32 -04:00
|
|
|
if c.defaults[:config_script]
|
|
|
|
c.log "Loading #{c.defaults[:config_script]} external config script"
|
|
|
|
c.run_config(c.defaults[:config_script])
|
2006-09-22 04:16:54 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2006-05-22 23:58:37 -04:00
|
|
|
|
2006-09-22 04:16:54 -04:00
|
|
|
config.run
|
2011-09-22 22:24:43 -04:00
|
|
|
config.log "Puma #{Puma::Const::PUMA_VERSION} available at #{@address}:#{@port}"
|
2011-09-23 23:24:32 -04:00
|
|
|
config.log "Use CTRL-C to stop."
|
2006-09-22 04:16:54 -04:00
|
|
|
|
|
|
|
config.join
|
2006-02-11 22:37:38 -05:00
|
|
|
end
|
2007-08-25 13:32:51 -04:00
|
|
|
|
|
|
|
def load_config
|
|
|
|
settings = {}
|
|
|
|
begin
|
|
|
|
settings = YAML.load_file(@config_file)
|
|
|
|
ensure
|
2011-09-23 23:24:32 -04:00
|
|
|
@stderr.puts "** Loading settings from #{@config_file} (they override command line)."
|
2007-08-25 13:32:51 -04:00
|
|
|
end
|
|
|
|
|
2011-09-22 22:24:43 -04:00
|
|
|
settings[:includes] ||= ["puma"]
|
2007-08-25 13:32:51 -04:00
|
|
|
|
|
|
|
# Config file settings will override command line settings
|
|
|
|
settings.each do |key, value|
|
|
|
|
key = key.to_s
|
|
|
|
if config_keys.include?(key)
|
|
|
|
key = 'address' if key == 'host'
|
|
|
|
self.instance_variable_set("@#{key}", value)
|
|
|
|
else
|
|
|
|
failure "Unknown configuration setting: #{key}"
|
|
|
|
@valid = false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def config_keys
|
|
|
|
@config_keys ||=
|
2011-09-23 23:24:32 -04:00
|
|
|
%w(address host port cwd log_file pid_file config_script concurrency user group)
|
2007-08-25 13:32:51 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def settings
|
|
|
|
config_keys.inject({}) do |hash, key|
|
|
|
|
value = self.instance_variable_get("@#{key}")
|
|
|
|
key = 'host' if key == 'address'
|
2007-10-27 13:33:12 -04:00
|
|
|
hash[key.to_sym] ||= value
|
2007-08-25 13:32:51 -04:00
|
|
|
hash
|
|
|
|
end
|
|
|
|
end
|
2006-02-14 07:19:02 -05:00
|
|
|
end
|
2006-02-11 22:37:38 -05:00
|
|
|
|
2011-09-23 23:24:32 -04:00
|
|
|
def Puma.send_signal(signal, pid_file)
|
2008-03-23 23:48:10 -04:00
|
|
|
pid = File.read(pid_file).to_i
|
2011-09-22 22:24:43 -04:00
|
|
|
print "Sending #{signal} to Puma at PID #{pid}..."
|
2006-09-22 04:16:54 -04:00
|
|
|
begin
|
|
|
|
Process.kill(signal, pid)
|
|
|
|
rescue Errno::ESRCH
|
|
|
|
puts "Process does not exist. Not running."
|
|
|
|
end
|
2006-05-19 22:56:30 -04:00
|
|
|
|
2006-09-22 04:16:54 -04:00
|
|
|
puts "Done."
|
|
|
|
end
|
2006-02-11 22:37:38 -05:00
|
|
|
|
2006-09-22 04:16:54 -04:00
|
|
|
class Stop < GemPlugin::Plugin "/commands"
|
2011-09-22 22:24:43 -04:00
|
|
|
include Puma::Command::Base
|
2006-02-11 22:37:38 -05:00
|
|
|
|
2006-09-22 04:16:54 -04:00
|
|
|
def configure
|
|
|
|
options [
|
2006-09-26 17:25:44 -04:00
|
|
|
['-c', '--chdir PATH', "Change to dir before starting (will be expanded).", :@cwd, "."],
|
2006-12-16 00:54:59 -05:00
|
|
|
['-f', '--force', "Force the shutdown (kill -9).", :@force, false],
|
|
|
|
['-w', '--wait SECONDS', "Wait SECONDS before forcing shutdown", :@wait, "0"],
|
2011-09-22 22:24:43 -04:00
|
|
|
['-P', '--pid FILE', "Where the PID file is located.", :@pid_file, "log/puma.pid"]
|
2006-09-22 04:16:54 -04:00
|
|
|
]
|
|
|
|
end
|
2006-05-19 22:56:30 -04:00
|
|
|
|
2006-09-22 04:16:54 -04:00
|
|
|
def validate
|
|
|
|
@cwd = File.expand_path(@cwd)
|
|
|
|
valid_dir? @cwd, "Invalid path to change to during daemon mode: #@cwd"
|
2006-02-14 07:19:02 -05:00
|
|
|
|
2006-09-22 04:16:54 -04:00
|
|
|
Dir.chdir @cwd
|
2006-02-14 07:19:02 -05:00
|
|
|
|
2006-09-26 17:25:44 -04:00
|
|
|
valid_exists? @pid_file, "PID file #@pid_file does not exist. Not running?"
|
2006-09-22 04:16:54 -04:00
|
|
|
return @valid
|
|
|
|
end
|
2006-02-11 22:37:38 -05:00
|
|
|
|
2006-09-22 04:16:54 -04:00
|
|
|
def run
|
|
|
|
if @force
|
2006-12-16 00:54:59 -05:00
|
|
|
@wait.to_i.times do |waiting|
|
|
|
|
exit(0) if not File.exist? @pid_file
|
|
|
|
sleep 1
|
|
|
|
end
|
|
|
|
|
2011-09-23 23:24:32 -04:00
|
|
|
Puma.send_signal("KILL", @pid_file) if File.exist? @pid_file
|
2006-09-22 04:16:54 -04:00
|
|
|
else
|
2011-09-23 23:24:32 -04:00
|
|
|
Puma.send_signal("TERM", @pid_file)
|
2006-09-22 04:16:54 -04:00
|
|
|
end
|
2006-02-26 16:39:40 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2006-09-22 04:16:54 -04:00
|
|
|
class Restart < GemPlugin::Plugin "/commands"
|
2011-09-22 22:24:43 -04:00
|
|
|
include Puma::Command::Base
|
2006-02-26 16:39:40 -05:00
|
|
|
|
2006-09-22 04:16:54 -04:00
|
|
|
def configure
|
|
|
|
options [
|
|
|
|
['-c', '--chdir PATH', "Change to dir before starting (will be expanded)", :@cwd, '.'],
|
|
|
|
['-s', '--soft', "Do a soft restart rather than a process exit restart", :@soft, false],
|
2011-09-22 22:24:43 -04:00
|
|
|
['-P', '--pid FILE', "Where the PID file is located", :@pid_file, "log/puma.pid"]
|
2006-09-22 04:16:54 -04:00
|
|
|
]
|
|
|
|
end
|
2006-05-19 22:56:30 -04:00
|
|
|
|
2006-09-22 04:16:54 -04:00
|
|
|
def validate
|
|
|
|
@cwd = File.expand_path(@cwd)
|
|
|
|
valid_dir? @cwd, "Invalid path to change to during daemon mode: #@cwd"
|
2006-02-26 16:39:40 -05:00
|
|
|
|
2006-09-22 04:16:54 -04:00
|
|
|
Dir.chdir @cwd
|
2006-02-26 16:39:40 -05:00
|
|
|
|
2006-09-26 17:25:44 -04:00
|
|
|
valid_exists? @pid_file, "PID file #@pid_file does not exist. Not running?"
|
2006-09-22 04:16:54 -04:00
|
|
|
return @valid
|
|
|
|
end
|
2006-02-26 16:39:40 -05:00
|
|
|
|
2006-09-22 04:16:54 -04:00
|
|
|
def run
|
|
|
|
if @soft
|
2011-09-23 23:24:32 -04:00
|
|
|
Puma.send_signal("HUP", @pid_file)
|
2006-09-22 04:16:54 -04:00
|
|
|
else
|
2011-09-23 23:24:32 -04:00
|
|
|
Puma.send_signal("USR2", @pid_file)
|
2006-09-22 04:16:54 -04:00
|
|
|
end
|
2006-02-11 20:30:33 -05:00
|
|
|
end
|
|
|
|
end
|
2006-02-09 21:38:18 -05:00
|
|
|
end
|
2006-02-11 20:30:33 -05:00
|
|
|
|
2006-03-25 16:15:30 -05:00
|
|
|
|
2011-09-23 23:24:32 -04:00
|
|
|
GemPlugin::Manager.instance.load "puma" => GemPlugin::INCLUDE
|
2006-03-06 00:31:39 -05:00
|
|
|
|
2011-09-23 01:14:39 -04:00
|
|
|
exit 1 unless Puma::Command::Registry.instance.run(ARGV)
|