From 9d217025fac3e335c308f02e7377e14ccfdc0e66 Mon Sep 17 00:00:00 2001 From: Saku Ytti Date: Wed, 17 Apr 2013 17:48:50 +0300 Subject: Initial commit Silly for shit-and-giggles attempt at rancid --- lib/oxidized/node.rb | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 lib/oxidized/node.rb (limited to 'lib/oxidized/node.rb') diff --git a/lib/oxidized/node.rb b/lib/oxidized/node.rb new file mode 100644 index 0000000..740834a --- /dev/null +++ b/lib/oxidized/node.rb @@ -0,0 +1,69 @@ +module Oxidized + require 'resolv' + class MethodNotFound < StandardError; end + class ModelNotFound < StandardError; end + class Node + attr_reader :name, :ip, :model, :input, :output, :group, :auth, :prompt + attr_accessor :last, :running + alias :running? :running + def initialize opt + @name = opt[:name] + @ip = Resolv.getaddress @name + @group = opt[:group] + @input, @output = resolve_io opt + @model = resolve_model opt + @auth = resolve_auth opt + @prompt = resolve_prompt opt + end + + 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 + end + [status, config] + end + + private + + def resolve_prompt opt + prompt = opt[:prompt] + prompt ||= @model.prompt + prompt ||= CFG.prompt + end + + def resolve_auth opt + auth = {} + auth[:username] = (opt[:username] or CFG.username) + auth[:password] = (opt[:passowrd] or CFG.password) + 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" + end + if not mgr.output[output] + mgr.output = output or raise MethodNotFound, "#{output} not found" + end + [ mgr.input[input], mgr.output[output] ] + end + + def resolve_model opt + model = (opt[:model] or CFG.model) + mgr = Oxidized.mgr + if not mgr.model[model] + mgr.model = model or raise ModelNotFound, "#{model} not found" + end + mgr.model[model].new + end + + end +end -- cgit v1.2.1