summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSaku Ytti <saku@ytti.fi>2014-03-14 21:47:46 +0200
committerSaku Ytti <saku@ytti.fi>2014-03-14 21:47:46 +0200
commit71cf3505fc548b1bc8bdd6092cd8dd5a1e24cc91 (patch)
tree8bcc7d491b3bf405f6fede0b5cd3dd2a3f8dc81f
parent7e0cf45a1481345e309e14eb1ae9c6b0a59457f8 (diff)
Introduce 'post' and 'pre' commands
Both post and pre are called after all 'cmd' are already called, but output from 'pre' is put on top of configuration output and output from 'post' is put on bottom of confguration output. Rationale is dynamic configuration, where you'll only know after running some commands what commands you want to run. Both except blocks, such as pre do # commands to execute end Both can be called multiple times
-rw-r--r--lib/oxidized/model/junos.rb2
-rw-r--r--lib/oxidized/model/model.rb46
2 files changed, 42 insertions, 6 deletions
diff --git a/lib/oxidized/model/junos.rb b/lib/oxidized/model/junos.rb
index a7080e1..4bca998 100644
--- a/lib/oxidized/model/junos.rb
+++ b/lib/oxidized/model/junos.rb
@@ -19,7 +19,7 @@ class JunOS < Oxidized::Model
comment cfg
end
- def main
+ post do
case @model
when 'mx960'
cmd('show chassis fabric reachability') { |cfg| comment cfg }
diff --git a/lib/oxidized/model/model.rb b/lib/oxidized/model/model.rb
index f95443d..70b307f 100644
--- a/lib/oxidized/model/model.rb
+++ b/lib/oxidized/model/model.rb
@@ -2,8 +2,9 @@ module Oxidized
class Model
class << self
def inherited klass
- klass.instance_variable_set '@cmd', Hash.new { |h,k| h[k] = [] }
- klass.instance_variable_set '@cfg', Hash.new { |h,k| h[k] = [] }
+ klass.instance_variable_set '@cmd', Hash.new { |h,k| h[k] = [] }
+ klass.instance_variable_set '@cfg', Hash.new { |h,k| h[k] = [] }
+ klass.instance_variable_set '@procs', Hash.new { |h,k| h[k] = [] }
klass.instance_variable_set '@expect', []
klass.const_set :CFG, CFG
Oxidized.mgr.loader = { :class => klass }
@@ -39,6 +40,35 @@ module Oxidized
def expects
@expect
end
+
+ # calls the block at the end of the model, prepending the output of the
+ # block to the output string
+ #
+ # @author Saku Ytti <saku@ytti.fi>
+ # @since 0.0.39
+ # @yield expects block which should return [String]
+ # @return [void]
+ def pre &block
+ @procs[:pre] << block
+ end
+
+ # calls the block at the end of the model, adding the output of the block
+ # to the output string
+ #
+ # @author Saku Ytti <saku@ytti.fi>
+ # @since 0.0.39
+ # @yield expects block which should return [String]
+ # @return [void]
+ def post &block
+ @procs[:post] << block
+ end
+
+ # @author Saku Ytti <saku@ytti.fi>
+ # @since 0.0.39
+ # @return [Hash] hash proc procs :pre+:post to be prepended/postfixed to output
+ def procs
+ @procs
+ end
end
attr_accessor :input
@@ -87,14 +117,20 @@ module Oxidized
end
def get
- data = ''
+ data, pre = '', ''
+ procs = self.class.procs
self.class.cmds[:cmd].each do |command, block|
out = cmd command, &block
return false unless out
data << out.to_s
end
- data << main.to_s if respond_to? :main
- data
+ procs[:pre].each do |pre_proc|
+ pre << instance_eval(&pre_proc)
+ end
+ procs[:post].each do |post_proc|
+ data << instance_eval(&post_proc)
+ end
+ pre + data
end
def comment _comment