Skip to content

Commit

Permalink
Merge pull request #18864 from Homebrew/systemd-new-line
Browse files Browse the repository at this point in the history
service: end systemd configs with a new line
  • Loading branch information
MikeMcQuaid authored Dec 4, 2024
2 parents 52c8c64 + 46fab44 commit 8b7c074
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 40 deletions.
44 changes: 21 additions & 23 deletions Library/Homebrew/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -454,16 +454,6 @@ def to_plist
# @return [String]
sig { returns(String) }
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
cmd = command&.map { |arg| Utils::Shell.sh_quote(arg) }
&.join(" ")
Expand All @@ -481,24 +471,22 @@ def to_systemd_unit
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?

unit + options.join("\n")
<<~SYSTEMD
[Unit]
Description=Homebrew generated unit for #{@formula.name}
[Install]
WantedBy=default.target
[Service]
#{options.join("\n")}
SYSTEMD
end

# Returns a `String` systemd unit timer.
# @return [String]
sig { returns(String) }
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 << "Persistent=true" if @run_type == RUN_TYPE_CRON
options << "OnUnitActiveSec=#{@interval}" if @run_type == RUN_TYPE_INTERVAL
Expand All @@ -509,7 +497,17 @@ def to_systemd_timer
options << "OnCalendar=#{@cron[:Weekday]}-*-#{@cron[:Month]}-#{@cron[:Day]} #{hours}:#{minutes}:00"
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

# Prepare the service hash for inclusion in the formula API JSON.
Expand Down
35 changes: 18 additions & 17 deletions Library/Homebrew/test/service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,7 @@ def stub_formula_with_service_sockets(sockets_var)

unit = f.service.to_systemd_unit
std_path = "#{HOMEBREW_PREFIX}/bin:#{HOMEBREW_PREFIX}/sbin:/usr/bin:/bin:/usr/sbin:/sbin"
unit_expect = <<~EOS
unit_expect = <<~SYSTEMD
[Unit]
Description=Homebrew generated unit for formula_name
Expand All @@ -737,8 +737,8 @@ def stub_formula_with_service_sockets(sockets_var)
StandardError=append:#{HOMEBREW_PREFIX}/var/log/beanstalkd.error.log
Environment="PATH=#{std_path}"
Environment="FOO=BAR"
EOS
expect(unit).to eq(unit_expect.strip)
SYSTEMD
expect(unit).to eq(unit_expect)
end

it "returns valid partial oneshot unit" do
Expand All @@ -751,7 +751,7 @@ def stub_formula_with_service_sockets(sockets_var)
end

unit = f.service.to_systemd_unit
unit_expect = <<~EOS
unit_expect = <<~SYSTEMD
[Unit]
Description=Homebrew generated unit for formula_name
Expand All @@ -761,8 +761,8 @@ def stub_formula_with_service_sockets(sockets_var)
[Service]
Type=oneshot
ExecStart=#{HOMEBREW_PREFIX}/opt/#{name}/bin/beanstalkd
EOS
expect(unit).to eq(unit_expect.strip)
SYSTEMD
expect(unit).to eq(unit_expect)
end

it "expands paths" do
Expand All @@ -774,7 +774,7 @@ def stub_formula_with_service_sockets(sockets_var)
end

unit = f.service.to_systemd_unit
unit_expect = <<~EOS
unit_expect = <<~SYSTEMD
[Unit]
Description=Homebrew generated unit for formula_name
Expand All @@ -785,8 +785,8 @@ def stub_formula_with_service_sockets(sockets_var)
Type=simple
ExecStart=#{HOMEBREW_PREFIX}/opt/#{name}/bin/beanstalkd
WorkingDirectory=#{Dir.home}
EOS
expect(unit).to eq(unit_expect.strip)
SYSTEMD
expect(unit).to eq(unit_expect)
end
end

Expand All @@ -801,7 +801,7 @@ def stub_formula_with_service_sockets(sockets_var)
end

unit = f.service.to_systemd_timer
unit_expect = <<~EOS
unit_expect = <<~SYSTEMD
[Unit]
Description=Homebrew generated timer for formula_name
Expand All @@ -811,8 +811,8 @@ def stub_formula_with_service_sockets(sockets_var)
[Timer]
Unit=homebrew.formula_name
OnUnitActiveSec=5
EOS
expect(unit).to eq(unit_expect.strip)
SYSTEMD
expect(unit).to eq(unit_expect)
end

it "returns valid partial timer" do
Expand All @@ -824,7 +824,7 @@ def stub_formula_with_service_sockets(sockets_var)
end

unit = f.service.to_systemd_timer
unit_expect = <<~EOS
unit_expect = <<~SYSTEMD
[Unit]
Description=Homebrew generated timer for formula_name
Expand All @@ -833,7 +833,8 @@ def stub_formula_with_service_sockets(sockets_var)
[Timer]
Unit=homebrew.formula_name
EOS
SYSTEMD
expect(unit).to eq(unit_expect)
end

Expand Down Expand Up @@ -872,7 +873,7 @@ def stub_formula_with_service_sockets(sockets_var)
end

unit = f.service.to_systemd_timer
unit_expect = <<~EOS
unit_expect = <<~SYSTEMD
[Unit]
Description=Homebrew generated timer for formula_name
Expand All @@ -883,8 +884,8 @@ def stub_formula_with_service_sockets(sockets_var)
Unit=homebrew.formula_name
Persistent=true
OnCalendar=#{calendar}
EOS
expect(unit).to eq(unit_expect.chomp)
SYSTEMD
expect(unit).to eq(unit_expect)
end
end
end
Expand Down

0 comments on commit 8b7c074

Please sign in to comment.