Skip to content

Commit d4af1d8

Browse files
authored
Merge pull request #1396 from SimonHoenscheid/shoenscheid_systemd_cleanup
Cleanup Systemd drop-in file handling
2 parents 42fe0c4 + 5eb1e8e commit d4af1d8

File tree

10 files changed

+75
-123
lines changed

10 files changed

+75
-123
lines changed

.fixtures.yml

+1
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ fixtures:
1010
puppet_agent: 'https://github.com/puppetlabs/puppetlabs-puppet_agent.git'
1111
stdlib: "https://github.com/puppetlabs/puppetlabs-stdlib.git"
1212
yumrepo_core: "https://github.com/puppetlabs/puppetlabs-yumrepo_core.git"
13+
systemd: "https://github.com/voxpupuli/puppet-systemd.git"

manifests/params.pp

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
$package_ensure = 'present'
2727
$module_workdir = pick($module_workdir,'/tmp')
2828
$password_encryption = undef
29-
$extra_systemd_config = ''
29+
$extra_systemd_config = undef
3030
$manage_datadir = true
3131
$manage_logdir = true
3232
$manage_xlogdir = true

manifests/server/instance/config.pp

+6-50
Original file line numberDiff line numberDiff line change
@@ -245,58 +245,14 @@
245245
}
246246
}
247247
# lint:ignore:140chars
248-
# 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.
249-
# Gentoo also supports drop-in files.
248+
# RHEL 7 and 8 both support drop-in files for systemd units. Gentoo also supports drop-in files.
249+
# 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.
250250
# lint:endignore:140chars
251251
if $facts['os']['family'] in ['RedHat', 'Gentoo'] and $facts['service_provider'] == 'systemd' {
252-
# While Puppet 6.1 and newer can do a daemon-reload if needed, systemd
253-
# doesn't appear to report that correctly in all cases.
254-
# One such case seems to be when an overriding unit file is removed from /etc
255-
# and the original one from /lib *should* be used again.
256-
#
257-
# This can be removed when Puppet < 6.1 support is dropped *and* the file
258-
# old-systemd-override is removed.
259-
$systemd_command = ['systemctl', 'daemon-reload']
260-
exec { 'restart-systemd':
261-
command => $systemd_command,
262-
refreshonly => true,
263-
path => '/bin:/usr/bin:/usr/local/bin',
264-
before => Class['postgresql::server::service'],
265-
}
266-
267-
file {
268-
default:
269-
ensure => file,
270-
owner => root,
271-
group => root,
272-
notify => [Exec['restart-systemd'], Class['postgresql::server::service']],
273-
before => Class['postgresql::server::reload'];
274-
275-
'systemd-conf-dir':
276-
ensure => directory,
277-
path => "/etc/systemd/system/${service_name}.service.d";
278-
279-
# Template uses:
280-
# - $facts['os']['name']
281-
# - $facts['os']['release']['major']
282-
# - $service_name
283-
# - $port
284-
# - $datadir
285-
# - $extra_systemd_config
286-
'systemd-override':
287-
path => "/etc/systemd/system/${service_name}.service.d/${service_name}.conf",
288-
content => template('postgresql/systemd-override.erb'),
289-
require => File['systemd-conf-dir'];
290-
}
291-
292-
if $service_enable != 'mask' {
293-
# Remove old unit file to avoid conflicts
294-
file { 'old-systemd-override':
295-
ensure => absent,
296-
path => "/etc/systemd/system/${service_name}.service",
297-
notify => [Exec['restart-systemd'], Class['postgresql::server::service']],
298-
before => Class['postgresql::server::reload'],
299-
}
252+
postgresql::server::instance::systemd { $service_name:
253+
port => $port,
254+
datadir => $datadir,
255+
extra_systemd_config => $extra_systemd_config,
300256
}
301257
}
302258
}

manifests/server/instance/systemd.pp

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# @summary This define handles systemd drop-in files for the postgres main instance (default) or additional instances
2+
# @param service_name Overrides the default PostgreSQL service name.
3+
# @param drop_in_ensure sets the Systemd drop-in file to present or absent
4+
# @api private
5+
define postgresql::server::instance::systemd (
6+
Variant[String[1], Stdlib::Port] $port,
7+
Stdlib::Absolutepath $datadir,
8+
Optional[String[1]] $extra_systemd_config = undef,
9+
String[1] $service_name = $name,
10+
Enum[present, absent] $drop_in_ensure = 'present',
11+
12+
) {
13+
# Template uses:
14+
# - $port
15+
# - $datadir
16+
# - $extra_systemd_config
17+
systemd::dropin_file { "${service_name}.conf":
18+
ensure => $drop_in_ensure,
19+
unit => "${service_name}.service",
20+
owner => 'root',
21+
group => 'root',
22+
content => template('postgresql/systemd-override.erb'),
23+
notify => Class['postgresql::server::service'],
24+
before => Class['postgresql::server::reload'],
25+
}
26+
}

metadata.json

+5-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
"name": "puppetlabs/apt",
1717
"version_requirement": ">= 2.0.0 < 10.0.0"
1818
},
19+
{
20+
"name": "puppet/systemd",
21+
"version_requirement": ">= 4.0.1 < 5.0.0"
22+
},
1923
{
2024
"name": "puppetlabs/concat",
2125
"version_requirement": ">= 4.1.0 < 8.0.0"
@@ -87,7 +91,7 @@
8791
"requirements": [
8892
{
8993
"name": "puppet",
90-
"version_requirement": ">= 6.0.0 < 8.0.0"
94+
"version_requirement": ">= 6.24.0 < 8.0.0"
9195
}
9296
],
9397
"pdk-version": "2.5.0",

spec/classes/server/config_spec.rb

+29-62
Original file line numberDiff line numberDiff line change
@@ -19,29 +19,22 @@
1919
.that_requires('Package[policycoreutils-python]')
2020
end
2121

22-
it 'removes the old systemd-override file' do
23-
is_expected.to contain_file('old-systemd-override')
24-
.with(ensure: 'absent', path: '/etc/systemd/system/postgresql.service')
25-
end
26-
2722
it 'has the correct systemd-override drop file' do
28-
is_expected.to contain_file('systemd-override').with(
29-
ensure: 'file', path: '/etc/systemd/system/postgresql.service.d/postgresql.conf',
30-
owner: 'root', group: 'root'
31-
) .that_requires('File[systemd-conf-dir]')
23+
is_expected.to contain_file('/etc/systemd/system/postgresql.service.d/postgresql.conf').with(
24+
ensure: 'file', owner: 'root', group: 'root',
25+
) .that_requires('File[/etc/systemd/system/postgresql.service.d]')
3226
end
3327

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

3932
context 'RHEL 7 host with Puppet 5' do
4033
include_examples 'RedHat 7'
4134

4235
it 'has systemctl restart command' do
43-
is_expected.to contain_exec('restart-systemd').with(
44-
command: ['systemctl', 'daemon-reload'],
36+
is_expected.to contain_exec('systemd-postgresql.service-systemctl-daemon-reload').with(
37+
command: 'systemctl daemon-reload',
4538
refreshonly: true,
4639
path: '/bin:/usr/bin:/usr/local/bin',
4740
)
@@ -53,21 +46,20 @@
5346
<<-EOS
5447
class { 'postgresql::globals':
5548
manage_package_repo => true,
56-
version => '9.4',
49+
version => '10',
5750
}->
5851
class { 'postgresql::server': }
5952
EOS
6053
end
6154

6255
it 'has the correct systemd-override file' do
63-
is_expected.to contain_file('systemd-override').with(
64-
ensure: 'file', path: '/etc/systemd/system/postgresql-9.4.service.d/postgresql-9.4.conf',
65-
owner: 'root', group: 'root'
56+
is_expected.to contain_file('/etc/systemd/system/postgresql-10.service.d/postgresql-10.conf').with(
57+
ensure: 'file', owner: 'root', group: 'root',
6658
)
6759
end
6860

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

87-
it 'removes the old systemd-override file' do
88-
is_expected.to contain_file('old-systemd-override')
89-
.with(ensure: 'absent', path: '/etc/systemd/system/postgresql.service')
90-
end
91-
9279
it 'has the correct systemd-override drop file' do
93-
is_expected.to contain_file('systemd-override').with(
94-
ensure: 'file', path: '/etc/systemd/system/postgresql.service.d/postgresql.conf',
95-
owner: 'root', group: 'root'
96-
) .that_requires('File[systemd-conf-dir]')
80+
is_expected.to contain_file('/etc/systemd/system/postgresql.service.d/postgresql.conf').with(
81+
ensure: 'file', owner: 'root', group: 'root',
82+
) .that_requires('File[/etc/systemd/system/postgresql.service.d]')
9783
end
9884

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

10389
describe 'with manage_package_repo => true and a version' do
10490
let(:pre_condition) do
10591
<<-EOS
10692
class { 'postgresql::globals':
10793
manage_package_repo => true,
108-
version => '9.4',
94+
version => '14',
10995
}->
11096
class { 'postgresql::server': }
11197
EOS
11298
end
11399

114100
it 'has the correct systemd-override file' do
115-
is_expected.to contain_file('systemd-override').with(
116-
ensure: 'file', path: '/etc/systemd/system/postgresql-9.4.service.d/postgresql-9.4.conf',
117-
owner: 'root', group: 'root'
101+
is_expected.to contain_file('/etc/systemd/system/postgresql-14.service.d/postgresql-14.conf').with(
102+
ensure: 'file', owner: 'root', group: 'root',
118103
)
119104
end
120105
it 'has the correct systemd-override file #regex' do
121-
is_expected.to contain_file('systemd-override') .without_content(%r{\.include})
106+
is_expected.to contain_file('/etc/systemd/system/postgresql-14.service.d/postgresql-14.conf') .without_content(%r{\.include})
122107
end
123108
end
124109
end
@@ -135,20 +120,14 @@ class { 'postgresql::server': }
135120
.that_requires('Package[policycoreutils-python-utils]')
136121
end
137122

138-
it 'removes the old systemd-override file' do
139-
is_expected.to contain_file('old-systemd-override')
140-
.with(ensure: 'absent', path: '/etc/systemd/system/postgresql.service')
141-
end
142-
143123
it 'has the correct systemd-override drop file' do
144-
is_expected.to contain_file('systemd-override').with(
145-
ensure: 'file', path: '/etc/systemd/system/postgresql.service.d/postgresql.conf',
146-
owner: 'root', group: 'root'
147-
) .that_requires('File[systemd-conf-dir]')
124+
is_expected.to contain_file('/etc/systemd/system/postgresql.service.d/postgresql.conf').with(
125+
ensure: 'file', owner: 'root', group: 'root',
126+
) .that_requires('File[/etc/systemd/system/postgresql.service.d]')
148127
end
149128

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

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

165144
it 'has the correct systemd-override file' do
166-
is_expected.to contain_file('systemd-override').with(
167-
ensure: 'file', path: '/etc/systemd/system/postgresql-13.service.d/postgresql-13.conf',
168-
owner: 'root', group: 'root'
145+
is_expected.to contain_file('/etc/systemd/system/postgresql-13.service.d/postgresql-13.conf').with(
146+
ensure: 'file', owner: 'root', group: 'root',
169147
)
170148
end
171149

172150
it 'has the correct systemd-override file #regex' do
173-
is_expected.to contain_file('systemd-override') .without_content(%r{\.include})
151+
is_expected.to contain_file('/etc/systemd/system/postgresql-13.service.d/postgresql-13.conf') .without_content(%r{\.include})
174152
end
175153
end
176154
end
@@ -193,7 +171,7 @@ class { 'postgresql::server': }
193171
let(:pre_condition) do
194172
<<-EOS
195173
class { 'postgresql::globals':
196-
version => '9.5',
174+
version => '14',
197175
}->
198176
class { 'postgresql::server':
199177
manage_pg_hba_conf => true,
@@ -221,7 +199,7 @@ class { 'postgresql::server':
221199
let(:pre_condition) do
222200
<<-EOS
223201
class { 'postgresql::globals':
224-
version => '9.5',
202+
version => '14',
225203
}->
226204
class { 'postgresql::server': }
227205
EOS
@@ -231,22 +209,11 @@ class { 'postgresql::server': }
231209
is_expected.not_to contain_exec('/usr/sbin/semanage port -a -t postgresql_port_t -p tcp 5432')
232210
end
233211

234-
it 'removes the old systemd-override file' do
235-
is_expected.to contain_file('old-systemd-override')
236-
.with(ensure: 'absent', path: '/etc/systemd/system/postgresql-9.5.service')
237-
end
238-
239212
it 'has the correct systemd-override drop file' do
240-
is_expected.to contain_file('systemd-override').with(
241-
ensure: 'file', path: '/etc/systemd/system/postgresql-9.5.service.d/postgresql-9.5.conf',
242-
owner: 'root', group: 'root'
213+
is_expected.to contain_file('/etc/systemd/system/postgresql-14.service.d/postgresql-14.conf').with(
214+
ensure: 'file', owner: 'root', group: 'root',
243215
)
244216
end
245-
246-
it 'has the correct systemd-override file #regex' do
247-
is_expected.to contain_file('systemd-override') \
248-
.with_content(%r{(?!^.include)})
249-
end
250217
end
251218
end
252219
end

spec/classes/server_spec.rb

+5-5
Original file line numberDiff line numberDiff line change
@@ -187,17 +187,17 @@ class { 'postgresql::globals':
187187
<<-EOS
188188
class { 'postgresql::globals':
189189
manage_package_repo => true,
190-
version => '99.5',
190+
version => '14',
191191
before => Class['postgresql::server'],
192192
}
193193
EOS
194194
end
195195

196196
it 'contains the correct package version' do
197-
is_expected.to contain_class('postgresql::repo').with_version('99.5')
198-
is_expected.to contain_file('/var/lib/postgresql/99.5/main') # FIXME: be more precise
199-
is_expected.to contain_concat('/etc/postgresql/99.5/main/pg_hba.conf') # FIXME: be more precise
200-
is_expected.to contain_concat('/etc/postgresql/99.5/main/pg_ident.conf') # FIXME: be more precise
197+
is_expected.to contain_class('postgresql::repo').with_version('14')
198+
is_expected.to contain_file('/var/lib/postgresql/14/main') # FIXME: be more precise
199+
is_expected.to contain_concat('/etc/postgresql/14/main/pg_hba.conf') # FIXME: be more precise
200+
is_expected.to contain_concat('/etc/postgresql/14/main/pg_ident.conf') # FIXME: be more precise
201201
end
202202
end
203203

spec/default_facts.yml

+1
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ ipaddress: "172.16.254.254"
66
ipaddress6: "FE80:0000:0000:0000:AAAA:AAAA:AAAA"
77
is_pe: false
88
macaddress: "AA:AA:AA:AA:AA:AA"
9+
path: '/bin:/usr/bin:/usr/local/bin'

spec/defines/server/config_entry_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
include_examples 'RedHat 7'
2929

3030
it 'stops postgresql and changes the port #file' do
31-
is_expected.to contain_file('systemd-override')
31+
is_expected.to contain_file('/etc/systemd/system/postgresql.service.d/postgresql.conf')
3232
end
3333
end
3434
end

templates/systemd-override.erb

-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
<%- if @os['name'] == 'Fedora' and @os['release']['major'] <= '31' -%>
2-
.include /usr/lib/systemd/system/<%= @service_name %>.service
3-
<% end -%>
41
[Service]
52
Environment=PGPORT=<%= @port %>
63
<%- if @os['family'] == 'Gentoo' -%>

0 commit comments

Comments
 (0)