class Puppet::Util::NetworkDevice::Config

Attributes

devices[R]

Public Class Methods

devices() click to toggle source
   # File lib/puppet/util/network_device/config.rb
11 def self.devices
12   main.devices || []
13 end
main() click to toggle source
  # File lib/puppet/util/network_device/config.rb
7 def self.main
8   @main ||= self.new
9 end
new() click to toggle source
   # File lib/puppet/util/network_device/config.rb
21 def initialize
22   @file = Puppet::Util::WatchedFile.new(Puppet[:deviceconfig])
23 
24   @devices = {}
25 
26   read(true) # force reading at start
27 end

Public Instance Methods

exists?() click to toggle source
   # File lib/puppet/util/network_device/config.rb
17 def exists?
18   Puppet::FileSystem.exist?(@file.to_str)
19 end
read(force = false) click to toggle source

Read the configuration file.

   # File lib/puppet/util/network_device/config.rb
30 def read(force = false)
31   return unless exists?
32 
33   parse if force or @file.changed?
34 end

Private Instance Methods

parse() click to toggle source
   # File lib/puppet/util/network_device/config.rb
38 def parse
39   begin
40     devices = {}
41     device = nil
42     File.open(@file) do |f|
43       file_line_count = 1
44       f.each do |line|
45         case line
46         when /^\s*#/ # skip comments
47           file_line_count += 1
48           next
49         when /^\s*$/  # skip blank lines
50           file_line_count += 1
51           next
52         when /^\[([\w.-]+)\]\s*$/ # [device.fqdn]
53           name = $1
54           name.chomp!
55           if devices.include?(name)
56             file_error_location = Puppet::Util::Errors.error_location(nil, file_line_count)
57             device_error_location = Puppet::Util::Errors.error_location(nil, device.line)
58             raise Puppet::Error, _("Duplicate device found at %{file_error_location}, already found at %{device_error_location}") %
59                 { file_error_location: file_error_location, device_error_location: device_error_location }
60           end
61           device = OpenStruct.new
62           device.name = name
63           device.line = file_line_count
64           device.options = { :debug => false }
65           Puppet.debug "found device: #{device.name} at #{device.line}"
66           devices[name] = device
67         when /^\s*(type|url|debug)(\s+(.+)\s*)*$/
68           parse_directive(device, $1, $3, file_line_count)
69         else
70           error_location_str = Puppet::Util::Errors.error_location(nil, file_line_count)
71           raise Puppet::Error, _("Invalid entry at %{error_location}: %{file_text}") %
72               { error_location: error_location_str, file_text: line }
73         end
74       end
75     end
76   rescue Errno::EACCES
77     Puppet.err _("Configuration error: Cannot read %{file}; cannot serve") % { file: @file }
78     #raise Puppet::Error, "Cannot read #{@config}"
79   rescue Errno::ENOENT
80     Puppet.err _("Configuration error: '%{file}' does not exit; cannot serve") % { file: @file }
81   end
82 
83   @devices = devices
84 end
parse_directive(device, var, value, count) click to toggle source
    # File lib/puppet/util/network_device/config.rb
 86 def parse_directive(device, var, value, count)
 87   case var
 88   when "type"
 89     device.provider = value
 90   when "url"
 91     begin
 92       URI.parse(value)
 93     rescue URI::InvalidURIError
 94       raise Puppet::Error, _("%{value} is an invalid url") % { value: value }
 95     end
 96     device.url = value
 97   when "debug"
 98     device.options[:debug] = true
 99   else
100     error_location_str = Puppet::Util::Errors.error_location(nil, count)
101     raise Puppet::Error, _("Invalid argument '%{var}' at %{error_location}") % { var: var, error_location: error_location_str }
102   end
103 end