]> git.openstreetmap.org Git - chef.git/blob - cookbooks/nominatim/recipes/base.rb
9dd0f1c1fcfd80d7bee959c9dc0f8cb865251794
[chef.git] / cookbooks / nominatim / recipes / base.rb
1 #
2 # Cookbook Name:: nominatim
3 # Recipe:: base
4 #
5 # Copyright 2015, OpenStreetMap Foundation
6 #
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
10 #
11 #     http://www.apache.org/licenses/LICENSE-2.0
12 #
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.
18 #
19
20 include_recipe "apache::ssl"
21 include_recipe "postgresql"
22 include_recipe "git"
23
24 package "php5"
25 package "php5-cli"
26 package "php5-pgsql"
27 package "php5-fpm"
28 package "php-pear"
29 package "php-apc"
30
31 apache_module "rewrite"
32 apache_module "proxy"
33 apache_module "proxy_fcgi"
34
35 home_directory = data_bag_item("accounts", "nominatim")["home"]
36 source_directory = "#{home_directory}/nominatim"
37 email_errors = data_bag_item("accounts", "lonvia")["email"]
38
39 database_cluster = node[:nominatim][:database][:cluster]
40 database_version = database_cluster.sub(%r{/.*}, "")
41 database_name = node[:nominatim][:database][:dbname]
42
43 postgis_version = node[:nominatim][:database][:postgis]
44
45 service "php5-fpm" do
46   provider Chef::Provider::Service::Upstart
47   action [:enable, :start]
48   supports :status => true, :restart => true, :reload => true
49 end
50
51 apache_site "nominatim.openstreetmap.org" do
52   template "apache.erb"
53   directory source_directory
54   variables :pools => node[:nominatim][:fpm_pools]
55 end
56
57 apache_site "default" do
58   action [:disable]
59 end
60
61 node[:nominatim][:fpm_pools].each do |name, data|
62   template "/etc/php5/fpm/pool.d/#{name}.conf" do
63     source "fpm.conf.erb"
64     owner "root"
65     group "root"
66     mode 0644
67     variables data.merge(:name => name, :port => data[:port])
68     notifies :reload, "service[php5-fpm]"
69   end
70 end
71
72 postgresql_munin "nominatim" do
73   cluster database_cluster
74   database database_name
75 end
76
77 directory "/var/log/nominatim" do
78   owner "nominatim"
79   group "nominatim"
80   mode 0755
81 end
82
83 template "/etc/logrotate.d/nominatim" do
84   source "logrotate.nominatim.erb"
85   owner "root"
86   group "root"
87   mode 0644
88 end
89
90 package "osmosis"
91 package "gcc"
92 package "proj-bin"
93 package "libgeos-c1"
94 package "postgresql-#{database_version}-postgis-#{postgis_version}"
95 package "postgresql-server-dev-#{database_version}"
96 package "build-essential"
97 package "libxml2-dev"
98 package "libgeos-dev"
99 package "libgeos++-dev"
100 package "libpq-dev"
101 package "libbz2-dev"
102 package "libtool"
103 package "automake"
104 package "libproj-dev"
105 package "libprotobuf-c0-dev"
106 package "protobuf-c-compiler"
107 package "python-psycopg2"
108 package "libboost-dev"
109 package "libboost-system-dev"
110 package "libboost-filesystem-dev"
111 package "libboost-thread-dev"
112
113 execute "php-pear-db" do
114   command "pear install DB"
115   not_if { File.exist?("/usr/share/php/DB") }
116 end
117
118 execute "compile_nominatim" do
119   action :nothing
120   command "cd #{source_directory} && ./autogen.sh && ./configure && make"
121   user "nominatim"
122 end
123
124 directory "#{source_directory}/log" do
125   owner "nominatim"
126   group "nominatim"
127   mode 0755
128 end
129
130 template "#{source_directory}/.git/hooks/post-merge" do
131   source "update_source.erb"
132   owner "nominatim"
133   group "nominatim"
134   mode 0755
135   variables :source_directory => source_directory
136 end
137
138 template "#{source_directory}/settings/local.php" do
139   source "nominatim.erb"
140   owner "nominatim"
141   group "nominatim"
142   mode 0664
143   variables :postgres_version => database_version
144 end
145
146 template "#{source_directory}/settings/ip_blocks.conf" do
147   action :create_if_missing
148   source "ipblocks.erb"
149   owner "nominatim"
150   group "nominatim"
151   mode 0664
152 end
153
154 file "#{source_directory}/settings/apache_blocks.conf" do
155   action :create_if_missing
156   owner "nominatim"
157   group "nominatim"
158   mode 0664
159 end
160
161 file "#{source_directory}/settings/ip_blocks.map" do
162   action :create_if_missing
163   owner "nominatim"
164   group "nominatim"
165   mode 0664
166 end
167
168 if node[:nominatim][:enabled]
169   cron_action = :create
170 else
171   cron_action = :delete
172 end
173
174 template "/etc/cron.d/nominatim" do
175   action cron_action
176   source "cron.erb"
177   owner "root"
178   group "root"
179   mode "0644"
180   variables :bin_directory => "#{source_directory}/utils", :mailto => email_errors
181 end
182
183 template "#{source_directory}/utils/nominatim-update" do
184   source "updater.erb"
185   user "nominatim"
186   group "nominatim"
187   mode 0755
188 end
189
190 template "/etc/init.d/nominatim-update" do
191   source "updater.init.erb"
192   user "nominatim"
193   group "nominatim"
194   mode 0755
195   variables :source_directory => source_directory
196 end
197
198 munin_plugin_conf "nominatim" do
199   template "munin.erb"
200 end
201
202 munin_plugin "nominatim_importlag" do
203   target "#{source_directory}/munin/nominatim_importlag"
204 end
205
206 munin_plugin "nominatim_query_speed" do
207   target "#{source_directory}/munin/nominatim_query_speed_querylog"
208 end
209
210 munin_plugin "nominatim_requests" do
211   target "#{source_directory}/munin/nominatim_requests_querylog"
212 end
213
214 munin_plugin "nominatim_throttled_ips" do
215   target "#{source_directory}/munin/nominatim_throttled_ips"
216 end
217
218 external_data = [
219   "wikipedia_article.sql.bin",
220   "wikipedia_redirect.sql.bin",
221   "gb_postcode_data.sql.gz"
222 ]
223
224 external_data.each do |fname|
225   remote_file "#{source_directory}/data/#{fname}" do
226     action :create_if_missing
227     source "http://www.nominatim.org/data/#{fname}"
228     owner "nominatim"
229     group "nominatim"
230     mode 0644
231   end
232 end
233
234 additional_scripts = %w(backup-nominatim clean-db-nominatim)
235
236 additional_scripts.each do |fname|
237   template "/usr/local/bin/#{fname}" do
238     source "#{fname}.erb"
239     owner "root"
240     group "root"
241     mode 0755
242   end
243 end
244
245 directory File.dirname(node[:nominatim][:flatnode_file]) do
246   owner "nominatim"
247   group "nominatim"
248   mode 0755
249   recursive true
250 end
251
252 directory "/data/postgresql-archive" do
253   owner "postgres"
254   group "postgres"
255   mode 0700
256   only_if { node[:postgresql][:settings][:defaults][:archive_mode] == "on" }
257 end
258
259 fail2ban_filter "nominatim" do
260   failregex '^<HOST> - - \[[^]]+\] "[^"]+" (403|429) '
261 end
262
263 fail2ban_jail "nominatim" do
264   filter "nominatim"
265   logpath "/var/log/apache2/nominatim.openstreetmap.org-access.log"
266   ports [80, 443]
267   maxretry 100
268 end