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"
108 restrict_address_families "AF_UNIX"
109 read_write_paths "/store/tiles"
113 systemd_service "renderd" do
118 action [:enable, :start]
119 subscribes :restart, "systemd_service[renderd]"
122 directory "/srv/tile.openstreetmap.org/tiles" do
128 template "/etc/renderd.conf" do
129 source "renderd.conf.erb"
133 notifies :reload, "service[apache2]"
134 notifies :restart, "service[renderd]"
137 remote_directory "/srv/tile.openstreetmap.org/html" do
147 template "/srv/tile.openstreetmap.org/html/index.html" do
148 source "index.html.erb"
161 python_package "pyotp" do
165 directory "/srv/tile.openstreetmap.org/cgi-bin" do
171 template "/srv/tile.openstreetmap.org/cgi-bin/export" do
176 variables :blocks => blocks, :totp_key => web_passwords["totp_key"]
179 template "/srv/tile.openstreetmap.org/cgi-bin/debug" do
186 template "/etc/cron.hourly/export" do
187 source "export.cron.erb"
193 directory "/srv/tile.openstreetmap.org/data" do
205 node[:tile][:data].each_value do |data|
207 file = "/srv/tile.openstreetmap.org/data/#{File.basename(url)}"
210 directory = "/srv/tile.openstreetmap.org/data/#{data[:directory]}"
212 directory directory do
218 directory = "/srv/tile.openstreetmap.org/data"
224 command "tar -zxf #{file} -C #{directory}"
228 elsif file =~ /\.tar\.bz2$/
231 command "tar -jxf #{file} -C #{directory}"
235 elsif file =~ /\.zip$/
238 command "unzip -qq -o #{file} -d #{directory}"
244 execute "#{file}_shapeindex" do
246 command "find #{directory} -type f -iname '*.shp' -print0 | xargs -0 --no-run-if-empty shapeindex --shape_files"
249 subscribes :run, "execute[#{file}]", :immediately
255 use_conditional_get true
258 action :create_if_missing
266 notifies :run, "execute[#{file}]", :immediately
267 notifies :restart, "service[renderd]"
271 nodejs_package "carto"
273 systemd_service "update-lowzoom@" do
274 description "Low zoom tile update service for %i layer"
276 exec_start_pre "+/bin/systemctl stop render-lowzoom.service"
277 exec_start "/bin/bash /usr/local/bin/update-lowzoom-%i"
278 runtime_directory "update-lowzoom-%i"
280 restrict_address_families "AF_UNIX"
282 "/srv/tile.openstreetmap.org/tiles/%i",
288 directory "/srv/tile.openstreetmap.org/styles" do
294 node[:tile][:styles].each do |name, details|
295 style_directory = "/srv/tile.openstreetmap.org/styles/#{name}"
296 tile_directory = "/srv/tile.openstreetmap.org/tiles/#{name}"
298 template "/usr/local/bin/update-lowzoom-#{name}" do
299 source "update-lowzoom.erb"
303 variables :style => name
306 service "update-lowzoom@#{name}" do
308 supports :restart => true
311 directory tile_directory do
317 details[:tile_directories].each do |directory|
318 directory directory[:name] do
324 directory[:min_zoom].upto(directory[:max_zoom]) do |zoom|
325 directory "#{directory[:name]}/#{zoom}" do
331 link "#{tile_directory}/#{zoom}" do
332 to "#{directory[:name]}/#{zoom}"
339 file "#{tile_directory}/planet-import-complete" do
340 action :create_if_missing
346 git style_directory do
348 repository details[:repository]
349 revision details[:revision]
354 link "#{style_directory}/data" do
355 to "/srv/tile.openstreetmap.org/data"
360 if details[:fonts_script]
361 execute details[:fonts_script] do
363 command details[:fonts_script]
367 subscribes :run, "git[#{style_directory}]"
371 execute "#{style_directory}/project.mml" do
373 command "carto -a 3.0.22 project.mml > project.xml"
377 subscribes :run, "git[#{style_directory}]"
378 notifies :restart, "service[renderd]", :immediately
379 notifies :restart, "service[update-lowzoom@#{name}]"
383 postgresql_version = node[:tile][:database][:cluster].split("/").first
384 postgis_version = node[:tile][:database][:postgis]
386 package "postgresql-#{postgresql_version}-postgis-#{postgis_version}"
388 postgresql_user "jburgess" do
389 cluster node[:tile][:database][:cluster]
393 postgresql_user "tomh" do
394 cluster node[:tile][:database][:cluster]
398 postgresql_user "pnorman" do
399 cluster node[:tile][:database][:cluster]
403 postgresql_user "tile" do
404 cluster node[:tile][:database][:cluster]
407 postgresql_user "www-data" do
408 cluster node[:tile][:database][:cluster]
411 postgresql_user "_renderd" do
412 cluster node[:tile][:database][:cluster]
415 postgresql_database "gis" do
416 cluster node[:tile][:database][:cluster]
420 postgresql_extension "postgis" do
421 cluster node[:tile][:database][:cluster]
425 postgresql_extension "hstore" do
426 cluster node[:tile][:database][:cluster]
428 only_if { node[:tile][:database][:hstore] }
431 %w[geography_columns planet_osm_nodes planet_osm_rels planet_osm_ways raster_columns raster_overviews spatial_ref_sys].each do |table|
432 postgresql_table table do
433 cluster node[:tile][:database][:cluster]
436 permissions "tile" => :all
440 %w[geometry_columns planet_osm_line planet_osm_point planet_osm_polygon planet_osm_roads].each do |table|
441 postgresql_table table do
442 cluster node[:tile][:database][:cluster]
445 permissions "tile" => :all, "www-data" => :select, "_renderd" => :select
455 if node[:tile][:database][:external_data_script]
456 execute node[:tile][:database][:external_data_script] do
457 command "#{node[:tile][:database][:external_data_script]} -R _renderd"
458 cwd "/srv/tile.openstreetmap.org"
464 Array(node[:tile][:database][:external_data_tables]).each do |table|
465 postgresql_table table do
466 cluster node[:tile][:database][:cluster]
469 permissions "tile" => :all, "www-data" => :select, "_renderd" => :select
474 postgresql_munin "gis" do
475 cluster node[:tile][:database][:cluster]
479 directory File.dirname(node[:tile][:database][:node_file]) do
486 file node[:tile][:database][:node_file] do
492 directory "/var/log/tile" do
505 gem_package "apachelogregex" do
506 gem_binary node[:ruby][:gem]
509 gem_package "file-tail" do
510 gem_binary node[:ruby][:gem]
513 gem_package "lru_redux" do
514 gem_binary node[:ruby][:gem]
517 remote_directory "/usr/local/bin" do
527 template "/usr/local/bin/tile-ratelimit" do
528 source "tile-ratelimit.erb"
534 systemd_service "tile-ratelimit" do
535 description "Monitor tile requests and enforce rate limits"
536 after "apache2.service"
539 exec_start "/usr/local/bin/tile-ratelimit"
542 read_write_paths "/srv/tile.openstreetmap.org/conf"
546 service "tile-ratelimit" do
547 action [:enable, :start]
548 subscribes :restart, "file[/usr/local/bin/tile-ratelimit]"
549 subscribes :restart, "systemd_service[tile-ratelimit]"
552 template "/usr/local/bin/expire-tiles" do
553 source "expire-tiles.erb"
559 directory "/var/lib/replicate" do
565 directory "/var/lib/replicate/expire-queue" do
571 template "/usr/local/bin/replicate" do
572 source "replicate.erb"
576 variables :postgresql_version => postgresql_version.to_f
579 systemd_service "expire-tiles" do
580 description "Tile dirtying service"
583 exec_start "/usr/local/bin/expire-tiles"
585 standard_output "null"
588 "/store/database/nodes",
590 "/var/lib/replicate/expire-queue",
595 systemd_path "expire-tiles" do
596 description "Tile dirtying trigger"
597 directory_not_empty "/var/lib/replicate/expire-queue"
600 service "expire-tiles.path" do
601 action [:enable, :start]
602 subscribes :restart, "systemd_path[expire-tiles]"
605 systemd_service "replicate" do
606 description "Rendering database replication service"
607 after "postgresql.service"
608 wants "postgresql.service"
610 exec_start "/usr/local/bin/replicate"
611 sandbox :enable_network => true
612 restrict_address_families "AF_UNIX"
614 "/store/database/nodes",
615 "/var/lib/replicate",
621 service "replicate" do
622 action [:enable, :start]
623 subscribes :restart, "template[/usr/local/bin/replicate]"
624 subscribes :restart, "systemd_service[replicate]"
627 template "/etc/logrotate.d/replicate" do
628 source "replicate.logrotate.erb"
634 template "/usr/local/bin/render-lowzoom" do
635 source "render-lowzoom.erb"
641 systemd_service "render-lowzoom" do
642 description "Render low zoom tiles"
643 condition_path_exists_glob "!/run/update-lowzoom-*"
645 exec_start "/usr/local/bin/render-lowzoom"
647 restrict_address_families "AF_UNIX"
648 read_write_paths "/var/log/tile"
651 systemd_timer "render-lowzoom" do
652 description "Render low zoom tiles"
653 on_calendar "Fri *-*-* 23:00:00 UTC"
656 service "render-lowzoom.timer" do
657 action [:enable, :start]
660 package "liblockfile-simple-perl"
661 package "libfilesys-df-perl"
663 template "/usr/local/bin/cleanup-tiles" do
664 source "cleanup-tiles.erb"
670 tile_directories = node[:tile][:styles].collect do |_, style|
671 style[:tile_directories].collect { |directory| directory[:name] }
672 end.flatten.sort.uniq
674 tile_directories.each do |directory|
675 label = directory.gsub("/", "-")
677 cron_d "cleanup-tiles#{label}" do
680 command "ionice -c 3 /usr/local/bin/cleanup-tiles #{directory}"
681 mailto "admins@openstreetmap.org"
685 munin_plugin "mod_tile_fresh"
686 munin_plugin "mod_tile_latency"
687 munin_plugin "mod_tile_response"
688 munin_plugin "mod_tile_zoom"
690 munin_plugin "renderd_processed"
691 munin_plugin "renderd_queue"
692 munin_plugin "renderd_queue_time"
693 munin_plugin "renderd_zoom"
694 munin_plugin "renderd_zoom_time"
696 munin_plugin "replication_delay"
698 package "ruby-webrick"
700 prometheus_exporter "modtile" do
704 prometheus_exporter "renderd" do