Merge pull request #14113 from SMillerDev/feature/service/require_root

Service: add method to define a root requirement
This commit is contained in:
Sean Molenaar 2022-11-08 11:56:31 +01:00 committed by GitHub
commit 133c639ba9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 61 additions and 2 deletions

View File

@ -995,6 +995,8 @@ class Formula
# </plist>
# EOS
# end</pre>
#
# @deprecated Please use {#service} instead
def plist
nil
end
@ -1014,6 +1016,14 @@ class Formula
# The generated launchd {.plist} file path.
sig { returns(Pathname) }
def plist_path
# TODO: Add deprecation
# odeprecated "formula.plist_path", "formula.launchd_service_path"
launchd_service_path
end
# The generated systemd {.service} file path.
sig { returns(Pathname) }
def launchd_service_path
opt_prefix/"#{plist_name}.plist"
end
@ -3121,7 +3131,11 @@ class Formula
#
# Or perhaps you'd like to give the user a choice? Ooh fancy.
# <pre>plist_options startup: true, manual: "foo start"</pre>
#
# @deprecated Please use {#service.require_root} instead
def plist_options(options)
# TODO: Deprecate
# odeprecated "plist_options", "service.require_root"
@plist_startup = options[:startup]
@plist_manual = options[:manual]
end
@ -3259,7 +3273,7 @@ class Formula
# Service can be used to define services.
# This method evaluates the DSL specified in the service block of the
# {Formula} (if it exists) and sets the instance variables of a Service
# object accordingly. This is used by `brew install` to generate a plist.
# object accordingly. This is used by `brew install` to generate a service file.
#
# <pre>service do
# run [opt_bin/"foo"]

View File

@ -129,6 +129,26 @@ module Homebrew
end
end
sig { params(value: T.nilable(T::Boolean)).returns(T.nilable(T::Boolean)) }
def require_root(value = nil)
case T.unsafe(value)
when nil
@require_root
when true, false
@require_root = value
else
raise TypeError, "Service#require_root expects a Boolean"
end
end
# Returns a `Boolean` describing if a service requires root access.
# @return [Boolean]
sig { returns(T::Boolean) }
def requires_root?
instance_eval(&@service_block)
@require_root.present? && @require_root == true
end
sig { params(value: T.nilable(String)).returns(T.nilable(T::Hash[Symbol, String])) }
def sockets(value = nil)
case T.unsafe(value)

View File

@ -733,7 +733,7 @@ describe Formula do
expect(f.plist_name).to eq("homebrew.mxcl.formula_name")
expect(f.service_name).to eq("homebrew.formula_name")
expect(f.plist_path).to eq(HOMEBREW_PREFIX/"opt/formula_name/homebrew.mxcl.formula_name.plist")
expect(f.launchd_service_path).to eq(HOMEBREW_PREFIX/"opt/formula_name/homebrew.mxcl.formula_name.plist")
expect(f.systemd_service_path).to eq(HOMEBREW_PREFIX/"opt/formula_name/homebrew.formula_name.service")
expect(f.systemd_timer_path).to eq(HOMEBREW_PREFIX/"opt/formula_name/homebrew.formula_name.timer")
end

View File

@ -64,6 +64,29 @@ describe Homebrew::Service do
end
end
describe "#requires_root?" do
it "returns status when set" do
f = stub_formula do
service do
run opt_bin/"beanstalkd"
require_root true
end
end
expect(f.service.requires_root?).to be(true)
end
it "returns status when not set" do
f = stub_formula do
service do
run opt_bin/"beanstalkd"
end
end
expect(f.service.requires_root?).to be(false)
end
end
describe "#run_type" do
it "throws for unexpected type" do
f = stub_formula do
@ -166,6 +189,7 @@ describe Homebrew::Service do
error_log_path var/"log/beanstalkd.error.log"
log_path var/"log/beanstalkd.log"
input_path var/"in/beanstalkd"
require_root true
root_dir var
working_dir var
keep_alive true
@ -539,6 +563,7 @@ describe Homebrew::Service do
error_log_path var/"log/beanstalkd.error.log"
log_path var/"log/beanstalkd.log"
input_path var/"in/beanstalkd"
require_root true
root_dir var
working_dir var
keep_alive true