Merge platform.pp back into params.pp

Nan showed me a trick that will let us keep all of that param stuff
inside of params.pp, make it a parameterized class, and still support
the ability for users to specify a custom (non-system-default) pg
version.  This commit takes the first step towards that pattern by
consolidating platform.pp and params.pp.  (Everything old is new again!)
This commit is contained in:
Chris Price 2012-12-03 20:42:20 -08:00
parent fa24f24397
commit 18167c7a3e
12 changed files with 99 additions and 211 deletions

View file

@ -39,27 +39,11 @@ class postgresql::config(
$listen_addresses = $postgresql::params::listen_addresses, $listen_addresses = $postgresql::params::listen_addresses,
$ipv4acls = $postgresql::params::ipv4acls, $ipv4acls = $postgresql::params::ipv4acls,
$ipv6acls = $postgresql::params::ipv6acls, $ipv6acls = $postgresql::params::ipv6acls,
$pg_hba_conf_path = '', $pg_hba_conf_path = $postgresql::params::pg_hba_conf_path,
$postgresql_conf_path = '', $postgresql_conf_path = $postgresql::params::postgresql_conf_path,
$manage_redhat_firewall = $postgresql::params::manage_redhat_firewall $manage_redhat_firewall = $postgresql::params::manage_redhat_firewall
) inherits postgresql::params { ) inherits postgresql::params {
include postgresql::platform
if ! $pg_hba_conf_path {
$pg_hba_conf_path_real = $postgresql::platform::pg_hba_conf_path
}
else {
$pg_hba_conf_path_real = $pg_hba_conf_path
}
if ! $postgresql_conf_path {
$postgresql_conf_path_real = $postgresql::platform::postgresql_conf_path
}
else {
$postgresql_conf_path_real = $postgresql_conf_path
}
# Basically, all this class needs to handle is passing parameters on # Basically, all this class needs to handle is passing parameters on
# to the "beforeservice" and "afterservice" classes, and ensure # to the "beforeservice" and "afterservice" classes, and ensure
# the proper ordering. # the proper ordering.
@ -70,8 +54,8 @@ class postgresql::config(
listen_addresses => $listen_addresses, listen_addresses => $listen_addresses,
ipv4acls => $ipv4acls, ipv4acls => $ipv4acls,
ipv6acls => $ipv6acls, ipv6acls => $ipv6acls,
pg_hba_conf_path => $pg_hba_conf_path_real, pg_hba_conf_path => $pg_hba_conf_path,
postgresql_conf_path => $postgresql_conf_path_real, postgresql_conf_path => $postgresql_conf_path,
manage_redhat_firewall => $manage_redhat_firewall, manage_redhat_firewall => $manage_redhat_firewall,
} }

View file

@ -23,13 +23,13 @@ define postgresql::database(
$dbname = $title, $dbname = $title,
$charset = 'UTF8') $charset = 'UTF8')
{ {
include postgresql::platform include postgresql::params
if ($::postgres_default_version != '8.1') { if ($::postgres_default_version != '8.1') {
$locale_option = '--locale=C' $locale_option = '--locale=C'
} }
$createdb_command = "${postgresql::platform::createdb_path} --template=template0 --encoding '${charset}' ${locale_option} '${dbname}'" $createdb_command = "${postgresql::params::createdb_path} --template=template0 --encoding '${charset}' ${locale_option} '${dbname}'"
postgresql_psql { "Check for existence of db '$dbname'": postgresql_psql { "Check for existence of db '$dbname'":
command => "SELECT 1", command => "SELECT 1",

View file

@ -13,20 +13,12 @@
# Sample Usage: # Sample Usage:
# #
class postgresql::devel( class postgresql::devel(
$package_name = '', $package_name = $postgresql::params::devel_package_name,
$package_ensure = 'present' $package_ensure = 'present'
) { ) inherits postgresql::params {
if ! $package_name {
include postgresql::platform
$package_name_real = $postgresql::platform::devel_package_name
}
else {
$package_name_real = $package_name
}
package { 'postgresql_devel': package { 'postgresql_devel':
ensure => $package_ensure, ensure => $package_ensure,
name => $package_name_real, name => $package_name,
} }
} }

View file

@ -17,21 +17,13 @@
# #
class postgresql ( class postgresql (
$version = $::postgres_default_version, $version = $::postgres_default_version,
$package_name = '', $package_name = $postgresql::params::client_package_name,
$package_ensure = 'present' $package_ensure = 'present'
) inherits postgresql::params { ) inherits postgresql::params {
if ! $package_name {
include postgresql::platform
$package_name_real = $postgresql::platform::client_package_name
}
else {
$package_name_real = $package_name
}
package { 'postgresql_client': package { 'postgresql_client':
ensure => $package_ensure, ensure => $package_ensure,
name => $package_name_real, name => $package_name,
} }
} }

View file

@ -17,32 +17,17 @@
# limitations under the License. # limitations under the License.
class postgresql::initdb( class postgresql::initdb(
$datadir = '', $datadir = $postgresql::params::datadir,
$encoding = 'UTF8', $encoding = 'UTF8',
$group = 'postgres', $group = 'postgres',
$initdb_path = '', $initdb_path = $postgresql::params::initdb_path,
$options = '',
$user = 'postgres' $user = 'postgres'
) inherits postgresql::params { ) inherits postgresql::params {
include postgresql::platform $initdb_command = "${initdb_path} --encoding '${encoding}' --pgdata '${datadir}'"
if ! $datadir {
$datadir_real = $postgresql::platform::datadir
} else {
$datadir_real = $datadir
}
if ! $initdb_path {
$initdb_path_real = $postgresql::platform::initdb_path
} else {
$initdb_path_real = $initdb_path
}
$initdb_command = "${initdb_path_real} --encoding '${encoding}' --pgdata '${datadir_real}'"
exec { $initdb_command: exec { $initdb_command:
creates => "${datadir_real}/PG_VERSION", creates => "${datadir}/PG_VERSION",
user => $user, user => $user,
group => $group group => $group
} }

View file

@ -31,22 +31,87 @@ class postgresql::params {
fail "No value for postgres_default_version facter fact; it's possible that you don't have pluginsync enabled." fail "No value for postgres_default_version facter fact; it's possible that you don't have pluginsync enabled."
} }
if defined(Class[Postgresql::Package_source_info]) {
$version = $postgresql::package_source_info::version
} else {
$version = $::postgres_default_version
}
case $::operatingsystem {
default: {
$service_provider = undef
}
}
case $::osfamily { case $::osfamily {
'RedHat': { 'RedHat': {
$needs_initdb = true $needs_initdb = true
$firewall_supported = true $firewall_supported = true
$persist_firewall_command = '/sbin/iptables-save > /etc/sysconfig/iptables' $persist_firewall_command = '/sbin/iptables-save > /etc/sysconfig/iptables'
if $version == $::postgres_default_version {
$client_package_name = 'postgresql'
$server_package_name = 'postgresql-server'
$devel_package_name = 'postgresql-devel'
$service_name = 'postgresql'
$bindir = '/usr/bin'
$datadir = '/var/lib/pgsql/data'
$confdir = $datadir
} else {
$version_parts = split($version, '[.]')
$package_version = "${version_parts[0]}${version_parts[1]}"
$client_package_name = "postgresql${package_version}"
$server_package_name = "postgresql${package_version}-server"
$devel_package_name = "postgresql${package_version}-devel"
$service_name = "postgresql-${version}"
$bindir = "/usr/pgsql-${version}/bin"
$datadir = "/var/lib/pgsql/${version}/data"
$confdir = $datadir
} }
$service_status = undef
}
'Debian': { 'Debian': {
$needs_initdb = false $needs_initdb = false
$firewall_supported = false $firewall_supported = false
# TODO: not exactly sure yet what the right thing to do for Debian/Ubuntu is. # TODO: not exactly sure yet what the right thing to do for Debian/Ubuntu is.
#$persist_firewall_command = '/sbin/iptables-save > /etc/iptables/rules.v4' #$persist_firewall_command = '/sbin/iptables-save > /etc/iptables/rules.v4'
case $::operatingsystem {
'Debian': {
$service_name = 'postgresql'
}
'Ubuntu': {
case $::lsbmajdistrelease {
# thanks, ubuntu
'10': { $service_name = "postgresql-${::postgres_default_version}" }
default: { $service_name = 'postgresql' }
}
}
}
$client_package_name = 'postgresql-client'
$server_package_name = 'postgresql'
$devel_package_name = 'libpq-dev'
$bindir = "/usr/lib/postgresql/${::postgres_default_version}/bin"
$datadir = "/var/lib/postgresql/${::postgres_default_version}/main"
$confdir = "/etc/postgresql/${::postgres_default_version}/main"
$service_status = "/etc/init.d/${service_name} status | /bin/egrep -q 'Running clusters: .+'"
} }
default: { default: {
fail("Unsupported osfamily: ${::osfamily} operatingsystem: ${::operatingsystem}, module ${module_name} currently only supports osfamily RedHat and Debian") fail("Unsupported osfamily: ${::osfamily} operatingsystem: ${::operatingsystem}, module ${module_name} currently only supports osfamily RedHat and Debian")
} }
} }
$initdb_path = "${bindir}/initdb"
$createdb_path = "${bindir}/createdb"
$psql_path = "${bindir}/psql"
$pg_hba_conf_path = "${confdir}/pg_hba.conf"
$postgresql_conf_path = "${confdir}/postgresql.conf"
} }

View file

@ -1,95 +0,0 @@
# Class: postgresql::platform
#
# An abstraction for the postgresql platform. Based on the desired version of
# postgres, whether it is part of the OS distro, OS distro differences / defaults,
# etc., figures out the package names, service names, paths, etc.
#
# Parameters:
#
# Actions:
#
# Requires:
#
# Sample Usage:
#
class postgresql::platform {
if defined(Class[Postgresql::Package_source_info]) {
$version = $postgresql::package_source_info::version
} else {
$version = $::postgres_default_version
}
case $::operatingsystem {
default: {
$service_provider = undef
}
}
case $::osfamily {
'RedHat': {
if $version == $::postgres_default_version {
$client_package_name = 'postgresql'
$server_package_name = 'postgresql-server'
$devel_package_name = 'postgresql-devel'
$service_name = 'postgresql'
$bindir = '/usr/bin'
$datadir = '/var/lib/pgsql/data'
$confdir = $datadir
} else {
$version_parts = split($version, '[.]')
$package_version = "${version_parts[0]}${version_parts[1]}"
$client_package_name = "postgresql${package_version}"
$server_package_name = "postgresql${package_version}-server"
$devel_package_name = "postgresql${package_version}-devel"
$service_name = "postgresql-${version}"
$bindir = "/usr/pgsql-${version}/bin"
$datadir = "/var/lib/pgsql/${version}/data"
$confdir = $datadir
}
$service_status = undef
}
'Debian': {
case $::operatingsystem {
'Debian': {
$service_name = 'postgresql'
}
'Ubuntu': {
case $::lsbmajdistrelease {
# thanks, ubuntu
'10': { $service_name = "postgresql-${::postgres_default_version}" }
default: { $service_name = 'postgresql' }
}
}
}
$client_package_name = 'postgresql-client'
$server_package_name = 'postgresql'
$devel_package_name = 'libpq-dev'
$bindir = "/usr/lib/postgresql/${::postgres_default_version}/bin"
$datadir = "/var/lib/postgresql/${::postgres_default_version}/main"
$confdir = "/etc/postgresql/${::postgres_default_version}/main"
$service_status = "/etc/init.d/${service_name} status | /bin/egrep -q 'Running clusters: .+'"
# TODO: not exactly sure yet what the right thing to do for Debian/Ubuntu is.
#$persist_firewall_command = '/sbin/iptables-save > /etc/iptables/rules.v4'
}
default: {
fail("Unsupported osfamily: ${::osfamily} operatingsystem: ${::operatingsystem}, module ${module_name} currently only supports osfamily RedHat and Debian")
}
}
$initdb_path = "${bindir}/initdb"
$createdb_path = "${bindir}/createdb"
$psql_path = "${bindir}/psql"
$pg_hba_conf_path = "${confdir}/pg_hba.conf"
$postgresql_conf_path = "${confdir}/postgresql.conf"
}

View file

@ -24,7 +24,7 @@ define postgresql::psql(
$user = 'postgres' $user = 'postgres'
) { ) {
require postgresql::platform include postgresql::params
# TODO: FIXME: shellquote does not work, and this regex works for trivial # TODO: FIXME: shellquote does not work, and this regex works for trivial
# things but not nested escaping. Need a lexer, preferably a ruby SQL parser # things but not nested escaping. Need a lexer, preferably a ruby SQL parser
@ -34,7 +34,7 @@ define postgresql::psql(
$no_password_option = '--no-password' $no_password_option = '--no-password'
} }
$psql = "${postgresql::platform::psql_path} ${no_password_option} --tuples-only --quiet --dbname ${db}" $psql = "${postgresql::params::psql_path} ${no_password_option} --tuples-only --quiet --dbname ${db}"
$quoted_command = regsubst($command, '"', '\\"', 'G') $quoted_command = regsubst($command, '"', '\\"', 'G')
$quoted_unless = regsubst($unless, '"', '\\"', 'G') $quoted_unless = regsubst($unless, '"', '\\"', 'G')

View file

@ -15,43 +15,17 @@
# Sample Usage: # Sample Usage:
# #
class postgresql::server ( class postgresql::server (
$package_name = '', $package_name = $postgresql::params::server_package_name,
$package_ensure = 'present', $package_ensure = 'present',
$service_name = '', $service_name = $postgresql::params::service_name,
$service_provider = '', $service_provider = $postgresql::params::service_provider,
$service_status = '', $service_status = $postgresql::params::service_status,
$config_hash = {} $config_hash = {}
) inherits postgresql::params { ) inherits postgresql::params {
include postgresql::platform
if ! $package_name {
$package_name_real = $postgresql::platform::server_package_name
} else {
$package_name_real = $package_name
}
if ! $service_name {
$service_name_real = $postgresql::platform::service_name
} else {
$service_name_real = $service_name
}
if ! $service_provider {
$service_provider_real = $postgresql::platform::service_provider
} else {
$service_provider_real = $service_provider
}
if ! $service_status {
$service_status_real = $postgresql::platform::service_status
} else {
$service_status_real = $service_status
}
package { 'postgresql-server': package { 'postgresql-server':
ensure => $package_ensure, ensure => $package_ensure,
name => $package_name_real, name => $package_name,
} }
if defined(Yumrepo['yum.postgresql.org']) { if defined(Yumrepo['yum.postgresql.org']) {
@ -66,11 +40,11 @@ class postgresql::server (
service { 'postgresqld': service { 'postgresqld':
ensure => running, ensure => running,
name => $service_name_real, name => $service_name,
enable => true, enable => true,
require => Package['postgresql-server'], require => Package['postgresql-server'],
provider => $service_provider_real, provider => $service_provider,
status => $service_status_real, status => $service_status,
} }
if ($postgresql::params::needs_initdb) { if ($postgresql::params::needs_initdb) {
@ -84,10 +58,10 @@ class postgresql::server (
exec { 'reload_postgresql': exec { 'reload_postgresql':
path => '/usr/bin:/usr/sbin:/bin:/sbin', path => '/usr/bin:/usr/sbin:/bin:/sbin',
command => "service ${service_name_real} reload", command => "service ${service_name} reload",
user => $postgresql::params::user, user => $postgresql::params::user,
group => $postgresql::params::group, group => $postgresql::params::group,
onlyif => $service_status_real, onlyif => $service_status,
refreshonly => true, refreshonly => true,
} }

View file

@ -49,28 +49,19 @@ define postgresql::validate_db_connection(
$database_name, $database_name,
$database_password, $database_password,
$database_username, $database_username,
$client_package_name = '', $client_package_name = $postgresql::params::client_package_name,
$database_port = 5432 $database_port = 5432
) { ) inherits postgresql::params {
include postgresql::platform
# This is a bit messy, but we need to use the correct client package name
# from the params class if the user did not explicitly provide one.
if (! $client_package_name) {
$package_name = $postgresql::platform::client_package_name
} else {
$package_name = $client_package_name
}
# Make sure the postgres client package is installed; we need it for # Make sure the postgres client package is installed; we need it for
# `psql`. # `psql`.
package { 'postgresql-client': package { 'postgresql-client':
ensure => present, ensure => present,
name => $package_name, name => $client_package_name,
} }
# TODO: port to ruby # TODO: port to ruby
$psql = "${postgresql::platform::psql_path} --tuples-only --quiet -h ${database_host} -U ${database_username} -p ${database_port} --dbname ${database_name}" $psql = "${postgresql::params::psql_path} --tuples-only --quiet -h ${database_host} -U ${database_username} -p ${database_port} --dbname ${database_name}"
$exec_name = "validate postgres connection for ${database_host}/${database_name}" $exec_name = "validate postgres connection for ${database_host}/${database_name}"
exec { $exec_name: exec { $exec_name:

View file

@ -23,7 +23,7 @@ shared_context :pg_vm_context do
@env.cli("up", vm.to_s) @env.cli("up", vm.to_s)
if PostgresTestConfig::HardCoreTesting if PostgresTestConfig::HardCoreTesting
sudo_and_log(vm, '[ "$(facter osfamily)" == "Debian" ] && apt-get update') sudo_and_log(vm, 'if [ "$(facter osfamily)" == "Debian" ] ; then apt-get update ; fi')
end end
install_postgres install_postgres