Configure the publisher URL for rails port instances
[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_protocol:.*$/, "\\1server_protocol: \"https\"")
113     line.gsub!(/^( *)#server_url:.*$/, "\\1server_url: \"#{name}\"")
114
115     line.gsub!(/^( *)#publisher_url:.*$/, "\\1publisher_url: \"https://plus.google.com/111953119785824514010\"")
116
117     if params[:email_from]
118       line.gsub!(/^( *)email_from:.*$/, "\\1email_from: \"#{params[:email_from]}\"")
119     end
120
121     line.gsub!(/^( *)status:.*$/, "\\1status: :#{status}")
122
123     if params[:messages_domain]
124       line.gsub!(/^( *)#messages_domain:.*$/, "\\1messages_domain: \"#{params[:messages_domain]}\"")
125     end
126
127     line.gsub!(/^( *)#geonames_username:.*$/, "\\1geonames_username: \"openstreetmap\"")
128
129     line.gsub!(/^( *)#geoip_database:.*$/, "\\1geoip_database: \"/usr/share/GeoIP/GeoIPv6.dat\"")
130
131     if params[:gpx_dir]
132       line.gsub!(/^( *)gpx_trace_dir:.*$/, "\\1gpx_trace_dir: \"#{params[:gpx_dir]}/traces\"")
133       line.gsub!(/^( *)gpx_image_dir:.*$/, "\\1gpx_image_dir: \"#{params[:gpx_dir]}/images\"")
134     end
135
136     if params[:attachments_dir]
137       line.gsub!(/^( *)attachments_dir:.*$/, "\\1attachments_dir: \"#{params[:attachments_dir]}\"")
138     end
139
140     if params[:log_path]
141       line.gsub!(/^( *)#log_path:.*$/, "\\1log_path: \"#{params[:log_path]}\"")
142     end
143
144     if params[:logstash_path]
145       line.gsub!(/^( *)#logstash_path:.*$/, "\\1logstash_path: \"#{params[:logstash_path]}\"")
146     end
147
148     if params[:memcache_servers]
149       line.gsub!(/^( *)#memcache_servers:.*$/, "\\1memcache_servers: [ \"#{params[:memcache_servers].join('", "')}\" ]")
150     end
151
152     if params[:potlatch2_key]
153       line.gsub!(/^( *)#potlatch2_key:.*$/, "\\1potlatch2_key: \"#{params[:potlatch2_key]}\"")
154     end
155
156     if params[:id_key]
157       line.gsub!(/^( *)#id_key:.*$/, "\\1id_key: \"#{params[:id_key]}\"")
158     end
159
160     if params[:oauth_key]
161       line.gsub!(/^( *)#oauth_key:.*$/, "\\1oauth_key: \"#{params[:oauth_key]}\"")
162     end
163
164     if params[:nominatim_url]
165       line.gsub!(/^( *)nominatim_url:.*$/, "\\1nominatim_url: \"#{params[:nominatim_url]}\"")
166     end
167
168     if params[:osrm_url]
169       line.gsub!(/^( *)osrm_url:.*$/, "\\1osrm_url: \"#{params[:osrm_url]}\"")
170     end
171
172     if params[:google_auth_id]
173       line.gsub!(/^( *)#google_auth_id:.*$/, "\\1google_auth_id: \"#{params[:google_auth_id]}\"")
174       line.gsub!(/^( *)#google_auth_secret:.*$/, "\\1google_auth_secret: \"#{params[:google_auth_secret]}\"")
175       line.gsub!(/^( *)#google_openid_realm:.*$/, "\\1google_openid_realm: \"#{params[:google_openid_realm]}\"")
176     end
177
178     if params[:facebook_auth_id]
179       line.gsub!(/^( *)#facebook_auth_id:.*$/, "\\1facebook_auth_id: \"#{params[:facebook_auth_id]}\"")
180       line.gsub!(/^( *)#facebook_auth_secret:.*$/, "\\1facebook_auth_secret: \"#{params[:facebook_auth_secret]}\"")
181     end
182
183     if params[:windowslive_auth_id]
184       line.gsub!(/^( *)#windowslive_auth_id:.*$/, "\\1windowslive_auth_id: \"#{params[:windowslive_auth_id]}\"")
185       line.gsub!(/^( *)#windowslive_auth_secret:.*$/, "\\1windowslive_auth_secret: \"#{params[:windowslive_auth_secret]}\"")
186     end
187
188     if params[:github_auth_id]
189       line.gsub!(/^( *)#github_auth_id:.*$/, "\\1github_auth_id: \"#{params[:github_auth_id]}\"")
190       line.gsub!(/^( *)#github_auth_secret:.*$/, "\\1github_auth_secret: \"#{params[:github_auth_secret]}\"")
191     end
192
193     if params[:wikipedia_auth_id]
194       line.gsub!(/^( *)#wikipedia_auth_id:.*$/, "\\1wikipedia_auth_id: \"#{params[:wikipedia_auth_id]}\"")
195       line.gsub!(/^( *)#wikipedia_auth_secret:.*$/, "\\1wikipedia_auth_secret: \"#{params[:wikipedia_auth_secret]}\"")
196     end
197
198     if params[:mapquest_key]
199       line.gsub!(/^( *)#mapquest_key:.*$/, "\\1mapquest_key: \"#{params[:mapquest_key]}\"")
200     end
201
202     if params[:mapzen_valhalla_key]
203       line.gsub!(/^( *)#mapzen_valhalla_key:.*$/, "\\1mapzen_valhalla_key: \"#{params[:mapzen_valhalla_key]}\"")
204     end
205
206     if params[:thunderforest_key]
207       line.gsub!(/^( *)#thunderforest_key:.*$/, "\\1thunderforest_key: \"#{params[:thunderforest_key]}\"")
208     end
209
210     if params[:totp_key]
211       line.gsub!(/^( *)#totp_key:.*$/, "\\1totp_key: \"#{params[:totp_key]}\"")
212     end
213
214     line.gsub!(/^( *)require_terms_seen:.*$/, "\\1require_terms_seen: true")
215     line.gsub!(/^( *)require_terms_agreed:.*$/, "\\1require_terms_agreed: true")
216
217     line
218   end
219
220   file "#{rails_directory}/config/application.yml" do
221     owner rails_user
222     group rails_group
223     mode 0o664
224     content application_yml
225     notifies :run, "execute[#{rails_directory}/public/assets]"
226   end
227
228   if params[:piwik_configuration]
229     file "#{rails_directory}/config/piwik.yml" do
230       owner rails_user
231       group rails_group
232       mode 0o664
233       content YAML.dump(params[:piwik_configuration])
234       notifies :run, "execute[#{rails_directory}/public/assets]"
235     end
236   else
237     file "#{rails_directory}/config/piwik.yml" do
238       action :delete
239       notifies :run, "execute[#{rails_directory}/public/assets]"
240     end
241   end
242
243   execute "#{rails_directory}/Gemfile" do
244     action :nothing
245     command "bundle#{ruby_version} install"
246     cwd rails_directory
247     user "root"
248     group "root"
249     environment "NOKOGIRI_USE_SYSTEM_LIBRARIES" => "yes"
250     subscribes :run, "gem_package[bundler#{ruby_version}]"
251     notifies :run, "execute[#{rails_directory}]"
252   end
253
254   execute "#{rails_directory}/db/migrate" do
255     action :nothing
256     command "bundle#{ruby_version} exec rake db:migrate"
257     cwd rails_directory
258     user rails_user
259     group rails_group
260     subscribes :run, "git[#{rails_directory}]"
261     notifies :run, "execute[#{rails_directory}]"
262     only_if { run_migrations }
263   end
264
265   execute "#{rails_directory}/public/assets" do
266     action :nothing
267     command "bundle#{ruby_version} exec rake assets:precompile"
268     environment "RAILS_ENV" => "production"
269     cwd rails_directory
270     user rails_user
271     group rails_group
272     notifies :run, "execute[#{rails_directory}]"
273   end
274
275   file "#{rails_directory}/public/export/embed.html" do
276     action :nothing
277   end
278
279   execute "#{rails_directory}/lib/quad_tile/extconf.rb" do
280     command "ruby extconf.rb"
281     cwd "#{rails_directory}/lib/quad_tile"
282     user rails_user
283     group rails_group
284     not_if do
285       File.exist?("#{rails_directory}/lib/quad_tile/quad_tile_so.so") &&
286         File.mtime("#{rails_directory}/lib/quad_tile/quad_tile_so.so") >= File.mtime("#{rails_directory}/lib/quad_tile/extconf.rb") &&
287         File.mtime("#{rails_directory}/lib/quad_tile/quad_tile_so.so") >= File.mtime("#{rails_directory}/lib/quad_tile/quad_tile.c") &&
288         File.mtime("#{rails_directory}/lib/quad_tile/quad_tile_so.so") >= File.mtime("#{rails_directory}/lib/quad_tile/quad_tile.h")
289     end
290     notifies :run, "execute[#{rails_directory}/lib/quad_tile/Makefile]"
291   end
292
293   execute "#{rails_directory}/lib/quad_tile/Makefile" do
294     action :nothing
295     command "make"
296     cwd "#{rails_directory}/lib/quad_tile"
297     user rails_user
298     group rails_group
299     notifies :run, "execute[#{rails_directory}]"
300   end
301
302   execute rails_directory do
303     action :nothing
304     command "passenger-config restart-app --ignore-app-not-running #{rails_directory}"
305     user "root"
306     group "root"
307     only_if { File.exist?("/usr/bin/passenger-config") }
308   end
309
310   template "/etc/cron.daily/rails-#{name.tr('.', '-')}" do
311     cookbook "web"
312     source "rails.cron.erb"
313     owner "root"
314     group "root"
315     mode 0o755
316     variables :directory => rails_directory
317   end
318 end