Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Referencing the current server in Capistrano task

How would I reference the current server in a Capistrano task? I want to curl a local file to clear the APC cache but the server does not listen on localhost so I need the server's IP address.

For instance,

role :web, "1.1.1.1", "2.2.2.2", "3.3.3.3"

task :clear_apc, :role => :web do
    run "curl http://#{WHAT_DO_I_PUT_HERE}/deploy/clearAPC.php"
end

What variable would I use so that when the task is run on 1.1.1.1 it curls http://1.1.1.1/deploy/clearAPC.php but when run on 2.2.2.2 it calls curls http://2.2.2.2/deploy/clearAPC.php

like image 813
Brad Dwyer Avatar asked Jun 15 '12 14:06

Brad Dwyer


1 Answers

In Capistrano, tasks don't get executed once for each server, run executes your command on each server. Here is what you should do instead:

task :clear_apc, :role => :web do
    find_servers_for_task(current_task).each do |current_server|

        run "curl http://#{current_server.host}/deploy/clearAPC.php", :hosts => current_server.host

    end
end

The accepted answer will work, but this one lets you access the servers as variables/methods

like image 62
user1158559 Avatar answered Oct 04 '22 22:10

user1158559