Skip to content

Cleanup Systemd drop-in file handling #1396

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

Merged
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
1 change: 1 addition & 0 deletions .fixtures.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ fixtures:
puppet_agent: 'https://github.com/puppetlabs/puppetlabs-puppet_agent.git'
stdlib: "https://github.com/puppetlabs/puppetlabs-stdlib.git"
yumrepo_core: "https://github.com/puppetlabs/puppetlabs-yumrepo_core.git"
systemd: "https://github.com/voxpupuli/puppet-systemd.git"
2 changes: 1 addition & 1 deletion manifests/params.pp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
$package_ensure = 'present'
$module_workdir = pick($module_workdir,'/tmp')
$password_encryption = undef
$extra_systemd_config = ''
$extra_systemd_config = undef
$manage_datadir = true
$manage_logdir = true
$manage_xlogdir = true
Expand Down
56 changes: 6 additions & 50 deletions manifests/server/instance/config.pp
Original file line number Diff line number Diff line change
Expand Up @@ -245,58 +245,14 @@
}
}
# lint:ignore:140chars
# RHEL 7 and 8 both support drop-in files for systemd units. The old include directive is deprecated and may be removed in future systemd releases.
# Gentoo also supports drop-in files.
# RHEL 7 and 8 both support drop-in files for systemd units. Gentoo also supports drop-in files.
# Edit 02/2023 RHEL basedc Systems and Gentoo need Variables set for $PGPORT, $DATA_DIR or $PGDATA, thats what the drop-in file is for.
# lint:endignore:140chars
if $facts['os']['family'] in ['RedHat', 'Gentoo'] and $facts['service_provider'] == 'systemd' {
# While Puppet 6.1 and newer can do a daemon-reload if needed, systemd
# doesn't appear to report that correctly in all cases.
# One such case seems to be when an overriding unit file is removed from /etc
# and the original one from /lib *should* be used again.
#
# This can be removed when Puppet < 6.1 support is dropped *and* the file
# old-systemd-override is removed.
$systemd_command = ['systemctl', 'daemon-reload']
exec { 'restart-systemd':
command => $systemd_command,
refreshonly => true,
path => '/bin:/usr/bin:/usr/local/bin',
before => Class['postgresql::server::service'],
}

file {
default:
ensure => file,
owner => root,
group => root,
notify => [Exec['restart-systemd'], Class['postgresql::server::service']],
before => Class['postgresql::server::reload'];

'systemd-conf-dir':
ensure => directory,
path => "/etc/systemd/system/${service_name}.service.d";

# Template uses:
# - $facts['os']['name']
# - $facts['os']['release']['major']
# - $service_name
# - $port
# - $datadir
# - $extra_systemd_config
'systemd-override':
path => "/etc/systemd/system/${service_name}.service.d/${service_name}.conf",
content => template('postgresql/systemd-override.erb'),
require => File['systemd-conf-dir'];
}

if $service_enable != 'mask' {
# Remove old unit file to avoid conflicts
file { 'old-systemd-override':
ensure => absent,
path => "/etc/systemd/system/${service_name}.service",
notify => [Exec['restart-systemd'], Class['postgresql::server::service']],
before => Class['postgresql::server::reload'],
}
postgresql::server::instance::systemd { $service_name:
port => $port,
datadir => $datadir,
extra_systemd_config => $extra_systemd_config,
}
}
}
26 changes: 26 additions & 0 deletions manifests/server/instance/systemd.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# @summary This define handles systemd drop-in files for the postgres main instance (default) or additional instances
# @param service_name Overrides the default PostgreSQL service name.
# @param drop_in_ensure sets the Systemd drop-in file to present or absent
# @api private
define postgresql::server::instance::systemd (
Variant[String[1], Stdlib::Port] $port,
Stdlib::Absolutepath $datadir,
Optional[String[1]] $extra_systemd_config = undef,
String[1] $service_name = $name,
Enum[present, absent] $drop_in_ensure = 'present',

) {
# Template uses:
# - $port
# - $datadir
# - $extra_systemd_config
systemd::dropin_file { "${service_name}.conf":
ensure => $drop_in_ensure,
unit => "${service_name}.service",
owner => 'root',
group => 'root',
content => template('postgresql/systemd-override.erb'),
notify => Class['postgresql::server::service'],
before => Class['postgresql::server::reload'],
}
}
6 changes: 5 additions & 1 deletion metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
"name": "puppetlabs/apt",
"version_requirement": ">= 2.0.0 < 10.0.0"
},
{
"name": "puppet/systemd",
"version_requirement": ">= 4.0.1 < 5.0.0"
},
{
"name": "puppetlabs/concat",
"version_requirement": ">= 4.1.0 < 8.0.0"
Expand Down Expand Up @@ -87,7 +91,7 @@
"requirements": [
{
"name": "puppet",
"version_requirement": ">= 6.0.0 < 8.0.0"
"version_requirement": ">= 6.24.0 < 8.0.0"
}
],
"pdk-version": "2.5.0",
Expand Down
91 changes: 29 additions & 62 deletions spec/classes/server/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,22 @@
.that_requires('Package[policycoreutils-python]')
end

it 'removes the old systemd-override file' do
is_expected.to contain_file('old-systemd-override')
.with(ensure: 'absent', path: '/etc/systemd/system/postgresql.service')
end

it 'has the correct systemd-override drop file' do
is_expected.to contain_file('systemd-override').with(
ensure: 'file', path: '/etc/systemd/system/postgresql.service.d/postgresql.conf',
owner: 'root', group: 'root'
) .that_requires('File[systemd-conf-dir]')
is_expected.to contain_file('/etc/systemd/system/postgresql.service.d/postgresql.conf').with(
ensure: 'file', owner: 'root', group: 'root',
) .that_requires('File[/etc/systemd/system/postgresql.service.d]')
end

it 'has the correct systemd-override file #regex' do
is_expected.to contain_file('systemd-override') \
.with_content(%r{(?!^.include)})
is_expected.to contain_file('/etc/systemd/system/postgresql.service.d/postgresql.conf')
end

context 'RHEL 7 host with Puppet 5' do
include_examples 'RedHat 7'

it 'has systemctl restart command' do
is_expected.to contain_exec('restart-systemd').with(
command: ['systemctl', 'daemon-reload'],
is_expected.to contain_exec('systemd-postgresql.service-systemctl-daemon-reload').with(
command: 'systemctl daemon-reload',
refreshonly: true,
path: '/bin:/usr/bin:/usr/local/bin',
)
Expand All @@ -53,21 +46,20 @@
<<-EOS
class { 'postgresql::globals':
manage_package_repo => true,
version => '9.4',
version => '10',
}->
class { 'postgresql::server': }
EOS
end

it 'has the correct systemd-override file' do
is_expected.to contain_file('systemd-override').with(
ensure: 'file', path: '/etc/systemd/system/postgresql-9.4.service.d/postgresql-9.4.conf',
owner: 'root', group: 'root'
is_expected.to contain_file('/etc/systemd/system/postgresql-10.service.d/postgresql-10.conf').with(
ensure: 'file', owner: 'root', group: 'root',
)
end

it 'has the correct systemd-override file #regex' do
is_expected.to contain_file('systemd-override') .without_content(%r{\.include})
is_expected.to contain_file('/etc/systemd/system/postgresql-10.service.d/postgresql-10.conf') .without_content(%r{\.include})
end
end
end
Expand All @@ -84,41 +76,34 @@ class { 'postgresql::server': }
.that_requires('Package[policycoreutils-python-utils]')
end

it 'removes the old systemd-override file' do
is_expected.to contain_file('old-systemd-override')
.with(ensure: 'absent', path: '/etc/systemd/system/postgresql.service')
end

it 'has the correct systemd-override drop file' do
is_expected.to contain_file('systemd-override').with(
ensure: 'file', path: '/etc/systemd/system/postgresql.service.d/postgresql.conf',
owner: 'root', group: 'root'
) .that_requires('File[systemd-conf-dir]')
is_expected.to contain_file('/etc/systemd/system/postgresql.service.d/postgresql.conf').with(
ensure: 'file', owner: 'root', group: 'root',
) .that_requires('File[/etc/systemd/system/postgresql.service.d]')
end

it 'has the correct systemd-override file #regex' do
is_expected.to contain_file('systemd-override') .without_content(%r{\.include})
is_expected.to contain_file('/etc/systemd/system/postgresql.service.d/postgresql.conf').without_content(%r{\.include})
end

describe 'with manage_package_repo => true and a version' do
let(:pre_condition) do
<<-EOS
class { 'postgresql::globals':
manage_package_repo => true,
version => '9.4',
version => '14',
}->
class { 'postgresql::server': }
EOS
end

it 'has the correct systemd-override file' do
is_expected.to contain_file('systemd-override').with(
ensure: 'file', path: '/etc/systemd/system/postgresql-9.4.service.d/postgresql-9.4.conf',
owner: 'root', group: 'root'
is_expected.to contain_file('/etc/systemd/system/postgresql-14.service.d/postgresql-14.conf').with(
ensure: 'file', owner: 'root', group: 'root',
)
end
it 'has the correct systemd-override file #regex' do
is_expected.to contain_file('systemd-override') .without_content(%r{\.include})
is_expected.to contain_file('/etc/systemd/system/postgresql-14.service.d/postgresql-14.conf') .without_content(%r{\.include})
end
end
end
Expand All @@ -135,20 +120,14 @@ class { 'postgresql::server': }
.that_requires('Package[policycoreutils-python-utils]')
end

it 'removes the old systemd-override file' do
is_expected.to contain_file('old-systemd-override')
.with(ensure: 'absent', path: '/etc/systemd/system/postgresql.service')
end

it 'has the correct systemd-override drop file' do
is_expected.to contain_file('systemd-override').with(
ensure: 'file', path: '/etc/systemd/system/postgresql.service.d/postgresql.conf',
owner: 'root', group: 'root'
) .that_requires('File[systemd-conf-dir]')
is_expected.to contain_file('/etc/systemd/system/postgresql.service.d/postgresql.conf').with(
ensure: 'file', owner: 'root', group: 'root',
) .that_requires('File[/etc/systemd/system/postgresql.service.d]')
end

it 'has the correct systemd-override file #regex' do
is_expected.to contain_file('systemd-override') .without_content(%r{\.include})
is_expected.to contain_file('/etc/systemd/system/postgresql.service.d/postgresql.conf').without_content(%r{\.include})
end

describe 'with manage_package_repo => true and a version' do
Expand All @@ -163,14 +142,13 @@ class { 'postgresql::server': }
end

it 'has the correct systemd-override file' do
is_expected.to contain_file('systemd-override').with(
ensure: 'file', path: '/etc/systemd/system/postgresql-13.service.d/postgresql-13.conf',
owner: 'root', group: 'root'
is_expected.to contain_file('/etc/systemd/system/postgresql-13.service.d/postgresql-13.conf').with(
ensure: 'file', owner: 'root', group: 'root',
)
end

it 'has the correct systemd-override file #regex' do
is_expected.to contain_file('systemd-override') .without_content(%r{\.include})
is_expected.to contain_file('/etc/systemd/system/postgresql-13.service.d/postgresql-13.conf') .without_content(%r{\.include})
end
end
end
Expand All @@ -193,7 +171,7 @@ class { 'postgresql::server': }
let(:pre_condition) do
<<-EOS
class { 'postgresql::globals':
version => '9.5',
version => '14',
}->
class { 'postgresql::server':
manage_pg_hba_conf => true,
Expand Down Expand Up @@ -221,7 +199,7 @@ class { 'postgresql::server':
let(:pre_condition) do
<<-EOS
class { 'postgresql::globals':
version => '9.5',
version => '14',
}->
class { 'postgresql::server': }
EOS
Expand All @@ -231,22 +209,11 @@ class { 'postgresql::server': }
is_expected.not_to contain_exec('/usr/sbin/semanage port -a -t postgresql_port_t -p tcp 5432')
end

it 'removes the old systemd-override file' do
is_expected.to contain_file('old-systemd-override')
.with(ensure: 'absent', path: '/etc/systemd/system/postgresql-9.5.service')
end

it 'has the correct systemd-override drop file' do
is_expected.to contain_file('systemd-override').with(
ensure: 'file', path: '/etc/systemd/system/postgresql-9.5.service.d/postgresql-9.5.conf',
owner: 'root', group: 'root'
is_expected.to contain_file('/etc/systemd/system/postgresql-14.service.d/postgresql-14.conf').with(
ensure: 'file', owner: 'root', group: 'root',
)
end

it 'has the correct systemd-override file #regex' do
is_expected.to contain_file('systemd-override') \
.with_content(%r{(?!^.include)})
end
end
end
end
10 changes: 5 additions & 5 deletions spec/classes/server_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -187,17 +187,17 @@ class { 'postgresql::globals':
<<-EOS
class { 'postgresql::globals':
manage_package_repo => true,
version => '99.5',
version => '14',
before => Class['postgresql::server'],
}
EOS
end

it 'contains the correct package version' do
is_expected.to contain_class('postgresql::repo').with_version('99.5')
is_expected.to contain_file('/var/lib/postgresql/99.5/main') # FIXME: be more precise
is_expected.to contain_concat('/etc/postgresql/99.5/main/pg_hba.conf') # FIXME: be more precise
is_expected.to contain_concat('/etc/postgresql/99.5/main/pg_ident.conf') # FIXME: be more precise
is_expected.to contain_class('postgresql::repo').with_version('14')
is_expected.to contain_file('/var/lib/postgresql/14/main') # FIXME: be more precise
is_expected.to contain_concat('/etc/postgresql/14/main/pg_hba.conf') # FIXME: be more precise
is_expected.to contain_concat('/etc/postgresql/14/main/pg_ident.conf') # FIXME: be more precise
end
end

Expand Down
1 change: 1 addition & 0 deletions spec/default_facts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ ipaddress: "172.16.254.254"
ipaddress6: "FE80:0000:0000:0000:AAAA:AAAA:AAAA"
is_pe: false
macaddress: "AA:AA:AA:AA:AA:AA"
path: '/bin:/usr/bin:/usr/local/bin'
2 changes: 1 addition & 1 deletion spec/defines/server/config_entry_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
include_examples 'RedHat 7'

it 'stops postgresql and changes the port #file' do
is_expected.to contain_file('systemd-override')
is_expected.to contain_file('/etc/systemd/system/postgresql.service.d/postgresql.conf')
end
end
end
Expand Down
3 changes: 0 additions & 3 deletions templates/systemd-override.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
<%- if @os['name'] == 'Fedora' and @os['release']['major'] <= '31' -%>
.include /usr/lib/systemd/system/<%= @service_name %>.service
<% end -%>
[Service]
Environment=PGPORT=<%= @port %>
<%- if @os['family'] == 'Gentoo' -%>
Expand Down