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.
20 define :rails_port, :action => [ :create, :enable ] do
22 ruby_version = params[:ruby] || "1.9.1"
23 rails_directory = params[:directory] || "/srv/#{name}"
24 rails_user = params[:user]
25 rails_group = params[:group]
26 rails_repository = params[:repository] || "git://git.openstreetmap.org/rails.git"
27 rails_revision = params[:revision] || "live"
28 run_migrations = params[:run_migrations] || false
29 status = params[:status] || "online"
32 :host => params[:database_host],
33 :port => params[:database_port],
34 :name => params[:database_name],
35 :username => params[:database_username],
36 :password => params[:database_password]
39 package "ruby#{ruby_version}"
40 package "ruby#{ruby_version}-dev"
41 package "rubygems#{ruby_version}"
42 package "irb#{ruby_version}"
47 package "libsasl2-dev"
49 package "libxslt1-dev"
50 package "libmemcached-dev"
52 gem_package "bundler#{ruby_version}" do
53 package_name "bundler"
54 gem_binary "gem#{ruby_version}"
55 options "--format-executable"
58 file "/usr/lib/ruby/1.8/rack.rb" do
62 directory "/usr/lib/ruby/1.8/rack" do
67 file "#{rails_directory}/tmp/restart.txt" do
71 file "#{rails_directory}/public/export/embed.html" do
75 execute "#{rails_directory}/public/assets" do
77 command "rake#{ruby_version} assets:precompile"
81 notifies :delete, resources(:file => "#{rails_directory}/public/export/embed.html")
82 notifies :touch, resources(:file => "#{rails_directory}/tmp/restart.txt")
85 execute "#{rails_directory}/db/migrate" do
87 command "rake#{ruby_version} db:migrate"
91 notifies :run, resources(:execute => "#{rails_directory}/public/assets")
94 execute "#{rails_directory}/Gemfile" do
96 command "bundle#{ruby_version} install"
101 notifies :run, resources(:execute => "#{rails_directory}/db/migrate")
103 notifies :run, resources(:execute => "#{rails_directory}/public/assets")
105 subscribes :run, resources(:gem_package => "bundler#{ruby_version}")
108 directory rails_directory do
114 git rails_directory do
116 repository rails_repository
117 revision rails_revision
120 notifies :run, resources(:execute => "#{rails_directory}/Gemfile")
123 directory "#{rails_directory}/tmp" do
128 file "#{rails_directory}/config/environment.rb" do
133 template "#{rails_directory}/config/database.yml" do
135 source "database.yml.erb"
139 variables database_params
140 notifies :touch, resources(:file => "#{rails_directory}/tmp/restart.txt")
143 file "#{rails_directory}/config/application.yml" do
147 content_from_file "#{rails_directory}/config/example.application.yml" do |line|
148 line.gsub!(/^( *)server_url:.*$/, "\\1server_url: \"#{name}\"")
150 if params[:email_from]
151 line.gsub!(/^( *)email_from:.*$/, "\\1email_from: \"#{params[:email_from]}\"")
154 line.gsub!(/^( *)status:.*$/, "\\1status: :#{status}")
156 if params[:messages_domain]
157 line.gsub!(/^( *)#messages_domain:.*$/, "\\1messages_domain: \"#{params[:messages_domain]}\"")
160 line.gsub!(/^( *)#geonames_username:.*$/, "\\1geonames_username: \"openstreetmap\"")
162 if params[:quova_username]
163 line.gsub!(/^( *)#quova_username:.*$/, "\\1quova_username: \"#{params[:quova_username]}\"")
164 line.gsub!(/^( *)#quova_password:.*$/, "\\1quova_password: \"#{params[:quova_password]}\"")
167 if params[:soft_memory_limit]
168 line.gsub!(/^( *)#soft_memory_limit:.*$/, "\\1soft_memory_limit: #{params[:soft_memory_limit]}")
171 if params[:hard_memory_limit]
172 line.gsub!(/^( *)#hard_memory_limit:.*$/, "\\1hard_memory_limit: #{params[:hard_memory_limit]}")
176 line.gsub!(/^( *)gpx_trace_dir:.*$/, "\\1gpx_trace_dir: \"#{params[:gpx_dir]}/traces\"")
177 line.gsub!(/^( *)gpx_image_dir:.*$/, "\\1gpx_image_dir: \"#{params[:gpx_dir]}/images\"")
180 if params[:attachments_dir]
181 line.gsub!(/^( *)attachments_dir:.*$/, "\\1attachments_dir: \"#{params[:attachments_dir]}\"")
185 line.gsub!(/^( *)#log_path:.*$/, "\\1log_path: \"#{params[:log_path]}\"")
188 if params[:memcache_servers]
189 line.gsub!(/^( *)#memcache_servers:.*$/, "\\1memcache_servers: [ \"#{params[:memcache_servers].join("\", \"")}\" ]")
192 if params[:potlatch2_key]
193 line.gsub!(/^( *)#potlatch2_key:.*$/, "\\1potlatch2_key: \"#{params[:potlatch2_key]}\"")
197 line.gsub!(/^( *)#id_key:.*$/, "\\1id_key: \"#{params[:id_key]}\"")
200 if params[:oauth_key]
201 line.gsub!(/^( *)#oauth_key:.*$/, "\\1oauth_key: \"#{params[:oauth_key]}\"")
204 line.gsub!(/^( *)require_terms_seen:.*$/, "\\1require_terms_seen: true")
205 line.gsub!(/^( *)require_terms_agreed:.*$/, "\\1require_terms_agreed: true")
207 if params[:piwik_location]
208 line.gsub!(/^( *)#piwik_location:.*$/, "\\1piwik_location: \"#{params[:piwik_location]}\"")
209 line.gsub!(/^( *)#piwik_site:.*$/, "\\1piwik_site: #{params[:piwik_site]}")
210 line.gsub!(/^( *)#piwik_signup_goal:.*$/, "\\1piwik_signup_goal: #{params[:piwik_signup_goal]}")
215 notifies :touch, resources(:file => "#{rails_directory}/tmp/restart.txt")
218 execute "#{rails_directory}/lib/quad_tile/extconf.rb" do
219 command "ruby extconf.rb"
220 cwd "#{rails_directory}/lib/quad_tile"
223 not_if { File.exist?("#{rails_directory}/lib/quad_tile/Makefile") and File.mtime("#{rails_directory}/lib/quad_tile/Makefile") >= File.mtime("#{rails_directory}/lib/quad_tile/extconf.rb") }
226 execute "#{rails_directory}/lib/quad_tile/Makefile" do
228 cwd "#{rails_directory}/lib/quad_tile"
232 File.exist?("#{rails_directory}/lib/quad_tile/quad_tile_so.so") and
233 File.mtime("#{rails_directory}/lib/quad_tile/quad_tile_so.so") >= File.mtime("#{rails_directory}/lib/quad_tile/Makefile") and
234 File.mtime("#{rails_directory}/lib/quad_tile/quad_tile_so.so") >= File.mtime("#{rails_directory}/lib/quad_tile/quad_tile.c") and
235 File.mtime("#{rails_directory}/lib/quad_tile/quad_tile_so.so") >= File.mtime("#{rails_directory}/lib/quad_tile/quad_tile.h")
237 notifies :touch, resources(:file => "#{rails_directory}/tmp/restart.txt")
240 template "/etc/cron.daily/rails-#{name}" do
242 source "rails.cron.erb"
246 variables :directory => rails_directory