Question

I'm trying to learn to use Capistrano 3 by following the DOCS and I've written a simple task to test my understanding.

The task is created in the default task dir in lib/capistrano/tasks:

  desc "Check status of web server"
    task :nginx_status do
      on roles(:web) do |host|
        execute 'service nginx status'
      end
    end

My config/deploy/staging.rb file contains the following:

set :stage, :staging

role :web, "192.168.0.11"
role :app,  "192.168.0.11"
role :db, "192.168.0.11", primary:true

server '192.168.0.11', user: 'vagrant', roles: %w{web app}

Capfile

# Load DSL and Setup Up Stages
require 'capistrano/setup'

# Includes default deployment tasks
require 'capistrano/deploy'

# Loads custom tasks from `lib/capistrano/tasks' if you have any defined.
Dir.glob('lib/capistrano/tasks/*.rake').each { |r| import r }

My deploy.rb contains an app name and a repo url to github.

When I run cap staging nginx_status I get the following error:

** Invoke staging (first_time)
** Execute staging
** Invoke load:defaults (first_time)
** Execute load:defaults
cap aborted!
Don't know how to build task 'nginx_status'
/Users/H/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/rake-10.3.1/lib/rake/task_manager.rb:62:in `[]'
/Users/H/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/rake-10.3.1/lib/rake/application.rb:149:in `invoke_task'
/Users/H/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/rake-10.3.1/lib/rake/application.rb:106:in `block (2 levels) in top_level'
/Users/H/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/rake-10.3.1/lib/rake/application.rb:106:in `each'
/Users/H/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/rake-10.3.1/lib/rake/application.rb:106:in `block in top_level'
/Users/H/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/rake-10.3.1/lib/rake/application.rb:115:in `run_with_threads'
/Users/H/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/rake-10.3.1/lib/rake/application.rb:100:in `top_level'
/Users/H/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/rake-10.3.1/lib/rake/application.rb:78:in `block in run'
/Users/H/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/rake-10.3.1/lib/rake/application.rb:176:in `standard_exception_handling'
/Users/H/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/rake-10.3.1/lib/rake/application.rb:75:in `run'
/Users/H/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/capistrano-3.2.1/lib/capistrano/application.rb:15:in `run'
/Users/H/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/capistrano-3.2.1/bin/cap:3:in `<top (required)>'
/Users/H/.rbenv/versions/2.1.1/bin/cap:23:in `load'
/Users/H/.rbenv/versions/2.1.1/bin/cap:23:in `<main>'

I also noticed that if I ran cap -vT the task nginx_status is not in the list(?)

On a side note, are SSH keys a must to connect to remote servers?

UPDATE 1

I found that if I place my task in config/deploy.rb it works. So the question I now have is how do you run tasks from the task dir?

UPDATE 2

So as per the Capfile the tasks dir is looked at by the following:

# Loads custom tasks from `lib/capistrano/tasks' if you have any defined.
Dir.glob('lib/capistrano/tasks/*.rake').each { |r| import r }

So I changed the task extension to .rake and it worked. However the documentation says it should be .cap

"We'll dive into those more later, but add those lines to a file in ./lib/capistrano/tasks, call it something like access_check.cap"

Was it helpful?

Solution

Take a look at this commit.

They've switched from .cap to .rake 3 months ago.

The whole idea is to make capistrano3 fully rake-compatible. Just use .rake extension for you tasks.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top