108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
|
# File 'modules/systemd/manifests/timer/job.pp', line 108
define systemd::timer::job (
Variant[
Systemd::Timer::Schedule,
Array[Systemd::Timer::Schedule, 1]] $interval,
String $description,
Systemd::Command $command,
String $user,
Wmflib::Ensure $ensure = 'present',
Hash[String, String] $environment = {},
Boolean $monitoring_enabled = false,
String $monitoring_contact_groups = 'admins',
Stdlib::HTTPSUrl $monitoring_notes_url = 'https://wikitech.wikimedia.org/wiki/Monitoring/systemd_unit_state',
Boolean $logging_enabled = true,
String $logfile_basedir = '/var/log',
String $logfile_name = 'syslog.log',
String $logfile_group = 'root',
Enum['user', 'group', 'all'] $logfile_perms = 'all',
Boolean $syslog_force_stop = true,
Boolean $syslog_match_startswith = true,
Boolean $send_mail = false,
String $send_mail_to = "root@${facts['networking']['fqdn']}",
Boolean $ignore_errors = false,
Boolean $send_mail_only_on_error = true,
Boolean $private_tmp = false,
Optional[Integer] $splay = undef,
# TODO: set some sane default otherwise its infinite T324466
Optional[Integer] $timeout_start_sec = undef,
Optional[String] $logfile_owner = undef,
Optional[String] $syslog_identifier = undef,
Optional[Integer] $max_runtime_seconds = undef,
Optional[Pattern[/\w+\.slice/]] $slice = undef,
Optional[Stdlib::Unixpath] $environment_file = undef,
Optional[Systemd::Input] $stdin = undef,
Optional[Systemd::Output] $stdout = undef,
Optional[Systemd::Output] $stderr = undef,
Optional[Stdlib::Unixpath] $working_directory = undef,
Optional[Systemd::Command] $exec_start_pre = undef,
Optional[String] $after = undef,
Optional[String] $group = undef,
Optional[String] $path_exists = undef,
Array[Integer[1, 255]] $success_exit_status = [],
Optional[Wmflib::Team] $team = undef
) {
unless $path_exists =~ Undef or $path_exists =~ Stdlib::UnixPath
or ($path_exists[0] == '!' and $path_exists[1,-1] =~ Stdlib::UnixPath) {
fail('$path_exists must either be Stdlib::UnixPath or Stdlib::UnixPath prefixed with `!`')
}
# Systemd doesn't play well with spaces in unit names, so check for that
if $title =~ /\s/ {
fail("Invalid title '${title}' for systemd timer - it should not include spaces.")
}
# Sanitize the title for use on the filesystem
$safe_title = regsubst($title, '[^\w\-]', '_', 'G')
$input_intervals = $interval ? {
Systemd::Timer::Schedule => [$interval],
default => $interval,
}
# If we were provided with only OnUnitActive/OnUnitInactive intervals, which are times relative
# to when the service unit was last activated or deactivated, we need to add an additional
# interval to get systemd to DTRT with this timer: an OnActiveSec that will start the service
# for the first time after the timer and service are installed (or after a reboot).
#
# This activation gives meaning to the OnUnit intervals, as a null value for the timestamp of
# last service activation/deactivation acts like NaN, making the comparison always false.
$mangled_intervals = $input_intervals.all |$iv| { $iv['start'] =~ /OnUnit(In)?[Aa]ctive/ } ? {
false => $input_intervals,
true => $input_intervals + [{
'interval' => '1s',
'start' => 'OnActiveSec'
}],
}
systemd::unit { "${title}.service":
ensure => $ensure,
team => $team,
content => template('systemd/timer_service.erb'),
}
systemd::timer { $title:
ensure => $ensure,
timer_intervals => $mangled_intervals,
splay => $splay,
unit_name => "${title}.service",
}
if $logging_enabled {
# The owner of the log files
$log_owner = $logfile_owner ? {
undef => $user,
default => $logfile_owner
}
# If syslog_match_startswith is false, use equality when matching
# the programname to output to the log file, else use startswith.
$syslog_programname_comparison = $syslog_match_startswith ? {
false => 'isequal',
true => 'startswith',
}
$syslog_title = $syslog_identifier ? {
default => $syslog_identifier,
undef => $safe_title,
}
systemd::syslog { $syslog_title:
ensure => $ensure,
base_dir => $logfile_basedir,
log_filename => $logfile_name,
owner => $log_owner,
group => $logfile_group,
readable_by => $logfile_perms,
force_stop => $syslog_force_stop,
programname_comparison => $syslog_programname_comparison,
}
}
if $monitoring_enabled {
systemd::monitor { $title:
ensure => $ensure,
notes_url => $monitoring_notes_url,
contact_group => $monitoring_contact_groups,
}
}
}
|