views:

29

answers:

2

I'm using Capistrano to handle my deployment, and I've got two different roles in my setup - :web and :processing. They both have the usual :deploy tasks, but the :restart task needs to be different for the two types of server.

So my first attempt was something like this:

task :restart, :roles => :web do
    run "... web related restart stuff ..."
end

task :restart, :roles => :processing do
    run "... processing related restart stuff ..."
end

Which doesn't work, because the second :restart (for :processing role) replaces the first :restart (for the :web role), and the :web :restart never happens.

I had a quick look around to see if I could write conditional code depending on which role (or roles) a server might be in when the task is run but there's next to no documentation out there for that kind of thing. Any ideas?

+2  A: 

You should use namespaces:

namespace :web do
  desc "Restart web servers"
  task :restart, :roles => :web do
    # Restart Magic Here
  end
end

namespace :process do
  desc "Restart process servers"
  task :restart, :roles => :process do
    # Restart magic here
  end
end

# Optionally:
task :restart do
  web.restart
  process.restart
end

That's what you're looking for I think!

Also, to use these on the command line, you would use

$ cap <stage>           # (if using multistage)
$ cap web:restart       # Restarts web servers
$ cap process:restart   # Restarts process servers
$ cap restart           # Restarts both process and web servers

(Source: I'm the Capistrano maintainer.)

Beaks
+1  A: 

Found the same question and (pretty good) answer here: http://stackoverflow.com/questions/754015/creating-a-capistrano-task-that-performs-different-tasks-based-on-role

Tim Macfarlane