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

Remove yaml (psych) requirement in StateFile (#2784)

Don't require yaml in state_file.rb
cli.rb & launcher.rb - deprecate state file constants
This commit is contained in:
MSP-Greg 2022-01-01 15:52:48 -06:00 committed by GitHub
parent 06e88c1d85
commit 2b6968f9d4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 61 additions and 13 deletions

View file

@ -21,6 +21,7 @@ module Puma
# Handles invoke a Puma::Server in a command line style.
#
class CLI
# @deprecated 6.0.0
KEYS_NOT_TO_PERSIST_IN_STATE = Launcher::KEYS_NOT_TO_PERSIST_IN_STATE
# Create a new CLI object using +argv+ as the command line

View file

@ -15,6 +15,7 @@ module Puma
# It is responsible for either launching a cluster of Puma workers or a single
# puma server.
class Launcher
# @deprecated 6.0.0
KEYS_NOT_TO_PERSIST_IN_STATE = [
:logger, :lowlevel_error_handler,
:before_worker_shutdown, :before_worker_boot, :before_worker_fork,

View file

@ -1,15 +1,40 @@
# frozen_string_literal: true
require 'yaml'
module Puma
# Puma::Launcher uses StateFile to write a yaml file for use with Puma::ControlCLI.
#
# In previous versions of Puma, YAML was used to read/write the state file.
# Since Puma is similar to Bundler/RubyGems in that it may load before one's app
# does, minimizing the dependencies that may be shared with the app is desired.
#
# At present, it only works with numeric and string values. It is still a valid
# yaml file, and the CI tests parse it with Psych.
#
class StateFile
ALLOWED_FIELDS = %w!control_url control_auth_token pid running_from!
# @deprecated 6.0.0
FIELDS = ALLOWED_FIELDS
def initialize
@options = {}
end
def save(path, permission = nil)
contents =YAML.dump @options
contents = "---\n".dup
@options.each do |k,v|
next unless ALLOWED_FIELDS.include? k
case v
when Numeric
contents << "#{k}: #{v}\n"
when String
next if v.strip.empty?
contents << (k == 'running_from' || v.to_s.include?(' ') ?
"#{k}: \"#{v}\"\n" : "#{k}: #{v}\n")
end
end
if permission
File.write path, contents, mode: 'wb:UTF-8'
else
@ -18,12 +43,21 @@ module Puma
end
def load(path)
@options = YAML.load File.read(path)
File.read(path).lines.each do |line|
next if line.start_with? '#'
k,v = line.split ':', 2
next unless v && ALLOWED_FIELDS.include?(k)
v = v.strip
@options[k] =
case v
when /\A\d+\z/ then v.to_i
when /\A\d+\.\d+\z/ then v.to_f
else v.gsub(/\A"|"\z/, '')
end
end
end
FIELDS = %w!control_url control_auth_token pid running_from!
FIELDS.each do |f|
ALLOWED_FIELDS.each do |f|
define_method f do
@options[f]
end

View file

@ -4,6 +4,7 @@ require_relative "helpers/tmp_path"
require "puma/cli"
require "json"
require "psych"
class TestCLI < Minitest::Test
include SSLHelper if ::Puma::HAS_SSL
@ -347,9 +348,21 @@ class TestCLI < Minitest::Test
cli = Puma::CLI.new ["--state", @tmp_path, "--control-url", "auto"]
cli.launcher.write_state
data = YAML.load File.read(@tmp_path)
opts = cli.launcher.instance_variable_get(:@options)
assert_equal Process.pid, data["pid"]
data = Psych.load_file @tmp_path
Puma::StateFile::ALLOWED_FIELDS.each do |key|
val =
case key
when 'pid' then Process.pid
when 'running_from' then File.expand_path('.') # same as Launcher
else opts[key.to_sym]
end
assert_equal val, data[key]
end
assert_equal (Puma::StateFile::ALLOWED_FIELDS & data.keys).sort, data.keys.sort
url = data["control_url"]
@ -364,10 +377,9 @@ class TestCLI < Minitest::Test
"--state", @tmp_path ]
cli.launcher.write_state
data = YAML.load_file(@tmp_path)
data = Psych.load_file @tmp_path
keys_not_stripped = data.keys & Puma::CLI::KEYS_NOT_TO_PERSIST_IN_STATE
assert_empty keys_not_stripped
assert_equal (Puma::StateFile::ALLOWED_FIELDS & data.keys).sort, data.keys.sort
end
def test_log_formatter_default_single
@ -401,7 +413,7 @@ class TestCLI < Minitest::Test
cli = Puma::CLI.new ["--state", @tmp_path, "--control-url", url]
cli.launcher.write_state
data = YAML.load File.read(@tmp_path)
data = Psych.load_file @tmp_path
assert_equal Process.pid, data["pid"]
assert_equal url, data["control_url"]