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

Merge pull request #2597 from rackspace/ssh_host

[core] Add ability to specify IP Address for SSH, SCP and sshable?
This commit is contained in:
Wesley Beary 2014-01-24 08:09:14 -08:00
commit f531f0bdd7
18 changed files with 90 additions and 41 deletions

View file

@ -210,7 +210,7 @@ module Fog
end
def setup(credentials = {})
requires :public_ip_address, :username
requires :ssh_ip_address, :username
require 'net/ssh'
commands = [
@ -225,7 +225,7 @@ module Fog
# wait for aws to be ready
wait_for { sshable?(credentials) }
Fog::SSH.new(public_ip_address, username, credentials).run(commands)
Fog::SSH.new(ssh_ip_address, username, credentials).run(commands)
end
def start

View file

@ -100,7 +100,7 @@ module Fog
def setup(credentials = {})
requires :identity, :ips, :public_key, :username
Fog::SSH.new(public_ip_address, username, credentials).run([
Fog::SSH.new(ssh_ip_address, username, credentials).run([
%{mkdir .ssh},
%{echo "#{public_key}" >> ~/.ssh/authorized_keys},
%{passwd -l #{username}},

View file

@ -96,8 +96,8 @@ module Fog
end
def setup(credentials = {})
requires :public_ip_address, :identity, :public_key, :username
Fog::SSH.new(public_ip_address, username, credentials).run([
requires :ssh_ip_address, :identity, :public_key, :username
Fog::SSH.new(ssh_ip_address, username, credentials).run([
%{mkdir .ssh},
%{echo "#{public_key}" >> ~/.ssh/authorized_keys},
%{passwd -l #{username}},

View file

@ -42,6 +42,9 @@ module Fog
attr_accessor :network_ids, :disk_offering_id, :ip_address, :ip_to_network_list
attr_writer :security_group_ids
alias_method :public_ip_address, :ip_address
alias_method :public_ip_address=, :ip_address=
def addresses
nics.map{|nic| Address.new(nic)}
end

View file

@ -28,10 +28,36 @@ module Fog
@public_key ||= public_key_path && File.read(public_key_path)
end
# Port used for ssh/scp interactions with server.
# @return [Integer] IP port
# @note By default this returns 22
def ssh_port
@ssh_port ||= 22
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.
# @return [String] IP Address
# @note By default this returns the public_ip_address
def ssh_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
def ssh_options
@ssh_options ||= {}
ssh_options = @ssh_options.merge({:port => ssh_port})
@ -44,31 +70,31 @@ module Fog
def scp(local_path, remote_path, upload_options = {})
require 'net/scp'
requires :public_ip_address, :username
requires :ssh_ip_address, :username
Fog::SCP.new(public_ip_address, username, ssh_options).upload(local_path, remote_path, upload_options)
Fog::SCP.new(ssh_ip_address, username, ssh_options).upload(local_path, remote_path, upload_options)
end
alias_method :scp_upload, :scp
def scp_download(remote_path, local_path, download_options = {})
require 'net/scp'
requires :public_ip_address, :username
requires :ssh_ip_address, :username
Fog::SCP.new(public_ip_address, username, ssh_options).download(remote_path, local_path, download_options)
Fog::SCP.new(ssh_ip_address, username, ssh_options).download(remote_path, local_path, download_options)
end
def ssh(commands, options={}, &blk)
require 'net/ssh'
requires :public_ip_address, :username
requires :ssh_ip_address, :username
options = ssh_options.merge(options)
Fog::SSH.new(public_ip_address, username, options).run(commands, &blk)
Fog::SSH.new(ssh_ip_address, username, options).run(commands, &blk)
end
def sshable?(options={})
ready? && !public_ip_address.nil? && !!Timeout::timeout(8) { ssh('pwd', options) }
ready? && !ssh_ip_address.nil? && !!Timeout::timeout(8) { ssh('pwd', options) }
rescue SystemCallError, Net::SSH::AuthenticationFailed, Net::SSH::Disconnect, Timeout::Error
false
end

View file

@ -83,7 +83,7 @@ module Fog
end
def setup(credentials = {})
requires :public_ip_address
requires :ssh_ip_address
require 'net/ssh'
commands = [
@ -98,7 +98,7 @@ module Fog
# wait for aws to be ready
wait_for { sshable?(credentials) }
Fog::SSH.new(public_ip_address, username, credentials).run(commands)
Fog::SSH.new(ssh_ip_address, username, credentials).run(commands)
end
# Creates the server (not to be called directly).

View file

@ -79,7 +79,7 @@ module Fog
end
def setup(credentials = {})
requires :public_ip_address, :username
requires :ssh_ip_address, :username
require 'net/ssh'
attrs = attributes.dup
@ -100,7 +100,7 @@ module Fog
# wait for glesys to be ready
wait_for { sshable?(credentials) }
Fog::SSH.new(public_ip_address, username, credentials).run(commands)
Fog::SSH.new(ssh_ip_address, username, credentials).run(commands)
end
def ssh(command, options={}, &block)

View file

@ -71,8 +71,8 @@ module Fog
end
def setup(credentials = {})
requires :identity, :public_ip_address, :public_key, :username
Fog::SSH.new(public_ip_address, username, credentials).run([
requires :identity, :ssh_ip_address, :public_key, :username
Fog::SSH.new(ssh_ip_address, username, credentials).run([
%{mkdir .ssh},
%{echo "#{public_key}" >> ~/.ssh/authorized_keys},
%{passwd -l root},

View file

@ -267,8 +267,8 @@ module Fog
end
def setup(credentials = {})
requires :public_ip_address, :identity, :public_key, :username
Fog::SSH.new(public_ip_address, username, credentials).run([
requires :ssh_ip_address, :identity, :public_key, :username
Fog::SSH.new(ssh_ip_address, username, credentials).run([
%{mkdir .ssh},
%{echo "#{public_key}" >> ~/.ssh/authorized_keys},
%{passwd -l #{username}},

View file

@ -296,8 +296,8 @@ module Fog
end
def setup(credentials = {})
requires :public_ip_address, :identity, :public_key, :username
Fog::SSH.new(public_ip_address, username, credentials).run([
requires :ssh_ip_address, :identity, :public_key, :username
Fog::SSH.new(ssh_ip_address, username, credentials).run([
%{mkdir .ssh},
%{echo "#{public_key}" >> ~/.ssh/authorized_keys},
%{passwd -l #{username}},

View file

@ -135,7 +135,7 @@ module Fog
end
def ssh(commands)
requires :public_ip_address, :username
requires :ssh_ip_address, :username
ssh_options={}
ssh_options[:password] = password unless password.nil?
@ -153,19 +153,19 @@ module Fog
# Transfers a file
def scp(local_path, remote_path, upload_options = {})
requires :public_ip_address, :username
requires :ssh_ip_address, :username
scp_options = {}
scp_options[:password] = password unless self.password.nil?
scp_options[:key_data] = [private_key] if self.private_key
scp_options[:proxy]= ssh_proxy unless self.ssh_proxy.nil?
Fog::SCP.new(public_ip_address, username, scp_options).upload(local_path, remote_path, upload_options)
Fog::SCP.new(ssh_ip_address, username, scp_options).upload(local_path, remote_path, upload_options)
end
# Sets up a new key
def setup(credentials = {})
requires :public_key, :public_ip_address, :username
requires :public_key, :ssh_ip_address, :username
credentials[:proxy]= ssh_proxy unless ssh_proxy.nil?
credentials[:password] = password unless self.password.nil?
@ -184,7 +184,7 @@ module Fog
Timeout::timeout(360) do
begin
Timeout::timeout(8) do
Fog::SSH.new(public_ip_address, username, credentials.merge(:timeout => 4)).run('pwd')
Fog::SSH.new(ssh_ip_address, username, credentials.merge(:timeout => 4)).run('pwd')
end
rescue Errno::ECONNREFUSED
sleep(2)
@ -193,7 +193,7 @@ module Fog
retry
end
end
Fog::SSH.new(public_ip_address, username, credentials).run(commands)
Fog::SSH.new(ssh_ip_address, username, credentials).run(commands)
end
def update_display attrs = {}

View file

@ -305,8 +305,8 @@ module Fog
end
def setup(credentials = {})
requires :public_ip_address, :identity, :public_key, :username
Fog::SSH.new(public_ip_address, username, credentials).run([
requires :ssh_ip_address, :identity, :public_key, :username
Fog::SSH.new(ssh_ip_address, username, credentials).run([
%{mkdir .ssh},
%{echo "#{public_key}" >> ~/.ssh/authorized_keys},
%{passwd -l #{username}},

View file

@ -80,8 +80,8 @@ module Fog
end
def setup(credentials = {})
requires :public_ip_address, :identity, :public_key, :username
Fog::SSH.new(public_ip_address, username, credentials).run([
requires :ssh_ip_address, :identity, :public_key, :username
Fog::SSH.new(ssh_ip_address, username, credentials).run([
%{mkdir .ssh},
%{echo "#{public_key}" >> ~/.ssh/authorized_keys},
%{passwd -l #{username}},

View file

@ -521,7 +521,7 @@ module Fog
# Setup server for SSH access
# @see Servers#bootstrap
def setup(credentials = {})
requires :public_ip_address, :identity, :public_key, :username
requires :ssh_ip_address, :identity, :public_key, :username
commands = [
%{mkdir .ssh},
@ -534,7 +534,7 @@ module Fog
@password = nil if password_lock
Fog::SSH.new(public_ip_address, username, credentials).run(commands)
Fog::SSH.new(ssh_ip_address, username, credentials).run(commands)
rescue Errno::ECONNREFUSED
sleep(1)
retry

View file

@ -185,19 +185,19 @@ module Fog
# SCP something to our VM.
def scp(local_path, remote_path, upload_options = {})
requires :ipaddress, :username
requires :ssh_ip_address, :username
scp_options = {}
scp_options[:password] = password unless self.password.nil?
scp_options[:key_data] = [private_key] if self.private_key
Fog::SCP.new(ipaddress, username, scp_options).upload(local_path, remote_path, upload_options)
Fog::SCP.new(ssh_ip_address, username, scp_options).upload(local_path, remote_path, upload_options)
end
# Sets up a new SSH key on the VM so one doesn't need to use a password
# ever again.
def setup(credentials = {})
requires :public_key, :ipaddress, :username
requires :public_key, :ssh_ip_address, :username
credentials[:password] = password unless self.password.nil?
credentails[:key_data] = [private_key] if self.private_key
@ -213,7 +213,7 @@ module Fog
Timeout::timeout(360) do
begin
Timeout::timeout(8) do
Fog::SSH.new(ipaddress, username, credentials.merge(:timeout => 4)).run('pwd')
Fog::SSH.new(ssh_ip_address, username, credentials.merge(:timeout => 4)).run('pwd')
end
rescue Errno::ECONNREFUSED
sleep(2)
@ -222,7 +222,7 @@ module Fog
retry
end
end
Fog::SSH.new(ipaddress, username, credentials).run(commands)
Fog::SSH.new(ssh_ip_address, username, credentials).run(commands)
end
private

View file

@ -11,14 +11,32 @@ for provider, config in compute_providers
if Fog.mocking? && !config[:mocked]
pending
else
responds_to(:boostrap)
responds_to(:public_ip_address)
responds_to(:scp)
responds_to(:ssh)
end
tests('ssh_ip_address') do
tests('defaults to public_ip_address').returns(true) do
@instance.ssh_ip_address == @instance.public_ip_address
end
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'
@instance.ssh_ip_address = ip_address
@instance.ssh_ip_address == ip_address
end
end
end
end
end

View file

@ -10,6 +10,7 @@ def server_tests(connection, params = {}, mocks_implemented = true)
end
responds_to([:ready?, :state])
yield if block_given?
tests('#reboot').succeeds do
pending if Fog.mocking? && !mocks_implemented

View file

@ -4,6 +4,7 @@ def servers_tests(connection, params = {}, mocks_implemented = true)
if !Fog.mocking? || mocks_implemented
@instance.wait_for { ready? }
yield if block_given?
end
end