Skip to content

Commit 2114333

Browse files
committed
Add locale parameter support
This adds the parameter 'locale' to the 'postgresql' class so we have a global default, and adds it two the defined resources 'postgresql::db' and 'postgresql::database'. This allows users to either: * Defined a global default for the cluster * Define a per-database default As a side-effect I had to make sure 'charset' was also exposed in a similar manner as some locales need a particular charset to work. Tests were added to test both the 'createdb' case and 'initdb' case for Redhat, and some refactoring was done to make the existing non_default test area use heredocs so my manifests and test code was kept close together. As apposed to entirely different files and places in the directory structure. I cleaned up the related execs a little bit, adding logoutput => on_failure where needed so we can debug failures. Beforehand execs just 'failed', but now we should be able to get better feedback from failed execs helping support. I also add intention comments in parts of the Puppet code that I touched where it made sense. Signed-off-by: Ken Barber <[email protected]>
1 parent 4abcff0 commit 2114333

File tree

9 files changed

+126
-79
lines changed

9 files changed

+126
-79
lines changed

Diff for: manifests/database.pp

+14-7
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,20 @@
2020
# needs to be moved over to ruby, and add support for ensurable.
2121

2222
define postgresql::database(
23-
$dbname = $title,
24-
$charset = 'UTF8',
25-
$tablespace = undef)
26-
{
23+
$dbname = $title,
24+
$tablespace = undef,
25+
$charset = $postgresql::params::charset,
26+
$locale = $postgresql::params::locale
27+
) {
2728
include postgresql::params
2829

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.
2932
if ($postgresql::params::version != '8.1') {
30-
$locale_option = '--locale=C'
33+
$locale_option = $locale ? {
34+
undef => '',
35+
default => "--locale=${locale}",
36+
}
3137
$public_revoke_privilege = "CONNECT"
3238
} else {
3339
$locale_option = ""
@@ -52,8 +58,9 @@
5258

5359
exec { $createdb_command :
5460
refreshonly => true,
55-
user => 'postgres',
56-
cwd => $postgresql::params::datadir,
61+
user => 'postgres',
62+
cwd => $postgresql::params::datadir,
63+
logoutput => on_failure,
5764
} ~>
5865

5966
# This will prevent users from connecting to the database unless they've been

Diff for: manifests/db.pp

+7-3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
# [*charset*] - database charset. defaults to 'utf8'
1818
# [*grant*] - privilege to grant user. defaults to 'all'.
1919
# [*tablespace*] - database tablespace. default to use the template database's tablespace.
20+
# [*locale*] - locale for database. defaults to 'undef' (effectively 'C').
2021
#
2122
# Actions:
2223
#
@@ -35,10 +36,12 @@
3536
define postgresql::db (
3637
$user,
3738
$password,
38-
$charset = 'utf8',
39+
$charset = $postgresql::params::charset,
40+
$locale = $postgresql::params::locale,
3941
$grant = 'ALL',
40-
$tablespace = undef)
41-
{
42+
$tablespace = undef
43+
) {
44+
include postgresql::params
4245

4346
postgresql::database { $name:
4447
# TODO: ensure is not yet supported
@@ -47,6 +50,7 @@
4750
tablespace => $tablespace,
4851
#provider => 'postgresql',
4952
require => Class['postgresql::server'],
53+
locale => $locale,
5054
}
5155

5256
if ! defined(Postgresql::Database_user[$user]) {

Diff for: manifests/init.pp

+11-3
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,28 @@
2323
# set to `true`. It determines which package repository should
2424
# be used to install the postgres packages. Currently supported
2525
# values include `yum.postgresql.org`.
26+
# [*locale*] - This setting defines the default locale for initdb and createdb
27+
# commands. This default to 'undef' which is effectively 'C'.
28+
# [*charset*] - Sets the default charset to be used for initdb and createdb.
29+
# Defaults to 'UTF8'.
2630
# Actions:
2731
#
2832
# Requires:
2933
#
3034
# Sample Usage:
3135
#
3236
class postgresql (
33-
$version = $::postgres_default_version,
37+
$version = $::postgres_default_version,
3438
$manage_package_repo = false,
35-
$package_source = undef
39+
$package_source = undef,
40+
$locale = undef,
41+
$charset = 'UTF8'
3642
) {
3743
class { 'postgresql::params':
3844
version => $version,
3945
manage_package_repo => $manage_package_repo,
40-
package_source => $package_source
46+
package_source => $package_source,
47+
locale => $locale,
48+
charset => $charset,
4149
}
4250
}

Diff for: manifests/initdb.pp

+23-10
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,35 @@
1818

1919
class postgresql::initdb(
2020
$datadir = $postgresql::params::datadir,
21-
$encoding = 'UTF8',
21+
$encoding = $postgresql::params::charset,
2222
$group = 'postgres',
2323
$initdb_path = $postgresql::params::initdb_path,
2424
$user = 'postgres'
2525
) inherits postgresql::params {
26+
# Build up the initdb command.
27+
#
28+
# We optionally add the locale switch if specified. Older versions of the
29+
# initdb command don't accept this switch. So if the user didn't pass the
30+
# parameter, lets not pass the switch at all.
31+
$initdb_command = $postgresql::params::locale ? {
32+
undef => "${initdb_path} --encoding '${encoding}' --pgdata '${datadir}'",
33+
default => "${initdb_path} --encoding '${encoding}' --pgdata '${datadir}' --locale '${postgresql::params::locale}'"
34+
}
2635

27-
$initdb_command = "${initdb_path} --encoding '${encoding}' --pgdata '${datadir}'"
28-
29-
exec { $initdb_command:
30-
creates => "${datadir}/PG_VERSION",
31-
user => $user,
32-
group => $group
36+
# This runs the initdb command, we use the existance of the PG_VERSION file to
37+
# ensure we don't keep running this command.
38+
exec { 'postgresql_initdb':
39+
command => $initdb_command,
40+
creates => "${datadir}/PG_VERSION",
41+
user => $user,
42+
group => $group,
43+
logoutput => on_failure,
3344
}
3445

35-
if defined(Package["$postgresql::params::server_package_name"]) {
36-
Package["$postgresql::params::server_package_name"] ->
37-
Exec[$initdb_command]
46+
# If we manage the package (which is user configurable) make sure the
47+
# package exists first.
48+
if defined(Package[$postgresql::params::server_package_name]) {
49+
Package[$postgresql::params::server_package_name]->
50+
Exec['postgresql_initdb']
3851
}
3952
}

Diff for: manifests/params.pp

+5-3
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,11 @@
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
31+
$version = $::postgres_default_version,
32+
$manage_package_repo = false,
33+
$package_source = undef,
34+
$locale = undef,
35+
$charset = 'UTF8'
3436
) {
3537
$user = 'postgres'
3638
$group = 'postgres'

Diff for: spec/support/shared_examples/non_default_postgres.rb

+47-10
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,65 @@
66
include_context :pg_vm_context
77

88
# this method is required by the pg_vm shared context
9-
def install_postgres
10-
sudo_and_log(vm, 'puppet apply -e "include postgresql_tests::non_default::test_install"')
11-
end
12-
13-
describe 'postgresql::db' do
14-
it 'should idempotently create a db that we can connect to' do
9+
def install_postgres; end
1510

16-
# A bare-minimum class to add a DB to postgres, which will be running due to ubuntu
17-
test_class = 'class {"postgresql_tests::non_default::test_db": db => "postgresql_test_db" }'
11+
describe 'postgresql global config' do
12+
it 'with version and manage_package_repo set, we should be able to idempotently create a db that we can connect to' do
13+
manifest = <<-EOS
14+
# Configure version and manage_package_repo globally, install postgres
15+
# and then try to install a new database.
16+
class { "postgresql":
17+
version => "9.2",
18+
manage_package_repo => true,
19+
package_source => "yum.postgresql.org",
20+
}->
21+
class { "postgresql::server": }->
22+
postgresql::db { "postgresql_test_db":
23+
user => "foo1",
24+
password => "foo1",
25+
}
26+
EOS
1827

1928
begin
2029
# Run once to check for crashes
21-
sudo_and_log(vm, "puppet apply --detailed-exitcodes -e '#{test_class}'; [ $? == 2 ]")
30+
sudo_and_log(vm, "puppet apply --detailed-exitcodes -e '#{manifest}'; [ $? = 2 ]")
2231

2332
# Run again to check for idempotence
24-
sudo_and_log(vm, "puppet apply --detailed-exitcodes -e '#{test_class}'")
33+
sudo_and_log(vm, "puppet apply --detailed-exitcodes -e '#{manifest}'")
2534

2635
# Check that the database name is present
2736
sudo_psql_and_log(vm, 'postgresql_test_db --command="select datname from pg_database limit 1"')
2837
ensure
2938
sudo_psql_and_log(vm, '--command="drop database postgresql_test_db" postgres')
3039
end
3140
end
41+
42+
it 'with locale and charset, the postgres database should reflect that locale' do
43+
pending('no support for initdb with lucid', :if => vm == :lucid)
44+
45+
manifest = <<-EOS
46+
# Set global locale and charset option, and try installing postgres
47+
class { 'postgresql':
48+
locale => 'en_NG',
49+
charset => 'UTF8',
50+
}->
51+
class { 'postgresql::server': }
52+
EOS
53+
54+
# Run once to check for crashes
55+
sudo_and_log(vm, "puppet apply --detailed-exitcodes -e '#{manifest}'; [ $? = 2 ]")
56+
# Run again to check for idempotence
57+
sudo_and_log(vm, "puppet apply --detailed-exitcodes -e '#{manifest}'")
58+
59+
# Use the command line to create the database, instead of the puppet way
60+
# to bypass any puppet effects and make sure the default works _always_.
61+
sudo_and_log(vm, "su postgres -c 'createdb test1'")
62+
63+
# Check locale of database 'postgres' to ensure initdb did the correct
64+
# thing.
65+
sudo_and_log(vm, 'su postgres -c \'psql -c "show lc_ctype" test1\'')
66+
sudo_and_log(vm, 'su postgres -c \'psql -c "show lc_ctype" test1\' | grep en_NG')
67+
sudo_and_log(vm, 'su postgres -c \'psql -c "show lc_collate" test1\' | grep en_NG')
68+
end
3269
end
3370
end

Diff for: spec/support/shared_examples/system_default_postgres.rb

+19
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,25 @@ def install_postgres
4949
sudo_psql_and_log(vm, '--command="drop database postgresql_test_db" postgres')
5050
end
5151
end
52+
53+
it 'should take a locale parameter' do
54+
manifest = <<-EOS
55+
include postgresql::server
56+
postgresql::db { 'test1':
57+
user => 'test1',
58+
password => 'test1',
59+
charset => 'UTF8',
60+
locale => 'en_NG',
61+
}
62+
EOS
63+
sudo_and_log(vm, "puppet apply -e '#{manifest}'")
64+
65+
# Some basic tests here to check if the db indeed was created with the
66+
#rr correct locale.
67+
sudo_and_log(vm, 'su postgres -c \'psql -c "show lc_ctype" test1\'')
68+
sudo_and_log(vm, 'su postgres -c \'psql -c "show lc_ctype" test1\' | grep en_NG')
69+
sudo_and_log(vm, 'su postgres -c \'psql -c "show lc_collate" test1\' | grep en_NG')
70+
end
5271
end
5372

5473
describe 'postgresql::psql' do

Diff for: spec/system/test_module/manifests/non_default/test_db.pp

-33
This file was deleted.

Diff for: spec/system/test_module/manifests/non_default/test_install.pp

-10
This file was deleted.

0 commit comments

Comments
 (0)