5 # Copyright:: 2013, OpenStreetMap Foundation
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
11 # https://www.apache.org/licenses/LICENSE-2.0
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.
20 include_recipe "accounts"
21 include_recipe "apache"
23 include_recipe "nodejs"
24 include_recipe "postgresql"
25 include_recipe "prometheus"
26 include_recipe "python"
28 include_recipe "tools"
30 blocks = data_bag_item("tile", "blocks")
31 admins = data_bag_item("apache", "admins")
32 web_passwords = data_bag_item("web", "passwords")
36 apache_module "expires"
37 apache_module "headers"
38 apache_module "remoteip"
39 apache_module "rewrite"
41 apache_module "tile" do
45 apache_conf "renderd" do
49 ssl_certificate node[:fqdn] do
50 domains [node[:fqdn], "tile.openstreetmap.org", "render.openstreetmap.org"]
51 notifies :reload, "service[apache2]"
54 remote_file "#{Chef::Config[:file_cache_path]}/fastly-ip-list.json" do
55 source "https://api.fastly.com/public-ip-list"
60 fastlyips = JSON.parse(IO.read("#{Chef::Config[:file_cache_path]}/fastly-ip-list.json"))
62 remote_file "#{Chef::Config[:file_cache_path]}/statuscake-locations.json" do
63 source "https://app.statuscake.com/Workfloor/Locations.php?format=json"
68 statuscakelocations = JSON.parse(IO.read("#{Chef::Config[:file_cache_path]}/statuscake-locations.json"))
70 apache_site "default" do
74 apache_site "tileserver_site" do
78 apache_site "tile.openstreetmap.org" do
80 variables :fastly => fastlyips["addresses"] + fastlyips["ipv6_addresses"],
81 :statuscake => statuscakelocations.flat_map { |_, v| [v["ip"], v["ipv6"]] },
82 :admins => admins["hosts"]
85 template "/etc/logrotate.d/apache2" do
86 source "logrotate.apache.erb"
92 directory "/srv/tile.openstreetmap.org" do
98 tile_directories = node[:tile][:styles].collect do |_, style|
99 style[:tile_directories].collect { |directory| directory[:name] }
100 end.flatten.sort.uniq
107 systemd_service "renderd" do
109 after "postgresql.service"
110 wants "postgresql.service"
111 environment "LD_PRELOAD" => "libtcmalloc.so.4"
116 restrict_address_families "AF_UNIX"
117 read_write_paths tile_directories
118 system_call_filter ["@system-service", "mincore"]
123 action [:enable, :start]
124 subscribes :restart, "systemd_service[renderd]"
127 directory "/srv/tile.openstreetmap.org/tiles" do
133 template "/etc/renderd.conf" do
134 source "renderd.conf.erb"
138 notifies :reload, "service[apache2]"
139 notifies :restart, "service[renderd]"
142 remote_directory "/srv/tile.openstreetmap.org/html" do
152 template "/srv/tile.openstreetmap.org/html/index.html" do
153 source "index.html.erb"
166 python_package "pyotp" do
170 directory "/srv/tile.openstreetmap.org/cgi-bin" do
176 template "/srv/tile.openstreetmap.org/cgi-bin/export" do
181 variables :blocks => blocks, :totp_key => web_passwords["totp_key"]
184 template "/srv/tile.openstreetmap.org/cgi-bin/debug" do
191 systemd_service "export-cleanup" do
192 description "Cleanup stale export temporary files"
193 joins_namespace_of "apache2.service"
194 exec_start "find /tmp -ignore_readdir_race -name 'export??????' -mmin +60 -delete"
199 systemd_timer "export-cleanup" do
200 description "Cleanup stale export temporary files"
202 on_unit_inactive_sec "60m"
205 service "export-cleanup.timer" do
206 action [:enable, :start]
209 directory "/srv/tile.openstreetmap.org/data" do
221 node[:tile][:data].each_value do |data|
223 file = "/srv/tile.openstreetmap.org/data/#{File.basename(url)}"
226 directory = "/srv/tile.openstreetmap.org/data/#{data[:directory]}"
228 directory directory do
234 directory = "/srv/tile.openstreetmap.org/data"
240 command "tar -zxf #{file} -C #{directory}"
244 elsif file =~ /\.tar\.bz2$/
247 command "tar -jxf #{file} -C #{directory}"
251 elsif file =~ /\.zip$/
254 command "unzip -qq -o #{file} -d #{directory}"
260 execute "#{file}_shapeindex" do
262 command "find #{directory} -type f -iname '*.shp' -print0 | xargs -0 --no-run-if-empty shapeindex --shape_files"
265 subscribes :run, "execute[#{file}]", :immediately
271 use_conditional_get true
274 action :create_if_missing
282 notifies :run, "execute[#{file}]", :immediately
283 notifies :restart, "service[renderd]"
287 nodejs_package "carto"
289 lowzoom_threads = node.cpu_cores - 1
291 systemd_service "update-lowzoom@" do
292 description "Low zoom tile update service for %i layer"
294 exec_start_pre "+/bin/systemctl stop render-lowzoom.service"
295 exec_start "/bin/bash /usr/local/bin/update-lowzoom-%i"
296 runtime_directory "update-lowzoom-%i"
298 restrict_address_families "AF_UNIX"
300 "/srv/tile.openstreetmap.org/tiles/%i",
306 directory "/srv/tile.openstreetmap.org/styles" do
312 node[:tile][:styles].each do |name, details|
313 style_directory = "/srv/tile.openstreetmap.org/styles/#{name}"
314 tile_directory = "/srv/tile.openstreetmap.org/tiles/#{name}"
316 template "/usr/local/bin/update-lowzoom-#{name}" do
317 source "update-lowzoom.erb"
321 variables :style => name, :threads => lowzoom_threads
324 service "update-lowzoom@#{name}" do
326 supports :restart => true
329 directory tile_directory do
335 details[:tile_directories].each do |directory|
336 directory directory[:name] do
342 directory[:min_zoom].upto(directory[:max_zoom]) do |zoom|
343 directory "#{directory[:name]}/#{zoom}" do
349 link "#{tile_directory}/#{zoom}" do
350 to "#{directory[:name]}/#{zoom}"
357 file "#{tile_directory}/planet-import-complete" do
358 action :create_if_missing
364 git style_directory do
366 repository details[:repository]
367 revision details[:revision]
372 link "#{style_directory}/data" do
373 to "/srv/tile.openstreetmap.org/data"
378 if details[:fonts_script]
379 execute details[:fonts_script] do
381 command details[:fonts_script]
385 subscribes :run, "git[#{style_directory}]"
389 execute "#{style_directory}/project.mml" do
391 command "carto -a 3.0.22 project.mml > project.xml"
395 subscribes :run, "git[#{style_directory}]"
396 notifies :restart, "service[renderd]", :immediately
397 notifies :restart, "service[update-lowzoom@#{name}]"
401 postgresql_version = node[:tile][:database][:cluster].split("/").first
402 postgis_version = node[:tile][:database][:postgis]
404 package "postgresql-#{postgresql_version}-postgis-#{postgis_version}"
406 postgresql_user "jburgess" do
407 cluster node[:tile][:database][:cluster]
411 postgresql_user "tomh" do
412 cluster node[:tile][:database][:cluster]
416 postgresql_user "pnorman" do
417 cluster node[:tile][:database][:cluster]
421 postgresql_user "tile" do
422 cluster node[:tile][:database][:cluster]
425 postgresql_user "www-data" do
426 cluster node[:tile][:database][:cluster]
429 postgresql_user "_renderd" do
430 cluster node[:tile][:database][:cluster]
433 postgresql_database "gis" do
434 cluster node[:tile][:database][:cluster]
438 postgresql_extension "postgis" do
439 cluster node[:tile][:database][:cluster]
443 postgresql_extension "hstore" do
444 cluster node[:tile][:database][:cluster]
446 only_if { node[:tile][:database][:hstore] }
449 %w[geography_columns planet_osm_nodes planet_osm_rels planet_osm_ways raster_columns raster_overviews].each do |table|
450 postgresql_table table do
451 cluster node[:tile][:database][:cluster]
454 permissions "tile" => :all
458 %w[geometry_columns planet_osm_line planet_osm_point planet_osm_polygon planet_osm_roads spatial_ref_sys].each do |table|
459 postgresql_table table do
460 cluster node[:tile][:database][:cluster]
463 permissions "tile" => :all, "www-data" => :select, "_renderd" => :select
473 if node[:tile][:database][:external_data_script]
474 execute node[:tile][:database][:external_data_script] do
475 command "#{node[:tile][:database][:external_data_script]} -R _renderd"
476 cwd "/srv/tile.openstreetmap.org"
482 Array(node[:tile][:database][:external_data_tables]).each do |table|
483 postgresql_table table do
484 cluster node[:tile][:database][:cluster]
487 permissions "tile" => :all, "www-data" => :select, "_renderd" => :select
492 directory File.dirname(node[:tile][:database][:node_file]) do
499 file node[:tile][:database][:node_file] do
505 directory "/var/log/tile" do
517 directory "/var/lib/replicate" do
523 template "/usr/local/bin/expire-tiles" do
524 source "expire-tiles.erb"
530 directory "/var/lib/replicate/expire-queue" do
536 template "/usr/local/bin/replicate" do
537 source "replicate.erb"
543 systemd_service "expire-tiles" do
544 description "Tile dirtying service"
547 exec_start "/usr/local/bin/expire-tiles"
550 restrict_address_families "AF_UNIX"
551 read_write_paths tile_directories + [
552 "/var/lib/replicate/expire-queue"
556 systemd_path "expire-tiles" do
557 description "Tile dirtying trigger"
558 directory_not_empty "/var/lib/replicate/expire-queue"
561 service "expire-tiles.path" do
562 action [:enable, :start]
563 subscribes :restart, "systemd_path[expire-tiles]"
566 template "/usr/local/bin/replicate-post" do
567 source "replicate-post.erb"
573 osm2pgsql_arguments = %w[
577 --expire-output=/var/lib/replicate/dirty-tiles.txt
580 osm2pgsql_arguments.append("--multi-geometry") if node[:tile][:database][:multi_geometry]
581 osm2pgsql_arguments.append("--hstore") if node[:tile][:database][:hstore]
582 osm2pgsql_arguments.append("--tag-transform-script=#{node[:tile][:database][:tag_transform_script]}") if node[:tile][:database][:tag_transform_script]
584 systemd_service "replicate" do
585 description "Rendering database replication service"
586 after "postgresql.service"
587 wants "postgresql.service"
589 exec_start "/usr/local/bin/replicate"
590 sandbox :enable_network => true
591 restrict_address_families "AF_UNIX"
593 "/store/database/nodes",
599 service "replicate" do
600 action [:enable, :start]
601 subscribes :restart, "template[/usr/local/bin/replicate]"
602 subscribes :restart, "systemd_service[replicate]"
605 template "/usr/local/bin/render-lowzoom" do
606 source "render-lowzoom.erb"
610 variables :threads => lowzoom_threads
613 systemd_service "render-lowzoom" do
614 description "Render low zoom tiles"
615 condition_path_exists_glob "!/run/update-lowzoom-*"
617 exec_start "/usr/local/bin/render-lowzoom"
619 restrict_address_families "AF_UNIX"
620 read_write_paths "/var/log/tile"
623 systemd_timer "render-lowzoom" do
624 description "Render low zoom tiles"
625 on_calendar "23:00 #{node[:timezone]}"
628 service "render-lowzoom.timer" do
629 action [:enable, :start]
632 package "liblockfile-simple-perl"
633 package "libfilesys-df-perl"
635 template "/usr/local/bin/cleanup-tiles" do
636 source "cleanup-tiles.erb"
642 systemd_service "cleanup-tiles@" do
643 description "Cleanup old tiles for /%I"
644 exec_start "/usr/local/bin/cleanup-tiles /%I"
646 io_scheduling_class "idle"
648 read_write_paths "/%I"
651 systemd_timer "cleanup-tiles@" do
652 description "Cleanup old tiles for /%I"
654 on_unit_inactive_sec "60m"
655 randomized_delay_sec "10m"
658 tile_directories.each do |directory|
659 label = directory[1..].gsub("/", "-")
661 service "cleanup-tiles@#{label}.timer" do
662 action [:enable, :start]
666 package "ruby-webrick"
668 prometheus_exporter "modtile" do
672 prometheus_exporter "renderd" do