74

I'm new to Capistrano and I've tried using Capistrano's DSL to run shell commands on the server ('run', 'execute', etc.), but it appears that it was deprecated. After searching and searching for a functional equivalent, I still am lost.

Current code:

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

Output:

    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
fotanus
  • 19,618
  • 13
  • 77
  • 111
Justin Godesky
  • 1,019
  • 1
  • 10
  • 17
  • I had exactly the same problem with the methods "info" and "error" -- the same problem because the methods belong to SSHKit and must be in an SSHKit block. – Dave Burt Apr 20 '15 at 00:58

1 Answers1

118

In Capistrano v3, you must specify where you want to run the code by calling on with a list of hostnames, e.g.

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

If you have roles set up, you can use the roles method as a convenience:

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

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

There is some v3 documentation here: http://www.capistranorb.com/

lmars
  • 2,502
  • 1
  • 16
  • 9
  • 8
    Awesome, I wish they made it clearer in the walkthrough. They only use test, info, etc. – Justin Godesky Sep 17 '13 at 18:45
  • 2
    Capistrano `execute` method rely on implementation in `sshkit`. Right now you can find more information on `execute` here: https://github.com/leehambley/sshkit Capistrano 3 documentation is still incomplete. – Tombart Nov 09 '13 at 23:26
  • @KitHo Yes, you can use sudo, just as you can with a manual ssh connection. – Benubird Aug 07 '14 at 15:38
  • 3
    When I had the same problem with 'info' and 'error' methods, I used `run_locally { ... }` rather than `on ... { ... }`. That gets you into SSHKit context without connecting to a remote box. – Dave Burt Apr 20 '15 at 00:59