Skip to content

Commit

Permalink
added parameters to initdb
Browse files Browse the repository at this point in the history
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
  • Loading branch information
SimonHoenscheid committed Jun 27, 2023
1 parent 9c0dae1 commit 181fbd2
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 29 deletions.
8 changes: 8 additions & 0 deletions manifests/server.pp
Expand Up @@ -87,6 +87,10 @@
# @param version Deprecated. Use postgresql::globals instead. Sets PostgreSQL version
#
# @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
# @param auth_host auth method used by default for host authorization
# @param auth_local auth method used by default for local authorization
# @param lc_messages locale used for logging and system messages
# @param username username of user running the postgres instance
#
class postgresql::server (
Optional[Variant[String[1], Sensitive[String[1]], Integer]] $postgres_password = undef,
Expand Down Expand Up @@ -136,9 +140,13 @@

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

Optional[String[1]] $auth_host = undef,
Optional[String[1]] $auth_local = undef,
Optional[String[1]] $encoding = $postgresql::params::encoding,
Optional[String[1]] $locale = $postgresql::params::locale,
Optional[String[1]] $lc_messages = undef,
Optional[Boolean] $data_checksums = $postgresql::params::data_checksums,
Optional[String[1]] $username = undef,
Optional[String[1]] $timezone = $postgresql::params::timezone,

Boolean $manage_pg_hba_conf = $postgresql::params::manage_pg_hba_conf,
Expand Down
4 changes: 4 additions & 0 deletions manifests/server/initdb.pp
@@ -1,6 +1,8 @@
# @api private
class postgresql::server::initdb {
postgresql::server::instance::initdb { 'main':
auth_host => $postgresql::server::auth_host,
auth_local => $postgresql::server::auth_local,
needs_initdb => $postgresql::server::needs_initdb,
initdb_path => $postgresql::server::initdb_path,
datadir => $postgresql::server::datadir,
Expand All @@ -10,10 +12,12 @@
manage_logdir => $postgresql::server::manage_logdir,
manage_xlogdir => $postgresql::server::manage_xlogdir,
encoding => $postgresql::server::encoding,
lc_messages => $postgresql::server::lc_messages,
locale => $postgresql::server::locale,
data_checksums => $postgresql::server::data_checksums,
group => $postgresql::server::group,
user => $postgresql::server::user,
username => $postgresql::server::username,
module_workdir => $postgresql::server::module_workdir,
}
}
81 changes: 52 additions & 29 deletions manifests/server/instance/initdb.pp
Expand Up @@ -17,6 +17,10 @@
# @param user Overrides the default PostgreSQL super user and owner of PostgreSQL related files in the file system.
# @param group Overrides the default postgres user group to be used for related files in the file system.
# @param module_workdir Working directory for the PostgreSQL module
# @param auth_host auth method used by default for host authorization
# @param auth_local auth method used by default for local authorization

Check warning on line 21 in manifests/server/instance/initdb.pp

View workflow job for this annotation

GitHub Actions / Spec / Spec tests (Puppet: ~> 7.24, Ruby Ver: 2.7)

No matching defined type parameter for documentation of postgresql::server::instance::initdb::auth_local (check: parameter_documentation)

Check warning on line 21 in manifests/server/instance/initdb.pp

View workflow job for this annotation

GitHub Actions / Spec / Spec tests (Puppet: ~> 8.0, Ruby Ver: 3.2)

No matching defined type parameter for documentation of postgresql::server::instance::initdb::auth_local (check: parameter_documentation)
# @param lc_messages locale used for logging and system messages

Check warning on line 22 in manifests/server/instance/initdb.pp

View workflow job for this annotation

GitHub Actions / Spec / Spec tests (Puppet: ~> 7.24, Ruby Ver: 2.7)

No matching defined type parameter for documentation of postgresql::server::instance::initdb::lc_messages (check: parameter_documentation)

Check warning on line 22 in manifests/server/instance/initdb.pp

View workflow job for this annotation

GitHub Actions / Spec / Spec tests (Puppet: ~> 8.0, Ruby Ver: 3.2)

No matching defined type parameter for documentation of postgresql::server::instance::initdb::lc_messages (check: parameter_documentation)
# @param username username of user running the postgres instance

Check warning on line 23 in manifests/server/instance/initdb.pp

View workflow job for this annotation

GitHub Actions / Spec / Spec tests (Puppet: ~> 7.24, Ruby Ver: 2.7)

No matching defined type parameter for documentation of postgresql::server::instance::initdb::username (check: parameter_documentation)

Check warning on line 23 in manifests/server/instance/initdb.pp

View workflow job for this annotation

GitHub Actions / Spec / Spec tests (Puppet: ~> 8.0, Ruby Ver: 3.2)

No matching defined type parameter for documentation of postgresql::server::instance::initdb::username (check: parameter_documentation)
# lint:endignore:140chars
define postgresql::server::instance::initdb (
Boolean $needs_initdb = $postgresql::server::needs_initdb,
Expand All @@ -37,14 +41,12 @@
if $facts['os']['family'] == 'RedHat' and $facts['os']['selinux']['enabled'] == true {
$seltype = 'postgresql_db_t'
$logdir_type = 'postgresql_log_t'
}

else {
} else {
$seltype = undef
$logdir_type = undef
}

if($manage_datadir) {
if $manage_datadir {
# Make sure the data directory exists, and has the correct permissions.
file { $datadir:
ensure => directory,
Expand All @@ -64,8 +66,11 @@
}
}

if($xlogdir) {
if($manage_xlogdir) {
if $xlogdir {
# The xlogdir need to be present before initdb runs.
# If xlogdir is default it's created by package installer
$require_before_initdb = [$datadir, $xlogdir]
if$manage_xlogdir {
# Make sure the xlog directory exists, and has the correct permissions.
file { $xlogdir:
ensure => directory,
Expand All @@ -84,10 +89,12 @@
seltype => $seltype,
}
}
} else {
$require_before_initdb = [$datadir]
}

if($logdir) {
if($manage_logdir) {
if $logdir {
if $manage_logdir {
# Make sure the log directory exists, and has the correct permissions.
file { $logdir:
ensure => directory,
Expand All @@ -106,43 +113,59 @@
}
}

if($needs_initdb) {
if $needs_initdb {
# Build up the initdb command.
#
# We optionally add the locale switch if specified. Older versions of the
# initdb command don't accept this switch. So if the user didn't pass the
# parameter, lets not pass the switch at all.
$ic_base = "${initdb_path} --pgdata '${datadir}'"
$ic_xlog = $xlogdir ? {
undef => $ic_base,
default => "${ic_base} -X '${xlogdir}'"

$auth_host_parameter = $auth_host ? {

Check warning on line 123 in manifests/server/instance/initdb.pp

View workflow job for this annotation

GitHub Actions / Spec / Spec tests (Puppet: ~> 7.24, Ruby Ver: 2.7)

top-scope variable being used without an explicit namespace (check: variable_scope)

Check warning on line 123 in manifests/server/instance/initdb.pp

View workflow job for this annotation

GitHub Actions / Spec / Spec tests (Puppet: ~> 8.0, Ruby Ver: 3.2)

top-scope variable being used without an explicit namespace (check: variable_scope)
undef => undef,
default => "--auth-host '${auth_host}'"

Check warning on line 125 in manifests/server/instance/initdb.pp

View workflow job for this annotation

GitHub Actions / Spec / Spec tests (Puppet: ~> 7.24, Ruby Ver: 2.7)

top-scope variable being used without an explicit namespace (check: variable_scope)

Check warning on line 125 in manifests/server/instance/initdb.pp

View workflow job for this annotation

GitHub Actions / Spec / Spec tests (Puppet: ~> 8.0, Ruby Ver: 3.2)

top-scope variable being used without an explicit namespace (check: variable_scope)
}

# The xlogdir need to be present before initdb runs.
# If xlogdir is default it's created by package installer
if($xlogdir) {
$require_before_initdb = [$datadir, $xlogdir]
} else {
$require_before_initdb = [$datadir]
$auth_local_parameter = $auth_local ? {

Check warning on line 128 in manifests/server/instance/initdb.pp

View workflow job for this annotation

GitHub Actions / Spec / Spec tests (Puppet: ~> 7.24, Ruby Ver: 2.7)

top-scope variable being used without an explicit namespace (check: variable_scope)

Check warning on line 128 in manifests/server/instance/initdb.pp

View workflow job for this annotation

GitHub Actions / Spec / Spec tests (Puppet: ~> 8.0, Ruby Ver: 3.2)

top-scope variable being used without an explicit namespace (check: variable_scope)
undef => undef,
default => "--auth-local '${auth_local}'"

Check warning on line 130 in manifests/server/instance/initdb.pp

View workflow job for this annotation

GitHub Actions / Spec / Spec tests (Puppet: ~> 7.24, Ruby Ver: 2.7)

top-scope variable being used without an explicit namespace (check: variable_scope)

Check warning on line 130 in manifests/server/instance/initdb.pp

View workflow job for this annotation

GitHub Actions / Spec / Spec tests (Puppet: ~> 8.0, Ruby Ver: 3.2)

top-scope variable being used without an explicit namespace (check: variable_scope)
}

$data_checksums_parameter = $data_checksums ? {
undef => undef,
false => undef,
default => '--data-checksums'
}

$datadir_parameter = "--pgdata '${datadir}'"

# PostgreSQL 11 no longer allows empty encoding
$ic_encoding = $encoding ? {
undef => $ic_xlog,
default => "${ic_xlog} --encoding '${encoding}'"
$encoding_parameter = $encoding ? {
undef => undef,
default => "--encoding '${encoding}'"
}

$ic_locale = $locale ? {
undef => $ic_encoding,
default => "${ic_encoding} --locale '${locale}'"
$lc_messages_parameter = $locale ? {
undef => undef,
default => "--lc-messages '${lc_messages}'"

Check warning on line 149 in manifests/server/instance/initdb.pp

View workflow job for this annotation

GitHub Actions / Spec / Spec tests (Puppet: ~> 7.24, Ruby Ver: 2.7)

top-scope variable being used without an explicit namespace (check: variable_scope)

Check warning on line 149 in manifests/server/instance/initdb.pp

View workflow job for this annotation

GitHub Actions / Spec / Spec tests (Puppet: ~> 8.0, Ruby Ver: 3.2)

top-scope variable being used without an explicit namespace (check: variable_scope)
}

$initdb_command = $data_checksums ? {
undef => $ic_locale,
false => $ic_locale,
default => "${ic_locale} --data-checksums"
$locale_parameter = $locale ? {
undef => undef,
default => "--locale '${locale}'"
}

$username_parameter = $username ? {

Check warning on line 157 in manifests/server/instance/initdb.pp

View workflow job for this annotation

GitHub Actions / Spec / Spec tests (Puppet: ~> 7.24, Ruby Ver: 2.7)

top-scope variable being used without an explicit namespace (check: variable_scope)

Check warning on line 157 in manifests/server/instance/initdb.pp

View workflow job for this annotation

GitHub Actions / Spec / Spec tests (Puppet: ~> 8.0, Ruby Ver: 3.2)

top-scope variable being used without an explicit namespace (check: variable_scope)
undef => undef,
default => "--username '${username}'"

Check warning on line 159 in manifests/server/instance/initdb.pp

View workflow job for this annotation

GitHub Actions / Spec / Spec tests (Puppet: ~> 7.24, Ruby Ver: 2.7)

top-scope variable being used without an explicit namespace (check: variable_scope)

Check warning on line 159 in manifests/server/instance/initdb.pp

View workflow job for this annotation

GitHub Actions / Spec / Spec tests (Puppet: ~> 8.0, Ruby Ver: 3.2)

top-scope variable being used without an explicit namespace (check: variable_scope)
}

$xlogdir_parameter = $xlogdir ? {
undef => undef,
default => "-X '${xlogdir}'"
}

$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

# This runs the initdb command, we use the existance of the PG_VERSION
# file to ensure we don't keep running this command.
exec { 'postgresql_initdb':
Expand Down

0 comments on commit 181fbd2

Please sign in to comment.