Defined Type: systemd::timer_wrapper

Defined in:
manifests/timer_wrapper.pp

Summary

Helper to define timer and accompanying services for a given task (cron like interface).

Overview

Examples:

Create a timer that runs every 5 minutes

systemd::timer_wrapper { 'my_timer':
  ensure        => 'present',
  command       => '/usr/bin/echo "Hello World"',
  on_calendar   => '*:0/5',
}

Create a timer with overrides for the service and timer

systemd::timer_wrapper { 'my_timer':
  ensure             => 'present',
  command            => '/usr/bin/echo "Hello World"',
  on_calendar        => '*:0/5',
  service_overrides => { 'Group' => 'nobody' },
  timer_overrides   => { 'OnBootSec' => '10' },
}

Create a timer with overrides for the service_unit and timer_unit

systemd::timer_wrapper { 'my_timer':
  ensure                 => 'present',
  command                => '/usr/bin/echo "Hello World"',
  on_calendar            => '*:0/5',
  service_unit_overrides => { 'Wants' => 'network-online.target' },
  timer_unit_overrides   => { 'Description' => 'Very special timer' },
}

Parameters:

  • ensure (Enum['present', 'absent'])

    whether the timer and service should be present or absent

  • command (Optional[Systemd::Unit::Service::Exec]) (defaults to: undef)

    the command for the systemd servie to execute

  • user (Optional[String[1]]) (defaults to: undef)

    the user to run the command as

  • on_active_sec (Optional[Systemd::Unit::Timespan]) (defaults to: undef)

    run service relative to the time when the timer was activated

  • on_boot_sec (Optional[Systemd::Unit::Timespan]) (defaults to: undef)

    run service relative to when the machine was booted

  • on_start_up_sec (Optional[Systemd::Unit::Timespan]) (defaults to: undef)

    run service relative to when the service manager was started

  • on_unit_active_sec (Optional[Systemd::Unit::Timespan]) (defaults to: undef)

    run service relative to when the unit was last activated

  • on_unit_inactive_sec (Optional[Systemd::Unit::Timespan]) (defaults to: undef)

    run service relative to when the unit was last deactivated

  • on_calendar (Optional[Systemd::Unit::Timespan]) (defaults to: undef)

    the calendar event expressions time to run the service

  • service_overrides (Systemd::Unit::Service) (defaults to: {})

    override for the`‘ section of the service

  • timer_overrides (Systemd::Unit::Timer) (defaults to: {})

    override for the`‘ section of the timer

  • service_unit_overrides (Systemd::Unit::Unit) (defaults to: {})

    override for the`‘ section of the service

  • timer_unit_overrides (Systemd::Unit::Unit) (defaults to: {})

    override for the ‘[Unit]` section of the timer



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'manifests/timer_wrapper.pp', line 38

define systemd::timer_wrapper (
  Enum['present', 'absent']              $ensure,
  Optional[Systemd::Unit::Service::Exec] $command = undef,
  Optional[String[1]]                    $user = undef,
  Optional[Systemd::Unit::Timespan]      $on_active_sec = undef,
  Optional[Systemd::Unit::Timespan]      $on_boot_sec = undef,
  Optional[Systemd::Unit::Timespan]      $on_start_up_sec = undef,
  Optional[Systemd::Unit::Timespan]      $on_unit_active_sec = undef,
  Optional[Systemd::Unit::Timespan]      $on_unit_inactive_sec = undef,
  Optional[Systemd::Unit::Timespan]      $on_calendar = undef,
  Systemd::Unit::Service                 $service_overrides         = {},
  Systemd::Unit::Timer                   $timer_overrides           = {},
  Systemd::Unit::Unit                    $timer_unit_overrides      = {},
  Systemd::Unit::Unit                    $service_unit_overrides    = {},
) {
  $_timer_spec = {
    'OnActiveSec'       => $on_active_sec,
    'OnBootSec'         => $on_boot_sec,
    'OnStartUpSec'      => $on_start_up_sec,
    'OnUnitActiveSec'   => $on_unit_active_sec,
    'OnUnitInactiveSec' => $on_unit_inactive_sec,
    'OnCalendar'        => $on_calendar,
  }.filter |$k, $v| { $v =~ NotUndef }

  if $ensure == 'present' {
    if $_timer_spec == {} {
      fail('At least one of on_active_sec,
        on_boot_sec,
        on_start_up_sec,
        on_unit_active_sec,
        on_unit_inactive_sec,
        or on_calendar must be set'
      )
    }
    if ! $command {
      fail('command must be set')
    }
  }

  $service_ensure = $ensure ? { 'absent' => false,  default  => true, }

  systemd::manage_unit { "${title}.service":
    ensure        => $ensure,
    unit_entry    => $service_unit_overrides,
    service_entry => {
      'ExecStart' => $command, # if ensure present command is defined is checked above
      'User'      => $user, # defaults apply
      'Type'      => 'oneshot',
    }.filter |$key, $val| { $val =~ NotUndef } + $service_overrides,
  }
  systemd::manage_unit { "${title}.timer":
    ensure        => $ensure,
    unit_entry    => $timer_unit_overrides,
    timer_entry   => $_timer_spec +  $timer_overrides,
    install_entry => {
      'WantedBy' => 'timers.target',
    },
  }

  service { "${title}.timer":
    ensure => $service_ensure,
    enable => $service_ensure,
  }

  if $ensure == 'present' {
    Systemd::Manage_unit["${title}.service"]
    -> Systemd::Manage_unit["${title}.timer"]
    -> Service["${title}.timer"]
  } else {
    # Ensure the timer is stopped and disabled before the service
    Service["${title}.timer"]
    -> Systemd::Manage_unit["${title}.timer"]
    -> Systemd::Manage_unit["${title}.service"]
  }
}