Capistrano自定义任务失败,因为“Rails需要RubyGems> = 1.3.2”

我的自定义capistrano任务“app:sample”失败,并显示以下错误消息:

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 

我错过了什么或做错了什么? 任务是:

 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 

如果我从实际服务器运行相同的命令,它工作正常。

Cap正在以意外用户身份运行远程命令 – 该用户没有正确的ruby和gem路径。 检查配方中的设置:user:use_sudo 。 仔细阅读帽输出以查看正在连接的用户。 我看到你正在使用:gateway ; 在这种情况下可以有两个用户。 一个用于连接到网关,另一个用于在目标服务器上实际运行命令。

好的,解决了。

问题是生产服务器上有两个Ruby安装。

生产服务器上我的主目录下的.profile文件将PATH环境变量设置为指向正确的Ruby版本。

看来, 运行命令不会获取.profile文件,因此,在任务中运行ruby script / coursegen 10使用了错误的Ruby版本,这就是关于RubyGems版本的奇怪错误消息的原因。 这也解释了为什么在从生产服务器shell手动运行命令时它起作用。

我的解决方案是在运行任务中使用Ruby可执行文件的完整路径,如下所示:

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

当然,这并不漂亮,但它确实有效。 如果有人有任何更漂亮的解决方案,我会非常乐意使用这些解决方案。 🙂

好像你应该在远程服务器上更新你的RubyGems。