]> git.openstreetmap.org Git - chef.git/blob - cookbooks/postgresql/recipes/default.rb
Split out default munin plugin installation to a new recipe
[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 "munin"
21
22 package "locales-all"
23 package "postgresql-common"
24
25 node[:postgresql][:versions].each do |version|
26   package "postgresql-#{version}"
27   package "postgresql-client-#{version}"
28   package "postgresql-contrib-#{version}"
29   package "postgresql-server-dev-#{version}"
30
31   defaults = node[:postgresql][:settings][:defaults] || {}
32   settings = node[:postgresql][:settings][version] || {}
33
34   template "/etc/postgresql/#{version}/main/postgresql.conf" do
35     source "postgresql.conf.erb"
36     owner "postgres"
37     group "postgres"
38     mode 0o644
39     variables :version => version, :defaults => defaults, :settings => settings
40     notifies :reload, "service[postgresql]"
41   end
42
43   template "/etc/postgresql/#{version}/main/pg_hba.conf" do
44     source "pg_hba.conf.erb"
45     owner "postgres"
46     group "postgres"
47     mode 0o640
48     variables :early_rules => settings[:early_authentication_rules] || defaults[:early_authentication_rules],
49               :late_rules => settings[:late_authentication_rules] || defaults[:late_authentication_rules]
50     notifies :reload, "service[postgresql]"
51   end
52
53   template "/etc/postgresql/#{version}/main/pg_ident.conf" do
54     source "pg_ident.conf.erb"
55     owner "postgres"
56     group "postgres"
57     mode 0o640
58     variables :maps => settings[:user_name_maps] || defaults[:user_name_maps]
59     notifies :reload, "service[postgresql]"
60   end
61
62   link "/var/lib/postgresql/#{version}/main/server.crt" do
63     to "/etc/ssl/certs/ssl-cert-snakeoil.pem"
64   end
65
66   link "/var/lib/postgresql/#{version}/main/server.key" do
67     to "/etc/ssl/private/ssl-cert-snakeoil.key"
68   end
69
70   standby_mode = settings[:standby_mode] || defaults[:standby_mode]
71   primary_conninfo = settings[:primary_conninfo] || defaults[:primary_conninfo]
72   restore_command = settings[:restore_command] || defaults[:restore_command]
73
74   if restore_command || standby_mode == "on"
75     passwords = if primary_conninfo
76                   data_bag_item(primary_conninfo[:passwords][:bag],
77                                 primary_conninfo[:passwords][:item])
78                 end
79
80     template "/var/lib/postgresql/#{version}/main/recovery.conf" do
81       source "recovery.conf.erb"
82       owner "postgres"
83       group "postgres"
84       mode 0o640
85       variables :standby_mode => standby_mode,
86                 :primary_conninfo => primary_conninfo,
87                 :restore_command => restore_command,
88                 :passwords => passwords
89       notifies :reload, "service[postgresql]"
90     end
91   else
92     template "/var/lib/postgresql/#{version}/main/recovery.conf" do
93       action :delete
94       notifies :reload, "service[postgresql]"
95     end
96   end
97 end
98
99 service "postgresql" do
100   action [:enable, :start]
101   supports :status => true, :restart => true, :reload => true
102 end
103
104 ohai_plugin "postgresql" do
105   template "ohai.rb.erb"
106 end
107
108 package "ptop"
109 package "libdbd-pg-perl"
110
111 clusters = node[:postgresql][:clusters] || []
112
113 clusters.each do |name, details|
114   suffix = name.tr("/", ":")
115
116   munin_plugin "postgres_bgwriter_#{suffix}" do
117     target "postgres_bgwriter"
118     conf "munin.erb"
119     conf_variables :port => details[:port]
120   end
121
122   munin_plugin "postgres_checkpoints_#{suffix}" do
123     target "postgres_checkpoints"
124     conf "munin.erb"
125     conf_variables :port => details[:port]
126   end
127
128   munin_plugin "postgres_connections_db_#{suffix}" do
129     target "postgres_connections_db"
130     conf "munin.erb"
131     conf_variables :port => details[:port]
132   end
133
134   munin_plugin "postgres_users_#{suffix}" do
135     target "postgres_users"
136     conf "munin.erb"
137     conf_variables :port => details[:port]
138   end
139
140   munin_plugin "postgres_xlog_#{suffix}" do
141     target "postgres_xlog"
142     conf "munin.erb"
143     conf_variables :port => details[:port]
144   end
145
146   next unless File.exist?("/var/lib/postgresql/#{details[:version]}/main/recovery.conf")
147
148   munin_plugin "postgres_replication_#{suffix}" do
149     target "postgres_replication"
150     conf "munin.erb"
151     conf_variables :port => details[:port]
152   end
153 end