setting production environment in rails 3.0 - ruby-on-rails-3

How is the environment set in Rails 3.0?
In Rails 2.x, environment.rb contained a line setting RAILS_ENV to production. It was commented out in the generated file. To force a production environment, uncomment that line.
Rails 3.0 contains no such line in environment.rb, and RAILS_ENV is deprecated. Is there something missing, or is the environment set when the server is started (eg "start Mongrel_rails -e production ..."
I'm trying out Rails 3.0 on my deployment host and getting some odd behavior. Specifically, it seems to be trying to load the :development object from database.yml, and it seems to be ignoring the :groups => :development option in the gemfile. Consequently the app is trying to use Sqlite3 on the deployment server where it is not available.

The replacement is Rails.env
I set the environment in my server config.. thin.yml, mongrel_cluster.yml, or whatever server I am using.
When you are using Cap, how do you call "bundle install"? You should be using the --deployment flag when deploying to prod. It would be helpful to see your deploy.rb file.

Related

How to create an RVM environment on a server through net-ssh?

I have a script which is installing Ruby/RVM (and more) on my server to get an environment ready to deploy a Rails project. This script is in ruby and use the net-ssh lib to do the job.
After having installed RVM, Ruby, I would like to create the project Gemset:
connection do |conn|
logger("Create RVM environment #{ruby_version}##{project_name}")
conn.exec!("#{rvmsudo_path} #{rvm_path} gemset create #{project_name}")
end
I get my gemset created under, all good:
/usr/local/rvm/gems/ruby-1.9.3-p286#my_project_name
Here is my Capistrano setting for rvm/ruby:
set :rvm_type, :system
set :rvm_ruby_string, ENV['GEM_HOME'].gsub(/.*\//,"")
When I'm trying to deploy using capistrano, I get:
/usr/local/rvm/environments/ruby-1.9.3-p286#my_project_name: Permission denied
The environment file 'ruby-1.9.3-p286#my_project_name' is actually missing in that folder. I need to log into the server and navigate to my project so the .rvmrc file to trigger the creation of the environment (rvm --create my_project_name). I would like to avoid this last step. Do you know how to trigger the creation of this environment? (I though it would create it when I have created the Gemset)
RVM has support for installing itself via capistrano, to install rubies and to create gemsets, you should make use of it:
https://rvm.io/integration/capistrano/#install_rvm
https://rvm.io/integration/capistrano/#install_ruby
Basically the RVM environment need to be loaded to be able to script it remotely.
https://rvm.io/workflow/scripting/
You can run the following command with net-ssh to do so:
conn.exec!("source "/usr/local/rvm/scripts/rvm; rvm --create ruby-1.9.3-p286#my_project_name")
It will create your environment and associated gemset.

Rails Reporting Wrong Environment in Multi-stage Setup

Using capistrano 2.12.0, capistrano-ext 1.2.1, Rails 3.2.7 and Phusion Passenger 3.0.15.
For no logical reason, my 'playground' server has started reporting that it's actually running in development mode.
I've reinstalled things, restarted apache etc but am still stuck.
Don't really want to hardcode as I've got a production environment to think about.
In my apache conf. I've set
RailsEnv playground
There's a gist of my deploy.rb file here
Capistrano is sending to the correct location etc. so I think it's rails or passenger rather than cap.
What else can I do to get this going properly??
---- EDIT ----
I've just tested with Unicorn and it's also reporting that it's running in development mode.
Starting unicorn with the following:
unicorn -E playground -l 8000
After much experimentation, I found the root cause to be a line in one of my models:
- if Rails.env = 'development'
Obviously, this should have been a '==' not a single one.

How do I ensure my jruby command line options are used when running "rails", "rake", "rspec" etc?

I currently run my Rails app using:
jruby --1.9 -J-XX:+CMSClassUnloadingEnabled -J-XX:+UseConcMarkSweepGC -J-XX:MaxPermSize=256m -S rails server
This is getting pretty old now. How can I set my Rails project up so that just running
rails server
has the same effect?
(Note: bash aliases and the like are not what I'm looking for here. I want to make the project work right, not fix my local settings)
When using RVM and a project .rvmrc, the canonical way is to set PROJECT_JRUBY_OPTS in the project .rvmrc. A bug prevented this from working for me, so use rvm head.
If not using rvm then use JRUBY_OPTS, which is the built-in way of doing it that JRuby checks (in fact, the PROJECT_JRUBY_OPTS thing ends up being converted to JRUBY_OPTS by rvm).

Rake tasks from cron - uninitialized constant YAML::ENGINE

I am getting uninitialized constant YAML::ENGINE when running a rake task from cron since I upgraded my server to ruby 1.9.2. I had the same error with the app but putting ...
require 'yaml'
YAML::ENGINE.yamler= 'syck'
in the boot.rb file fixed it. If I run the task directly from the command line on my Ubuntu server it works fine, the server uses RVM.
However running a task from cron doesn't seem to pickup this fix, I have tried this ...
task :twitter, :needs => :environment do
require 'yaml'
YAML::ENGINE.yamler= 'syck'
#tweets = Property.updatetwitter
end
to no avail.
Are you sure you're running it under Ruby 1.9.2? Because while YAML::ENGINE exists in 1.9.2, it's not in 1.8.7. Check your Ruby version.
UPDATE
How to tell which Ruby version program is using from within the program:
puts `ruby -v`
Lame way how to enforce cron task to run under certain Ruby version (if server uses RVM):
rvm use 1.8.7; ...

Register a script to use with the rails command, like `rails mycommand`

The rails command-line command provides a couple of commands, like rails generate, rails console etc. Now I'd like write a gem which registers my own command for use with rails mycommand.
Is this possible?
If so, any guides on how to do that?
NB: This is for rails 3+
regards, apeiros
Haven't done it, but here are some leads. In your Rails app, the script directory holds a file called 'rails' that has this line
# This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.
It then requires this Rails file: https://github.com/rails/rails/blob/master/railties/lib/rails/commands.rb