Skip to content

Commit dcc1358

Browse files
Add multi instance support, refactoring late_initdb.pp
This commit adds changes a class to add multi instance support to this module. The general idea is to first copy all classes which are used and create defines from them. These classes will use the defines as is. Necessary changes for the instances itself will be added to the classes and defined types at a later point. This ensures, the module will work as it does right now and there are no breaking changes.
1 parent fb05c69 commit dcc1358

File tree

3 files changed

+73
-31
lines changed

3 files changed

+73
-31
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# lint:ignore:140chars
2+
# @summary Manage the default encoding when database initialization is managed by the package
3+
# @param encoding Sets the default encoding for all databases created with this module. On certain operating systems this is also used during the template1 initialization, so it becomes a default outside of the module as well.
4+
# @param user Overrides the default PostgreSQL super user and owner of PostgreSQL related files in the file system.
5+
# @param group Overrides the default postgres user group to be used for related files in the file system.
6+
# @param psql_path Specifies the path to the psql command.
7+
# @param port Specifies the port for the PostgreSQL server to listen on. Note: The same port number is used for all IP addresses the server listens on. Also, for Red Hat systems and early Debian systems, changing the port causes the server to come to a full stop before being able to make the change.
8+
# @param module_workdir Working directory for the PostgreSQL module
9+
# lint:endignore:140chars
10+
define postgresql::server::instance_late_initdb (
11+
$encoding = $postgresql::server::encoding,
12+
$user = $postgresql::server::user,
13+
$group = $postgresql::server::group,
14+
$psql_path = $postgresql::server::psql_path,
15+
$port = $postgresql::server::port,
16+
$module_workdir = $postgresql::server::module_workdir,
17+
) {
18+
# Set the defaults for the postgresql_psql resource
19+
Postgresql_psql {
20+
psql_user => $user,
21+
psql_group => $group,
22+
psql_path => $psql_path,
23+
port => $port,
24+
cwd => $module_workdir,
25+
}
26+
27+
# [workaround]
28+
# by default pg_createcluster encoding derived from locale
29+
# but it do does not work by installing postgresql via puppet because puppet
30+
# always override LANG to 'C'
31+
postgresql_psql { "Set template1 encoding to ${encoding}":
32+
command => "UPDATE pg_database
33+
SET datistemplate = FALSE
34+
WHERE datname = 'template1'
35+
;
36+
UPDATE pg_database
37+
SET encoding = pg_char_to_encoding('${encoding}'), datistemplate = TRUE
38+
WHERE datname = 'template1'",
39+
unless => "SELECT datname FROM pg_database WHERE
40+
datname = 'template1' AND encoding = pg_char_to_encoding('${encoding}')",
41+
before => Anchor['postgresql::server::service::end'],
42+
}
43+
}

manifests/server/late_initdb.pp

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,12 @@
44
class postgresql::server::late_initdb {
55
assert_private()
66

7-
$encoding = $postgresql::server::encoding
8-
$user = $postgresql::server::user
9-
$group = $postgresql::server::group
10-
$psql_path = $postgresql::server::psql_path
11-
$port = $postgresql::server::port
12-
$module_workdir = $postgresql::server::module_workdir
13-
14-
# Set the defaults for the postgresql_psql resource
15-
Postgresql_psql {
16-
psql_user => $user,
17-
psql_group => $group,
18-
psql_path => $psql_path,
19-
port => $port,
20-
cwd => $module_workdir,
21-
}
22-
23-
# [workaround]
24-
# by default pg_createcluster encoding derived from locale
25-
# but it do does not work by installing postgresql via puppet because puppet
26-
# always override LANG to 'C'
27-
postgresql_psql { "Set template1 encoding to ${encoding}":
28-
command => "UPDATE pg_database
29-
SET datistemplate = FALSE
30-
WHERE datname = 'template1'
31-
;
32-
UPDATE pg_database
33-
SET encoding = pg_char_to_encoding('${encoding}'), datistemplate = TRUE
34-
WHERE datname = 'template1'",
35-
unless => "SELECT datname FROM pg_database WHERE
36-
datname = 'template1' AND encoding = pg_char_to_encoding('${encoding}')",
37-
before => Anchor['postgresql::server::service::end'],
7+
postgresql::server::instance_late_initdb { 'main':
8+
encoding => $postgresql::server::encoding,
9+
user => $postgresql::server::user,
10+
group => $postgresql::server::group,
11+
psql_path => $postgresql::server::psql_path,
12+
port => $postgresql::server::port,
13+
module_workdir => $postgresql::server::module_workdir,
3814
}
3915
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
5+
describe 'postgresql::server::instance_late_initdb' do
6+
let(:title) { 'main' }
7+
8+
on_supported_os.each do |os, os_facts|
9+
context "on #{os}" do
10+
let :facts do
11+
os_facts
12+
end
13+
14+
let :pre_condition do
15+
"class {'postgresql::initdb':}"
16+
end
17+
18+
context 'with defaults from initdb class' do
19+
it { is_expected.to compile.with_all_deps }
20+
end
21+
end
22+
end
23+
end

0 commit comments

Comments
 (0)