2 # Cookbook:: postgresql
 
   5 # Copyright:: 2012, OpenStreetMap Foundation
 
   7 # Licensed under the Apache License, Version 2.0 (the "License");
 
   8 # you may not use this file except in compliance with the License.
 
   9 # You may obtain a copy of the License at
 
  11 #     https://www.apache.org/licenses/LICENSE-2.0
 
  13 # Unless required by applicable law or agreed to in writing, software
 
  14 # distributed under the License is distributed on an "AS IS" BASIS,
 
  15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  16 # See the License for the specific language governing permissions and
 
  17 # limitations under the License.
 
  21 include_recipe "munin"
 
  24 package "postgresql-common"
 
  26 node[:postgresql][:versions].each do |version|
 
  27   package "postgresql-#{version}"
 
  28   package "postgresql-client-#{version}"
 
  29   package "postgresql-contrib-#{version}"
 
  30   package "postgresql-server-dev-#{version}"
 
  32   defaults = node[:postgresql][:settings][:defaults] || {}
 
  33   settings = node[:postgresql][:settings][version] || {}
 
  35   template "/etc/postgresql/#{version}/main/postgresql.conf" do
 
  36     source "postgresql.conf.erb"
 
  40     variables :version => version, :defaults => defaults, :settings => settings
 
  41     notifies :reload, "service[postgresql]"
 
  44   template "/etc/postgresql/#{version}/main/pg_hba.conf" do
 
  45     source "pg_hba.conf.erb"
 
  49     variables :early_rules => settings[:early_authentication_rules] || defaults[:early_authentication_rules],
 
  50               :late_rules => settings[:late_authentication_rules] || defaults[:late_authentication_rules]
 
  51     notifies :reload, "service[postgresql]"
 
  54   template "/etc/postgresql/#{version}/main/pg_ident.conf" do
 
  55     source "pg_ident.conf.erb"
 
  59     variables :maps => settings[:user_name_maps] || defaults[:user_name_maps]
 
  60     notifies :reload, "service[postgresql]"
 
  63   link "/var/lib/postgresql/#{version}/main/server.crt" do
 
  64     to "/etc/ssl/certs/ssl-cert-snakeoil.pem"
 
  67   link "/var/lib/postgresql/#{version}/main/server.key" do
 
  68     to "/etc/ssl/private/ssl-cert-snakeoil.key"
 
  71   standby_mode = settings[:standby_mode] || defaults[:standby_mode]
 
  72   primary_conninfo = settings[:primary_conninfo] || defaults[:primary_conninfo]
 
  73   restore_command = settings[:restore_command] || defaults[:restore_command]
 
  75   if restore_command || standby_mode == "on"
 
  76     passwords = if primary_conninfo
 
  77                   data_bag_item(primary_conninfo[:passwords][:bag],
 
  78                                 primary_conninfo[:passwords][:item])
 
  81     template "/var/lib/postgresql/#{version}/main/recovery.conf" do
 
  82       source "recovery.conf.erb"
 
  86       variables :standby_mode => standby_mode,
 
  87                 :primary_conninfo => primary_conninfo,
 
  88                 :restore_command => restore_command,
 
  89                 :passwords => passwords
 
  90       notifies :reload, "service[postgresql]"
 
  93     template "/var/lib/postgresql/#{version}/main/recovery.conf" do
 
  95       notifies :reload, "service[postgresql]"
 
 100 service "postgresql" do
 
 101   action [:enable, :start]
 
 102   supports :status => true, :restart => true, :reload => true
 
 105 ohai_plugin "postgresql" do
 
 106   template "ohai.rb.erb"
 
 110 package "libdbd-pg-perl"
 
 112 clusters = node[:postgresql][:clusters] || []
 
 114 clusters.each do |name, details|
 
 115   suffix = name.tr("/", ":")
 
 117   munin_plugin "postgres_bgwriter_#{suffix}" do
 
 118     target "postgres_bgwriter"
 
 120     conf_variables :port => details[:port]
 
 123   munin_plugin "postgres_checkpoints_#{suffix}" do
 
 124     target "postgres_checkpoints"
 
 126     conf_variables :port => details[:port]
 
 129   munin_plugin "postgres_connections_db_#{suffix}" do
 
 130     target "postgres_connections_db"
 
 132     conf_variables :port => details[:port]
 
 135   munin_plugin "postgres_users_#{suffix}" do
 
 136     target "postgres_users"
 
 138     conf_variables :port => details[:port]
 
 141   munin_plugin "postgres_xlog_#{suffix}" do
 
 142     target "postgres_xlog"
 
 144     conf_variables :port => details[:port]
 
 147   next unless File.exist?("/var/lib/postgresql/#{details[:version]}/main/recovery.conf")
 
 149   munin_plugin "postgres_replication_#{suffix}" do
 
 150     target "postgres_replication"
 
 152     conf_variables :port => details[:port]