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
108 systemd_service "renderd" do
110 after "postgresql.service"
111 wants "postgresql.service"
112 environment "LD_PRELOAD" => "libtcmalloc.so.4"
117 restrict_address_families "AF_UNIX"
118 read_write_paths tile_directories
119 system_call_filter ["@system-service", "mincore"]
124 action [:enable, :start]
125 subscribes :restart, "systemd_service[renderd]"
128 directory "/srv/tile.openstreetmap.org/tiles" do
134 template "/etc/renderd.conf" do
135 source "renderd.conf.erb"
139 notifies :reload, "service[apache2]"
140 notifies :restart, "service[renderd]"
143 remote_directory "/srv/tile.openstreetmap.org/html" do
153 template "/srv/tile.openstreetmap.org/html/index.html" do
154 source "index.html.erb"
167 python_package "pyotp" do
171 directory "/srv/tile.openstreetmap.org/cgi-bin" do
177 template "/srv/tile.openstreetmap.org/cgi-bin/export" do
182 variables :blocks => blocks, :totp_key => web_passwords["totp_key"]
185 template "/srv/tile.openstreetmap.org/cgi-bin/debug" do
192 systemd_service "export-cleanup" do
193 description "Cleanup stale export temporary files"
194 joins_namespace_of "apache2.service"
195 exec_start "find /tmp -ignore_readdir_race -name 'export??????' -mmin +60 -delete"
200 systemd_timer "export-cleanup" do
201 description "Cleanup stale export temporary files"
203 on_unit_inactive_sec "60m"
206 service "export-cleanup.timer" do
207 action [:enable, :start]
210 directory "/srv/tile.openstreetmap.org/data" do
222 node[:tile][:data].each_value do |data|
224 file = "/srv/tile.openstreetmap.org/data/#{File.basename(url)}"
227 directory = "/srv/tile.openstreetmap.org/data/#{data[:directory]}"
229 directory directory do
235 directory = "/srv/tile.openstreetmap.org/data"
241 command "tar -zxf #{file} -C #{directory}"
245 elsif file =~ /\.tar\.bz2$/
248 command "tar -jxf #{file} -C #{directory}"
252 elsif file =~ /\.zip$/
255 command "unzip -qq -o #{file} -d #{directory}"
261 execute "#{file}_shapeindex" do
263 command "find #{directory} -type f -iname '*.shp' -print0 | xargs -0 --no-run-if-empty shapeindex --shape_files"
266 subscribes :run, "execute[#{file}]", :immediately
272 use_conditional_get true
275 action :create_if_missing
283 notifies :run, "execute[#{file}]", :immediately
284 notifies :restart, "service[renderd]"
288 nodejs_package "carto"
290 lowzoom_threads = [node.cpu_cores - 1, node[:memory][:total].to_f / 6291456].min.floor
292 systemd_service "update-lowzoom@" do
293 description "Low zoom tile update service for %i layer"
295 exec_start_pre "+/bin/systemctl stop render-lowzoom.service"
296 exec_start "/bin/bash /usr/local/bin/update-lowzoom-%i"
297 runtime_directory "update-lowzoom-%i"
299 restrict_address_families "AF_UNIX"
301 "/srv/tile.openstreetmap.org/tiles/%i",
307 directory "/srv/tile.openstreetmap.org/styles" do
313 node[:tile][:styles].each do |name, details|
314 style_directory = "/srv/tile.openstreetmap.org/styles/#{name}"
315 tile_directory = "/srv/tile.openstreetmap.org/tiles/#{name}"
317 template "/usr/local/bin/update-lowzoom-#{name}" do
318 source "update-lowzoom.erb"
322 variables :style => name, :threads => lowzoom_threads
325 service "update-lowzoom@#{name}" do
327 supports :restart => true
330 directory tile_directory do
336 details[:tile_directories].each do |directory|
337 directory directory[:name] do
343 directory[:min_zoom].upto(directory[:max_zoom]) do |zoom|
344 directory "#{directory[:name]}/#{zoom}" do
350 link "#{tile_directory}/#{zoom}" do
351 to "#{directory[:name]}/#{zoom}"
358 file "#{tile_directory}/planet-import-complete" do
359 action :create_if_missing
365 git style_directory do
367 repository details[:repository]
368 revision details[:revision]
373 link "#{style_directory}/data" do
374 to "/srv/tile.openstreetmap.org/data"
379 if details[:fonts_script]
380 execute details[:fonts_script] do
382 command details[:fonts_script]
386 subscribes :run, "git[#{style_directory}]"
390 execute "#{style_directory}/project.mml" do
392 command "carto -a 3.0.22 project.mml > project.xml"
396 subscribes :run, "git[#{style_directory}]"
397 notifies :restart, "service[renderd]", :immediately
398 notifies :restart, "service[update-lowzoom@#{name}]"
402 postgresql_version = node[:tile][:database][:cluster].split("/").first
403 postgis_version = node[:tile][:database][:postgis]
405 package "postgresql-#{postgresql_version}-postgis-#{postgis_version}"
407 postgresql_user "jburgess" do
408 cluster node[:tile][:database][:cluster]
412 postgresql_user "tomh" do
413 cluster node[:tile][:database][:cluster]
417 postgresql_user "pnorman" do
418 cluster node[:tile][:database][:cluster]
422 postgresql_user "tile" do
423 cluster node[:tile][:database][:cluster]
426 postgresql_user "www-data" do
427 cluster node[:tile][:database][:cluster]
430 postgresql_user "_renderd" do
431 cluster node[:tile][:database][:cluster]
434 postgresql_database "gis" do
435 cluster node[:tile][:database][:cluster]
439 postgresql_extension "postgis" do
440 cluster node[:tile][:database][:cluster]
444 postgresql_extension "hstore" do
445 cluster node[:tile][:database][:cluster]
447 only_if { node[:tile][:database][:hstore] }
450 %w[geography_columns planet_osm_nodes planet_osm_rels planet_osm_ways raster_columns raster_overviews].each do |table|
451 postgresql_table table do
452 cluster node[:tile][:database][:cluster]
455 permissions "tile" => :all
459 %w[geometry_columns planet_osm_line planet_osm_point planet_osm_polygon planet_osm_roads spatial_ref_sys].each do |table|
460 postgresql_table table do
461 cluster node[:tile][:database][:cluster]
464 permissions "tile" => :all, "www-data" => :select, "_renderd" => :select
474 if node[:tile][:database][:external_data_script]
475 execute node[:tile][:database][:external_data_script] do
476 command "#{node[:tile][:database][:external_data_script]} -R _renderd"
477 cwd "/srv/tile.openstreetmap.org"
483 Array(node[:tile][:database][:external_data_tables]).each do |table|
484 postgresql_table table do
485 cluster node[:tile][:database][:cluster]
488 permissions "tile" => :all, "www-data" => :select, "_renderd" => :select
493 postgresql_munin "gis" do
494 cluster node[:tile][:database][:cluster]
498 directory File.dirname(node[:tile][:database][:node_file]) do
505 file node[:tile][:database][:node_file] do
511 directory "/var/log/tile" do
523 directory "/var/lib/replicate" do
529 template "/usr/local/bin/expire-tiles" do
530 source "expire-tiles.erb"
536 directory "/var/lib/replicate/expire-queue" do
542 template "/usr/local/bin/replicate" do
543 source "replicate.erb"
549 systemd_service "expire-tiles" do
550 description "Tile dirtying service"
553 exec_start "/usr/local/bin/expire-tiles"
556 restrict_address_families "AF_UNIX"
557 read_write_paths tile_directories + [
558 "/var/lib/replicate/expire-queue"
562 systemd_path "expire-tiles" do
563 description "Tile dirtying trigger"
564 directory_not_empty "/var/lib/replicate/expire-queue"
567 service "expire-tiles.path" do
568 action [:enable, :start]
569 subscribes :restart, "systemd_path[expire-tiles]"
572 template "/usr/local/bin/replicate-post" do
573 source "replicate-post.erb"
579 osm2pgsql_arguments = %w[
583 --expire-output=/var/lib/replicate/dirty-tiles.txt
586 osm2pgsql_arguments.append("--multi-geometry") if node[:tile][:database][:multi_geometry]
587 osm2pgsql_arguments.append("--hstore") if node[:tile][:database][:hstore]
588 osm2pgsql_arguments.append("--tag-transform-script=#{node[:tile][:database][:tag_transform_script]}") if node[:tile][:database][:tag_transform_script]
590 systemd_service "replicate" do
591 description "Rendering database replication service"
592 after "postgresql.service"
593 wants "postgresql.service"
595 exec_start "/usr/local/bin/replicate"
596 sandbox :enable_network => true
597 restrict_address_families "AF_UNIX"
599 "/store/database/nodes",
605 service "replicate" do
606 action [:enable, :start]
607 subscribes :restart, "template[/usr/local/bin/replicate]"
608 subscribes :restart, "systemd_service[replicate]"
611 template "/usr/local/bin/render-lowzoom" do
612 source "render-lowzoom.erb"
616 variables :threads => lowzoom_threads
619 systemd_service "render-lowzoom" do
620 description "Render low zoom tiles"
621 condition_path_exists_glob "!/run/update-lowzoom-*"
623 exec_start "/usr/local/bin/render-lowzoom"
625 restrict_address_families "AF_UNIX"
626 read_write_paths "/var/log/tile"
629 systemd_timer "render-lowzoom" do
630 description "Render low zoom tiles"
631 on_calendar "23:00 #{node[:timezone]}"
634 service "render-lowzoom.timer" do
635 action [:enable, :start]
638 package "liblockfile-simple-perl"
639 package "libfilesys-df-perl"
641 template "/usr/local/bin/cleanup-tiles" do
642 source "cleanup-tiles.erb"
648 systemd_service "cleanup-tiles@" do
649 description "Cleanup old tiles for /%I"
650 exec_start "/usr/local/bin/cleanup-tiles /%I"
652 io_scheduling_class "idle"
654 read_write_paths "/%I"
657 systemd_timer "cleanup-tiles@" do
658 description "Cleanup old tiles for /%I"
660 on_unit_inactive_sec "60m"
661 randomized_delay_sec "10m"
664 tile_directories.each do |directory|
665 label = directory[1..].gsub("/", "-")
667 service "cleanup-tiles@#{label}.timer" do
668 action [:enable, :start]
672 munin_plugin "mod_tile_fresh"
673 munin_plugin "mod_tile_latency"
674 munin_plugin "mod_tile_response"
675 munin_plugin "mod_tile_zoom"
677 munin_plugin "renderd_processed"
678 munin_plugin "renderd_queue"
679 munin_plugin "renderd_queue_time"
680 munin_plugin "renderd_zoom"
681 munin_plugin "renderd_zoom_time"
683 munin_plugin "replication_delay"
685 package "ruby-webrick"
687 prometheus_exporter "modtile" do
691 prometheus_exporter "renderd" do