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