summaryrefslogtreecommitdiff
path: root/lib/oxidized/model
diff options
context:
space:
mode:
authorSaku Ytti <saku@ytti.fi>2013-04-17 17:48:50 +0300
committerSaku Ytti <saku@ytti.fi>2013-04-17 17:48:50 +0300
commit9d217025fac3e335c308f02e7377e14ccfdc0e66 (patch)
treeb90d4d04947fe26a9e592e12d8c4352142380c03 /lib/oxidized/model
Initial commit
Silly for shit-and-giggles attempt at rancid
Diffstat (limited to 'lib/oxidized/model')
-rw-r--r--lib/oxidized/model/ios.rb26
-rw-r--r--lib/oxidized/model/junos.rb47
-rw-r--r--lib/oxidized/model/model.rb70
3 files changed, 143 insertions, 0 deletions
diff --git a/lib/oxidized/model/ios.rb b/lib/oxidized/model/ios.rb
new file mode 100644
index 0000000..a316ccf
--- /dev/null
+++ b/lib/oxidized/model/ios.rb
@@ -0,0 +1,26 @@
+class IOS < Oxidized::Model
+
+ comment '! '
+
+ cmd 'show running-config' do |cfg|
+ cfg = cfg.each_line.to_a[3..-2].join
+ cfg.sub! /^(ntp clock-period).*/, '! \1'
+ cfg
+ end
+
+ cmd 'show inventory' do |cfg|
+ comment cfg.each_line.to_a[1..-2].join
+ end
+
+ cfg :telnet do
+ username /^Username:/
+ password /^Password:/
+ end
+
+ cfg :telnet, :ssh do
+ post_login 'terminal length 0'
+ post_login 'terminal width 0'
+ pre_logout 'exit'
+ end
+
+end
diff --git a/lib/oxidized/model/junos.rb b/lib/oxidized/model/junos.rb
new file mode 100644
index 0000000..caa6536
--- /dev/null
+++ b/lib/oxidized/model/junos.rb
@@ -0,0 +1,47 @@
+class JunOS < Oxidized::Model
+
+ comment '# '
+
+ def telnet
+ @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
+ cfg = cfg.lines[1..-2].join if telnet
+ cfg
+ end
+
+ cmd 'show version' do |cfg|
+ chassis = model $1 if cfg.match /^Model: (\S+)/
+ comment cfg << chassis.to_s
+ end
+
+ def model chassis
+ case chassis
+ when 'mx960'
+ cmd('show chassis fabric reachability') { |cfg| comment cfg }
+ end
+ end
+
+ cmd 'show chassis hardware' do |cfg|
+ comment cfg
+ end
+
+ cfg :telnet do
+ username /^login:/
+ password /^Password:/
+ end
+
+ cfg :ssh do
+ exec true # don't run shell, run each command in exec channel
+ end
+
+ cfg :telnet, :ssh do
+ post_login 'set cli screen-length 0'
+ post_login 'set cli screen-width 0'
+ pre_logout 'exit'
+ end
+
+end
diff --git a/lib/oxidized/model/model.rb b/lib/oxidized/model/model.rb
new file mode 100644
index 0000000..6f02659
--- /dev/null
+++ b/lib/oxidized/model/model.rb
@@ -0,0 +1,70 @@
+module Oxidized
+ class Model
+ class << self
+ def inherited klass
+ klass.instance_variable_set '@cmd', []
+ klass.instance_variable_set '@cfg', Hash.new { |h,k| h[k] = [] }
+ Oxidized.mgr.loader = { :class => klass }
+ end
+ def comment _comment='# '
+ return @comment if @comment
+ @comment = block_given? ? yield : _comment
+ end
+ def cfg *methods, &block
+ [methods].flatten.each do |method|
+ @cfg[method.to_s] << block
+ end
+ end
+ def prompt _prompt=nil
+ @prompt or @prompt = _prompt
+ end
+ def cfgs
+ @cfg
+ end
+ def cmd _cmd=nil, &block
+ @cmd << [_cmd, block]
+ end
+ def cmds
+ @cmd
+ end
+ def post_login &block
+ @post_login or @post_login = block
+ end
+ end
+
+ attr_accessor :input
+
+ def cmd string
+ out = @input.cmd string
+ out = yield out if block_given?
+ out
+ end
+
+ def cfg
+ self.class.cfgs
+ end
+
+ def prompt
+ self.class.prompt
+ end
+
+ def cmds
+ data = ''
+ self.class.cmds.each do |cmd, cb|
+ out = @input.cmd cmd
+ out = instance_exec out, &cb if cb
+ data << out.to_s
+ end
+ data
+ end
+
+ def comment _comment
+ data = ''
+ _comment.each_line do |line|
+ data << self.class.comment << line
+ end
+ data
+ end
+
+ end
+end