2010-03-16 43 views
0

我的deploy.rb設置了一些環境變量來使用常規用戶的本地Ruby,而不是系統範圍的。如何防止由Capistrano的sudo操作設置default_environment變量?

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' 
} 

當然,當一個任務使用sudo時,我會期望使用系統範圍的ruby來代替。但似乎這些環境變量被設置,無論如何,這是root用戶顯然無效,並返回一個錯誤:

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 

沒有什麼差別我是否使用Capistrano酒店的須藤「系統調用」或定期運行「sudo系統調用」

我該如何避免這種情況?

回答

2

一個簡單的方法可能是採取這種帶外方式,由於連接(不)在Capistrano中回收的方式,像這樣的環境變量不限於一次調用(正如人們所期望的那樣)!

我建議用香草的Net :: SSH(http://net-ssh.rubyforge.org/ssh/v2/api/index.html)製作出的帶外連接,這可能是這麼簡單:

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

只是一個想法!

相關問題