我不确定这是否只是我对 Capistrano 和 Rake 的误解,但我正在浏览 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 代码,但我无法弄清楚。也许这只是我对角色(...)如何工作的误解,但我无法弄清楚。