Class: Hocho::Inventory
- Inherits:
-
Object
- Object
- Hocho::Inventory
- Defined in:
- lib/hocho/inventory.rb
Instance Method Summary collapse
- #filter(include_filters, exclude_filters: []) ⇒ Object
- #hosts ⇒ Object
-
#initialize(providers, property_providers) ⇒ Inventory
constructor
A new instance of Inventory.
Constructor Details
#initialize(providers, property_providers) ⇒ Inventory
Returns a new instance of Inventory.
3 4 5 6 |
# File 'lib/hocho/inventory.rb', line 3 def initialize(providers, property_providers) @providers = providers @property_providers = property_providers end |
Instance Method Details
#filter(include_filters, exclude_filters: []) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/hocho/inventory.rb', line 23 def filter(include_filters, exclude_filters: []) include_filters, exclude_filters = [include_filters, exclude_filters].map do |f| f.map do |name, value| values = value.to_s.split(?,).map! do |_| if _[0] == '/' && _[-1] == '/' Regexp.new(_[1...-1]) else /#{Regexp.escape(_).gsub(/\*/,'.*')}/ end end [name.to_s, values] end.to_h end filters = include_filters.map do |name, conditions| [name, [conditions, exclude_filters.fetch(name, [])]] end hosts.select do |host| filters.all? do |name, (conditions, exclude_conditions)| case name when 'name' conditions.any? { |c| host.name.match(c) } && !exclude_conditions.any? { |c| host.name.match(c) } else v = (host.attributes[name] || host.attributes[name.to_sym] || host.[name] || host.[name.to_sym]) v && conditions.any? { |c| v.to_s.match(c) } && !exclude_conditions.any?{ |c| v.to_s.match(c) } end end end end |
#hosts ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/hocho/inventory.rb', line 8 def hosts @hosts ||= @providers.inject({}) do |r,provider| provider.hosts.each do |host| if r.key?(host.name) r[host.name].merge!(host) else r[host.name] = host end end r end.each do |name, host| host.apply_property_providers(@property_providers) end.values end |