Skip to content

Commit 4f4cfb0

Browse files
author
Morgan Haskel
committed
Merge pull request puppetlabs#517 from cmurphy/fix_datadir
Fix data directory handling
2 parents e688274 + 6ba3179 commit 4f4cfb0

File tree

9 files changed

+99
-61
lines changed

9 files changed

+99
-61
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,9 @@ Path to your `postgresql.conf` file.
335335
If false, disables the defaults supplied with the module for `pg\_hba.conf`. This is useful if you disagree with the defaults and wish to override them yourself. Be sure that your changes of course align with the rest of the module, as some access is required to perform basic `psql` operations for example.
336336

337337
####`datadir`
338-
This setting can be used to override the default postgresql data directory for the target platform. If not specified, the module will use whatever directory is the default for your OS distro.
338+
This setting can be used to override the default postgresql data directory for the target platform. If not specified, the module will use whatever directory is the default for your OS distro. Please note that changing the datadir after installation will cause the server to come to a full stop before being able to make the change. For RedHat systems, the data directory must be labeled appropriately for SELinux. On Ubuntu, you need to explicitly set needs\_initdb to true in order to allow Puppet to initialize the database in the new datadir (needs\_initdb defaults to true on other systems).
339+
340+
Warning: If datadir is changed from the default, puppet will not manage purging of the original data directory, which will cause it to fail if the data directory is changed back to the original.
339341

340342
####`confdir`
341343
This setting can be used to override the default postgresql configuration directory for the target platform. If not specified, the module will use whatever directory is the default for your OS distro.
@@ -407,7 +409,7 @@ This setting is used to specify the name of the default database to connect with
407409
This value defaults to `localhost`, meaning the postgres server will only accept connections from localhost. If you'd like to be able to connect to postgres from remote machines, you can override this setting. A value of `*` will tell postgres to accept connections from any remote machine. Alternately, you can specify a comma-separated list of hostnames or IP addresses. (For more info, have a look at the `postgresql.conf` file from your system's postgres package).
408410

409411
####`port`
410-
This value defaults to `5432`, meaning the postgres server will listen on TCP port 5432. Note that the same port number is used for all IP addresses the server listens on.
412+
This value defaults to `5432`, meaning the postgres server will listen on TCP port 5432. Note that the same port number is used for all IP addresses the server listens on. Also note that for RedHat systems and early Debian systems, changing the port will cause the server to come to a full stop before being able to make the change.
411413

412414
####`ip_mask_deny_postgres_user`
413415
This value defaults to `0.0.0.0/0`. Sometimes it can be useful to block the superuser account from remote connections if you are allowing other database users to connect remotely. Set this to an IP and mask for which you want to deny connections by the postgres superuser account. So, e.g., the default value of `0.0.0.0/0` will match any remote IP and deny access, so the postgres user won't be able to connect remotely at all. Conversely, a value of `0.0.0.0/32` would not match any remote IP, and thus the deny rule will not be applied and the postgres user will be allowed to connect.

manifests/server/config.pp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,4 +136,23 @@
136136
notify => Class['postgresql::server::reload'],
137137
}
138138
}
139+
140+
if $::osfamily == 'RedHat' {
141+
if $::operatingsystemrelease =~ /^7/ or $::operatingsystem == 'Fedora' {
142+
file { 'systemd-override':
143+
ensure => present,
144+
path => '/etc/systemd/system/postgresql.service',
145+
owner => root,
146+
group => root,
147+
content => template('postgresql/systemd-override.erb'),
148+
notify => [ Exec['restart-systemd'], Class['postgresql::server::service'] ],
149+
before => Class['postgresql::server::reload'],
150+
}
151+
exec { "restart-systemd":
152+
command => 'systemctl daemon-reload',
153+
refreshonly => true,
154+
path => '/bin:/usr/bin:/usr/local/bin'
155+
}
156+
}
157+
}
139158
}

manifests/server/config_entry.pp

Lines changed: 35 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -30,47 +30,38 @@
3030
}
3131
}
3232

33-
# We have to handle ports in a weird and special way. On early Debian and
34-
# Ubuntu we have to ensure we stop the service completely. On Redhat we
35-
# either have to create a systemd override for the port or update the
36-
# sysconfig file.
33+
# We have to handle ports and the data directory in a weird and
34+
# special way. On early Debian and Ubuntu and RHEL we have to ensure
35+
# we stop the service completely. On RHEL 7 we either have to create
36+
# a systemd override for the port or update the sysconfig file, but this
37+
# is managed for us in postgresql::server::config.
3738
if $::operatingsystem == 'Debian' or $::operatingsystem == 'Ubuntu' {
38-
if $::operatingsystemrelease =~ /^6/ or $::operatingsystemrelease =~ /^10\.04/ {
39-
if $name == 'port' {
40-
exec { 'postgresql_stop':
39+
if $name == 'port' and ( $::operatingsystemrelease =~ /^6/ or $::operatingsystemrelease =~ /^10\.04/ ) {
40+
exec { "postgresql_stop_${name}":
4141
command => "service ${::postgresql::server::service_name} stop",
4242
onlyif => "service ${::postgresql::server::service_name} status",
4343
unless => "grep 'port = ${value}' ${::postgresql::server::postgresql_conf_path}",
4444
path => '/usr/sbin:/sbin:/bin:/usr/bin:/usr/local/bin',
4545
before => Postgresql_conf[$name],
4646
}
47+
}
48+
elsif $name == 'data_directory' {
49+
exec { "postgresql_stop_${name}":
50+
command => "service ${::postgresql::server::service_name} stop",
51+
onlyif => "service ${::postgresql::server::service_name} status",
52+
unless => "grep \"data_directory = '${value}'\" ${::postgresql::server::postgresql_conf_path}",
53+
path => '/usr/sbin:/sbin:/bin:/usr/bin:/usr/local/bin',
54+
before => Postgresql_conf[$name],
4755
}
4856
}
4957
}
5058
if $::osfamily == 'RedHat' {
51-
if $::operatingsystemrelease =~ /^7/ or $::operatingsystem == 'Fedora' {
52-
if $name == 'port' {
53-
file { 'systemd-port-override':
54-
ensure => present,
55-
path => '/etc/systemd/system/postgresql.service',
56-
owner => root,
57-
group => root,
58-
content => template('postgresql/systemd-port-override.erb'),
59-
notify => [ Exec['restart-systemd'], Class['postgresql::server::service'] ],
60-
before => Class['postgresql::server::reload'],
61-
}
62-
exec { 'restart-systemd':
63-
command => 'systemctl daemon-reload',
64-
refreshonly => true,
65-
path => '/bin:/usr/bin:/usr/local/bin'
66-
}
67-
}
68-
} else {
59+
if ! ($::operatingsystemrelease =~ /^7/ or $::operatingsystem == 'Fedora') {
6960
if $name == 'port' {
7061
# We need to force postgresql to stop before updating the port
7162
# because puppet becomes confused and is unable to manage the
7263
# service appropriately.
73-
exec { 'postgresql_stop':
64+
exec { "postgresql_stop_${name}":
7465
command => "service ${::postgresql::server::service_name} stop",
7566
onlyif => "service ${::postgresql::server::service_name} status",
7667
unless => "grep 'PGPORT=${value}' /etc/sysconfig/pgsql/postgresql",
@@ -86,26 +77,24 @@
8677
notify => Class['postgresql::server::service'],
8778
before => Class['postgresql::server::reload'],
8879
}
89-
} else {
90-
if $name == 'data_directory' {
91-
# We need to force postgresql to stop before updating the data directory
92-
# otherwise init script breaks
93-
exec { "postgresql_${name}":
94-
command => "service ${::postgresql::server::service_name} stop",
95-
onlyif => "service ${::postgresql::server::service_name} status",
96-
unless => "grep 'PGDATA=${value}' /etc/sysconfig/pgsql/postgresql",
97-
path => '/sbin:/bin:/usr/bin:/usr/local/bin',
98-
require => File['/etc/sysconfig/pgsql/postgresql'],
99-
} ->
100-
augeas { 'override PGDATA in /etc/sysconfig/pgsql/postgresql':
101-
lens => 'Shellvars.lns',
102-
incl => '/etc/sysconfig/pgsql/*',
103-
context => '/files/etc/sysconfig/pgsql/postgresql',
104-
changes => "set PGDATA ${value}",
105-
require => File['/etc/sysconfig/pgsql/postgresql'],
106-
notify => Class['postgresql::server::service'],
107-
before => Class['postgresql::server::reload'],
108-
}
80+
} elsif $name == 'data_directory' {
81+
# We need to force postgresql to stop before updating the data directory
82+
# otherwise init script breaks
83+
exec { "postgresql_${name}":
84+
command => "service ${::postgresql::server::service_name} stop",
85+
onlyif => "service ${::postgresql::server::service_name} status",
86+
unless => "grep 'PGDATA=${value}' /etc/sysconfig/pgsql/postgresql",
87+
path => '/sbin:/bin:/usr/bin:/usr/local/bin',
88+
require => File['/etc/sysconfig/pgsql/postgresql'],
89+
} ->
90+
augeas { 'override PGDATA in /etc/sysconfig/pgsql/postgresql':
91+
lens => 'Shellvars.lns',
92+
incl => '/etc/sysconfig/pgsql/*',
93+
context => '/files/etc/sysconfig/pgsql/postgresql',
94+
changes => "set PGDATA ${value}",
95+
require => File['/etc/sysconfig/pgsql/postgresql'],
96+
notify => Class['postgresql::server::service'],
97+
before => Class['postgresql::server::reload'],
10998
}
11099
}
111100
}

manifests/server/initdb.pp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,5 +62,23 @@
6262
logoutput => on_failure,
6363
require => File[$require_before_initdb],
6464
}
65+
# The package will take care of this for us the first time, but if we
66+
# ever need to init a new db we need to make these links explicitly
67+
if $::operatingsystem == 'Debian' or $::operatingsystem == 'Ubuntu' {
68+
if $::operatingsystemrelease =~ /^6/ or $::operatingsystemrelease =~ /^7/ or $::operatingsystemrelease =~ /^10\.04/ or $::operatingsystemrelease =~ /^12\.04/ {
69+
file { 'server.crt':
70+
ensure => link,
71+
path => "${datadir}/server.crt",
72+
target => '/etc/ssl/certs/ssl-cert-snakeoil.pem',
73+
require => Exec['postgresql_initdb'],
74+
}
75+
file { 'server.key':
76+
ensure => link,
77+
path => "${datadir}/server.key",
78+
target => '/etc/ssl/private/ssl-cert-snakeoil.key',
79+
require => Exec['postgresql_initdb'],
80+
}
81+
}
82+
}
6583
}
6684
}

spec/acceptance/alternative_port_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,19 @@
55
describe 'postgres::server', :unless => UNSUPPORTED_PLATFORMS.include?(fact('osfamily')) do
66
it 'on an alternative port' do
77
pp = <<-EOS
8-
class { 'postgresql::server': port => '5433' }
8+
class { 'postgresql::server': port => '55433' }
99
EOS
1010

1111
apply_manifest(pp, :catch_failures => true)
1212
apply_manifest(pp, :catch_changes => true)
1313
end
1414

15-
describe port(5433) do
15+
describe port(55433) do
1616
it { is_expected.to be_listening }
1717
end
1818

1919
it 'can connect with psql' do
20-
psql('-p 5433 --command="\l" postgres', 'postgres') do |r|
20+
psql('-p 55433 --command="\l" postgres', 'postgres') do |r|
2121
expect(r.stdout).to match(/List of databases/)
2222
end
2323
end

spec/acceptance/alternative_pgdata_spec.rb renamed to spec/acceptance/z_alternative_pgdata_spec.rb

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,27 @@
22

33
# These tests ensure that postgres can change itself to an alternative pgdata
44
# location properly.
5+
6+
# Allow postgresql to use /tmp/* as a datadir
7+
if fact('osfamily') == 'RedHat'
8+
shell("setenforce 0")
9+
end
10+
511
describe 'postgres::server', :unless => UNSUPPORTED_PLATFORMS.include?(fact('osfamily')) do
612
it 'on an alternative pgdata location' do
713
pp = <<-EOS
8-
class { 'postgresql::server': datadir => '/var/pgsql' }
14+
#file { '/var/lib/pgsql': ensure => directory, } ->
15+
# needs_initdb will be true by default for all OS's except Debian
16+
# in order to change the datadir we need to tell it explicitly to call initdb
17+
class { 'postgresql::server': datadir => '/tmp/data', needs_initdb => true }
918
EOS
1019

1120
apply_manifest(pp, :catch_failures => true)
1221
apply_manifest(pp, :catch_changes => true)
1322
end
14-
15-
describe "Alternate Directory" do
16-
File.directory?("/var/pgsql").should be true
23+
24+
describe file('/tmp/data') do
25+
it { should be_directory }
1726
end
1827

1928
it 'can connect with psql' do

spec/unit/defines/server/config_entry_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
let(:params) {{ :ensure => 'present', :name => 'port_spec', :value => '5432' }}
4545

4646
it 'stops postgresql and changes the port' do
47-
is_expected.to contain_exec('postgresql_stop')
47+
is_expected.to contain_exec('postgresql_stop_port')
4848
is_expected.to contain_augeas('override PGPORT in /etc/sysconfig/pgsql/postgresql')
4949
end
5050
end
@@ -63,7 +63,7 @@
6363
let(:params) {{ :ensure => 'present', :name => 'port_spec', :value => '5432' }}
6464

6565
it 'stops postgresql and changes the port' do
66-
is_expected.to contain_file('systemd-port-override')
66+
is_expected.to contain_file('systemd-override')
6767
is_expected.to contain_exec('restart-systemd')
6868
end
6969
end
@@ -82,7 +82,7 @@
8282
let(:params) {{ :ensure => 'present', :name => 'port_spec', :value => '5432' }}
8383

8484
it 'stops postgresql and changes the port' do
85-
is_expected.to contain_file('systemd-port-override')
85+
is_expected.to contain_file('systemd-override')
8686
is_expected.to contain_exec('restart-systemd')
8787
end
8888
end

templates/systemd-override.erb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.include /lib/systemd/system/postgresql.service
2+
[Service]
3+
Environment=PGPORT=<%= @port %>
4+
Environment=PGDATA=<%= @datadir %>

templates/systemd-port-override.erb

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)