Merge pull request #18864 from Homebrew/systemd-new-line

service: end systemd configs with a new line
This commit is contained in:
Mike McQuaid 2024-12-04 09:24:09 +00:00 committed by GitHub
commit 8b7c074b6b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 39 additions and 40 deletions

View File

@ -454,16 +454,6 @@ module Homebrew
# @return [String] # @return [String]
sig { returns(String) } sig { returns(String) }
def to_systemd_unit def to_systemd_unit
unit = <<~EOS
[Unit]
Description=Homebrew generated unit for #{@formula.name}
[Install]
WantedBy=default.target
[Service]
EOS
# command needs to be first because it initializes all other values # command needs to be first because it initializes all other values
cmd = command&.map { |arg| Utils::Shell.sh_quote(arg) } cmd = command&.map { |arg| Utils::Shell.sh_quote(arg) }
&.join(" ") &.join(" ")
@ -481,24 +471,22 @@ module Homebrew
options << "StandardError=append:#{File.expand_path(@error_log_path)}" if @error_log_path.present? options << "StandardError=append:#{File.expand_path(@error_log_path)}" if @error_log_path.present?
options += @environment_variables.map { |k, v| "Environment=\"#{k}=#{v}\"" } if @environment_variables.present? options += @environment_variables.map { |k, v| "Environment=\"#{k}=#{v}\"" } if @environment_variables.present?
unit + options.join("\n") <<~SYSTEMD
[Unit]
Description=Homebrew generated unit for #{@formula.name}
[Install]
WantedBy=default.target
[Service]
#{options.join("\n")}
SYSTEMD
end end
# Returns a `String` systemd unit timer. # Returns a `String` systemd unit timer.
# @return [String] # @return [String]
sig { returns(String) } sig { returns(String) }
def to_systemd_timer def to_systemd_timer
timer = <<~EOS
[Unit]
Description=Homebrew generated timer for #{@formula.name}
[Install]
WantedBy=timers.target
[Timer]
Unit=#{service_name}
EOS
options = [] options = []
options << "Persistent=true" if @run_type == RUN_TYPE_CRON options << "Persistent=true" if @run_type == RUN_TYPE_CRON
options << "OnUnitActiveSec=#{@interval}" if @run_type == RUN_TYPE_INTERVAL options << "OnUnitActiveSec=#{@interval}" if @run_type == RUN_TYPE_INTERVAL
@ -509,7 +497,17 @@ module Homebrew
options << "OnCalendar=#{@cron[:Weekday]}-*-#{@cron[:Month]}-#{@cron[:Day]} #{hours}:#{minutes}:00" options << "OnCalendar=#{@cron[:Weekday]}-*-#{@cron[:Month]}-#{@cron[:Day]} #{hours}:#{minutes}:00"
end end
timer + options.join("\n") <<~SYSTEMD
[Unit]
Description=Homebrew generated timer for #{@formula.name}
[Install]
WantedBy=timers.target
[Timer]
Unit=#{service_name}
#{options.join("\n")}
SYSTEMD
end end
# Prepare the service hash for inclusion in the formula API JSON. # Prepare the service hash for inclusion in the formula API JSON.

View File

@ -718,7 +718,7 @@ RSpec.describe Homebrew::Service do
unit = f.service.to_systemd_unit unit = f.service.to_systemd_unit
std_path = "#{HOMEBREW_PREFIX}/bin:#{HOMEBREW_PREFIX}/sbin:/usr/bin:/bin:/usr/sbin:/sbin" std_path = "#{HOMEBREW_PREFIX}/bin:#{HOMEBREW_PREFIX}/sbin:/usr/bin:/bin:/usr/sbin:/sbin"
unit_expect = <<~EOS unit_expect = <<~SYSTEMD
[Unit] [Unit]
Description=Homebrew generated unit for formula_name Description=Homebrew generated unit for formula_name
@ -737,8 +737,8 @@ RSpec.describe Homebrew::Service do
StandardError=append:#{HOMEBREW_PREFIX}/var/log/beanstalkd.error.log StandardError=append:#{HOMEBREW_PREFIX}/var/log/beanstalkd.error.log
Environment="PATH=#{std_path}" Environment="PATH=#{std_path}"
Environment="FOO=BAR" Environment="FOO=BAR"
EOS SYSTEMD
expect(unit).to eq(unit_expect.strip) expect(unit).to eq(unit_expect)
end end
it "returns valid partial oneshot unit" do it "returns valid partial oneshot unit" do
@ -751,7 +751,7 @@ RSpec.describe Homebrew::Service do
end end
unit = f.service.to_systemd_unit unit = f.service.to_systemd_unit
unit_expect = <<~EOS unit_expect = <<~SYSTEMD
[Unit] [Unit]
Description=Homebrew generated unit for formula_name Description=Homebrew generated unit for formula_name
@ -761,8 +761,8 @@ RSpec.describe Homebrew::Service do
[Service] [Service]
Type=oneshot Type=oneshot
ExecStart=#{HOMEBREW_PREFIX}/opt/#{name}/bin/beanstalkd ExecStart=#{HOMEBREW_PREFIX}/opt/#{name}/bin/beanstalkd
EOS SYSTEMD
expect(unit).to eq(unit_expect.strip) expect(unit).to eq(unit_expect)
end end
it "expands paths" do it "expands paths" do
@ -774,7 +774,7 @@ RSpec.describe Homebrew::Service do
end end
unit = f.service.to_systemd_unit unit = f.service.to_systemd_unit
unit_expect = <<~EOS unit_expect = <<~SYSTEMD
[Unit] [Unit]
Description=Homebrew generated unit for formula_name Description=Homebrew generated unit for formula_name
@ -785,8 +785,8 @@ RSpec.describe Homebrew::Service do
Type=simple Type=simple
ExecStart=#{HOMEBREW_PREFIX}/opt/#{name}/bin/beanstalkd ExecStart=#{HOMEBREW_PREFIX}/opt/#{name}/bin/beanstalkd
WorkingDirectory=#{Dir.home} WorkingDirectory=#{Dir.home}
EOS SYSTEMD
expect(unit).to eq(unit_expect.strip) expect(unit).to eq(unit_expect)
end end
end end
@ -801,7 +801,7 @@ RSpec.describe Homebrew::Service do
end end
unit = f.service.to_systemd_timer unit = f.service.to_systemd_timer
unit_expect = <<~EOS unit_expect = <<~SYSTEMD
[Unit] [Unit]
Description=Homebrew generated timer for formula_name Description=Homebrew generated timer for formula_name
@ -811,8 +811,8 @@ RSpec.describe Homebrew::Service do
[Timer] [Timer]
Unit=homebrew.formula_name Unit=homebrew.formula_name
OnUnitActiveSec=5 OnUnitActiveSec=5
EOS SYSTEMD
expect(unit).to eq(unit_expect.strip) expect(unit).to eq(unit_expect)
end end
it "returns valid partial timer" do it "returns valid partial timer" do
@ -824,7 +824,7 @@ RSpec.describe Homebrew::Service do
end end
unit = f.service.to_systemd_timer unit = f.service.to_systemd_timer
unit_expect = <<~EOS unit_expect = <<~SYSTEMD
[Unit] [Unit]
Description=Homebrew generated timer for formula_name Description=Homebrew generated timer for formula_name
@ -833,7 +833,8 @@ RSpec.describe Homebrew::Service do
[Timer] [Timer]
Unit=homebrew.formula_name Unit=homebrew.formula_name
EOS
SYSTEMD
expect(unit).to eq(unit_expect) expect(unit).to eq(unit_expect)
end end
@ -872,7 +873,7 @@ RSpec.describe Homebrew::Service do
end end
unit = f.service.to_systemd_timer unit = f.service.to_systemd_timer
unit_expect = <<~EOS unit_expect = <<~SYSTEMD
[Unit] [Unit]
Description=Homebrew generated timer for formula_name Description=Homebrew generated timer for formula_name
@ -883,8 +884,8 @@ RSpec.describe Homebrew::Service do
Unit=homebrew.formula_name Unit=homebrew.formula_name
Persistent=true Persistent=true
OnCalendar=#{calendar} OnCalendar=#{calendar}
EOS SYSTEMD
expect(unit).to eq(unit_expect.chomp) expect(unit).to eq(unit_expect)
end end
end end
end end