Skip to content

Commit dce527f

Browse files
Add multi instance support, refactoring initdb.pp
This commit is the second of many 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 3ee94b6 commit dce527f

File tree

2 files changed

+175
-137
lines changed

2 files changed

+175
-137
lines changed

manifests/server/initdb.pp

Lines changed: 15 additions & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -1,141 +1,19 @@
11
# @api private
22
class postgresql::server::initdb {
3-
$needs_initdb = $postgresql::server::needs_initdb
4-
$initdb_path = $postgresql::server::initdb_path
5-
$datadir = $postgresql::server::datadir
6-
$xlogdir = $postgresql::server::xlogdir
7-
$logdir = $postgresql::server::logdir
8-
$manage_datadir = $postgresql::server::manage_datadir
9-
$manage_logdir = $postgresql::server::manage_logdir
10-
$manage_xlogdir = $postgresql::server::manage_xlogdir
11-
$encoding = $postgresql::server::encoding
12-
$locale = $postgresql::server::locale
13-
$data_checksums = $postgresql::server::data_checksums
14-
$group = $postgresql::server::group
15-
$user = $postgresql::server::user
16-
$module_workdir = $postgresql::server::module_workdir
17-
18-
if $facts['os']['family'] == 'RedHat' and $facts['os']['selinux']['enabled'] == true {
19-
$seltype = 'postgresql_db_t'
20-
$logdir_type = 'postgresql_log_t'
21-
}
22-
23-
else {
24-
$seltype = undef
25-
$logdir_type = undef
26-
}
27-
28-
if($manage_datadir) {
29-
# Make sure the data directory exists, and has the correct permissions.
30-
file { $datadir:
31-
ensure => directory,
32-
owner => $user,
33-
group => $group,
34-
mode => '0700',
35-
seltype => $seltype,
36-
}
37-
} else {
38-
# changes an already defined datadir
39-
File <| title == $datadir |> {
40-
ensure => directory,
41-
owner => $user,
42-
group => $group,
43-
mode => '0700',
44-
seltype => $seltype,
45-
}
46-
}
47-
48-
if($xlogdir) {
49-
if($manage_xlogdir) {
50-
# Make sure the xlog directory exists, and has the correct permissions.
51-
file { $xlogdir:
52-
ensure => directory,
53-
owner => $user,
54-
group => $group,
55-
mode => '0700',
56-
seltype => $seltype,
57-
}
58-
} else {
59-
# changes an already defined xlogdir
60-
File <| title == $xlogdir |> {
61-
ensure => directory,
62-
owner => $user,
63-
group => $group,
64-
mode => '0700',
65-
seltype => $seltype,
66-
}
67-
}
68-
}
69-
70-
if($logdir) {
71-
if($manage_logdir) {
72-
# Make sure the log directory exists, and has the correct permissions.
73-
file { $logdir:
74-
ensure => directory,
75-
owner => $user,
76-
group => $group,
77-
seltype => $logdir_type,
78-
}
79-
} else {
80-
# changes an already defined logdir
81-
File <| title == $logdir |> {
82-
ensure => directory,
83-
owner => $user,
84-
group => $group,
85-
seltype => $logdir_type,
86-
}
87-
}
88-
}
89-
90-
if($needs_initdb) {
91-
# Build up the initdb command.
92-
#
93-
# We optionally add the locale switch if specified. Older versions of the
94-
# initdb command don't accept this switch. So if the user didn't pass the
95-
# parameter, lets not pass the switch at all.
96-
$ic_base = "${initdb_path} --pgdata '${datadir}'"
97-
$ic_xlog = $xlogdir ? {
98-
undef => $ic_base,
99-
default => "${ic_base} -X '${xlogdir}'"
100-
}
101-
102-
# The xlogdir need to be present before initdb runs.
103-
# If xlogdir is default it's created by package installer
104-
if($xlogdir) {
105-
$require_before_initdb = [$datadir, $xlogdir]
106-
} else {
107-
$require_before_initdb = [$datadir]
108-
}
109-
110-
# PostgreSQL 11 no longer allows empty encoding
111-
$ic_encoding = $encoding ? {
112-
undef => $ic_xlog,
113-
default => "${ic_xlog} --encoding '${encoding}'"
114-
}
115-
116-
$ic_locale = $locale ? {
117-
undef => $ic_encoding,
118-
default => "${ic_encoding} --locale '${locale}'"
119-
}
120-
121-
$initdb_command = $data_checksums ? {
122-
undef => $ic_locale,
123-
false => $ic_locale,
124-
default => "${ic_locale} --data-checksums"
125-
}
126-
127-
# This runs the initdb command, we use the existance of the PG_VERSION
128-
# file to ensure we don't keep running this command.
129-
exec { 'postgresql_initdb':
130-
command => $initdb_command,
131-
creates => "${datadir}/PG_VERSION",
132-
user => $user,
133-
group => $group,
134-
logoutput => on_failure,
135-
require => File[$require_before_initdb],
136-
cwd => $module_workdir,
137-
}
138-
} elsif $encoding != undef {
139-
include postgresql::server::late_initdb
3+
postgresql::server::instance_initdb { 'main':
4+
needs_initdb => $postgresql::server::needs_initdb,
5+
initdb_path => $postgresql::server::initdb_path,
6+
datadir => $postgresql::server::datadir,
7+
xlogdir => $postgresql::server::xlogdir,
8+
logdir => $postgresql::server::logdir,
9+
manage_datadir => $postgresql::server::manage_datadir,
10+
manage_logdir => $postgresql::server::manage_logdir,
11+
manage_xlogdir => $postgresql::server::manage_xlogdir,
12+
encoding => $postgresql::server::encoding,
13+
locale => $postgresql::server::locale,
14+
data_checksums => $postgresql::server::data_checksums,
15+
group => $postgresql::server::group,
16+
user => $postgresql::server::user,
17+
module_workdir => $postgresql::server::module_workdir,
14018
}
14119
}

manifests/server/instance_initdb.pp

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
# lint:ignore:140chars
2+
# @param needs_initdb Explicitly calls the initdb operation after server package is installed
3+
# and before the PostgreSQL service is started.
4+
# @param initdb_path Specifies the path to the initdb command.
5+
# @param datadir PostgreSQL data directory
6+
# @param xlogdir PostgreSQL xlog directory
7+
# @param logdir PostgreSQL log directory
8+
# @param manage_datadir Set to false if you have file{ $datadir: } already defined
9+
# @param manage_logdir Set to false if you have file{ $logdir: } already defined
10+
# @param manage_xlogdir Set to false if you have file{ $xlogdir: } already defined
11+
# @param encoding Sets the default encoding for all databases created with this module.
12+
# On certain operating systems this is also used during the template1 initialization, so it becomes a default outside of the module as well.
13+
# @param locale Sets the default database locale for all databases created with this module.
14+
# On certain operating systems this is used during the template1 initialization as well, so it becomes a default outside of the module.
15+
# @param data_checksums Boolean. Use checksums on data pages to help detect corruption by the I/O system that would otherwise be silent.
16+
# Warning: This option is used during initialization by initdb, and cannot be changed later. If set, checksums are calculated for all objects, in all databases.
17+
# @param user Overrides the default PostgreSQL super user and owner of PostgreSQL related files in the file system.
18+
# @param group Overrides the default postgres user group to be used for related files in the file system.
19+
# @param module_workdir Working directory for the PostgreSQL module
20+
# lint:endignore:140chars
21+
define postgresql::server::instance_initdb (
22+
Boolean $needs_initdb = $postgresql::server::needs_initdb,
23+
$initdb_path = $postgresql::server::initdb_path,
24+
$datadir = $postgresql::server::datadir,
25+
$xlogdir = $postgresql::server::xlogdir,
26+
$logdir = $postgresql::server::logdir,
27+
Boolean $manage_datadir = $postgresql::server::manage_datadir,
28+
Boolean $manage_logdir = $postgresql::server::manage_logdir,
29+
Boolean $manage_xlogdir = $postgresql::server::manage_xlogdir,
30+
$encoding = $postgresql::server::encoding,
31+
$locale = $postgresql::server::locale,
32+
Boolean $data_checksums = $postgresql::server::data_checksums,
33+
$group = $postgresql::server::group,
34+
$user = $postgresql::server::user,
35+
$module_workdir = $postgresql::server::module_workdir,
36+
) {
37+
if $facts['os']['family'] == 'RedHat' and $facts['os']['selinux']['enabled'] == true {
38+
$seltype = 'postgresql_db_t'
39+
$logdir_type = 'postgresql_log_t'
40+
}
41+
42+
else {
43+
$seltype = undef
44+
$logdir_type = undef
45+
}
46+
47+
if($manage_datadir) {
48+
# Make sure the data directory exists, and has the correct permissions.
49+
file { $datadir:
50+
ensure => directory,
51+
owner => $user,
52+
group => $group,
53+
mode => '0700',
54+
seltype => $seltype,
55+
}
56+
} else {
57+
# changes an already defined datadir
58+
File <| title == $datadir |> {
59+
ensure => directory,
60+
owner => $user,
61+
group => $group,
62+
mode => '0700',
63+
seltype => $seltype,
64+
}
65+
}
66+
67+
if($xlogdir) {
68+
if($manage_xlogdir) {
69+
# Make sure the xlog directory exists, and has the correct permissions.
70+
file { $xlogdir:
71+
ensure => directory,
72+
owner => $user,
73+
group => $group,
74+
mode => '0700',
75+
seltype => $seltype,
76+
}
77+
} else {
78+
# changes an already defined xlogdir
79+
File <| title == $xlogdir |> {
80+
ensure => directory,
81+
owner => $user,
82+
group => $group,
83+
mode => '0700',
84+
seltype => $seltype,
85+
}
86+
}
87+
}
88+
89+
if($logdir) {
90+
if($manage_logdir) {
91+
# Make sure the log directory exists, and has the correct permissions.
92+
file { $logdir:
93+
ensure => directory,
94+
owner => $user,
95+
group => $group,
96+
seltype => $logdir_type,
97+
}
98+
} else {
99+
# changes an already defined logdir
100+
File <| title == $logdir |> {
101+
ensure => directory,
102+
owner => $user,
103+
group => $group,
104+
seltype => $logdir_type,
105+
}
106+
}
107+
}
108+
109+
if($needs_initdb) {
110+
# Build up the initdb command.
111+
#
112+
# We optionally add the locale switch if specified. Older versions of the
113+
# initdb command don't accept this switch. So if the user didn't pass the
114+
# parameter, lets not pass the switch at all.
115+
$ic_base = "${initdb_path} --pgdata '${datadir}'"
116+
$ic_xlog = $xlogdir ? {
117+
undef => $ic_base,
118+
default => "${ic_base} -X '${xlogdir}'"
119+
}
120+
121+
# The xlogdir need to be present before initdb runs.
122+
# If xlogdir is default it's created by package installer
123+
if($xlogdir) {
124+
$require_before_initdb = [$datadir, $xlogdir]
125+
} else {
126+
$require_before_initdb = [$datadir]
127+
}
128+
129+
# PostgreSQL 11 no longer allows empty encoding
130+
$ic_encoding = $encoding ? {
131+
undef => $ic_xlog,
132+
default => "${ic_xlog} --encoding '${encoding}'"
133+
}
134+
135+
$ic_locale = $locale ? {
136+
undef => $ic_encoding,
137+
default => "${ic_encoding} --locale '${locale}'"
138+
}
139+
140+
$initdb_command = $data_checksums ? {
141+
undef => $ic_locale,
142+
false => $ic_locale,
143+
default => "${ic_locale} --data-checksums"
144+
}
145+
146+
# This runs the initdb command, we use the existance of the PG_VERSION
147+
# file to ensure we don't keep running this command.
148+
exec { 'postgresql_initdb':
149+
command => $initdb_command,
150+
creates => "${datadir}/PG_VERSION",
151+
user => $user,
152+
group => $group,
153+
logoutput => on_failure,
154+
require => File[$require_before_initdb],
155+
cwd => $module_workdir,
156+
}
157+
} elsif $encoding != undef {
158+
include postgresql::server::late_initdb
159+
}
160+
}

0 commit comments

Comments
 (0)