Class: Junos::Ez::Config::Provider
- Inherits:
-
Provider::Parent
- Object
- Provider::Parent
- Junos::Ez::Config::Provider
- Defined in:
- lib/junos-ez/utils/config.rb
Overview
PUBLIC METHODS
Instance Attribute Summary
Attributes inherited from Provider::Parent
#catalog, #has, #list, #name, #ndev, #parent, #properties, #providers, #should
Instance Method Summary collapse
-
#commit!(opts = {}) ⇒ Object
————————————————————— commit! - commits the configuration to the device.
-
#commit? ⇒ Boolean
————————————————————— commit? - perform commit configuration check.
-
#diff?(rollback_id = 0) ⇒ Boolean
————————————————————— diff? - displays diff (patch format) between current candidate configuration loaded and the rollback_id.
-
#load!(opts = {}) ⇒ Object
————————————————————— load! - used to load configuration files / templates.
-
#lock! ⇒ Object
————————————————————— lock! - takes an exclusive lock on the candidate config.
-
#rollback!(rollback_id = 0) ⇒ Object
————————————————————— rollback! - used to rollback the configuration —————————————————————.
-
#unlock! ⇒ Object
————————————————————— unlock! - releases exclusive lock on candidate config.
Methods inherited from Provider::Parent
#[], #[]=, #activate!, #active?, #catalog!, #create, #create!, #create_from_hash!, #create_from_yaml!, #deactivate!, #delete!, #each, #exists?, #init_has, #initialize, #is_new?, #is_provider?, #list!, #name_decorated, #need_write?, #read!, #rename!, #reorder!, #select, #to_h, #to_h_expanded, #to_yaml, #write!, #xml_at_edit, #xml_at_top, #xml_build_change, #xml_change__active, #xml_change__exist, #xml_change_admin, #xml_change_description, #xml_config_read!, #xml_element_newname, #xml_get_has_xml, #xml_on_create, #xml_on_delete
Constructor Details
This class inherits a constructor from Junos::Ez::Provider::Parent
Instance Method Details
#commit!(opts = {}) ⇒ Object
commit! - commits the configuration to the device
— options —
:confirm => true | timeout :comment => commit log comment
— returns —
true if commit completed
raises Netconf::CommitError otherwise
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/junos-ez/utils/config.rb', line 116 def commit!( opts = {} ) args = {} args[:log] = opts[:comment] if opts[:comment] if opts[:confirm] args[:confirmed] = true if opts[:confirm] != true timeout = Integer( opts[:confirm] ) rescue false raise ArgumentError "invalid timeout #{opts[:confirm]}" unless timeout args[:confirm_timeout] = timeout end end @ndev.rpc.commit_configuration( args ) true end |
#commit? ⇒ Boolean
commit? - perform commit configuration check
— returns —
true if candidate config is OK to commit
Array of rpc-error data otherwise
141 142 143 144 145 146 147 148 |
# File 'lib/junos-ez/utils/config.rb', line 141 def commit? begin @ndev.rpc.commit_configuration( :check => true ) rescue => e return Junos::Ez::rpc_errors( e.rsp ) end true # commit check OK! end |
#diff?(rollback_id = 0) ⇒ Boolean
diff? - displays diff (patch format) between current candidate configuration loaded and the rollback_id
— returns —
false if no diff
String of diff output otherwise
169 170 171 172 173 174 175 |
# File 'lib/junos-ez/utils/config.rb', line 169 def diff?( rollback_id = 0 ) raise ArgumentError, "invalid rollback #{rollback_id}" unless ( rollback_id >= 0 and rollback_id <= 50 ) got = ndev.rpc.get_configuration( :compare=>'rollback', :rollback=> rollback_id.to_s ) diff = got.xpath('configuration-output').text return false if diff == "\n" diff end |
#load!(opts = {}) ⇒ Object
load! - used to load configuration files / templates. This
does not perform a 'commit', just the equivalent of the
load-configuration RPC
— options —
:filename => path - indcates the filename of content
note: filename extension will also define format
.{conf,text,txt} <==> :text
.xml <==> :xml
.set <==> :set
:content => String - string content of data (vs. :filename)
:format => [:text, :set, :xml], default :text (curly-brace)
this will override any auto-format from the :filename
:binding - indicates file/content is an ERB
=> <object> - will grab the binding from this object
using a bit of meta-programming magic
=> <binding> - will use this binding
:replace! => true - enables the ‘replace’ option :overwrite! => true - enables the ‘overwrite’ optoin
— returns —
true if the configuration is loaded OK
raise Netconf::EditError otherwise
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/junos-ez/utils/config.rb', line 62 def load!( opts = {} ) raise ArgumentError unless opts[:content] || opts[:filename] content = opts[:content] || File.read( opts[:filename] ) attrs = {} attrs[:action] = 'replace' if opts[:replace!] attrs[:action] = 'override' if opts[:override!] if opts[:format] attrs[:format] = opts[:format].to_s elsif opts[:filename] case f_ext = File.extname( opts[:filename] ) when '.conf','.text','.txt'; attrs[:format] = 'text' when '.set'; attrs[:format] = 'set' when '.xml'; # default is XML else raise ArgumentError, "unknown format from extension: #{f_ext}" end else raise ArgumentError "unspecified format" end if opts[:binding] erb = ERB.new( content, nil, '>' ) case opts[:binding] when Binding # binding was provided to use content = erb.result( opts[:binding] ) when Object obj = opts[:binding] def obj.junos_ez_binding; binding end content = erb.result( obj.junos_ez_binding ) class << obj; remove_method :junos_ez_binding end end end @ndev.rpc.load_configuration( content, attrs ) true # everthing OK! end |
#lock! ⇒ Object
lock! - takes an exclusive lock on the candidate config
— returns —
true if lock acquired
raise Netconf::LockError otherwise
185 186 187 188 |
# File 'lib/junos-ez/utils/config.rb', line 185 def lock! @ndev.rpc.lock_configuration true end |
#rollback!(rollback_id = 0) ⇒ Object
rollback! - used to rollback the configuration
154 155 156 157 158 |
# File 'lib/junos-ez/utils/config.rb', line 154 def rollback!( rollback_id = 0 ) raise ArgumentError, "invalid rollback #{rollback_id}" unless ( rollback_id >= 0 and rollback_id <= 50 ) @ndev.rpc.load_configuration( :compare=>'rollback', :rollback=> rollback_id.to_s ) true # rollback OK! end |
#unlock! ⇒ Object
unlock! - releases exclusive lock on candidate config
— returns —
true if lock release
raise Netconf::RpcError otherwise
198 199 200 201 |
# File 'lib/junos-ez/utils/config.rb', line 198 def unlock! @ndev.rpc.unlock_configuration true end |