|
| 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 | + Optional[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