class Junos::Ez::Config::Provider


PUBLIC METHODS


Public Instance Methods

commit!( opts = {} ) click to toggle source

commit! - commits the configuration to the device

— options —

:confirm => true | timeout :comment => commit log comment

— returns —

true if commit completed
raises Netconf::CommitError otherwise

# 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?() click to toggle source

commit? - perform commit configuration check

— returns —

true if candidate config is OK to commit
Array of rpc-error data otherwise

# 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 ) click to toggle source

diff? - displays diff (patch format) between current candidate configuration loaded and the rollback_id

— returns —

nil if no diff
String of diff output otherwise

# 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 ) click to toggle source

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

# 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 = {} ) click to toggle source

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

# 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!() click to toggle source

lock! - takes an exclusive lock on the candidate config

— returns —

true if lock acquired
raise Netconf::LockError otherwise

# File lib/junos-ez/utils/config.rb, line 186
def lock!
  @ndev.rpc.lock_configuration
  true
end
rollback!( rollback_id = 0 ) click to toggle source

rollback! - used to rollback the configuration


# 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!() click to toggle source

unlock! - releases exclusive lock on candidate config

— returns —

true if lock release
raise Netconf::RpcError otherwise

# File lib/junos-ez/utils/config.rb, line 199
def unlock!
  @ndev.rpc.unlock_configuration
  true
end