shopify-junos-ez-stdlib 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (71) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +91 -0
  3. data/LICENSE +26 -0
  4. data/README.md +199 -0
  5. data/docs/Facts.md +192 -0
  6. data/docs/Providers/Group.md +61 -0
  7. data/docs/Providers/IPports.md +61 -0
  8. data/docs/Providers/L1ports.md +29 -0
  9. data/docs/Providers/L2ports.md +43 -0
  10. data/docs/Providers/LAGports.md +57 -0
  11. data/docs/Providers/StaticHosts.md +26 -0
  12. data/docs/Providers/StaticRoutes.md +37 -0
  13. data/docs/Providers/UserAuths.md +32 -0
  14. data/docs/Providers/Users.md +122 -0
  15. data/docs/Providers/Vlans.md +43 -0
  16. data/docs/Providers_Resources.md +353 -0
  17. data/docs/README_FIRST.md +27 -0
  18. data/docs/Utils/Config.md +160 -0
  19. data/docs/Utils/Filesystem.md +360 -0
  20. data/docs/Utils/Routing-Engine.md +379 -0
  21. data/docs/Utils/SCP.md +24 -0
  22. data/examples/config/config_file.rb +72 -0
  23. data/examples/config/config_template_object.rb +81 -0
  24. data/examples/config/config_template_simple.rb +76 -0
  25. data/examples/config/multi_config.rb +60 -0
  26. data/examples/fs_utils.rb +31 -0
  27. data/examples/lag_port.rb +27 -0
  28. data/examples/re_upgrade.rb +99 -0
  29. data/examples/re_utils.rb +33 -0
  30. data/examples/simple.rb +46 -0
  31. data/examples/st_hosts.rb +33 -0
  32. data/examples/user.rb +32 -0
  33. data/examples/vlans.rb +31 -0
  34. data/junos-ez-stdlib.gemspec +15 -0
  35. data/lib/junos-ez/exceptions.rb +3 -0
  36. data/lib/junos-ez/facts.rb +83 -0
  37. data/lib/junos-ez/facts/chassis.rb +51 -0
  38. data/lib/junos-ez/facts/ifd_style.rb +17 -0
  39. data/lib/junos-ez/facts/personality.rb +25 -0
  40. data/lib/junos-ez/facts/switch_style.rb +31 -0
  41. data/lib/junos-ez/facts/version.rb +58 -0
  42. data/lib/junos-ez/group.rb +206 -0
  43. data/lib/junos-ez/ip_ports.rb +30 -0
  44. data/lib/junos-ez/ip_ports/classic.rb +188 -0
  45. data/lib/junos-ez/l1_ports.rb +121 -0
  46. data/lib/junos-ez/l1_ports/classic.rb +87 -0
  47. data/lib/junos-ez/l1_ports/switch.rb +134 -0
  48. data/lib/junos-ez/l2_ports.rb +66 -0
  49. data/lib/junos-ez/l2_ports/bridge_domain.rb +499 -0
  50. data/lib/junos-ez/l2_ports/vlan.rb +433 -0
  51. data/lib/junos-ez/l2_ports/vlan_l2ng.rb +502 -0
  52. data/lib/junos-ez/lag_ports.rb +268 -0
  53. data/lib/junos-ez/provider.rb +619 -0
  54. data/lib/junos-ez/stdlib.rb +18 -0
  55. data/lib/junos-ez/system.rb +48 -0
  56. data/lib/junos-ez/system/st_hosts.rb +92 -0
  57. data/lib/junos-ez/system/st_routes.rb +159 -0
  58. data/lib/junos-ez/system/syscfg.rb +103 -0
  59. data/lib/junos-ez/system/userauths.rb +84 -0
  60. data/lib/junos-ez/system/users.rb +217 -0
  61. data/lib/junos-ez/utils/config.rb +236 -0
  62. data/lib/junos-ez/utils/fs.rb +385 -0
  63. data/lib/junos-ez/utils/re.rb +558 -0
  64. data/lib/junos-ez/version.rb +6 -0
  65. data/lib/junos-ez/vlans.rb +38 -0
  66. data/lib/junos-ez/vlans/bridge_domain.rb +89 -0
  67. data/lib/junos-ez/vlans/vlan.rb +119 -0
  68. data/lib/junos-ez/vlans/vlan_l2ng.rb +126 -0
  69. data/shipit.yml +4 -0
  70. data/tmp +7 -0
  71. metadata +129 -0
data/docs/Utils/SCP.md ADDED
@@ -0,0 +1,24 @@
1
+ # `Net::SCP`
2
+
3
+ The NETCONF object already provides a mechanism to use secure-copy (SCP), so technically this is _not_ part of the `Junos::EZ` framework. That being said, this is some quick documentation and URLs so you can copy files to and from Junos devices.
4
+
5
+ # DOCS
6
+
7
+ For documentation on `Net::SCP`, please refer to this website: http://net-ssh.github.io/scp/v1/api/index.html
8
+
9
+ # USAGE
10
+
11
+ The NETCONF object includes an instance variable `scp` that is class `Net::SCP`.
12
+
13
+ To copy a file from the server to the Junos target, you use the `upload` or `upload!` method. The bang(!) version is blocking, meaning the code execution will resume only after the file has been completely transfered.
14
+
15
+ ```ruby
16
+ file_on_server = "/cygwin/junos/junos-ex4200-image.tgz"
17
+ location_on_junos = "/var/tmp"
18
+
19
+ ndev.scp.upload!( from_on_server, location_on_junos )
20
+ ```
21
+
22
+ To copy a file from the Junos target to the server, you use the `download` or `download!` method.
23
+
24
+ Both upload and download methods can take a Ruby block which is used to provide progress updates on the transfer. There is a complete "software upgrade" example that illustrates this technique in the _examples_ directory.
@@ -0,0 +1,72 @@
1
+ =begin
2
+
3
+ This exmaple illustrates the use of the Junos::Ez::Config::Utils library. The code will load the contents
4
+ of a configuration file and save the diff output to the file "diffs.txt". Error checking/exception
5
+ handling is also demonstrated. If there are any errors, the "pretty-print" (pp) function will dump the
6
+ contents of the result structure to stderr so you can see what it looks like.
7
+
8
+ =end
9
+
10
+ require 'net/netconf/jnpr'
11
+ require 'junos-ez/stdlib'
12
+
13
+ # login information for NETCONF session
14
+
15
+ login = { :target => 'vsrx', :username => 'jeremy', :password => 'jeremy1', }
16
+
17
+ ## create a NETCONF object to manage the device and open the connection ...
18
+
19
+ ndev = Netconf::SSH.new( login )
20
+ $stdout.print "Connecting to device #{login[:target]} ... "
21
+ ndev.open
22
+ $stdout.puts "OK!"
23
+
24
+ # attach the junos-ez objects to the ndev object ...
25
+
26
+ Junos::Ez::Provider( ndev )
27
+ Junos::Ez::Config::Utils( ndev, :cfg )
28
+
29
+ # begin a block to trap any raised expections ...
30
+ begin
31
+
32
+ # lock the candidate config
33
+ ndev.cfg.lock!
34
+
35
+ # load the contents of the 'load_sample.conf' file
36
+ # into the device.
37
+
38
+ $stdout.puts "Loading changes ..."
39
+ ndev.cfg.load! :filename => 'load_sample.conf'
40
+
41
+ # check to see if commit-check passes. if it doesn't
42
+ # it will return a structure of errors
43
+
44
+ unless (errs = ndev.cfg.commit?) == true
45
+ $stderr.puts "Commit check failed"
46
+ pp errs
47
+ ndev.close # will auto-rollback changes
48
+ exit 1
49
+ end
50
+
51
+ # save the cnfig diff to a file ...
52
+ File.open( "diffs.txt", "w") {|f| f.write ndev.cfg.diff? }
53
+
54
+ # commit the changes and unlock the config
55
+
56
+ $stdout.puts "Commiting changes ..."
57
+ ndev.cfg.commit!
58
+ ndev.cfg.unlock!
59
+
60
+ $stdout.puts "Done!"
61
+
62
+ rescue Netconf::LockError
63
+ $stderr.puts "Unable to lock config"
64
+ rescue Netconf::EditError => e
65
+ $stderr.puts "Unable to load configuration"
66
+ pp Junos::Ez::rpc_errors( e.rsp )
67
+ rescue Netconf::CommitError => e
68
+ $stderr.puts "Unable to commit configuration"
69
+ pp Junos::Ez::rpc_errors( e.rpc )
70
+ end
71
+
72
+ ndev.close
@@ -0,0 +1,81 @@
1
+ =begin
2
+
3
+ This exmaple illustrates the use of the Junos::Ez::Config::Utils library. The code will load the contents
4
+ of a template (ERB) file and use an object to contain the variables used in scope (binding).
5
+
6
+ Error checking/exception handling is also demonstrated. If there are any errors, the "pretty-print"
7
+ (pp) function will dump the contents of the result structure to stderr so you can see what it looks like.
8
+
9
+ =end
10
+
11
+ require 'net/netconf/jnpr'
12
+ require 'junos-ez/stdlib'
13
+
14
+ # login information for NETCONF session
15
+
16
+ login = { :target => 'vsrx', :username => 'jeremy', :password => 'jeremy1', }
17
+
18
+ ## create a NETCONF object to manage the device and open the connection ...
19
+
20
+ ndev = Netconf::SSH.new( login )
21
+ $stdout.print "Connecting to device #{login[:target]} ... "
22
+ ndev.open
23
+ $stdout.puts "OK!"
24
+
25
+ # attach the junos-ez objects to the ndev object ...
26
+
27
+ Junos::Ez::Provider( ndev )
28
+ Junos::Ez::Config::Utils( ndev, :cfg )
29
+
30
+ # begin a block to trap any raised expections ...
31
+ begin
32
+
33
+ # lock the candidate config
34
+ ndev.cfg.lock!
35
+
36
+ # load the template (ERB) file and use an object to contains the variables
37
+ # in scope durning the ERB result evaluation, so declare that now just for demo purposes ...
38
+
39
+ class MyClass
40
+ def initialize
41
+ @interfaces = ['ge-0/0/1','ge-0/0/2','ge-0/0/3']
42
+ end
43
+ end
44
+
45
+ myobj = MyClass.new
46
+
47
+ $stdout.puts "Loading changes ..."
48
+ ndev.cfg.load! :filename => 'load_template_object.conf', :binding => myobj
49
+
50
+ # check to see if commit-check passes. if it doesn't
51
+ # it will return a structure of errors
52
+
53
+ unless (errs = ndev.cfg.commit?) == true
54
+ $stderr.puts "Commit check failed"
55
+ pp errs
56
+ ndev.close # will auto-rollback changes
57
+ exit 1
58
+ end
59
+
60
+ # save the cnfig diff to a file ...
61
+ File.open( "diffs.txt", "w") {|f| f.write ndev.cfg.diff? }
62
+
63
+ # commit the changes and unlock the config
64
+
65
+ $stdout.puts "Commiting changes ..."
66
+ ndev.cfg.commit!
67
+ ndev.cfg.unlock!
68
+
69
+ $stdout.puts "Done!"
70
+
71
+ rescue Netconf::LockError
72
+ $stderr.puts "Unable to lock config"
73
+ rescue Netconf::EditError => e
74
+ $stderr.puts "Unable to load configuration"
75
+ pp Junos::Ez::rpc_errors( e.rsp )
76
+ rescue Netconf::CommitError => e
77
+ $stderr.puts "Unable to commit configuration"
78
+ pp Junos::Ez::rpc_errors( e.rpc )
79
+ end
80
+
81
+ ndev.close
@@ -0,0 +1,76 @@
1
+ =begin
2
+
3
+ This exmaple illustrates the use of the Junos::Ez::Config::Utils library. The code will load the contents
4
+ of a template (ERB) file and use the current variables in the main scope (binding) to fill in the details.
5
+
6
+ Error checking/exception handling is also demonstrated. If there are any errors, the "pretty-print"
7
+ (pp) function will dump the contents of the result structure to stderr so you can see what it looks like.
8
+
9
+ =end
10
+
11
+ require 'net/netconf/jnpr'
12
+ require 'junos-ez/stdlib'
13
+
14
+ # login information for NETCONF session
15
+
16
+ login = { :target => 'vsrx', :username => 'jeremy', :password => 'jeremy1', }
17
+
18
+ ## create a NETCONF object to manage the device and open the connection ...
19
+
20
+ ndev = Netconf::SSH.new( login )
21
+ $stdout.print "Connecting to device #{login[:target]} ... "
22
+ ndev.open
23
+ $stdout.puts "OK!"
24
+
25
+ # attach the junos-ez objects to the ndev object ...
26
+
27
+ Junos::Ez::Provider( ndev )
28
+ Junos::Ez::Config::Utils( ndev, :cfg )
29
+
30
+ # begin a block to trap any raised expections ...
31
+ begin
32
+
33
+ # lock the candidate config
34
+ ndev.cfg.lock!
35
+
36
+ # load the template (ERB) file and use the current variables in scope (binding)
37
+ # when evaluating the results of the template. The template references a
38
+ # veriable called 'interfaces', so declare that now ...
39
+
40
+ interfaces = ['ge-0/0/1','ge-0/0/2','ge-0/0/3']
41
+
42
+ $stdout.puts "Loading changes ..."
43
+ ndev.cfg.load! :filename => 'load_template_main.conf', :binding => binding
44
+
45
+ # check to see if commit-check passes. if it doesn't
46
+ # it will return a structure of errors
47
+
48
+ unless (errs = ndev.cfg.commit?) == true
49
+ $stderr.puts "Commit check failed"
50
+ pp errs
51
+ ndev.close # will auto-rollback changes
52
+ exit 1
53
+ end
54
+
55
+ # save the cnfig diff to a file ...
56
+ File.open( "diffs.txt", "w") {|f| f.write ndev.cfg.diff? }
57
+
58
+ # commit the changes and unlock the config
59
+
60
+ $stdout.puts "Commiting changes ..."
61
+ ndev.cfg.commit!
62
+ ndev.cfg.unlock!
63
+
64
+ $stdout.puts "Done!"
65
+
66
+ rescue Netconf::LockError
67
+ $stderr.puts "Unable to lock config"
68
+ rescue Netconf::EditError => e
69
+ $stderr.puts "Unable to load configuration"
70
+ pp Junos::Ez::rpc_errors( e.rsp )
71
+ rescue Netconf::CommitError => e
72
+ $stderr.puts "Unable to commit configuration"
73
+ pp Junos::Ez::rpc_errors( e.rpc )
74
+ end
75
+
76
+ ndev.close
@@ -0,0 +1,60 @@
1
+ require 'pry'
2
+ require 'net/netconf/jnpr'
3
+ require 'junos-ez/stdlib'
4
+
5
+ # list of targets to make the change. Hardcoding these as array
6
+ # but you could load them from a file, etc.
7
+
8
+ targets = [ 'vsrx', 'ex-10', 'ex-20', 'ex-33' ]
9
+
10
+ # let's assume that all targets use the same login/password ...
11
+
12
+ login = { :username => 'jeremy', :password => 'jeremy1', }
13
+
14
+
15
+ # -------------------------------------------------------------------
16
+ # define a function to do the configuration actions
17
+ # -------------------------------------------------------------------
18
+
19
+ def load_stuff( login )
20
+
21
+ ## create a NETCONF object to manage the device and open the connection ...
22
+ ndev = Netconf::SSH.new( login )
23
+ $stdout.print "Connecting to device #{login[:target]} ... "
24
+ ndev.open
25
+ $stdout.puts "OK!"
26
+
27
+ Junos::Ez::Provider( ndev )
28
+ Junos::Ez::Config::Utils( ndev, :cfg )
29
+
30
+ # lock the candidate config
31
+ # ndev.cfg.lock!
32
+
33
+ # examples of loading ...
34
+ # ndev.cfg.load! :filename => 'load_sample.conf'
35
+ # ndev.cfg.load! :content => File.read( 'load_sample.conf' ), :format => :text
36
+ # ndev.cfg.load! :filename => 'load_sample.set'
37
+
38
+ binding.pry
39
+
40
+ # check to see if the config is OK to commit
41
+ # ndev.cfg.commit?
42
+
43
+ # perform the commit
44
+ # ndev.cfg.commit!
45
+
46
+ # unlock the config
47
+ # ndev.cfg.unlock!
48
+
49
+ ndev.close
50
+ end
51
+
52
+ ### -----------------------------------------------------------------
53
+ ### run through each of the target names and load configs ..
54
+ ### -----------------------------------------------------------------
55
+
56
+ targets.each do |target|
57
+ login[:target] = target
58
+ load_stuff( login )
59
+ end
60
+
@@ -0,0 +1,31 @@
1
+ require 'pry'
2
+ require 'pp'
3
+ require 'yaml'
4
+ require 'net/netconf/jnpr'
5
+ require 'junos-ez/stdlib'
6
+
7
+ # login information for NETCONF session
8
+
9
+ login = { :target => 'vsrx', :username => 'jeremy', :password => 'jeremy1', }
10
+
11
+ ## create a NETCONF object to manage the device and open the connection ...
12
+
13
+ ndev = Netconf::SSH.new( login )
14
+ $stdout.print "Connecting to device #{login[:target]} ... "
15
+ ndev.open
16
+ $stdout.puts "OK!"
17
+
18
+ ## Now bind providers to the device object.
19
+ ## the 'Junos::Ez::Provider' must be first before all others
20
+ ## this provider will setup the device 'facts'. The other providers
21
+ ## allow you to define the instance variables; so this example
22
+ ## is using 'l1_ports' and 'ip_ports', but you could name them
23
+ ## what you like, yo!
24
+
25
+ Junos::Ez::Provider( ndev )
26
+ Junos::Ez::Fs::Utils( ndev, :fs )
27
+
28
+
29
+ binding.pry
30
+
31
+ ndev.close
@@ -0,0 +1,27 @@
1
+ require 'net/netconf/jnpr'
2
+ require 'junos-ez/stdlib'
3
+
4
+ unless ARGV[0]
5
+ puts "You must specify a target"
6
+ exit 1
7
+ end
8
+
9
+ # login information for NETCONF session
10
+ login = { :target => ARGV[0], :username => 'jeremy', :password => 'jeremy1', }
11
+
12
+ ## create a NETCONF object to manage the device and open the connection ...
13
+
14
+ ndev = Netconf::SSH.new( login )
15
+ $stdout.print "Connecting to device #{login[:target]} ... "
16
+ ndev.open
17
+ $stdout.puts "OK!"
18
+
19
+ Junos::Ez::Provider( ndev )
20
+ Junos::Ez::Config::Utils( ndev, :cu )
21
+ Junos::Ez::LAGports::Provider( ndev, :lags )
22
+ Junos::Ez::Vlans::Provider( ndev, :vlans )
23
+ Junos::Ez::L2ports::Provider( ndev, :l2_ports )
24
+
25
+ binding.pry
26
+
27
+ ndev.close
@@ -0,0 +1,99 @@
1
+ require 'pry'
2
+ require 'pp'
3
+ require 'net/scp'
4
+ require 'net/netconf/jnpr'
5
+ require 'junos-ez/stdlib'
6
+
7
+ unless ARGV[0]
8
+ puts "You must specify a target"
9
+ exit 1
10
+ end
11
+
12
+ # login information for NETCONF session
13
+
14
+ login = { :target => ARGV[0], :username => 'jeremy', :password => 'jeremy1', }
15
+
16
+ ## create a NETCONF object to manage the device and open the connection ...
17
+
18
+ ndev = Netconf::SSH.new( login )
19
+ print "Connecting to device #{login[:target]} ... "
20
+ ndev.open
21
+ puts "OK!"
22
+
23
+ ## attach our private & utils that we need ...
24
+
25
+ Junos::Ez::Provider( ndev )
26
+ Junos::Ez::RE::Utils( ndev, :re ) # routing-engine utils
27
+ Junos::Ez::FS::Utils( ndev, :fs ) # filesystem utils
28
+
29
+ ## upload the software image to the target device
30
+ ## http://net-ssh.github.io/net-scp/
31
+
32
+ file_name = 'junos-vsrx-domestic.tgz'
33
+ file_on_server = '/home/jschulman/junos-images/vsrx/' + file_name
34
+ file_on_junos = '/var/tmp/' + file_name
35
+
36
+ ## simple function to use the Netconf::SSH SCP functionality to
37
+ ## upload a file to the device and watch the percetage tick by ...
38
+
39
+ def copy_file_to_junos( ndev, file_on_server, file_on_junos )
40
+ mgr_i = cur_i = 0
41
+ backitup = 8.chr * 4
42
+ ndev.scp.upload!( file_on_server, file_on_junos ) do |ch, name, sent, total|
43
+ pct = (sent.to_f / total.to_f) * 100
44
+ mgr_i = pct.to_i
45
+ if mgr_i != cur_i
46
+ cur_i = mgr_i
47
+ printf "%4s", cur_i.to_s + "%"
48
+ print backitup
49
+ end
50
+ end
51
+ end
52
+
53
+ print "Copying file to Junos ... "
54
+ copy_file_to_junos( ndev, file_on_server, file_on_junos )
55
+ puts "OK! "
56
+
57
+ ###
58
+ ### check the MD5 checksum values
59
+ ###
60
+
61
+ print "Validating MD5 checksum ... "
62
+ md5_on_s = Digest::MD5.file( file_on_server ).to_s
63
+ md5_on_j = ndev.fs.checksum( :md5, file_on_junos )
64
+
65
+ if md5_on_s != md5_on_j
66
+ puts "The MD5 checksum values do not match!"
67
+ ndev.close
68
+ exit 1
69
+ end
70
+
71
+ puts "OK!"
72
+
73
+ print "Validating image ... please wait ... "
74
+
75
+ unless ndev.re.software_validate?( file_on_junos )
76
+ puts "The softare does not validate!"
77
+ ndev.close
78
+ exit 1
79
+ end
80
+
81
+ puts "OK!"
82
+
83
+ print "Installing image ... please wait ... "
84
+ rc = ndev.re.software_install!( :package => file_on_junos, :no_validate => true )
85
+ if rc != true
86
+ puts rc
87
+ end
88
+
89
+ puts "OK!"
90
+
91
+ ### use pry if you want to 'look around'
92
+ ## -> binding.pry
93
+
94
+ if ARGV[1] == 'reboot'
95
+ puts "Rebooting!"
96
+ ndev.re.reboot!
97
+ end
98
+
99
+ ndev.close