我通过Capistrano 3设置页面(www.capistranorb.com)和一些步骤提到如何编写服务器以及如何运行 :check_write_permissions 的基本任务 .

我的问题是当我尝试使用以下服务器设置在我的服务器上运行示例代码时,我得到了意想不到的结果 .

我有我的config / deploy / production.rb文件设置如下:

server '10.1.28.90', roles: [:web, :app]
server '10.1.246.239', roles: [:db]

然后我在lib / capistrano / tasks / access_check.rake中创建了:check_write_permissions任务 . 我对"on roles(:all)"做了一个小修改,所以它将改为"on roles(:web)" .

desc "Check that we can access everything"
task :check_write_permissions do
  on roles(:web) do |host|
    if test("[ -w #{fetch(:deploy_to)} ]")
      info "#{fetch(:deploy_to)} is writable on #{host}"
    else
      error "#{fetch(:deploy_to)} is not writable on #{host}"
    end
  end
end

当我运行任务时:

cap production check_write_permissions

要么

bundle exec cap production check_write_permissions

...我希望它只针对具有:web角色的服务器运行:check_write_permissions代码 . 相反,我的输出显示:db服务器也与:check_write_permissions任务一起运行 . 这会引发异常,因为我在数据库服务器上没有部署目录 .

DEBUG[90f77252] Running /usr/local/rvm/bin/rvm version on 10.1.246.239
DEBUG[90f77252] Command: /usr/local/rvm/bin/rvm version
DEBUG[fa4e93ec] Running /usr/local/rvm/bin/rvm version on 10.1.28.90
DEBUG[fa4e93ec] Command: /usr/local/rvm/bin/rvm version
DEBUG[90f77252]     bash: /usr/local/rvm/bin/rvm: No such file or directory
DEBUG[fa4e93ec]     rvm 1.25.28 (stable) by Wayne E. Seguin <wayneeseguin@gmail.com>, Michal Papis <mpapis@gmail.com> [https://rvm.io/]
DEBUG[fa4e93ec] Finished in 1.060 seconds with exit status 0 (successful).
rvm 1.25.28 (stable) by Wayne E. Seguin <wayneeseguin@gmail.com>, Michal Papis <mpapis@gmail.com> [https://rvm.io/]
DEBUG[3583646b] Running /usr/local/rvm/bin/rvm current on 10.1.28.90
DEBUG[3583646b] Command: /usr/local/rvm/bin/rvm current
DEBUG[3583646b]     ruby-2.0.0-p481
DEBUG[3583646b] Finished in 0.286 seconds with exit status 0 (successful).
ruby-2.0.0-p481
DEBUG[b91aa735] Running /usr/local/rvm/bin/rvm 2.0.0-p481 do ruby --version on 10.1.28.90
DEBUG[b91aa735] Command: /usr/local/rvm/bin/rvm 2.0.0-p481 do ruby --version
DEBUG[b91aa735]     ruby 2.0.0p481 (2014-05-08 revision 45883) [x86_64-linux]
DEBUG[b91aa735] Finished in 0.400 seconds with exit status 0 (successful).
ruby 2.0.0p481 (2014-05-08 revision 45883) [x86_64-linux]
cap aborted!
SSHKit::Runner::ExecuteError: Exception while executing on host 10.1.246.239: rvm exit status: 127
rvm stdout: Nothing written
rvm stderr: bash: /usr/local/rvm/bin/rvm: No such file or directory

当我在执行的命令中使用ROLE过滤器运行任务时,例如:

ROLES=web cap production check_write_permissions

这按预期工作 . 我看到只有Web服务器才能运行任务 .

DEBUG[7974b8ee] Running /usr/local/rvm/bin/rvm version on 10.1.28.90
DEBUG[7974b8ee] Command: /usr/local/rvm/bin/rvm version
DEBUG[7974b8ee]     rvm 1.25.28 (stable) by Wayne E. Seguin <wayneeseguin@gmail.com>, Michal Papis <mpapis@gmail.com> [https://rvm.io/]
DEBUG[7974b8ee] Finished in 1.062 seconds with exit status 0 (successful).
rvm 1.25.28 (stable) by Wayne E. Seguin <wayneeseguin@gmail.com>, Michal Papis <mpapis@gmail.com> [https://rvm.io/]
DEBUG[23f666d8] Running /usr/local/rvm/bin/rvm current on 10.1.28.90
DEBUG[23f666d8] Command: /usr/local/rvm/bin/rvm current
DEBUG[23f666d8]     ruby-2.0.0-p481
DEBUG[23f666d8] Finished in 0.297 seconds with exit status 0 (successful).
ruby-2.0.0-p481
DEBUG[7ae64240] Running /usr/local/rvm/bin/rvm 2.0.0-p481 do ruby --version on 10.1.28.90
DEBUG[7ae64240] Command: /usr/local/rvm/bin/rvm 2.0.0-p481 do ruby --version
DEBUG[7ae64240]     ruby 2.0.0p481 (2014-05-08 revision 45883) [x86_64-linux]
DEBUG[7ae64240] Finished in 0.387 seconds with exit status 0 (successful).
ruby 2.0.0p481 (2014-05-08 revision 45883) [x86_64-linux]
DEBUG[c0ebccc0] Running /usr/bin/env [ -w /data/union_benefits/ ] on 10.1.28.90
DEBUG[c0ebccc0] Command: [ -w /data/union_benefits/ ]
DEBUG[c0ebccc0] Finished in 0.126 seconds with exit status 1 (failed).
ERROR/data/union_benefits/ is not writable on 10.1.28.90

这背后的原因是什么?我已经挖掘了Capistrano 3.2.1代码,我无法弄清楚这一点 . 也许这只是我对角色(...)如何运作的误解,但我无法弄清楚 .