Callbacks and roles

387 views
Skip to first unread message

Olivier Brisse

unread,
Dec 18, 2008, 11:02:41 AM12/18/08
to Capistrano
Hi everyone,

I'm trying to deploy a non Rails application with capistrano 2.5.3 to
two server sets with different configurations.
Basically, I have two roles in my deploy.rb :
role :role1, "server1.example.com", "server2.example.com"
role :role2, "server3.example.com", "server4.example.com"

And I have a task that should be run only on role2 server set after
deploy:restart.

So my recipe looks like this :

namespace :custom do
task :my_custom_task, :roles => :role2 do
run "some_custom_script"
end
end

namespace :deploy do
task :restart, :roles => [:role1, :role2] do
run "restart_script"
end
end

after "deploy:restart" do
puts "foo"
custom.my_custom_task
end

But when i run "cap deploy:restart ROLES=role1", "my_custom_task" is
run after deploy:restart...

Did i miss something ?

Thanks,
--
Olivier

Jamis Buck

unread,
Dec 18, 2008, 11:16:59 AM12/18/08
to capis...@googlegroups.com
The ROLES environment variable is an override, not a filter. This means
it will force all tasks that run, to run on the given roles.

There is not currently a way to specify a role filter. If you really
want that task to run ONLY on the servers in role2, you'd need to
manually guard that task:

task :my_custom_task, :roles => :role2 do
servers = find_servers_for_task(current_task) & roles[:role2].servers
run "whatever", :hosts => servers if servers.any?
end

What that does is find all the servers that capistrano WANTS to execute
the task on (via find_servers_for_task), intersects that with the
servers in role2, and then runs the command against those servers,
explicitly.

You can tell this is a use case that Capistrano was not designed for. :/

- Jamis

Olivier Brisse

unread,
Dec 18, 2008, 11:31:18 AM12/18/08
to Capistrano
Ok, thanks. I've understood.

It's right that if I run cap deploy:restart, my_custom_task runs only
on role2 servers.

Tony Pitluga

unread,
Dec 18, 2008, 2:20:17 PM12/18/08
to capis...@googlegroups.com
You could also put your roles definitions inside of a task:

task :on_role1 doend

task :on_role2 do end


You could then

Tony Pitluga

unread,
Dec 18, 2008, 2:21:43 PM12/18/08
to capis...@googlegroups.com
*blush* - so that's the keyboard shortcut to send a mail in gmail:

Anyways... to complete that thought, you would then call it from the command line like this:

cap on_role1 deploy

Tony
Reply all
Reply to author
Forward
0 new messages