lib/chef_metal/transport/ssh.rb in clc-fork-chef-metal-0.11.beta.6 vs lib/chef_metal/transport/ssh.rb in clc-fork-chef-metal-0.11.2.alpha.1

- old
+ new

@@ -107,15 +107,14 @@ end def make_url_available_to_remote(local_url) uri = URI(local_url) host = Socket.getaddrinfo(uri.host, uri.scheme, nil, :STREAM)[0][3] - if host == '127.0.0.1' || host == '[::1]' - unless session.forward.active_remotes.any? { |port, bind| port == uri.port && bind == '127.0.0.1' } - # TODO IPv6 - Chef::Log.debug("Forwarding local server 127.0.0.1:#{uri.port} to port #{uri.port} on #{username}@#{host}") - session.forward.remote(uri.port, '127.0.0.1', uri.port) + if host == '127.0.0.1' || host == '::1' + unless session.forward.active_remotes.any? { |port, bind| port == uri.port && bind == uri.host } + Chef::Log.debug("Forwarding local server #{uri.host}:#{uri.port} to port #{uri.port} on #{username}@#{self.host}") + session.forward.remote(uri.port, uri.host, uri.port) end end local_url end @@ -123,52 +122,32 @@ if @session begin Chef::Log.debug("Closing SSH session on #{username}@#{host}") @session.close rescue + ensure + @session = nil end - @session = nil end end def available? # If you can't pwd within 10 seconds, you can't pwd execute('pwd', :timeout => 10) true rescue Timeout::Error, Errno::EHOSTUNREACH, Errno::ETIMEDOUT, Errno::ECONNREFUSED, Errno::ECONNRESET, Net::SSH::Disconnect Chef::Log.debug("#{username}@#{host} unavailable: network connection failed or broke: #{$!.inspect}") + disconnect false rescue Net::SSH::AuthenticationFailed, Net::SSH::HostKeyMismatch Chef::Log.debug("#{username}@#{host} unavailable: SSH authentication error: #{$!.inspect} ") + disconnect false end protected - def gateway? - options.key?(:ssh_gateway) and ! options[:ssh_gateway].nil? - end - - def gateway - @gateway ||= begin - gw_host, gw_user = options[:ssh_gateway].split('@').reverse - gw_host, gw_port = gw_host.split(':') - gw_user = ssh_options[:ssh_username] unless gw_user - - ssh_start_opts = { timeout:10 }.merge(ssh_options) - ssh_start_opts[:port] = gw_port || 22 - - Chef::Log.debug("Opening SSH gateway to #{gw_user}@#{gw_host} with options #{ssh_start_opts.inspect}") - begin - Net::SSH::Gateway.new(gw_host, gw_user, ssh_start_opts) - rescue Errno::ETIMEDOUT - Chef::Log.debug("Timed out connecting to gateway: #{$!}") - raise InitialConnectTimeout.new($!) - end - end - end - def session @session ||= begin ssh_start_opts = { timeout:10 }.merge(ssh_options) Chef::Log.debug("Opening SSH connection to #{username}@#{host} with options #{ssh_start_opts.inspect}") # Small initial connection timeout (10s) to help us fail faster when server is just dead @@ -227,9 +206,32 @@ super(original_error.message) @original_error = original_error end attr_reader :original_error + end + + private + + def gateway? + options.key?(:ssh_gateway) and ! options[:ssh_gateway].nil? + end + + def gateway + gw_host, gw_user = options[:ssh_gateway].split('@').reverse + gw_host, gw_port = gw_host.split(':') + gw_user = ssh_options[:ssh_username] unless gw_user + + ssh_start_opts = { timeout:10 }.merge(ssh_options) + ssh_start_opts[:port] = gw_port || 22 + + Chef::Log.debug("Opening SSH gateway to #{gw_user}@#{gw_host} with options #{ssh_start_opts.inspect}") + begin + Net::SSH::Gateway.new(gw_host, gw_user, ssh_start_opts) + rescue Errno::ETIMEDOUT + Chef::Log.debug("Timed out connecting to gateway: #{$!}") + raise InitialConnectTimeout.new($!) + end end end end end