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