Sha256: 021836436f339281b4edbfd1cb8ebd7a70c6dab3da434eb6d81b6ade208376e6

Contents?: true

Size: 1.26 KB

Versions: 2

Compression:

Stored size: 1.26 KB

Contents

# frozen_string_literal: true

module Facter
  module Resolvers
    class Hostname < BaseResolver
      @log = Facter::Log.new(self)
      @semaphore = Mutex.new
      @fact_list ||= {}
      class << self
        private

        def post_resolve(fact_name)
          @fact_list.fetch(fact_name) { retrieve_hostname(fact_name) }
        end

        def retrieve_hostname(fact_name)
          output, _status = Open3.capture2('hostname')

          # get domain
          domain = read_domain(output)

          # get hostname
          hostname = output =~ /(.*?)\./ ? Regexp.last_match(1) : output
          @fact_list[:hostname] ||= hostname&.strip
          @fact_list[:domain] ||= domain&.strip
          @fact_list[:fqdn] ||= "#{@fact_list[:hostname]}.#{@fact_list[:domain]}"
          @fact_list[fact_name]
        end

        def read_domain(output)
          if output =~ /.*?\.(.+$)/
            domain = Regexp.last_match(1)
          elsif File.exist?('/etc/resolv.conf')
            file = File.read('/etc/resolv.conf')
            if file =~ /^search\s+(\S+)/
              domain = Regexp.last_match(1)
            elsif file =~ /^domain\s+(\S+)/
              domain = Regexp.last_match(1)
            end
          end
          domain
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
facter-4.0.9.pre lib/resolvers/hostname_resolver.rb
facter-4.0.8.pre lib/resolvers/hostname_resolver.rb