Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

service: support multiple sockets in DSL #16026

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
57 changes: 40 additions & 17 deletions Library/Homebrew/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -187,17 +187,26 @@ def run_at_load(value = nil)
end
end

sig { params(value: T.nilable(String)).returns(T.nilable(T::Hash[Symbol, String])) }
SOCKET_STRING_REGEX = %r{([a-z]+)://([a-z0-9.]+):([0-9]+)}i.freeze

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)
case value
when nil
@sockets
return @sockets if value.nil?

@sockets = case value
when String
match = T.must(value).match(%r{([a-z]+)://([a-z0-9.]+):([0-9]+)}i)
{ listeners: value }
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?

type, host, port = match.captures
@sockets = { host: host, port: port, type: type }
{ host: host, port: port, type: type }
end
end

Expand Down Expand Up @@ -410,12 +419,14 @@ def to_plist

if @sockets.present?
base[:Sockets] = {}
base[:Sockets][:Listeners] = {
SockNodeName: @sockets[:host],
SockServiceName: @sockets[:port],
SockProtocol: @sockets[:type].upcase,
SockFamily: "IPv4v6",
}
@sockets.each do |name, info|
base[:Sockets][name] = {
SockNodeName: info[:host],
SockServiceName: info[:port],
SockProtocol: info[:type].upcase,
SockFamily: "IPv4v6",
}
end
end

if @cron.present? && @run_type == RUN_TYPE_CRON
Expand Down Expand Up @@ -511,7 +522,11 @@ def serialize
.join(" ")
end

sockets_string = "#{@sockets[:type]}://#{@sockets[:host]}:#{@sockets[:port]}" if @sockets.present?
sockets_hash = if @sockets.present?
@sockets.transform_values do |info|
"#{info[:type]}://#{info[:host]}:#{info[:port]}"
end
end

{
name: name_params.presence,
Expand All @@ -531,7 +546,7 @@ def serialize
restart_delay: @restart_delay,
process_type: @process_type,
macos_legacy_timers: @macos_legacy_timers,
sockets: sockets_string,
sockets: sockets_hash,
}.compact
end

Expand Down Expand Up @@ -565,8 +580,6 @@ def self.deserialize(api_hash)
raise ArgumentError, "Unexpected run command: #{api_hash["run"]}"
end

hash[:keep_alive] = api_hash["keep_alive"].transform_keys(&:to_sym) if api_hash.key?("keep_alive")

if api_hash.key?("environment_variables")
hash[:environment_variables] = api_hash["environment_variables"].to_h do |key, value|
[key.to_sym, replace_placeholders(value)]
Expand All @@ -585,12 +598,22 @@ def self.deserialize(api_hash)
hash[key.to_sym] = replace_placeholders(value)
end

%w[interval cron launch_only_once require_root restart_delay macos_legacy_timers sockets].each do |key|
%w[interval cron launch_only_once require_root restart_delay macos_legacy_timers].each do |key|
next if (value = api_hash[key]).nil?

hash[key.to_sym] = value
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
end

Expand Down
121 changes: 90 additions & 31 deletions Library/Homebrew/test/service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ def stub_formula(&block)
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
it "returns valid std_service_path_env" do
f = stub_formula do
Expand Down Expand Up @@ -102,43 +111,33 @@ def stub_formula(&block)
end

describe "#sockets" do
let(:sockets_type_error_message) { "Service#sockets a formatted socket definition as <type>://<host>:<port>" }

it "throws for missing type" do
f = stub_formula do
service do
run opt_bin/"beanstalkd"
sockets "127.0.0.1:80"
end
[
stub_formula_with_service_sockets("127.0.0.1:80"),
stub_formula_with_service_sockets({ "Socket" => "127.0.0.1:80" }),
].each do |f|
expect { f.service.manual_command }.to raise_error TypeError, sockets_type_error_message
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
f = stub_formula do
service do
run opt_bin/"beanstalkd"
sockets "tcp://:80"
end
[
stub_formula_with_service_sockets("tcp://:80"),
stub_formula_with_service_sockets({ "Socket" => "tcp://:80" }),
].each do |f|
expect { f.service.manual_command }.to raise_error TypeError, sockets_type_error_message
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 port" do
f = stub_formula do
service do
run opt_bin/"beanstalkd"
sockets "tcp://127.0.0.1"
end
[
stub_formula_with_service_sockets("tcp://127.0.0.1"),
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

expect do
f.service.manual_command
end.to raise_error TypeError, "Service#sockets a formatted socket definition as <type>://<host>:<port>"
end
end

Expand Down Expand Up @@ -259,10 +258,59 @@ def stub_formula(&block)
end

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
service do
run [opt_bin/"beanstalkd", "test"]
sockets "tcp://127.0.0.1:80"
sockets socket: "tcp://0.0.0.0:80", socket_tls: "tcp://0.0.0.0:443"
end
end

Expand Down Expand Up @@ -291,17 +339,28 @@ def stub_formula(&block)
\t<true/>
\t<key>Sockets</key>
\t<dict>
\t\t<key>Listeners</key>
\t\t<key>socket</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<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>80</string>
\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>
</dict>
</plist>
Expand Down Expand Up @@ -990,7 +1049,7 @@ def stub_formula(&block)
run_type: :immediate,
working_dir: "/$HOME",
cron: "0 0 * * 0",
sockets: "tcp://0.0.0.0:80",
sockets: { listeners: "tcp://0.0.0.0:80" },
}
end

Expand Down
18 changes: 18 additions & 0 deletions docs/Formula-Cookbook.md
Original file line number Diff line number Diff line change
Expand Up @@ -1052,6 +1052,24 @@ 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.

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

Homebrew has multiple levels of environment variable filtering which affects which variables are available to formulae.
Expand Down