HEX
Server: Apache
System: Linux vpshost0650.publiccloud.com.br 4.4.79-grsec-1.lc.x86_64 #1 SMP Wed Aug 2 14:18:21 -03 2017 x86_64
User: bandeirantesbomb3 (10068)
PHP: 8.0.7
Disabled: apache_child_terminate,dl,escapeshellarg,escapeshellcmd,exec,link,mail,openlog,passthru,pcntl_alarm,pcntl_exec,pcntl_fork,pcntl_get_last_error,pcntl_getpriority,pcntl_setpriority,pcntl_signal,pcntl_signal_dispatch,pcntl_sigprocmask,pcntl_sigtimedwait,pcntl_sigwaitinfo,pcntl_strerror,pcntl_wait,pcntl_waitpid,pcntl_wexitstatus,pcntl_wifexited,pcntl_wifsignaled,pcntl_wifstopped,pcntl_wstopsig,pcntl_wtermsig,php_check_syntax,php_strip_whitespace,popen,proc_close,proc_open,shell_exec,symlink,system
Upload Files
File: //proc/thread-self/root/opt/puppetlabs/puppet/lib/ruby/vendor_ruby/mcollective/facts/base.rb
module MCollective
  module Facts
    # A base class for fact providers, to make a new fully functional fact provider
    # inherit from this and simply provide a self.get_facts method that returns a
    # hash like:
    #
    #  {"foo" => "bar",
    #   "bar" => "baz"}
    class Base
      def initialize
        @mutex = Mutex.new
        @facts = {}
        @last_good_facts = {}
        @last_facts_load = 0
      end

      # Registers new fact sources into the plugin manager
      def self.inherited(klass)
        PluginManager << {:type => "facts_plugin", :class => klass.to_s}
      end

      # Returns the value of a single fact
      def get_fact(fact=nil)
        config = Config.instance

        cache_time = config.fact_cache_time || 300

        @mutex.synchronize do
          begin
            if (Time.now.to_i - @last_facts_load > cache_time.to_i ) || force_reload?
              Log.debug("Resetting facter cache, now: #{Time.now.to_i} last-known-good: #{@last_facts_load}")

              tfacts = load_facts_from_source

              # Force reset to last known good state on empty facts
              raise "Got empty facts" if tfacts.empty?

              @facts = normalize_facts(tfacts)

              @last_good_facts = @facts.clone
              @last_facts_load = Time.now.to_i
            else
              Log.debug("Using cached facts now: #{Time.now.to_i} last-known-good: #{@last_facts_load}")
            end
          rescue Exception => e
            Log.error("Failed to load facts: #{e.class}: #{e}")

            # Avoid loops where failing fact loads cause huge CPU
            # loops, this way it only retries once every cache_time
            # seconds
            @last_facts_load = Time.now.to_i

            # Revert to last known good state
            @facts = @last_good_facts.clone
          end
        end


        # If you do not supply a specific fact all facts will be returned
        if fact.nil?
          return @facts
        else
          @facts.include?(fact) ? @facts[fact] : nil
        end
      end

      # Returns all facts
      def get_facts
        get_fact(nil)
      end

      # Returns true if we know about a specific fact, false otherwise
      def has_fact?(fact)
        get_fact(nil).include?(fact)
      end

      # Plugins can override this to provide forced fact invalidation
      def force_reload?
        false
      end

      private

      def normalize_facts(value)
        case value
        when Array
          return value.map { |v| normalize_facts(v) }
        when Hash
          new_hash = {}
          value.each do |k,v|
            new_hash[k.to_s] = normalize_facts(v)
          end
          return new_hash
        else
          return value.to_s
        end
      end
    end
  end
end