Class: MCollective::Facts::Yaml_facts

Inherits:
Base
  • Object
show all
Defined in:
lib/mcollective/facts/yaml_facts.rb

Overview

A factsource that reads a hash of facts from a YAML file

Multiple files can be specified seperated with a : in the config file, they will be merged with later files overriding earlier ones in the list.

Instance Method Summary collapse

Methods inherited from Base

#get_fact, #get_facts, #has_fact?, inherited

Constructor Details

#initializeYaml_facts

Returns a new instance of Yaml_facts.



11
12
13
14
15
# File 'lib/mcollective/facts/yaml_facts.rb', line 11

def initialize
  @yaml_file_mtimes = {}

  super
end

Instance Method Details

#force_reload?Boolean

force fact reloads when the mtime on the yaml file change

Returns:

  • (Boolean)


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/mcollective/facts/yaml_facts.rb', line 43

def force_reload?
  config = Config.instance

  fact_files = config.pluginconf["yaml"].split(File::PATH_SEPARATOR)

  fact_files.each do |file|
    @yaml_file_mtimes[file] ||= File.stat(file).mtime
    mtime = File.stat(file).mtime

    next unless mtime > @yaml_file_mtimes[file]

    @yaml_file_mtimes[file] = mtime

    Log.debug("Forcing fact reload due to age of #{file}")

    return true
  end

  false
end

#load_facts_from_sourceObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/mcollective/facts/yaml_facts.rb', line 17

def load_facts_from_source
  config = Config.instance

  fact_files = config.pluginconf["yaml"].split(File::PATH_SEPARATOR)
  facts = {}

  fact_files.each do |file|
    begin
      if File.exist?(file)
        if YAML.respond_to? :safe_load
          facts.merge!(YAML.safe_load(File.read(file)))
        else
          facts.merge!(YAML.load(File.read(file)))
        end
      else
        raise("Can't find YAML file to load: #{file}")
      end
    rescue Exception => e # rubocop:disable Lint/RescueException
      Log.error("Failed to load yaml facts from #{file}: #{e.class}: #{e}")
    end
  end

  facts
end