’ve been knee deep in capistrano lately, and I’ve come across in
interesting behavior:

Say you have the following:

role :app, "app"
role :web, "web"

task :run_on_web, :roles => :web do
  do_something_on_server
end

task :run_on_app, :roles => :app do
  do_something_on_server
end

task :do_something_on_server do
  run "/usr/sbin/something"
end

If you run the run_on_web task, it will execute the
do_something_on_server task using all roles. This isn’t the behavior
you most expected most likely. The secret to making this work is to
set ENV[‘HOSTS’] match your the set of hosts you are interested in.
After you are done you’ll want to set ENV[‘HOSTS’] hosts back to it’s
original contents. I’ve whipped up a quick method to make this even
easier.

def with_role(role, &block)
  original, ENV['HOSTS'] = ENV['HOSTS'], find_servers(:roles
=>role).map{|d| d.host}.join(",")
  begin
    yield
  ensure
    ENV['HOSTS'] = original
  end
end
You can call it like this:


role :app, "app"
role :web, "web"

task :run_on_web, :roles => :web do
  with_role :web do
    do_something_on_server
  end
end

task :run_on_app, :roles => :app do
  with_role :app do
    do_something_on_server
  end
end

task :do_something_on_server do
  run "/usr/sbin/something"
end


Hopefully this helps someone.

(also posted here: 
http://smartic.us/2008/5/6/keeping-capistrano-in-check-ensuring-roles-are-respected-in-sub-tasks)
--~--~---------~--~----~------------~-------~--~----~
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/capistrano
-~----------~----~----~----~------~----~------~--~---

Reply via email to