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 "prometheus"
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 standby_mode = settings[:standby_mode] || defaults[:standby_mode]
36 primary_conninfo = settings[:primary_conninfo] || defaults[:primary_conninfo]
38 passwords = if primary_conninfo
39 data_bag_item(primary_conninfo[:passwords][:bag],
40 primary_conninfo[:passwords][:item])
43 template "/etc/postgresql/#{version}/main/postgresql.conf" do
44 source "postgresql.conf.erb"
48 variables :version => version,
49 :defaults => defaults,
50 :settings => settings,
51 :primary_conninfo => primary_conninfo,
52 :passwords => passwords
53 notifies :reload, "service[postgresql]"
54 only_if { ::Dir.exist?("/etc/postgresql/#{version}/main") }
57 template "/etc/postgresql/#{version}/main/pg_hba.conf" do
58 source "pg_hba.conf.erb"
62 variables :early_rules => settings[:early_authentication_rules] || defaults[:early_authentication_rules],
63 :late_rules => settings[:late_authentication_rules] || defaults[:late_authentication_rules]
64 notifies :reload, "service[postgresql]"
65 only_if { ::Dir.exist?("/etc/postgresql/#{version}/main") }
68 template "/etc/postgresql/#{version}/main/pg_ident.conf" do
69 source "pg_ident.conf.erb"
73 variables :maps => settings[:user_name_maps] || defaults[:user_name_maps]
74 notifies :reload, "service[postgresql]"
75 only_if { ::Dir.exist?("/etc/postgresql/#{version}/main") }
78 link "/var/lib/postgresql/#{version}/main/server.crt" do
79 to "/etc/ssl/certs/ssl-cert-snakeoil.pem"
80 only_if { ::Dir.exist?("/var/lib/postgresql/#{version}/main") }
83 link "/var/lib/postgresql/#{version}/main/server.key" do
84 to "/etc/ssl/private/ssl-cert-snakeoil.key"
85 only_if { ::Dir.exist?("/var/lib/postgresql/#{version}/main") }
88 if standby_mode == "on"
89 file "/var/lib/postgresql/#{version}/main/standby.signal" do
95 file "/var/lib/postgresql/#{version}/main/standby.signal" do
101 service "postgresql" do
102 action [:enable, :start]
103 supports :status => true, :restart => true, :reload => true
106 ohai_plugin "postgresql" do
107 template "ohai.rb.erb"
111 package "libdbd-pg-perl"
113 clusters = node[:postgresql][:clusters] || []
114 passwords = data_bag_item("postgresql", "passwords")
116 clusters.each do |name, details|
117 prometheus_suffix = name.tr("/", "-")
118 prometheus_database = node[:postgresql][:monitor_database]
120 postgresql_user "prometheus" do
122 password passwords["prometheus"]
124 not_if { ::File.exist?("/var/lib/postgresql/#{name}/standby.signal") }
127 prometheus_exporter "postgres" do
128 port 10000 + details[:port].to_i
129 service "postgres-#{prometheus_suffix}"
130 labels "cluster" => name
134 --collector.database_wraparound
135 --collector.long_running_transactions
136 --collector.process_idle
137 --collector.stat_activity_autovacuum
138 --collector.stat_wal_receiver
139 --collector.statio_user_indexes
141 environment "DATA_SOURCE_NAME" => "postgres:///#{prometheus_database}?host=/run/postgresql&port=#{details[:port]}&user=prometheus&password=#{passwords['prometheus']}"
142 restrict_address_families "AF_UNIX"
143 subscribes :restart, "template[/etc/prometheus/exporters/postgres_queries.yml]"
146 if node[:postgresql][:monitor_queries]
147 template "/etc/prometheus/exporters/sql_exporter.yml" do
148 source "sql_exporter.yml.erb"
154 prometheus_exporter "sql" do
155 port 20000 + details[:port].to_i
156 service "sql-#{prometheus_suffix}"
157 labels "cluster" => name
160 options "--config.file=/etc/prometheus/exporters/sql_exporter.yml"
161 environment "SQLEXPORTER_TARGET_DSN" => "postgres://prometheus:#{passwords['prometheus']}@/run/postgresql:#{details[:port]}/#{prometheus_database}"
162 restrict_address_families "AF_UNIX"
163 subscribes :restart, "template[/etc/prometheus/exporters/sql_exporter.yml]"
166 prometheus_exporter "sql" do
168 service "sql-#{prometheus_suffix}"
171 file "/etc/prometheus/exporters/sql_exporter.yml" do