]> git.openstreetmap.org Git - chef.git/blob - cookbooks/nominatim/recipes/default.rb
Merge remote-tracking branch 'github/pull/350' into master
[chef.git] / cookbooks / nominatim / recipes / default.rb
1 #
2 # Cookbook:: 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 #     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 "accounts"
21 include_recipe "munin"
22 include_recipe "php::fpm"
23
24 basedir = data_bag_item("accounts", "nominatim")["home"]
25 email_errors = data_bag_item("accounts", "lonvia")["email"]
26
27 directory basedir do
28   owner "nominatim"
29   group "nominatim"
30   mode "755"
31   recursive true
32 end
33
34 directory node[:nominatim][:logdir] do
35   owner "nominatim"
36   group "nominatim"
37   mode "755"
38   recursive true
39 end
40
41 file "#{node[:nominatim][:logdir]}/query.log" do
42   action :create_if_missing
43   owner "www-data"
44   group "adm"
45   mode "664"
46 end
47
48 file "#{node[:nominatim][:logdir]}/update.log" do
49   action :create_if_missing
50   owner "nominatim"
51   group "adm"
52   mode "664"
53 end
54
55 # exception granted for a limited time so that they can set up their own server
56 firewall_rule "increase-limits-gnome-proxy" do
57   action :accept
58   family "inet"
59   source "net:8.43.85.23"
60   dest "fw"
61   proto "tcp:syn"
62   dest_ports "https"
63   rate_limit "s:10/sec:30"
64 end
65
66 ## Postgresql
67
68 include_recipe "postgresql"
69
70 postgresql_version = node[:nominatim][:dbcluster].split("/").first
71 postgis_version = node[:nominatim][:postgis]
72
73 package "postgresql-#{postgresql_version}-postgis-#{postgis_version}"
74
75 node[:nominatim][:dbadmins].each do |user|
76   postgresql_user user do
77     cluster node[:nominatim][:dbcluster]
78     superuser true
79     only_if { node[:nominatim][:state] != "slave" }
80   end
81 end
82
83 postgresql_user "nominatim" do
84   cluster node[:nominatim][:dbcluster]
85   superuser true
86   only_if { node[:nominatim][:state] != "slave" }
87 end
88
89 postgresql_user "www-data" do
90   cluster node[:nominatim][:dbcluster]
91   only_if { node[:nominatim][:state] != "slave" }
92 end
93
94 postgresql_munin "nominatim" do
95   cluster node[:nominatim][:dbcluster]
96   database node[:nominatim][:dbname]
97 end
98
99 directory "#{basedir}/tablespaces" do
100   owner "postgres"
101   group "postgres"
102   mode "700"
103 end
104
105 # NOTE: tablespaces must be exactly in the same location on each
106 #       Nominatim instance when replication is in use. Therefore
107 #       use symlinks to canonical directory locations.
108 node[:nominatim][:tablespaces].each do |name, location|
109   directory location do
110     owner "postgres"
111     group "postgres"
112     mode "700"
113     recursive true
114   end
115
116   link "#{basedir}/tablespaces/#{name}" do
117     to location
118   end
119
120   postgresql_tablespace name do
121     cluster node[:nominatim][:dbcluster]
122     location "#{basedir}/tablespaces/#{name}"
123   end
124 end
125
126 if node[:nominatim][:state] == "master"
127   postgresql_user "replication" do
128     cluster node[:nominatim][:dbcluster]
129     password data_bag_item("nominatim", "passwords")["replication"]
130     replication true
131   end
132
133   directory node[:rsyncd][:modules][:archive][:path] do
134     owner "postgres"
135     group "postgres"
136     mode "700"
137   end
138
139   template "/usr/local/bin/clean-db-nominatim" do
140     source "clean-db-nominatim.erb"
141     owner "root"
142     group "root"
143     mode "755"
144     variables :archive_dir => node[:rsyncd][:modules][:archive][:path],
145               :update_stop_file => "#{basedir}/status/updates_disabled",
146               :streaming_clients => search(:node, "nominatim_state:slave").map { |slave| slave[:fqdn] }.join(" ")
147   end
148 end
149
150 ## Nominatim backend
151
152 include_recipe "git"
153
154 package %w[
155   build-essential
156   cmake
157   g++
158   libboost-dev
159   libboost-system-dev
160   libboost-filesystem-dev
161   libexpat1-dev
162   zlib1g-dev
163   libxml2-dev
164   libbz2-dev
165   libpq-dev
166   libgeos++-dev
167   libproj-dev
168   python3-pyosmium
169   pyosmium
170   python3-psycopg2
171   php-pgsql
172   php-intl
173 ]
174
175 source_directory = "#{basedir}/nominatim"
176 build_directory = "#{basedir}/bin"
177 ui_directory = "#{basedir}/ui"
178
179 directory build_directory do
180   owner "nominatim"
181   group "nominatim"
182   mode "755"
183   recursive true
184 end
185
186 # Normally syncing via chef is a bad idea because syncing might involve
187 # an update of database functions which should not be done while an update
188 # is ongoing. Therefore we sync in between update cycles. There is an
189 # exception for slaves: they get DB function updates from the master, so
190 # only the source code needs to be updated, which chef may do.
191 git source_directory do
192   action node[:nominatim][:state] == "slave" ? :sync : :checkout
193   repository node[:nominatim][:repository]
194   revision node[:nominatim][:revision]
195   enable_submodules true
196   user "nominatim"
197   group "nominatim"
198   not_if { node[:nominatim][:state] != "slave" && File.exist?("#{source_directory}/README.md") }
199   notifies :run, "execute[compile_nominatim]", :immediately
200 end
201
202 execute "compile_nominatim" do
203   action :nothing
204   user "nominatim"
205   cwd build_directory
206   command "cmake #{source_directory} && make"
207 end
208
209 template "#{source_directory}/.git/hooks/post-merge" do
210   source "git-post-merge-hook.erb"
211   owner "nominatim"
212   group "nominatim"
213   mode "755"
214   variables :srcdir => source_directory,
215             :builddir => build_directory,
216             :dbname => node[:nominatim][:dbname]
217 end
218
219 template "#{build_directory}/settings/local.php" do
220   source "settings.erb"
221   owner "nominatim"
222   group "nominatim"
223   mode "664"
224   variables :base_url => node[:nominatim][:state] == "off" ? node[:fqdn] : "nominatim.openstreetmap.org",
225             :dbname => node[:nominatim][:dbname],
226             :flatnode_file => node[:nominatim][:flatnode_file],
227             :log_file => "#{node[:nominatim][:logdir]}/query.log"
228 end
229
230 git ui_directory do
231   action :sync
232   repository node[:nominatim][:ui_repository]
233   revision node[:nominatim][:ui_revision]
234   user "nominatim"
235   group "nominatim"
236 end
237
238 template "#{ui_directory}/dist/config.js" do
239   source "ui-config.js.erb"
240   owner "nominatim"
241   group "nominatim"
242   mode "664"
243 end
244
245 if node[:nominatim][:flatnode_file]
246   directory File.dirname(node[:nominatim][:flatnode_file]) do
247     recursive true
248   end
249 end
250
251 template "/etc/logrotate.d/nominatim" do
252   source "logrotate.nominatim.erb"
253   owner "root"
254   group "root"
255   mode "644"
256 end
257
258 external_data = [
259   "wikimedia-importance.sql.gz",
260   "gb_postcode_data.sql.gz"
261 ]
262
263 external_data.each do |fname|
264   remote_file "#{source_directory}/data/#{fname}" do
265     action :create_if_missing
266     source "https://www.nominatim.org/data/#{fname}"
267     owner "nominatim"
268     group "nominatim"
269     mode "644"
270   end
271 end
272
273 remote_file "#{source_directory}/data/country_osm_grid.sql.gz" do
274   action :create_if_missing
275   source "https://www.nominatim.org/data/country_grid.sql.gz"
276   owner "nominatim"
277   group "nominatim"
278   mode "644"
279 end
280
281 if node[:nominatim][:state] == "off"
282   cron_d "nominatim-backup" do
283     action :delete
284   end
285
286   cron_d "nominatim-vacuum-db" do
287     action :delete
288   end
289
290   cron_d "nominatim-clean-db" do
291     action :delete
292   end
293
294   cron_d "nominatim-update-maintenance-trigger" do
295     action :delete
296   end
297 else
298   cron_d "nominatim-backup" do
299     action node[:nominatim][:enable_backup] ? :create : :delete
300     minute "0"
301     hour "3"
302     day "1"
303     user "nominatim"
304     command "/usr/local/bin/backup-nominatim"
305     mailto email_errors
306   end
307
308   cron_d "nominatim-vacuum-db" do
309     minute "20"
310     hour "0"
311     user "postgres"
312     command "/usr/local/bin/vacuum-db-nominatim"
313     mailto email_errors
314   end
315
316   cron_d "nominatim-clean-db" do
317     action node[:nominatim][:state] == "master" ? :create : :delete
318     minute "5"
319     hour "*/4"
320     user "postgres"
321     command "/usr/local/bin/clean-db-nominatim"
322     mailto email_errors
323   end
324
325   cron_d "nominatim-update-maintenance-trigger" do
326     minute "18"
327     hour "1"
328     user "nominatim"
329     command "touch #{basedir}/status/update_maintenance"
330     mailto email_errors
331   end
332 end
333
334 template "#{source_directory}/utils/nominatim-update" do
335   source "updater.erb"
336   user "nominatim"
337   group "nominatim"
338   mode "755"
339   variables :bindir => build_directory,
340             :srcdir => source_directory,
341             :logfile => "#{node[:nominatim][:logdir]}/update.log",
342             :branch => node[:nominatim][:revision],
343             :update_stop_file => "#{basedir}/status/updates_disabled",
344             :update_maintenance_trigger => "#{basedir}/status/update_maintenance"
345 end
346
347 template "/etc/init.d/nominatim-update" do
348   source "updater.init.erb"
349   user "nominatim"
350   group "nominatim"
351   mode "755"
352   variables :source_directory => source_directory
353 end
354
355 %w[backup-nominatim vacuum-db-nominatim].each do |fname|
356   template "/usr/local/bin/#{fname}" do
357     source "#{fname}.erb"
358     owner "root"
359     group "root"
360     mode "755"
361     variables :db => node[:nominatim][:dbname]
362   end
363 end
364
365 ## webserver frontend
366
367 directory "#{basedir}/etc" do
368   owner "nominatim"
369   group "adm"
370   mode "775"
371 end
372
373 %w[user_agent referrer email generic].each do |name|
374   file "#{basedir}/etc/nginx_blocked_#{name}.conf" do
375     action :create_if_missing
376     owner "nominatim"
377     group "adm"
378     mode "664"
379   end
380 end
381
382 node[:nominatim][:fpm_pools].each do |name, data|
383   php_fpm name.to_s do
384     port data[:port]
385     pm data[:pm]
386     pm_max_children data[:max_children]
387     pm_start_servers 20
388     pm_min_spare_servers 10
389     pm_max_spare_servers 20
390     pm_max_requests 10000
391   end
392 end
393
394 ssl_certificate node[:fqdn] do
395   domains [node[:fqdn],
396            "nominatim.openstreetmap.org",
397            "nominatim.osm.org",
398            "nominatim.openstreetmap.com",
399            "nominatim.openstreetmap.net",
400            "nominatim.openstreetmaps.org",
401            "nominatim.openmaps.org"]
402   notifies :reload, "service[nginx]"
403 end
404
405 package "apache2" do
406   action :remove
407 end
408
409 include_recipe "nginx"
410
411 nginx_site "default" do
412   action [:delete]
413 end
414
415 frontends = search(:node, "recipes:web\\:\\:frontend").sort_by(&:name)
416
417 nginx_site "nominatim" do
418   template "nginx.erb"
419   directory build_directory
420   variables :pools => node[:nominatim][:fpm_pools],
421             :frontends => frontends,
422             :confdir => "#{basedir}/etc",
423             :ui_directory => ui_directory
424 end
425
426 template "/etc/logrotate.d/nginx" do
427   source "logrotate.nginx.erb"
428   owner "root"
429   group "root"
430   mode "644"
431 end
432
433 munin_plugin_conf "nominatim" do
434   template "munin.erb"
435   variables :db => node[:nominatim][:dbname],
436             :querylog => "#{node[:nominatim][:logdir]}/query.log"
437 end
438
439 munin_plugin "nominatim_importlag" do
440   target "#{source_directory}/munin/nominatim_importlag"
441 end
442
443 munin_plugin "nominatim_query_speed" do
444   target "#{source_directory}/munin/nominatim_query_speed_querylog"
445 end
446
447 munin_plugin "nominatim_requests" do
448   target "#{source_directory}/munin/nominatim_requests_querylog"
449 end
450
451 directory "#{basedir}/status" do
452   owner "nominatim"
453   group "postgres"
454   mode "775"
455 end
456
457 include_recipe "fail2ban"
458
459 frontend_addresses = frontends.collect { |f| f.ipaddresses(:role => :external) }
460
461 fail2ban_jail "nominatim_limit_req" do
462   filter "nginx-limit-req"
463   logpath "#{node[:nominatim][:logdir]}/nominatim.openstreetmap.org-error.log"
464   ports [80, 443]
465   maxretry 5
466   ignoreips frontend_addresses.flatten.sort
467 end