forked from voxpupuli/puppet-systemd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
timer_wrapper.pp
92 lines (87 loc) · 4.01 KB
/
timer_wrapper.pp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
# @summary
# Helper to define timer and accompanying services for a given task (cron like interface).
# @param ensure whether the timer and service should be present or absent
# @param command the command for the systemd servie to execute
# @param user the user to run the command as
# @param on_active_sec run service relative to the time when the timer was activated
# @param on_boot_sec run service relative to when the machine was booted
# @param on_start_up_sec run service relative to when the service manager was started
# @param on_unit_active_sec run service relative to when the unit was last activated
# @param on_unit_inactive_sec run service relative to when the unit was last deactivated
# @param on_calendar the calendar event expressions time to run the service
# @param service_overrides override for the`[Service]` section of the service
# example: `'service_overrides' => { 'Wants' => 'network-online.target' }`
# @param timer_overrides override for the`[Timer]` section of the timer
# example: `'timer_overrides' => { 'OnBootSec' => '10' }`
# @param service_unit_overrides override for the`[Unit]` section of the service
# example: `'service_unit_overrides' => { 'Wants' => 'network-online.target' }`
# @param timer_unit_overrides override for the `[Unit]` section of the timer
# can not be used in combination with `additional_timer_params`
define systemd::timer_wrapper (
Enum['present', 'absent'] $ensure,
Optional[Systemd::Unit::Service::Exec] $command = undef,
Optional[String[1]] $user = undef,
Optional[Systemd::Unit::Timer::TimerSpec] $on_active_sec = undef,
Optional[Systemd::Unit::Timer::TimerSpec] $on_boot_sec = undef,
Optional[Systemd::Unit::Timer::TimerSpec] $on_start_up_sec = undef,
Optional[Systemd::Unit::Timer::TimerSpec] $on_unit_active_sec = undef,
Optional[Systemd::Unit::Timer::TimerSpec] $on_unit_inactive_sec = undef,
Optional[Systemd::Unit::Timer::TimerSpec] $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, }
$unit_name = systemd::escape($title)
systemd::manage_unit { "${unit_name}.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 { "${unit_name}.timer":
ensure => $ensure,
unit_entry => $timer_unit_overrides,
timer_entry => $_timer_spec + $timer_overrides,
}
service { "${unit_name}.timer":
ensure => $service_ensure,
enable => $service_ensure,
}
if $ensure == 'present' {
Systemd::Manage_unit["${unit_name}.service"]
-> Systemd::Manage_unit["${unit_name}.timer"]
-> Service["${unit_name}.timer"]
} else {
# Ensure the timer is stopped and disabled before the service
Service["${unit_name}.timer"]
-> Systemd::Manage_unit["${unit_name}.timer"]
-> Systemd::Manage_unit["${unit_name}.service"]
}
}