Which server is this task being executed on?

336 views
Skip to first unread message

Tom Copeland

unread,
May 16, 2008, 4:27:55 PM5/16/08
to capis...@googlegroups.com
Hi all -

I've got a task that needs to know which server it's currently being
executed on. Is there a variable containing this value that the task
can get at? e.g.,

task :foobar, :roles => app do
echo "I'm running on the server #{fetch(:something)}"
end

I could get this info by parsing out `hostname` or something, but was
hoping to avoid that... is there a "current_session" or something?

Thanks,

tom

Jamis Buck

unread,
May 16, 2008, 4:54:38 PM5/16/08
to capis...@googlegroups.com
Note that a task is not executed once per matching server, it is
executed once, for all matching servers. So, depending on what you need:

1. You simply want the host name to appear somewhere in a command
you're running in the current task:

run "echo server is $CAPISTRANO:HOST$"

The $CAPISTRANO:HOST$ text gets replaced with the actual hostname
immediately before the command is executed on each server.

2. You want to know which servers a task is restricted to:

servers = find_servers_for_task(current_task)
puts "servers: #{servers.join(', ')}"

Hope that helps,

Jamis

> --~--~---------~--~----~------------~-------~--~----~
> To unsubscribe from this group, send email to capistrano-...@googlegroups.com
> For more options, visit this group at http://groups.google.com/group/capistrano
> -~----------~----~----~----~------~----~------~--~---
>

Tom Copeland

unread,
May 16, 2008, 5:04:06 PM5/16/08
to capis...@googlegroups.com

On Fri, 2008-05-16 at 14:54 -0600, Jamis Buck wrote:
> Note that a task is not executed once per matching server, it is
> executed once, for all matching servers. So, depending on what you need:
>
> 1. You simply want the host name to appear somewhere in a command
> you're running in the current task:
>
> run "echo server is $CAPISTRANO:HOST$"
>
> The $CAPISTRANO:HOST$ text gets replaced with the actual hostname
> immediately before the command is executed on each server.

Perfect! I was reading through command.rb but was just looking in
open_channels and glazed right over that call to replace_placeholders.
Thanks much!

Yours,

Tom


Reply all
Reply to author
Forward
0 new messages