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