Module: Wheneverd::Systemd::UnitContentBuilder

Defined in:
lib/wheneverd/systemd/unit_content_builder.rb

Overview

Builds the text content for systemd unit files.

Handles the structure of service and timer unit files, including the marker header, sections, and proper formatting.

Constant Summary collapse

SERVICE_SECTION =
["[Service]", "Type=oneshot"].freeze
TIMER_SUFFIX =
["Persistent=true", "", "[Install]", "WantedBy=timers.target", ""].freeze

Class Method Summary collapse

Class Method Details

.service_contents(path_basename, command) ⇒ String

Build service unit file contents.

Parameters:

  • path_basename (String)

    unit file name for description

  • command (String)

    ExecStart command

Returns:

  • (String)

    complete unit file contents



18
19
20
21
22
23
# File 'lib/wheneverd/systemd/unit_content_builder.rb', line 18

def self.service_contents(path_basename, command)
  build_unit(
    description: "wheneverd job #{path_basename}",
    sections: SERVICE_SECTION + ["ExecStart=#{command}", ""]
  )
end

.standalone_service_contents(path_basename, service) ⇒ String

Build a long-running service unit file.

Parameters:

Returns:

  • (String)

    complete unit file contents



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/wheneverd/systemd/unit_content_builder.rb', line 30

def self.standalone_service_contents(path_basename, service)
  build_unit(
    description: "wheneverd service #{path_basename}",
    sections: [
      "[Service]",
      "Type=simple",
      "ExecStart=#{service.command.command}",
      "Restart=#{service.restart}",
      "RestartSec=#{service.restart_sec}",
      *service.service_lines,
      "",
      "[Install]",
      "WantedBy=default.target",
      ""
    ]
  )
end

.timer_contents(path_basename, timer_lines) ⇒ String

Build timer unit file contents.

Parameters:

  • path_basename (String)

    unit file name for description

  • timer_lines (Array<String>)

    timer configuration lines (OnCalendar, OnActiveSec, etc.)

Returns:

  • (String)

    complete unit file contents



53
54
55
56
57
58
# File 'lib/wheneverd/systemd/unit_content_builder.rb', line 53

def self.timer_contents(path_basename, timer_lines)
  build_unit(
    description: "wheneverd timer #{path_basename}",
    sections: ["[Timer]"] + timer_lines + TIMER_SUFFIX
  )
end

.timer_lines_for(trigger) ⇒ Array<String>

Build timer lines for a trigger.

Parameters:

Returns:

  • (Array<String>)

    timer configuration lines

Raises:

  • (ArgumentError)

    if trigger type is unsupported



65
66
67
68
69
70
71
72
73
74
# File 'lib/wheneverd/systemd/unit_content_builder.rb', line 65

def self.timer_lines_for(trigger)
  # Calendar triggers need special handling to convert DSL specs to systemd specs
  return calendar_timer_lines(trigger) if trigger.is_a?(Wheneverd::Trigger::Calendar)

  unless trigger.respond_to?(:systemd_timer_lines)
    raise ArgumentError, "Unsupported trigger type: #{trigger.class}"
  end

  trigger.systemd_timer_lines
end