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"
64 # FIXME: StatusCake IP lookup. Use manual backup source for now. 13 Nov 2024
65 # https://www.statuscake.com/kb/knowledge-base/what-are-your-ips/
66 source "https://grant.dev.openstreetmap.org/statuscake-locations-patch.json"
71 statuscakelocations = JSON.parse(IO.read("#{Chef::Config[:file_cache_path]}/statuscake-locations.json"))
73 apache_site "default" do
77 apache_site "tileserver_site" do
81 apache_site "tile.openstreetmap.org" do
83 variables :fastly => fastlyips["addresses"] + fastlyips["ipv6_addresses"],
84 :statuscake => statuscakelocations.flat_map { |_, v| [v["ip"], v["ipv6"]] },
85 :admins => admins["hosts"]
88 template "/etc/logrotate.d/apache2" do
89 source "logrotate.apache.erb"
95 directory "/srv/tile.openstreetmap.org" do
101 tile_directories = node[:tile][:styles].collect do |_, style|
102 style[:tile_directories].collect { |directory| directory[:name] }
103 end.flatten.sort.uniq
110 systemd_service "renderd" do
112 after "postgresql.service"
113 wants "postgresql.service"
114 environment "LD_PRELOAD" => "libtcmalloc.so.4"
119 restrict_address_families "AF_UNIX"
120 read_write_paths tile_directories
121 system_call_filter ["@system-service", "mincore"]
126 action [:enable, :start]
127 subscribes :restart, "systemd_service[renderd]"
130 directory "/srv/tile.openstreetmap.org/tiles" do
136 template "/etc/renderd.conf" do
137 source "renderd.conf.erb"
141 notifies :reload, "service[apache2]"
142 notifies :restart, "service[renderd]"
145 remote_directory "/srv/tile.openstreetmap.org/html" do
155 template "/srv/tile.openstreetmap.org/html/index.html" do
156 source "index.html.erb"
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 postgresql_version = node[:tile][:database][:cluster].split("/").first
307 postgis_version = node[:tile][:database][:postgis]
309 package "postgresql-#{postgresql_version}-postgis-#{postgis_version}"
311 postgresql_user "jburgess" do
312 cluster node[:tile][:database][:cluster]
316 postgresql_user "tomh" do
317 cluster node[:tile][:database][:cluster]
321 postgresql_user "pnorman" do
322 cluster node[:tile][:database][:cluster]
326 postgresql_user "tile" do
327 cluster node[:tile][:database][:cluster]
330 postgresql_user "www-data" do
331 cluster node[:tile][:database][:cluster]
334 postgresql_user "_renderd" do
335 cluster node[:tile][:database][:cluster]
338 postgresql_database "gis" do
339 cluster node[:tile][:database][:cluster]
343 postgresql_extension "postgis" do
344 cluster node[:tile][:database][:cluster]
348 postgresql_extension "hstore" do
349 cluster node[:tile][:database][:cluster]
351 only_if { node[:tile][:database][:hstore] }
354 %w[geography_columns planet_osm_nodes planet_osm_rels planet_osm_ways raster_columns raster_overviews].each do |table|
355 postgresql_table table do
356 cluster node[:tile][:database][:cluster]
359 permissions "tile" => :all
363 %w[geometry_columns planet_osm_line planet_osm_point planet_osm_polygon planet_osm_roads spatial_ref_sys].each do |table|
364 postgresql_table table do
365 cluster node[:tile][:database][:cluster]
368 permissions "tile" => :all, "www-data" => :select, "_renderd" => :select
372 directory "/srv/tile.openstreetmap.org/styles" do
378 node[:tile][:styles].each do |name, details|
379 style_directory = "/srv/tile.openstreetmap.org/styles/#{name}"
380 tile_directory = "/srv/tile.openstreetmap.org/tiles/#{name}"
382 template "/usr/local/bin/update-lowzoom-#{name}" do
383 source "update-lowzoom.erb"
387 variables :style => name, :threads => lowzoom_threads
390 service "update-lowzoom@#{name}" do
392 supports :restart => true
395 directory tile_directory do
401 details[:tile_directories].each do |directory|
402 directory directory[:name] do
408 directory[:min_zoom].upto(directory[:max_zoom]) do |zoom|
409 directory "#{directory[:name]}/#{zoom}" do
415 link "#{tile_directory}/#{zoom}" do
416 to "#{directory[:name]}/#{zoom}"
423 file "#{tile_directory}/planet-import-complete" do
424 action :create_if_missing
430 git style_directory do
432 repository details[:repository]
433 revision details[:revision]
438 link "#{style_directory}/data" do
439 to "/srv/tile.openstreetmap.org/data"
444 if details[:fonts_script]
445 execute details[:fonts_script] do
447 command details[:fonts_script]
451 subscribes :run, "git[#{style_directory}]"
455 if details[:functions_script]
456 postgresql_execute details[:functions_script] do
458 command details[:functions_script]
459 cluster node[:tile][:database][:cluster]
463 subscribes :run, "git[#{style_directory}]"
467 execute "#{style_directory}/project.mml" do
469 command "carto -a 3.0.22 project.mml > project.xml"
473 subscribes :run, "git[#{style_directory}]"
474 notifies :restart, "service[renderd]", :immediately
475 notifies :restart, "service[update-lowzoom@#{name}]"
485 if node[:tile][:database][:external_data_script]
486 execute node[:tile][:database][:external_data_script] do
487 command "#{node[:tile][:database][:external_data_script]} -R _renderd"
488 cwd "/srv/tile.openstreetmap.org"
494 Array(node[:tile][:database][:external_data_tables]).each do |table|
495 postgresql_table table do
496 cluster node[:tile][:database][:cluster]
499 permissions "tile" => :all, "www-data" => :select, "_renderd" => :select
504 directory File.dirname(node[:tile][:database][:node_file]) do
511 file node[:tile][:database][:node_file] do
517 directory "/var/log/tile" do
529 directory node[:tile][:replication][:directory] do
535 link "/var/lib/replicate" do
536 to node[:tile][:replication][:directory]
539 not_if { node[:tile][:replication][:directory] == "/var/lib/replicate" }
542 template "/usr/local/bin/expire-tiles" do
543 source "expire-tiles.erb"
549 directory "/var/lib/replicate/expire-queue" do
555 template "/usr/local/bin/replicate" do
556 source "replicate.erb"
562 systemd_service "expire-tiles" do
563 description "Tile dirtying service"
566 exec_start "/usr/local/bin/expire-tiles"
569 restrict_address_families "AF_UNIX"
570 read_write_paths tile_directories + [
571 "/var/lib/replicate/expire-queue"
575 systemd_path "expire-tiles" do
576 description "Tile dirtying trigger"
577 directory_not_empty "/var/lib/replicate/expire-queue"
580 service "expire-tiles.path" do
581 action [:enable, :start]
582 subscribes :restart, "systemd_path[expire-tiles]"
585 template "/usr/local/bin/replicate-post" do
586 source "replicate-post.erb"
592 systemd_service "replicate" do
593 description "Rendering database replication service"
594 after "postgresql.service"
595 wants "postgresql.service"
597 exec_start "/usr/local/bin/replicate"
598 sandbox :enable_network => true
599 restrict_address_families "AF_UNIX"
601 "/store/database/nodes",
607 service "replicate" do
608 action [:enable, :start]
609 subscribes :restart, "template[/usr/local/bin/replicate]"
610 subscribes :restart, "systemd_service[replicate]"
613 template "/usr/local/bin/render-lowzoom" do
614 source "render-lowzoom.erb"
618 variables :threads => lowzoom_threads
621 systemd_service "render-lowzoom" do
622 description "Render low zoom tiles"
623 condition_path_exists_glob "!/run/update-lowzoom-*"
625 exec_start "/usr/local/bin/render-lowzoom"
627 restrict_address_families "AF_UNIX"
628 read_write_paths "/var/log/tile"
631 systemd_timer "render-lowzoom" do
632 description "Render low zoom tiles"
633 on_calendar "23:00 #{node[:timezone]}"
636 service "render-lowzoom.timer" do
637 action [:enable, :start]
640 package "liblockfile-simple-perl"
641 package "libfilesys-df-perl"
643 template "/usr/local/bin/cleanup-tiles" do
644 source "cleanup-tiles.erb"
650 systemd_service "cleanup-tiles@" do
651 description "Cleanup old tiles for /%I"
652 exec_start "/usr/local/bin/cleanup-tiles /%I"
654 io_scheduling_class "idle"
656 read_write_paths "/%I"
659 systemd_timer "cleanup-tiles@" do
660 description "Cleanup old tiles for /%I"
662 on_unit_inactive_sec "60m"
663 randomized_delay_sec "10m"
666 tile_directories.each do |directory|
667 label = directory[1..].gsub("/", "-")
669 service "cleanup-tiles@#{label}.timer" do
670 action [:enable, :start]
674 package "ruby-webrick"
676 prometheus_exporter "modtile" do
680 prometheus_exporter "renderd" do