class Puppet::Util::NetworkDevice::Config

Attributes

devices[R]

Public Class Methods

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

Public Instance Methods

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

Read the configuration file.

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

Private Instance Methods

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