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