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"
111 restrict_address_families "AF_UNIX"
112 read_write_paths tile_directories
117 action [:enable, :start]
118 subscribes :restart, "systemd_service[renderd]"
121 directory "/srv/tile.openstreetmap.org/tiles" do
127 template "/etc/renderd.conf" do
128 source "renderd.conf.erb"
132 notifies :reload, "service[apache2]"
133 notifies :restart, "service[renderd]"
136 remote_directory "/srv/tile.openstreetmap.org/html" do
146 template "/srv/tile.openstreetmap.org/html/index.html" do
147 source "index.html.erb"
160 python_package "pyotp" do
164 directory "/srv/tile.openstreetmap.org/cgi-bin" do
170 template "/srv/tile.openstreetmap.org/cgi-bin/export" do
175 variables :blocks => blocks, :totp_key => web_passwords["totp_key"]
178 template "/srv/tile.openstreetmap.org/cgi-bin/debug" do
185 systemd_service "export-cleanup" do
186 description "Cleanup stale export temporary files"
187 joins_namespace_of "apache2.service"
188 exec_start "find /tmp -ignore_readdir_race -name 'export??????' -mmin +60 -delete"
193 systemd_timer "export-cleanup" do
194 description "Cleanup stale export temporary files"
196 on_unit_inactive_sec "60m"
199 service "export-cleanup.timer" do
200 action [:enable, :start]
203 directory "/srv/tile.openstreetmap.org/data" do
215 node[:tile][:data].each_value do |data|
217 file = "/srv/tile.openstreetmap.org/data/#{File.basename(url)}"
220 directory = "/srv/tile.openstreetmap.org/data/#{data[:directory]}"
222 directory directory do
228 directory = "/srv/tile.openstreetmap.org/data"
234 command "tar -zxf #{file} -C #{directory}"
238 elsif file =~ /\.tar\.bz2$/
241 command "tar -jxf #{file} -C #{directory}"
245 elsif file =~ /\.zip$/
248 command "unzip -qq -o #{file} -d #{directory}"
254 execute "#{file}_shapeindex" do
256 command "find #{directory} -type f -iname '*.shp' -print0 | xargs -0 --no-run-if-empty shapeindex --shape_files"
259 subscribes :run, "execute[#{file}]", :immediately
265 use_conditional_get true
268 action :create_if_missing
276 notifies :run, "execute[#{file}]", :immediately
277 notifies :restart, "service[renderd]"
281 nodejs_package "carto"
283 systemd_service "update-lowzoom@" do
284 description "Low zoom tile update service for %i layer"
286 exec_start_pre "+/bin/systemctl stop render-lowzoom.service"
287 exec_start "/bin/bash /usr/local/bin/update-lowzoom-%i"
288 runtime_directory "update-lowzoom-%i"
290 restrict_address_families "AF_UNIX"
292 "/srv/tile.openstreetmap.org/tiles/%i",
298 directory "/srv/tile.openstreetmap.org/styles" do
304 node[:tile][:styles].each do |name, details|
305 style_directory = "/srv/tile.openstreetmap.org/styles/#{name}"
306 tile_directory = "/srv/tile.openstreetmap.org/tiles/#{name}"
308 template "/usr/local/bin/update-lowzoom-#{name}" do
309 source "update-lowzoom.erb"
313 variables :style => name
316 service "update-lowzoom@#{name}" do
318 supports :restart => true
321 directory tile_directory do
327 details[:tile_directories].each do |directory|
328 directory directory[:name] do
334 directory[:min_zoom].upto(directory[:max_zoom]) do |zoom|
335 directory "#{directory[:name]}/#{zoom}" do
341 link "#{tile_directory}/#{zoom}" do
342 to "#{directory[:name]}/#{zoom}"
349 file "#{tile_directory}/planet-import-complete" do
350 action :create_if_missing
356 git style_directory do
358 repository details[:repository]
359 revision details[:revision]
364 link "#{style_directory}/data" do
365 to "/srv/tile.openstreetmap.org/data"
370 if details[:fonts_script]
371 execute details[:fonts_script] do
373 command details[:fonts_script]
377 subscribes :run, "git[#{style_directory}]"
381 execute "#{style_directory}/project.mml" do
383 command "carto -a 3.0.22 project.mml > project.xml"
387 subscribes :run, "git[#{style_directory}]"
388 notifies :restart, "service[renderd]", :immediately
389 notifies :restart, "service[update-lowzoom@#{name}]"
393 postgresql_version = node[:tile][:database][:cluster].split("/").first
394 postgis_version = node[:tile][:database][:postgis]
396 package "postgresql-#{postgresql_version}-postgis-#{postgis_version}"
398 postgresql_user "jburgess" do
399 cluster node[:tile][:database][:cluster]
403 postgresql_user "tomh" do
404 cluster node[:tile][:database][:cluster]
408 postgresql_user "pnorman" do
409 cluster node[:tile][:database][:cluster]
413 postgresql_user "tile" do
414 cluster node[:tile][:database][:cluster]
417 postgresql_user "www-data" do
418 cluster node[:tile][:database][:cluster]
421 postgresql_user "_renderd" do
422 cluster node[:tile][:database][:cluster]
425 postgresql_database "gis" do
426 cluster node[:tile][:database][:cluster]
430 postgresql_extension "postgis" do
431 cluster node[:tile][:database][:cluster]
435 postgresql_extension "hstore" do
436 cluster node[:tile][:database][:cluster]
438 only_if { node[:tile][:database][:hstore] }
441 %w[geography_columns planet_osm_nodes planet_osm_rels planet_osm_ways raster_columns raster_overviews].each do |table|
442 postgresql_table table do
443 cluster node[:tile][:database][:cluster]
446 permissions "tile" => :all
450 %w[geometry_columns planet_osm_line planet_osm_point planet_osm_polygon planet_osm_roads spatial_ref_sys].each do |table|
451 postgresql_table table do
452 cluster node[:tile][:database][:cluster]
455 permissions "tile" => :all, "www-data" => :select, "_renderd" => :select
465 if node[:tile][:database][:external_data_script]
466 execute node[:tile][:database][:external_data_script] do
467 command "#{node[:tile][:database][:external_data_script]} -R _renderd"
468 cwd "/srv/tile.openstreetmap.org"
474 Array(node[:tile][:database][:external_data_tables]).each do |table|
475 postgresql_table table do
476 cluster node[:tile][:database][:cluster]
479 permissions "tile" => :all, "www-data" => :select, "_renderd" => :select
484 postgresql_munin "gis" do
485 cluster node[:tile][:database][:cluster]
489 directory File.dirname(node[:tile][:database][:node_file]) do
496 file node[:tile][:database][:node_file] do
502 directory "/var/log/tile" do
515 remote_directory "/usr/local/bin" do
525 directory "/var/lib/replicate" do
531 if node[:tile][:replication][:engine] == "custom"
532 template "/usr/local/bin/expire-tiles" do
533 source "expire-tiles-custom.erb"
539 directory "/var/lib/replicate/expire-queue" do
545 template "/usr/local/bin/replicate" do
546 source "replicate.erb"
550 variables :postgresql_version => postgresql_version.to_f
553 systemd_service "expire-tiles" do
554 description "Tile dirtying service"
557 exec_start "/usr/local/bin/expire-tiles"
560 read_write_paths tile_directories + [
561 "/store/database/nodes",
562 "/var/lib/replicate/expire-queue",
567 systemd_path "expire-tiles" do
568 description "Tile dirtying trigger"
569 directory_not_empty "/var/lib/replicate/expire-queue"
572 service "expire-tiles.path" do
573 action [:enable, :start]
574 subscribes :restart, "systemd_path[expire-tiles]"
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",
587 "/var/lib/replicate",
593 service "replicate" do
594 action [:enable, :start]
595 subscribes :restart, "template[/usr/local/bin/replicate]"
596 subscribes :restart, "systemd_service[replicate]"
598 elsif node[:tile][:replication][:engine] == "osm2pgsql"
599 service "expire-tiles.path" do
600 action [:disable, :stop]
603 systemd_path "expire-tiles" do
607 service "expire-tiles" do
611 systemd_service "expire-tiles" do
615 template "/usr/local/bin/expire-tiles" do
616 source "expire-tiles-osm2pgsql.erb"
622 osm2pgsql_arguments = %w[
626 --expire-output=/var/lib/replicate/dirty-tiles.txt
629 osm2pgsql_arguments.append("--multi-geometry") if node[:tile][:database][:multi_geometry]
630 osm2pgsql_arguments.append("--hstore") if node[:tile][:database][:hstore]
631 osm2pgsql_arguments.append("--tag-transform-script=#{node[:tile][:database][:tag_transform_script]}") if node[:tile][:database][:tag_transform_script]
633 systemd_service "replicate" do
634 description "Rendering database replication service"
635 after "postgresql.service"
636 wants "postgresql.service"
638 exec_start "/bin/osm2pgsql-replication update --database gis --post-processing /usr/local/bin/expire-tiles -- #{osm2pgsql_arguments.join(' ')}"
639 sandbox :enable_network => true
640 restrict_address_families "AF_UNIX"
642 "/store/database/nodes",
649 service "replicate" do
650 action [:enable, :start]
651 subscribes :restart, "systemd_service[replicate]"
655 template "/usr/local/bin/render-lowzoom" do
656 source "render-lowzoom.erb"
662 systemd_service "render-lowzoom" do
663 description "Render low zoom tiles"
664 condition_path_exists_glob "!/run/update-lowzoom-*"
666 exec_start "/usr/local/bin/render-lowzoom"
668 restrict_address_families "AF_UNIX"
669 read_write_paths "/var/log/tile"
672 systemd_timer "render-lowzoom" do
673 description "Render low zoom tiles"
674 on_calendar "Fri *-*-* 23:00:00 UTC"
677 service "render-lowzoom.timer" do
678 action [:enable, :start]
681 package "liblockfile-simple-perl"
682 package "libfilesys-df-perl"
684 template "/usr/local/bin/cleanup-tiles" do
685 source "cleanup-tiles.erb"
691 systemd_service "cleanup-tiles@" do
692 description "Cleanup old tiles for /%I"
693 exec_start "/usr/local/bin/cleanup-tiles /%I"
695 io_scheduling_class "idle"
697 read_write_paths "/%I"
700 systemd_timer "cleanup-tiles@" do
701 description "Cleanup old tiles for /%I"
703 on_unit_inactive_sec "60m"
704 randomized_delay_sec "10m"
707 tile_directories.each do |directory|
708 label = directory[1..].gsub("/", "-")
710 service "cleanup-tiles@#{label}.timer" do
711 action [:enable, :start]
715 munin_plugin "mod_tile_fresh"
716 munin_plugin "mod_tile_latency"
717 munin_plugin "mod_tile_response"
718 munin_plugin "mod_tile_zoom"
720 munin_plugin "renderd_processed"
721 munin_plugin "renderd_queue"
722 munin_plugin "renderd_queue_time"
723 munin_plugin "renderd_zoom"
724 munin_plugin "renderd_zoom_time"
726 munin_plugin "replication_delay"
728 package "ruby-webrick"
730 prometheus_exporter "modtile" do
734 prometheus_exporter "renderd" do