]> git.openstreetmap.org Git - chef.git/blob - cookbooks/nominatim/recipes/default.rb
nominatim: now needs dotenv package
[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   php-symfony-dotenv
174 ]
175
176 source_directory = "#{basedir}/nominatim"
177 build_directory = "#{basedir}/bin"
178 ui_directory = "#{basedir}/ui"
179
180 directory build_directory do
181   owner "nominatim"
182   group "nominatim"
183   mode "755"
184   recursive true
185 end
186
187 # Normally syncing via chef is a bad idea because syncing might involve
188 # an update of database functions which should not be done while an update
189 # is ongoing. Therefore we sync in between update cycles. There is an
190 # exception for slaves: they get DB function updates from the master, so
191 # only the source code needs to be updated, which chef may do.
192 git source_directory do
193   action node[:nominatim][:state] == "slave" ? :sync : :checkout
194   repository node[:nominatim][:repository]
195   revision node[:nominatim][:revision]
196   enable_submodules true
197   user "nominatim"
198   group "nominatim"
199   not_if { node[:nominatim][:state] != "slave" && File.exist?("#{source_directory}/README.md") }
200   notifies :run, "execute[compile_nominatim]", :immediately
201 end
202
203 execute "compile_nominatim" do
204   action :nothing
205   user "nominatim"
206   cwd build_directory
207   command "cmake #{source_directory} && make"
208 end
209
210 template "#{source_directory}/.git/hooks/post-merge" do
211   source "git-post-merge-hook.erb"
212   owner "nominatim"
213   group "nominatim"
214   mode "755"
215   variables :srcdir => source_directory,
216             :builddir => build_directory,
217             :dbname => node[:nominatim][:dbname]
218 end
219
220 template "#{build_directory}/settings/local.php" do
221   source "settings.erb"
222   owner "nominatim"
223   group "nominatim"
224   mode "664"
225   variables :base_url => node[:nominatim][:state] == "off" ? node[:fqdn] : "nominatim.openstreetmap.org",
226             :dbname => node[:nominatim][:dbname],
227             :flatnode_file => node[:nominatim][:flatnode_file],
228             :log_file => "#{node[:nominatim][:logdir]}/query.log"
229 end
230
231 template "#{build_directory}/.env" do
232   source "nominatim.env.erb"
233   owner "nominatim"
234   group "nominatim"
235   mode "664"
236   variables :base_url => node[:nominatim][:state] == "off" ? node[:fqdn] : "nominatim.openstreetmap.org",
237             :dbname => node[:nominatim][:dbname],
238             :flatnode_file => node[:nominatim][:flatnode_file],
239             :log_file => "#{node[:nominatim][:logdir]}/query.log"
240 end
241
242 git ui_directory do
243   action :sync
244   repository node[:nominatim][:ui_repository]
245   revision node[:nominatim][:ui_revision]
246   user "nominatim"
247   group "nominatim"
248 end
249
250 template "#{ui_directory}/dist/config.js" do
251   source "ui-config.js.erb"
252   owner "nominatim"
253   group "nominatim"
254   mode "664"
255 end
256
257 if node[:nominatim][:flatnode_file]
258   directory File.dirname(node[:nominatim][:flatnode_file]) do
259     recursive true
260   end
261 end
262
263 template "/etc/logrotate.d/nominatim" do
264   source "logrotate.nominatim.erb"
265   owner "root"
266   group "root"
267   mode "644"
268 end
269
270 external_data = [
271   "wikimedia-importance.sql.gz",
272   "gb_postcode_data.sql.gz"
273 ]
274
275 external_data.each do |fname|
276   remote_file "#{source_directory}/data/#{fname}" do
277     action :create_if_missing
278     source "https://www.nominatim.org/data/#{fname}"
279     owner "nominatim"
280     group "nominatim"
281     mode "644"
282   end
283 end
284
285 remote_file "#{source_directory}/data/country_osm_grid.sql.gz" do
286   action :create_if_missing
287   source "https://www.nominatim.org/data/country_grid.sql.gz"
288   owner "nominatim"
289   group "nominatim"
290   mode "644"
291 end
292
293 if node[:nominatim][:state] == "off"
294   cron_d "nominatim-backup" do
295     action :delete
296   end
297
298   cron_d "nominatim-vacuum-db" do
299     action :delete
300   end
301
302   cron_d "nominatim-clean-db" do
303     action :delete
304   end
305
306   cron_d "nominatim-update-maintenance-trigger" do
307     action :delete
308   end
309 else
310   cron_d "nominatim-backup" do
311     action node[:nominatim][:enable_backup] ? :create : :delete
312     minute "0"
313     hour "3"
314     day "1"
315     user "nominatim"
316     command "/usr/local/bin/backup-nominatim"
317     mailto email_errors
318   end
319
320   cron_d "nominatim-vacuum-db" do
321     minute "20"
322     hour "0"
323     user "postgres"
324     command "/usr/local/bin/vacuum-db-nominatim"
325     mailto email_errors
326   end
327
328   cron_d "nominatim-clean-db" do
329     action node[:nominatim][:state] == "master" ? :create : :delete
330     minute "5"
331     hour "*/4"
332     user "postgres"
333     command "/usr/local/bin/clean-db-nominatim"
334     mailto email_errors
335   end
336
337   cron_d "nominatim-update-maintenance-trigger" do
338     minute "18"
339     hour "1"
340     user "nominatim"
341     command "touch #{basedir}/status/update_maintenance"
342     mailto email_errors
343   end
344 end
345
346 template "#{source_directory}/utils/nominatim-update" do
347   source "updater.erb"
348   user "nominatim"
349   group "nominatim"
350   mode "755"
351   variables :bindir => build_directory,
352             :srcdir => source_directory,
353             :logfile => "#{node[:nominatim][:logdir]}/update.log",
354             :branch => node[:nominatim][:revision],
355             :update_stop_file => "#{basedir}/status/updates_disabled",
356             :update_maintenance_trigger => "#{basedir}/status/update_maintenance"
357 end
358
359 template "/etc/init.d/nominatim-update" do
360   source "updater.init.erb"
361   user "nominatim"
362   group "nominatim"
363   mode "755"
364   variables :source_directory => source_directory
365 end
366
367 %w[backup-nominatim vacuum-db-nominatim].each do |fname|
368   template "/usr/local/bin/#{fname}" do
369     source "#{fname}.erb"
370     owner "root"
371     group "root"
372     mode "755"
373     variables :db => node[:nominatim][:dbname]
374   end
375 end
376
377 ## webserver frontend
378
379 directory "#{basedir}/etc" do
380   owner "nominatim"
381   group "adm"
382   mode "775"
383 end
384
385 %w[user_agent referrer email generic].each do |name|
386   file "#{basedir}/etc/nginx_blocked_#{name}.conf" do
387     action :create_if_missing
388     owner "nominatim"
389     group "adm"
390     mode "664"
391   end
392 end
393
394 node[:nominatim][:fpm_pools].each do |name, data|
395   php_fpm name do
396     port data[:port]
397     pm data[:pm]
398     pm_max_children data[:max_children]
399     pm_start_servers 20
400     pm_min_spare_servers 10
401     pm_max_spare_servers 20
402     pm_max_requests 10000
403     prometheus_port data[:prometheus_port]
404   end
405 end
406
407 ssl_certificate node[:fqdn] do
408   domains [node[:fqdn],
409            "nominatim.openstreetmap.org",
410            "nominatim.osm.org",
411            "nominatim.openstreetmap.com",
412            "nominatim.openstreetmap.net",
413            "nominatim.openstreetmaps.org",
414            "nominatim.openmaps.org"]
415   notifies :reload, "service[nginx]"
416 end
417
418 package "apache2" do
419   action :remove
420 end
421
422 include_recipe "nginx"
423
424 nginx_site "default" do
425   action [:delete]
426 end
427
428 frontends = search(:node, "recipes:web\\:\\:frontend").sort_by(&:name)
429
430 nginx_site "nominatim" do
431   template "nginx.erb"
432   directory build_directory
433   variables :pools => node[:nominatim][:fpm_pools],
434             :frontends => frontends,
435             :confdir => "#{basedir}/etc",
436             :ui_directory => ui_directory
437 end
438
439 template "/etc/logrotate.d/nginx" do
440   source "logrotate.nginx.erb"
441   owner "root"
442   group "root"
443   mode "644"
444 end
445
446 munin_plugin_conf "nominatim" do
447   template "munin.erb"
448   variables :db => node[:nominatim][:dbname],
449             :querylog => "#{node[:nominatim][:logdir]}/query.log"
450 end
451
452 munin_plugin "nominatim_importlag" do
453   target "#{source_directory}/munin/nominatim_importlag"
454 end
455
456 munin_plugin "nominatim_query_speed" do
457   target "#{source_directory}/munin/nominatim_query_speed_querylog"
458 end
459
460 munin_plugin "nominatim_requests" do
461   target "#{source_directory}/munin/nominatim_requests_querylog"
462 end
463
464 directory "#{basedir}/status" do
465   owner "nominatim"
466   group "postgres"
467   mode "775"
468 end
469
470 include_recipe "fail2ban"
471
472 frontend_addresses = frontends.collect { |f| f.ipaddresses(:role => :external) }
473
474 fail2ban_jail "nominatim_limit_req" do
475   filter "nginx-limit-req"
476   logpath "#{node[:nominatim][:logdir]}/nominatim.openstreetmap.org-error.log"
477   ports [80, 443]
478   maxretry 5
479   ignoreips frontend_addresses.flatten.sort
480 end