Skip to content

Commit

Permalink
Merge pull request #26 from opscode-cookbooks/COOK-1871
Browse files Browse the repository at this point in the history
COOK-1871 - Additional commits, cleanup.
  • Loading branch information
Joshua Timberman committed Nov 13, 2012
2 parents 8407978 + b802a57 commit 712df25
Show file tree
Hide file tree
Showing 10 changed files with 189 additions and 1,143 deletions.
91 changes: 78 additions & 13 deletions README.md
Expand Up @@ -54,7 +54,66 @@ The following attributes are generated in
* `node['postgresql']['password']['postgres']` - randomly generated
password by the `openssl` cookbook's library.
* `node['postgresql']['ssl']` - whether to enable SSL (off for version
8.3, true for 8.4).
8.3, true for 8.4+).

Configuration
-------------

The `postgresql.conf` and `pg_hba.conf` files are dynamically
generated from attributes. Each key in `node['postgresql']['config']`
is a postgresql configuration directive, and will be rendered in the
config file. For example, the attribute:

node['postgresql']['config']['listen_address'] = 'localhost'

Will result in the following line in the `postgresql.conf` file:

listen_address = 'localhost'

The attributes file contains default values for Debian and RHEL
platform families (per the `node['platform_family']`). These defaults
have disparity between the platforms because they were originally
extracted from the postgresql.conf files in the previous version of
this cookbook, which differed in their default config. The resulting
configuration files will be the same as before, but the content will
be dynamically rendered from the attributes. The helpful commentary
will no longer be present. You should consult the PostgreSQL
documentation for specific configuration details.

For values that are "on" or "off", they should be specified as literal
`true` or `false`. String values will be used with single quotes. Any
configuration option set to the literal `nil` will be skipped
entirely. All other values (e.g., numeric literals) will be used as
is. So for example:

node.default['postgresql']['config']['logging_collector'] = true
node.default['postgresql']['config']['datestyle'] = 'iso, mdy'
node.default['postgresql']['config']['ident_file'] = nil
node.default['postgresql']['config']['port] = 5432

Will result in the following config lines:

logging_collector = 'on'
datestyle = 'iso,mdy'
port = 5432

(no line printed for `ident_file` as it is `nil`)

The `pg_hba.conf` file is dynamically generated from the
`node['postgresql']['pg_hba']` attribute. This attribute must be an
array of hashes, each hash containing the authorization data. As it is
an array, you can append to it in your own recipes. The hash keys in
the array must be symbols. Each hash will be written as a line in
`pg_hba.conf`. For example, this entry from
`node['postgresql']['pg_hba']`:

{:type => 'local', :db => 'all', :user => 'postgres', :addr => nil, :method => 'ident'}

Will result in the following line in `pg_hba.conf`:

local all postgres ident

Use `nil` if the CIDR-ADDRESS should be empty (as above).

Recipes
=======
Expand All @@ -67,10 +126,8 @@ Includes the client recipe.
client
------

Installs postgresql client packages and development headers during the
compile phase. Also installs the `pg` Ruby gem during the compile
phase so it can be made available for the `database` cookbook's
resources, providers and libraries.
Installs the packages defined in the
`node['postgresql']['client']['packages']` attribute.

ruby
----
Expand All @@ -79,10 +136,14 @@ ruby
the
["Omnibus" full stack installer](http://opscode.com/chef/install) on
some platforms due to an incompatibility with OpenSSL. See
[COOK-1406](http://tickets.opscode.com/browse/COOK-1406)
[COOK-1406](http://tickets.opscode.com/browse/COOK-1406). You can
build from source into the Chef omnibus installation to work around
this issue.

Install the `pg` gem under Chef's Ruby environment so it can be used
in other recipes.
in other recipes. The build-essential packages and postgresql client
packages will be installed during the compile phase, so that the
native extensions of `pg` can be compiled.

server
------
Expand All @@ -93,21 +154,24 @@ manages the configuration for the server:

* generates a strong default password (via `openssl`) for `postgres`
* sets the password for postgres
* manages the `postgresql.conf` file.
* manages the `pg_hba.conf` file.

server\_debian
--------------

Installs the postgresql server packages, manages the postgresql
service and the postgresql.conf file.
Installs the postgresql server packages and sets up the service. You
should include the `postgresql::server` recipe, which will include
this on Debian platforms.

server\_redhat
--------------

Manages the postgres user and group (with UID/GID 26, per RHEL package
conventions), installs the postgresql server packages, initializes the
database and manages the postgresql service, and manages the
postgresql.conf file.
database, and manages the postgresql service. You should include the
`postgresql::server` recipe, which will include this on RHEL/Fedora
platforms.

Resources/Providers
===================
Expand All @@ -132,8 +196,9 @@ your node's `json_attribs` file or in a role.
License and Author
==================

Author:: Joshua Timberman (<joshua@opscode.com>)
Author:: Lamont Granquist (<lamont@opscode.com>)
- Author:: Joshua Timberman (<joshua@opscode.com>)
- Author:: Lamont Granquist (<lamont@opscode.com>)
- Author:: Chris Roberts (<chrisroberts.code@gmail.com>)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
83 changes: 66 additions & 17 deletions attributes/default.rb
Expand Up @@ -17,60 +17,60 @@
# limitations under the License.
#

case platform
case node['platform']
when "debian"

case
when platform_version.to_f <= 5.0
when node['platform_version'].to_f <= 5.0
default['postgresql']['version'] = "8.3"
when platform_version.to_f == 6.0
when node['platform_version'].to_f == 6.0
default['postgresql']['version'] = "8.4"
else
default['postgresql']['version'] = "9.1"
end

set['postgresql']['dir'] = "/etc/postgresql/#{node['postgresql']['version']}/main"
default['postgresql']['dir'] = "/etc/postgresql/#{node['postgresql']['version']}/main"
default['postgresql']['client']['packages'] = %w{postgresql-client libpq-dev}
default['postgresql']['server']['packages'] = %w{postgresql}

when "ubuntu"

case
when platform_version.to_f <= 9.04
when node['platform_version'].to_f <= 9.04
default['postgresql']['version'] = "8.3"
when platform_version.to_f <= 11.04
when node['platform_version'].to_f <= 11.04
default['postgresql']['version'] = "8.4"
else
default['postgresql']['version'] = "9.1"
end

set['postgresql']['dir'] = "/etc/postgresql/#{node['postgresql']['version']}/main"
default['postgresql']['dir'] = "/etc/postgresql/#{node['postgresql']['version']}/main"
default['postgresql']['client']['packages'] = %w{postgresql-client libpq-dev}
default['postgresql']['server']['packages'] = %w{postgresql}

when "fedora"

if platform_version.to_f <= 12
if node['platform_version'].to_f <= 12
default['postgresql']['version'] = "8.3"
else
default['postgresql']['version'] = "8.4"
end

set['postgresql']['dir'] = "/var/lib/pgsql/data"
default['postgresql']['dir'] = "/var/lib/pgsql/data"
default['postgresql']['client']['packages'] = %w{postgresql-devel}
default['postgresql']['server']['packages'] = %w{postgresql-server}

when "amazon"

default['postgresql']['version'] = "8.4"
set['postgresql']['dir'] = "/var/lib/pgsql/data"
default['postgresql']['dir'] = "/var/lib/pgsql/data"
default['postgresql']['client']['packages'] = %w{postgresql-devel}
default['postgresql']['server']['packages'] = %w{postgresql-server}

when "redhat","centos","scientific"
when "redhat", "centos", "scientific", "oracle"

default['postgresql']['version'] = "8.4"
set['postgresql']['dir'] = "/var/lib/pgsql/data"
default['postgresql']['dir'] = "/var/lib/pgsql/data"

if node['platform_version'].to_f >= 6.0
default['postgresql']['client']['packages'] = %w{postgresql-devel}
Expand All @@ -82,22 +82,71 @@

when "suse"

case
when platform_version.to_f <= 11.1
if node['platform_version'].to_f <= 11.1
default['postgresql']['version'] = "8.3"
else
default['postgresql']['version'] = "9.0"
end

set['postgresql']['dir'] = "/var/lib/pgsql/data"
default['postgresql']['dir'] = "/var/lib/pgsql/data"
default['postgresql']['client']['packages'] = %w{postgresql-client libpq-dev}
default['postgresql']['server']['packages'] = %w{postgresql-server}

else
default['postgresql']['version'] = "8.4"
set['postgresql']['dir'] = "/etc/postgresql/#{node['postgresql']['version']}/main"
default['postgresql']['dir'] = "/etc/postgresql/#{node['postgresql']['version']}/main"
default['postgresql']['client']['packages'] = ["postgresql"]
default['postgresql']['server']['packages'] = ["postgresql"]
end

default['postgresql']['listen_addresses'] = "localhost"
# These defaults have disparity between which postgresql configuration
# settings are used because they were extracted from the original
# configuration files that are now removed in favor of dynamic
# generation.
#
# While the configuration ends up being the same as the default
# in previous versions of the cookbook, the content of the rendered
# template will change, and this will result in service notification
# if you upgrade the cookbook on existing systems.
#
# The ssl config attribute is generated in the recipe to avoid awkward
# merge/precedence order during the Chef run.
case node['platform_family']
when 'debian'
default['postgresql']['config']['data_directory'] = "/var/lib/postgresql/#{node['postgresql']['version']}/main"
default['postgresql']['config']['hba_file'] = "/etc/postgresql/#{node['postgresql']['version']}/main/pg_hba.conf"
default['postgresql']['config']['ident_file'] = "/etc/postgresql/#{node['postgresql']['version']}/main/pg_ident.conf"
default['postgresql']['config']['external_pid_file'] = "/var/run/postgresql/#{node['postgresql']['version']}-main.pid"
default['postgresql']['config']['listen_addresses'] = 'localhost'
default['postgresql']['config']['port'] = 5432
default['postgresql']['config']['max_connections'] = 100
default['postgresql']['config']['unix_socket_directory'] = '/var/run/postgresql'
default['postgresql']['config']['shared_buffers'] = '24MB'
default['postgresql']['config']['max_fsm_pages'] = 153600 if node['postgresql']['version'].to_f < 8.4
default['postgresql']['config']['log_line_prefix'] = '%t '
default['postgresql']['config']['datestyle'] = 'iso, mdy'
default['postgresql']['config']['default_text_search_config'] = 'pg_catalog.english'
when 'rhel', 'fedora'
default['postgresql']['config']['listen_addresses'] = 'localhost'
default['postgresql']['config']['max_connections'] = 100
default['postgresql']['config']['shared_buffers'] = '32MB'
default['postgresql']['config']['logging_collector'] = true
default['postgresql']['config']['log_directory'] = 'pg_log'
default['postgresql']['config']['log_filename'] = 'postgresql-%a.log'
default['postgresql']['config']['log_truncate_on_rotation'] = true
default['postgresql']['config']['log_rotation_age'] = '1d'
default['postgresql']['config']['log_rotation_size'] = 0
default['postgresql']['config']['datestyle'] = 'iso, mdy'
default['postgresql']['config']['lc_messages'] = 'en_US.UTF-8'
default['postgresql']['config']['lc_monetary'] = 'en_US.UTF-8'
default['postgresql']['config']['lc_numeric'] = 'en_US.UTF-8'
default['postgresql']['config']['lc_time'] = 'en_US.UTF-8'
default['postgresql']['config']['default_text_search_config'] = 'pg_catalog.english'
end

default['postgresql']['pg_hba'] = [
{:type => 'local', :db => 'all', :user => 'postgres', :addr => nil, :method => 'ident'},
{:type => 'local', :db => 'all', :user => 'all', :addr => nil, :method => 'ident'},
{:type => 'host', :db => 'all', :user => 'all', :addr => '127.0.0.1/32', :method => 'md5'},
{:type => 'host', :db => 'all', :user => 'all', :addr => '::1/128', :method => 'md5'}
]
6 changes: 3 additions & 3 deletions recipes/client.rb
Expand Up @@ -20,7 +20,7 @@
#

node['postgresql']['client']['packages'].each do |pg_pack|
package pg_pack do
action :install
end

package pg_pack

end
14 changes: 11 additions & 3 deletions recipes/server.rb
Expand Up @@ -28,9 +28,9 @@
node.save unless Chef::Config[:solo]

if node['postgresql']['version'].to_f <= 8.3
node.default['postgresql']['ssl'] = "off"
node.default['postgresql']['config']['ssl'] = false
else
node.default['postgresql']['ssl'] = "true"
node.default['postgresql']['config']['ssl'] = true
end

# Include the right "family" recipe for installing the server
Expand All @@ -42,12 +42,20 @@
include_recipe "postgresql::server_debian"
end

template "#{node['postgresql']['dir']}/postgresql.conf" do
source "postgresql.conf.erb"
owner "postgres"
group "postgres"
mode 0600
notifies :restart, 'service[postgresql]', :immediately
end

template "#{node['postgresql']['dir']}/pg_hba.conf" do
source "pg_hba.conf.erb"
owner "postgres"
group "postgres"
mode 00600
notifies :reload, resources(:service => "postgresql"), :immediately
notifies :reload, 'service[postgresql]', :immediately
end

# Default PostgreSQL install has 'ident' checking on unix user 'postgres'
Expand Down
19 changes: 2 additions & 17 deletions recipes/server_debian.rb
Expand Up @@ -21,17 +21,10 @@

include_recipe "postgresql::client"

node['postgresql']['server']['packages'].each do |pg_pack|

if node['postgresql']['version'].to_f <= 8.3
node.default['postgresql']['ssl'] = "off"
else
node.default['postgresql']['ssl'] = "true"
end
package pg_pack

node['postgresql']['server']['packages'].each do |pg_pack|
package pg_pack do
action :install
end
end

service "postgresql" do
Expand All @@ -54,11 +47,3 @@
supports :restart => true, :status => true, :reload => true
action [:enable, :start]
end

template "#{node['postgresql']['dir']}/postgresql.conf" do
source "debian.postgresql.conf.erb"
owner "postgres"
group "postgres"
mode 0600
notifies :restart, resources(:service => "postgresql"), :immediately
end
14 changes: 3 additions & 11 deletions recipes/server_redhat.rb
Expand Up @@ -39,9 +39,9 @@
end

node['postgresql']['server']['packages'].each do |pg_pack|
package pg_pack do
action :install
end

package pg_pack

end

case node['platform']
Expand All @@ -67,11 +67,3 @@
supports :restart => true, :status => true, :reload => true
action [:enable, :start]
end

template "#{node['postgresql']['dir']}/postgresql.conf" do
source "redhat.postgresql.conf.erb"
owner "postgres"
group "postgres"
mode 0600
notifies :restart, resources(:service => "postgresql"), :immediately
end

0 comments on commit 712df25

Please sign in to comment.