Class: Junos::Ez::Config::Provider

Inherits:
Provider::Parent show all
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

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, #with, #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



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/junos-ez/utils/config.rb', line 117

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

Returns:

  • (Boolean)


142
143
144
145
146
147
148
149
# File 'lib/junos-ez/utils/config.rb', line 142

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 —

nil if no diff
String of diff output otherwise

Returns:

  • (Boolean)

Raises:

  • (ArgumentError)


170
171
172
173
174
175
176
# File 'lib/junos-ez/utils/config.rb', line 170

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 nil if diff == "\n"
  diff
end

#get_config(rqst = nil) ⇒ Object


get_config - returns String of requested (or entire) config in “text” (curly-brace) format. The ‘rqst’ argument identifies the scope of the config, for example:

.get_config( “interfaces ge-0/0/0” )

If there is no configuration available, ‘nil’ is returned

If there is an error in the request, that will be returned

as a String with "ERROR!" prepended



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/junos-ez/utils/config.rb', line 217

def get_config( rqst = nil )
  scope = "show configuration"
  scope.concat( " " + rqst ) if rqst
  begin
    @ndev.rpc.command( scope, :format => 'text' ).xpath('configuration-output').text
  rescue NoMethodError
    # indicates no configuration found
    nil
  rescue => e
    # indicates error in request 
    err = e.rsp.xpath('rpc-error')[0]
    err_info = err.xpath('error-info/bad-element').text
    err_msg = err.xpath('error-message').text
    "ERROR! " + err_msg + ": " + err_info
  end
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

Raises:

  • (ArgumentError)


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
102
# File 'lib/junos-ez/utils/config.rb', line 63

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



186
187
188
189
# File 'lib/junos-ez/utils/config.rb', line 186

def lock!
  @ndev.rpc.lock_configuration
  true
end

#rollback!(rollback_id = 0) ⇒ Object


rollback! - used to rollback the configuration


Raises:

  • (ArgumentError)


155
156
157
158
159
# File 'lib/junos-ez/utils/config.rb', line 155

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



199
200
201
202
# File 'lib/junos-ez/utils/config.rb', line 199

def unlock!
  @ndev.rpc.unlock_configuration
  true
end