sloe 0.5.4 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/sloe/common.rb CHANGED
@@ -2,12 +2,20 @@ require 'net/scp'
2
2
  require 'snmp'
3
3
 
4
4
  module Sloe
5
+ # Base class. Inherits from {http://rubydoc.info/gems/netconf/Netconf/SSH Netconf::SSH}
5
6
  class Common < Netconf::SSH
6
7
 
8
+ # Provides access to the SNMP object
7
9
  attr_reader :snmp
8
10
 
11
+ # Create Sloe::Common object.
12
+ # Accepts arguments for {http://rubydoc.info/gems/netconf/Netconf/SSH:initialize Netconf::SSH#new} and {http://rubydoc.info/gems/snmp/SNMP/Manager:initialize SNMP::Manager#new}
9
13
  def initialize(args, &block)
10
- @snmp_args = {:host => args[:target], :mib_dir => args[:mib_dir], :mib_modules => args[:mib_modules]}
14
+ @snmp_args = {
15
+ :host => args[:target],
16
+ :mib_dir => args[:mib_dir],
17
+ :mib_modules => args[:mib_modules]
18
+ }
11
19
  @snmp = SNMP::Manager.new(@snmp_args)
12
20
 
13
21
  if block_given?
data/lib/sloe/device.rb CHANGED
@@ -2,8 +2,10 @@ require 'net/netconf'
2
2
  require 'sloe/common'
3
3
 
4
4
  module Sloe
5
+ # Class for working with generic Netconf (i.e. a device that does not support vendor specific extension)
5
6
  class Device < Sloe::Common
6
7
 
8
+ # Sloe::Device object for generic Netconf device
7
9
  def initialize(args, &block)
8
10
 
9
11
  # Stop netconf gem from defaulting to :Junos and thus
data/lib/sloe/junos.rb CHANGED
@@ -1,17 +1,105 @@
1
1
  require 'net/netconf/jnpr'
2
2
  require 'sloe/common'
3
+ require 'fileutils'
3
4
 
4
5
  module Sloe
6
+ # Class for working with Netconf device that supports Junos specifc Netconf extensions
5
7
  class Junos < Sloe::Common
6
8
 
9
+ # Create Sloe::Junos object. It takes the same arguments as Sloe::Common
10
+ #
11
+ # @param args [Hash] key value pairs of arguments for object
12
+ # @param block [Code] code block that will be executed once object created
13
+ # @return [Sloe::Junos] object that interacts with a Junos device
7
14
  def initialize(args, &block)
8
15
  super( args, &block )
9
16
  end
10
17
 
18
+ # execute CLI commands over NETCONF transport returns plain text, rather than XML, by default
19
+ #
20
+ # @param cmd_str [String] A valid Junos CLI command.
21
+ # @param attrs [Hash] Supports same attributes as {http://rubydoc.info/gems/netconf/Netconf/RPC/Junos:command Junos#command}
22
+ # @return nil if command returns no text. Otherwise returns text in format requested (default is plain text)
11
23
  def cli(cmd_str, attrs = { :format => 'text' })
12
24
  attrs[:format] ||= 'text'
13
25
  reply = self.rpc.command(cmd_str, attrs)
14
26
  reply.respond_to?(:text) ? reply.text : reply
15
27
  end
28
+
29
+ # Applies configuration to one or more Junos devices. This method is designed to make it easy to apply a list of configs to a list of Junos devices. Defaults are design for applying new configuration.
30
+ # The :username and password arguments are used when connecting to the device to apply the configuration
31
+ # The :glob argument is used to find a list of files containing config to apply. The default looks in ./configs/ for all files whose basename ends with "-apply".
32
+ # The :match argument is used to find the hostname and configuration format of the config to apply. That file is applied to that host using the format gleaned from the filename suffix.
33
+ #
34
+ # @param args [Hash] Hash of username, password, file glob and regex
35
+ def setup( args = {
36
+ :username => 'netconf',
37
+ :password => 'netconf',
38
+ :glob => "configs/*-apply.*",
39
+ :match => /(?<host>\w+)-apply\.(?<format>\w+)/} )
40
+ _apply_configs( args )
41
+ end
42
+
43
+ # Applies configuration to one or more Junos devices. Defaults are designed for removing configuration.
44
+ # @see #setup
45
+ def clearup( args = {
46
+ :username => 'netconf',
47
+ :password => 'netconf',
48
+ :glob => "configs/*-delete.*",
49
+ :match => /(?<host>\w+)-delete\.(?<format>\w+)/} )
50
+ _apply_configs( args )
51
+ end
52
+
53
+ # Simplifies applying configuration to a Junos device.
54
+ # Uses Junos NETCONF extensions to apply the configuration. It also captures all config error conditions and ensures the config database is returned to the previous committed state.
55
+ #
56
+ # @param config [String] Configuration to be applied the device
57
+ # @param attrs [Hash] Takes same attributes as {http://rubydoc.info/gems/netconf/Netconf/RPC/Junos:load_configuration Junos#load_configuration}
58
+ def apply_configuration( config, attrs = { :format => 'text' } )
59
+ begin
60
+ rpc.lock_configuration
61
+ rpc.load_configuration( config, attrs )
62
+ rpc.commit_configuration
63
+ rpc.unlock_configuration
64
+ rescue Netconf::LockError => e
65
+ rpc.unlock_configuration
66
+ puts e.message
67
+ rescue Netconf::EditError => e
68
+ rpc.discard_changes
69
+ rpc.unlock_configuration
70
+ puts e.message
71
+ rescue Netconf::ValidateError => e
72
+ rpc.discard_changes
73
+ rpc.unlock_configuration
74
+ puts e.message
75
+ rescue Netconf::CommitError => e
76
+ rpc.discard_changes
77
+ rpc.unlock_configuration
78
+ puts e.message
79
+ rescue Netconf::RpcError => e
80
+ rpc.discard_changes
81
+ rpc.unlock_configuration
82
+ puts e.message
83
+ end
84
+ end
85
+
86
+ private
87
+
88
+ def _apply_configs( args )
89
+ raise Errno::ENOENT, "file glob matches no files" if Dir.glob( args[:glob] ).length < 1
90
+ Dir.glob( args[:glob] ) do |file|
91
+ param = args[:match].match( file )
92
+
93
+ @login = {
94
+ :target => param[:host],
95
+ :username => args[:username],
96
+ :password => args[:password]
97
+ }
98
+ @dut = Sloe::Junos.new( @login )
99
+ @config = File.read( file )
100
+ apply_configuration( @config, :format => param[:format] )
101
+ @dut.close
102
+ end
103
+ end
16
104
  end
17
105
  end
data/lib/sloe/version.rb CHANGED
@@ -1,3 +1,4 @@
1
1
  module Sloe
2
- VERSION = "0.5.4"
2
+ # Current version
3
+ VERSION = "0.6.0"
3
4
  end
data/lib/sloe.rb CHANGED
@@ -2,6 +2,7 @@ require 'sloe/version'
2
2
  require 'sloe/device'
3
3
  require 'sloe/junos'
4
4
 
5
+ # Sloe is a module designed to ease interacting with a network device when using NETCONF
5
6
  module Sloe
6
7
 
7
8
  end
data/spec/methods_spec.rb CHANGED
@@ -81,4 +81,28 @@ describe Sloe do
81
81
  lambda { @dut.cli('clear interface statistics fxp0') }.should_not raise_error
82
82
  end
83
83
  end
84
+
85
+ context "Helper methods" do
86
+ before( :all ) do
87
+ Dir.mkdir( 'configs' )
88
+ @apply = File.open( "configs/capella-apply.set", "w" )
89
+ @apply.write( 'set system location building "In a galaxy far, far, away"' )
90
+ @apply.close
91
+
92
+ @delete = File.open( "configs/capella-delete.set", "w" )
93
+ @delete.write( 'delete system location')
94
+ @delete.close
95
+ end
96
+
97
+ it "setup() with default args updates the router config" do
98
+ lambda { @dut.setup() }.should_not raise_error
99
+ end
100
+ it "clearup() with default args updates the router config" do
101
+ lambda { @dut.clearup() }.should_not raise_error
102
+ end
103
+
104
+ after(:all) do
105
+ FileUtils.rm_rf( 'configs' )
106
+ end
107
+ end
84
108
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sloe
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.4
4
+ version: 0.6.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-03-19 00:00:00.000000000 Z
12
+ date: 2013-03-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: snmp