]> git.openstreetmap.org Git - chef.git/blob - cookbooks/nominatim/recipes/default.rb
00bfb2715900da39225fcc5c5af7ff1e86697f4e
[chef.git] / cookbooks / nominatim / recipes / default.rb
1 #
2 # Cookbook Name:: nominatim
3 # Recipe:: base
4 #
5 # Copyright 2015, 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 basedir = data_bag_item("accounts", "nominatim")["home"]
21 email_errors = data_bag_item("accounts", "lonvia")["email"]
22
23 directory node[:nominatim][:logdir] do
24   owner "nominatim"
25   group "nominatim"
26   mode 0o755
27   recursive true
28 end
29
30 file "#{node[:nominatim][:logdir]}/query.log" do
31   action :create_if_missing
32   owner "www-data"
33   group "adm"
34   mode 0o664
35 end
36
37 file "#{node[:nominatim][:logdir]}/update.log" do
38   action :create_if_missing
39   owner "nominatim"
40   group "adm"
41   mode 0o664
42 end
43
44 directory "#{basedir}/status" do
45   owner "nominatim"
46   group "postgres"
47   mode 0o775
48 end
49
50 ## Postgresql
51
52 include_recipe "postgresql"
53
54 package "postgis"
55
56 node[:nominatim][:dbadmins].each do |user|
57   postgresql_user user do
58     cluster node[:nominatim][:dbcluster]
59     superuser true
60     only_if { node[:nominatim][:state] != "slave" }
61   end
62 end
63
64 postgresql_user "nominatim" do
65   cluster node[:nominatim][:dbcluster]
66   superuser true
67   only_if { node[:nominatim][:state] != "slave" }
68 end
69
70 postgresql_user "www-data" do
71   cluster node[:nominatim][:dbcluster]
72   only_if { node[:nominatim][:state] != "slave" }
73 end
74
75 postgresql_munin "nominatim" do
76   cluster node[:nominatim][:dbcluster]
77   database node[:nominatim][:dbname]
78 end
79
80 directory "#{basedir}/tablespaces" do
81   owner "postgres"
82   group "postgres"
83   mode 0o700
84 end
85
86 # Note: tablespaces must be exactly in the same location on each
87 #       Nominatim instance when replication is in use. Therefore
88 #       use symlinks to canonical directory locations.
89 node[:nominatim][:tablespaces].each do |name, location|
90   directory location do
91     owner "postgres"
92     group "postgres"
93     mode 0o700
94     recursive true
95   end
96
97   link "#{basedir}/tablespaces/#{name}" do
98     to location
99   end
100
101   postgresql_tablespace name do
102     cluster node[:nominatim][:dbcluster]
103     location "#{basedir}/tablespaces/#{name}"
104   end
105 end
106
107 if node[:nominatim][:state] == "master" # ~FC023
108   postgresql_user "replication" do
109     cluster node[:nominatim][:dbcluster]
110     password data_bag_item("nominatim", "passwords")["replication"]
111     replication true
112   end
113
114   directory node[:rsyncd][:modules][:archive][:path] do
115     owner "postgres"
116     group "postgres"
117     mode 0o700
118   end
119
120   template "/usr/local/bin/clean-db-nominatim" do
121     source "clean-db-nominatim.erb"
122     owner "root"
123     group "root"
124     mode 0o755
125     variables :archive_dir => node[:rsyncd][:modules][:archive][:path],
126               :update_stop_file => "#{basedir}/status/updates_disabled",
127               :streaming_clients => search(:node, "nominatim_state:slave").map { |slave| slave[:fqdn] }.join(" ")
128   end
129 end
130
131 ## Nominatim backend
132
133 include_recipe "git"
134
135 package "build-essential"
136 package "cmake"
137 package "g++"
138 package "libboost-dev"
139 package "libboost-system-dev"
140 package "libboost-filesystem-dev"
141 package "libexpat1-dev"
142 package "zlib1g-dev"
143 package "libxml2-dev"
144 package "libbz2-dev"
145 package "libpq-dev"
146 package "libgeos++-dev"
147 package "libproj-dev"
148 package "osmosis"
149
150 source_directory = "#{basedir}/nominatim"
151 build_directory = "#{basedir}/bin"
152
153 directory build_directory do
154   owner "nominatim"
155   group "nominatim"
156   mode 0o755
157   recursive true
158 end
159
160 # Normally syncing via chef is a bad idea because syncing might involve
161 # an update of database functions which should not be done while an update
162 # is ongoing. Therefore we sync in between update cycles. There is an
163 # exception for slaves: they get DB function updates from the master, so
164 # only the source code needs to be updated, which chef may do.
165 git source_directory do
166   action node[:nominatim][:state] == "slave" ? :sync : :checkout
167   repository node[:nominatim][:repository]
168   revision node[:nominatim][:revision]
169   enable_submodules true
170   user "nominatim"
171   group "nominatim"
172   notifies :run, "execute[compile_nominatim]", :immediately
173 end
174
175 execute "compile_nominatim" do
176   action :nothing
177   user "nominatim"
178   cwd build_directory
179   command "cmake #{source_directory} && make"
180 end
181
182 template "#{source_directory}/.git/hooks/post-merge" do
183   source "git-post-merge-hook.erb"
184   owner "nominatim"
185   group "nominatim"
186   mode 0o755
187   variables :srcdir => source_directory,
188             :builddir => build_directory,
189             :dbname => node[:nominatim][:dbname]
190 end
191
192 template "#{build_directory}/settings/local.php" do
193   source "settings.erb"
194   owner "nominatim"
195   group "nominatim"
196   mode 0o664
197   variables :base_url => node[:nominatim][:state] == "off" ? node[:fqdn] : "nominatim.openstreetmap.org",
198             :dbname => node[:nominatim][:dbname],
199             :flatnode_file => node[:nominatim][:flatnode_file],
200             :log_file => "#{node[:nominatim][:logdir]}/query.log"
201 end
202
203 if node[:nominatim][:flatnode_file] # ~FC023
204   directory File.dirname(node[:nominatim][:flatnode_file]) do
205     recursive true
206   end
207 end
208
209 template "/etc/logrotate.d/nominatim" do
210   source "logrotate.nominatim.erb"
211   owner "root"
212   group "root"
213   mode 0o644
214 end
215
216 external_data = [
217   "wikipedia_article.sql.bin",
218   "wikipedia_redirect.sql.bin",
219   "gb_postcode_data.sql.gz"
220 ]
221
222 external_data.each do |fname|
223   remote_file "#{source_directory}/data/#{fname}" do
224     action :create_if_missing
225     source "http://www.nominatim.org/data/#{fname}"
226     owner "nominatim"
227     group "nominatim"
228     mode 0o644
229   end
230 end
231
232 template "/etc/cron.d/nominatim" do
233   action node[:nominatim][:state] == :off ? :delete : :create
234   source "nominatim.cron.erb"
235   owner "root"
236   group "root"
237   mode "0644"
238   variables :bin_directory => "#{source_directory}/utils", :mailto => email_errors
239 end
240
241 template "#{source_directory}/utils/nominatim-update" do
242   source "updater.erb"
243   user "nominatim"
244   group "nominatim"
245   mode 0o755
246   variables :logfile => "#{node[:nominatim][:logdir]}/update.log",
247             :branch => node[:nominatim][:revision],
248             :update_stop_file => "#{basedir}/status/updates_disabled"
249 end
250
251 template "/etc/init.d/nominatim-update" do
252   source "updater.init.erb"
253   user "nominatim"
254   group "nominatim"
255   mode 0o755
256   variables :source_directory => source_directory
257 end
258
259 %w(backup-nominatim vacuum-db-nominatim).each do |fname|
260   template "/usr/local/bin/#{fname}" do
261     source "#{fname}.erb"
262     owner "root"
263     group "root"
264     mode 0o755
265     variables :db => node[:nominatim][:dbname]
266   end
267 end
268
269 ## webserver frontend
270
271 template "#{build_directory}/settings/ip_blocks.conf" do
272   action :create_if_missing
273   source "ipblocks.erb"
274   owner "nominatim"
275   group "nominatim"
276   mode 0o664
277 end
278
279 file "#{build_directory}/settings/apache_blocks.conf" do
280   action :create_if_missing
281   owner "nominatim"
282   group "nominatim"
283   mode 0o664
284 end
285
286 file "#{build_directory}/settings/ip_blocks.map" do
287   action :create_if_missing
288   owner "nominatim"
289   group "nominatim"
290   mode 0o664
291 end
292
293 include_recipe "apache::ssl"
294
295 package "php"
296 package "php-fpm"
297 package "php-pgsql"
298 package "php-pear"
299 package "php-db"
300
301 apache_module "rewrite"
302 apache_module "proxy"
303 apache_module "proxy_fcgi"
304 apache_module "proxy_http"
305 apache_module "headers"
306
307 service "php5-fpm" do
308   if node[:lsb][:release].to_f >= 15.10
309     provider Chef::Provider::Service::Systemd
310     service_name "php7.0-fpm"
311   elsif node[:lsb][:release].to_f >= 14.04
312     provider Chef::Provider::Service::Upstart
313   end
314   action [:enable, :start]
315   supports :status => true, :restart => true, :reload => true
316 end
317
318 php_confdir = node[:lsb][:release].to_f >= 15.10 ? "/etc/php/7.0" : "/etc/php5"
319
320 node[:nominatim][:fpm_pools].each do |name, data|
321   template "#{php_confdir}/fpm/pool.d/#{name}.conf" do
322     source "fpm.conf.erb"
323     owner "root"
324     group "root"
325     mode 0o644
326     variables data.merge(:name => name)
327     notifies :reload, "service[php5-fpm]"
328   end
329 end
330
331 apache_site "nominatim.openstreetmap.org" do
332   template "apache.erb"
333   directory build_directory
334   variables :pools => node[:nominatim][:fpm_pools]
335 end
336
337 apache_site "default" do
338   action [:disable]
339 end
340
341 template "/etc/logrotate.d/apache2" do
342   source "logrotate.apache.erb"
343   owner "root"
344   group "root"
345   mode 0o644
346 end
347
348 include_recipe "fail2ban"
349
350 fail2ban_filter "nominatim" do
351   failregex '^<HOST> - - \[[^]]+\] "[^"]+" 429 '
352 end
353
354 fail2ban_jail "nominatim" do
355   filter "nominatim"
356   logpath "/var/log/apache2/nominatim.openstreetmap.org-access.log"
357   ports [80, 443]
358   maxretry 100
359 end
360
361 munin_plugin_conf "nominatim" do
362   template "munin.erb"
363   variables :db => node[:nominatim][:dbname],
364             :querylog => "#{node[:nominatim][:logdir]}/query.log"
365 end
366
367 munin_plugin "nominatim_importlag" do
368   target "#{source_directory}/munin/nominatim_importlag"
369 end
370
371 munin_plugin "nominatim_query_speed" do
372   target "#{source_directory}/munin/nominatim_query_speed_querylog"
373 end
374
375 munin_plugin "nominatim_requests" do
376   target "#{source_directory}/munin/nominatim_requests_querylog"
377 end
378
379 munin_plugin "nominatim_throttled_ips" do
380   target "#{source_directory}/munin/nominatim_throttled_ips"
381 end