Class: MotherBrain::NodeFilter
- Inherits:
-
Object
- Object
- MotherBrain::NodeFilter
- Defined in:
- lib/mb/node_filter.rb
Instance Attribute Summary collapse
- #segments ⇒ Array<String> readonly
Class Method Summary collapse
-
.expand_ipranges(segments) ⇒ Array<String>
Expands any IP address ranges in the given segments and returns the segments Array with any IP ranges expanded.
-
.filter(segments, nodes) ⇒ Array
Filters the given nodes based on the given segments.
Instance Method Summary collapse
-
#filter(nodes) ⇒ Array
Filters the given array of nodes against the segments and returns the matched nodes.
-
#initialize(segments) ⇒ NodeFilter
constructor
A new instance of NodeFilter.
-
#ipaddress?(segment) ⇒ Boolean
Checks the given segment and returns true if it is an ipaddress.
-
#iprange(segment) ⇒ Array
Checks the given segment and either returns nil, if it is not a range of IPs or expands and returns the range as an array.
-
#matches?(node) ⇒ Boolean
Checks the node against the instance’s segments and returns nodes that match an ipaddress or hostname.
Constructor Details
#initialize(segments) ⇒ NodeFilter
Returns a new instance of NodeFilter.
36 37 38 |
# File 'lib/mb/node_filter.rb', line 36 def initialize(segments) @segments = Array(segments).flatten end |
Instance Attribute Details
#segments ⇒ Array<String> (readonly)
32 33 34 |
# File 'lib/mb/node_filter.rb', line 32 def segments @segments end |
Class Method Details
.expand_ipranges(segments) ⇒ Array<String>
Expands any IP address ranges in the given segments and returns the segments Array with any IP ranges expanded.
22 23 24 25 26 27 28 |
# File 'lib/mb/node_filter.rb', line 22 def (segments) node_filter = new(segments) segments.collect do |segment| range = node_filter.iprange(segment) range.nil? ? segment : range end.flatten end |
.filter(segments, nodes) ⇒ Array
Filters the given nodes based on the given segments
11 12 13 |
# File 'lib/mb/node_filter.rb', line 11 def filter(segments, nodes) new(segments).filter(nodes) end |
Instance Method Details
#filter(nodes) ⇒ Array
Filters the given array of nodes against the segments and returns the matched nodes.
46 47 48 |
# File 'lib/mb/node_filter.rb', line 46 def filter(nodes) nodes.select { |node| matches?(node) } end |
#ipaddress?(segment) ⇒ Boolean
Checks the given segment and returns true if it is an ipaddress.
76 77 78 |
# File 'lib/mb/node_filter.rb', line 76 def ipaddress?(segment) segment.match(/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) end |
#iprange(segment) ⇒ Array
Checks the given segment and either returns nil, if it is not a range of IPs or expands and returns the range as an array.
90 91 92 93 94 95 |
# File 'lib/mb/node_filter.rb', line 90 def iprange(segment) match = segment.match(/^(\d{1,3}\.\d{1,3}\.\d{1,3})\.(\d{1,3}-\d{1,3})$/) return nil unless match first,last = match[2].split('-') (first..last).to_a.collect {|l| "#{match[1]}.#{l}" } end |
#matches?(node) ⇒ Boolean
Checks the node against the instance’s segments and returns nodes that match an ipaddress or hostname.
56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/mb/node_filter.rb', line 56 def matches?(node) segments.empty? || segments.any? do |s| if ipaddress?(s) s == node.public_ipv4 elsif r = iprange(s) r.include?(node.public_ipv4) # elsif regex?(s) else s == node.public_hostname || s == node.public_hostname.sub(/\..*/,'') end end end |