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 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 apache_site "default" do
66 apache_site "tileserver_site" do
70 apache_site "tile.openstreetmap.org" do
72 variables :fastly => fastlyips["addresses"]
75 template "/etc/logrotate.d/apache2" do
76 source "logrotate.apache.erb"
82 directory "/srv/tile.openstreetmap.org" do
88 directory "/srv/tile.openstreetmap.org/conf" do
94 file "/srv/tile.openstreetmap.org/conf/ip.map" do
102 systemd_service "renderd" do
104 after "postgresql.service"
105 wants "postgresql.service"
110 protect_system "full"
112 no_new_privileges true
116 systemd_service "renderd" do
121 action [:enable, :start]
122 subscribes :restart, "systemd_service[renderd]"
125 directory "/srv/tile.openstreetmap.org/tiles" do
131 template "/etc/renderd.conf" do
132 source "renderd.conf.erb"
136 notifies :reload, "service[apache2]"
137 notifies :restart, "service[renderd]"
140 remote_directory "/srv/tile.openstreetmap.org/html" do
150 template "/srv/tile.openstreetmap.org/html/index.html" do
151 source "index.html.erb"
164 python_package "pyotp" do
168 directory "/srv/tile.openstreetmap.org/cgi-bin" do
174 template "/srv/tile.openstreetmap.org/cgi-bin/export" do
179 variables :blocks => blocks, :totp_key => web_passwords["totp_key"]
182 template "/srv/tile.openstreetmap.org/cgi-bin/debug" do
189 template "/etc/cron.hourly/export" do
190 source "export.cron.erb"
196 directory "/srv/tile.openstreetmap.org/data" do
202 package "mapnik-utils"
204 node[:tile][:data].each_value do |data|
206 file = "/srv/tile.openstreetmap.org/data/#{File.basename(url)}"
209 directory = "/srv/tile.openstreetmap.org/data/#{data[:directory]}"
211 directory directory do
217 directory = "/srv/tile.openstreetmap.org/data"
225 command "tar -zxf #{file} -C #{directory}"
229 elsif file =~ /\.tar\.bz2$/
234 command "tar -jxf #{file} -C #{directory}"
238 elsif file =~ /\.zip$/
243 command "unzip -qq -o #{file} -d #{directory}"
249 execute "#{file}_shapeindex" do
251 command "find #{directory} -type f -iname '*.shp' -print0 | xargs -0 --no-run-if-empty shapeindex --shape_files"
254 subscribes :run, "execute[#{file}]", :immediately
260 use_conditional_get true
263 action :create_if_missing
271 notifies :run, "execute[#{file}]", :immediately
272 notifies :restart, "service[renderd]"
276 nodejs_package "carto"
278 systemd_service "update-lowzoom@" do
279 description "Low zoom tile update service for %i layer"
280 conflicts "render-lowzoom.service"
282 exec_start "/bin/bash /usr/local/bin/update-lowzoom-%i"
283 runtime_directory "update-lowzoom-%i"
287 protect_system "strict"
290 "/srv/tile.openstreetmap.org/tiles/%i",
293 no_new_privileges true
297 directory "/srv/tile.openstreetmap.org/styles" do
303 node[:tile][:styles].each do |name, details|
304 style_directory = "/srv/tile.openstreetmap.org/styles/#{name}"
305 tile_directory = "/srv/tile.openstreetmap.org/tiles/#{name}"
307 template "/usr/local/bin/update-lowzoom-#{name}" do
308 source "update-lowzoom.erb"
312 variables :style => name
315 service "update-lowzoom@#{name}" do
317 supports :restart => true
320 directory tile_directory do
326 details[:tile_directories].each do |directory|
327 directory directory[:name] do
333 directory[:min_zoom].upto(directory[:max_zoom]) do |zoom|
334 directory "#{directory[:name]}/#{zoom}" do
340 link "#{tile_directory}/#{zoom}" do
341 to "#{directory[:name]}/#{zoom}"
348 file "#{tile_directory}/planet-import-complete" do
349 action :create_if_missing
355 git style_directory do
357 repository details[:repository]
358 revision details[:revision]
363 link "#{style_directory}/data" do
364 to "/srv/tile.openstreetmap.org/data"
369 if details[:fonts_script]
370 execute details[:fonts_script] do
372 command details[:fonts_script]
376 subscribes :run, "git[#{style_directory}]"
380 execute "#{style_directory}/project.mml" do
382 command "carto -a 3.0.0 project.mml > project.xml"
386 subscribes :run, "git[#{style_directory}]"
387 notifies :restart, "service[renderd]", :immediately
388 notifies :restart, "service[update-lowzoom@#{name}]"
392 postgresql_version = node[:tile][:database][:cluster].split("/").first
393 postgis_version = node[:tile][:database][:postgis]
395 package "postgresql-#{postgresql_version}-postgis-#{postgis_version}"
397 postgresql_user "jburgess" do
398 cluster node[:tile][:database][:cluster]
402 postgresql_user "tomh" do
403 cluster node[:tile][:database][:cluster]
407 postgresql_user "pnorman" do
408 cluster node[:tile][:database][:cluster]
412 postgresql_user "tile" do
413 cluster node[:tile][:database][:cluster]
416 postgresql_user "www-data" do
417 cluster node[:tile][:database][:cluster]
420 postgresql_user "_renderd" do
421 cluster node[:tile][:database][:cluster]
424 postgresql_database "gis" do
425 cluster node[:tile][:database][:cluster]
429 postgresql_extension "postgis" do
430 cluster node[:tile][:database][:cluster]
434 postgresql_extension "hstore" do
435 cluster node[:tile][:database][:cluster]
437 only_if { node[:tile][:database][:hstore] }
440 %w[geography_columns planet_osm_nodes planet_osm_rels planet_osm_ways raster_columns raster_overviews spatial_ref_sys].each do |table|
441 postgresql_table table do
442 cluster node[:tile][:database][:cluster]
445 permissions "tile" => :all
449 %w[geometry_columns planet_osm_line planet_osm_point planet_osm_polygon planet_osm_roads].each do |table|
450 postgresql_table table do
451 cluster node[:tile][:database][:cluster]
454 permissions "tile" => :all, "www-data" => :select, "_renderd" => :select
464 if node[:tile][:database][:external_data_script]
465 execute node[:tile][:database][:external_data_script] do
466 command "#{node[:tile][:database][:external_data_script]} -R _renderd"
467 cwd "/srv/tile.openstreetmap.org"
473 Array(node[:tile][:database][:external_data_tables]).each do |table|
474 postgresql_table table do
475 cluster node[:tile][:database][:cluster]
478 permissions "tile" => :all, "www-data" => :select, "_renderd" => :select
483 postgresql_munin "gis" do
484 cluster node[:tile][:database][:cluster]
488 directory File.dirname(node[:tile][:database][:node_file]) do
495 file node[:tile][:database][:node_file] do
501 directory "/var/log/tile" do
514 gem_package "apachelogregex" do
515 gem_binary node[:ruby][:gem]
518 gem_package "file-tail" do
519 gem_binary node[:ruby][:gem]
522 gem_package "lru_redux" do
523 gem_binary node[:ruby][:gem]
526 remote_directory "/usr/local/bin" do
536 template "/usr/local/bin/tile-ratelimit" do
537 source "tile-ratelimit.erb"
543 systemd_service "tile-ratelimit" do
544 description "Monitor tile requests and enforce rate limits"
545 after "apache2.service"
548 exec_start "/usr/local/bin/tile-ratelimit"
553 protect_system "strict"
555 read_write_paths "/srv/tile.openstreetmap.org/conf"
556 no_new_privileges true
560 service "tile-ratelimit" do
561 action [:enable, :start]
562 subscribes :restart, "file[/usr/local/bin/tile-ratelimit]"
563 subscribes :restart, "systemd_service[tile-ratelimit]"
566 template "/usr/local/bin/expire-tiles" do
567 source "expire-tiles.erb"
573 directory "/var/lib/replicate" do
579 directory "/var/lib/replicate/expire-queue" do
585 template "/usr/local/bin/replicate" do
586 source "replicate.erb"
590 variables :postgresql_version => postgresql_version.to_f
593 systemd_service "expire-tiles" do
594 description "Tile dirtying service"
597 exec_start "/usr/local/bin/expire-tiles"
599 standard_output "null"
602 protect_system "strict"
605 "/store/database/nodes",
607 "/var/lib/replicate/expire-queue",
610 no_new_privileges true
613 systemd_path "expire-tiles" do
614 description "Tile dirtying trigger"
615 directory_not_empty "/var/lib/replicate/expire-queue"
618 service "expire-tiles.path" do
619 action [:enable, :start]
620 subscribes :restart, "systemd_path[expire-tiles]"
623 systemd_service "replicate" do
624 description "Rendering database replication service"
625 after "postgresql.service"
626 wants "postgresql.service"
628 exec_start "/usr/local/bin/replicate"
631 protect_system "strict"
634 "/store/database/nodes",
635 "/var/lib/replicate",
638 no_new_privileges true
642 service "replicate" do
643 action [:enable, :start]
644 subscribes :restart, "template[/usr/local/bin/replicate]"
645 subscribes :restart, "systemd_service[replicate]"
648 template "/etc/logrotate.d/replicate" do
649 source "replicate.logrotate.erb"
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"
670 protect_system "strict"
672 read_write_paths "/var/log/tile"
673 no_new_privileges true
676 systemd_timer "render-lowzoom" do
677 description "Render low zoom tiles"
678 on_calendar "Fri *-*-* 23:00:00 UTC"
681 service "render-lowzoom.timer" do
682 action [:enable, :start]
685 package "liblockfile-simple-perl"
686 package "libfilesys-df-perl"
688 template "/usr/local/bin/cleanup-tiles" do
689 source "cleanup-tiles.erb"
695 tile_directories = node[:tile][:styles].collect do |_, style|
696 style[:tile_directories].collect { |directory| directory[:name] }
697 end.flatten.sort.uniq
699 tile_directories.each do |directory|
700 label = directory.gsub("/", "-")
702 cron_d "cleanup-tiles#{label}" do
705 command "ionice -c 3 /usr/local/bin/cleanup-tiles #{directory}"
706 mailto "admins@openstreetmap.org"
710 munin_plugin "mod_tile_fresh"
711 munin_plugin "mod_tile_latency"
712 munin_plugin "mod_tile_response"
713 munin_plugin "mod_tile_zoom"
715 munin_plugin "renderd_processed"
716 munin_plugin "renderd_queue"
717 munin_plugin "renderd_queue_time"
718 munin_plugin "renderd_zoom"
719 munin_plugin "renderd_zoom_time"
721 munin_plugin "replication_delay"
723 package "ruby-webrick"
725 prometheus_exporter "modtile" do
729 prometheus_exporter "renderd" do