lib/chef_metal/transport/ssh.rb in chef-metal-0.11.beta.6 vs lib/chef_metal/transport/ssh.rb in chef-metal-0.11.beta.7
- old
+ new
@@ -123,52 +123,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 +207,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