In capistrano, can I retrieve server info from a role symbol?
I am using Capistrano to deploy an application in a nonstandard manner and I have a very large file that I upload to multiple servers through a gateway. I would like, instead of having capistrano upload this file from the deploying machine to every server in the list, to upload it to the gateway server and then scp it around the LAN from there (this is much faster when I test it by hand).
I'm having trouble finding a way to get capistrano开发者_Python百科 to do this. I can use something like:
myservers = find_servers_for_task(current_task)
myservers.each do |server|
run "scp HARDCODED_GATEWAY:/remote/file /local/file", :hosts => server.host
end
I don't like the hardcoded gateway though; I've got this defined above in a role named :gateway. Is there a way to extract the host name from the :gateway symbol? I know that there could be multiple servers defined in a role but I would be satisfied to have the server information for just the first server in the role array -- or the last; for my purposes there will only ever be one server in it, but WHICH server that is could change.
I'm fairly new to ruby, so I might be reading this wrong, but I think you can get the host from the role with roles[:gateway].servers[0].host
. This seems to answer the question you have asked.
You can set variables specific for the capistrano recipe via
set :gateway, "HARDCODED_GATEWAY:/remote/file"
and later use it:
myservers = find_servers_for_task(current_task)
myservers.each do |server|
run "scp #{gateway} /local/file", :hosts => server.host
end
If you want to input it at runtime, use the following:
set(:gateway) { Capistrano::CLI.ui.ask("gateway: ") }
More information can be found in the capistrano wiki.
精彩评论