From 22475b3b0af7091f5c579843f3af1192a355f8cc Mon Sep 17 00:00:00 2001 From: Laurent Arnoud Date: Mon, 5 Aug 2019 20:09:29 +0200 Subject: [PATCH] Check puma config environment file on control_cli Fix https://github.com/puma/puma/issues/1445 --- lib/puma/control_cli.rb | 13 +++++++++++-- test/test_pumactl.rb | 27 +++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/lib/puma/control_cli.rb b/lib/puma/control_cli.rb index ef405eb810..0f5feea528 100644 --- a/lib/puma/control_cli.rb +++ b/lib/puma/control_cli.rb @@ -22,6 +22,7 @@ def initialize(argv, stdout=STDOUT, stderr=STDERR) @control_auth_token = nil @config_file = nil @command = nil + @environment = ENV['RACK_ENV'] || "development" @argv = argv.dup @stdout = stdout @@ -59,6 +60,11 @@ def initialize(argv, stdout=STDOUT, stderr=STDERR) @config_file = arg end + o.on "-e", "--environment ENVIRONMENT", + "The environment to run the Rack app on (default development)" do |arg| + @environment = arg + end + o.on_tail("-H", "--help", "Show this message") do @stdout.puts o exit @@ -76,8 +82,10 @@ def initialize(argv, stdout=STDOUT, stderr=STDERR) @command = argv.shift unless @config_file == '-' - if @config_file.nil? and File.exist?('config/puma.rb') - @config_file = 'config/puma.rb' + if @config_file.nil? + @config_file = %W(config/puma/#{@environment}.rb config/puma.rb).find do |f| + File.exist?(f) + end end if @config_file @@ -258,6 +266,7 @@ def start run_args += ["--control-url", @control_url] if @control_url run_args += ["--control-token", @control_auth_token] if @control_auth_token run_args += ["-C", @config_file] if @config_file + run_args += ["-e", @environment] if @environment events = Puma::Events.new @stdout, @stderr diff --git a/test/test_pumactl.rb b/test/test_pumactl.rb index db3c38e64e..400a8803f5 100644 --- a/test/test_pumactl.rb +++ b/test/test_pumactl.rb @@ -29,6 +29,33 @@ def test_config_file assert_equal "t3-pid", control_cli.instance_variable_get("@pidfile") end + def test_environment + control_cli = Puma::ControlCLI.new ["-e", "development", "halt"] + assert_equal "development", control_cli.instance_variable_get("@environment") + end + + def test_config_file_exist + port = 6001 + Dir.mktmpdir do |d| + Dir.chdir(d) do + FileUtils.mkdir("config") + File.open("config/puma.rb", "w") { |f| f << "port #{port}" } + control_cli = Puma::ControlCLI.new ["halt"] + assert_equal "config/puma.rb", + control_cli.instance_variable_get("@config_file") + end + end + Dir.mktmpdir do |d| + Dir.chdir(d) do + FileUtils.mkdir_p("config/puma") + File.open("config/puma/development.rb", "w") { |f| f << "port #{port}" } + control_cli = Puma::ControlCLI.new ["halt"] + assert_equal "config/puma/development.rb", + control_cli.instance_variable_get("@config_file") + end + end + end + def test_control_no_token opts = [ "--config-file", "test/config/control_no_token.rb",