Sha256: 8dc5b10663691c8fe06b147af9dccd74a8bb868d85f0bd2291ad36345a7a6957

Contents?: true

Size: 1.5 KB

Versions: 23

Compression:

Stored size: 1.5 KB

Contents

module MCollective
  module Facts
    require 'yaml'

    # 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.
    class Yaml_facts<Base
      def initialize
        @yaml_file_mtimes = {}

        super
      end

      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)
              facts.merge!(YAML.load(File.read(file)))
            else
              raise("Can't find YAML file to load: #{file}")
            end
          rescue Exception => e
            Log.error("Failed to load yaml facts from #{file}: #{e.class}: #{e}")
          end
        end

        facts
      end

      # force fact reloads when the mtime on the yaml file change
      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

          if mtime > @yaml_file_mtimes[file]
            @yaml_file_mtimes[file] = mtime

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

            return true
          end
        end

        false
      end
    end
  end
end

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
mcollective-client-2.10.6 lib/mcollective/facts/yaml_facts.rb
mcollective-client-2.11.4 lib/mcollective/facts/yaml_facts.rb
mcollective-client-2.11.3 lib/mcollective/facts/yaml_facts.rb
mcollective-client-2.11.2 lib/mcollective/facts/yaml_facts.rb
mcollective-client-2.11.1 lib/mcollective/facts/yaml_facts.rb
mcollective-client-2.11.0 lib/mcollective/facts/yaml_facts.rb
mcollective-client-2.10.4 lib/mcollective/facts/yaml_facts.rb
mcollective-client-2.10.3 lib/mcollective/facts/yaml_facts.rb
mcollective-client-2.10.2 lib/mcollective/facts/yaml_facts.rb
mcollective-client-2.10.1 lib/mcollective/facts/yaml_facts.rb
mcollective-client-2.10.0 lib/mcollective/facts/yaml_facts.rb
mcollective-client-2.8.7 lib/mcollective/facts/yaml_facts.rb
mcollective-client-2.8.5 lib/mcollective/facts/yaml_facts.rb
mcollective-client-2.8.8 lib/mcollective/facts/yaml_facts.rb
mcollective-client-2.8.6 lib/mcollective/facts/yaml_facts.rb
mcollective-client-2.9.1 lib/mcollective/facts/yaml_facts.rb
mcollective-client-2.9.0 lib/mcollective/facts/yaml_facts.rb
mcollective-client-2.8.9 lib/mcollective/facts/yaml_facts.rb
mcollective-client-2.8.4 lib/mcollective/facts/yaml_facts.rb
mcollective-client-2.8.3 lib/mcollective/facts/yaml_facts.rb