如何在Capistrano v3上的服务器上运行shell命令?

时间:2013-09-16 23:20:37

标签: ruby capistrano3

我是Capistrano的新手,我尝试使用Capistrano的DSL在服务器上运行shell命令('run','execute'等),但它似乎已被弃用。在搜索并搜索功能等同物之后,我仍然迷失了。

当前代码:

desc 'Do something'
task :do_something
  execute 'echo sometext'
end

输出:

    cap aborted!
    undefined method `execute' for main:Object
    /Users/Justin/Dropbox/xxxx/xxxx/xxxx/Capfile:45:in `block (2 levels) in <top (required)>'
    /Users/Justin/.rvm/gems/ruby-2.0.0-p247/bundler/gems/capistrano-2dc1627838f9/lib/capistrano/application.rb:12:in `run'
    /Users/Justin/.rvm/gems/ruby-2.0.0-p247/bundler/gems/capistrano-2dc1627838f9/bin/cap:3:in `<top (required)>'
    /Users/Justin/.rvm/gems/ruby-2.0.0-p247/bin/cap:23:in `load'
    /Users/Justin/.rvm/gems/ruby-2.0.0-p247/bin/cap:23:in `<main>'
    /Users/Justin/.rvm/gems/ruby-2.0.0-p247/bin/ruby_noexec_wrapper:14:in `eval'
    /Users/Justin/.rvm/gems/ruby-2.0.0-p247/bin/ruby_noexec_wrapper:14:in `<main>'
    Tasks: TOP => deploy:do_something

1 个答案:

答案 0 :(得分:112)

在Capistrano v3中,您必须通过使用主机名列表调用on来指定运行代码的位置,例如

task :execute_on_server do
  on "root@example.com" do
    execute "some_command"
  end
end

如果您设置了角色,则可以使用roles方法:

role :mailserver, "root@mail.example.com"

task :check_mail do
  on roles(:mailserver) do
    execute "some_command"
  end
end

这里有一些v3文档:http://www.capistranorb.com/