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