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"
167 directory "/srv/tile.openstreetmap.org/cgi-bin" do
173 template "/srv/tile.openstreetmap.org/cgi-bin/export" do
178 variables :blocks => blocks, :totp_key => web_passwords["totp_key"]
181 template "/srv/tile.openstreetmap.org/cgi-bin/debug" do
188 systemd_service "export-cleanup" do
189 description "Cleanup stale export temporary files"
190 joins_namespace_of "apache2.service"
191 exec_start "find /tmp -ignore_readdir_race -name 'export??????' -mmin +60 -delete"
196 systemd_timer "export-cleanup" do
197 description "Cleanup stale export temporary files"
199 on_unit_inactive_sec "60m"
202 service "export-cleanup.timer" do
203 action [:enable, :start]
206 directory "/srv/tile.openstreetmap.org/data" do
218 node[:tile][:data].each_value do |data|
220 file = "/srv/tile.openstreetmap.org/data/#{File.basename(url)}"
223 directory = "/srv/tile.openstreetmap.org/data/#{data[:directory]}"
225 directory directory do
231 directory = "/srv/tile.openstreetmap.org/data"
237 command "tar -zxf #{file} -C #{directory}"
241 elsif file =~ /\.tar\.bz2$/
244 command "tar -jxf #{file} -C #{directory}"
248 elsif file =~ /\.zip$/
251 command "unzip -qq -o #{file} -d #{directory}"
257 execute "#{file}_shapeindex" do
259 command "find #{directory} -type f -iname '*.shp' -print0 | xargs -0 --no-run-if-empty shapeindex --shape_files"
262 subscribes :run, "execute[#{file}]", :immediately
268 use_conditional_get true
271 action :create_if_missing
279 notifies :run, "execute[#{file}]", :immediately
280 notifies :restart, "service[renderd]"
284 nodejs_package "carto"
286 lowzoom_threads = node.cpu_cores - 1
288 systemd_service "update-lowzoom@" do
289 description "Low zoom tile update service for %i layer"
291 exec_start_pre "+/bin/systemctl stop render-lowzoom.service"
292 exec_start "/bin/bash /usr/local/bin/update-lowzoom-%i"
293 runtime_directory "update-lowzoom-%i"
295 restrict_address_families "AF_UNIX"
297 "/srv/tile.openstreetmap.org/tiles/%i",
303 postgresql_version = node[:tile][:database][:cluster].split("/").first
304 postgis_version = node[:tile][:database][:postgis]
306 package "postgresql-#{postgresql_version}-postgis-#{postgis_version}"
308 postgresql_user "jburgess" do
309 cluster node[:tile][:database][:cluster]
313 postgresql_user "tomh" do
314 cluster node[:tile][:database][:cluster]
318 postgresql_user "pnorman" do
319 cluster node[:tile][:database][:cluster]
323 postgresql_user "tile" do
324 cluster node[:tile][:database][:cluster]
327 postgresql_user "www-data" do
328 cluster node[:tile][:database][:cluster]
331 postgresql_user "_renderd" do
332 cluster node[:tile][:database][:cluster]
335 postgresql_database "gis" do
336 cluster node[:tile][:database][:cluster]
340 postgresql_extension "postgis" do
341 cluster node[:tile][:database][:cluster]
345 postgresql_extension "hstore" do
346 cluster node[:tile][:database][:cluster]
348 only_if { node[:tile][:database][:hstore] }
351 %w[geography_columns planet_osm_nodes planet_osm_rels planet_osm_ways raster_columns raster_overviews].each do |table|
352 postgresql_table table do
353 cluster node[:tile][:database][:cluster]
356 permissions "tile" => :all
360 %w[geometry_columns planet_osm_line planet_osm_point planet_osm_polygon planet_osm_roads spatial_ref_sys].each do |table|
361 postgresql_table table do
362 cluster node[:tile][:database][:cluster]
365 permissions "tile" => :all, "www-data" => :select, "_renderd" => :select
369 directory "/srv/tile.openstreetmap.org/styles" do
375 node[:tile][:styles].each do |name, details|
376 style_directory = "/srv/tile.openstreetmap.org/styles/#{name}"
377 tile_directory = "/srv/tile.openstreetmap.org/tiles/#{name}"
379 template "/usr/local/bin/update-lowzoom-#{name}" do
380 source "update-lowzoom.erb"
384 variables :style => name, :threads => lowzoom_threads
387 service "update-lowzoom@#{name}" do
389 supports :restart => true
392 directory tile_directory do
398 details[:tile_directories].each do |directory|
399 directory directory[:name] do
405 directory[:min_zoom].upto(directory[:max_zoom]) do |zoom|
406 directory "#{directory[:name]}/#{zoom}" do
412 link "#{tile_directory}/#{zoom}" do
413 to "#{directory[:name]}/#{zoom}"
420 file "#{tile_directory}/planet-import-complete" do
421 action :create_if_missing
427 git style_directory do
429 repository details[:repository]
430 revision details[:revision]
435 link "#{style_directory}/data" do
436 to "/srv/tile.openstreetmap.org/data"
441 if details[:fonts_script]
442 execute details[:fonts_script] do
444 command details[:fonts_script]
448 subscribes :run, "git[#{style_directory}]"
452 if details[:functions_script]
453 postgresql_execute details[:functions_script] do
455 command details[:functions_script]
456 cluster node[:tile][:database][:cluster]
460 subscribes :run, "git[#{style_directory}]"
464 execute "#{style_directory}/project.mml" do
466 command "carto -a 3.0.22 project.mml > project.xml"
470 subscribes :run, "git[#{style_directory}]"
471 notifies :restart, "service[renderd]", :immediately
472 notifies :restart, "service[update-lowzoom@#{name}]"
482 if node[:tile][:database][:external_data_script]
483 execute node[:tile][:database][:external_data_script] do
484 command "#{node[:tile][:database][:external_data_script]} -R _renderd"
485 cwd "/srv/tile.openstreetmap.org"
491 Array(node[:tile][:database][:external_data_tables]).each do |table|
492 postgresql_table table do
493 cluster node[:tile][:database][:cluster]
496 permissions "tile" => :all, "www-data" => :select, "_renderd" => :select
501 directory File.dirname(node[:tile][:database][:node_file]) do
508 file node[:tile][:database][:node_file] do
514 directory "/var/log/tile" do
526 directory node[:tile][:replication][:directory] do
532 link "/var/lib/replicate" do
533 to node[:tile][:replication][:directory]
536 not_if { node[:tile][:replication][:directory] == "/var/lib/replicate" }
539 template "/usr/local/bin/expire-tiles" do
540 source "expire-tiles.erb"
546 directory "/var/lib/replicate/expire-queue" do
552 template "/usr/local/bin/replicate" do
553 source "replicate.erb"
559 systemd_service "expire-tiles" do
560 description "Tile dirtying service"
563 exec_start "/usr/local/bin/expire-tiles"
566 restrict_address_families "AF_UNIX"
567 read_write_paths tile_directories + [
568 "/var/lib/replicate/expire-queue"
572 systemd_path "expire-tiles" do
573 description "Tile dirtying trigger"
574 directory_not_empty "/var/lib/replicate/expire-queue"
577 service "expire-tiles.path" do
578 action [:enable, :start]
579 subscribes :restart, "systemd_path[expire-tiles]"
582 template "/usr/local/bin/replicate-post" do
583 source "replicate-post.erb"
589 systemd_service "replicate" do
590 description "Rendering database replication service"
591 after "postgresql.service"
592 wants "postgresql.service"
594 exec_start "/usr/local/bin/replicate"
595 sandbox :enable_network => true
596 restrict_address_families "AF_UNIX"
598 "/store/database/nodes",
604 service "replicate" do
605 action [:enable, :start]
606 subscribes :restart, "template[/usr/local/bin/replicate]"
607 subscribes :restart, "systemd_service[replicate]"
610 template "/usr/local/bin/render-lowzoom" do
611 source "render-lowzoom.erb"
615 variables :threads => lowzoom_threads
618 systemd_service "render-lowzoom" do
619 description "Render low zoom tiles"
620 condition_path_exists_glob "!/run/update-lowzoom-*"
622 exec_start "/usr/local/bin/render-lowzoom"
624 restrict_address_families "AF_UNIX"
625 read_write_paths "/var/log/tile"
628 systemd_timer "render-lowzoom" do
629 description "Render low zoom tiles"
630 on_calendar "23:00 #{node[:timezone]}"
633 service "render-lowzoom.timer" do
634 action [:enable, :start]
637 package "liblockfile-simple-perl"
638 package "libfilesys-df-perl"
640 template "/usr/local/bin/cleanup-tiles" do
641 source "cleanup-tiles.erb"
647 systemd_service "cleanup-tiles@" do
648 description "Cleanup old tiles for /%I"
649 exec_start "/usr/local/bin/cleanup-tiles /%I"
651 io_scheduling_class "idle"
653 read_write_paths "/%I"
656 systemd_timer "cleanup-tiles@" do
657 description "Cleanup old tiles for /%I"
659 on_unit_inactive_sec "60m"
660 randomized_delay_sec "10m"
663 tile_directories.each do |directory|
664 label = directory[1..].gsub("/", "-")
666 service "cleanup-tiles@#{label}.timer" do
667 action [:enable, :start]
671 package "ruby-webrick"
673 prometheus_exporter "modtile" do
677 prometheus_exporter "renderd" do