How can I prevent default_environment variables from getting set by Capistrano's sudo action?

Posted by Logan Koester on Stack Overflow See other posts from Stack Overflow or by Logan Koester
Published on 2010-03-16T22:06:51Z Indexed on 2010/03/16 22:11 UTC
Read the original article Hit count: 322

Filed under:
|
|
|
|

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?

© Stack Overflow or respective owner

Related posts about capistrano

Related posts about ruby