7

Capistranoタスクで現在のサーバーをどのように参照しますか?ローカルファイルでAPCキャッシュをクリアしたいのですcurlが、サーバーがリッスンしないlocalhostため、サーバーのIPアドレスが必要です。

例えば、

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

curlタスクを1.1.1.1で実行するとshttp://1.1.1.1/deploy/clearAPC.phpを呼び出すが、2.2.2.2で実行するとcurlsを呼び出すように、どの変数を使用しますかhttp://2.2.2.2/deploy/clearAPC.php

4

6 に答える 6

28

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

于 2012-11-20T11:53:00.617 に答える
11

There's the magical $CAPISTRANO:HOST$

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

should do exactly what you want.

note: don't use it as a variable via string interpolation, capistrano will just replace the $CAPISTRANO:HOST$ in the string itself.

That's a very weird and (afaik) undocumented feature :-)

于 2012-06-15T16:13:18.573 に答える
3
current_host = capture("echo $CAPISTRANO:HOST$").strip
于 2013-08-28T13:45:22.457 に答える
1

Capistrano 3 - server is available as an object, with set of properties, in the roles block :

on roles(:app) do |server|

So for example to pass the hostname property to docker compose

  on roles(:app)  do |server|
    execute "docker-compose --build-arg SOME_HOST_NEEDED=#{server.hostname} ......
  end
于 2020-01-07T15:42:45.543 に答える
0

I wanted to know the current server I was deploying to, so that I could send a message to campfire. This is what I was able to figure out, though 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 deploy it posts this to campfire I am deploying 20121206154442 using cap QA2 campfire:notify deploy deploy:flex_master

于 2012-12-07T23:51:32.130 に答える
0

capistrano (2.13.5) required

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

figured this out by

puts current_task.namespace.logger.inspect
于 2014-01-30T21:32:47.167 に答える