lib/drbman/host_machine.rb in royw-drbman-0.0.3 vs lib/drbman/host_machine.rb in royw-drbman-0.0.4
- old
+ new
@@ -55,10 +55,25 @@
@name = "#{user}@#{machine}:#{port}"
@ssh = nil
@logger.debug { self.pretty_inspect }
end
+ def alive?
+ result = true
+ begin
+ session do |host|
+ response = host.sh('uptime')
+ @logger.debug { "#{host.name} #{response}" }
+ raise Exception.new('no response from uptime') if response.strip.empty?
+ end
+ rescue Exception => e
+ @logger.warn { "#{@name} is not responding: #{e}" }
+ result = false
+ end
+ result
+ end
+
# Connect to the host, execute the given block, then disconnect from the host
# @yield [HostMachine]
# @example
# host_machine = HostMachine.new('localhost', @logger)
# host_machine.session do |host|
@@ -72,14 +87,11 @@
HostMachine.connection_mutex.synchronize do
connect
end
yield self
rescue Net::SSH::AuthenticationFailed => e
- @logger.error { "Authentication Failed" }
- rescue Exception => e
- @logger.error { e }
- @logger.error { e.backtrace.join("\n") }
+ @logger.error { "Authentication Failed for #{e}" }
raise e
ensure
disconnect
end
end
@@ -163,17 +175,25 @@
# connect to the host machine
# note, you should not need to call the connect method.
# @see {#session}
def connect
if @ssh.nil?
- options = @password.merge({
- :timeout=>2,
- :auth_methods => %w(publickey hostbased password)
- })
- options = @password.merge({:verbose=>Logger::DEBUG}) if @choices[:ssh_debug]
+ options = @password
+ options = options.merge({:verbose => Logger::INFO}) if @choices[:ssh_debug]
+ options = options.merge({:password => ''}) if options[:password].nil?
@logger.debug { "connect: @machine=>#{@machine}, @user=>#{@user}, options=>#{options.inspect}" }
- @ssh = Net::SSH.start(@machine, @user, options)
- # @ssh.forward.local(@port, @machine, @port)
+ begin
+ @ssh = Net::SSH.start(@machine, @user, options)
+ # @ssh.forward.local(@port, @machine, @port)
+ rescue Net::SSH::AuthenticationFailed
+ @logger.debug { "connect raised Net::SSH::AuthenticationFailed" }
+ @ssh = nil
+ raise Net::SSH::AuthenticationFailed.new(@name)
+ rescue Exception => e
+ @logger.debug { "connect raised #{e}"}
+ @ssh = nil
+ raise e
+ end
end
end
# disconnect from the host machine.
# note, you should not need to call the disconnect method.