Revert "service: support multiple sockets in DSL"

This commit is contained in:
Kevin 2023-09-28 09:58:03 -07:00 committed by GitHub
parent d73e1fc413
commit 653f333d73
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 48 additions and 148 deletions

View File

@ -187,26 +187,17 @@ module Homebrew
end end
end end
SOCKET_STRING_REGEX = %r{([a-z]+)://([a-z0-9.]+):([0-9]+)}i.freeze sig { params(value: T.nilable(String)).returns(T.nilable(T::Hash[Symbol, String])) }
sig {
params(value: T.nilable(T.any(String, T::Hash[Symbol, String])))
.returns(T.nilable(T::Hash[Symbol, T::Hash[Symbol, String]]))
}
def sockets(value = nil) def sockets(value = nil)
return @sockets if value.nil? case value
when nil
@sockets = case value @sockets
when String when String
{ listeners: value } match = T.must(value).match(%r{([a-z]+)://([a-z0-9.]+):([0-9]+)}i)
when Hash
value
end.transform_values do |socket_string|
match = socket_string.match(SOCKET_STRING_REGEX)
raise TypeError, "Service#sockets a formatted socket definition as <type>://<host>:<port>" if match.blank? raise TypeError, "Service#sockets a formatted socket definition as <type>://<host>:<port>" if match.blank?
type, host, port = match.captures type, host, port = match.captures
{ host: host, port: port, type: type } @sockets = { host: host, port: port, type: type }
end end
end end
@ -419,15 +410,13 @@ module Homebrew
if @sockets.present? if @sockets.present?
base[:Sockets] = {} base[:Sockets] = {}
@sockets.each do |name, info| base[:Sockets][:Listeners] = {
base[:Sockets][name] = { SockNodeName: @sockets[:host],
SockNodeName: info[:host], SockServiceName: @sockets[:port],
SockServiceName: info[:port], SockProtocol: @sockets[:type].upcase,
SockProtocol: info[:type].upcase,
SockFamily: "IPv4v6", SockFamily: "IPv4v6",
} }
end end
end
if @cron.present? && @run_type == RUN_TYPE_CRON if @cron.present? && @run_type == RUN_TYPE_CRON
base[:StartCalendarInterval] = @cron.reject { |_, value| value == "*" } base[:StartCalendarInterval] = @cron.reject { |_, value| value == "*" }
@ -522,11 +511,7 @@ module Homebrew
.join(" ") .join(" ")
end end
sockets_hash = if @sockets.present? sockets_string = "#{@sockets[:type]}://#{@sockets[:host]}:#{@sockets[:port]}" if @sockets.present?
@sockets.transform_values do |info|
"#{info[:type]}://#{info[:host]}:#{info[:port]}"
end
end
{ {
name: name_params.presence, name: name_params.presence,
@ -546,7 +531,7 @@ module Homebrew
restart_delay: @restart_delay, restart_delay: @restart_delay,
process_type: @process_type, process_type: @process_type,
macos_legacy_timers: @macos_legacy_timers, macos_legacy_timers: @macos_legacy_timers,
sockets: sockets_hash, sockets: sockets_string,
}.compact }.compact
end end
@ -580,6 +565,8 @@ module Homebrew
raise ArgumentError, "Unexpected run command: #{api_hash["run"]}" raise ArgumentError, "Unexpected run command: #{api_hash["run"]}"
end end
hash[:keep_alive] = api_hash["keep_alive"].transform_keys(&:to_sym) if api_hash.key?("keep_alive")
if api_hash.key?("environment_variables") if api_hash.key?("environment_variables")
hash[:environment_variables] = api_hash["environment_variables"].to_h do |key, value| hash[:environment_variables] = api_hash["environment_variables"].to_h do |key, value|
[key.to_sym, replace_placeholders(value)] [key.to_sym, replace_placeholders(value)]
@ -598,22 +585,12 @@ module Homebrew
hash[key.to_sym] = replace_placeholders(value) hash[key.to_sym] = replace_placeholders(value)
end end
%w[interval cron launch_only_once require_root restart_delay macos_legacy_timers].each do |key| %w[interval cron launch_only_once require_root restart_delay macos_legacy_timers sockets].each do |key|
next if (value = api_hash[key]).nil? next if (value = api_hash[key]).nil?
hash[key.to_sym] = value hash[key.to_sym] = value
end end
%w[sockets keep_alive].each do |key|
next unless (value = api_hash[key])
hash[key.to_sym] = if value.is_a?(Hash)
value.transform_keys(&:to_sym)
else
value
end
end
hash hash
end end

View File

@ -14,15 +14,6 @@ describe Homebrew::Service do
end end
end end
def stub_formula_with_service_sockets(sockets_var)
stub_formula do
service do
run opt_bin/"beanstalkd"
sockets sockets_var
end
end
end
describe "#std_service_path_env" do describe "#std_service_path_env" do
it "returns valid std_service_path_env" do it "returns valid std_service_path_env" do
f = stub_formula do f = stub_formula do
@ -111,33 +102,43 @@ describe Homebrew::Service do
end end
describe "#sockets" do describe "#sockets" do
let(:sockets_type_error_message) { "Service#sockets a formatted socket definition as <type>://<host>:<port>" }
it "throws for missing type" do it "throws for missing type" do
[ f = stub_formula do
stub_formula_with_service_sockets("127.0.0.1:80"), service do
stub_formula_with_service_sockets({ "Socket" => "127.0.0.1:80" }), run opt_bin/"beanstalkd"
].each do |f| sockets "127.0.0.1:80"
expect { f.service.manual_command }.to raise_error TypeError, sockets_type_error_message
end end
end end
expect do
f.service.manual_command
end.to raise_error TypeError, "Service#sockets a formatted socket definition as <type>://<host>:<port>"
end
it "throws for missing host" do it "throws for missing host" do
[ f = stub_formula do
stub_formula_with_service_sockets("tcp://:80"), service do
stub_formula_with_service_sockets({ "Socket" => "tcp://:80" }), run opt_bin/"beanstalkd"
].each do |f| sockets "tcp://:80"
expect { f.service.manual_command }.to raise_error TypeError, sockets_type_error_message
end end
end end
it "throws for missing port" do expect do
[ f.service.manual_command
stub_formula_with_service_sockets("tcp://127.0.0.1"), end.to raise_error TypeError, "Service#sockets a formatted socket definition as <type>://<host>:<port>"
stub_formula_with_service_sockets({ "Socket" => "tcp://127.0.0.1" }),
].each do |f|
expect { f.service.manual_command }.to raise_error TypeError, sockets_type_error_message
end end
it "throws for missing port" do
f = stub_formula do
service do
run opt_bin/"beanstalkd"
sockets "tcp://127.0.0.1"
end
end
expect do
f.service.manual_command
end.to raise_error TypeError, "Service#sockets a formatted socket definition as <type>://<host>:<port>"
end end
end end
@ -258,59 +259,10 @@ describe Homebrew::Service do
end end
it "returns valid plist with socket" do it "returns valid plist with socket" do
plist_expect = <<~EOS
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
\t<key>Label</key>
\t<string>homebrew.mxcl.formula_name</string>
\t<key>LimitLoadToSessionType</key>
\t<array>
\t\t<string>Aqua</string>
\t\t<string>Background</string>
\t\t<string>LoginWindow</string>
\t\t<string>StandardIO</string>
\t\t<string>System</string>
\t</array>
\t<key>ProgramArguments</key>
\t<array>
\t\t<string>#{HOMEBREW_PREFIX}/opt/formula_name/bin/beanstalkd</string>
\t</array>
\t<key>RunAtLoad</key>
\t<true/>
\t<key>Sockets</key>
\t<dict>
\t\t<key>listeners</key>
\t\t<dict>
\t\t\t<key>SockFamily</key>
\t\t\t<string>IPv4v6</string>
\t\t\t<key>SockNodeName</key>
\t\t\t<string>127.0.0.1</string>
\t\t\t<key>SockProtocol</key>
\t\t\t<string>TCP</string>
\t\t\t<key>SockServiceName</key>
\t\t\t<string>80</string>
\t\t</dict>
\t</dict>
</dict>
</plist>
EOS
[
stub_formula_with_service_sockets("tcp://127.0.0.1:80"),
stub_formula_with_service_sockets({ listeners: "tcp://127.0.0.1:80" }),
].each do |f|
plist = f.service.to_plist
expect(plist).to eq(plist_expect)
end
end
it "returns valid plist with multiple sockets" do
f = stub_formula do f = stub_formula do
service do service do
run [opt_bin/"beanstalkd", "test"] run [opt_bin/"beanstalkd", "test"]
sockets socket: "tcp://0.0.0.0:80", socket_tls: "tcp://0.0.0.0:443" sockets "tcp://127.0.0.1:80"
end end
end end
@ -339,28 +291,17 @@ describe Homebrew::Service do
\t<true/> \t<true/>
\t<key>Sockets</key> \t<key>Sockets</key>
\t<dict> \t<dict>
\t\t<key>socket</key> \t\t<key>Listeners</key>
\t\t<dict> \t\t<dict>
\t\t\t<key>SockFamily</key> \t\t\t<key>SockFamily</key>
\t\t\t<string>IPv4v6</string> \t\t\t<string>IPv4v6</string>
\t\t\t<key>SockNodeName</key> \t\t\t<key>SockNodeName</key>
\t\t\t<string>0.0.0.0</string> \t\t\t<string>127.0.0.1</string>
\t\t\t<key>SockProtocol</key> \t\t\t<key>SockProtocol</key>
\t\t\t<string>TCP</string> \t\t\t<string>TCP</string>
\t\t\t<key>SockServiceName</key> \t\t\t<key>SockServiceName</key>
\t\t\t<string>80</string> \t\t\t<string>80</string>
\t\t</dict> \t\t</dict>
\t\t<key>socket_tls</key>
\t\t<dict>
\t\t\t<key>SockFamily</key>
\t\t\t<string>IPv4v6</string>
\t\t\t<key>SockNodeName</key>
\t\t\t<string>0.0.0.0</string>
\t\t\t<key>SockProtocol</key>
\t\t\t<string>TCP</string>
\t\t\t<key>SockServiceName</key>
\t\t\t<string>443</string>
\t\t</dict>
\t</dict> \t</dict>
</dict> </dict>
</plist> </plist>
@ -1049,7 +990,7 @@ describe Homebrew::Service do
run_type: :immediate, run_type: :immediate,
working_dir: "/$HOME", working_dir: "/$HOME",
cron: "0 0 * * 0", cron: "0 0 * * 0",
sockets: { listeners: "tcp://0.0.0.0:80" }, sockets: "tcp://0.0.0.0:80",
} }
end end

View File

@ -1052,24 +1052,6 @@ The `sockets` method accepts a formatted socket definition as `<type>://<host>:<
Please note that sockets will be accessible on IPv4 and IPv6 addresses by default. Please note that sockets will be accessible on IPv4 and IPv6 addresses by default.
If you only need one socket and you don't care about the name (the default is `listeners`):
```rb
service do
run [opt_bin/"beanstalkd", "test"]
sockets "tcp://127.0.0.1:80"
end
```
If you need multiple sockets and/or you want to specify the name:
```rb
service do
run [opt_bin/"beanstalkd", "test"]
sockets http: "tcp://0.0.0.0:80", https: "tcp://0.0.0.0:443"
end
```
### Using environment variables ### Using environment variables
Homebrew has multiple levels of environment variable filtering which affects which variables are available to formulae. Homebrew has multiple levels of environment variable filtering which affects which variables are available to formulae.