【问题标题】:Sudo doesn't have access to shell commands when deploying with Capistrano使用 Capistrano 部署时,Sudo 无法访问 shell 命令
【发布时间】:2011-02-15 17:59:14
【问题描述】:

我正在使用 capistrano 部署我的 Rails 3 应用程序。

我有已添加到 sudoers 的用户(部署)。这是我正在部署的用户。

当我以 deploy 身份登录到服务器时,我可以访问我需要的所有 gem 命令。例如:bundle,无论何时等等。

Capistrano 似乎以 sudo 运行,当我尝试时:

sudo whenever

我明白了

sudo: whenever: command not found

这意味着每次我尝试部署时,它都会失败并回滚。

我尝试在 deploy.rb 文件中将 :use_sudo 设置为 false,但仍然没有成功

set :user, "deploy"
set :runner, user
set :use_sudo, false

有什么建议吗?

这是我完整的部署脚本,以防我遗漏了什么:

require 'config/boot'
require 'hoptoad_notifier/capistrano'
require 'capistrano/ext/multistage'
require "whenever/capistrano"
# 
set :whenever_command, "bundle exec whenever"

set :application, "MYAPP"
set :repository,  "git@github.com:myAccount/myRepos.git"

# only keep 3 previous releases after cleanup
set :keep_releases, 3

set :scm, "git"
set :scm_user, "me"
set :branch, lambda {rails_env}
set :deploy_to, lambda {"/var/www/#{application}/#{rails_env}"}


default_run_options[:pty] = true

role :web, "xxx.xxx.xxx.xxx"                          # Your HTTP server, Apache/etc
role :app, "xxx.xxx.xxx.xxx"                          # This may be the same as your `Web` server
role :db,  "xxx.xxx.xxx.xxx", :primary => true        # This is where Rails migrations will run

set :user, "deploy"
set :runner, user
set :use_sudo, false

ssh_options[:paranoid] = false 
ssh_options[:port] = 22 

namespace :deploy do
  task :start do ; end
  task :stop do ; end
  task :restart, :roles => :app do
    run " touch #{File.join(current_path,'tmp','restart.txt')}"
  end

end

namespace :bundle do

  desc "run bundle install"
  task :install do
    run "cd #{current_release} && bundle install"
  end

end

namespace :tail do

  desc "Tail the current environment's log file"
  task :log, :roles => :app do
    stream "tail -f #{shared_path}/log/#{rails_env}.log"  
  end

  desc "Tail the new relic log file"
  task :new_relic, :roles => :app do
    stream "tail -f #{shared_path}/log/new_relic.log"  
  end

end


before "deploy:restart", "bundle:install"
after "deploy:restart", "deploy:cleanup"
after "deploy:restart", "whenever:update_crontab"

【问题讨论】:

  • 我应该补充一点,我正在使用 Capistrano 多级,因此 rails_env 变量设置在 /deploy/staging.rb
  • Gavin,请添加你得到的错误和堆栈跟踪?
  • 这是相关部分:** [out :: xxx.xxx.xxx.xxx] sh: 无论何时:未找到命令完成 *** [deploy:update_code] 回滚 * 执行“rm -rf /var/www/mysite/staging/releases/20110215174217; true" 服务器:["xxx.xxx.xxx.xxx"] [xxx.xxx.xxx.xxx] 执行命令命令完成失败:"sh -c ' cd /var/www/mysite/staging/releases/20110215174217 && 每当 --clear-crontab mysite'" on xxx.xxx.xxx.xxx
  • 请记住,Capistrano 默认情况下不会启动会话并运行 ~/.bashrc 等设置环境的文件(例如,未根据需要设置 PATH 变量)。另一个问题是配置错误的 /etc/sudoers 文件。可以贴在这里吗?

标签: ruby-on-rails ubuntu capistrano whenever


【解决方案1】:

在服务器上运行 which wheneverwhereis whenever 你应该得到将其放入脚本的命令的完整路径:

set :whenever_command, "path_to-whenever"

这不是干净的解决方案,但可能有效。


另一个解决方案可能是 sudo 重新配置,转到 /etc/sudoers 并查看 env_keep 添加 PATH 可能很重要,为了保留所有对应用程序重要的东西,您可以使用 rvm、capistrano-rvm 集成和将所有显示的变量从rvm info 放到env_keep,理论上应该可以,只是注意不要弄乱任何东西

【讨论】:

  • 这是一个快速的解决方法,但它不能修复其他 gem 命令 sudo cmd 使用与 cmd 不同的路径吗?
  • RVM 是一个正确的方向......现在我已经在我的服务器上安装了 RVM,并且我正在为我的特定应用程序使用 gemset。 ree@myapp 现在可以使用,因为我的部署用户现在可以使用 gem 可执行文件
猜你喜欢
  • 2012-04-13
  • 2014-01-24
  • 1970-01-01
  • 2019-07-03
  • 1970-01-01
  • 2014-01-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多