Skip to content

Commit b550d4b

Browse files
author
Ashley Penney
committed
Merge pull request puppetlabs#404 from thunderkeys/postgresql-server-port-support
Add support for port parameter to postgresql::server
2 parents 3d73e7f + 86f2950 commit b550d4b

17 files changed

+116
-9
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,9 @@ This setting is used to specify the name of the default database to connect with
399399
####`listen_addresses`
400400
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).
401401

402+
####`port`
403+
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.
404+
402405
####`ip_mask_deny_postgres_user`
403406
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.
404407

lib/puppet/provider/postgresql_psql/ruby.rb

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ def run_sql_command(sql)
6363

6464
command = [resource[:psql_path]]
6565
command.push("-d", resource[:db]) if resource[:db]
66+
command.push("-p", resource[:port]) if resource[:port]
6667
command.push("-t", "-c", sql)
6768

6869
if resource[:cwd]

lib/puppet/type/postgresql_psql.rb

+4
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ def sync(refreshing = false)
4949
desc "The name of the database to execute the SQL command against."
5050
end
5151

52+
newparam(:port) do
53+
desc "The port of the database server to execute the SQL command against."
54+
end
55+
5256
newparam(:search_path) do
5357
desc "The schema search path to use when executing the SQL command"
5458
end

manifests/params.pp

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
$version = $globals_version
55
$postgis_version = $globals_postgis_version
66
$listen_addresses = 'localhost'
7+
$port = 5432
78
$ip_mask_deny_postgres_user = '0.0.0.0/0'
89
$ip_mask_allow_all_users = '127.0.0.1/32'
910
$ipv4acls = []

manifests/server.pp

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
$default_database = $postgresql::params::default_database,
1919

2020
$listen_addresses = $postgresql::params::listen_addresses,
21+
$port = $postgresql::params::port,
2122
$ip_mask_deny_postgres_user = $postgresql::params::ip_mask_deny_postgres_user,
2223
$ip_mask_allow_all_users = $postgresql::params::ip_mask_allow_all_users,
2324
$ipv4acls = $postgresql::params::ipv4acls,

manifests/server/config.pp

+13
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
$ip_mask_deny_postgres_user = $postgresql::server::ip_mask_deny_postgres_user
55
$ip_mask_allow_all_users = $postgresql::server::ip_mask_allow_all_users
66
$listen_addresses = $postgresql::server::listen_addresses
7+
$port = $postgresql::server::port
78
$ipv4acls = $postgresql::server::ipv4acls
89
$ipv6acls = $postgresql::server::ipv6acls
910
$pg_hba_conf_path = $postgresql::server::pg_hba_conf_path
@@ -97,6 +98,18 @@
9798
postgresql::server::config_entry { 'listen_addresses':
9899
value => $listen_addresses,
99100
}
101+
postgresql::server::config_entry { 'port':
102+
value => "${port}",
103+
}
104+
105+
# RedHat-based systems hardcode some PG* variables in the init script, and need to be overriden
106+
# in /etc/sysconfig/pgsql/postgresql. Create a blank file so we can manage it with augeas later.
107+
if ($::osfamily == 'RedHat') {
108+
file { '/etc/sysconfig/pgsql/postgresql':
109+
ensure => present,
110+
replace => false,
111+
}
112+
}
100113
} else {
101114
file { $pg_hba_conf_path:
102115
ensure => absent,

manifests/server/config_entry.pp

+14
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,20 @@
2626
}
2727
}
2828

29+
if ($::osfamily == 'RedHat') {
30+
if ($name == 'port') {
31+
augeas { 'override PGPORT in /etc/sysconfig/pgsql/postgresql':
32+
lens => 'Shellvars.lns',
33+
incl => '/etc/sysconfig/pgsql/*',
34+
context => '/files/etc/sysconfig/pgsql/postgresql',
35+
changes => "set PGPORT $value",
36+
require => File['/etc/sysconfig/pgsql/postgresql'],
37+
notify => Class['postgresql::server::service'],
38+
before => Class['postgresql::server::reload'],
39+
}
40+
}
41+
}
42+
2943
case $ensure {
3044
/present|absent/: {
3145
postgresql_conf { $name:

manifests/server/database.pp

+5-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
$user = $postgresql::server::user
1313
$group = $postgresql::server::group
1414
$psql_path = $postgresql::server::psql_path
15+
$port = $postgresql::server::port
1516
$version = $postgresql::server::version
1617
$default_db = $postgresql::server::default_database
1718

@@ -20,6 +21,7 @@
2021
psql_user => $user,
2122
psql_group => $group,
2223
psql_path => $psql_path,
24+
port => $port,
2325
}
2426

2527
# Optionally set the locale switch. Older versions of createdb may not accept
@@ -45,12 +47,13 @@
4547
default => "--tablespace='${tablespace}' ",
4648
}
4749

48-
$createdb_command = "${createdb_path} --owner='${owner}' --template=${template} ${encoding_option}${locale_option}${tablespace_option} '${dbname}'"
50+
$createdb_command = "${createdb_path} --port='${port}' --owner='${owner}' --template=${template} ${encoding_option}${locale_option}${tablespace_option} '${dbname}'"
4951

5052
postgresql_psql { "Check for existence of db '${dbname}'":
5153
command => 'SELECT 1',
5254
unless => "SELECT datname FROM pg_database WHERE datname='${dbname}'",
5355
db => $default_db,
56+
port => $port,
5457
require => Class['postgresql::server::service']
5558
}~>
5659
exec { $createdb_command :
@@ -63,6 +66,7 @@
6366
# granted privileges.
6467
postgresql_psql {"REVOKE ${public_revoke_privilege} ON DATABASE \"${dbname}\" FROM public":
6568
db => $default_db,
69+
port => $port,
6670
refreshonly => true,
6771
}
6872

manifests/server/firewall.pp

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@
33
$ensure = $postgresql::server::ensure
44
$manage_firewall = $postgresql::server::manage_firewall
55
$firewall_supported = $postgresql::server::firewall_supported
6+
$port = $postgresql::server::port
67

78
if ($manage_firewall and $firewall_supported) {
89
if ($ensure == 'present' or $ensure == true) {
9-
# TODO: get rid of hard-coded port
10-
firewall { '5432 accept - postgres':
11-
port => '5432',
10+
firewall { "$port accept - postgres":
11+
port => $port,
1212
proto => 'tcp',
1313
action => 'accept',
1414
}
1515
} else {
16-
firewall { '5432 accept - postgres':
16+
firewall { "$port accept - postgres":
1717
ensure => absent,
1818
}
1919
}

manifests/server/grant.pp

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
$object_type = 'database',
77
$object_name = $db,
88
$psql_db = $postgresql::server::default_database,
9-
$psql_user = $postgresql::server::user
9+
$psql_user = $postgresql::server::user,
10+
$port = $postgresql::server::port
1011
) {
1112
$group = $postgresql::server::group
1213
$psql_path = $postgresql::server::psql_path
@@ -68,6 +69,7 @@
6869
$grant_cmd = "GRANT ${_privilege} ON ${_object_type} \"${object_name}\" TO \"${role}\""
6970
postgresql_psql { $grant_cmd:
7071
db => $on_db,
72+
port => $port,
7173
psql_user => $psql_user,
7274
psql_group => $group,
7375
psql_path => $psql_path,

manifests/server/role.pp

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
$createdb = false,
55
$createrole = false,
66
$db = $postgresql::server::default_database,
7+
$port = $postgresql::server::port,
78
$login = true,
89
$inherit = true,
910
$superuser = false,
@@ -30,6 +31,7 @@
3031

3132
Postgresql_psql {
3233
db => $db,
34+
port => $port,
3335
psql_user => $psql_user,
3436
psql_group => $psql_group,
3537
psql_path => $psql_path,

manifests/server/service.pp

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
$service_provider = $postgresql::server::service_provider
66
$service_status = $postgresql::server::service_status
77
$user = $postgresql::server::user
8+
$port = $postgresql::server::port
89
$default_database = $postgresql::server::default_database
910

1011
$service_ensure = $ensure ? {
@@ -33,6 +34,7 @@
3334
postgresql::validate_db_connection { 'validate_service_is_running':
3435
run_as => $user,
3536
database_name => $default_database,
37+
database_port => $port,
3638
sleep => 1,
3739
tries => 60,
3840
create_db_first => false,

manifests/server/table_grant.pp

+2
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
$table,
66
$db,
77
$role,
8+
$port = $postgresql::server::port,
89
$psql_db = undef,
910
$psql_user = undef
1011
) {
1112
postgresql::server::grant { "table:${name}":
1213
role => $role,
1314
db => $db,
15+
port => $port,
1416
privilege => $privilege,
1517
object_type => 'TABLE',
1618
object_name => $table,

manifests/server/tablespace.pp

+2
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66
) {
77
$user = $postgresql::server::user
88
$group = $postgresql::server::group
9+
$port = $postgresql::server::port
910
$psql_path = $postgresql::server::psql_path
1011

1112
Postgresql_psql {
1213
psql_user => $user,
1314
psql_group => $group,
1415
psql_path => $psql_path,
16+
port => $port,
1517
}
1618

1719
if ($owner == undef) {

spec/acceptance/server/database_spec.rb

+25
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,28 @@ class { 'postgresql::server': }
2727
end
2828
end
2929
end
30+
31+
describe 'postgresql::server::database: alternate port', :unless => UNSUPPORTED_PLATFORMS.include?(fact('osfamily')) do
32+
it 'should idempotently create a db on a non-default port that we can connect to' do
33+
begin
34+
pp = <<-EOS.unindent
35+
$db = 'postgresql_test_db'
36+
class { 'postgresql::server':
37+
port => 5433,
38+
}
39+
40+
postgresql::server::database { $db: }
41+
EOS
42+
43+
apply_manifest(pp, :catch_failures => true)
44+
apply_manifest(pp, :catch_changes => true)
45+
46+
psql('--command="select datname from pg_database" --port=5433 postgresql_test_db') do |r|
47+
expect(r.stdout).to match(/postgresql_test_db/)
48+
expect(r.stderr).to eq('')
49+
end
50+
ensure
51+
psql('--command="drop database postgresql_test_db" --port=5433 postgres')
52+
end
53+
end
54+
end

spec/acceptance/server_spec.rb

+23-3
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,6 @@ class { "postgresql::server": }
8787
user => "foo1",
8888
password => postgresql_password('foo1', 'foo1'),
8989
}
90-
postgresql::server::config_entry { 'port':
91-
value => '5432',
92-
}
9390
EOS
9491

9592
apply_manifest(pp, :catch_failures => true)
@@ -181,3 +178,26 @@ class { "postgresql::server":
181178
end
182179
end
183180
end
181+
182+
describe 'server on alternate port:', :unless => UNSUPPORTED_PLATFORMS.include?(fact('osfamily')) do
183+
after :all do
184+
apply_manifest("class { 'postgresql::server': ensure => absent }", :catch_failures => true)
185+
end
186+
187+
context 'test installing postgresql with alternate port' do
188+
it 'perform installation and make sure it is idempotent' do
189+
pp = <<-EOS.unindent
190+
class { "postgresql::server":
191+
port => 5433,
192+
}
193+
EOS
194+
195+
apply_manifest(pp, :catch_failures => true)
196+
apply_manifest(pp, :catch_changes => true)
197+
end
198+
199+
describe port(5433) do
200+
it { should be_listening }
201+
end
202+
end
203+
end

spec/spec_helper_acceptance.rb

+11
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,17 @@ def psql(psql_cmd, user = 'postgres', exit_codes = [0], &block)
4545
install_package host, 'rubygems'
4646
on host, 'gem install puppet --no-ri --no-rdoc'
4747
on host, "mkdir -p #{host['distmoduledir']}"
48+
osfamily = fact 'osfamily'
49+
# install augeas dependencies
50+
if osfamily =~ /Debian/
51+
install_package host, 'ruby-dev'
52+
install_package host, 'libaugeas-dev'
53+
end
54+
if osfamily =~ /RedHat/
55+
install_package host, 'ruby-devel'
56+
install_package host, 'augeas-devel'
57+
end
58+
on host, 'gem install ruby-augeas --no-ri --no-rdoc'
4859
end
4960
end
5061

0 commit comments

Comments
 (0)