From 5a393d6102655f575549311e6b250534b4dcbb59 Mon Sep 17 00:00:00 2001 From: Saku Ytti Date: Fri, 19 Apr 2013 14:58:58 +0300 Subject: Add 'cmd :all' convenience, ran after each command Easier screen-scraping, if each command produces always same set of cruft that needs to be removed --- lib/oxidized/api/rest.rb | 2 +- lib/oxidized/model/ios.rb | 8 ++++++-- lib/oxidized/model/junos.rb | 7 ++++--- lib/oxidized/model/model.rb | 15 +++++++++++---- 4 files changed, 22 insertions(+), 10 deletions(-) diff --git a/lib/oxidized/api/rest.rb b/lib/oxidized/api/rest.rb index ee06bc8..4a5fbca 100644 --- a/lib/oxidized/api/rest.rb +++ b/lib/oxidized/api/rest.rb @@ -7,7 +7,7 @@ module Oxidized @nodes = nodes addr, port = listen.to_s.split ':' port, addr = addr, nil if not port - @web = WEBrick::HTTPServer.new :BindAddress=>addr, :Port=>port + @web = WEBrick::HTTPServer.new :BindAddress=>addr, :Port=>port, :Logger=>Log maps end def work diff --git a/lib/oxidized/model/ios.rb b/lib/oxidized/model/ios.rb index a316ccf..c3027e2 100644 --- a/lib/oxidized/model/ios.rb +++ b/lib/oxidized/model/ios.rb @@ -2,14 +2,18 @@ class IOS < Oxidized::Model comment '! ' + cmd :all do |cfg| + cfg.each_line.to_a[1..-3].join + end + cmd 'show running-config' do |cfg| - cfg = cfg.each_line.to_a[3..-2].join + cfg = cfg.each_line.to_a[3..-1].join cfg.sub! /^(ntp clock-period).*/, '! \1' cfg end cmd 'show inventory' do |cfg| - comment cfg.each_line.to_a[1..-2].join + comment cfg end cfg :telnet do diff --git a/lib/oxidized/model/junos.rb b/lib/oxidized/model/junos.rb index caa6536..d1f4de6 100644 --- a/lib/oxidized/model/junos.rb +++ b/lib/oxidized/model/junos.rb @@ -6,13 +6,14 @@ class JunOS < Oxidized::Model @input.class.to_s.match /Telnet/ end - cmd 'show configuration' do |cfg| - # example how to handle different output from different methods. Other option would be to - # pass string to helper method, which checks if top/bottom has prompts and removes + cmd :all do |cfg| + # we don't need screen-scraping in ssh due to exec cfg = cfg.lines[1..-2].join if telnet cfg end + cmd 'show configuration' + cmd 'show version' do |cfg| chassis = model $1 if cfg.match /^Model: (\S+)/ comment cfg << chassis.to_s diff --git a/lib/oxidized/model/model.rb b/lib/oxidized/model/model.rb index 6f02659..00a520d 100644 --- a/lib/oxidized/model/model.rb +++ b/lib/oxidized/model/model.rb @@ -2,7 +2,7 @@ module Oxidized class Model class << self def inherited klass - klass.instance_variable_set '@cmd', [] + klass.instance_variable_set '@cmd', Hash.new { |h,k| h[k] = [] } klass.instance_variable_set '@cfg', Hash.new { |h,k| h[k] = [] } Oxidized.mgr.loader = { :class => klass } end @@ -22,7 +22,11 @@ module Oxidized @cfg end def cmd _cmd=nil, &block - @cmd << [_cmd, block] + if _cmd.class == Symbol + @cmd[_cmd] << block + else + @cmd[:cmd] << [_cmd, block] + end end def cmds @cmd @@ -50,9 +54,12 @@ module Oxidized def cmds data = '' - self.class.cmds.each do |cmd, cb| + self.class.cmds[:cmd].each do |cmd, cmd_block| out = @input.cmd cmd - out = instance_exec out, &cb if cb + self.class.cmds[:all].each do |all_block| + out = instance_exec out, &all_block + end + out = instance_exec out, &cmd_block if cmd_block data << out.to_s end data -- cgit v1.2.1