Sha256: 97d6aff5d8d95b2e8ec675038dd07241c650ca33feaed029d5f908fda4c51ceb

Contents?: true

Size: 1.73 KB

Versions: 15

Compression:

Stored size: 1.73 KB

Contents

# frozen_string_literal: true

module Facts
  module Linux
    module Hypervisors
      class Kvm
        FACT_NAME = 'hypervisors.kvm'

        def initialize
          @log = Facter::Log.new(self)
        end

        def call_the_resolver
          hypervisor = discover_hypervisor
          @log.debug("Detected hypervisor #{hypervisor}")

          return Facter::ResolvedFact.new(FACT_NAME, nil) if %w[virtualbox parallels].include?(hypervisor)

          fact_value = discover_provider if kvm?

          Facter::ResolvedFact.new(FACT_NAME, fact_value)
        end

        private

        def discover_hypervisor
          product_name = Facter::Resolvers::Linux::DmiBios.resolve(:product_name)
          @log.debug("Detected product name: #{product_name}")

          return unless product_name

          Facter::Util::Facts::HYPERVISORS_HASH.each { |key, value| return value if product_name.include?(key) }

          product_name
        end

        def kvm?
          bios_vendor = Facter::Resolvers::Linux::DmiBios.resolve(:bios_vendor)
          @log.debug("Detected bios vendor: #{bios_vendor}")

          Facter::Resolvers::VirtWhat.resolve(:vm) == 'kvm' ||
            Facter::Resolvers::Lspci.resolve(:vm) == 'kvm' ||
            bios_vendor&.include?('Amazon EC2') ||
            bios_vendor&.include?('Google')
        end

        def discover_provider
          manufacturer = Facter::Resolvers::Linux::DmiBios.resolve(:sys_vendor)
          @log.debug("Detected manufacturer: #{manufacturer}")

          return { google: true } if manufacturer == 'Google'

          return { openstack: true } if manufacturer =~ /^OpenStack/

          return { amazon: true } if manufacturer =~ /^Amazon/

          {}
        end
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
facter-4.2.6 lib/facter/facts/linux/hypervisors/kvm.rb
facter-4.2.5 lib/facter/facts/linux/hypervisors/kvm.rb
facter-4.2.4 lib/facter/facts/linux/hypervisors/kvm.rb
facter-4.2.3 lib/facter/facts/linux/hypervisors/kvm.rb
facter-4.2.2 lib/facter/facts/linux/hypervisors/kvm.rb
facter-4.2.1 lib/facter/facts/linux/hypervisors/kvm.rb
facter-4.2.0 lib/facter/facts/linux/hypervisors/kvm.rb
facter-4.1.1 lib/facter/facts/linux/hypervisors/kvm.rb
facter-4.1.0 lib/facter/facts/linux/hypervisors/kvm.rb
facter-4.0.52 lib/facter/facts/linux/hypervisors/kvm.rb
facter-4.0.51 lib/facter/facts/linux/hypervisors/kvm.rb
facter-4.0.50 lib/facter/facts/linux/hypervisors/kvm.rb
facter-4.0.49 lib/facter/facts/linux/hypervisors/kvm.rb
facter-4.0.48 lib/facter/facts/linux/hypervisors/kvm.rb
facter-4.0.47 lib/facter/facts/linux/hypervisors/kvm.rb