newer pg module
[mirror/dsa-puppet.git] / 3rdparty / modules / postgresql / manifests / server / initdb.pp
1 # PRIVATE CLASS: do not call directly
2 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   $encoding       = $postgresql::server::encoding
9   $locale         = $postgresql::server::locale
10   $data_checksums = $postgresql::server::data_checksums
11   $group          = $postgresql::server::group
12   $user           = $postgresql::server::user
13   $psql_path      = $postgresql::server::psql_path
14   $port           = $postgresql::server::port
15   $module_workdir = $postgresql::server::module_workdir
16
17   # Set the defaults for the postgresql_psql resource
18   Postgresql_psql {
19     psql_user  => $user,
20     psql_group => $group,
21     psql_path  => $psql_path,
22     port       => $port,
23     cwd        => $module_workdir,
24   }
25
26   if $::osfamily == 'RedHat' and $::selinux == true {
27     $seltype = 'postgresql_db_t'
28     $logdir_type = 'postgresql_log_t'
29   }
30
31   else {
32     $seltype = undef
33     $logdir_type = undef
34   }
35
36   # Make sure the data directory exists, and has the correct permissions.
37   file { $datadir:
38     ensure  => directory,
39     owner   => $user,
40     group   => $group,
41     mode    => '0700',
42     seltype => $seltype,
43   }
44
45   if($xlogdir) {
46     # Make sure the xlog directory exists, and has the correct permissions.
47     file { $xlogdir:
48       ensure  => directory,
49       owner   => $user,
50       group   => $group,
51       mode    => '0700',
52       seltype => $seltype,
53     }
54   }
55
56   if($logdir) {
57     # Make sure the log directory exists, and has the correct permissions.
58     file { $logdir:
59       ensure  => directory,
60       owner   => $user,
61       group   => $group,
62       seltype => $logdir_type,
63     }
64   }
65
66   if($needs_initdb) {
67     # Build up the initdb command.
68     #
69     # We optionally add the locale switch if specified. Older versions of the
70     # initdb command don't accept this switch. So if the user didn't pass the
71     # parameter, lets not pass the switch at all.
72     $ic_base = "${initdb_path} --encoding '${encoding}' --pgdata '${datadir}'"
73     $ic_xlog = $xlogdir ? {
74       undef   => $ic_base,
75       default => "${ic_base} --xlogdir '${xlogdir}'"
76     }
77
78     # The xlogdir need to be present before initdb runs.
79     # If xlogdir is default it's created by package installer
80     if($xlogdir) {
81       $require_before_initdb = [$datadir, $xlogdir]
82     } else {
83       $require_before_initdb = [$datadir]
84     }
85
86     $ic_locale = $locale ? {
87       undef   => $ic_xlog,
88       default => "${ic_xlog} --locale '${locale}'"
89     }
90
91     $initdb_command = $data_checksums ? {
92       undef   => $ic_locale,
93       false   => $ic_locale,
94       default => "${ic_locale} --data-checksums"
95     }
96
97     # This runs the initdb command, we use the existance of the PG_VERSION
98     # file to ensure we don't keep running this command.
99     exec { 'postgresql_initdb':
100       command   => $initdb_command,
101       creates   => "${datadir}/PG_VERSION",
102       user      => $user,
103       group     => $group,
104       logoutput => on_failure,
105       require   => File[$require_before_initdb],
106       cwd       => $module_workdir,
107     }
108     # The package will take care of this for us the first time, but if we
109     # ever need to init a new db we need to copy these files explicitly
110     if $::operatingsystem == 'Debian' or $::operatingsystem == 'Ubuntu' {
111       if $::operatingsystemrelease =~ /^6/ or $::operatingsystemrelease =~ /^7/ or $::operatingsystemrelease =~ /^10\.04/ or $::operatingsystemrelease =~ /^12\.04/ {
112         file { 'server.crt':
113           ensure  => file,
114           path    => "${datadir}/server.crt",
115           source  => 'file:///etc/ssl/certs/ssl-cert-snakeoil.pem',
116           owner   => $::postgresql::server::user,
117           group   => $::postgresql::server::group,
118           mode    => '0644',
119           require => Exec['postgresql_initdb'],
120         }
121         file { 'server.key':
122           ensure  => file,
123           path    => "${datadir}/server.key",
124           source  => 'file:///etc/ssl/private/ssl-cert-snakeoil.key',
125           owner   => $::postgresql::server::user,
126           group   => $::postgresql::server::group,
127           mode    => '0600',
128           require => Exec['postgresql_initdb'],
129         }
130       }
131     }
132   } elsif $encoding != undef {
133     # [workaround]
134     # by default pg_createcluster encoding derived from locale
135     # but it do does not work by installing postgresql via puppet because puppet
136     # always override LANG to 'C'
137     postgresql_psql { "Set template1 encoding to ${encoding}":
138       command => "UPDATE pg_database
139         SET datistemplate = FALSE
140         WHERE datname = 'template1'
141         ;
142         UPDATE pg_database
143         SET encoding = pg_char_to_encoding('${encoding}'), datistemplate = TRUE
144         WHERE datname = 'template1'",
145       unless  => "SELECT datname FROM pg_database WHERE
146         datname = 'template1' AND encoding = pg_char_to_encoding('${encoding}')",
147     }
148   }
149 }