Skip to content

Commit

Permalink
Configurable minute and hour for cronjob and systemd.timer
Browse files Browse the repository at this point in the history
  • Loading branch information
Maurice Meyer authored and mmoll committed Mar 31, 2019
1 parent 457bcfb commit c6d90ab
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 5 deletions.
4 changes: 4 additions & 0 deletions manifests/agent/service.pp
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,15 @@

class { 'puppet::agent::service::systemd':
enabled => $systemd_enabled,
hour => $::puppet::run_hour,
minute => $::puppet::run_minute,
}
contain puppet::agent::service::systemd

class { 'puppet::agent::service::cron':
enabled => $cron_enabled,
hour => $::puppet::run_hour,
minute => $::puppet::run_minute,
}
contain puppet::agent::service::cron
}
12 changes: 9 additions & 3 deletions manifests/agent/service/cron.pp
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
# Set up running the agent via cron
# @api private
class puppet::agent::service::cron (
Boolean $enabled = false,
Boolean $enabled = false,
Optional[Integer[0,23]] $hour = undef,
Optional[Integer[0,59]] $minute = undef,
) {
unless $::puppet::runmode == 'unmanaged' or 'cron' in $::puppet::unavailable_runmodes {
if $enabled {
$command = pick($::puppet::cron_cmd, "${::puppet::puppet_cmd} agent --config ${::puppet::dir}/puppet.conf --onetime --no-daemonize")
$times = extlib::ip_to_cron($::puppet::runinterval)

$_hour = pick($hour, $times[0])
$_minute = pick($minute, $times[1])

cron { 'puppet':
command => $command,
user => root,
hour => $times[0],
minute => $times[1],
hour => $_hour,
minute => $_minute,
}
} else{
cron { 'puppet':
Expand Down
8 changes: 7 additions & 1 deletion manifests/agent/service/systemd.pp
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# Set up running the agent via a systemd timer
# @api private
class puppet::agent::service::systemd (
Boolean $enabled = false,
Boolean $enabled = false,
Optional[Integer[0,23]] $hour = undef,
Optional[Integer[0,59]] $minute = undef,
) {
unless $::puppet::runmode == 'unmanaged' or 'systemd.timer' in $::puppet::unavailable_runmodes {
exec { 'systemctl-daemon-reload-puppet':
Expand All @@ -14,6 +16,10 @@
# Use the same times as for cron
$times = extlib::ip_to_cron($::puppet::runinterval)

# But only if they are not explicitly specified
$_hour = pick($hour, $times[0])
$_minute = pick($minute, $times[1])

$command = $::puppet::systemd_cmd ? {
undef => "${::puppet::puppet_cmd} agent --config ${::puppet::dir}/puppet.conf --onetime --no-daemonize --detailed-exitcode --no-usecacheonfailure",
default => $::puppet::systemd_cmd,
Expand Down
8 changes: 8 additions & 0 deletions manifests/init.pp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@
#
# $runmode:: Select the mode to setup the puppet agent.
#
# $run_hour:: The hour at which to run the puppet agent
# when runmode is cron or systemd.timer.
#
# $run_minute:: The minute at which to run the puppet agent
# when runmode is cron or systemd.timer.
#
# $cron_cmd:: Specify command to launch when runmode is
# set 'cron'.
#
Expand Down Expand Up @@ -563,6 +569,8 @@
Variant[Integer[0],Pattern[/^\d+[smhdy]?$/]] $runinterval = $puppet::params::runinterval,
Boolean $usecacheonfailure = $puppet::params::usecacheonfailure,
Enum['cron', 'service', 'systemd.timer', 'none', 'unmanaged'] $runmode = $puppet::params::runmode,
Optional[Integer[0,23]] $run_hour = undef,
Optional[Integer[0,59]] $run_minute = undef,
Array[Enum['cron', 'service', 'systemd.timer', 'none']] $unavailable_runmodes = $puppet::params::unavailable_runmodes,
Optional[String] $cron_cmd = $puppet::params::cron_cmd,
Optional[String] $systemd_cmd = $puppet::params::systemd_cmd,
Expand Down
95 changes: 95 additions & 0 deletions spec/classes/puppet_agent_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,52 @@
end
end

describe 'when runmode => cron with specified time' do
let :params do
super().merge(runmode: 'cron',
run_hour: 22,
run_minute: 01
)
end

case os
when /\A(windows|archlinux)/
it { is_expected.to raise_error(Puppet::Error, /Runmode of cron not supported on #{facts[:kernel]} operating systems!/) }
when /\Adebian-/, /\A(redhat|centos|scientific)-7/, /\Afedora-/, /\Aubuntu-(16|18)/
it { is_expected.to contain_class('puppet::agent::service::cron').with_enabled(true) }
it { is_expected.to contain_class('puppet::agent::service::daemon').with_enabled(false) }
it do
is_expected.to contain_service('puppet')
.with_ensure('stopped')
.with_name('puppet')
.with_hasstatus('true')
.with_enable('false')
end
it { is_expected.to contain_class('puppet::agent::service::systemd').with_enabled(false) }
it { is_expected.to contain_service('puppet-run.timer').with_ensure(:stopped) }
it do
is_expected.to contain_cron('puppet')
.with_command("#{bindir}/puppet agent --config #{confdir}/puppet.conf --onetime --no-daemonize")
.with_user('root')
.with_minute('1')
.with_hour('22')
end
else
it { is_expected.to compile.with_all_deps }
it { is_expected.to contain_class('puppet::agent::service::cron').with_enabled(true) }
it { is_expected.to contain_class('puppet::agent::service::daemon').with_enabled(false) }
it { is_expected.to contain_class('puppet::agent::service::systemd').with_enabled(false) }
it { is_expected.not_to contain_service('puppet-run.timer') }
it do
is_expected.to contain_cron('puppet')
.with_command("#{bindir}/puppet agent --config #{confdir}/puppet.conf --onetime --no-daemonize")
.with_user('root')
.with_minute('1')
.with_hour('22')
end
end
end

describe 'when runmode => systemd.timer' do
let :params do
super().merge(runmode: 'systemd.timer')
Expand Down Expand Up @@ -285,6 +331,55 @@
end
end

describe 'when runmode => systemd.timer with configured time' do
let :params do
super().merge(runmode: 'systemd.timer',
run_hour: 22,
run_minute: 01
)
end

case os
when /\Adebian-/, /\A(redhat|centos|scientific)-7/, /\Afedora-/, /\Aubuntu-(16|18)/, /\Aarchlinux-/
it { is_expected.to compile.with_all_deps }
it { is_expected.to contain_class('puppet::agent::service::daemon').with_enabled(false) }
it { is_expected.to contain_class('puppet::agent::service::cron').with_enabled(false) }
it { is_expected.to contain_class('puppet::agent::service::systemd').with_enabled(true) }
it { is_expected.to contain_service('puppet-run.timer').with_ensure(:running) }

it do
is_expected.to contain_file('/etc/systemd/system/puppet-run.timer')
.with_content(/.*OnCalendar\=\*-\*-\* 22:1:00.*/)
end

it do
is_expected.to contain_file('/etc/systemd/system/puppet-run.timer')
.with_content(/^RandomizedDelaySec\=0$/)
end

it do
is_expected.to contain_file('/etc/systemd/system/puppet-run.service')
.with_content(%r{^ExecStart=#{bindir}/puppet agent --config #{confdir}/puppet.conf --onetime --no-daemonize --detailed-exitcode --no-usecacheonfailure$})
end

it do
is_expected.to contain_exec('systemctl-daemon-reload-puppet')
.with_refreshonly(true)
.with_command('systemctl daemon-reload')
end

it do
is_expected.to contain_service('puppet-run.timer')
.with_provider('systemd')
.with_ensure('running')
.with_name('puppet-run.timer')
.with_enable('true')
end
else
it { is_expected.to raise_error(Puppet::Error, /Runmode of systemd.timer not supported on #{facts[:kernel]} operating systems!/) }
end
end

describe 'when runmode => none' do
let :params do
super().merge(runmode: 'none')
Expand Down
2 changes: 1 addition & 1 deletion templates/agent/systemd.puppet-run.timer.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
Description=Systemd Timer for Puppet Agent

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

Expand Down

0 comments on commit c6d90ab

Please sign in to comment.