Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow camptocamp/systemd 3.x + use it for timers #790

Merged
merged 2 commits into from
May 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'),
}
}
}
2 changes: 1 addition & 1 deletion metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
},
{
"name": "camptocamp/systemd",
"version_requirement": ">= 2.9.0 < 3.0.0"
"version_requirement": ">= 2.9.0 < 4.0.0"
}
],
"requirements": [
Expand Down
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