Sha256: c70e60ee660639cfc66d6b6bc36d857036e373747a4aec36968f4ee63b8f6624

Contents?: true

Size: 1.99 KB

Versions: 3

Compression:

Stored size: 1.99 KB

Contents

# frozen_string_literal: true

module Facter
  module Resolvers
    class Virtualization < BaseResolver
      @log = Facter::Log.new(self)

      init_resolver

      class << self
        # Virtual
        # Is_Virtual

        MODEL_HASH = { 'VirtualBox' => 'virtualbox', 'VMware' => 'vmware', 'KVM' => 'kvm',
                       'Bochs' => 'bochs', 'Google' => 'gce', 'OpenStack' => 'openstack' }.freeze

        private

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

        def read_fact_from_computer_system(fact_name)
          win = Facter::Util::Windows::Win32Ole.new
          comp = win.exec_query('SELECT Manufacturer,Model,OEMStringArray FROM Win32_ComputerSystem')
          unless comp
            @log.debug 'WMI query returned no results for Win32_ComputerSystem with values'\
            ' Manufacturer, Model and OEMStringArray.'
            return
          end

          build_fact_list(comp)
          @fact_list[fact_name]
        end

        def determine_hypervisor_by_model(comp)
          MODEL_HASH[MODEL_HASH.keys.find { |key| comp.Model =~ /^#{key}/ }]
        end

        def determine_hypervisor_by_manufacturer(comp)
          manufacturer = comp.Manufacturer
          if comp.Model =~ /^Virtual Machine/ && manufacturer =~ /^Microsoft/
            'hyperv'
          elsif manufacturer =~ /^Xen/
            'xen'
          elsif manufacturer =~ /^Amazon EC2/
            'kvm'
          else
            'physical'
          end
        end

        def build_fact_list(comp)
          @fact_list[:oem_strings] = []
          @fact_list[:oem_strings] += comp.to_enum.map(&:OEMStringArray).flatten

          comp = comp.to_enum.first
          hypervisor = determine_hypervisor_by_model(comp) || determine_hypervisor_by_manufacturer(comp)

          @fact_list[:virtual] = hypervisor
          @fact_list[:is_virtual] = hypervisor.include?('physical') ? false : true
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
facter-4.0.49 lib/facter/resolvers/windows/virtualization.rb
facter-4.0.48 lib/facter/resolvers/windows/virtualization.rb
facter-4.0.47 lib/facter/resolvers/windows/virtualization_resolver.rb