2011-12-05 18:58:23 -05:00
|
|
|
require 'optparse'
|
|
|
|
|
|
|
|
require 'puma/const'
|
2011-12-07 16:42:53 -05:00
|
|
|
require 'puma/configuration'
|
2011-12-06 17:56:38 -05:00
|
|
|
|
2011-12-05 18:58:23 -05:00
|
|
|
require 'yaml'
|
|
|
|
require 'uri'
|
|
|
|
|
|
|
|
require 'socket'
|
|
|
|
|
|
|
|
module Puma
|
|
|
|
class ControlCLI
|
|
|
|
|
2012-09-25 12:40:00 -04:00
|
|
|
def initialize(argv, stdout=STDOUT, stderr=STDERR)
|
2011-12-05 18:58:23 -05:00
|
|
|
@argv = argv
|
2011-12-07 17:48:41 -05:00
|
|
|
@stdout = stdout
|
2012-09-25 12:40:00 -04:00
|
|
|
@stderr = stderr
|
|
|
|
@path = nil
|
|
|
|
@config = nil
|
2011-12-05 18:58:23 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def setup_options
|
|
|
|
@parser = OptionParser.new do |o|
|
|
|
|
o.on "-S", "--state PATH", "Where the state file to use is" do |arg|
|
|
|
|
@path = arg
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def connect
|
2011-12-06 17:56:38 -05:00
|
|
|
if str = @config.options[:control_url]
|
2011-12-05 18:58:23 -05:00
|
|
|
uri = URI.parse str
|
|
|
|
case uri.scheme
|
|
|
|
when "tcp"
|
|
|
|
return TCPSocket.new uri.host, uri.port
|
|
|
|
when "unix"
|
|
|
|
path = "#{uri.host}#{uri.path}"
|
|
|
|
return UNIXSocket.new path
|
|
|
|
else
|
|
|
|
raise "Invalid URI: #{str}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
raise "No status address configured"
|
|
|
|
end
|
|
|
|
|
|
|
|
def run
|
|
|
|
setup_options
|
|
|
|
|
2012-09-25 12:40:00 -04:00
|
|
|
@parser.order!(@argv) { |a| @parser.terminate a }
|
2011-12-05 18:58:23 -05:00
|
|
|
|
2012-09-25 12:40:00 -04:00
|
|
|
if @path
|
|
|
|
@state = YAML.load File.read(@path)
|
|
|
|
@config = @state['config']
|
|
|
|
end
|
2011-12-05 18:58:23 -05:00
|
|
|
|
|
|
|
cmd = @argv.shift
|
|
|
|
|
|
|
|
meth = "command_#{cmd}"
|
|
|
|
|
|
|
|
if respond_to?(meth)
|
|
|
|
__send__(meth)
|
|
|
|
else
|
|
|
|
raise "Unknown command: #{cmd}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-12-07 13:46:36 -05:00
|
|
|
def request(sock, url)
|
|
|
|
token = @config.options[:control_auth_token]
|
|
|
|
if token
|
|
|
|
url = "#{url}?token=#{token}"
|
|
|
|
end
|
|
|
|
|
|
|
|
sock << "GET #{url} HTTP/1.0\r\n\r\n"
|
|
|
|
|
|
|
|
rep = sock.read.split("\r\n")
|
|
|
|
|
|
|
|
m = %r!HTTP/1.\d (\d+)!.match(rep.first)
|
|
|
|
if m[1] == "403"
|
|
|
|
raise "Unauthorized access to server (wrong auth token)"
|
|
|
|
elsif m[1] != "200"
|
|
|
|
raise "Bad response code from server: #{m[1]}"
|
|
|
|
end
|
|
|
|
|
|
|
|
return rep.last
|
|
|
|
end
|
|
|
|
|
2011-12-05 18:58:23 -05:00
|
|
|
def command_pid
|
2011-12-07 17:48:41 -05:00
|
|
|
@stdout.puts "#{@state['pid']}"
|
2011-12-05 18:58:23 -05:00
|
|
|
end
|
|
|
|
|
2012-09-25 12:40:00 -04:00
|
|
|
def command_start
|
|
|
|
require 'puma/cli'
|
|
|
|
|
|
|
|
cli = Puma::CLI.new @argv, @stdout, @stderr
|
|
|
|
cli.run
|
|
|
|
end
|
|
|
|
|
2011-12-05 18:58:23 -05:00
|
|
|
def command_stop
|
|
|
|
sock = connect
|
2011-12-07 13:46:36 -05:00
|
|
|
body = request sock, "/stop"
|
2011-12-05 18:58:23 -05:00
|
|
|
|
|
|
|
if body != '{ "status": "ok" }'
|
|
|
|
raise "Invalid response: '#{body}'"
|
|
|
|
else
|
2011-12-07 17:48:41 -05:00
|
|
|
@stdout.puts "Requested stop from server"
|
2011-12-05 18:58:23 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def command_halt
|
|
|
|
sock = connect
|
2011-12-07 13:46:36 -05:00
|
|
|
body = request sock, "/halt"
|
2011-12-05 18:58:23 -05:00
|
|
|
|
|
|
|
if body != '{ "status": "ok" }'
|
|
|
|
raise "Invalid response: '#{body}'"
|
|
|
|
else
|
2011-12-07 17:48:41 -05:00
|
|
|
@stdout.puts "Requested halt from server"
|
2011-12-05 18:58:23 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def command_restart
|
|
|
|
sock = connect
|
2011-12-07 13:46:36 -05:00
|
|
|
body = request sock, "/restart"
|
2011-12-05 18:58:23 -05:00
|
|
|
|
|
|
|
if body != '{ "status": "ok" }'
|
|
|
|
raise "Invalid response: '#{body}'"
|
|
|
|
else
|
2011-12-07 17:48:41 -05:00
|
|
|
@stdout.puts "Requested restart from server"
|
2011-12-05 18:58:23 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def command_stats
|
|
|
|
sock = connect
|
2011-12-07 13:46:36 -05:00
|
|
|
body = request sock, "/stats"
|
2011-12-05 18:58:23 -05:00
|
|
|
|
2011-12-07 17:48:41 -05:00
|
|
|
@stdout.puts body
|
2011-12-05 18:58:23 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|