Skip to content

Commit 181fbd2

Browse files
added parameters to initdb
postgresql::server::instance::initdb define: * auth_host * auth_local * lc_messages * username postgresql::server::initdb class: * auth_host * auth_local * lc_messages * username postgresql::server class: * auth_host * auth_local * lc_messages * username refactoring postgresql::server::instance::initdb define: * unnecessary conditions * remove brackets from if conditions * parameter checking and initdb command concatination
1 parent 9c0dae1 commit 181fbd2

File tree

3 files changed

+64
-29
lines changed

3 files changed

+64
-29
lines changed

manifests/server.pp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@
8787
# @param version Deprecated. Use postgresql::globals instead. Sets PostgreSQL version
8888
#
8989
# @param extra_systemd_config Adds extra config to systemd config file, can for instance be used to add extra openfiles. This can be a multi line string
90+
# @param auth_host auth method used by default for host authorization
91+
# @param auth_local auth method used by default for local authorization
92+
# @param lc_messages locale used for logging and system messages
93+
# @param username username of user running the postgres instance
9094
#
9195
class postgresql::server (
9296
Optional[Variant[String[1], Sensitive[String[1]], Integer]] $postgres_password = undef,
@@ -136,9 +140,13 @@
136140

137141
Boolean $needs_initdb = $postgresql::params::needs_initdb,
138142

143+
Optional[String[1]] $auth_host = undef,
144+
Optional[String[1]] $auth_local = undef,
139145
Optional[String[1]] $encoding = $postgresql::params::encoding,
140146
Optional[String[1]] $locale = $postgresql::params::locale,
147+
Optional[String[1]] $lc_messages = undef,
141148
Optional[Boolean] $data_checksums = $postgresql::params::data_checksums,
149+
Optional[String[1]] $username = undef,
142150
Optional[String[1]] $timezone = $postgresql::params::timezone,
143151

144152
Boolean $manage_pg_hba_conf = $postgresql::params::manage_pg_hba_conf,

manifests/server/initdb.pp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# @api private
22
class postgresql::server::initdb {
33
postgresql::server::instance::initdb { 'main':
4+
auth_host => $postgresql::server::auth_host,
5+
auth_local => $postgresql::server::auth_local,
46
needs_initdb => $postgresql::server::needs_initdb,
57
initdb_path => $postgresql::server::initdb_path,
68
datadir => $postgresql::server::datadir,
@@ -10,10 +12,12 @@
1012
manage_logdir => $postgresql::server::manage_logdir,
1113
manage_xlogdir => $postgresql::server::manage_xlogdir,
1214
encoding => $postgresql::server::encoding,
15+
lc_messages => $postgresql::server::lc_messages,
1316
locale => $postgresql::server::locale,
1417
data_checksums => $postgresql::server::data_checksums,
1518
group => $postgresql::server::group,
1619
user => $postgresql::server::user,
20+
username => $postgresql::server::username,
1721
module_workdir => $postgresql::server::module_workdir,
1822
}
1923
}

manifests/server/instance/initdb.pp

Lines changed: 52 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
# @param user Overrides the default PostgreSQL super user and owner of PostgreSQL related files in the file system.
1818
# @param group Overrides the default postgres user group to be used for related files in the file system.
1919
# @param module_workdir Working directory for the PostgreSQL module
20+
# @param auth_host auth method used by default for host authorization
21+
# @param auth_local auth method used by default for local authorization
22+
# @param lc_messages locale used for logging and system messages
23+
# @param username username of user running the postgres instance
2024
# lint:endignore:140chars
2125
define postgresql::server::instance::initdb (
2226
Boolean $needs_initdb = $postgresql::server::needs_initdb,
@@ -37,14 +41,12 @@
3741
if $facts['os']['family'] == 'RedHat' and $facts['os']['selinux']['enabled'] == true {
3842
$seltype = 'postgresql_db_t'
3943
$logdir_type = 'postgresql_log_t'
40-
}
41-
42-
else {
44+
} else {
4345
$seltype = undef
4446
$logdir_type = undef
4547
}
4648

47-
if($manage_datadir) {
49+
if $manage_datadir {
4850
# Make sure the data directory exists, and has the correct permissions.
4951
file { $datadir:
5052
ensure => directory,
@@ -64,8 +66,11 @@
6466
}
6567
}
6668

67-
if($xlogdir) {
68-
if($manage_xlogdir) {
69+
if $xlogdir {
70+
# The xlogdir need to be present before initdb runs.
71+
# If xlogdir is default it's created by package installer
72+
$require_before_initdb = [$datadir, $xlogdir]
73+
if$manage_xlogdir {
6974
# Make sure the xlog directory exists, and has the correct permissions.
7075
file { $xlogdir:
7176
ensure => directory,
@@ -84,10 +89,12 @@
8489
seltype => $seltype,
8590
}
8691
}
92+
} else {
93+
$require_before_initdb = [$datadir]
8794
}
8895

89-
if($logdir) {
90-
if($manage_logdir) {
96+
if $logdir {
97+
if $manage_logdir {
9198
# Make sure the log directory exists, and has the correct permissions.
9299
file { $logdir:
93100
ensure => directory,
@@ -106,43 +113,59 @@
106113
}
107114
}
108115

109-
if($needs_initdb) {
116+
if $needs_initdb {
110117
# Build up the initdb command.
111118
#
112119
# We optionally add the locale switch if specified. Older versions of the
113120
# initdb command don't accept this switch. So if the user didn't pass the
114121
# 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}'"
122+
123+
$auth_host_parameter = $auth_host ? {
124+
undef => undef,
125+
default => "--auth-host '${auth_host}'"
119126
}
120127

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]
128+
$auth_local_parameter = $auth_local ? {
129+
undef => undef,
130+
default => "--auth-local '${auth_local}'"
131+
}
132+
133+
$data_checksums_parameter = $data_checksums ? {
134+
undef => undef,
135+
false => undef,
136+
default => '--data-checksums'
127137
}
128138

139+
$datadir_parameter = "--pgdata '${datadir}'"
140+
129141
# PostgreSQL 11 no longer allows empty encoding
130-
$ic_encoding = $encoding ? {
131-
undef => $ic_xlog,
132-
default => "${ic_xlog} --encoding '${encoding}'"
142+
$encoding_parameter = $encoding ? {
143+
undef => undef,
144+
default => "--encoding '${encoding}'"
133145
}
134146

135-
$ic_locale = $locale ? {
136-
undef => $ic_encoding,
137-
default => "${ic_encoding} --locale '${locale}'"
147+
$lc_messages_parameter = $locale ? {
148+
undef => undef,
149+
default => "--lc-messages '${lc_messages}'"
138150
}
139151

140-
$initdb_command = $data_checksums ? {
141-
undef => $ic_locale,
142-
false => $ic_locale,
143-
default => "${ic_locale} --data-checksums"
152+
$locale_parameter = $locale ? {
153+
undef => undef,
154+
default => "--locale '${locale}'"
144155
}
145156

157+
$username_parameter = $username ? {
158+
undef => undef,
159+
default => "--username '${username}'"
160+
}
161+
162+
$xlogdir_parameter = $xlogdir ? {
163+
undef => undef,
164+
default => "-X '${xlogdir}'"
165+
}
166+
167+
$initdb_command = "${initdb_path} ${auth_host_parameter} ${auth_local_parameter } ${data_checksums_parameter} ${datadir_parameter} ${encoding_parameter} ${lc_messages_parameter} ${locale_parameter} ${username_parameter} ${xlogdir_parameter}" # lint:ignore:140chars
168+
146169
# This runs the initdb command, we use the existance of the PG_VERSION
147170
# file to ensure we don't keep running this command.
148171
exec { 'postgresql_initdb':

0 commit comments

Comments
 (0)