Skip to content

Commit d065d78

Browse files
committed
Merge pull request puppetlabs#514 from mixacha/pg_data_support
Adds support for PGDATA changing
2 parents 6a7e798 + 2785947 commit d065d78

File tree

4 files changed

+59
-0
lines changed

4 files changed

+59
-0
lines changed

manifests/server/config.pp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
$version = $postgresql::server::_version
1616
$manage_pg_hba_conf = $postgresql::server::manage_pg_hba_conf
1717
$manage_pg_ident_conf = $postgresql::server::manage_pg_ident_conf
18+
$datadir = $postgresql::server::datadir
1819

1920
if ($manage_pg_hba_conf == true) {
2021
# Prepare the main pg_hba file
@@ -100,6 +101,9 @@
100101
postgresql::server::config_entry { 'port':
101102
value => $port,
102103
}
104+
postgresql::server::config_entry { 'data_directory':
105+
value => $datadir,
106+
}
103107

104108
# RedHat-based systems hardcode some PG* variables in the init script, and need to be overriden
105109
# in /etc/sysconfig/pgsql/postgresql. Create a blank file so we can manage it with augeas later.

manifests/server/config_entry.pp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,27 @@
8686
notify => Class['postgresql::server::service'],
8787
before => Class['postgresql::server::reload'],
8888
}
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+
}
109+
}
89110
}
90111
}
91112
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
require 'spec_helper_acceptance'
2+
3+
# These tests ensure that postgres can change itself to an alternative pgdata
4+
# location properly.
5+
describe 'postgres::server', :unless => UNSUPPORTED_PLATFORMS.include?(fact('osfamily')) do
6+
it 'on an alternative pgdata location' do
7+
pp = <<-EOS
8+
class { 'postgresql::server': datadir => '/var/pgsql' }
9+
EOS
10+
11+
apply_manifest(pp, :catch_failures => true)
12+
apply_manifest(pp, :catch_changes => true)
13+
end
14+
15+
describe "Alternate Directory" do
16+
File.directory?("/var/pgsql").should be true
17+
end
18+
19+
it 'can connect with psql' do
20+
psql('--command="\l" postgres', 'postgres') do |r|
21+
expect(r.stdout).to match(/List of databases/)
22+
end
23+
end
24+
25+
end

spec/unit/defines/server/config_entry_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,15 @@
8888
end
8989
end
9090

91+
context "data_directory" do
92+
let(:params) {{ :ensure => 'present', :name => 'data_directory_spec', :value => '/var/pgsql' }}
93+
94+
it 'stops postgresql and changes the data directory' do
95+
is_expected.to contain_exec('postgresql_data_directory')
96+
is_expected.to contain_augeas('override PGDATA in /etc/sysconfig/pgsql/postgresql')
97+
end
98+
end
99+
91100
context "passes values through appropriately" do
92101
let(:params) {{ :ensure => 'present', :name => 'check_function_bodies', :value => 'off' }}
93102

0 commit comments

Comments
 (0)