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.
20 include_recipe "apt::postgresql"
21 include_recipe "munin"
22 include_recipe "prometheus"
25 package "postgresql-common"
27 node[:postgresql][:versions].each do |version|
28 package "postgresql-#{version}"
29 package "postgresql-client-#{version}"
30 package "postgresql-contrib-#{version}"
31 package "postgresql-server-dev-#{version}"
33 defaults = node[:postgresql][:settings][:defaults] || {}
34 settings = node[:postgresql][:settings][version] || {}
36 standby_mode = settings[:standby_mode] || defaults[:standby_mode]
37 primary_conninfo = settings[:primary_conninfo] || defaults[:primary_conninfo]
38 restore_command = settings[:restore_command] || defaults[:restore_command]
40 passwords = if primary_conninfo
41 data_bag_item(primary_conninfo[:passwords][:bag],
42 primary_conninfo[:passwords][:item])
45 template "/etc/postgresql/#{version}/main/postgresql.conf" do
46 source "postgresql.conf.erb"
50 variables :version => version,
51 :defaults => defaults,
52 :settings => settings,
53 :primary_conninfo => primary_conninfo,
54 :passwords => passwords
55 notifies :reload, "service[postgresql]"
56 only_if { ::Dir.exist?("/etc/postgresql/#{version}/main") }
59 template "/etc/postgresql/#{version}/main/pg_hba.conf" do
60 source "pg_hba.conf.erb"
64 variables :early_rules => settings[:early_authentication_rules] || defaults[:early_authentication_rules],
65 :late_rules => settings[:late_authentication_rules] || defaults[:late_authentication_rules]
66 notifies :reload, "service[postgresql]"
67 only_if { ::Dir.exist?("/etc/postgresql/#{version}/main") }
70 template "/etc/postgresql/#{version}/main/pg_ident.conf" do
71 source "pg_ident.conf.erb"
75 variables :maps => settings[:user_name_maps] || defaults[:user_name_maps]
76 notifies :reload, "service[postgresql]"
77 only_if { ::Dir.exist?("/etc/postgresql/#{version}/main") }
80 link "/var/lib/postgresql/#{version}/main/server.crt" do
81 to "/etc/ssl/certs/ssl-cert-snakeoil.pem"
82 only_if { ::Dir.exist?("/var/lib/postgresql/#{version}/main") }
85 link "/var/lib/postgresql/#{version}/main/server.key" do
86 to "/etc/ssl/private/ssl-cert-snakeoil.key"
87 only_if { ::Dir.exist?("/var/lib/postgresql/#{version}/main") }
90 if version.to_f < 12 && (restore_command || standby_mode == "on")
91 template "/var/lib/postgresql/#{version}/main/recovery.conf" do
92 source "recovery.conf.erb"
96 variables :standby_mode => standby_mode,
97 :primary_conninfo => primary_conninfo,
98 :restore_command => restore_command,
99 :passwords => passwords
100 notifies :reload, "service[postgresql]"
101 only_if { ::Dir.exist?("/var/lib/postgresql/#{version}/main") }
104 template "/var/lib/postgresql/#{version}/main/recovery.conf" do
106 notifies :reload, "service[postgresql]"
107 only_if { ::Dir.exist?("/var/lib/postgresql/#{version}/main") }
111 if version.to_f > 11 && standby_mode == "on"
112 file "/var/lib/postgresql/#{version}/main/standby.signal" do
118 file "/var/lib/postgresql/#{version}/main/standby.signal" do
124 service "postgresql" do
125 action [:enable, :start]
126 supports :status => true, :restart => true, :reload => true
129 ohai_plugin "postgresql" do
130 template "ohai.rb.erb"
134 package "libdbd-pg-perl"
136 clusters = node[:postgresql][:clusters] || []
138 clusters.each do |name, details|
139 suffix = name.tr("/", ":")
141 munin_plugin "postgres_bgwriter_#{suffix}" do
142 target "postgres_bgwriter"
144 conf_variables :port => details[:port]
147 munin_plugin "postgres_checkpoints_#{suffix}" do
148 target "postgres_checkpoints"
150 conf_variables :port => details[:port]
153 munin_plugin "postgres_connections_db_#{suffix}" do
154 target "postgres_connections_db"
156 conf_variables :port => details[:port]
159 munin_plugin "postgres_users_#{suffix}" do
160 target "postgres_users"
162 conf_variables :port => details[:port]
165 munin_plugin "postgres_xlog_#{suffix}" do
166 target "postgres_xlog"
168 conf_variables :port => details[:port]
171 next unless File.exist?("/var/lib/postgresql/#{details[:version]}/main/recovery.conf")
173 munin_plugin "postgres_replication_#{suffix}" do
174 target "postgres_replication"
176 conf_variables :port => details[:port]
180 uris = clusters.collect do |_, details|
181 "postgres@:#{details[:port]}/postgres?host=/run/postgresql"
184 template "/etc/prometheus/exporters/postgres_queries.yml" do
185 source "postgres_queries.yml.erb"
191 prometheus_exporter "postgres" do
196 options "--extend.query-path=/etc/prometheus/exporters/postgres_queries.yml"
197 environment "DATA_SOURCE_URI" => uris.sort.uniq.first,
198 "PG_EXPORTER_AUTO_DISCOVER_DATABASES" => "true",
199 "PG_EXPORTER_EXCLUDE_DATABASES" => "postgres,template0,template1"
200 restrict_address_families "AF_UNIX"
201 subscribes :restart, "template[/etc/prometheus/exporters/postgres_queries.yml]"