From 5e22f695c771e523a9edabed0f452e6cf12c52ee Mon Sep 17 00:00:00 2001 From: Jerome Charaoui Date: Thu, 8 Mar 2018 12:22:59 -0500 Subject: [PATCH 1/5] Add systemd_randomizeddelaysec Allow configuration of the RandomizedDelaySec parameter of the systemd timer, which allows randomizing Puppet runs in a way that lets the administrator see exact moment of the next Puppet run. There's no incompatibility between Puppet's internal splay and this, but both probably shouldn't be used together. --- manifests/agent/service/systemd.pp | 2 ++ manifests/init.pp | 4 ++++ manifests/params.pp | 2 ++ templates/agent/systemd.puppet-run.timer.erb | 1 + 4 files changed, 9 insertions(+) diff --git a/manifests/agent/service/systemd.pp b/manifests/agent/service/systemd.pp index e5800af2..b1fb8dd3 100644 --- a/manifests/agent/service/systemd.pp +++ b/manifests/agent/service/systemd.pp @@ -18,6 +18,8 @@ default => $::puppet::systemd_cmd, } + $randomizeddelaysec = $::puppet::systemd_randomizeddelaysec + file { "/etc/systemd/system/${::puppet::systemd_unit_name}.timer": content => template('puppet/agent/systemd.puppet-run.timer.erb'), notify => [ diff --git a/manifests/init.pp b/manifests/init.pp index 2118e7da..6007c220 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -113,6 +113,9 @@ # $systemd_cmd:: Specify command to launch when runmode is # set 'systemd.timer'. # +# $systemd_randomizeddelaysec:: Adds a random delay between 0 and this value +# (in seconds) to the timer. +# # $show_diff:: Show and report changed files with diff output # # $module_repository:: Use a different puppet module repository @@ -581,6 +584,7 @@ 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, + Optional[Integer[0]] $systemd_randomizeddelaysec = $puppet::params::systemd_randomizeddelaysec, Boolean $agent_noop = $puppet::params::agent_noop, Boolean $show_diff = $puppet::params::show_diff, Optional[Stdlib::HTTPUrl] $module_repository = $puppet::params::module_repository, diff --git a/manifests/params.pp b/manifests/params.pp index a6896dcc..258ededa 100644 --- a/manifests/params.pp +++ b/manifests/params.pp @@ -52,6 +52,8 @@ $aio_package = ($::osfamily == 'Windows' or $::rubysitedir =~ /\/opt\/puppetlabs\/puppet/) $deb_naio_package = ($::osfamily == 'Debian') + $systemd_randomizeddelaysec = 0 + case $::osfamily { 'Windows' : { # Windows prefixes normal paths with the Data Directory's path and leaves 'puppet' off the end diff --git a/templates/agent/systemd.puppet-run.timer.erb b/templates/agent/systemd.puppet-run.timer.erb index 33fd3ce1..794f7652 100644 --- a/templates/agent/systemd.puppet-run.timer.erb +++ b/templates/agent/systemd.puppet-run.timer.erb @@ -4,6 +4,7 @@ Description=Systemd Timer for Puppet Agent [Timer] OnCalendar=*-*-* <%= Array(@times[0]).join(',') %>:<%= Array(@times[1]).join(',') %>:00 Persistent=true +RandomizedDelaySec=<%= @randomizeddelaysec %> [Install] WantedBy=timers.target From b5ac37ef4bf5b10c8c3c6ef148cd8155f286f8ce Mon Sep 17 00:00:00 2001 From: Jerome Charaoui Date: Mon, 12 Mar 2018 12:10:11 -0400 Subject: [PATCH 2/5] Parameter systemd_randomizeddelaysec not optional Undef isn't handled, and 0 is default anyways --- manifests/init.pp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/manifests/init.pp b/manifests/init.pp index 6007c220..f4508222 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -584,7 +584,7 @@ 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, - Optional[Integer[0]] $systemd_randomizeddelaysec = $puppet::params::systemd_randomizeddelaysec, + Integer[0] $systemd_randomizeddelaysec = $puppet::params::systemd_randomizeddelaysec, Boolean $agent_noop = $puppet::params::agent_noop, Boolean $show_diff = $puppet::params::show_diff, Optional[Stdlib::HTTPUrl] $module_repository = $puppet::params::module_repository, From adbcf366129876d58892688e56ddd93e47fe5a5d Mon Sep 17 00:00:00 2001 From: Jerome Charaoui Date: Mon, 12 Mar 2018 12:11:55 -0400 Subject: [PATCH 3/5] Specify $systemd_randomizeddelaysec context --- manifests/init.pp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/manifests/init.pp b/manifests/init.pp index f4508222..161c3ae5 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -114,7 +114,8 @@ # set 'systemd.timer'. # # $systemd_randomizeddelaysec:: Adds a random delay between 0 and this value -# (in seconds) to the timer. +# (in seconds) to the timer. Only relevant when +# runmode is 'systemd.timer'. # # $show_diff:: Show and report changed files with diff output # From 8b7febfd6d66311093f8e61852e7029c35ed6a8a Mon Sep 17 00:00:00 2001 From: Jerome Charaoui Date: Mon, 12 Mar 2018 12:13:41 -0400 Subject: [PATCH 4/5] Add test for $systemd_randomizeddelaysec --- spec/classes/puppet_agent_service_systemd_spec.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/spec/classes/puppet_agent_service_systemd_spec.rb b/spec/classes/puppet_agent_service_systemd_spec.rb index bc32859c..03f59fb1 100644 --- a/spec/classes/puppet_agent_service_systemd_spec.rb +++ b/spec/classes/puppet_agent_service_systemd_spec.rb @@ -68,6 +68,9 @@ should contain_file('/etc/systemd/system/puppet-run.timer'). with_content(/.*OnCalendar\=\*-\*-\* \*\:10,40:00.*/) + should contain_file('/etc/systemd/system/puppet-run.timer'). + with_content(/.*RandomizedDelaySec\=0.*/) + should contain_file('/etc/systemd/system/puppet-run.service'). with_content(/.*ExecStart=#{bindir}\/puppet agent --config #{confdir}\/puppet.conf --onetime --no-daemonize.*/) From 39e4c9767f9f8ae6a6b545ddeb579c11b95f358c Mon Sep 17 00:00:00 2001 From: Jerome Charaoui Date: Mon, 12 Mar 2018 13:10:58 -0400 Subject: [PATCH 5/5] Make test a bit more strict --- spec/classes/puppet_agent_service_systemd_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/classes/puppet_agent_service_systemd_spec.rb b/spec/classes/puppet_agent_service_systemd_spec.rb index 03f59fb1..2df23f44 100644 --- a/spec/classes/puppet_agent_service_systemd_spec.rb +++ b/spec/classes/puppet_agent_service_systemd_spec.rb @@ -69,7 +69,7 @@ with_content(/.*OnCalendar\=\*-\*-\* \*\:10,40:00.*/) should contain_file('/etc/systemd/system/puppet-run.timer'). - with_content(/.*RandomizedDelaySec\=0.*/) + with_content(/^RandomizedDelaySec\=0$/) should contain_file('/etc/systemd/system/puppet-run.service'). with_content(/.*ExecStart=#{bindir}\/puppet agent --config #{confdir}\/puppet.conf --onetime --no-daemonize.*/)