Class: Iface::Config
- Inherits:
-
Object
- Object
- Iface::Config
- Defined in:
- lib/iface/config.rb
Overview
Represents a set of ConfigFiles for network interface configuration
Constant Summary collapse
- Reserved_IP_Ranges =
%w[10.0.0.0/8 192.168.0.0/16].collect { |i| IPAddr.new(i) }
Class Method Summary collapse
Instance Method Summary collapse
-
#[](device) ⇒ Object
Returns the ‘PrimaryFile` for `device`.
-
#add(filename, io) ⇒ Object
Adds the given ‘filename` and `io` and returns self.
-
#initialize ⇒ Config
constructor
A new instance of Config.
-
#primary ⇒ Object
Returns the ‘PrimaryFile` for the primary interface.
Constructor Details
#initialize ⇒ Config
Returns a new instance of Config.
19 20 21 22 |
# File 'lib/iface/config.rb', line 19 def initialize @files = {} @devices = {} end |
Class Method Details
.discover(pattern) ⇒ Object
11 12 13 14 15 16 17 |
# File 'lib/iface/config.rb', line 11 def self.discover(pattern) new.tap do |config| Dir.glob(pattern) do |fullname| File.open(fullname) { |io| config.add(fullname, io) } end end end |
Instance Method Details
#[](device) ⇒ Object
Returns the ‘PrimaryFile` for `device`
43 44 45 |
# File 'lib/iface/config.rb', line 43 def [](device) @devices[device] end |
#add(filename, io) ⇒ Object
Adds the given ‘filename` and `io` and returns self
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/iface/config.rb', line 25 def add(filename, io) file = ConfigFile.create(filename, io) # add to files by type file_type = file.class.file_type_name if @files.key?(file_type) @files[file_type] << file else @files[file_type] = [file] end # add PrimaryFile to devices by device name @devices[file.device] = file if file.is_a?(PrimaryFile) self end |
#primary ⇒ Object
Returns the ‘PrimaryFile` for the primary interface
There should be 0 or 1 of these; else it’s an error.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/iface/config.rb', line 50 def primary result = @files[:primary].select do |file| if !file.static? true elsif file.ip_address.nil? false else ipaddr = IPAddr.new(file.ip_address) Reserved_IP_Ranges.none? { |range| range.include?(ipaddr) } end end case result.size when 0 nil when 1 result.first else raise "Expected 0 or 1 primary files; found #{result.size}" end end |