views:

313

answers:

1

My deploy.rb sets some environment variables to use the regular user's local Ruby rather than the system-wide one.

set :default_environment, {
  :PATH => '/home/myapp/.rvm/bin:/home/myapp/.rvm/bin:/home/myapp/.rvm/rubies/ruby-1.9.1-p378/bin:/home/myapp/.rvm/gems/ruby-1.9.1-p378/bin:/home/myapp/.rvm/gems/ruby-1.9.1-p378%global/bin:/home/myapp/bin:/usr/bin:/opt/local/bin:/opt/local/sbin:/usr/local/bin:/usr/local/sbin:/usr/sbin:/sbin:/bin:/usr/games',
  :RUBY_VERSION => 'ruby-1.9.1-p378',
  :GEM_HOME => '/home/myapp/.rvm/gems/ruby-1.9.1-p378',
  :GEM_PATH => '/home/myapp/.rvm/gems/ruby-1.9.1-p378:/home/myapp/.rvm/gems/ruby-1.9.1-p378%global'
}

Naturally, when a task is using sudo, I would expect the system-wide ruby to be used instead. But it seems the environment variables are being set anyway, which is obviously invalid for the root user and returns an error:

executing "sudo -p 'sudo password: ' /etc/init.d/god stop"
    servers: ["myapp.com"]
    [myapp.com] executing command
    command finished
failed: "env PATH=/home/myapp/.rvm/bin:/home/myapp/.rvm/bin:/home/myapp/.rvm/rubies/ruby-1.9.1-p378/bin:/home/myapp/.rvm/gems/ruby-1.9.1-p378/bin:/home/myapp/.rvm/gems/ruby-1.9.1-p378%global/bin:/home/myapp/bin:/usr/bin:/opt/local/bin:/opt/local/sbin:/usr/local/bin:/usr/local/sbin:/usr/sbin:/sbin:/bin:/usr/games RUBY_VERSION=ruby-1.9.1-p378 GEM_HOME=/home/myapp/.rvm/gems/ruby-1.9.1-p378 GEM_PATH=/home/myapp/.rvm/gems/ruby-1.9.1-p378:/home/myapp/.rvm/gems/ruby-1.9.1-p378%global sh -c 'sudo -p '\\''sudo password: '\\'' /etc/init.d/god stop'" on myapp.com

It makes no difference whether I use capistrano's sudo "system call" or the regular run "sudo system call".

How can I avoid this?

+2  A: 

One easy way might be to take this out-of-band, due to the way connections (aren't) recycled in Capistrano, environmental variables like this are not restricted to a single call (as one would expect!)

I suggest making an out-of-band connection using vanilla Net::SSH (http://net-ssh.rubyforge.org/ssh/v2/api/index.html), this could be as simple as:

Net::SSH.start('host', 'user', :password => "password") do |ssh| 
  ssh.exec!("my-task-here")
end

Just a thought!

Beaks