Sha256: 3bfd003d2daace99ad419aca49aa417834c97401448cacd906a8a55b1a6b4fbf

Contents?: true

Size: 1.97 KB

Versions: 13

Compression:

Stored size: 1.97 KB

Contents

# frozen_string_literal: true

require 'rbconfig'

class OsDetector
  include Singleton

  attr_reader :identifier, :version, :hierarchy

  def initialize(*_args)
    @log = Facter::Log.new(self)
    @os_hierarchy = Facter::OsHierarchy.new
    @identifier = detect
  end

  private

  def detect
    host_os = RbConfig::CONFIG['host_os']
    identifier = case host_os
                 when /mswin|msys|mingw|cygwin|bccwin|wince|emc/
                   :windows
                 when /darwin|mac os/
                   :macosx
                 when /linux/
                   detect_distro
                 when /freebsd/
                   :freebsd
                 when /bsd/
                   :bsd
                 when /solaris/
                   :solaris
                 when /aix/
                   :aix
                 else
                   raise "unknown os: #{host_os.inspect}"
                 end

    @hierarchy = detect_hierarchy(identifier)
    @identifier = identifier
  end

  def detect_hierarchy(identifier)
    hierarchy = @os_hierarchy.construct_hierarchy(identifier)
    if hierarchy.empty?
      @log.debug("Could not detect hierarchy using os identifier: #{identifier} , trying with family")

      detect_family.to_s.split.each do |family|
        hierarchy = @os_hierarchy.construct_hierarchy(family)
        return hierarchy unless hierarchy.empty?
      end
    end

    if hierarchy.empty?
      @log.debug("Could not detect hierarchy using family #{detect_family}, falling back to Linux")
      hierarchy = @os_hierarchy.construct_hierarchy(:linux)
    end

    hierarchy
  end

  def detect_family
    Facter::Resolvers::OsRelease.resolve(:id_like)
  end

  def detect_distro
    [Facter::Resolvers::OsRelease,
     Facter::Resolvers::RedHatRelease,
     Facter::Resolvers::SuseRelease].each do |resolver|
      @identifier = resolver.resolve(:identifier)
      @version = resolver.resolve(:version)
      break if @identifier
    end

    @identifier&.downcase&.to_sym
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
facter-4.0.47 lib/facter/framework/detector/os_detector.rb
facter-4.0.46 lib/facter/framework/detector/os_detector.rb
facter-4.0.44 lib/facter/framework/detector/os_detector.rb
facter-4.0.43 lib/facter/framework/detector/os_detector.rb
facter-4.0.42 lib/facter/framework/detector/os_detector.rb
facter-4.0.41 lib/facter/framework/detector/os_detector.rb
facter-4.0.40 lib/facter/framework/detector/os_detector.rb
facter-4.0.39 lib/facter/framework/detector/os_detector.rb
facter-4.0.38 lib/facter/framework/detector/os_detector.rb
facter-4.0.37 lib/facter/framework/detector/os_detector.rb
facter-4.0.36 lib/facter/framework/detector/os_detector.rb
facter-4.0.35 lib/facter/framework/detector/os_detector.rb
facter-4.0.34 lib/facter/framework/detector/os_detector.rb