Skip to content

Commit 6034e12

Browse files
committed
Working version of postgresql::server
This is a first working version of postgresql::server. It includes a very simple test manifest, which has been tried out on CentOS6 and Ubuntu 10.04; initial tests were successful both from a clean state and for subsequent runs. Includes a new fact called 'postgres_default_version', which detects what the default version of postgres is for a given OS. This is needed because some of the commands and directory names include this version string. Current implementation *only* supports managing the system default version; in the future it would be nice to allow the user to explicitly specify a postgres version, but that isn't yet supported. The "postgresql::server" class includes a call to postgres's initdb command on redhat systems, because they don't do this automatically when the package is installed.
1 parent 21467c8 commit 6034e12

File tree

7 files changed

+156
-9
lines changed

7 files changed

+156
-9
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
def get_debian_postgres_version
2+
depends = Facter::Util::Resolution.exec('apt-cache show postgresql |grep "^Depends" |head -n 1')
3+
if match = /^Depends: postgresql-(.*)$/.match(depends)
4+
match[1]
5+
else
6+
nil
7+
end
8+
end
9+
10+
def get_redhat_postgres_version
11+
version = Facter::Util::Resolution.exec('yum info postgresql-server |grep "^Version"')
12+
if match = /^Version\s*:\s*(\d+\.\d+).*$/.match(version)
13+
match[1]
14+
else
15+
nil
16+
end
17+
end
18+
19+
Facter.add("postgres_default_version") do
20+
setcode do
21+
case Facter.value('osfamily')
22+
when 'RedHat'
23+
get_redhat_postgres_version()
24+
when 'Debian'
25+
get_debian_postgres_version()
26+
else
27+
nil
28+
end
29+
end
30+
end

manifests/init.pp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Class: postgresql
2+
#
3+
# This class installs postgresql client software.
4+
#
5+
# Parameters:
6+
# [*client_package_name*] - The name of the postgresql client package.
7+
#
8+
# Actions:
9+
#
10+
# Requires:
11+
#
12+
# Sample Usage:
13+
#
14+
class postgresql (
15+
$package_name = $postgresql::params::client_package_name,
16+
$package_ensure = 'present'
17+
) inherits postgresql::params {
18+
19+
package { 'postgresql_client':
20+
name => $package_name,
21+
ensure => $package_ensure,
22+
}
23+
24+
}

manifests/initdb.pp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,18 @@
1616
# See the License for the specific language governing permissions and
1717
# limitations under the License.
1818

19-
define postgresql::initdb(
20-
$directory = $title,
21-
$version = '9.1',
19+
class postgresql::initdb(
20+
$datadir = $postgresql::params::datadir,
21+
$initdb_path = $postgresql::params::initdb_path,
2222
$user = 'postgres',
2323
$group = 'postgres',
2424
$encoding = 'UTF8',
25-
$options='') {
25+
$options=''
26+
) inherits postgresql::params {
2627

27-
# TODO: This should be found based on the operating system; currently hardcoded to Ubuntu's path choice
28-
exec {"/usr/lib/postgresql/${version}/bin/initdb --encoding '$encoding' --pgdata '$directory'":
29-
command => "/usr/lib/postgresql/${version}/bin/initdb --encoding '$encoding' --pgdata '$directory'",
30-
creates => "$directory",
28+
exec {"${initdb_path} --encoding '$encoding' --pgdata '$datadir'":
29+
creates => "${datadir}/PG_VERSION",
3130
user => "$user",
3231
group => "$group",
33-
require => Package["postgresql-$version"],
3432
}
3533
}

manifests/params.pp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Class: postgresql::params
2+
#
3+
# The postgresql configuration settings.
4+
#
5+
# Parameters:
6+
#
7+
# Actions:
8+
#
9+
# Requires:
10+
#
11+
# Sample Usage:
12+
#
13+
class postgresql::params {
14+
15+
case $::operatingsystem {
16+
"Ubuntu": {
17+
$service_provider = upstart
18+
}
19+
default: {
20+
$service_provider = undef
21+
}
22+
}
23+
24+
case $::osfamily {
25+
'RedHat': {
26+
$service_name = 'postgresql'
27+
$client_package_name = 'postgresql'
28+
$server_package_name = 'postgresql-server'
29+
$needs_initdb = true
30+
$initdb_path = '/usr/bin/initdb'
31+
$datadir = '/var/lib/pgsql/data/'
32+
}
33+
34+
'Debian': {
35+
$service_name = "postgresql-${::postgres_default_version}"
36+
$client_package_name = 'postgresql-client'
37+
$server_package_name = 'postgresql'
38+
$needs_initdb = false
39+
$initdb_path = "/usr/lib/postgresql/${::postgres_default_version}/bin/initdb"
40+
$datadir = "/var/lib/postgresql/${::postgres_default_version}/main"
41+
}
42+
43+
44+
default: {
45+
fail("Unsupported osfamily: ${::osfamily} operatingsystem: ${::operatingsystem}, module ${module_name} currently only supports osfamily RedHat and Debian")
46+
}
47+
}
48+
49+
}

manifests/server.pp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Class: postgresql::server
2+
#
3+
# manages the installation of the postgresql server. manages the package and service
4+
#
5+
# Parameters:
6+
# [*package_name*] - name of package
7+
# [*service_name*] - name of service
8+
#
9+
# Actions:
10+
#
11+
# Requires:
12+
#
13+
# Sample Usage:
14+
#
15+
class postgresql::server (
16+
$package_name = $postgresql::params::server_package_name,
17+
$package_ensure = 'present',
18+
$service_name = $postgresql::params::service_name,
19+
$service_provider = $postgresql::params::service_provider,
20+
$config_hash = {}
21+
) inherits postgresql::params {
22+
23+
package { 'postgresql-server':
24+
name => $package_name,
25+
ensure => $package_ensure,
26+
}
27+
28+
if ($needs_initdb) {
29+
include postgresql::initdb
30+
31+
Class['postgresql::initdb'] -> Service['postgresqld']
32+
}
33+
34+
service { 'postgresqld':
35+
name => $service_name,
36+
ensure => running,
37+
enable => true,
38+
require => Package['postgresql-server'],
39+
provider => $service_provider,
40+
}
41+
42+
43+
}

tests/init.pp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
include postgresql

tests/server.pp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
class { 'postgresql::server':
2+
}

0 commit comments

Comments
 (0)