From 83163c3f284eabe7ba9912f0968026e2533a4489 Mon Sep 17 00:00:00 2001 From: Igor Bochkariov Date: Fri, 19 Jul 2013 10:30:15 +0400 Subject: [PATCH] Allow configuring pumactl with config.rb example: echo "pidfile '/tmp/app.pid'; rackup 'config.ru'" > /etc/puma/app.rb pumactl -F /etc/puma/app.rb start # starts an app and store pid in /tmp/app.pid pumactl -F /etc/puma/app.rb stop # stops the started earlier app Integration test t2 included: rake test:integration Also fixed discrepancy in naming options :pidfile/:pid_file and :state/:status_file --- lib/puma/control_cli.rb | 31 ++++++++++++++++++++----------- test/shell/run.sh | 14 ++++++++++++-- test/shell/t1.rb | 4 ++-- test/shell/t2.rb | 17 +++++++++++++++++ test/shell/t2_conf.rb | 5 +++++ 5 files changed, 56 insertions(+), 15 deletions(-) create mode 100644 test/shell/t2.rb create mode 100644 test/shell/t2_conf.rb diff --git a/lib/puma/control_cli.rb b/lib/puma/control_cli.rb index 530107c1..5a7e5bce 100644 --- a/lib/puma/control_cli.rb +++ b/lib/puma/control_cli.rb @@ -20,10 +20,10 @@ module Puma @options = {} opts = OptionParser.new do |o| - o.banner = "Usage: pumactl (-p PID | -P pidfile | -S status_file | -C url -T token) (#{COMMANDS.join("|")})" + o.banner = "Usage: pumactl (-p PID | -P pidfile | -S status_file | -C url -T token | -F config.rb) (#{COMMANDS.join("|")})" o.on "-S", "--state PATH", "Where the state file to use is" do |arg| - @options[:status_path] = arg + @options[:state] = arg end o.on "-Q", "--quiet", "Not display messages" do |arg| @@ -31,7 +31,7 @@ module Puma end o.on "-P", "--pidfile PATH", "Pid file" do |arg| - @options[:pid_file] = arg + @options[:pidfile] = arg end o.on "-p", "--pid PID", "Pid" do |arg| @@ -46,6 +46,10 @@ module Puma @options[:control_auth_token] = arg end + o.on "-F", "--config-file PATH", "Puma config script" do |arg| + @options[:config_file] = arg + end + o.on_tail("-H", "--help", "Show this message") do @stdout.puts o exit @@ -62,6 +66,8 @@ module Puma command = argv.shift @options[:command] = command if command + Puma::Configuration.new(@options).load if @options[:config_file] + # check present of command unless @options[:command] raise "Available commands: #{COMMANDS.join(", ")}" @@ -81,12 +87,12 @@ module Puma end def prepare_configuration - if @options.has_key? :status_path - unless File.exist? @options[:status_path] - raise "Status file not found: #{@options[:status_path]}" + if @options.has_key? :state + unless File.exist? @options[:state] + raise "Status file not found: #{@options[:state]}" end - status = YAML.load File.read(@options[:status_path]) + status = YAML.load File.read(@options[:state]) if status.kind_of?(Hash) && status.has_key?("config") @@ -105,12 +111,12 @@ module Puma # get pid @options[:pid] = status["pid"].to_i else - raise "Invalid status file: #{@options[:status_path]}" + raise "Invalid status file: #{@options[:state]}" end - elsif @options.has_key? :pid_file + elsif @options.has_key? :pidfile # get pid from pid_file - @options[:pid] = File.open(@options[:pid_file]).gets.to_i + @options[:pid] = File.open(@options[:pidfile]).gets.to_i end end @@ -206,9 +212,12 @@ module Puma require 'puma/cli' run_args = @argv - if path = @options[:status_path] + 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 diff --git a/test/shell/run.sh b/test/shell/run.sh index 392943c4..120b1f8d 100644 --- a/test/shell/run.sh +++ b/test/shell/run.sh @@ -1,7 +1,17 @@ +ERROR=0 + if ruby -rubygems t1.rb > /dev/null 2>&1; then echo "t1 OK" - exit 0 else echo "t1 FAIL" - exit 1 + ERROR=1 fi + +if ruby -rubygems t2.rb > /dev/null 2>&1; then + echo "t2 OK" +else + echo "t2 FAIL" + ERROR=2 +fi + +exit $ERROR diff --git a/test/shell/t1.rb b/test/shell/t1.rb index 4ac983af..e0a529d7 100644 --- a/test/shell/t1.rb +++ b/test/shell/t1.rb @@ -8,8 +8,8 @@ sleep 1 log = File.read("t1-stdout") -File.unlink "t1-stdout" -File.unlink "t1-pid" +File.unlink "t1-stdout" if File.file? "t1-stdout" +File.unlink "t1-pid" if File.file? "t1-pid" if log =~ %r!GET / HTTP/1\.1! exit 0 diff --git a/test/shell/t2.rb b/test/shell/t2.rb new file mode 100644 index 00000000..83ed511c --- /dev/null +++ b/test/shell/t2.rb @@ -0,0 +1,17 @@ +system "ruby -rubygems -I../../lib ../../bin/pumactl -F t2_conf.rb start" +sleep 5 +system "curl http://localhost:10103/" + +system "ruby -rubygems -I../../lib ../../bin/pumactl -F t2_conf.rb stop" + +sleep 1 + +log = File.read("t2-stdout") + +File.unlink "t2-stdout" if File.file? "t2-stdout" + +if log =~ %r(GET / HTTP/1\.1) && !File.file?("t2-pid") + exit 0 +else + exit 1 +end diff --git a/test/shell/t2_conf.rb b/test/shell/t2_conf.rb new file mode 100644 index 00000000..dab60320 --- /dev/null +++ b/test/shell/t2_conf.rb @@ -0,0 +1,5 @@ +stdout_redirect "t2-stdout" +pidfile "t2-pid" +bind "tcp://0.0.0.0:10103" +rackup File.expand_path('../hello.ru', File.dirname(__FILE__)) +daemonize