Class: Facter::FactGroups

Inherits:
Object
  • Object
show all
Defined in:
lib/facter/framework/config/fact_groups.rb

Constant Summary collapse

STRING_TO_SECONDS =
{ 'seconds' => 1, 'minutes' => 60, 'hours' => 3600, 'days' => 3600 * 24 }.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(group_list_path = nil) ⇒ FactGroups

Returns a new instance of FactGroups.



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/facter/framework/config/fact_groups.rb', line 11

def initialize(group_list_path = nil)
  default_path = File.join(File.dirname(__FILE__), '../../fact_groups.conf')
  @groups_file_path = group_list_path || default_path
  @groups ||= File.readable?(@groups_file_path) ? Hocon.load(@groups_file_path) : {}
  load_groups
  load_groups_from_options
  load_facts_ttls

  # Reverse sort facts so that children have precedence when caching. eg: os.macosx vs os
  @facts_ttls = @facts_ttls.sort.reverse.to_h
end

Instance Attribute Details

#block_listObject (readonly)



5
6
7
# File 'lib/facter/framework/config/fact_groups.rb', line 5

def block_list
  @block_list
end

#facts_ttlsObject (readonly)



5
6
7
# File 'lib/facter/framework/config/fact_groups.rb', line 5

def facts_ttls
  @facts_ttls
end

#groupsObject (readonly)



5
6
7
# File 'lib/facter/framework/config/fact_groups.rb', line 5

def groups
  @groups
end

Instance Method Details

#blocked_factsObject

Breakes down blocked groups in blocked facts



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/facter/framework/config/fact_groups.rb', line 24

def blocked_facts
  fact_list = []

  @block_list.each do |group_name|
    facts_for_block = @groups[group_name]

    fact_list += facts_for_block || [group_name]
  end

  fact_list
end

#get_fact(fact_name) ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/facter/framework/config/fact_groups.rb', line 51

def get_fact(fact_name)
  return @facts_ttls[fact_name] if @facts_ttls[fact_name]

  result = @facts_ttls.select { |name, fact| break fact if fact_name =~ /^#{name}\..*/ }
  return nil if result == {}

  result
end

#get_fact_group(fact_name) ⇒ Object

Get the group name a fact is part of



37
38
39
40
41
42
# File 'lib/facter/framework/config/fact_groups.rb', line 37

def get_fact_group(fact_name)
  fact = get_fact(fact_name)
  return fact[:group] if fact

  @groups.detect { |k, v| break k if Array(v).find { |f| fact_name =~ /^#{f}.*/ } }
end

#get_group_ttls(group_name) ⇒ Object

Get config ttls for a given group



45
46
47
48
49
# File 'lib/facter/framework/config/fact_groups.rb', line 45

def get_group_ttls(group_name)
  return unless (ttls = @groups_ttls.find { |g| g[group_name] })

  ttls_to_seconds(ttls[group_name])
end