diff options
author | Anton Aksola <anton.aksola@nebula.fi> | 2014-05-05 09:40:11 +0300 |
---|---|---|
committer | Anton Aksola <anton.aksola@nebula.fi> | 2014-05-05 09:40:11 +0300 |
commit | 16795edea8e14230b67b28e7e8503c2daffd69d2 (patch) | |
tree | 4005091b6167c400a59b309e28ee280423edce8f | |
parent | 34af261b7928ba7d62496d2d87fbe64b1badc930 (diff) |
Introduce node and group level vars
Variables can now be fed to model from multiple locations. In order of
preference:
1) node (from source)
2) group
3) global
In a model vars should be accessed via 'vars' helper method though it is
not required. Helper method ignores nil values so care needs to taken
when designing model behaviour.
Support for node level vars is currently available on sql source via
'vars_map' configuration.
Following example populates node vars 'enable' and 'somevariable' from sql
columns 'var_enable' and 'var_somevariable'
sql:
adapter: sqlite
file: /home/aakso/.config/oxidized/sqrouter.db
table: nodes
map:
name: hostname
model: model
group: node_group
username: username
password: password
vars_map:
enable: var_enablepw
somevariable: var_somevariable
-rw-r--r-- | lib/oxidized/config.rb | 12 | ||||
-rw-r--r-- | lib/oxidized/input/input.rb | 2 | ||||
-rw-r--r-- | lib/oxidized/model/acos.rb | 4 | ||||
-rw-r--r-- | lib/oxidized/model/ios.rb | 6 | ||||
-rw-r--r-- | lib/oxidized/model/iosxr.rb | 4 | ||||
-rw-r--r-- | lib/oxidized/model/model.rb | 4 | ||||
-rw-r--r-- | lib/oxidized/node.rb | 9 | ||||
-rw-r--r-- | lib/oxidized/source/sql.rb | 7 |
8 files changed, 38 insertions, 10 deletions
diff --git a/lib/oxidized/config.rb b/lib/oxidized/config.rb index 9e5ec60..fd120b6 100644 --- a/lib/oxidized/config.rb +++ b/lib/oxidized/config.rb @@ -9,6 +9,17 @@ module Oxidized ModelDir = File.join Directory, %w(lib oxidized model) SourceDir = File.join Directory, %w(lib oxidized source) Sleep = 1 + + module Vars + # convenience method for accessing node, group or global level user variables + # nil values will be ignored + def vars name + r = @node.vars[name] + r ||= CFG.groups[@node.group].vars[name.to_s] if CFG.groups.has_key?(@node.group) + r ||= CFG.vars[name.to_s] + end + end + end class << self attr_accessor :mgr @@ -25,6 +36,7 @@ module Oxidized CFGS.default.prompt = /^([\w.@-]+[#>]\s?)$/ CFGS.default.rest = '0.0.0.0:8888' # or false to disable CFGS.default.vars = {} # could be 'enable'=>'enablePW' + CFGS.default.groups = {} # group level configuration CFGS.default.remove_secret = false # runs cmd(:secret) blocks if true CFGS.default.input.default = 'ssh, telnet' diff --git a/lib/oxidized/input/input.rb b/lib/oxidized/input/input.rb index a64a1f4..769b196 100644 --- a/lib/oxidized/input/input.rb +++ b/lib/oxidized/input/input.rb @@ -1,5 +1,7 @@ module Oxidized class Input + include Oxidized::Config::Vars + RescueFail = { :debug => [ Errno::ECONNREFUSED, diff --git a/lib/oxidized/model/acos.rb b/lib/oxidized/model/acos.rb index f06bc93..75fbacf 100644 --- a/lib/oxidized/model/acos.rb +++ b/lib/oxidized/model/acos.rb @@ -55,10 +55,10 @@ class ACOS < Oxidized::Model cfg :telnet, :ssh do # preferred way to handle additional passwords - if CFG.vars.enable? + if vars :enable post_login do send "enable\n" - send CFG.vars.enable + "\n" + send vars(:enable) + "\n" end end post_login 'terminal length 0' diff --git a/lib/oxidized/model/ios.rb b/lib/oxidized/model/ios.rb index c0994c2..e557469 100644 --- a/lib/oxidized/model/ios.rb +++ b/lib/oxidized/model/ios.rb @@ -12,7 +12,7 @@ class IOS < Oxidized::Model # non-preferred way to handle additional PW prompt #expect /^[\w.]+>$/ do |data| # send "enable\n" - # send CFG.vars.enable + "\n" + # send vars(:enable) + "\n" # data #end @@ -45,10 +45,10 @@ class IOS < Oxidized::Model post_login 'terminal length 0' post_login 'terminal width 0' # preferred way to handle additional passwords - if CFG.vars.enable? + if vars :enable post_login do send "enable\n" - send CFG.vars.enable + "\n" + send vars(:enable) + "\n" end end pre_logout 'exit' diff --git a/lib/oxidized/model/iosxr.rb b/lib/oxidized/model/iosxr.rb index 023dcca..1129a0c 100644 --- a/lib/oxidized/model/iosxr.rb +++ b/lib/oxidized/model/iosxr.rb @@ -29,10 +29,10 @@ class IOSXR < Oxidized::Model cfg :telnet, :ssh do post_login 'terminal length 0' post_login 'terminal width 0' - if CFG.vars.enable? + if vars :enable post_login do send "enable\n" - send CFG.vars.enable + "\n" + send vars(:enable) + "\n" end end pre_logout 'exit' diff --git a/lib/oxidized/model/model.rb b/lib/oxidized/model/model.rb index 6e44039..277a01b 100644 --- a/lib/oxidized/model/model.rb +++ b/lib/oxidized/model/model.rb @@ -1,5 +1,7 @@ module Oxidized class Model + include Oxidized::Config::Vars + class << self def inherited klass klass.instance_variable_set '@cmd', Hash.new { |h,k| h[k] = [] } @@ -71,7 +73,7 @@ module Oxidized end end - attr_accessor :input + attr_accessor :input, :node def cmd string, &block out = @input.cmd string diff --git a/lib/oxidized/node.rb b/lib/oxidized/node.rb index fce4c21..0537e45 100644 --- a/lib/oxidized/node.rb +++ b/lib/oxidized/node.rb @@ -3,7 +3,7 @@ module Oxidized class MethodNotFound < OxidizedError; end class ModelNotFound < OxidizedError; end class Node - attr_reader :name, :ip, :model, :input, :output, :group, :auth, :prompt + attr_reader :name, :ip, :model, :input, :output, :group, :auth, :prompt, :vars attr_accessor :last, :running, :user, :msg, :from alias :running? :running def initialize opt @@ -15,6 +15,10 @@ module Oxidized @model = resolve_model opt @auth = resolve_auth opt @prompt = resolve_prompt opt + @vars = opt[:vars] + + # model instance needs to access node instance + @model.node = self end def run @@ -73,6 +77,7 @@ module Oxidized :group => @group, :model => @model.class.to_s, :last => nil, + :vars => @vars, } h[:full_name] = [@group, @name].join('/') if @group if @last @@ -138,7 +143,7 @@ module Oxidized if not Oxidized.mgr.model[model] Oxidized.mgr.add_model model or raise ModelNotFound, "#{model} not found for node #{ip}" end - Oxidized.mgr.model[model].new + Oxidized.mgr.model[model].new end end diff --git a/lib/oxidized/source/sql.rb b/lib/oxidized/source/sql.rb index 1c4c33e..a115da8 100644 --- a/lib/oxidized/source/sql.rb +++ b/lib/oxidized/source/sql.rb @@ -35,9 +35,16 @@ class SQL < Source Sequel.sqlite @cfg.file end db[@cfg.table.to_sym].each do |node| + # map node parameters keys = {} @cfg.map.each { |key, sql_column| keys[key.to_sym] = node[sql_column.to_sym] } keys[:model] = map_model keys[:model] if keys.key? :model + + # map node specific vars + vars = {} + @cfg.vars_map.each { |key, sql_column| vars[key.to_sym] = node[sql_column.to_sym] } + keys[:vars] = vars unless vars.empty? + nodes << keys end nodes |