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

Style cleanup

This commit is contained in:
Evan Phoenix 2012-10-15 22:24:22 -07:00
parent e2c0f0ce15
commit aea32a9ca1

View file

@ -7,7 +7,7 @@ require 'socket'
module Puma module Puma
class ControlCLI class ControlCLI
COMMANDS = %w{status restart stop halt} COMMANDS = %w{status stats restart stop halt}
def is_windows? def is_windows?
RUBY_PLATFORM =~ /(win|w)32$/ ? true : false RUBY_PLATFORM =~ /(win|w)32$/ ? true : false
@ -18,32 +18,41 @@ module Puma
@stderr = stderr @stderr = stderr
@options = {} @options = {}
OptionParser.new do |option| opts = OptionParser.new do |o|
option.banner = "Usage: pumactl (-S status_file | -C url -T token) (#{COMMANDS.join("|")})" o.banner = "Usage: pumactl (-S status_file | -C url -T token) (#{COMMANDS.join("|")})"
option.on "-S", "--state PATH", "Where the state file to use is" do |arg|
o.on "-S", "--state PATH", "Where the state file to use is" do |arg|
@options[:status_path] = arg @options[:status_path] = arg
end end
option.on "-Q", "--quiet", "Not display messages" do |arg|
o.on "-Q", "--quiet", "Not display messages" do |arg|
@options[:quiet_flag] = true @options[:quiet_flag] = true
end end
option.on "-P", "--pidfile PATH", "Pid file" do |arg|
o.on "-P", "--pidfile PATH", "Pid file" do |arg|
@options[:pid_file] = arg @options[:pid_file] = arg
end end
option.on "-C", "--control-url URL", "The bind url to use for the control server" do |arg|
o.on "-C", "--control-url URL", "The bind url to use for the control server" do |arg|
@options[:control_url] = arg @options[:control_url] = arg
end end
option.on "-T", "--control-token TOKEN", "The token to use as authentication for the control server" do |arg|
o.on "-T", "--control-token TOKEN", "The token to use as authentication for the control server" do |arg|
@options[:control_auth_token] = arg @options[:control_auth_token] = arg
end end
option.on_tail("-H", "--help", "Show this message") do
o.on_tail("-H", "--help", "Show this message") do
@stdout.puts option @stdout.puts option
exit exit
end end
option.on_tail("-V", "--version", "Show version") do
o.on_tail("-V", "--version", "Show version") do
puts Const::PUMA_VERSION puts Const::PUMA_VERSION
exit exit
end end
end.parse!(argv) end
opts.parse!(argv)
command = argv.shift command = argv.shift
@options[:command] = command if command @options[:command] = command if command
@ -52,6 +61,7 @@ module Puma
unless @options[:command] unless @options[:command]
raise "Available commands: #{COMMANDS.join(", ")}" raise "Available commands: #{COMMANDS.join(", ")}"
end end
unless COMMANDS.include? @options[:command] unless COMMANDS.include? @options[:command]
raise "Invalid command: #{@options[:command]}" raise "Invalid command: #{@options[:command]}"
end end
@ -61,33 +71,42 @@ module Puma
exit 1 exit 1
end end
def message msg def message(msg)
@stdout.puts msg unless @options[:quiet_flag] @stdout.puts msg unless @options[:quiet_flag]
end end
def prepare_configuration def prepare_configuration
if @options.has_key? :status_path if @options.has_key? :status_path
raise "Status file not found: #{@options[:status_path]} " unless File.exist? @options[:status_path] unless File.exist? @options[:status_path]
raise "Status file not found: #{@options[:status_path]}"
end
status = YAML.load File.read(@options[:status_path]) status = YAML.load File.read(@options[:status_path])
if status.has_key? "config" if status.has_key? "config"
conf = status["config"]
# get control_url # get control_url
if status["config"].options.has_key?(:control_url) if url = conf.options[:control_url]
@options[:control_url] = status["config"].options[:control_url] @options[:control_url] = url
end end
# get control_auth_token # get control_auth_token
if status["config"].options.has_key?(:control_auth_token) if token = conf.options[:control_auth_token]
@options[:control_auth_token] = status["config"].options[:control_auth_token] @options[:control_auth_token] = token
end end
# get pid # get pid
@options[:pid] = status["pid"].to_i @options[:pid] = status["pid"].to_i
else else
raise "Invalid status file: #{@options[:status_path]}" raise "Invalid status file: #{@options[:status_path]}"
end end
elsif @options.has_key? :pid_file elsif @options.has_key? :pid_file
# get pid from pid_file # get pid from pid_file
@options[:pid] = File.open(@options[:pid_file]).gets.to_i @options[:pid] = File.open(@options[:pid_file]).gets.to_i
end end
end end
def send_request def send_request
@ -103,22 +122,28 @@ module Puma
raise "Invalid scheme: #{uri.scheme}" raise "Invalid scheme: #{uri.scheme}"
end end
unless @options[:command] == "status" if @options[:command] == "status"
message "Puma is started"
else
url = "/#{@options[:command]}" url = "/#{@options[:command]}"
if @options.has_key?(:control_auth_token) if @options.has_key?(:control_auth_token)
url = url + "?token=#{@options[:control_auth_token]}" url = url + "?token=#{@options[:control_auth_token]}"
end end
@server << "GET #{url} HTTP/1.0\r\n\r\n" @server << "GET #{url} HTTP/1.0\r\n\r\n"
response = @server.read.split("\r\n") response = @server.read.split("\r\n")
(@http,@code,@message) = response.first.split(" ") (@http,@code,@message) = response.first.split(" ")
if @code == "403" if @code == "403"
raise "Unauthorized access to server (wrong auth token)" raise "Unauthorized access to server (wrong auth token)"
elsif @code != "200" elsif @code != "200"
raise "Bad response from server: #{@code}" raise "Bad response from server: #{@code}"
end end
message "Command #{@options[:command]} sent success" message "Command #{@options[:command]} sent success"
else
message "Puma is started"
end end
@server.close @server.close
@ -126,17 +151,22 @@ module Puma
def send_signal def send_signal
Process.getpgid(@options[:pid]) Process.getpgid(@options[:pid])
case @options[:command] case @options[:command]
when "restart" when "restart"
Process.kill("SIGUSR2", @options[:pid]) Process.kill("SIGUSR2", @options[:pid])
when "halt" when "halt"
Process.kill("QUIT", @options[:pid]) Process.kill("QUIT", @options[:pid])
when "stop" when "stop"
Process.kill("SIGTERM", @options[:pid]) Process.kill("SIGTERM", @options[:pid])
else else
message "Puma is started" message "Puma is started"
return return
end end
message "Command #{@options[:command]} sent success" message "Command #{@options[:command]} sent success"
end end