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 # Old directory for IP rate limiting, now on the CDN
100 directory "/srv/tile.openstreetmap.org/conf" do
105 tile_directories = node[:tile][:styles].collect do |_, style|
106 style[:tile_directories].collect { |directory| directory[:name] }
107 end.flatten.sort.uniq
111 systemd_service "renderd" do
113 after "postgresql.service"
114 wants "postgresql.service"
117 restrict_address_families "AF_UNIX"
118 read_write_paths tile_directories
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"
166 python_package "pyotp" do
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 systemd_service "update-lowzoom@" do
290 description "Low zoom tile update service for %i layer"
292 exec_start_pre "+/bin/systemctl stop render-lowzoom.service"
293 exec_start "/bin/bash /usr/local/bin/update-lowzoom-%i"
294 runtime_directory "update-lowzoom-%i"
296 restrict_address_families "AF_UNIX"
298 "/srv/tile.openstreetmap.org/tiles/%i",
304 directory "/srv/tile.openstreetmap.org/styles" do
310 node[:tile][:styles].each do |name, details|
311 style_directory = "/srv/tile.openstreetmap.org/styles/#{name}"
312 tile_directory = "/srv/tile.openstreetmap.org/tiles/#{name}"
314 template "/usr/local/bin/update-lowzoom-#{name}" do
315 source "update-lowzoom.erb"
319 variables :style => name
322 service "update-lowzoom@#{name}" do
324 supports :restart => true
327 directory tile_directory do
333 details[:tile_directories].each do |directory|
334 directory directory[:name] do
340 directory[:min_zoom].upto(directory[:max_zoom]) do |zoom|
341 directory "#{directory[:name]}/#{zoom}" do
347 link "#{tile_directory}/#{zoom}" do
348 to "#{directory[:name]}/#{zoom}"
355 file "#{tile_directory}/planet-import-complete" do
356 action :create_if_missing
362 git style_directory do
364 repository details[:repository]
365 revision details[:revision]
370 link "#{style_directory}/data" do
371 to "/srv/tile.openstreetmap.org/data"
376 if details[:fonts_script]
377 execute details[:fonts_script] do
379 command details[:fonts_script]
383 subscribes :run, "git[#{style_directory}]"
387 execute "#{style_directory}/project.mml" do
389 command "carto -a 3.0.22 project.mml > project.xml"
393 subscribes :run, "git[#{style_directory}]"
394 notifies :restart, "service[renderd]", :immediately
395 notifies :restart, "service[update-lowzoom@#{name}]"
399 postgresql_version = node[:tile][:database][:cluster].split("/").first
400 postgis_version = node[:tile][:database][:postgis]
402 package "postgresql-#{postgresql_version}-postgis-#{postgis_version}"
404 postgresql_user "jburgess" do
405 cluster node[:tile][:database][:cluster]
409 postgresql_user "tomh" do
410 cluster node[:tile][:database][:cluster]
414 postgresql_user "pnorman" do
415 cluster node[:tile][:database][:cluster]
419 postgresql_user "tile" do
420 cluster node[:tile][:database][:cluster]
423 postgresql_user "www-data" do
424 cluster node[:tile][:database][:cluster]
427 postgresql_user "_renderd" do
428 cluster node[:tile][:database][:cluster]
431 postgresql_database "gis" do
432 cluster node[:tile][:database][:cluster]
436 postgresql_extension "postgis" do
437 cluster node[:tile][:database][:cluster]
441 postgresql_extension "hstore" do
442 cluster node[:tile][:database][:cluster]
444 only_if { node[:tile][:database][:hstore] }
447 %w[geography_columns planet_osm_nodes planet_osm_rels planet_osm_ways raster_columns raster_overviews].each do |table|
448 postgresql_table table do
449 cluster node[:tile][:database][:cluster]
452 permissions "tile" => :all
456 %w[geometry_columns planet_osm_line planet_osm_point planet_osm_polygon planet_osm_roads spatial_ref_sys].each do |table|
457 postgresql_table table do
458 cluster node[:tile][:database][:cluster]
461 permissions "tile" => :all, "www-data" => :select, "_renderd" => :select
471 if node[:tile][:database][:external_data_script]
472 execute node[:tile][:database][:external_data_script] do
473 command "#{node[:tile][:database][:external_data_script]} -R _renderd"
474 cwd "/srv/tile.openstreetmap.org"
480 Array(node[:tile][:database][:external_data_tables]).each do |table|
481 postgresql_table table do
482 cluster node[:tile][:database][:cluster]
485 permissions "tile" => :all, "www-data" => :select, "_renderd" => :select
490 postgresql_munin "gis" do
491 cluster node[:tile][:database][:cluster]
495 directory File.dirname(node[:tile][:database][:node_file]) do
502 file node[:tile][:database][:node_file] do
508 directory "/var/log/tile" do
521 remote_directory "/usr/local/bin" do
531 file "/usr/local/bin/tile-ratelimit" do
535 service "tile-ratelimit" do
536 action [:stop, :disable]
539 systemd_service "tile-ratelimit" do
543 template "/usr/local/bin/expire-tiles" do
544 source "expire-tiles.erb"
550 directory "/var/lib/replicate" do
556 directory "/var/lib/replicate/expire-queue" do
562 template "/usr/local/bin/replicate" do
563 source "replicate.erb"
567 variables :postgresql_version => postgresql_version.to_f
570 systemd_service "expire-tiles" do
571 description "Tile dirtying service"
574 exec_start "/usr/local/bin/expire-tiles"
576 standard_output "null"
578 read_write_paths tile_directories + [
579 "/store/database/nodes",
580 "/var/lib/replicate/expire-queue",
585 systemd_path "expire-tiles" do
586 description "Tile dirtying trigger"
587 directory_not_empty "/var/lib/replicate/expire-queue"
590 service "expire-tiles.path" do
591 action [:enable, :start]
592 subscribes :restart, "systemd_path[expire-tiles]"
595 systemd_service "replicate" do
596 description "Rendering database replication service"
597 after "postgresql.service"
598 wants "postgresql.service"
600 exec_start "/usr/local/bin/replicate"
601 sandbox :enable_network => true
602 restrict_address_families "AF_UNIX"
604 "/store/database/nodes",
605 "/var/lib/replicate",
611 service "replicate" do
612 action [:enable, :start]
613 subscribes :restart, "template[/usr/local/bin/replicate]"
614 subscribes :restart, "systemd_service[replicate]"
617 # FIXME: cleanup old replicate logrotate
618 file "/etc/logrotate.d/replicate" do
622 template "/usr/local/bin/render-lowzoom" do
623 source "render-lowzoom.erb"
629 systemd_service "render-lowzoom" do
630 description "Render low zoom tiles"
631 condition_path_exists_glob "!/run/update-lowzoom-*"
633 exec_start "/usr/local/bin/render-lowzoom"
635 restrict_address_families "AF_UNIX"
636 read_write_paths "/var/log/tile"
639 systemd_timer "render-lowzoom" do
640 description "Render low zoom tiles"
641 on_calendar "Fri *-*-* 23:00:00 UTC"
644 service "render-lowzoom.timer" do
645 action [:enable, :start]
648 package "liblockfile-simple-perl"
649 package "libfilesys-df-perl"
651 template "/usr/local/bin/cleanup-tiles" do
652 source "cleanup-tiles.erb"
658 systemd_service "cleanup-tiles@" do
659 description "Cleanup old tiles for /%I"
660 exec_start "/usr/local/bin/cleanup-tiles /%I"
662 io_scheduling_class "idle"
664 read_write_paths "/%I"
667 systemd_timer "cleanup-tiles@" do
668 description "Cleanup old tiles for /%I"
670 on_unit_inactive_sec "60m"
671 randomized_delay_sec "10m"
674 tile_directories.each do |directory|
675 label = directory[1..].gsub("/", "-")
677 service "cleanup-tiles@#{label}.timer" do
678 action [:enable, :start]
682 munin_plugin "mod_tile_fresh"
683 munin_plugin "mod_tile_latency"
684 munin_plugin "mod_tile_response"
685 munin_plugin "mod_tile_zoom"
687 munin_plugin "renderd_processed"
688 munin_plugin "renderd_queue"
689 munin_plugin "renderd_queue_time"
690 munin_plugin "renderd_zoom"
691 munin_plugin "renderd_zoom_time"
693 munin_plugin "replication_delay"
695 package "ruby-webrick"
697 prometheus_exporter "modtile" do
701 prometheus_exporter "renderd" do