service: support multiple sockets in DSL

This adds support for multiple named sockets to the service DSL.
It also retains backwards compatibility with the previous DSL
where you can declare one socket and it is always just named
Listener by default.
This commit is contained in:
apainintheneck 2023-09-23 14:40:24 -07:00
parent afbea15d5c
commit ae5e9387b9
2 changed files with 30 additions and 15 deletions

View File

@ -187,17 +187,26 @@ module Homebrew
end end
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[String, String])))
.returns(T.nilable(T::Hash[String, T::Hash[Symbol, String]]))
}
def sockets(value = nil) def sockets(value = nil)
case value return @sockets if value.nil?
when nil
@sockets @sockets = case value
when String 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? 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
@sockets = { host: host, port: port, type: type } { host: host, port: port, type: type }
end end
end end
@ -410,12 +419,14 @@ module Homebrew
if @sockets.present? if @sockets.present?
base[:Sockets] = {} base[:Sockets] = {}
base[:Sockets][:Listeners] = { @sockets.each do |name, info|
SockNodeName: @sockets[:host], base[:Sockets][name] = {
SockServiceName: @sockets[:port], SockNodeName: info[:host],
SockProtocol: @sockets[:type].upcase, SockServiceName: info[:port],
SockFamily: "IPv4v6", SockProtocol: info[:type].upcase,
} SockFamily: "IPv4v6",
}
end
end end
if @cron.present? && @run_type == RUN_TYPE_CRON if @cron.present? && @run_type == RUN_TYPE_CRON
@ -511,7 +522,11 @@ module Homebrew
.join(" ") .join(" ")
end 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, name: name_params.presence,
@ -531,7 +546,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_string, sockets: sockets_hash,
}.compact }.compact
end end

View File

@ -990,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: "tcp://0.0.0.0:80", sockets: { "Listeners" => "tcp://0.0.0.0:80" },
} }
end end