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