Sha256: 7560a3dc05c801f29332dff9a57ece7422bdce9ff875285a62c99deaf9a1c339

Contents?: true

Size: 1.67 KB

Versions: 20

Compression:

Stored size: 1.67 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)
              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

      # 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

          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
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
choria-mcorpc-support-2.26.5 lib/mcollective/facts/yaml_facts.rb
choria-mcorpc-support-2.26.4 lib/mcollective/facts/yaml_facts.rb
choria-mcorpc-support-2.26.3 lib/mcollective/facts/yaml_facts.rb
choria-mcorpc-support-2.26.2 lib/mcollective/facts/yaml_facts.rb
choria-mcorpc-support-2.26.1 lib/mcollective/facts/yaml_facts.rb
choria-mcorpc-support-2.26.0 lib/mcollective/facts/yaml_facts.rb
choria-mcorpc-support-2.25.3 lib/mcollective/facts/yaml_facts.rb
choria-mcorpc-support-2.25.2 lib/mcollective/facts/yaml_facts.rb
choria-mcorpc-support-2.25.1 lib/mcollective/facts/yaml_facts.rb
choria-mcorpc-support-2.25.0 lib/mcollective/facts/yaml_facts.rb
choria-mcorpc-support-2.24.4 lib/mcollective/facts/yaml_facts.rb
choria-mcorpc-support-2.24.3 lib/mcollective/facts/yaml_facts.rb
choria-mcorpc-support-2.24.2 lib/mcollective/facts/yaml_facts.rb
choria-mcorpc-support-2.24.1 lib/mcollective/facts/yaml_facts.rb
choria-mcorpc-support-2.24.0 lib/mcollective/facts/yaml_facts.rb
choria-mcorpc-support-2.23.3 lib/mcollective/facts/yaml_facts.rb
choria-mcorpc-support-2.23.2 lib/mcollective/facts/yaml_facts.rb
choria-mcorpc-support-2.23.1 lib/mcollective/facts/yaml_facts.rb
choria-mcorpc-support-2.23.0 lib/mcollective/facts/yaml_facts.rb
choria-mcorpc-support-2.23.0.pre lib/mcollective/facts/yaml_facts.rb