Class: NetdevJunos::Device

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet/provider/junos/junos_netdev_device.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(catalog_version) ⇒ Device

Returns a new instance of Device.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/puppet/provider/junos/junos_netdev_device.rb', line 14

def initialize( catalog_version )
  
  @catalog_version = catalog_version
  @edits_count = 0
  @ready = false      
  
  fqdn = Facter.value(:fqdn)       
  Puppet::Transaction.on_transaction_done = self.method(:commit)      

  NetdevJunos::Log.debug "Opening a local connection: #{fqdn}"  
  @netconf = Netconf::IOProc.new
  @netconf.instance_variable_set :@trans_timeout, nil
  
  # --- assuming caller is doing exception handling around this!
  @netconf.open          
  
  begin 
    locked = @netconf.rpc.lock_configuration         
  rescue Netconf::RpcError => e 
    errmsg = e.to_s
    NetdevJunos::Log.err errmsg
  else
    @ready = true
  end         
  
end

Instance Attribute Details

#edits_countObject

Returns the value of attribute edits_count.



12
13
14
# File 'lib/puppet/provider/junos/junos_netdev_device.rb', line 12

def edits_count
  @edits_count
end

#netconfObject

Returns the value of attribute netconf.



12
13
14
# File 'lib/puppet/provider/junos/junos_netdev_device.rb', line 12

def netconf
  @netconf
end

#readyObject

Returns the value of attribute ready.



12
13
14
# File 'lib/puppet/provider/junos/junos_netdev_device.rb', line 12

def ready
  @ready
end

Instance Method Details

#commitObject

Commit the candidate configuration, invoked by the ‘on transaction complete’ hooked in by junos_parent.rb



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/puppet/provider/junos/junos_netdev_device.rb', line 82

def commit

  NetdevJunos::Log.debug "Checking for changes to commit for catalog #{@catalog_version}"    
  
  if @edits_count > 0 
            
    NetdevJunos::Log.info "Committing #{@edits_count} changes.", :tags => [:config, :commit]                 
            
    begin                
      report_show_compare          
      @netconf.rpc.commit_configuration( :log => "Puppet agent catalog: #{@catalog_version}" )                    
    rescue Netconf::RpcError => e
      NetdevJunos::Log.err "ERROR: Configuration change\n" + e.rsp.to_xml, :tags => [:config, :fail]       
    else
      NetdevJunos::Log.notice "OK: COMMIT success!", :tags => [:config, :success]
    ensure
      @netconf.rpc.unlock_configuration                   
    end        
    
  end # -- committing changes
 
  NetdevJunos::Log.debug "Closing NETCONF connection"
  begin
    @netconf.close
  rescue
    # ignore - could be in a prior locked condition, and the call to close
    # currently raises an RPC error.
  end
  
end

#edit_config(jcfg_obj, format) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/puppet/provider/junos/junos_netdev_device.rb', line 41

def edit_config( jcfg_obj, format )      
  if jcfg_obj.is_a?(Resource)
    edits = Netconf::JunosConfig.new(:TOP)
    edits << jcfg_obj
    load_config = edits.doc.root 
    NetdevJunos::Log.debug load_config.to_xml, :tags => [:config, :changes]
  
  else
    load_config = jcfg_obj
  end     
  
  # if there is an RPC error (syntax error), it will generate an exception, and 
  # we want that to "bubble-up" to the calling context so don't 
  # rescue it here.  
  
  begin
    
    @edits_count += 1
    @netconf.rpc.load_configuration( load_config, :action => 'replace', :format => format )
    
  rescue Netconf::RpcError => e
    # the load_configuration may yeield rpc-errors that are in fact not errors, 
    # but merely warnings.  Check for that here.
    if rpc_errs = e.rsp.xpath('//rpc-error')
      # ignore warnings ...
      all_count = rpc_errs.count
      warn_count = rpc_errs.xpath('error-severity').select{|err| err.text == 'warning'}.count 
      if all_count - warn_count > 0          
        @edits_count -= 1
        NetdevJunos::Log.err "ERROR: load-configuration\n" + e.rsp.to_xml, :tags => [:config, :fail]
      end
    end
  end # rescue block
  
end

#report_show_compareObject



113
114
115
116
117
118
# File 'lib/puppet/provider/junos/junos_netdev_device.rb', line 113

def report_show_compare
  args = { :database=>'candidate', :compare=>'rollback', :rollback=>'0', :format=>'text' }
  compare_rsp = @netconf.rpc.get_configuration( args )
  diff = "\n" + compare_rsp.xpath('//configuration-output').text   
  NetdevJunos::Log.notice( diff, :tags => [:config, :changes] )
end