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 "munin"
24 include_recipe "nodejs"
25 include_recipe "postgresql"
26 include_recipe "prometheus"
27 include_recipe "python"
29 include_recipe "tools"
31 blocks = data_bag_item("tile", "blocks")
32 admins = data_bag_item("apache", "admins")
33 web_passwords = data_bag_item("web", "passwords")
37 apache_module "expires"
38 apache_module "headers"
39 apache_module "remoteip"
40 apache_module "rewrite"
42 apache_module "tile" do
46 apache_conf "renderd" do
50 ssl_certificate node[:fqdn] do
51 domains [node[:fqdn], "tile.openstreetmap.org", "render.openstreetmap.org"]
52 notifies :reload, "service[apache2]"
55 remote_file "#{Chef::Config[:file_cache_path]}/fastly-ip-list.json" do
56 source "https://api.fastly.com/public-ip-list"
61 fastlyips = JSON.parse(IO.read("#{Chef::Config[:file_cache_path]}/fastly-ip-list.json"))
63 remote_file "#{Chef::Config[:file_cache_path]}/statuscake-locations.json" do
64 source "https://app.statuscake.com/Workfloor/Locations.php?format=json"
69 statuscakelocations = JSON.parse(IO.read("#{Chef::Config[:file_cache_path]}/statuscake-locations.json"))
71 apache_site "default" do
75 apache_site "tileserver_site" do
79 apache_site "tile.openstreetmap.org" do
81 variables :fastly => fastlyips["addresses"] + fastlyips["ipv6_addresses"],
82 :statuscake => statuscakelocations.flat_map { |_, v| [v["ip"], v["ipv6"]] },
83 :admins => admins["hosts"]
86 template "/etc/logrotate.d/apache2" do
87 source "logrotate.apache.erb"
93 directory "/srv/tile.openstreetmap.org" do
99 tile_directories = node[:tile][:styles].collect do |_, style|
100 style[:tile_directories].collect { |directory| directory[:name] }
101 end.flatten.sort.uniq
105 systemd_service "renderd" do
107 after "postgresql.service"
108 wants "postgresql.service"
113 restrict_address_families "AF_UNIX"
114 read_write_paths tile_directories
119 action [:enable, :start]
120 subscribes :restart, "systemd_service[renderd]"
123 directory "/srv/tile.openstreetmap.org/tiles" do
129 template "/etc/renderd.conf" do
130 source "renderd.conf.erb"
134 notifies :reload, "service[apache2]"
135 notifies :restart, "service[renderd]"
138 remote_directory "/srv/tile.openstreetmap.org/html" do
148 template "/srv/tile.openstreetmap.org/html/index.html" do
149 source "index.html.erb"
162 python_package "pyotp" do
166 directory "/srv/tile.openstreetmap.org/cgi-bin" do
172 template "/srv/tile.openstreetmap.org/cgi-bin/export" do
177 variables :blocks => blocks, :totp_key => web_passwords["totp_key"]
180 template "/srv/tile.openstreetmap.org/cgi-bin/debug" do
187 systemd_service "export-cleanup" do
188 description "Cleanup stale export temporary files"
189 joins_namespace_of "apache2.service"
190 exec_start "find /tmp -ignore_readdir_race -name 'export??????' -mmin +60 -delete"
195 systemd_timer "export-cleanup" do
196 description "Cleanup stale export temporary files"
198 on_unit_inactive_sec "60m"
201 service "export-cleanup.timer" do
202 action [:enable, :start]
205 directory "/srv/tile.openstreetmap.org/data" do
217 node[:tile][:data].each_value do |data|
219 file = "/srv/tile.openstreetmap.org/data/#{File.basename(url)}"
222 directory = "/srv/tile.openstreetmap.org/data/#{data[:directory]}"
224 directory directory do
230 directory = "/srv/tile.openstreetmap.org/data"
236 command "tar -zxf #{file} -C #{directory}"
240 elsif file =~ /\.tar\.bz2$/
243 command "tar -jxf #{file} -C #{directory}"
247 elsif file =~ /\.zip$/
250 command "unzip -qq -o #{file} -d #{directory}"
256 execute "#{file}_shapeindex" do
258 command "find #{directory} -type f -iname '*.shp' -print0 | xargs -0 --no-run-if-empty shapeindex --shape_files"
261 subscribes :run, "execute[#{file}]", :immediately
267 use_conditional_get true
270 action :create_if_missing
278 notifies :run, "execute[#{file}]", :immediately
279 notifies :restart, "service[renderd]"
283 nodejs_package "carto"
285 lowzoom_threads = [node.cpu_cores - 1, node[:memory][:total].to_f / 6291456].min.floor
287 systemd_service "update-lowzoom@" do
288 description "Low zoom tile update service for %i layer"
290 exec_start_pre "+/bin/systemctl stop render-lowzoom.service"
291 exec_start "/bin/bash /usr/local/bin/update-lowzoom-%i"
292 runtime_directory "update-lowzoom-%i"
294 restrict_address_families "AF_UNIX"
296 "/srv/tile.openstreetmap.org/tiles/%i",
302 directory "/srv/tile.openstreetmap.org/styles" do
308 node[:tile][:styles].each do |name, details|
309 style_directory = "/srv/tile.openstreetmap.org/styles/#{name}"
310 tile_directory = "/srv/tile.openstreetmap.org/tiles/#{name}"
312 template "/usr/local/bin/update-lowzoom-#{name}" do
313 source "update-lowzoom.erb"
317 variables :style => name, :threads => lowzoom_threads
320 service "update-lowzoom@#{name}" do
322 supports :restart => true
325 directory tile_directory do
331 details[:tile_directories].each do |directory|
332 directory directory[:name] do
338 directory[:min_zoom].upto(directory[:max_zoom]) do |zoom|
339 directory "#{directory[:name]}/#{zoom}" do
345 link "#{tile_directory}/#{zoom}" do
346 to "#{directory[:name]}/#{zoom}"
353 file "#{tile_directory}/planet-import-complete" do
354 action :create_if_missing
360 git style_directory do
362 repository details[:repository]
363 revision details[:revision]
368 link "#{style_directory}/data" do
369 to "/srv/tile.openstreetmap.org/data"
374 if details[:fonts_script]
375 execute details[:fonts_script] do
377 command details[:fonts_script]
381 subscribes :run, "git[#{style_directory}]"
385 execute "#{style_directory}/project.mml" do
387 command "carto -a 3.0.22 project.mml > project.xml"
391 subscribes :run, "git[#{style_directory}]"
392 notifies :restart, "service[renderd]", :immediately
393 notifies :restart, "service[update-lowzoom@#{name}]"
397 postgresql_version = node[:tile][:database][:cluster].split("/").first
398 postgis_version = node[:tile][:database][:postgis]
400 package "postgresql-#{postgresql_version}-postgis-#{postgis_version}"
402 postgresql_user "jburgess" do
403 cluster node[:tile][:database][:cluster]
407 postgresql_user "tomh" do
408 cluster node[:tile][:database][:cluster]
412 postgresql_user "pnorman" do
413 cluster node[:tile][:database][:cluster]
417 postgresql_user "tile" do
418 cluster node[:tile][:database][:cluster]
421 postgresql_user "www-data" do
422 cluster node[:tile][:database][:cluster]
425 postgresql_user "_renderd" do
426 cluster node[:tile][:database][:cluster]
429 postgresql_database "gis" do
430 cluster node[:tile][:database][:cluster]
434 postgresql_extension "postgis" do
435 cluster node[:tile][:database][:cluster]
439 postgresql_extension "hstore" do
440 cluster node[:tile][:database][:cluster]
442 only_if { node[:tile][:database][:hstore] }
445 %w[geography_columns planet_osm_nodes planet_osm_rels planet_osm_ways raster_columns raster_overviews].each do |table|
446 postgresql_table table do
447 cluster node[:tile][:database][:cluster]
450 permissions "tile" => :all
454 %w[geometry_columns planet_osm_line planet_osm_point planet_osm_polygon planet_osm_roads spatial_ref_sys].each do |table|
455 postgresql_table table do
456 cluster node[:tile][:database][:cluster]
459 permissions "tile" => :all, "www-data" => :select, "_renderd" => :select
469 if node[:tile][:database][:external_data_script]
470 execute node[:tile][:database][:external_data_script] do
471 command "#{node[:tile][:database][:external_data_script]} -R _renderd"
472 cwd "/srv/tile.openstreetmap.org"
478 Array(node[:tile][:database][:external_data_tables]).each do |table|
479 postgresql_table table do
480 cluster node[:tile][:database][:cluster]
483 permissions "tile" => :all, "www-data" => :select, "_renderd" => :select
488 postgresql_munin "gis" do
489 cluster node[:tile][:database][:cluster]
493 directory File.dirname(node[:tile][:database][:node_file]) do
500 file node[:tile][:database][:node_file] do
506 directory "/var/log/tile" do
518 directory "/var/lib/replicate" do
524 template "/usr/local/bin/expire-tiles" do
525 source "expire-tiles.erb"
531 directory "/var/lib/replicate/expire-queue" do
537 template "/usr/local/bin/replicate" do
538 source "replicate.erb"
544 systemd_service "expire-tiles" do
545 description "Tile dirtying service"
548 exec_start "/usr/local/bin/expire-tiles"
551 restrict_address_families "AF_UNIX"
552 read_write_paths tile_directories + [
553 "/var/lib/replicate/expire-queue"
557 systemd_path "expire-tiles" do
558 description "Tile dirtying trigger"
559 directory_not_empty "/var/lib/replicate/expire-queue"
562 service "expire-tiles.path" do
563 action [:enable, :start]
564 subscribes :restart, "systemd_path[expire-tiles]"
567 template "/usr/local/bin/replicate-post" do
568 source "replicate-post.erb"
574 osm2pgsql_arguments = %w[
578 --expire-output=/var/lib/replicate/dirty-tiles.txt
581 osm2pgsql_arguments.append("--multi-geometry") if node[:tile][:database][:multi_geometry]
582 osm2pgsql_arguments.append("--hstore") if node[:tile][:database][:hstore]
583 osm2pgsql_arguments.append("--tag-transform-script=#{node[:tile][:database][:tag_transform_script]}") if node[:tile][:database][:tag_transform_script]
585 systemd_service "replicate" do
586 description "Rendering database replication service"
587 after "postgresql.service"
588 wants "postgresql.service"
590 exec_start "/usr/local/bin/replicate"
591 sandbox :enable_network => true
592 restrict_address_families "AF_UNIX"
594 "/store/database/nodes",
600 service "replicate" do
601 action [:enable, :start]
602 subscribes :restart, "template[/usr/local/bin/replicate]"
603 subscribes :restart, "systemd_service[replicate]"
606 template "/usr/local/bin/render-lowzoom" do
607 source "render-lowzoom.erb"
611 variables :threads => lowzoom_threads
614 systemd_service "render-lowzoom" do
615 description "Render low zoom tiles"
616 condition_path_exists_glob "!/run/update-lowzoom-*"
618 exec_start "/usr/local/bin/render-lowzoom"
620 restrict_address_families "AF_UNIX"
621 read_write_paths "/var/log/tile"
624 systemd_timer "render-lowzoom" do
625 description "Render low zoom tiles"
626 on_calendar "Fri *-*-* 23:00:00 UTC"
629 service "render-lowzoom.timer" do
630 action [:enable, :start]
633 package "liblockfile-simple-perl"
634 package "libfilesys-df-perl"
636 template "/usr/local/bin/cleanup-tiles" do
637 source "cleanup-tiles.erb"
643 systemd_service "cleanup-tiles@" do
644 description "Cleanup old tiles for /%I"
645 exec_start "/usr/local/bin/cleanup-tiles /%I"
647 io_scheduling_class "idle"
649 read_write_paths "/%I"
652 systemd_timer "cleanup-tiles@" do
653 description "Cleanup old tiles for /%I"
655 on_unit_inactive_sec "60m"
656 randomized_delay_sec "10m"
659 tile_directories.each do |directory|
660 label = directory[1..].gsub("/", "-")
662 service "cleanup-tiles@#{label}.timer" do
663 action [:enable, :start]
667 munin_plugin "mod_tile_fresh"
668 munin_plugin "mod_tile_latency"
669 munin_plugin "mod_tile_response"
670 munin_plugin "mod_tile_zoom"
672 munin_plugin "renderd_processed"
673 munin_plugin "renderd_queue"
674 munin_plugin "renderd_queue_time"
675 munin_plugin "renderd_zoom"
676 munin_plugin "renderd_zoom_time"
678 munin_plugin "replication_delay"
680 package "ruby-webrick"
682 prometheus_exporter "modtile" do
686 prometheus_exporter "renderd" do