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