Skip to content

Commit 5ef164b

Browse files
kbrezinakbarber
authored andcommitted
Generalization to provide more flexibility in postgresql configuration
1 parent cd31c71 commit 5ef164b

File tree

12 files changed

+204
-65
lines changed

12 files changed

+204
-65
lines changed

lib/puppet/type/postgresql_psql.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,15 @@ def sync(refreshing = false)
4949
desc "The name of the database to execute the SQL command against."
5050
end
5151

52+
newparam(:psql_path) do
53+
desc "The path to psql executable."
54+
defaultto("psql")
55+
end
56+
57+
newparam(:library_path) do
58+
desc "The path to postgresql libraries."
59+
end
60+
5261
newparam(:psql_user) do
5362
desc "The system user account under which the psql command should be executed."
5463
defaultto("postgres")

manifests/config/afterservice.pp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
# for pg_hba.conf.
2929
exec { 'set_postgres_postgrespw':
3030
# This command works w/no password because we run it as postgres system user
31-
command => "psql -c \"ALTER ROLE postgres PASSWORD '${postgres_password}'\"",
31+
command => "psql -c \"ALTER ROLE ${postgresql::params::user} PASSWORD '${postgres_password}'\"",
3232
user => $postgresql::params::user,
3333
group => $postgresql::params::group,
3434
logoutput => true,

manifests/database.pp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@
2929

3030
# Optionally set the locale switch. Older versions of createdb may not accept
3131
# --locale, so if the parameter is undefined its safer not to pass it.
32+
Postgresql_psql {
33+
psql_user => $postgresql::params::user,
34+
psql_group => $postgresql::params::group,
35+
psql_path => $postgresql::params::psql_path,
36+
library_path => $postgresql::params::library_path,
37+
}
38+
3239
if ($postgresql::params::version != '8.1') {
3340
$locale_option = $locale ? {
3441
undef => '',
@@ -58,15 +65,15 @@
5865

5966
exec { $createdb_command :
6067
refreshonly => true,
61-
user => 'postgres',
62-
cwd => $postgresql::params::datadir,
63-
logoutput => on_failure,
68+
user => $postgresql::params::user,
69+
cwd => $postgresql::params::datadir,
70+
logoutput => on_failure,
6471
} ~>
6572

6673
# This will prevent users from connecting to the database unless they've been
6774
# granted privileges.
68-
postgresql_psql {"REVOKE ${public_revoke_privilege} ON DATABASE ${dbname} FROM public":
69-
db => 'postgres',
75+
postgresql_psql {"REVOKE ${public_revoke_privilege} ON DATABASE \"${dbname}\" FROM public":
76+
db => $postgresql::params::user,
7077
refreshonly => true,
7178
cwd => $postgresql::params::datadir,
7279
}

manifests/database_grant.pp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,18 @@
3030
$privilege,
3131
$db,
3232
$role,
33-
$psql_db = 'postgres',
34-
$psql_user ='postgres'
33+
$psql_db = $postgresql::params::user,
34+
$psql_user = $postgresql::params::user
3535
) {
3636
include postgresql::params
3737

38+
Postgresql_psql {
39+
psql_user => $postgresql::params::user,
40+
psql_group => $postgresql::params::group,
41+
psql_path => $postgresql::params::psql_path,
42+
library_path => $postgresql::params::library_path,
43+
}
44+
3845
# TODO: FIXME: only works on databases, due to using has_database_privilege
3946

4047
# TODO: this is a terrible hack; if they pass "ALL" as the desired privilege,
@@ -50,7 +57,7 @@
5057
default => $privilege,
5158
}
5259

53-
postgresql_psql {"GRANT ${privilege} ON database ${db} TO ${role}":
60+
postgresql_psql {"GRANT ${privilege} ON database \"${db}\" TO \"${role}\"":
5461
db => $psql_db,
5562
psql_user => $psql_user,
5663
unless => "SELECT 1 WHERE has_database_privilege('${role}', '${db}', '${unless_privilege}')",

manifests/database_user.pp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
$password_hash,
4242
$createdb = false,
4343
$createrole = false,
44-
$db = 'postgres',
44+
$db = $postgresql::params::user,
4545
$superuser = false,
4646
$user = $title
4747
) {

manifests/init.pp

Lines changed: 83 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
# For examples, see the files in the `tests` directory; in particular,
1111
# `/server-yum-postgresql-org.pp`.
1212
#
13+
# Parameters:
1314
#
1415
# [*version*]
1516
# The postgresql version to install. If not specified, the
@@ -26,13 +27,63 @@
2627
# set to `true`. It determines which package repository should
2728
# be used to install the postgres packages. Currently supported
2829
# values include `yum.postgresql.org`.
29-
#
3030
# [*locale*]
3131
# This setting defines the default locale for initdb and createdb
3232
# commands. This default to 'undef' which is effectively 'C'.
3333
# [*charset*]
3434
# Sets the default charset to be used for initdb and createdb.
3535
# Defaults to 'UTF8'.
36+
# [*datadir*]
37+
# This setting can be used to override the default postgresql
38+
# data directory for the target platform. If not specified, the
39+
# module will use whatever directory is the default for your
40+
# OS distro.
41+
# [*confdir*]
42+
# This setting can be used to override the default postgresql
43+
# configuration directory for the target platform. If not
44+
# specified, the module will use whatever directory is the
45+
# default for your OS distro.
46+
# [*bindir*]
47+
# This setting can be used to override the default postgresql
48+
# binaries directory for the target platform. If not
49+
# specified, the module will use whatever directory is the
50+
# default for your OS distro.
51+
# [*libdir*]
52+
# This setting can be used to override the default postgresql
53+
# lib directory for the target platform. If not
54+
# specified, the module will assume that the libraries are
55+
# loaded without the explicit library location.
56+
# [*client_package_name*]
57+
# This setting can be used to override the default
58+
# postgresql client package name. If not specified, the module
59+
# will use whatever package name is the default for your
60+
# OS distro.
61+
# [*server_package_name*]
62+
# This setting can be used to override the default
63+
# postgresql server package name. If not specified, the module
64+
# will use whatever package name is the default for your
65+
# OS distro.
66+
# [*service_name*]
67+
# This setting can be used to override the default
68+
# postgresql service name. If not specified, the module
69+
# will use whatever service name is the default for your
70+
# OS distro.
71+
# [*user*]
72+
# This setting can be used to override the default
73+
# postgresql super user and owner of postgresql related files
74+
# in the file system. If not specified, the module will use
75+
# the user name 'postgres'.
76+
# [*group*]
77+
# This setting can be used to override the default
78+
# postgresql user group to be used for related files
79+
# in the file system. If not specified, the module will use
80+
# the group name 'postgres'.
81+
# [*run_initdb*]
82+
# This setting can be used to explicitly call the inidb
83+
# operation after server package is installed and before
84+
# the postgresql service is started. If not specified, the
85+
# module will decide whether to call initdb or not depending
86+
# on your OS distro.
3687
#
3788
# === Examples:
3889
#
@@ -47,13 +98,37 @@
4798
$manage_package_repo = false,
4899
$package_source = undef,
49100
$locale = undef,
50-
$charset = 'UTF8'
51-
) {
101+
$charset = 'UTF8',
102+
$datadir = undef,
103+
$confdir = undef,
104+
$bindir = undef,
105+
$libdir = undef,
106+
$client_package_name = undef,
107+
$server_package_name = undef,
108+
$devel_package_name = undef,
109+
$java_package_name = undef,
110+
$service_name = undef,
111+
$user = undef,
112+
$group = undef,
113+
$run_initdb = undef,) {
114+
52115
class { 'postgresql::params':
53-
version => $version,
54-
manage_package_repo => $manage_package_repo,
55-
package_source => $package_source,
56-
locale => $locale,
57-
charset => $charset,
116+
version => $version,
117+
manage_package_repo => $manage_package_repo,
118+
package_source => $package_source,
119+
locale => $locale,
120+
charset => $charset,
121+
custom_datadir => $datadir,
122+
custom_confdir => $confdir,
123+
custom_bindir => $bindir,
124+
custom_libdir => $libdir,
125+
custom_client_package_name => $client_package_name,
126+
custom_server_package_name => $server_package_name,
127+
custom_devel_package_name => $devel_package_name,
128+
custom_java_package_name => $java_package_name,
129+
custom_service_name => $service_name,
130+
custom_user => $user,
131+
custom_group => $group,
132+
run_initdb => $run_initdb,
58133
}
59134
}

manifests/initdb.pp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
class postgresql::initdb(
2020
$datadir = $postgresql::params::datadir,
2121
$encoding = $postgresql::params::charset,
22-
$group = 'postgres',
22+
$group = $postgresql::params::group,
2323
$initdb_path = $postgresql::params::initdb_path,
24-
$user = 'postgres'
24+
$user = $postgresql::params::user
2525
) inherits postgresql::params {
2626
# Build up the initdb command.
2727
#

manifests/params.pp

Lines changed: 52 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,26 @@
2828
# correct paths to the postgres dirs.
2929

3030
class postgresql::params(
31-
$version = $::postgres_default_version,
32-
$manage_package_repo = false,
33-
$package_source = undef,
34-
$locale = undef,
35-
$charset = 'UTF8'
31+
$version = $::postgres_default_version,
32+
$manage_package_repo = false,
33+
$package_source = undef,
34+
$locale = undef,
35+
$charset = 'UTF8',
36+
$custom_datadir = undef,
37+
$custom_confdir = undef,
38+
$custom_bindir = undef,
39+
$custom_libdir = undef,
40+
$custom_client_package_name = undef,
41+
$custom_server_package_name = undef,
42+
$custom_devel_package_name = undef,
43+
$custom_java_package_name = undef,
44+
$custom_service_name = undef,
45+
$custom_user = undef,
46+
$custom_group = undef,
47+
$run_initdb = undef,
3648
) {
37-
$user = 'postgres'
38-
$group = 'postgres'
49+
$user = pick($custom_user, 'postgres')
50+
$group = pick($custom_group, 'postgres')
3951
$ip_mask_deny_postgres_user = '0.0.0.0/0'
4052
$ip_mask_allow_all_users = '127.0.0.1/32'
4153
$listen_addresses = 'localhost'
@@ -93,64 +105,66 @@
93105
# Amazon Linux's OS Family is 'Linux', operating system 'Amazon'.
94106
case $::osfamily {
95107
'RedHat', 'Linux': {
96-
$needs_initdb = true
108+
$needs_initdb = pick($run_initdb, true)
97109
$firewall_supported = true
98110
$persist_firewall_command = '/sbin/iptables-save > /etc/sysconfig/iptables'
99111

100112
if $version == $::postgres_default_version {
101-
$client_package_name = 'postgresql'
102-
$server_package_name = 'postgresql-server'
103-
$devel_package_name = 'postgresql-devel'
104-
$java_package_name = 'postgresql-jdbc'
105-
$service_name = 'postgresql'
106-
$bindir = '/usr/bin'
107-
$datadir = '/var/lib/pgsql/data'
108-
$confdir = $datadir
113+
$client_package_name = pick($custom_client_package_name, 'postgresql')
114+
$server_package_name = pick($custom_server_package_name, 'postgresql-server')
115+
$devel_package_name = pick($custom_devel_package_name, 'postgresql-devel')
116+
$java_package_name = pick($custom_java_package_name, 'postgresql-jdbc')
117+
$service_name = pick($custom_service_name, 'postgresql')
118+
$bindir = pick($custom_bindir, '/usr/bin')
119+
$libdir = $custom_libdir
120+
$datadir = pick($custom_datadir, '/var/lib/pgsql/data')
121+
$confdir = pick($custom_confdir, $datadir)
109122
} else {
110123
$version_parts = split($version, '[.]')
111124
$package_version = "${version_parts[0]}${version_parts[1]}"
112-
$client_package_name = "postgresql${package_version}"
113-
$server_package_name = "postgresql${package_version}-server"
114-
$devel_package_name = "postgresql${package_version}-devel"
115-
$java_package_name = "postgresql${package_version}-jdbc"
116-
$service_name = "postgresql-${version}"
117-
$bindir = "/usr/pgsql-${version}/bin"
118-
$datadir = "/var/lib/pgsql/${version}/data"
119-
$confdir = $datadir
125+
$client_package_name = pick($custom_client_package_name, "postgresql${package_version}")
126+
$server_package_name = pick($custom_server_package_name, "postgresql${package_version}-server")
127+
$devel_package_name = pick($custom_devel_package_name, "postgresql${package_version}-devel")
128+
$java_package_name = pick($custom_java_package_name, "postgresql${package_version}-jdbc")
129+
$service_name = pick($custom_service_name, "postgresql-${version}")
130+
$bindir = pick($custom_bindir, "/usr/pgsql-${version}/bin")
131+
$libdir = $custom_libdir
132+
$datadir = pick($custom_datadir, "/var/lib/pgsql/${version}/data")
133+
$confdir = pick($custom_confdir, $datadir)
120134
}
121135

122136
$service_status = undef
123137
}
124138

125139
'Debian': {
126-
$needs_initdb = false
140+
$needs_initdb = pick($run_initdb, false)
127141
$firewall_supported = false
128142
# TODO: not exactly sure yet what the right thing to do for Debian/Ubuntu is.
129143
#$persist_firewall_command = '/sbin/iptables-save > /etc/iptables/rules.v4'
130144

131145

132146
case $::operatingsystem {
133147
'Debian': {
134-
$service_name = 'postgresql'
148+
$service_name = pick($custom_service_name, 'postgresql')
135149
}
136-
137150
'Ubuntu': {
138151
# thanks, ubuntu
139152
if($::lsbmajdistrelease == '10' and !$manage_package_repo) {
140-
$service_name = "postgresql-${version}"
153+
$service_name = pick($custom_service_name, "postgresql-${version}")
141154
} else {
142-
$service_name = 'postgresql'
155+
$service_name = pick($custom_service_name, 'postgresql')
143156
}
144157
}
145158
}
146159

147-
$client_package_name = "postgresql-client-${version}"
148-
$server_package_name = "postgresql-${version}"
149-
$devel_package_name = 'libpq-dev'
150-
$java_package_name = 'libpostgresql-jdbc-java'
151-
$bindir = "/usr/lib/postgresql/${version}/bin"
152-
$datadir = "/var/lib/postgresql/${version}/main"
153-
$confdir = "/etc/postgresql/${version}/main"
160+
$client_package_name = pick($custom_client_package_name, "postgresql-client-${version}")
161+
$server_package_name = pick($custom_server_package_name, "postgresql-${version}")
162+
$devel_package_name = pick($custom_devel_package_name, 'libpq-dev')
163+
$java_package_name = pick($custom_java_package_name, 'libpostgresql-jdbc-java')
164+
$bindir = pick($custom_bindir, "/usr/lib/postgresql/${version}/bin")
165+
$libdir = $custom_libdir
166+
$datadir = pick($custom_datadir, "/var/lib/postgresql/${version}/main")
167+
$confdir = pick($custom_confdir, "/etc/postgresql/${version}/main")
154168
$service_status = "/etc/init.d/${service_name} status | /bin/egrep -q 'Running clusters: .+|online'"
155169
}
156170

@@ -162,6 +176,8 @@
162176
$initdb_path = "${bindir}/initdb"
163177
$createdb_path = "${bindir}/createdb"
164178
$psql_path = "${bindir}/psql"
179+
$library_path = $libdir
165180
$pg_hba_conf_path = "${confdir}/pg_hba.conf"
166181
$postgresql_conf_path = "${confdir}/postgresql.conf"
182+
167183
}

manifests/psql.pp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
$unless,
2222
$command = $title,
2323
$refreshonly = false,
24-
$user = 'postgres'
24+
$user = $postgresql::params::user
2525
) {
2626

2727
include postgresql::params
@@ -34,7 +34,13 @@
3434
$no_password_option = '--no-password'
3535
}
3636

37-
$psql = "${postgresql::params::psql_path} ${no_password_option} --tuples-only --quiet --dbname ${db}"
37+
$psql_core = "${postgresql::params::psql_path} ${no_password_option} --tuples-only --quiet --dbname ${db}"
38+
39+
$psql = $postgresql::params::library_path ? {
40+
undef => $psql_core,
41+
default => "LD_LIBRARY_PATH=\"${postgresql::params::library_path}\" ${psql_core}"
42+
}
43+
3844
$quoted_command = regsubst($command, '"', '\\"', 'G')
3945
$quoted_unless = regsubst($unless, '"', '\\"', 'G')
4046

0 commit comments

Comments
 (0)