From b781849882ee5b36135f8d3abafa449721e19565 Mon Sep 17 00:00:00 2001 From: Milan Karalic Date: Tue, 21 Oct 2014 12:59:07 +0200 Subject: [PATCH] Adds support for PGDATA changing in config_entry.pp Ensure that data_directory is set in the config. per GitHub user tbd - PR#510 / PR#494 that was filed against wrong module branch" Adds acceptance test for non default PGDATA, based on alternative_port_spec.rb Fixes unit test for data directory --- manifests/server/config.pp | 4 +++ manifests/server/config_entry.pp | 21 ++++++++++++++++ spec/acceptance/alternative_pgdata_spec.rb | 25 +++++++++++++++++++ spec/unit/defines/server/config_entry_spec.rb | 9 +++++++ 4 files changed, 59 insertions(+) create mode 100644 spec/acceptance/alternative_pgdata_spec.rb diff --git a/manifests/server/config.pp b/manifests/server/config.pp index 2845cab..5d1c693 100644 --- a/manifests/server/config.pp +++ b/manifests/server/config.pp @@ -15,6 +15,7 @@ class postgresql::server::config { $version = $postgresql::server::_version $manage_pg_hba_conf = $postgresql::server::manage_pg_hba_conf $manage_pg_ident_conf = $postgresql::server::manage_pg_ident_conf + $datadir = $postgresql::server::datadir if ($manage_pg_hba_conf == true) { # Prepare the main pg_hba file @@ -100,6 +101,9 @@ class postgresql::server::config { postgresql::server::config_entry { 'port': value => $port, } + postgresql::server::config_entry { 'data_directory': + value => $datadir, + } # RedHat-based systems hardcode some PG* variables in the init script, and need to be overriden # in /etc/sysconfig/pgsql/postgresql. Create a blank file so we can manage it with augeas later. diff --git a/manifests/server/config_entry.pp b/manifests/server/config_entry.pp index 06ca0cd..cd73520 100644 --- a/manifests/server/config_entry.pp +++ b/manifests/server/config_entry.pp @@ -86,6 +86,27 @@ define postgresql::server::config_entry ( notify => Class['postgresql::server::service'], before => Class['postgresql::server::reload'], } + } else { + if $name == 'data_directory' { + # We need to force postgresql to stop before updating the data directory + # otherwise init script breaks + exec { "postgresql_${name}": + command => "service ${::postgresql::server::service_name} stop", + onlyif => "service ${::postgresql::server::service_name} status", + unless => "grep 'PGDATA=${value}' /etc/sysconfig/pgsql/postgresql", + path => '/sbin:/bin:/usr/bin:/usr/local/bin', + require => File['/etc/sysconfig/pgsql/postgresql'], + } -> + augeas { 'override PGDATA in /etc/sysconfig/pgsql/postgresql': + lens => 'Shellvars.lns', + incl => '/etc/sysconfig/pgsql/*', + context => '/files/etc/sysconfig/pgsql/postgresql', + changes => "set PGDATA ${value}", + require => File['/etc/sysconfig/pgsql/postgresql'], + notify => Class['postgresql::server::service'], + before => Class['postgresql::server::reload'], + } + } } } } diff --git a/spec/acceptance/alternative_pgdata_spec.rb b/spec/acceptance/alternative_pgdata_spec.rb new file mode 100644 index 0000000..0dd6400 --- /dev/null +++ b/spec/acceptance/alternative_pgdata_spec.rb @@ -0,0 +1,25 @@ +require 'spec_helper_acceptance' + +# These tests ensure that postgres can change itself to an alternative pgdata +# location properly. +describe 'postgres::server', :unless => UNSUPPORTED_PLATFORMS.include?(fact('osfamily')) do + it 'on an alternative pgdata location' do + pp = <<-EOS + class { 'postgresql::server': datadir => '/var/pgsql' } + EOS + + apply_manifest(pp, :catch_failures => true) + apply_manifest(pp, :catch_changes => true) + end + + describe "Alternate Directory" do + File.directory?("/var/pgsql").should be true + end + + it 'can connect with psql' do + psql('--command="\l" postgres', 'postgres') do |r| + expect(r.stdout).to match(/List of databases/) + end + end + +end diff --git a/spec/unit/defines/server/config_entry_spec.rb b/spec/unit/defines/server/config_entry_spec.rb index 0ef6049..4c3d9a9 100644 --- a/spec/unit/defines/server/config_entry_spec.rb +++ b/spec/unit/defines/server/config_entry_spec.rb @@ -88,6 +88,15 @@ describe 'postgresql::server::config_entry', :type => :define do end end + context "data_directory" do + let(:params) {{ :ensure => 'present', :name => 'data_directory_spec', :value => '/var/pgsql' }} + + it 'stops postgresql and changes the data directory' do + is_expected.to contain_exec('postgresql_data_directory') + is_expected.to contain_augeas('override PGDATA in /etc/sysconfig/pgsql/postgresql') + end + end + context "passes values through appropriately" do let(:params) {{ :ensure => 'present', :name => 'check_function_bodies', :value => 'off' }}