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/control_cli.rb

252 lines
6 KiB
Ruby
Raw Normal View History

require 'optparse'
require 'puma/const'
2011-12-07 16:42:53 -05:00
require 'puma/configuration'
require 'yaml'
require 'uri'
require 'socket'
module Puma
class ControlCLI
COMMANDS = %w{halt restart phased-restart start stats status stop}
2012-09-02 08:56:05 -04:00
def is_windows?
RUBY_PLATFORM =~ /(win|w)32$/ ? true : false
end
def initialize(argv, stdout=STDOUT, stderr=STDERR)
2012-10-16 01:37:28 -04:00
@argv = argv
@stdout = stdout
@stderr = stderr
2012-09-02 08:56:05 -04:00
@options = {}
2012-10-16 01:24:22 -04:00
opts = OptionParser.new do |o|
o.banner = "Usage: pumactl (-p PID | -P pidfile | -S status_file | -C url -T token | -F config.rb) (#{COMMANDS.join("|")})"
2012-10-16 01:24:22 -04:00
o.on "-S", "--state PATH", "Where the state file to use is" do |arg|
@options[:state] = arg
2012-09-02 08:56:05 -04:00
end
2012-10-16 01:24:22 -04:00
o.on "-Q", "--quiet", "Not display messages" do |arg|
2012-09-02 08:56:05 -04:00
@options[:quiet_flag] = true
end
2012-10-16 01:24:22 -04:00
o.on "-P", "--pidfile PATH", "Pid file" do |arg|
@options[:pidfile] = arg
2012-09-02 08:56:05 -04:00
end
2012-10-16 01:24:22 -04:00
2012-10-16 01:41:23 -04:00
o.on "-p", "--pid PID", "Pid" do |arg|
@options[:pid] = arg.to_i
end
2012-10-16 01:24:22 -04:00
o.on "-C", "--control-url URL", "The bind url to use for the control server" do |arg|
2012-09-02 08:56:05 -04:00
@options[:control_url] = arg
end
2012-10-16 01:24:22 -04:00
o.on "-T", "--control-token TOKEN", "The token to use as authentication for the control server" do |arg|
2012-09-02 08:56:05 -04:00
@options[:control_auth_token] = arg
end
2012-10-16 01:24:22 -04:00
o.on "-F", "--config-file PATH", "Puma config script" do |arg|
@options[:config_file] = arg
end
2012-10-16 01:24:22 -04:00
o.on_tail("-H", "--help", "Show this message") do
2012-10-16 01:37:28 -04:00
@stdout.puts o
2012-09-02 08:56:05 -04:00
exit
end
2012-10-16 01:24:22 -04:00
o.on_tail("-V", "--version", "Show version") do
2012-09-02 08:56:05 -04:00
puts Const::PUMA_VERSION
exit
end
2012-10-16 01:24:22 -04:00
end
2012-10-16 01:37:28 -04:00
opts.order!(argv) { |a| opts.terminate a }
2012-09-02 08:56:05 -04:00
command = argv.shift
@options[:command] = command if command
2012-10-16 01:24:22 -04:00
Puma::Configuration.new(@options).load if @options[:config_file]
2012-09-02 08:56:05 -04:00
# check present of command
unless @options[:command]
raise "Available commands: #{COMMANDS.join(", ")}"
2012-10-16 01:24:22 -04:00
end
2012-09-02 08:56:05 -04:00
unless COMMANDS.include? @options[:command]
raise "Invalid command: #{@options[:command]}"
end
2012-10-16 01:24:22 -04:00
2012-09-02 08:56:05 -04:00
rescue => e
@stdout.puts e.message
exit 1
end
2012-10-16 01:24:22 -04:00
def message(msg)
2012-09-02 08:56:05 -04:00
@stdout.puts msg unless @options[:quiet_flag]
end
2012-09-02 08:56:05 -04:00
def prepare_configuration
if @options.has_key? :state
unless File.exist? @options[:state]
raise "Status file not found: #{@options[:state]}"
2012-10-16 01:24:22 -04:00
end
status = YAML.load File.read(@options[:state])
2012-10-16 01:24:22 -04:00
2013-07-06 01:02:17 -04:00
if status.kind_of?(Hash) && status.has_key?("config")
2012-10-16 01:24:22 -04:00
conf = status["config"]
2012-09-02 08:56:05 -04:00
# get control_url
2012-10-16 01:24:22 -04:00
if url = conf.options[:control_url]
@options[:control_url] = url
2012-09-02 08:56:05 -04:00
end
2012-10-16 01:24:22 -04:00
2012-09-02 08:56:05 -04:00
# get control_auth_token
2012-10-16 01:24:22 -04:00
if token = conf.options[:control_auth_token]
@options[:control_auth_token] = token
2012-09-02 08:56:05 -04:00
end
2012-10-16 01:24:22 -04:00
2012-09-02 08:56:05 -04:00
# get pid
@options[:pid] = status["pid"].to_i
else
raise "Invalid status file: #{@options[:state]}"
end
2012-10-16 01:24:22 -04:00
elsif @options.has_key? :pidfile
2012-09-02 08:56:05 -04:00
# get pid from pid_file
@options[:pid] = File.open(@options[:pidfile]).gets.to_i
end
end
2012-09-02 08:56:05 -04:00
def send_request
uri = URI.parse @options[:control_url]
2012-09-02 08:56:05 -04:00
# create server object by scheme
@server = case uri.scheme
2012-10-16 01:41:23 -04:00
when "tcp"
TCPSocket.new uri.host, uri.port
when "unix"
UNIXSocket.new "#{uri.host}#{uri.path}"
else
raise "Invalid scheme: #{uri.scheme}"
end
2012-10-16 01:24:22 -04:00
if @options[:command] == "status"
message "Puma is started"
else
2012-09-02 08:56:05 -04:00
url = "/#{@options[:command]}"
2012-10-16 01:24:22 -04:00
2012-09-02 08:56:05 -04:00
if @options.has_key?(:control_auth_token)
url = url + "?token=#{@options[:control_auth_token]}"
end
2012-10-16 01:24:22 -04:00
2012-09-02 08:56:05 -04:00
@server << "GET #{url} HTTP/1.0\r\n\r\n"
2012-10-16 01:24:22 -04:00
2013-06-01 16:55:17 -04:00
unless data = @server.read
raise "Server closed connection before responding"
2013-06-01 17:24:33 -04:00
end
2013-06-01 16:55:17 -04:00
response = data.split("\r\n")
if response.empty?
raise "Server sent empty response"
end
2012-10-16 01:24:22 -04:00
(@http,@code,@message) = response.first.split(" ",3)
2012-10-16 01:24:22 -04:00
2012-09-02 08:56:05 -04:00
if @code == "403"
raise "Unauthorized access to server (wrong auth token)"
elsif @code == "404"
raise "Command error: #{response.last}"
2012-09-02 08:56:05 -04:00
elsif @code != "200"
raise "Bad response from server: #{@code}"
end
2012-10-16 01:24:22 -04:00
2012-09-02 08:56:05 -04:00
message "Command #{@options[:command]} sent success"
message response.last if @options[:command] == "stats"
end
2012-09-02 08:56:05 -04:00
@server.close
end
2012-09-02 08:56:05 -04:00
def send_signal
2012-10-16 01:41:23 -04:00
unless pid = @options[:pid]
raise "Neither pid nor control url available"
end
begin
Process.getpgid pid
rescue SystemCallError
if @options[:command] == "restart"
@options.delete(:command)
start
else
raise "No pid '#{pid}' found"
end
2012-10-16 01:41:23 -04:00
end
2012-10-16 01:24:22 -04:00
2012-09-02 08:56:05 -04:00
case @options[:command]
when "restart"
2012-10-16 01:41:23 -04:00
Process.kill "SIGUSR2", pid
2012-10-16 01:24:22 -04:00
2012-09-02 08:56:05 -04:00
when "halt"
2012-10-16 01:41:23 -04:00
Process.kill "QUIT", pid
2012-10-16 01:24:22 -04:00
2012-09-02 08:56:05 -04:00
when "stop"
2012-10-16 01:41:23 -04:00
Process.kill "SIGTERM", pid
when "stats"
puts "Stats not available via pid only"
return
2012-10-16 01:24:22 -04:00
when "phased-restart"
Process.kill "SIGUSR1", pid
else
2012-09-02 08:56:05 -04:00
message "Puma is started"
return
end
2012-10-16 01:24:22 -04:00
2012-09-02 08:56:05 -04:00
message "Command #{@options[:command]} sent success"
end
2012-09-02 08:56:05 -04:00
def run
start if @options[:command] == "start"
2012-10-16 01:37:28 -04:00
2012-09-02 08:56:05 -04:00
prepare_configuration
2012-09-02 08:56:05 -04:00
if is_windows?
send_request
else
2012-09-02 08:56:05 -04:00
@options.has_key?(:control_url) ? send_request : send_signal
end
2012-09-02 08:56:05 -04:00
rescue => e
message e.message
exit 1
end
private
def start
require 'puma/cli'
run_args = @argv
if path = @options[:state]
run_args = ["-S", path] + run_args
end
if path = @options[:config_file]
run_args = ["-C", path] + run_args
end
events = Puma::Events.new @stdout, @stderr
cli = Puma::CLI.new run_args, events
cli.run
end
end
end