The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
Keeping capistrano in check: ensuring roles are respected in sub-tasks

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Bryan Liles

Posts: 228
Nickname: bryanl
Registered: May, 2008

Bryan Liles is the Principal Web Developer for Sourcefire, Inc.
Keeping capistrano in check: ensuring roles are respected in sub-tasks Posted: May 20, 2008 8:36 PM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Bryan Liles.
Original Post: Keeping capistrano in check: ensuring roles are respected in sub-tasks
Feed Title: Smarticus
Feed URL: http://smartic.us/feed/atom.xml
Feed Description: Ramblings of a ruby hacker
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Bryan Liles
Latest Posts From Smarticus

Advertisement

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

Say you have the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 
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.

1
2
3
4
5
6
7
8
9

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

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.

Read: Keeping capistrano in check: ensuring roles are respected in sub-tasks

Topic: Updating Rubinius Previous Topic   Next Topic Topic: Rubinius on OS X

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use