]> git.openstreetmap.org Git - chef.git/blob - cookbooks/web/definitions/rails_port.rb
Configure the thunderforest API key
[chef.git] / cookbooks / web / definitions / rails_port.rb
1 #
2 # Cookbook Name:: web
3 # Definition:: rails_port
4 #
5 # Copyright 2012, 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 require "yaml"
21
22 define :rails_port, :action => [:create, :enable] do
23   name = params[:name]
24   ruby_version = params[:ruby] || "1.9.1"
25   rails_directory = params[:directory] || "/srv/#{name}"
26   rails_user = params[:user]
27   rails_group = params[:group]
28   rails_repository = params[:repository] || "git://git.openstreetmap.org/rails.git"
29   rails_revision = params[:revision] || "live"
30   run_migrations = params[:run_migrations] || false
31   status = params[:status] || "online"
32
33   database_params = {
34     :host => params[:database_host],
35     :port => params[:database_port],
36     :name => params[:database_name],
37     :username => params[:database_username],
38     :password => params[:database_password]
39   }
40
41   package "ruby#{ruby_version}"
42   package "ruby#{ruby_version}-dev"
43   package "rubygems#{ruby_version}" if ruby_version.to_f < 1.9
44   package "irb#{ruby_version}" if ruby_version.to_f < 1.9
45   package "imagemagick"
46   package "nodejs"
47
48   package "g++"
49   package "pkg-config"
50   package "libpq-dev"
51   package "libsasl2-dev"
52   package "libxml2-dev"
53   package "libxslt1-dev"
54   package "libmemcached-dev"
55
56   gem_package "bundler#{ruby_version}" do
57     package_name "bundler"
58     version "1.3.5"
59     gem_binary "gem#{ruby_version}"
60     options "--format-executable"
61   end
62
63   file "/usr/lib/ruby/1.8/rack.rb" do
64     action :delete
65   end
66
67   directory "/usr/lib/ruby/1.8/rack" do
68     action :delete
69     recursive true
70   end
71
72   directory rails_directory do
73     owner rails_user
74     group rails_group
75     mode 0o2775
76   end
77
78   git rails_directory do
79     action :sync
80     repository rails_repository
81     revision rails_revision
82     user rails_user
83     group rails_group
84     notifies :run, "execute[#{rails_directory}/Gemfile]"
85     notifies :run, "execute[#{rails_directory}/public/assets]"
86     notifies :delete, "file[#{rails_directory}/public/export/embed.html]"
87     notifies :run, "execute[#{rails_directory}]"
88   end
89
90   directory "#{rails_directory}/tmp" do
91     owner rails_user
92     group rails_group
93   end
94
95   file "#{rails_directory}/config/environment.rb" do
96     owner rails_user
97     group rails_group
98   end
99
100   template "#{rails_directory}/config/database.yml" do
101     cookbook "web"
102     source "database.yml.erb"
103     owner rails_user
104     group rails_group
105     mode 0o664
106     variables database_params
107     notifies :run, "execute[#{rails_directory}]"
108   end
109
110   application_yml = edit_file "#{rails_directory}/config/example.application.yml" do |line|
111     line.gsub!(/^( *)server_url:.*$/, "\\1server_url: \"#{name}\"")
112
113     if params[:email_from]
114       line.gsub!(/^( *)email_from:.*$/, "\\1email_from: \"#{params[:email_from]}\"")
115     end
116
117     line.gsub!(/^( *)status:.*$/, "\\1status: :#{status}")
118
119     if params[:messages_domain]
120       line.gsub!(/^( *)#messages_domain:.*$/, "\\1messages_domain: \"#{params[:messages_domain]}\"")
121     end
122
123     line.gsub!(/^( *)#geonames_username:.*$/, "\\1geonames_username: \"openstreetmap\"")
124
125     if params[:quova_username]
126       line.gsub!(/^( *)#quova_username:.*$/, "\\1quova_username: \"#{params[:quova_username]}\"")
127       line.gsub!(/^( *)#quova_password:.*$/, "\\1quova_password: \"#{params[:quova_password]}\"")
128     end
129
130     if params[:gpx_dir]
131       line.gsub!(/^( *)gpx_trace_dir:.*$/, "\\1gpx_trace_dir: \"#{params[:gpx_dir]}/traces\"")
132       line.gsub!(/^( *)gpx_image_dir:.*$/, "\\1gpx_image_dir: \"#{params[:gpx_dir]}/images\"")
133     end
134
135     if params[:attachments_dir]
136       line.gsub!(/^( *)attachments_dir:.*$/, "\\1attachments_dir: \"#{params[:attachments_dir]}\"")
137     end
138
139     if params[:log_path]
140       line.gsub!(/^( *)#log_path:.*$/, "\\1log_path: \"#{params[:log_path]}\"")
141     end
142
143     if params[:logstash_path]
144       line.gsub!(/^( *)#logstash_path:.*$/, "\\1logstash_path: \"#{params[:logstash_path]}\"")
145     end
146
147     if params[:memcache_servers]
148       line.gsub!(/^( *)#memcache_servers:.*$/, "\\1memcache_servers: [ \"#{params[:memcache_servers].join('", "')}\" ]")
149     end
150
151     if params[:potlatch2_key]
152       line.gsub!(/^( *)#potlatch2_key:.*$/, "\\1potlatch2_key: \"#{params[:potlatch2_key]}\"")
153     end
154
155     if params[:id_key]
156       line.gsub!(/^( *)#id_key:.*$/, "\\1id_key: \"#{params[:id_key]}\"")
157     end
158
159     if params[:oauth_key]
160       line.gsub!(/^( *)#oauth_key:.*$/, "\\1oauth_key: \"#{params[:oauth_key]}\"")
161     end
162
163     if params[:nominatim_url]
164       line.gsub!(/^( *)nominatim_url:.*$/, "\\1nominatim_url: \"#{params[:nominatim_url]}\"")
165     end
166
167     if params[:osrm_url]
168       line.gsub!(/^( *)osrm_url:.*$/, "\\1osrm_url: \"#{params[:osrm_url]}\"")
169     end
170
171     if params[:google_auth_id]
172       line.gsub!(/^( *)#google_auth_id:.*$/, "\\1google_auth_id: \"#{params[:google_auth_id]}\"")
173       line.gsub!(/^( *)#google_auth_secret:.*$/, "\\1google_auth_secret: \"#{params[:google_auth_secret]}\"")
174       line.gsub!(/^( *)#google_openid_realm:.*$/, "\\1google_openid_realm: \"#{params[:google_openid_realm]}\"")
175     end
176
177     if params[:facebook_auth_id]
178       line.gsub!(/^( *)#facebook_auth_id:.*$/, "\\1facebook_auth_id: \"#{params[:facebook_auth_id]}\"")
179       line.gsub!(/^( *)#facebook_auth_secret:.*$/, "\\1facebook_auth_secret: \"#{params[:facebook_auth_secret]}\"")
180     end
181
182     if params[:windowslive_auth_id]
183       line.gsub!(/^( *)#windowslive_auth_id:.*$/, "\\1windowslive_auth_id: \"#{params[:windowslive_auth_id]}\"")
184       line.gsub!(/^( *)#windowslive_auth_secret:.*$/, "\\1windowslive_auth_secret: \"#{params[:windowslive_auth_secret]}\"")
185     end
186
187     if params[:github_auth_id]
188       line.gsub!(/^( *)#github_auth_id:.*$/, "\\1github_auth_id: \"#{params[:github_auth_id]}\"")
189       line.gsub!(/^( *)#github_auth_secret:.*$/, "\\1github_auth_secret: \"#{params[:github_auth_secret]}\"")
190     end
191
192     if params[:mapquest_key]
193       line.gsub!(/^( *)#mapquest_key:.*$/, "\\1mapquest_key: \"#{params[:mapquest_key]}\"")
194     end
195
196     if params[:mapzen_valhalla_key]
197       line.gsub!(/^( *)#mapzen_valhalla_key:.*$/, "\\1mapzen_valhalla_key: \"#{params[:mapzen_valhalla_key]}\"")
198     end
199
200     if params[:thunderforest_key]
201       line.gsub!(/^( *)#thunderforest_key:.*$/, "\\1thunderforest_key: \"#{params[:thunderforest_key]}\"")
202     end
203
204     line.gsub!(/^( *)require_terms_seen:.*$/, "\\1require_terms_seen: true")
205     line.gsub!(/^( *)require_terms_agreed:.*$/, "\\1require_terms_agreed: true")
206
207     line
208   end
209
210   file "#{rails_directory}/config/application.yml" do
211     owner rails_user
212     group rails_group
213     mode 0o664
214     content application_yml
215     notifies :run, "execute[#{rails_directory}/public/assets]"
216   end
217
218   if params[:piwik_configuration]
219     file "#{rails_directory}/config/piwik.yml" do
220       owner rails_user
221       group rails_group
222       mode 0o664
223       content YAML.dump(params[:piwik_configuration])
224       notifies :run, "execute[#{rails_directory}/public/assets]"
225     end
226   else
227     file "#{rails_directory}/config/piwik.yml" do
228       action :delete
229       notifies :run, "execute[#{rails_directory}/public/assets]"
230     end
231   end
232
233   execute "#{rails_directory}/Gemfile" do
234     action :nothing
235     command "bundle#{ruby_version} install"
236     cwd rails_directory
237     user "root"
238     group "root"
239     environment "NOKOGIRI_USE_SYSTEM_LIBRARIES" => "yes"
240     subscribes :run, "gem_package[bundler#{ruby_version}]"
241     notifies :run, "execute[#{rails_directory}]"
242   end
243
244   execute "#{rails_directory}/db/migrate" do
245     action :nothing
246     command "bundle#{ruby_version} exec rake db:migrate"
247     cwd rails_directory
248     user rails_user
249     group rails_group
250     subscribes :run, "git[#{rails_directory}]"
251     notifies :run, "execute[#{rails_directory}]"
252     only_if { run_migrations }
253   end
254
255   execute "#{rails_directory}/public/assets" do
256     action :nothing
257     command "bundle#{ruby_version} exec rake assets:precompile"
258     environment "RAILS_ENV" => "production"
259     cwd rails_directory
260     user rails_user
261     group rails_group
262     notifies :run, "execute[#{rails_directory}]"
263   end
264
265   file "#{rails_directory}/public/export/embed.html" do
266     action :nothing
267   end
268
269   execute "#{rails_directory}/lib/quad_tile/extconf.rb" do
270     command "ruby extconf.rb"
271     cwd "#{rails_directory}/lib/quad_tile"
272     user rails_user
273     group rails_group
274     not_if { File.exist?("#{rails_directory}/lib/quad_tile/Makefile") && File.mtime("#{rails_directory}/lib/quad_tile/Makefile") >= File.mtime("#{rails_directory}/lib/quad_tile/extconf.rb") }
275   end
276
277   execute "#{rails_directory}/lib/quad_tile/Makefile" do
278     command "make"
279     cwd "#{rails_directory}/lib/quad_tile"
280     user rails_user
281     group rails_group
282     not_if do
283       File.exist?("#{rails_directory}/lib/quad_tile/quad_tile_so.so") &&
284         File.mtime("#{rails_directory}/lib/quad_tile/quad_tile_so.so") >= File.mtime("#{rails_directory}/lib/quad_tile/Makefile") &&
285         File.mtime("#{rails_directory}/lib/quad_tile/quad_tile_so.so") >= File.mtime("#{rails_directory}/lib/quad_tile/quad_tile.c") &&
286         File.mtime("#{rails_directory}/lib/quad_tile/quad_tile_so.so") >= File.mtime("#{rails_directory}/lib/quad_tile/quad_tile.h")
287     end
288     notifies :run, "execute[#{rails_directory}]"
289   end
290
291   execute rails_directory do
292     action :nothing
293     command "passenger-config restart-app --ignore-app-not-running #{rails_directory}"
294     user "root"
295     group "root"
296     only_if { File.exist?("/usr/bin/passenger-config") }
297   end
298
299   template "/etc/cron.daily/rails-#{name.tr('.', '-')}" do
300     cookbook "web"
301     source "rails.cron.erb"
302     owner "root"
303     group "root"
304     mode 0o755
305     variables :directory => rails_directory
306   end
307 end