1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00

[core] updating ssh_ip_address to take a block in order to defer address specification to execution time.

This commit is contained in:
Kyle Rames 2014-01-24 08:30:00 -06:00
parent 62bef4b5dc
commit fc6b13f285
2 changed files with 28 additions and 4 deletions

View file

@ -4,7 +4,7 @@ module Fog
module Compute module Compute
class Server < Fog::Model class Server < Fog::Model
attr_writer :username, :private_key, :private_key_path, :public_key, :public_key_path, :ssh_port, :ssh_ip_address, :ssh_options attr_writer :username, :private_key, :private_key_path, :public_key, :public_key_path, :ssh_port, :ssh_options
def username def username
@username ||= 'root' @username ||= 'root'
@ -35,11 +35,27 @@ module Fog
@ssh_port ||= 22 @ssh_port ||= 22
end end
# Sets the proc used to determine the IP Address used for ssh/scp interactions.
# @example
# service.servers.bootstrap :name => 'bootstrap-server',
# :flavor_id => service.flavors.first.id,
# :image_id => service.images.find {|img| img.name =~ /Ubuntu/}.id,
# :public_key_path => '~/.ssh/fog_rsa.pub',
# :private_key_path => '~/.ssh/fog_rsa',
# :ssh_ip_address => Proc.new {|server| server.private_ip_address }
#
# @note By default scp/ssh will use the public_ip_address if this proc is not set.
def ssh_ip_address=(proc)
@ssh_ip_address = proc
end
# IP Address used for ssh/scp interactions with server. # IP Address used for ssh/scp interactions with server.
# @return [String] IP Address # @return [String] IP Address
# @note By default this returns the public_ip_address # @note By default this returns the public_ip_address
def ssh_ip_address def ssh_ip_address
@ssh_ip_address ||= public_ip_address return public_ip_address unless @ssh_ip_address
return @ssh_ip_address.call(self) if @ssh_ip_address.is_a?(Proc)
@ssh_ip_address
end end
def ssh_options def ssh_options

View file

@ -20,9 +20,17 @@ for provider, config in compute_providers
tests('defaults to public_ip_address').returns(true) do tests('defaults to public_ip_address').returns(true) do
@instance.ssh_ip_address == @instance.public_ip_address @instance.ssh_ip_address == @instance.public_ip_address
end end
tests('ssh_ip_address overrides default').returns(true) do tests('ssh_ip_address overrides default with Proc').returns(true) do
ip_address = '5.5.5.5'
@instance.ssh_ip_address = Proc.new {|server| ip_address }
@instance.ssh_ip_address == ip_address
end
tests('Proc yields server').returns(true) do
@instance.ssh_ip_address = Proc.new {|server| server }
@instance.ssh_ip_address == @instance
end
tests('ssh_ip_address overrides default with String').returns(true) do
ip_address = '5.5.5.5' ip_address = '5.5.5.5'
# @instance.public_ip_address = '12.12.12.12'
@instance.ssh_ip_address = ip_address @instance.ssh_ip_address = ip_address
@instance.ssh_ip_address == ip_address @instance.ssh_ip_address == ip_address
end end