2 # Cookbook:: vectortile
5 # Copyright:: 2024, 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"
22 include_recipe "nginx"
23 include_recipe "postgresql"
24 include_recipe "prometheus"
25 include_recipe "python"
26 include_recipe "tools"
28 directory "/srv/vector.openstreetmap.org" do
34 nginx_site "default" do
38 nginx_site "vector.openstreetmap.org" do
42 ssl_certificate node[:fqdn] do
44 notifies :reload, "service[nginx]"
47 remote_directory "/srv/vector.openstreetmap.org/html" do
52 files_owner "www-data"
53 files_group "www-data"
57 template "/srv/vector.openstreetmap.org/html/index.html" do
58 source "index.html.erb"
64 postgresql_version = node[:vectortile][:database][:cluster].split("/").first
65 postgis_version = node[:vectortile][:database][:postgis]
67 package "postgresql-#{postgresql_version}-postgis-#{postgis_version}"
77 style_directory = "/srv/vector.openstreetmap.org/spirit"
78 git style_directory do
79 repository "https://github.com/pnorman/spirit.git"
84 shortbread_config = "#{style_directory}/shortbread.yaml"
86 themepark_directory = "/srv/vector.openstreetmap.org/osm2pgsql-themepark"
87 git themepark_directory do
88 repository "https://github.com/osm2pgsql-dev/osm2pgsql-themepark.git"
93 tilekiln_directory = "/opt/tilekiln"
95 python_virtualenv tilekiln_directory do
96 interpreter "/usr/bin/python3"
99 python_package "tilekiln" do
100 python_virtualenv tilekiln_directory
105 template "/srv/vector.openstreetmap.org/html/index.html" do
106 source "index.html.erb"
112 directory "/srv/vector.openstreetmap.org/data" do
118 node_store_options = node[:vectortile][:database][:nodes_store] == :flat ? "--flat-nodes '/srv/vector.openstreetmap.org/data/nodes.bin'" : ""
119 template "/usr/local/bin/import-planet" do
120 source "import-planet.erb"
124 variables :node_store_options => "#{node_store_options}"
127 template "/usr/local/bin/tilekiln-storage-init" do
128 source "tilekiln-storage-init.erb"
132 variables :tilekiln_bin => "#{tilekiln_directory}/bin/tilekiln", :storage_database => "tiles", :config_path => "#{shortbread_config}"
135 postgresql_user "tomh" do
136 cluster node[:vectortile][:database][:cluster]
140 postgresql_user "pnorman" do
141 cluster node[:vectortile][:database][:cluster]
145 postgresql_user "tilekiln" do
146 cluster node[:vectortile][:database][:cluster]
149 postgresql_user "tileupdate" do
150 cluster node[:vectortile][:database][:cluster]
153 postgresql_database "tiles" do
154 cluster node[:vectortile][:database][:cluster]
158 postgresql_database "spirit" do
159 cluster node[:vectortile][:database][:cluster]
163 postgresql_extension "postgis" do
164 cluster node[:vectortile][:database][:cluster]
168 postgresql_schema "tilekiln" do
169 cluster node[:vectortile][:database][:cluster]
172 permissions "tileupdate" => :all, "tilekiln" => :usage
173 notifies :run, "execute[tilekiln-storage-init]", :immediately
176 execute "tilekiln-storage-init" do
178 command "/usr/local/bin/tilekiln-storage-init"
182 %w[metadata shortbread_v1].each do |table|
183 postgresql_table table do
184 cluster node[:vectortile][:database][:cluster]
188 permissions "tileupdate" => :all, "tilekiln" => :select
192 (0..14).each do |zoom|
193 postgresql_table "shortbread_v1_z#{zoom}" do
194 cluster node[:vectortile][:database][:cluster]
198 permissions "tileupdate" => :all, "tilekiln" => node[:vectortile][:serve][:mode] == :live ? [:select, :insert, :update] : :select
202 %w[addresses aerialways aeroways boundaries boundary_labels bridges buildings
203 dam_lines dam_polygons ferries land pier_lines pier_polygons place_labels
204 planet_osm_nodes planet_osm_rels planet_osm_ways pois public_transport railways
205 road_routes roads sites street_polygons streets_labels_points
206 streets_polygons_labels water_area_labels water_areas water_lines water_lines_labels].each do |table|
207 postgresql_table table do
208 cluster node[:vectortile][:database][:cluster]
212 permissions "tileupdate" => :all, "tilekiln" => :select
216 tilekiln_mode = node[:vectortile][:serve][:mode] == :live ? "live --config #{shortbread_config} --source-dbname spirit" : "static"
218 systemd_service "tilekiln" do
219 description "Tilekiln vector tile server"
221 after "postgresql.service"
222 wants "postgresql.service"
223 sandbox :enable_network => true
224 restrict_address_families "AF_UNIX"
225 exec_start "#{tilekiln_directory}/bin/tilekiln serve #{tilekiln_mode} --storage-dbname tiles --num-threads #{node[:vectortile][:serve][:threads]}"
228 service "tilekiln" do
229 action [:enable, :start]
232 execute "/srv/vector.openstreetmap.org/spirit/scripts/get-external-data.py" do
233 command "/srv/vector.openstreetmap.org/spirit/scripts/get-external-data.py -R tilekiln"
234 cwd "/srv/vector.openstreetmap.org/spirit"
240 template "/usr/local/bin/vector-update" do
241 source node[:vectortile][:replication][:tileupdate] == :enabled ? "vector-update-tile.erb" : "vector-update-notile.erb"
245 variables :tilekiln_bin => "#{tilekiln_directory}/bin/tilekiln", :source_database => "spirit", :config_path => "#{shortbread_config}", :diff_size => "1000", :tiles_file => "/srv/vector.openstreetmap.org/data/tiles.txt", :post_processing => "/usr/local/bin/tiles-rerender"
248 template "/usr/local/bin/tiles-rerender" do
249 source "tiles-rerender.erb"
253 variables :tilekiln_bin => "#{tilekiln_directory}/bin/tilekiln", :source_database => "spirit", :storage_database => "tiles", :config_path => "#{shortbread_config}", :tiles_file => "/srv/vector.openstreetmap.org/data/tiles.txt", :update_threads => 4
256 systemd_service "replicate" do
257 description "Get replication updates"
259 after "postgresql.service"
260 wants "postgresql.service"
261 sandbox :enable_network => true
262 restrict_address_families "AF_UNIX"
263 read_write_paths ["/srv/vector.openstreetmap.org/data/"]
264 exec_start "/usr/local/bin/vector-update"
267 systemd_timer "replicate" do
268 description "Get replication updates"
270 on_unit_active_sec 30
274 if node[:vectortile][:replication][:status] == :enabled
275 service "replicate.timer" do
276 action [:enable, :start]
279 service "replicate.timer" do
280 action [:stop, :disable]