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}"
44 package "irb#{ruby_version}"
50 package "libsasl2-dev"
52 package "libxslt1-dev"
53 package "libmemcached-dev"
55 gem_package "bundler#{ruby_version}" do
56 package_name "bundler"
58 gem_binary "gem#{ruby_version}"
59 options "--format-executable"
62 file "/usr/lib/ruby/1.8/rack.rb" do
66 directory "/usr/lib/ruby/1.8/rack" do
71 file "#{rails_directory}/tmp/restart.txt" do
75 file "#{rails_directory}/public/export/embed.html" do
79 execute "#{rails_directory}/public/assets" do
81 command "rake#{ruby_version} assets:precompile"
82 environment "RAILS_ENV" => "production"
86 notifies :delete, "file[#{rails_directory}/public/export/embed.html]"
87 notifies :touch, "file[#{rails_directory}/tmp/restart.txt]"
90 execute "#{rails_directory}/db/migrate" do
92 command "rake#{ruby_version} db:migrate"
96 notifies :run, "execute[#{rails_directory}/public/assets]"
99 execute "#{rails_directory}/Gemfile" do
101 command "bundle#{ruby_version} install"
105 environment "NOKOGIRI_USE_SYSTEM_LIBRARIES" => "yes"
107 notifies :run, "execute[#{rails_directory}/db/migrate]"
109 notifies :run, "execute[#{rails_directory}/public/assets]"
111 subscribes :run, "gem_package[bundler#{ruby_version}]"
114 directory rails_directory do
120 git rails_directory do
122 repository rails_repository
123 revision rails_revision
126 notifies :run, "execute[#{rails_directory}/Gemfile]"
129 directory "#{rails_directory}/tmp" do
134 file "#{rails_directory}/config/environment.rb" do
139 template "#{rails_directory}/config/database.yml" do
141 source "database.yml.erb"
145 variables database_params
146 notifies :touch, "file[#{rails_directory}/tmp/restart.txt]"
149 application_yml = edit_file "#{rails_directory}/config/example.application.yml" do |line|
150 line.gsub!(/^( *)server_url:.*$/, "\\1server_url: \"#{name}\"")
152 if params[:email_from]
153 line.gsub!(/^( *)email_from:.*$/, "\\1email_from: \"#{params[:email_from]}\"")
156 line.gsub!(/^( *)status:.*$/, "\\1status: :#{status}")
158 if params[:messages_domain]
159 line.gsub!(/^( *)#messages_domain:.*$/, "\\1messages_domain: \"#{params[:messages_domain]}\"")
162 line.gsub!(/^( *)#geonames_username:.*$/, "\\1geonames_username: \"openstreetmap\"")
164 if params[:quova_username]
165 line.gsub!(/^( *)#quova_username:.*$/, "\\1quova_username: \"#{params[:quova_username]}\"")
166 line.gsub!(/^( *)#quova_password:.*$/, "\\1quova_password: \"#{params[:quova_password]}\"")
169 if params[:soft_memory_limit]
170 line.gsub!(/^( *)#soft_memory_limit:.*$/, "\\1soft_memory_limit: #{params[:soft_memory_limit]}")
173 if params[:hard_memory_limit]
174 line.gsub!(/^( *)#hard_memory_limit:.*$/, "\\1hard_memory_limit: #{params[:hard_memory_limit]}")
178 line.gsub!(/^( *)gpx_trace_dir:.*$/, "\\1gpx_trace_dir: \"#{params[:gpx_dir]}/traces\"")
179 line.gsub!(/^( *)gpx_image_dir:.*$/, "\\1gpx_image_dir: \"#{params[:gpx_dir]}/images\"")
182 if params[:attachments_dir]
183 line.gsub!(/^( *)attachments_dir:.*$/, "\\1attachments_dir: \"#{params[:attachments_dir]}\"")
187 line.gsub!(/^( *)#log_path:.*$/, "\\1log_path: \"#{params[:log_path]}\"")
190 if params[:memcache_servers]
191 line.gsub!(/^( *)#memcache_servers:.*$/, "\\1memcache_servers: [ \"#{params[:memcache_servers].join("\", \"")}\" ]")
194 if params[:potlatch2_key]
195 line.gsub!(/^( *)#potlatch2_key:.*$/, "\\1potlatch2_key: \"#{params[:potlatch2_key]}\"")
199 line.gsub!(/^( *)#id_key:.*$/, "\\1id_key: \"#{params[:id_key]}\"")
202 if params[:oauth_key]
203 line.gsub!(/^( *)#oauth_key:.*$/, "\\1oauth_key: \"#{params[:oauth_key]}\"")
206 line.gsub!(/^( *)require_terms_seen:.*$/, "\\1require_terms_seen: true")
207 line.gsub!(/^( *)require_terms_agreed:.*$/, "\\1require_terms_agreed: true")
212 file "#{rails_directory}/config/application.yml" do
216 content application_yml
217 notifies :touch, "file[#{rails_directory}/tmp/restart.txt]"
220 if params[:piwik_configuration]
221 file "#{rails_directory}/config/piwik.yml" do
225 content YAML.dump(params[:piwik_configuration])
226 notifies :run, "execute[#{rails_directory}/public/assets]"
229 file "#{rails_directory}/config/piwik.yml" do
231 notifies :run, "execute[#{rails_directory}/public/assets]"
235 execute "#{rails_directory}/lib/quad_tile/extconf.rb" do
236 command "ruby extconf.rb"
237 cwd "#{rails_directory}/lib/quad_tile"
240 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") }
243 execute "#{rails_directory}/lib/quad_tile/Makefile" do
245 cwd "#{rails_directory}/lib/quad_tile"
249 File.exist?("#{rails_directory}/lib/quad_tile/quad_tile_so.so") and
250 File.mtime("#{rails_directory}/lib/quad_tile/quad_tile_so.so") >= File.mtime("#{rails_directory}/lib/quad_tile/Makefile") and
251 File.mtime("#{rails_directory}/lib/quad_tile/quad_tile_so.so") >= File.mtime("#{rails_directory}/lib/quad_tile/quad_tile.c") and
252 File.mtime("#{rails_directory}/lib/quad_tile/quad_tile_so.so") >= File.mtime("#{rails_directory}/lib/quad_tile/quad_tile.h")
254 notifies :touch, "file[#{rails_directory}/tmp/restart.txt]"
257 template "/etc/cron.daily/rails-#{name}" do
259 source "rails.cron.erb"
263 variables :directory => rails_directory