3 # Definition:: rails_port
5 # Copyright 2012, 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 # http://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.
22 define :rails_port, :action => [:create, :enable] do
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"
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]
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
51 package "libsasl2-dev"
53 package "libxslt1-dev"
54 package "libmemcached-dev"
56 gem_package "bundler#{ruby_version}" do
57 package_name "bundler"
59 gem_binary "gem#{ruby_version}"
60 options "--format-executable"
63 file "/usr/lib/ruby/1.8/rack.rb" do
67 directory "/usr/lib/ruby/1.8/rack" do
72 directory rails_directory do
78 git rails_directory do
80 repository rails_repository
81 revision rails_revision
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}]"
90 directory "#{rails_directory}/tmp" do
95 file "#{rails_directory}/config/environment.rb" do
100 template "#{rails_directory}/config/database.yml" do
102 source "database.yml.erb"
106 variables database_params
107 notifies :run, "execute[#{rails_directory}]"
110 application_yml = edit_file "#{rails_directory}/config/example.application.yml" do |line|
111 line.gsub!(/^( *)server_url:.*$/, "\\1server_url: \"#{name}\"")
113 if params[:email_from]
114 line.gsub!(/^( *)email_from:.*$/, "\\1email_from: \"#{params[:email_from]}\"")
117 line.gsub!(/^( *)status:.*$/, "\\1status: :#{status}")
119 if params[:messages_domain]
120 line.gsub!(/^( *)#messages_domain:.*$/, "\\1messages_domain: \"#{params[:messages_domain]}\"")
123 line.gsub!(/^( *)#geonames_username:.*$/, "\\1geonames_username: \"openstreetmap\"")
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]}\"")
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\"")
135 if params[:attachments_dir]
136 line.gsub!(/^( *)attachments_dir:.*$/, "\\1attachments_dir: \"#{params[:attachments_dir]}\"")
140 line.gsub!(/^( *)#log_path:.*$/, "\\1log_path: \"#{params[:log_path]}\"")
143 if params[:logstash_path]
144 line.gsub!(/^( *)#logstash_path:.*$/, "\\1logstash_path: \"#{params[:logstash_path]}\"")
147 if params[:memcache_servers]
148 line.gsub!(/^( *)#memcache_servers:.*$/, "\\1memcache_servers: [ \"#{params[:memcache_servers].join("\", \"")}\" ]")
151 if params[:potlatch2_key]
152 line.gsub!(/^( *)#potlatch2_key:.*$/, "\\1potlatch2_key: \"#{params[:potlatch2_key]}\"")
156 line.gsub!(/^( *)#id_key:.*$/, "\\1id_key: \"#{params[:id_key]}\"")
159 if params[:oauth_key]
160 line.gsub!(/^( *)#oauth_key:.*$/, "\\1oauth_key: \"#{params[:oauth_key]}\"")
163 if params[:nominatim_url]
164 line.gsub!(/^( *)nominatim_url:.*$/, "\\1nominatim_url: \"#{params[:nominatim_url]}\"")
167 if params[:google_auth_id]
168 line.gsub!(/^( *)#google_auth_id:.*$/, "\\1google_auth_id: \"#{params[:google_auth_id]}\"")
169 line.gsub!(/^( *)#google_auth_secret:.*$/, "\\1google_auth_secret: \"#{params[:google_auth_secret]}\"")
170 line.gsub!(/^( *)#google_openid_realm:.*$/, "\\1google_openid_realm: \"#{params[:google_openid_realm]}\"")
173 if params[:facebook_auth_id]
174 line.gsub!(/^( *)#facebook_auth_id:.*$/, "\\1facebook_auth_id: \"#{params[:facebook_auth_id]}\"")
175 line.gsub!(/^( *)#facebook_auth_secret:.*$/, "\\1facebook_auth_secret: \"#{params[:facebook_auth_secret]}\"")
178 if params[:windowslive_auth_id]
179 line.gsub!(/^( *)#windowslive_auth_id:.*$/, "\\1windowslive_auth_id: \"#{params[:windowslive_auth_id]}\"")
180 line.gsub!(/^( *)#windowslive_auth_secret:.*$/, "\\1windowslive_auth_secret: \"#{params[:windowslive_auth_secret]}\"")
183 if params[:mapquest_key]
184 line.gsub!(/^( *)#mapquest_key:.*$/, "\\1mapquest_key: \"#{params[:mapquest_key]}\"")
187 if params[:mapzen_valhalla_key]
188 line.gsub!(/^( *)#mapzen_valhalla_key:.*$/, "\\1mapzen_valhalla_key: \"#{params[:mapzen_valhalla_key]}\"")
191 line.gsub!(/^( *)require_terms_seen:.*$/, "\\1require_terms_seen: true")
192 line.gsub!(/^( *)require_terms_agreed:.*$/, "\\1require_terms_agreed: true")
197 file "#{rails_directory}/config/application.yml" do
201 content application_yml
202 notifies :run, "execute[#{rails_directory}/public/assets]"
205 if params[:piwik_configuration]
206 file "#{rails_directory}/config/piwik.yml" do
210 content YAML.dump(params[:piwik_configuration])
211 notifies :run, "execute[#{rails_directory}/public/assets]"
214 file "#{rails_directory}/config/piwik.yml" do
216 notifies :run, "execute[#{rails_directory}/public/assets]"
220 execute "#{rails_directory}/Gemfile" do
222 command "bundle#{ruby_version} install"
226 environment "NOKOGIRI_USE_SYSTEM_LIBRARIES" => "yes"
227 subscribes :run, "gem_package[bundler#{ruby_version}]"
228 notifies :run, "execute[#{rails_directory}]"
231 execute "#{rails_directory}/db/migrate" do
233 command "bundle#{ruby_version} exec rake#{ruby_version} db:migrate"
237 subscribes :run, "git[#{rails_directory}]"
238 notifies :run, "execute[#{rails_directory}]"
239 only_if { run_migrations }
242 execute "#{rails_directory}/public/assets" do
244 command "bundle#{ruby_version} exec rake#{ruby_version} assets:precompile"
245 environment "RAILS_ENV" => "production"
249 notifies :run, "execute[#{rails_directory}]"
252 file "#{rails_directory}/public/export/embed.html" do
256 execute "#{rails_directory}/lib/quad_tile/extconf.rb" do
257 command "ruby extconf.rb"
258 cwd "#{rails_directory}/lib/quad_tile"
261 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") }
264 execute "#{rails_directory}/lib/quad_tile/Makefile" do
266 cwd "#{rails_directory}/lib/quad_tile"
270 File.exist?("#{rails_directory}/lib/quad_tile/quad_tile_so.so") &&
271 File.mtime("#{rails_directory}/lib/quad_tile/quad_tile_so.so") >= File.mtime("#{rails_directory}/lib/quad_tile/Makefile") &&
272 File.mtime("#{rails_directory}/lib/quad_tile/quad_tile_so.so") >= File.mtime("#{rails_directory}/lib/quad_tile/quad_tile.c") &&
273 File.mtime("#{rails_directory}/lib/quad_tile/quad_tile_so.so") >= File.mtime("#{rails_directory}/lib/quad_tile/quad_tile.h")
275 notifies :run, "execute[#{rails_directory}]"
278 execute rails_directory do
280 command "passenger-config restart-app --ignore-app-not-running #{rails_directory}"
283 only_if { File.exist?("/usr/bin/passenger-config") }
286 template "/etc/cron.daily/rails-#{name}" do
288 source "rails.cron.erb"
292 variables :directory => rails_directory