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 directory "/srv/tile.openstreetmap.org/styles" do
309 node[:tile][:styles].each do |name, details|
310 style_directory = "/srv/tile.openstreetmap.org/styles/#{name}"
311 tile_directory = "/srv/tile.openstreetmap.org/tiles/#{name}"
313 template "/usr/local/bin/update-lowzoom-#{name}" do
314 source "update-lowzoom.erb"
318 variables :style => name, :threads => lowzoom_threads
321 service "update-lowzoom@#{name}" do
323 supports :restart => true
326 directory tile_directory do
332 details[:tile_directories].each do |directory|
333 directory directory[:name] do
339 directory[:min_zoom].upto(directory[:max_zoom]) do |zoom|
340 directory "#{directory[:name]}/#{zoom}" do
346 link "#{tile_directory}/#{zoom}" do
347 to "#{directory[:name]}/#{zoom}"
354 file "#{tile_directory}/planet-import-complete" do
355 action :create_if_missing
361 git style_directory do
363 repository details[:repository]
364 revision details[:revision]
369 link "#{style_directory}/data" do
370 to "/srv/tile.openstreetmap.org/data"
375 if details[:fonts_script]
376 execute details[:fonts_script] do
378 command details[:fonts_script]
382 subscribes :run, "git[#{style_directory}]"
386 execute "#{style_directory}/project.mml" do
388 command "carto -a 3.0.22 project.mml > project.xml"
392 subscribes :run, "git[#{style_directory}]"
393 notifies :restart, "service[renderd]", :immediately
394 notifies :restart, "service[update-lowzoom@#{name}]"
398 postgresql_version = node[:tile][:database][:cluster].split("/").first
399 postgis_version = node[:tile][:database][:postgis]
401 package "postgresql-#{postgresql_version}-postgis-#{postgis_version}"
403 postgresql_user "jburgess" do
404 cluster node[:tile][:database][:cluster]
408 postgresql_user "tomh" do
409 cluster node[:tile][:database][:cluster]
413 postgresql_user "pnorman" do
414 cluster node[:tile][:database][:cluster]
418 postgresql_user "tile" do
419 cluster node[:tile][:database][:cluster]
422 postgresql_user "www-data" do
423 cluster node[:tile][:database][:cluster]
426 postgresql_user "_renderd" do
427 cluster node[:tile][:database][:cluster]
430 postgresql_database "gis" do
431 cluster node[:tile][:database][:cluster]
435 postgresql_extension "postgis" do
436 cluster node[:tile][:database][:cluster]
440 postgresql_extension "hstore" do
441 cluster node[:tile][:database][:cluster]
443 only_if { node[:tile][:database][:hstore] }
446 %w[geography_columns planet_osm_nodes planet_osm_rels planet_osm_ways raster_columns raster_overviews].each do |table|
447 postgresql_table table do
448 cluster node[:tile][:database][:cluster]
451 permissions "tile" => :all
455 %w[geometry_columns planet_osm_line planet_osm_point planet_osm_polygon planet_osm_roads spatial_ref_sys].each do |table|
456 postgresql_table table do
457 cluster node[:tile][:database][:cluster]
460 permissions "tile" => :all, "www-data" => :select, "_renderd" => :select
470 if node[:tile][:database][:external_data_script]
471 execute node[:tile][:database][:external_data_script] do
472 command "#{node[:tile][:database][:external_data_script]} -R _renderd"
473 cwd "/srv/tile.openstreetmap.org"
479 Array(node[:tile][:database][:external_data_tables]).each do |table|
480 postgresql_table table do
481 cluster node[:tile][:database][:cluster]
484 permissions "tile" => :all, "www-data" => :select, "_renderd" => :select
489 directory File.dirname(node[:tile][:database][:node_file]) do
496 file node[:tile][:database][:node_file] do
502 directory "/var/log/tile" do
514 directory node[:tile][:replication][:directory] do
520 link "/var/lib/replicate" do
521 to node[:tile][:replication][:directory]
524 not_if { node[:tile][:replication][:directory] == "/var/lib/replicate" }
527 template "/usr/local/bin/expire-tiles" do
528 source "expire-tiles.erb"
534 directory "/var/lib/replicate/expire-queue" do
540 template "/usr/local/bin/replicate" do
541 source "replicate.erb"
547 systemd_service "expire-tiles" do
548 description "Tile dirtying service"
551 exec_start "/usr/local/bin/expire-tiles"
554 restrict_address_families "AF_UNIX"
555 read_write_paths tile_directories + [
556 "/var/lib/replicate/expire-queue"
560 systemd_path "expire-tiles" do
561 description "Tile dirtying trigger"
562 directory_not_empty "/var/lib/replicate/expire-queue"
565 service "expire-tiles.path" do
566 action [:enable, :start]
567 subscribes :restart, "systemd_path[expire-tiles]"
570 template "/usr/local/bin/replicate-post" do
571 source "replicate-post.erb"
577 systemd_service "replicate" do
578 description "Rendering database replication service"
579 after "postgresql.service"
580 wants "postgresql.service"
582 exec_start "/usr/local/bin/replicate"
583 sandbox :enable_network => true
584 restrict_address_families "AF_UNIX"
586 "/store/database/nodes",
592 service "replicate" do
593 action [:enable, :start]
594 subscribes :restart, "template[/usr/local/bin/replicate]"
595 subscribes :restart, "systemd_service[replicate]"
598 template "/usr/local/bin/render-lowzoom" do
599 source "render-lowzoom.erb"
603 variables :threads => lowzoom_threads
606 systemd_service "render-lowzoom" do
607 description "Render low zoom tiles"
608 condition_path_exists_glob "!/run/update-lowzoom-*"
610 exec_start "/usr/local/bin/render-lowzoom"
612 restrict_address_families "AF_UNIX"
613 read_write_paths "/var/log/tile"
616 systemd_timer "render-lowzoom" do
617 description "Render low zoom tiles"
618 on_calendar "23:00 #{node[:timezone]}"
621 service "render-lowzoom.timer" do
622 action [:enable, :start]
625 package "liblockfile-simple-perl"
626 package "libfilesys-df-perl"
628 template "/usr/local/bin/cleanup-tiles" do
629 source "cleanup-tiles.erb"
635 systemd_service "cleanup-tiles@" do
636 description "Cleanup old tiles for /%I"
637 exec_start "/usr/local/bin/cleanup-tiles /%I"
639 io_scheduling_class "idle"
641 read_write_paths "/%I"
644 systemd_timer "cleanup-tiles@" do
645 description "Cleanup old tiles for /%I"
647 on_unit_inactive_sec "60m"
648 randomized_delay_sec "10m"
651 tile_directories.each do |directory|
652 label = directory[1..].gsub("/", "-")
654 service "cleanup-tiles@#{label}.timer" do
655 action [:enable, :start]
659 package "ruby-webrick"
661 prometheus_exporter "modtile" do
665 prometheus_exporter "renderd" do