views:

374

answers:

3

My custom capistrano task "app:sample" fails with the following error message:

mnylen ilmo-on-rails $ cap app:sample
* executing `app:sample'
* executing "export RAILS_ENV=production; cd /home/mnylen/ilmo-on-rails/current; ruby script/coursegen 10"
servers: ["rails.cs.helsinki.fi"]
* establishing connection to gateway `melkinpaasi.cs.helsinki.fi'
* Creating gateway using melkinpaasi.cs.helsinki.fi
* establishing connection to `rails.cs.helsinki.fi' via gateway
Password: 
[rails.cs.helsinki.fi] executing command
*** [err :: rails.cs.helsinki.fi] Rails requires RubyGems >= 1.3.2. Please install RubyGems and try again: http://rubygems.rubyforge.org
command finished
failed: "sh -c 'export RAILS_ENV=production; cd /home/mnylen/ilmo-on-rails/current; ruby script/coursegen 10'" on rails.cs.helsinki.fi

Am I missing something or doing something wrong? The task is:

namespace :app do
  desc "Run sample data on production2
  task :sample do
    run "export RAILS_ENV=production; cd #{current_path}; ruby script/coursegen 10"
  end
end

If I run the same command from the actual server, it works fine.

A: 

Seems like you should update your RubyGems on the remote server.

Milan Novota
Thanks for your reply. However, gem --version on the remote server says the RubyGems version is 1.3.5 and thus, according to the error message, no update should be required.And well, it still works if I manually connect to the remote server and run that command. :-)
mnylen
That's pretty weird then. +1 for your question to give you some courage. ;)
Milan Novota
+1  A: 

Cap is running the remote command as an unexpected user - and that user does not have the correct path to ruby and gem. Check your settings in your recipe for :user and :use_sudo. Carefully read the cap output to see what user is being connected. I see you are using a :gateway; there can be two users in this case. One to connect to the gateway, and another to actually run commands on the target server.

Jonathan Julian
Thanks. This clears things a bit up. I modified the task to run ruby --version. It seems, it's using the wrong version of Ruby.Can I somehow set the path of ruby and gem executables in my deployment recipe?
mnylen
Oh, forgot to mention, I've tried to put the correct path of ruby to my .profile file.In my recipe, I use set :use_sudo, false set :user, "mnylen"
mnylen
+1  A: 

Okay, solved.

The problem was that there was two Ruby installations on the production server.

The .profile file under my home directory on the production server set the PATH environment variable to point to the correct Ruby version.

run command, it seems, doesn't source the .profile file and thus, running ruby script/coursegen 10 in the task used the wrong Ruby version, which was the reason for the weird error message about RubyGems version. This also explains why it worked when manually running the command from production servers shell.

My solution was to use full path to the Ruby executable in my run task, like this:

run "export RAILS_ENV=production; cd #{current_path}; /opt/ruby-enterprise-1.8.7-2009.10/bin/ruby script/coursegen 10"

Of course, this isn't pretty, but it works. If anyone has any prettier solutions, I'd be more than glad to use those instead. :)

mnylen