Sha256: e85698b75b0febf438cd03775edb4477498bf7c577bc1b880cef6f3a1c7fa0a9

Contents?: true

Size: 1.27 KB

Versions: 5

Compression:

Stored size: 1.27 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)
          else
            file_content = Util::FileHelper.safe_read('/etc/resolv.conf')
            if file_content =~ /^search\s+(\S+)/
              domain = Regexp.last_match(1)
            elsif file_content =~ /^domain\s+(\S+)/
              domain = Regexp.last_match(1)
            end
          end
          domain
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
facter-4.0.20 lib/resolvers/hostname_resolver.rb
facter-4.0.19 lib/resolvers/hostname_resolver.rb
facter-4.0.18 lib/resolvers/hostname_resolver.rb
facter-4.0.17 lib/resolvers/hostname_resolver.rb
facter-4.0.16 lib/resolvers/hostname_resolver.rb