I’m using mod_rails with capistrano and multi-stage (qa, staging, production), and wrote this workaround so that the RAILS_ENV could be set correctly in each stage. Mod_rails sets RAILS_ENV once in the global server conf file, but I needed it set once for each environment: qa, staging, and production. I tried setting ENV['RAILS_ENV] in each file under config/deploy/ but the setting was not picked up.
Solution – write the correct value into config/environment.rb’s ENV[''RAILS_ENV] while deploying.
So here is my 3 step fix -
Step 1.
I created a new file
lib/set_mod_rails_env.rb
# sets ENV['rails_env'] for mod_rails
# http://www.megasolutions.net/ruby/search-a-file-and-replace-text-50116.aspx
# Robert Evans'
def ChangeOnFile(file, regex_to_find, text_to_put_in_place)
text = File.read file
File.open(file, 'w+'){|f| f << text.gsub(regex_to_find,
text_to_put_in_place)}
end
ChangeOnFile("#{ARGV[0]}/config/environment.rb", /#mod_rails_env_here/, "ENV['RAILS_ENV']='#{ARGV[1]}'")
Step 2.
Add the following code to config/deploy.rb
namespace :deploy do
desc "set ENV['RAILS_ENV'] for mod_rails"
task :before_restart do
run "ruby #{current_release}/lib/set_mod_rails_env.rb #{current_release} #{stage}"
end
end
Step 3.
Add the following code to config/environment.rb
# deploy.rb will swap this out for the appropriate rails_env.
# mod_rails apparently need this var in this file (not ./environments/qa.rb)
# do not change the following line. Ruby regexp looks for it.
#mod_rails_env_here
That’s it. Let me know if you know of an easier way.
rails template
Usage $ rails app_name -m ./ahabman_template.rb
run "rm README" run "rm public/index.html" run "rm public/favicon.ico" run "rm public/robots.txt" run "rm -f public/javascripts/*" gem 'ruby-openid', :lib => 'openid' rake("gems:install", :sudo => true) plugin "kamui_restful_authentication", :git => 'git://github.com/kamui/restful-authentication.git' plugin 'exception_notifier', :git => 'git://github.com/rails/exception_notification.git' plugin 'open_id_authentication', :git => 'git://github.com/rails/open_id_authentication.git' #plugin 'asset_packager', :git => 'http://synthesis.sbecker.net/pages/asset_packager' plugin 'role_requirement', :git => 'git://github.com/timcharper/role_requirement.git' #plugin 'acts_as_taggable_redux', :git => 'http://github.com/geemus/acts_as_taggable_redux/tree/master' #plugin 'restful-authentication', :git => 'git://github.com/technoweenie/restful-authentication.git' generate("authenticated user sessions --include-activation --include-forgot-password --email-as-login") generate("roles", "Role User") #rake('acts_as_taggable:db:create') rake("open_id_authentication:db:create") rake('db:migrate') route "map.root :controller => 'sessions', :action => 'signup' " run "echo generated from ahabman_template > README" run "cp config/database.yml config/example_database.yml" #if yes?("Do you want this thing?") # ... #end #my_var = ask("was up") #generate :something, my_var puts "SUCCESS!"