Skip to content

Commit c6d90ab

Browse files
Maurice Meyermmoll
authored andcommitted
Configurable minute and hour for cronjob and systemd.timer
1 parent 457bcfb commit c6d90ab

File tree

6 files changed

+124
-5
lines changed

6 files changed

+124
-5
lines changed

manifests/agent/service.pp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,15 @@
3939

4040
class { 'puppet::agent::service::systemd':
4141
enabled => $systemd_enabled,
42+
hour => $::puppet::run_hour,
43+
minute => $::puppet::run_minute,
4244
}
4345
contain puppet::agent::service::systemd
4446

4547
class { 'puppet::agent::service::cron':
4648
enabled => $cron_enabled,
49+
hour => $::puppet::run_hour,
50+
minute => $::puppet::run_minute,
4751
}
4852
contain puppet::agent::service::cron
4953
}

manifests/agent/service/cron.pp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
11
# Set up running the agent via cron
22
# @api private
33
class puppet::agent::service::cron (
4-
Boolean $enabled = false,
4+
Boolean $enabled = false,
5+
Optional[Integer[0,23]] $hour = undef,
6+
Optional[Integer[0,59]] $minute = undef,
57
) {
68
unless $::puppet::runmode == 'unmanaged' or 'cron' in $::puppet::unavailable_runmodes {
79
if $enabled {
810
$command = pick($::puppet::cron_cmd, "${::puppet::puppet_cmd} agent --config ${::puppet::dir}/puppet.conf --onetime --no-daemonize")
911
$times = extlib::ip_to_cron($::puppet::runinterval)
12+
13+
$_hour = pick($hour, $times[0])
14+
$_minute = pick($minute, $times[1])
15+
1016
cron { 'puppet':
1117
command => $command,
1218
user => root,
13-
hour => $times[0],
14-
minute => $times[1],
19+
hour => $_hour,
20+
minute => $_minute,
1521
}
1622
} else{
1723
cron { 'puppet':

manifests/agent/service/systemd.pp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
# Set up running the agent via a systemd timer
22
# @api private
33
class puppet::agent::service::systemd (
4-
Boolean $enabled = false,
4+
Boolean $enabled = false,
5+
Optional[Integer[0,23]] $hour = undef,
6+
Optional[Integer[0,59]] $minute = undef,
57
) {
68
unless $::puppet::runmode == 'unmanaged' or 'systemd.timer' in $::puppet::unavailable_runmodes {
79
exec { 'systemctl-daemon-reload-puppet':
@@ -14,6 +16,10 @@
1416
# Use the same times as for cron
1517
$times = extlib::ip_to_cron($::puppet::runinterval)
1618

19+
# But only if they are not explicitly specified
20+
$_hour = pick($hour, $times[0])
21+
$_minute = pick($minute, $times[1])
22+
1723
$command = $::puppet::systemd_cmd ? {
1824
undef => "${::puppet::puppet_cmd} agent --config ${::puppet::dir}/puppet.conf --onetime --no-daemonize --detailed-exitcode --no-usecacheonfailure",
1925
default => $::puppet::systemd_cmd,

manifests/init.pp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,12 @@
7272
#
7373
# $runmode:: Select the mode to setup the puppet agent.
7474
#
75+
# $run_hour:: The hour at which to run the puppet agent
76+
# when runmode is cron or systemd.timer.
77+
#
78+
# $run_minute:: The minute at which to run the puppet agent
79+
# when runmode is cron or systemd.timer.
80+
#
7581
# $cron_cmd:: Specify command to launch when runmode is
7682
# set 'cron'.
7783
#
@@ -563,6 +569,8 @@
563569
Variant[Integer[0],Pattern[/^\d+[smhdy]?$/]] $runinterval = $puppet::params::runinterval,
564570
Boolean $usecacheonfailure = $puppet::params::usecacheonfailure,
565571
Enum['cron', 'service', 'systemd.timer', 'none', 'unmanaged'] $runmode = $puppet::params::runmode,
572+
Optional[Integer[0,23]] $run_hour = undef,
573+
Optional[Integer[0,59]] $run_minute = undef,
566574
Array[Enum['cron', 'service', 'systemd.timer', 'none']] $unavailable_runmodes = $puppet::params::unavailable_runmodes,
567575
Optional[String] $cron_cmd = $puppet::params::cron_cmd,
568576
Optional[String] $systemd_cmd = $puppet::params::systemd_cmd,

spec/classes/puppet_agent_spec.rb

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,52 @@
239239
end
240240
end
241241

242+
describe 'when runmode => cron with specified time' do
243+
let :params do
244+
super().merge(runmode: 'cron',
245+
run_hour: 22,
246+
run_minute: 01
247+
)
248+
end
249+
250+
case os
251+
when /\A(windows|archlinux)/
252+
it { is_expected.to raise_error(Puppet::Error, /Runmode of cron not supported on #{facts[:kernel]} operating systems!/) }
253+
when /\Adebian-/, /\A(redhat|centos|scientific)-7/, /\Afedora-/, /\Aubuntu-(16|18)/
254+
it { is_expected.to contain_class('puppet::agent::service::cron').with_enabled(true) }
255+
it { is_expected.to contain_class('puppet::agent::service::daemon').with_enabled(false) }
256+
it do
257+
is_expected.to contain_service('puppet')
258+
.with_ensure('stopped')
259+
.with_name('puppet')
260+
.with_hasstatus('true')
261+
.with_enable('false')
262+
end
263+
it { is_expected.to contain_class('puppet::agent::service::systemd').with_enabled(false) }
264+
it { is_expected.to contain_service('puppet-run.timer').with_ensure(:stopped) }
265+
it do
266+
is_expected.to contain_cron('puppet')
267+
.with_command("#{bindir}/puppet agent --config #{confdir}/puppet.conf --onetime --no-daemonize")
268+
.with_user('root')
269+
.with_minute('1')
270+
.with_hour('22')
271+
end
272+
else
273+
it { is_expected.to compile.with_all_deps }
274+
it { is_expected.to contain_class('puppet::agent::service::cron').with_enabled(true) }
275+
it { is_expected.to contain_class('puppet::agent::service::daemon').with_enabled(false) }
276+
it { is_expected.to contain_class('puppet::agent::service::systemd').with_enabled(false) }
277+
it { is_expected.not_to contain_service('puppet-run.timer') }
278+
it do
279+
is_expected.to contain_cron('puppet')
280+
.with_command("#{bindir}/puppet agent --config #{confdir}/puppet.conf --onetime --no-daemonize")
281+
.with_user('root')
282+
.with_minute('1')
283+
.with_hour('22')
284+
end
285+
end
286+
end
287+
242288
describe 'when runmode => systemd.timer' do
243289
let :params do
244290
super().merge(runmode: 'systemd.timer')
@@ -285,6 +331,55 @@
285331
end
286332
end
287333

334+
describe 'when runmode => systemd.timer with configured time' do
335+
let :params do
336+
super().merge(runmode: 'systemd.timer',
337+
run_hour: 22,
338+
run_minute: 01
339+
)
340+
end
341+
342+
case os
343+
when /\Adebian-/, /\A(redhat|centos|scientific)-7/, /\Afedora-/, /\Aubuntu-(16|18)/, /\Aarchlinux-/
344+
it { is_expected.to compile.with_all_deps }
345+
it { is_expected.to contain_class('puppet::agent::service::daemon').with_enabled(false) }
346+
it { is_expected.to contain_class('puppet::agent::service::cron').with_enabled(false) }
347+
it { is_expected.to contain_class('puppet::agent::service::systemd').with_enabled(true) }
348+
it { is_expected.to contain_service('puppet-run.timer').with_ensure(:running) }
349+
350+
it do
351+
is_expected.to contain_file('/etc/systemd/system/puppet-run.timer')
352+
.with_content(/.*OnCalendar\=\*-\*-\* 22:1:00.*/)
353+
end
354+
355+
it do
356+
is_expected.to contain_file('/etc/systemd/system/puppet-run.timer')
357+
.with_content(/^RandomizedDelaySec\=0$/)
358+
end
359+
360+
it do
361+
is_expected.to contain_file('/etc/systemd/system/puppet-run.service')
362+
.with_content(%r{^ExecStart=#{bindir}/puppet agent --config #{confdir}/puppet.conf --onetime --no-daemonize --detailed-exitcode --no-usecacheonfailure$})
363+
end
364+
365+
it do
366+
is_expected.to contain_exec('systemctl-daemon-reload-puppet')
367+
.with_refreshonly(true)
368+
.with_command('systemctl daemon-reload')
369+
end
370+
371+
it do
372+
is_expected.to contain_service('puppet-run.timer')
373+
.with_provider('systemd')
374+
.with_ensure('running')
375+
.with_name('puppet-run.timer')
376+
.with_enable('true')
377+
end
378+
else
379+
it { is_expected.to raise_error(Puppet::Error, /Runmode of systemd.timer not supported on #{facts[:kernel]} operating systems!/) }
380+
end
381+
end
382+
288383
describe 'when runmode => none' do
289384
let :params do
290385
super().merge(runmode: 'none')

templates/agent/systemd.puppet-run.timer.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
Description=Systemd Timer for Puppet Agent
66

77
[Timer]
8-
OnCalendar=*-*-* <%= Array(@times[0]).join(',') %>:<%= Array(@times[1]).join(',') %>:00
8+
OnCalendar=*-*-* <%= Array(@_hour).join(',') %>:<%= Array(@_minute).join(',') %>:00
99
Persistent=true
1010
RandomizedDelaySec=<%= @randomizeddelaysec %>
1111

0 commit comments

Comments
 (0)