]> git.openstreetmap.org Git - chef.git/blob - cookbooks/nominatim/recipes/default.rb
df5f6618f89dcb38d35387f70998a74f4f0f7ed2
[chef.git] / cookbooks / nominatim / recipes / default.rb
1 #
2 # Cookbook Name:: nominatim
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 include_recipe "apache::ssl"
21 include_recipe "postgresql"
22 include_recipe "git"
23
24 package "php5"
25 package "php5-cli"
26 package "php5-pgsql"
27 package "php5-fpm"
28 package "php-pear"
29 package "php-apc"
30
31 apache_module "rewrite"
32 apache_module "proxy"
33 apache_module "proxy_fcgi"
34
35 passwords = data_bag_item("nominatim", "passwords")
36 home_directory = data_bag_item("accounts", "nominatim")["home"]
37 source_directory = "#{home_directory}/nominatim"
38 email_errors = data_bag_item("accounts", "lonvia")["email"]
39
40 database_cluster = node[:nominatim][:database][:cluster]
41 database_version = database_cluster.sub(%r{/.*}, "")
42 database_name = node[:nominatim][:database][:dbname]
43
44 postgis_version = node[:nominatim][:database][:postgis]
45
46 service "php5-fpm" do
47   provider Chef::Provider::Service::Upstart
48   action [:enable, :start]
49   supports :status => true, :restart => true, :reload => true
50 end
51
52 apache_site "nominatim.openstreetmap.org" do
53   template "apache.erb"
54   directory source_directory
55   variables :pools => node[:nominatim][:fpm_pools]
56 end
57
58 apache_site "default" do
59   action [:disable]
60 end
61
62 node[:nominatim][:fpm_pools].each do |name, data|
63   template "/etc/php5/fpm/pool.d/#{name}.conf" do
64     source "fpm.conf.erb"
65     owner "root"
66     group "root"
67     mode 0644
68     variables data.merge(:name => name, :port => data[:port])
69     notifies :reload, "service[php5-fpm]"
70   end
71 end
72
73 superusers = %w(tomh lonvia twain nominatim)
74
75 superusers.each do |user|
76   postgresql_user user do
77     cluster database_cluster
78     superuser true
79   end
80 end
81
82 postgresql_user "www-data" do
83   cluster database_cluster
84 end
85
86 postgresql_user "replication" do
87   cluster database_cluster
88   password passwords["replication"]
89   replication true
90 end
91
92 postgresql_munin "nominatim" do
93   cluster database_cluster
94   database database_name
95 end
96
97 directory "/var/log/nominatim" do
98   owner "nominatim"
99   group "nominatim"
100   mode 0755
101 end
102
103 template "/etc/logrotate.d/nominatim" do
104   source "logrotate.nominatim.erb"
105   owner "root"
106   group "root"
107   mode 0644
108 end
109
110 package "osmosis"
111 package "gcc"
112 package "proj-bin"
113 package "libgeos-c1"
114 package "postgresql-#{database_version}-postgis-#{postgis_version}"
115 package "postgresql-server-dev-#{database_version}"
116 package "build-essential"
117 package "libxml2-dev"
118 package "libgeos-dev"
119 package "libgeos++-dev"
120 package "libpq-dev"
121 package "libbz2-dev"
122 package "libtool"
123 package "automake"
124 package "libproj-dev"
125 package "libprotobuf-c0-dev"
126 package "protobuf-c-compiler"
127 package "python-psycopg2"
128 package "libboost-dev"
129 package "libboost-system-dev"
130 package "libboost-filesystem-dev"
131 package "libboost-thread-dev"
132
133 execute "php-pear-db" do
134   command "pear install DB"
135   not_if { File.exist?("/usr/share/php/DB") }
136 end
137
138 execute "compile_nominatim" do
139   action :nothing
140   command "cd #{source_directory} && ./autogen.sh && ./configure && make"
141   user "nominatim"
142 end
143
144 git source_directory do
145   action :checkout
146   repository node[:nominatim][:repository]
147   enable_submodules true
148   user "nominatim"
149   group "nominatim"
150   notifies :run, "execute[compile_nominatim]"
151 end
152
153 directory "#{source_directory}/log" do
154   owner "nominatim"
155   group "nominatim"
156   mode 0755
157 end
158
159 template "#{source_directory}/.git/hooks/post-merge" do
160   source "update_source.erb"
161   owner "nominatim"
162   group "nominatim"
163   mode 0755
164   variables :source_directory => source_directory
165 end
166
167 template "#{source_directory}/settings/local.php" do
168   source "nominatim.erb"
169   owner "nominatim"
170   group "nominatim"
171   mode 0664
172   variables :postgres_version => database_version
173 end
174
175 template "#{source_directory}/settings/ip_blocks.conf" do
176   action :create_if_missing
177   source "ipblocks.erb"
178   owner "nominatim"
179   group "nominatim"
180   mode 0664
181 end
182
183 file "#{source_directory}/settings/apache_blocks.conf" do
184   action :create_if_missing
185   owner "nominatim"
186   group "nominatim"
187   mode 0664
188 end
189
190 file "#{source_directory}/settings/ip_blocks.map" do
191   action :create_if_missing
192   owner "nominatim"
193   group "nominatim"
194   mode 0664
195 end
196
197 if node[:nominatim][:enabled]
198   cron_action = :create
199 else
200   cron_action = :delete
201 end
202
203 template "/etc/cron.d/nominatim" do
204   action cron_action
205   source "cron.erb"
206   owner "root"
207   group "root"
208   mode "0644"
209   variables :bin_directory => "#{source_directory}/utils", :mailto => email_errors
210 end
211
212 template "#{source_directory}/utils/nominatim-update" do
213   source "updater.erb"
214   user "nominatim"
215   group "nominatim"
216   mode 0755
217 end
218
219 template "/etc/init.d/nominatim-update" do
220   source "updater.init.erb"
221   user "nominatim"
222   group "nominatim"
223   mode 0755
224   variables :source_directory => source_directory
225 end
226
227 munin_plugin_conf "nominatim" do
228   template "munin.erb"
229 end
230
231 munin_plugin "nominatim_importlag" do
232   target "#{source_directory}/munin/nominatim_importlag"
233 end
234
235 munin_plugin "nominatim_query_speed" do
236   target "#{source_directory}/munin/nominatim_query_speed_querylog"
237 end
238
239 munin_plugin "nominatim_requests" do
240   target "#{source_directory}/munin/nominatim_requests_querylog"
241 end
242
243 munin_plugin "nominatim_throttled_ips" do
244   target "#{source_directory}/munin/nominatim_throttled_ips"
245 end
246
247 external_data = [
248   "wikipedia_article.sql.bin",
249   "wikipedia_redirect.sql.bin",
250   "gb_postcode_data.sql.gz"
251 ]
252
253 external_data.each do |fname|
254   remote_file "#{source_directory}/data/#{fname}" do
255     action :create_if_missing
256     source "http://www.nominatim.org/data/#{fname}"
257     owner "nominatim"
258     group "nominatim"
259     mode 0644
260   end
261 end
262
263 additional_scripts = %w(backup-nominatim clean-db-nominatim)
264
265 additional_scripts.each do |fname|
266   template "/usr/local/bin/#{fname}" do
267     source "#{fname}.erb"
268     owner "root"
269     group "root"
270     mode 0755
271   end
272 end
273
274 directory File.dirname(node[:nominatim][:flatnode_file]) do
275   owner "nominatim"
276   group "nominatim"
277   mode 0755
278   recursive true
279 end
280
281 directory "/data/postgresql-archive" do
282   owner "postgres"
283   group "postgres"
284   mode 0700
285   only_if { node[:postgresql][:settings][:defaults][:archive_mode] == "on" }
286 end
287
288 fail2ban_filter "nominatim" do
289   failregex '^<HOST> - - \[[^]]+\] "[^"]+" (403|429) '
290 end
291
292 fail2ban_jail "nominatim" do
293   filter "nominatim"
294   logpath "/var/log/apache2/nominatim.openstreetmap.org-access.log"
295   ports [80, 443]
296   maxretry 100
297 end