Link to the current server in the Capistrano task

How can I refer to the current server in the Capistrano task? I want to curl local file to clear the APC cache, but the server is not listening on localhost , so I need the IP address of the server.

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 

Which variable will I use so that when performing the task in 1.1.1.1 it curl http://1.1.1.1/deploy/clearAPC.php , but when launched in 2.2.2.2 it calls curl http://2.2.2.2/deploy/clearAPC.php

+6
source share
5 answers

There's magic $ CAPISTRANO: HOST $

 run "curl http://$CAPISTRANO:HOST$/deploy/clearAPC.php" 

should do exactly what you want.

note: do not use it as a variable using string interpolation, capistrano will simply replace $ CAPISTRANO: HOST $ in the string itself.

This is a very strange and (afaik) undocumented function :-)

+11
source

In Capistrano, tasks are not performed once for each server; launching 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 allows you to access the servers as variables / methods

+28
source
 current_host = capture("echo $CAPISTRANO:HOST$").strip 
+3
source

I wanted to know the current server on which I was deploying so that I could send a message to the bonfire. This is what I was able to figure out, although I'm sure there is a better way

  actions = current_task.namespace.parent.logger.instance_variable_get('@options')[:actions] message = "I am deploying #{fetch(:latest_release).split('/').last} using cap #{actions.join(' ')}" 

so when I bred it, it goes to the bonfire I deploy 20121206154442 using a cap QA2: report deployment deploy: flex_master

0
source

capistrano (2.13.5) required

 puts current_task.namespace.logger.instance_variable_get('@base_logger').instance_variable_get('@options')[:actions].join(' ') 

got it

 puts current_task.namespace.logger.inspect 
0
source

Source: https://habr.com/ru/post/918197/


All Articles