Skip to content

Commit 930e911

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 930e911

File tree

3 files changed

+68
-29
lines changed

3 files changed

+68
-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: 56 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,14 @@
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 (
26+
Optional[String[1]] $auth_host = $postgresql::server::auth_host,
27+
Optional[String[1]] $auth_local = $postgresql::server::auth_local,
2228
Boolean $needs_initdb = $postgresql::server::needs_initdb,
2329
Variant[String[1], Stdlib::Absolutepath] $initdb_path = $postgresql::server::initdb_path,
2430
String[1] $datadir = $postgresql::server::datadir,
@@ -28,23 +34,23 @@
2834
Boolean $manage_logdir = $postgresql::server::manage_logdir,
2935
Boolean $manage_xlogdir = $postgresql::server::manage_xlogdir,
3036
Optional[String[1]] $encoding = $postgresql::server::encoding,
37+
Optional[String[1]] $lc_messages = $postgresql::server::lc_messages,
3138
Optional[String[1]] $locale = $postgresql::server::locale,
3239
Optional[Boolean] $data_checksums = $postgresql::server::data_checksums,
3340
String[1] $group = $postgresql::server::group,
3441
String[1] $user = $postgresql::server::user,
42+
Optional[String[1]] $username = $postgresql::server::username,
3543
String[1] $module_workdir = $postgresql::server::module_workdir,
3644
) {
3745
if $facts['os']['family'] == 'RedHat' and $facts['os']['selinux']['enabled'] == true {
3846
$seltype = 'postgresql_db_t'
3947
$logdir_type = 'postgresql_log_t'
40-
}
41-
42-
else {
48+
} else {
4349
$seltype = undef
4450
$logdir_type = undef
4551
}
4652

47-
if($manage_datadir) {
53+
if $manage_datadir {
4854
# Make sure the data directory exists, and has the correct permissions.
4955
file { $datadir:
5056
ensure => directory,
@@ -64,8 +70,11 @@
6470
}
6571
}
6672

67-
if($xlogdir) {
68-
if($manage_xlogdir) {
73+
if $xlogdir {
74+
# The xlogdir need to be present before initdb runs.
75+
# If xlogdir is default it's created by package installer
76+
$require_before_initdb = [$datadir, $xlogdir]
77+
if$manage_xlogdir {
6978
# Make sure the xlog directory exists, and has the correct permissions.
7079
file { $xlogdir:
7180
ensure => directory,
@@ -84,10 +93,12 @@
8493
seltype => $seltype,
8594
}
8695
}
96+
} else {
97+
$require_before_initdb = [$datadir]
8798
}
8899

89-
if($logdir) {
90-
if($manage_logdir) {
100+
if $logdir {
101+
if $manage_logdir {
91102
# Make sure the log directory exists, and has the correct permissions.
92103
file { $logdir:
93104
ensure => directory,
@@ -106,43 +117,59 @@
106117
}
107118
}
108119

109-
if($needs_initdb) {
120+
if $needs_initdb {
110121
# Build up the initdb command.
111122
#
112123
# We optionally add the locale switch if specified. Older versions of the
113124
# initdb command don't accept this switch. So if the user didn't pass the
114125
# 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}'"
126+
127+
$auth_host_parameter = $auth_host ? {
128+
undef => undef,
129+
default => "--auth-host '${auth_host}'"
119130
}
120131

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]
132+
$auth_local_parameter = $auth_local ? {
133+
undef => undef,
134+
default => "--auth-local '${auth_local}'"
135+
}
136+
137+
$data_checksums_parameter = $data_checksums ? {
138+
undef => undef,
139+
false => undef,
140+
default => '--data-checksums'
127141
}
128142

143+
$datadir_parameter = "--pgdata '${datadir}'"
144+
129145
# PostgreSQL 11 no longer allows empty encoding
130-
$ic_encoding = $encoding ? {
131-
undef => $ic_xlog,
132-
default => "${ic_xlog} --encoding '${encoding}'"
146+
$encoding_parameter = $encoding ? {
147+
undef => undef,
148+
default => "--encoding '${encoding}'"
133149
}
134150

135-
$ic_locale = $locale ? {
136-
undef => $ic_encoding,
137-
default => "${ic_encoding} --locale '${locale}'"
151+
$lc_messages_parameter = $locale ? {
152+
undef => undef,
153+
default => "--lc-messages '${lc_messages}'"
138154
}
139155

140-
$initdb_command = $data_checksums ? {
141-
undef => $ic_locale,
142-
false => $ic_locale,
143-
default => "${ic_locale} --data-checksums"
156+
$locale_parameter = $locale ? {
157+
undef => undef,
158+
default => "--locale '${locale}'"
144159
}
145160

161+
$username_parameter = $username ? {
162+
undef => undef,
163+
default => "--username '${username}'"
164+
}
165+
166+
$xlogdir_parameter = $xlogdir ? {
167+
undef => undef,
168+
default => "-X '${xlogdir}'"
169+
}
170+
171+
$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
172+
146173
# This runs the initdb command, we use the existance of the PG_VERSION
147174
# file to ensure we don't keep running this command.
148175
exec { 'postgresql_initdb':

0 commit comments

Comments
 (0)