Skip to content

Commit 5012893

Browse files
committed
Cleaned up and added unit tests
Signed-off-by: Ken Barber <[email protected]>
1 parent 5ef164b commit 5012893

File tree

13 files changed

+142
-109
lines changed

13 files changed

+142
-109
lines changed

README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,39 @@ This will set the default database locale for all databases created with this mo
164164
####`charset`
165165
This will set the default charset for all databases created with this module. On certain operating systems this will be used during the `template1` initialization as well so it becomes a default outside of the module as well. Defaults to `UTF8`.
166166

167+
####`datadir`
168+
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.
169+
170+
####`confdir`
171+
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.
172+
173+
####`bindir`
174+
This setting can be used to override the default postgresql binaries directory for the target platform. If not specified, the module will use whatever directory is the default for your OS distro.
175+
176+
####`client_package_name`
177+
This setting can be used to override the default postgresql client package name. If not specified, the module will use whatever package name is the default for your OS distro.
178+
179+
####`server_package_name`
180+
This setting can be used to override the default postgresql server package name. If not specified, the module will use whatever package name is the default for your OS distro.
181+
182+
####`devel_package_name`
183+
This setting can be used to override the default postgresql devel package name. If not specified, the module will use whatever package name is the default for your OS distro.
184+
185+
####`java_package_name`
186+
This setting can be used to override the default postgresql java package name. If not specified, the module will use whatever package name is the default for your OS distro.
187+
188+
####`service_name`
189+
This setting can be used to override the default postgresql service name. If not specified, the module will use whatever service name is the default for your OS distro.
190+
191+
####`user`
192+
This setting can be used to override the default postgresql super user and owner of postgresql related files in the file system. If not specified, the module will use the user name 'postgres'.
193+
194+
####`group`
195+
This setting can be used to override the default postgresql user group to be used for related files in the file system. If not specified, the module will use the group name 'postgres'.
196+
197+
####`run_initdb`
198+
This setting can be used to explicitly call the initdb operation after server package is installed and before the postgresql service is started. If not specified, the module will decide whether to call initdb or not depending on your OS distro.
199+
167200
###Class: postgresql::server
168201
Here are the options that you can set in the `config_hash` parameter of `postgresql::server`:
169202

lib/puppet/provider/postgresql_psql/ruby.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ def run_unless_sql_command(sql)
5151
end
5252

5353
def run_sql_command(sql)
54-
command = %{psql #{"-d #{resource[:db]}" if resource[:db]} -t -c "#{sql.gsub('"', '\"')}"}
54+
command = %{#{resource[:psql_path]} #{"-d #{resource[:db]}" if resource[:db]} -t -c "#{sql.gsub('"', '\"')}"}
55+
5556
if resource[:cwd]
5657
Dir.chdir resource[:cwd] do
5758
Puppet::Util::SUIDManager.run_and_capture(command, resource[:psql_user], resource[:psql_group])

lib/puppet/type/postgresql_psql.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,6 @@ def sync(refreshing = false)
5454
defaultto("psql")
5555
end
5656

57-
newparam(:library_path) do
58-
desc "The path to postgresql libraries."
59-
end
60-
6157
newparam(:psql_user) do
6258
desc "The system user account under which the psql command should be executed."
6359
defaultto("postgres")

manifests/database.pp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@
2727
) {
2828
include postgresql::params
2929

30-
# Optionally set the locale switch. Older versions of createdb may not accept
31-
# --locale, so if the parameter is undefined its safer not to pass it.
30+
# Set the defaults for the postgresql_psql resource
3231
Postgresql_psql {
3332
psql_user => $postgresql::params::user,
3433
psql_group => $postgresql::params::group,
3534
psql_path => $postgresql::params::psql_path,
36-
library_path => $postgresql::params::library_path,
3735
}
3836

37+
# Optionally set the locale switch. Older versions of createdb may not accept
38+
# --locale, so if the parameter is undefined its safer not to pass it.
3939
if ($postgresql::params::version != '8.1') {
4040
$locale_option = $locale ? {
4141
undef => '',
@@ -59,23 +59,20 @@
5959
postgresql_psql { "Check for existence of db '${dbname}'":
6060
command => 'SELECT 1',
6161
unless => "SELECT datname FROM pg_database WHERE datname='${dbname}'",
62-
cwd => $postgresql::params::datadir,
6362
require => Class['postgresql::server']
6463
} ~>
6564

6665
exec { $createdb_command :
6766
refreshonly => true,
68-
user => $postgresql::params::user,
69-
cwd => $postgresql::params::datadir,
70-
logoutput => on_failure,
67+
user => $postgresql::params::user,
68+
logoutput => on_failure,
7169
} ~>
7270

7371
# This will prevent users from connecting to the database unless they've been
7472
# granted privileges.
7573
postgresql_psql {"REVOKE ${public_revoke_privilege} ON DATABASE \"${dbname}\" FROM public":
7674
db => $postgresql::params::user,
7775
refreshonly => true,
78-
cwd => $postgresql::params::datadir,
7976
}
8077

8178
}

manifests/database_grant.pp

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,20 @@
2525
# in the modules or not.
2626

2727
define postgresql::database_grant(
28-
# TODO: mysql supports an array of privileges here. We should do that if we
29-
# port this to ruby.
30-
$privilege,
31-
$db,
32-
$role,
33-
$psql_db = $postgresql::params::user,
34-
$psql_user = $postgresql::params::user
28+
# TODO: mysql supports an array of privileges here. We should do that if we
29+
# port this to ruby.
30+
$privilege,
31+
$db,
32+
$role,
33+
$psql_db = $postgresql::params::user,
34+
$psql_user = $postgresql::params::user
3535
) {
3636
include postgresql::params
3737

3838
Postgresql_psql {
3939
psql_user => $postgresql::params::user,
4040
psql_group => $postgresql::params::group,
4141
psql_path => $postgresql::params::psql_path,
42-
library_path => $postgresql::params::library_path,
4342
}
4443

4544
# TODO: FIXME: only works on databases, due to using has_database_privilege
@@ -61,7 +60,5 @@
6160
db => $psql_db,
6261
psql_user => $psql_user,
6362
unless => "SELECT 1 WHERE has_database_privilege('${role}', '${db}', '${unless_privilege}')",
64-
cwd => $postgresql::params::datadir,
6563
}
6664
}
67-

manifests/database_user.pp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,12 @@
3838
#
3939

4040
define postgresql::database_user(
41-
$password_hash,
42-
$createdb = false,
43-
$createrole = false,
44-
$db = $postgresql::params::user,
45-
$superuser = false,
46-
$user = $title
41+
$password_hash,
42+
$createdb = false,
43+
$createrole = false,
44+
$db = $postgresql::params::user,
45+
$superuser = false,
46+
$user = $title
4747
) {
4848
postgresql::role { $user:
4949
db => $db,

manifests/init.pp

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
# For examples, see the files in the `tests` directory; in particular,
1111
# `/server-yum-postgresql-org.pp`.
1212
#
13-
# Parameters:
13+
# === Parameters
1414
#
1515
# [*version*]
1616
# The postgresql version to install. If not specified, the
@@ -48,11 +48,6 @@
4848
# binaries directory for the target platform. If not
4949
# specified, the module will use whatever directory is the
5050
# 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.
5651
# [*client_package_name*]
5752
# This setting can be used to override the default
5853
# postgresql client package name. If not specified, the module
@@ -63,6 +58,16 @@
6358
# postgresql server package name. If not specified, the module
6459
# will use whatever package name is the default for your
6560
# OS distro.
61+
# [*devel_package_name*]
62+
# This setting can be used to override the default
63+
# postgresql devel package name. If not specified, the module
64+
# will use whatever package name is the default for your
65+
# OS distro.
66+
# [*java_package_name*]
67+
# This setting can be used to override the default
68+
# postgresql java package name. If not specified, the module
69+
# will use whatever package name is the default for your
70+
# OS distro.
6671
# [*service_name*]
6772
# This setting can be used to override the default
6873
# postgresql service name. If not specified, the module
@@ -79,13 +84,13 @@
7984
# in the file system. If not specified, the module will use
8085
# the group name 'postgres'.
8186
# [*run_initdb*]
82-
# This setting can be used to explicitly call the inidb
87+
# This setting can be used to explicitly call the initdb
8388
# operation after server package is installed and before
8489
# the postgresql service is started. If not specified, the
8590
# module will decide whether to call initdb or not depending
8691
# on your OS distro.
8792
#
88-
# === Examples:
93+
# === Examples
8994
#
9095
# class { 'postgresql':
9196
# version => '9.2',
@@ -102,15 +107,15 @@
102107
$datadir = undef,
103108
$confdir = undef,
104109
$bindir = undef,
105-
$libdir = undef,
106110
$client_package_name = undef,
107111
$server_package_name = undef,
108112
$devel_package_name = undef,
109113
$java_package_name = undef,
110114
$service_name = undef,
111115
$user = undef,
112116
$group = undef,
113-
$run_initdb = undef,) {
117+
$run_initdb = undef
118+
) {
114119

115120
class { 'postgresql::params':
116121
version => $version,
@@ -121,7 +126,6 @@
121126
custom_datadir => $datadir,
122127
custom_confdir => $confdir,
123128
custom_bindir => $bindir,
124-
custom_libdir => $libdir,
125129
custom_client_package_name => $client_package_name,
126130
custom_server_package_name => $server_package_name,
127131
custom_devel_package_name => $devel_package_name,

manifests/params.pp

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
$custom_datadir = undef,
3737
$custom_confdir = undef,
3838
$custom_bindir = undef,
39-
$custom_libdir = undef,
4039
$custom_client_package_name = undef,
4140
$custom_server_package_name = undef,
4241
$custom_devel_package_name = undef,
@@ -58,32 +57,32 @@
5857

5958

6059
if ($manage_package_repo) {
61-
case $::osfamily {
62-
'RedHat': {
63-
$rh_pkg_source = pick($package_source, 'yum.postgresql.org')
64-
65-
case $rh_pkg_source {
66-
'yum.postgresql.org': {
67-
class { 'postgresql::package_source::yum_postgresql_org':
68-
version => $version
69-
}
60+
case $::osfamily {
61+
'RedHat': {
62+
$rh_pkg_source = pick($package_source, 'yum.postgresql.org')
63+
64+
case $rh_pkg_source {
65+
'yum.postgresql.org': {
66+
class { 'postgresql::package_source::yum_postgresql_org':
67+
version => $version
7068
}
69+
}
7170

72-
default: {
73-
fail("Unsupported package source '${rh_pkg_source}' for ${::osfamily} OS family. Currently the only supported source is 'yum.postgresql.org'")
74-
}
71+
default: {
72+
fail("Unsupported package source '${rh_pkg_source}' for ${::osfamily} OS family. Currently the only supported source is 'yum.postgresql.org'")
7573
}
7674
}
75+
}
7776

78-
'Debian': {
79-
class { 'postgresql::package_source::apt_postgresql_org': }
80-
}
77+
'Debian': {
78+
class { 'postgresql::package_source::apt_postgresql_org': }
79+
}
8180

82-
default: {
83-
fail("Unsupported osfamily: ${::osfamily} operatingsystem: ${::operatingsystem}, module ${module_name} currently only supports osfamily RedHat and Debian")
84-
}
81+
default: {
82+
fail("Unsupported osfamily: ${::osfamily} operatingsystem: ${::operatingsystem}, module ${module_name} currently only supports osfamily RedHat and Debian")
8583
}
8684
}
85+
}
8786

8887

8988
# This is a bit hacky, but if the puppet nodes don't have pluginsync enabled,
@@ -116,7 +115,6 @@
116115
$java_package_name = pick($custom_java_package_name, 'postgresql-jdbc')
117116
$service_name = pick($custom_service_name, 'postgresql')
118117
$bindir = pick($custom_bindir, '/usr/bin')
119-
$libdir = $custom_libdir
120118
$datadir = pick($custom_datadir, '/var/lib/pgsql/data')
121119
$confdir = pick($custom_confdir, $datadir)
122120
} else {
@@ -128,7 +126,6 @@
128126
$java_package_name = pick($custom_java_package_name, "postgresql${package_version}-jdbc")
129127
$service_name = pick($custom_service_name, "postgresql-${version}")
130128
$bindir = pick($custom_bindir, "/usr/pgsql-${version}/bin")
131-
$libdir = $custom_libdir
132129
$datadir = pick($custom_datadir, "/var/lib/pgsql/${version}/data")
133130
$confdir = pick($custom_confdir, $datadir)
134131
}
@@ -145,7 +142,7 @@
145142

146143
case $::operatingsystem {
147144
'Debian': {
148-
$service_name = pick($custom_service_name, 'postgresql')
145+
$service_name = pick($custom_service_name, 'postgresql')
149146
}
150147
'Ubuntu': {
151148
# thanks, ubuntu
@@ -162,7 +159,6 @@
162159
$devel_package_name = pick($custom_devel_package_name, 'libpq-dev')
163160
$java_package_name = pick($custom_java_package_name, 'libpostgresql-jdbc-java')
164161
$bindir = pick($custom_bindir, "/usr/lib/postgresql/${version}/bin")
165-
$libdir = $custom_libdir
166162
$datadir = pick($custom_datadir, "/var/lib/postgresql/${version}/main")
167163
$confdir = pick($custom_confdir, "/etc/postgresql/${version}/main")
168164
$service_status = "/etc/init.d/${service_name} status | /bin/egrep -q 'Running clusters: .+|online'"
@@ -176,7 +172,6 @@
176172
$initdb_path = "${bindir}/initdb"
177173
$createdb_path = "${bindir}/createdb"
178174
$psql_path = "${bindir}/psql"
179-
$library_path = $libdir
180175
$pg_hba_conf_path = "${confdir}/pg_hba.conf"
181176
$postgresql_conf_path = "${confdir}/postgresql.conf"
182177

manifests/psql.pp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,7 @@
3434
$no_password_option = '--no-password'
3535
}
3636

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-
}
37+
$psql = "${postgresql::params::psql_path} ${no_password_option} --tuples-only --quiet --dbname ${db}"
4338

4439
$quoted_command = regsubst($command, '"', '\\"', 'G')
4540
$quoted_unless = regsubst($unless, '"', '\\"', 'G')

manifests/role.pp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
psql_user => $postgresql::params::user,
3232
psql_group => $postgresql::params::group,
3333
psql_path => $postgresql::params::psql_path,
34-
library_path => $postgresql::params::library_path,
3534
}
3635

3736
$login_sql = $login ? { true => 'LOGIN' , default => 'NOLOGIN' }
@@ -41,9 +40,8 @@
4140

4241
# TODO: FIXME: Will not correct the superuser / createdb / createrole / login status of a role that already exists
4342
postgresql_psql {"CREATE ROLE \"${username}\" ENCRYPTED PASSWORD '${password_hash}' ${login_sql} ${createrole_sql} ${createdb_sql} ${superuser_sql}":
44-
db => $db,
45-
psql_user => $postgresql::params::user,
46-
unless => "SELECT rolname FROM pg_roles WHERE rolname='${username}'",
47-
cwd => $postgresql::params::datadir,
43+
db => $db,
44+
psql_user => $postgresql::params::user,
45+
unless => "SELECT rolname FROM pg_roles WHERE rolname='${username}'",
4846
}
4947
}

0 commit comments

Comments
 (0)