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