Skip to content

Commit

Permalink
ref(salt): Use the helper Utils module for plist patching
Browse files Browse the repository at this point in the history
  • Loading branch information
cdalvaro committed Feb 2, 2024
1 parent 9cb8a6c commit 11cb792
Showing 1 changed file with 32 additions and 28 deletions.
60 changes: 32 additions & 28 deletions Casks/salt.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,34 @@
module Utils
def self.patch_plist(daemon)
plist_file = "/Library/LaunchDaemons/com.saltstack.salt.#{daemon}.plist"
xml, = system_command "plutil",
args: ["-convert", "xml1", "-o", "-", "--", plist_file],
sudo: true
xml = Plist.parse_xml(xml)

xml["EnvironmentVariables"] = {} unless xml.key?("EnvironmentVariables")
xml["EnvironmentVariables"]["HOMEBREW_PREFIX"] = HOMEBREW_PREFIX.to_s
xml["EnvironmentVariables"]["HOME"] ||= "/var/root"

path = xml["EnvironmentVariables"]["PATH"] || "/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
path = "#{HOMEBREW_PREFIX}/bin:#{path}" unless path.split(":").include?("#{HOMEBREW_PREFIX}/bin")
xml["EnvironmentVariables"]["PATH"] = path

random_str = (0...8).map { rand(65..90).chr }.join
new_plist_file = "/tmp/#{random_str}.#{File.basename(plist_file)}"
File.write(new_plist_file, xml.to_plist)
system_command "plutil",
args: ["-lint", new_plist_file]

system_command "mv",
args: [new_plist_file, plist_file],
sudo: true
system_command "chown",
args: ["root:wheel", plist_file],
sudo: true
end
end

cask "salt" do
arch arm: "arm64", intel: "x86_64"

Expand All @@ -20,34 +51,7 @@
pkg "salt-#{version}-py3-#{arch}.pkg"

postflight do
random_str = (0...8).map { rand(65..90).chr }.join
%w[api master minion syndic].each do |daemon|
plist_file = "/Library/LaunchDaemons/com.saltstack.salt.#{daemon}.plist"
xml, = system_command "plutil",
args: ["-convert", "xml1", "-o", "-", "--", plist_file],
sudo: true
xml = Plist.parse_xml(xml)

xml["EnvironmentVariables"] = {} unless xml.key?("EnvironmentVariables")
xml["EnvironmentVariables"]["HOMEBREW_PREFIX"] = HOMEBREW_PREFIX.to_s
xml["EnvironmentVariables"]["HOME"] ||= "/var/root"

path = xml["EnvironmentVariables"]["PATH"] || "/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
path = "#{HOMEBREW_PREFIX}/bin:#{path}" unless path.split(":").include?("#{HOMEBREW_PREFIX}/bin")
xml["EnvironmentVariables"]["PATH"] = path

new_plist_file = "/tmp/#{random_str}.#{File.basename(plist_file)}"
File.write(new_plist_file, xml.to_plist)
system_command "plutil",
args: ["-lint", new_plist_file]

system_command "mv",
args: [new_plist_file, plist_file],
sudo: true
system_command "chown",
args: ["root:wheel", plist_file],
sudo: true
end
%w[api master minion syndic].each { |daemon| Utils.patch_plist(daemon) }
end

uninstall launchctl: [
Expand Down

0 comments on commit 11cb792

Please sign in to comment.