Skip to content

Commit

Permalink
Revert "Revert "Convert the systemd timer service to camptocamp/syste…
Browse files Browse the repository at this point in the history
…md""

This reverts commit 904e51b.

Initially it was reverted since it led to a dependency cycle in the
Foreman installer. This was caused by a systemctl daemon-reload. Since
this was a bit of an edge case and not all users will hit this, it's
reinstated.

The cyclic dependency can also be mitigated by using camptocamp/systemd
3.x which no longer performs the exec. This only works on Puppet 6.1 or
newer though.
  • Loading branch information
ekohl committed May 27, 2021
1 parent 604d87e commit bd359f0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 92 deletions.
79 changes: 16 additions & 63 deletions manifests/agent/service/systemd.pp
Original file line number Diff line number Diff line change
Expand Up @@ -6,69 +6,22 @@
Optional[Integer[0,59]] $minute = undef,
) {
unless $puppet::runmode == 'unmanaged' or 'systemd.timer' in $puppet::unavailable_runmodes {
exec { 'systemctl-daemon-reload-puppet':
refreshonly => true,
path => $::path,
command => 'systemctl daemon-reload',
}

if $enabled {
# 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,
}

$randomizeddelaysec = $puppet::systemd_randomizeddelaysec

file { "/etc/systemd/system/${puppet::systemd_unit_name}.timer":
content => template('puppet/agent/systemd.puppet-run.timer.erb'),
notify => [
Exec['systemctl-daemon-reload-puppet'],
Service['puppet-run.timer'],
],
}

file { "/etc/systemd/system/${puppet::systemd_unit_name}.service":
content => template('puppet/agent/systemd.puppet-run.service.erb'),
notify => Exec['systemctl-daemon-reload-puppet'],
}

service { 'puppet-run.timer':
ensure => running,
provider => 'systemd',
name => "${puppet::systemd_unit_name}.timer",
enable => true,
require => Exec['systemctl-daemon-reload-puppet'],
}
} else {
# Reverse order - stop, delete files, exec
service { 'puppet-run.timer':
ensure => stopped,
provider => 'systemd',
name => "${puppet::systemd_unit_name}.timer",
enable => false,
before => [
File["/etc/systemd/system/${puppet::systemd_unit_name}.timer"],
File["/etc/systemd/system/${puppet::systemd_unit_name}.service"],
],
}

file { "/etc/systemd/system/${puppet::systemd_unit_name}.timer":
ensure => absent,
notify => Exec['systemctl-daemon-reload-puppet'],
}

file { "/etc/systemd/system/${puppet::systemd_unit_name}.service":
ensure => absent,
notify => Exec['systemctl-daemon-reload-puppet'],
}
# 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 = pick($puppet::systemd_cmd, "${puppet::puppet_cmd} agent --config ${puppet::dir}/puppet.conf --onetime --no-daemonize --detailed-exitcode --no-usecacheonfailure")
$randomizeddelaysec = $puppet::systemd_randomizeddelaysec

systemd::timer { "${puppet::systemd_unit_name}.timer":
ensure => bool2str($enabled, 'present', 'absent'),
active => $enabled,
enable => $enabled,
timer_content => template('puppet/agent/systemd.puppet-run.timer.erb'),
service_content => template('puppet/agent/systemd.puppet-run.service.erb'),
}
}
}
39 changes: 10 additions & 29 deletions spec/classes/puppet_agent_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,9 @@
it { is_expected.to contain_class('puppet::agent::service::systemd').with_enabled(false) }
case os
when /\Adebian-/, /\A(redhat|centos|scientific)-(7|8)/, /\Afedora-/, /\Aubuntu-/, /\Aarchlinux-/
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_ensure(:stopped)
.with_ensure(false)
.with_provider('systemd')
.with_name('puppet-run.timer')
.with_enable(false)
Expand All @@ -126,7 +120,6 @@
it { is_expected.not_to contain_service('puppet-run.timer') }
it { is_expected.not_to contain_file('/etc/systemd/system/puppet-run.timer') }
it { is_expected.not_to contain_file('/etc/systemd/system/puppet-run.service') }
it { is_expected.not_to contain_exec('systemctl-daemon-reload-puppet') }
end
end

Expand Down Expand Up @@ -213,7 +206,7 @@
.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 { is_expected.to contain_service('puppet-run.timer').with_ensure(false) }
it do
is_expected.to contain_cron('puppet')
.with_command("#{bindir}/puppet agent --config #{confdir}/puppet.conf --onetime --no-daemonize")
Expand Down Expand Up @@ -259,7 +252,7 @@
.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 { is_expected.to contain_service('puppet-run.timer').with_ensure(false) }
it do
is_expected.to contain_cron('puppet')
.with_command("#{bindir}/puppet agent --config #{confdir}/puppet.conf --onetime --no-daemonize")
Expand Down Expand Up @@ -294,7 +287,7 @@
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 { is_expected.to contain_service('puppet-run.timer').with_ensure(true) }

it do
is_expected.to contain_file('/etc/systemd/system/puppet-run.timer')
Expand All @@ -311,18 +304,12 @@
.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_ensure(true)
.with_name('puppet-run.timer')
.with_enable('true')
.with_enable(true)
end
else
it { is_expected.to raise_error(Puppet::Error, /Runmode of systemd.timer not supported on #{facts[:kernel]} operating systems!/) }
Expand All @@ -343,7 +330,7 @@
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 { is_expected.to contain_service('puppet-run.timer').with_ensure(true) }

it do
is_expected.to contain_file('/etc/systemd/system/puppet-run.timer')
Expand All @@ -360,18 +347,12 @@
.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_ensure(true)
.with_name('puppet-run.timer')
.with_enable('true')
.with_enable(true)
end
else
it { is_expected.to raise_error(Puppet::Error, /Runmode of systemd.timer not supported on #{facts[:kernel]} operating systems!/) }
Expand All @@ -393,7 +374,7 @@

case os
when /\Adebian-/, /\A(redhat|centos|scientific)-(7|8)/, /\Afedora-/, /\Aubuntu-/, /\Aarchlinux-/
it { is_expected.to contain_service('puppet-run.timer').with_ensure(:stopped) }
it { is_expected.to contain_service('puppet-run.timer').with_ensure(false) }
else
it { is_expected.not_to contain_service('puppet-run.timer') }
end
Expand Down

0 comments on commit bd359f0

Please sign in to comment.