From 5a4d2ce1a0380a47e51a9c7aa248e82a5ba5cd39 Mon Sep 17 00:00:00 2001 From: Saku Ytti Date: Sun, 21 Apr 2013 16:37:26 +0300 Subject: Add input method fallback, e.g. if ssh does not work, try telnet --- lib/oxidized/config/bootstrap.rb | 2 +- lib/oxidized/input/ssh.rb | 2 +- lib/oxidized/node.rb | 41 ++++++++++++++++++++++++---------------- 3 files changed, 27 insertions(+), 18 deletions(-) (limited to 'lib/oxidized') diff --git a/lib/oxidized/config/bootstrap.rb b/lib/oxidized/config/bootstrap.rb index c3ea09d..6d865b5 100644 --- a/lib/oxidized/config/bootstrap.rb +++ b/lib/oxidized/config/bootstrap.rb @@ -12,7 +12,7 @@ module Oxidized CFG.prompt = /^([\w\.\-@]{3,30}[#>]\s?)$/ CFG.rest = 8888 CFG.input = { - :default => 'ssh', + :default => 'ssh, telnet', } CFG.output = { :default => 'git', diff --git a/lib/oxidized/input/ssh.rb b/lib/oxidized/input/ssh.rb index 45c89ba..3471eea 100644 --- a/lib/oxidized/input/ssh.rb +++ b/lib/oxidized/input/ssh.rb @@ -12,7 +12,7 @@ module Oxidized begin @ssh = Net::SSH.start @node.ip, @node.auth[:username], :password => @node.auth[:password], :timeout => CFG.timeout - rescue Timeout::Error, Net::SSH::Disconnect + rescue Timeout::Error, Net::SSH::Disconnect, Errno::ECONNREFUSED return false end @ses = open_shell @ssh unless @exec diff --git a/lib/oxidized/node.rb b/lib/oxidized/node.rb index 35a5948..a41fcdc 100644 --- a/lib/oxidized/node.rb +++ b/lib/oxidized/node.rb @@ -10,7 +10,8 @@ module Oxidized @name = opt[:name] @ip = Resolv.getaddress @name @group = opt[:group] - @input, @output = resolve_io opt + @input = resolve_input opt + @output = resolve_output opt @model = resolve_model opt @auth = resolve_auth opt @prompt = resolve_prompt opt @@ -18,12 +19,15 @@ module Oxidized def run status, config = :fail, nil - @model.input = input = @input.new - if input.connect self - config = input.get - status = :success if config - else - status = :no_cconnection + @input.each do |input| + @model.input = input = input.new + if input.connect self + config = input.get + status = :success if config + break + else + status = :no_cconnection + end end [status, config] end @@ -66,17 +70,22 @@ module Oxidized auth end - def resolve_io opt - input = (opt[:input] or CFG.input[:default]) - output = (opt[:output] or CFG.output[:default]) - mgr = Oxidized.mgr - if not mgr.input[input] - mgr.input = input or raise MethodNotFound, "#{input} not found" + def resolve_input opt + inputs = (opt[:input] or CFG.input[:default]) + inputs.split(/\s*,\s*/).map do |input| + if not Oxidized.mgr.input[input] + Oxidized.mgr.input = input or raise MethodNotFound, "#{input} not found" + end + Oxidized.mgr.input[input] end - if not mgr.output[output] - mgr.output = output or raise MethodNotFound, "#{output} not found" + end + + def resolve_output opt + output = (opt[:output] or CFG.output[:default]) + if not Oxidized.mgr.output[output] + Oxidized.mgr.output = output or raise MethodNotFound, "#{output} not found" end - [ mgr.input[input], mgr.output[output] ] + Oxidized.mgr.output[output] end def resolve_model opt -- cgit v1.2.1