]> git.openstreetmap.org Git - chef.git/blob - cookbooks/postgresql/recipes/default.rb
Drop support for old versions of postgres
[chef.git] / cookbooks / postgresql / recipes / default.rb
1 #
2 # Cookbook:: postgresql
3 # Recipe:: default
4 #
5 # Copyright:: 2012, OpenStreetMap Foundation
6 #
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
10 #
11 #     https://www.apache.org/licenses/LICENSE-2.0
12 #
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.
18 #
19
20 include_recipe "apt::postgresql"
21 include_recipe "munin"
22 include_recipe "prometheus"
23
24 package "locales-all"
25 package "postgresql-common"
26
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}"
32
33   defaults = node[:postgresql][:settings][:defaults] || {}
34   settings = node[:postgresql][:settings][version] || {}
35
36   standby_mode = settings[:standby_mode] || defaults[:standby_mode]
37   primary_conninfo = settings[:primary_conninfo] || defaults[:primary_conninfo]
38
39   passwords = if primary_conninfo
40                 data_bag_item(primary_conninfo[:passwords][:bag],
41                               primary_conninfo[:passwords][:item])
42               end
43
44   template "/etc/postgresql/#{version}/main/postgresql.conf" do
45     source "postgresql.conf.erb"
46     owner "postgres"
47     group "postgres"
48     mode "644"
49     variables :version => version,
50               :defaults => defaults,
51               :settings => settings,
52               :primary_conninfo => primary_conninfo,
53               :passwords => passwords
54     notifies :reload, "service[postgresql]"
55     only_if { ::Dir.exist?("/etc/postgresql/#{version}/main") }
56   end
57
58   template "/etc/postgresql/#{version}/main/pg_hba.conf" do
59     source "pg_hba.conf.erb"
60     owner "postgres"
61     group "postgres"
62     mode "640"
63     variables :early_rules => settings[:early_authentication_rules] || defaults[:early_authentication_rules],
64               :late_rules => settings[:late_authentication_rules] || defaults[:late_authentication_rules]
65     notifies :reload, "service[postgresql]"
66     only_if { ::Dir.exist?("/etc/postgresql/#{version}/main") }
67   end
68
69   template "/etc/postgresql/#{version}/main/pg_ident.conf" do
70     source "pg_ident.conf.erb"
71     owner "postgres"
72     group "postgres"
73     mode "640"
74     variables :maps => settings[:user_name_maps] || defaults[:user_name_maps]
75     notifies :reload, "service[postgresql]"
76     only_if { ::Dir.exist?("/etc/postgresql/#{version}/main") }
77   end
78
79   link "/var/lib/postgresql/#{version}/main/server.crt" do
80     to "/etc/ssl/certs/ssl-cert-snakeoil.pem"
81     only_if { ::Dir.exist?("/var/lib/postgresql/#{version}/main") }
82   end
83
84   link "/var/lib/postgresql/#{version}/main/server.key" do
85     to "/etc/ssl/private/ssl-cert-snakeoil.key"
86     only_if { ::Dir.exist?("/var/lib/postgresql/#{version}/main") }
87   end
88
89   if standby_mode == "on"
90     file "/var/lib/postgresql/#{version}/main/standby.signal" do
91       owner "postgres"
92       group "postgres"
93       mode "640"
94     end
95   else
96     file "/var/lib/postgresql/#{version}/main/standby.signal" do
97       action :delete
98     end
99   end
100 end
101
102 service "postgresql" do
103   action [:enable, :start]
104   supports :status => true, :restart => true, :reload => true
105 end
106
107 ohai_plugin "postgresql" do
108   template "ohai.rb.erb"
109 end
110
111 package "pgtop"
112 package "libdbd-pg-perl"
113
114 clusters = node[:postgresql][:clusters] || []
115
116 clusters.each do |name, details|
117   suffix = name.tr("/", ":")
118
119   munin_plugin "postgres_bgwriter_#{suffix}" do
120     target "postgres_bgwriter"
121     conf "munin.erb"
122     conf_variables :port => details[:port]
123   end
124
125   munin_plugin "postgres_checkpoints_#{suffix}" do
126     target "postgres_checkpoints"
127     conf "munin.erb"
128     conf_variables :port => details[:port]
129   end
130
131   munin_plugin "postgres_connections_db_#{suffix}" do
132     target "postgres_connections_db"
133     conf "munin.erb"
134     conf_variables :port => details[:port]
135   end
136
137   munin_plugin "postgres_users_#{suffix}" do
138     target "postgres_users"
139     conf "munin.erb"
140     conf_variables :port => details[:port]
141   end
142
143   munin_plugin "postgres_xlog_#{suffix}" do
144     target "postgres_xlog"
145     conf "munin.erb"
146     conf_variables :port => details[:port]
147   end
148
149   next unless File.exist?("/var/lib/postgresql/#{details[:version]}/main/recovery.conf")
150
151   munin_plugin "postgres_replication_#{suffix}" do
152     target "postgres_replication"
153     conf "munin.erb"
154     conf_variables :port => details[:port]
155   end
156 end
157
158 uris = clusters.collect do |_, details|
159   "postgres@:#{details[:port]}/postgres?host=/run/postgresql"
160 end
161
162 template "/etc/prometheus/exporters/postgres_queries.yml" do
163   source "postgres_queries.yml.erb"
164   owner "root"
165   group "root"
166   mode "644"
167 end
168
169 prometheus_exporter "postgres" do
170   port 9187
171   scrape_interval "1m"
172   scrape_timeout "1m"
173   user "postgres"
174   options "--extend.query-path=/etc/prometheus/exporters/postgres_queries.yml"
175   environment "DATA_SOURCE_URI" => uris.sort.uniq.first,
176               "PG_EXPORTER_AUTO_DISCOVER_DATABASES" => "true",
177               "PG_EXPORTER_EXCLUDE_DATABASES" => "postgres,template0,template1"
178   restrict_address_families "AF_UNIX"
179   remove_ipc false
180   subscribes :restart, "template[/etc/prometheus/exporters/postgres_queries.yml]"
181 end