class Facter::FactGroups

Constants

STRING_TO_SECONDS

Attributes

block_list[R]
facts_ttls[R]
groups[R]
groups_ttls[RW]

Public Class Methods

new() click to toggle source
# File lib/facter/framework/config/fact_groups.rb, line 12
def initialize
  @groups = Facter::Config::FACT_GROUPS.dup
  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

Public Instance Methods

blocked_facts() click to toggle source

Breakes down blocked groups in blocked facts

# File lib/facter/framework/config/fact_groups.rb, line 23
def blocked_facts
  fact_list = []

  @block_list.each do |group_name|
    # legacy is a special group and does not need to be broken into facts
    next if group_name == 'legacy'

    facts_for_block = @groups[group_name]

    fact_list += facts_for_block || [group_name]
  end

  fact_list
end
get_fact(fact_name) click to toggle source
# File lib/facter/framework/config/fact_groups.rb, line 56
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) click to toggle source

Get the group name a fact is part of

# File lib/facter/framework/config/fact_groups.rb, line 39
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}.*/ } }
  @groups.detect do |k, v|
    break k if Array(v).find { |f| fact_name.include?('.*') ? fact_name == f : fact_name =~ /^#{f}.*/ }
  end
end
get_group_ttls(group_name) click to toggle source

Get config ttls for a given group

# File lib/facter/framework/config/fact_groups.rb, line 50
def get_group_ttls(group_name)
  return unless (ttls = @groups_ttls.find { |g| g[group_name] })

  ttls_to_seconds(ttls[group_name])
end

Private Instance Methods

load_facts_ttls() click to toggle source
# File lib/facter/framework/config/fact_groups.rb, line 79
def load_facts_ttls
  @facts_ttls ||= {}
  return if @groups_ttls == []

  @groups_ttls.reduce(:merge).each do |group, ttls|
    ttls = ttls_to_seconds(ttls)
    if @groups[group]
      @groups[group].each do |fact|
        if (@facts_ttls[fact] && @facts_ttls[fact][:ttls] < ttls) || @facts_ttls[fact].nil?
          @facts_ttls[fact] = { ttls: ttls, group: group }
        end
      end
    else
      @facts_ttls[group] = { ttls: ttls, group: group }
    end
  end
end
load_groups() click to toggle source
# File lib/facter/framework/config/fact_groups.rb, line 97
def load_groups
  config = ConfigReader.init(Options[:config])
  @block_list = config.block_list || []
  @groups_ttls = config.ttls || []
  @groups.merge!(config.fact_groups) if config.fact_groups
end
load_groups_from_options() click to toggle source
# File lib/facter/framework/config/fact_groups.rb, line 67
def load_groups_from_options
  Options.external_dir.each do |dir|
    next unless Dir.exist?(dir)

    ext_facts = Dir.entries(dir)
    ext_facts.reject! { |ef| ef =~ /^(\.|\.\.)$/ }
    ext_facts.each do |ef|
      @groups[ef] = nil
    end
  end
end
ttls_to_seconds(ttls) click to toggle source
# File lib/facter/framework/config/fact_groups.rb, line 104
def ttls_to_seconds(ttls)
  duration, unit = ttls.split(' ', 2)
  duration.to_i * STRING_TO_SECONDS[unit]
end