diff --git a/app/lib/actions/foreman_wreckingball/host/remediate_vmware_operatingsystem.rb b/app/lib/actions/foreman_wreckingball/host/remediate_vmware_operatingsystem.rb index 150ac53..c71f216 100644 --- a/app/lib/actions/foreman_wreckingball/host/remediate_vmware_operatingsystem.rb +++ b/app/lib/actions/foreman_wreckingball/host/remediate_vmware_operatingsystem.rb @@ -28,20 +28,7 @@ def run current_os_id = host.vmware_facet.guest_id current_os = VsphereOsIdentifiers.lookup(current_os_id) - selectors = { - :architecture => host.architecture.name, - :osfamily => host.operatingsystem.family, - :name => host.operatingsystem.name, - :major => host.operatingsystem.major.to_i, - :release => host.facts['os::release::full'] - } - - desired_os = VsphereOsIdentifiers.find_by(selectors) - desired_os ||= VsphereOsIdentifiers.find_by(selectors.except(:major)) - desired_os ||= VsphereOsIdentifiers.find_by(selectors.except(:release)) - desired_os ||= VsphereOsIdentifiers.find_by(selectors.except(:major, :release)) - desired_os ||= VsphereOsIdentifiers.find_by(selectors.except(:major, :name)) - desired_os ||= VsphereOsIdentifiers.find_by(selectors.except(:major, :name, :release)) + desired_os = host.deduced_vsphere_guest fail _('Could not auto detect desired operatingsystem.') unless desired_os diff --git a/app/models/concerns/foreman_wreckingball/host_extensions.rb b/app/models/concerns/foreman_wreckingball/host_extensions.rb index 18c2155..95b66b1 100644 --- a/app/models/concerns/foreman_wreckingball/host_extensions.rb +++ b/app/models/concerns/foreman_wreckingball/host_extensions.rb @@ -21,5 +21,29 @@ module HostExtensions def action_input_key 'host' end + + def deduced_vsphere_guest + return unless architecture && operatingsystem + + selectors = { + architecture: architecture.name, + osfamily: operatingsystem.family, + name: operatingsystem.name, + major: operatingsystem.major.to_i + } + + deduced_os = VsphereOsIdentifiers.find_by(selectors) + deduced_os ||= VsphereOsIdentifiers.find_by(selectors.except(:major)) + deduced_os ||= VsphereOsIdentifiers.find_by(selectors.except(:release)) + deduced_os ||= VsphereOsIdentifiers.find_by(selectors.except(:major, :release)) + deduced_os ||= VsphereOsIdentifiers.find_by(selectors.except(:major, :name)) + deduced_os ||= VsphereOsIdentifiers.find_by(selectors.except(:major, :name, :release)) + + deduced_os + end + + def deduced_vsphere_guest_id + deduced_vsphere_guest&.id + end end end diff --git a/app/models/foreman_wreckingball/operatingsystem_status.rb b/app/models/foreman_wreckingball/operatingsystem_status.rb index f1af922..54a49d4 100644 --- a/app/models/foreman_wreckingball/operatingsystem_status.rb +++ b/app/models/foreman_wreckingball/operatingsystem_status.rb @@ -73,6 +73,11 @@ def os_matches_identifier? return false if vsphere_os.name && vsphere_os.name != host.operatingsystem.name return false if vsphere_os.major && ![vsphere_os.major].flatten.include?(host.operatingsystem.major.to_i) return false if vsphere_os.release && ![vsphere_os.release].flatten.include?(host.facts['os::release::full']) + + deduced_guest_id = host.deduced_vsphere_guest_id + + return false if deduced_guest_id && deduced_guest_id != guest_id + true end end diff --git a/test/factories/compute_resource.rb b/test/factories/compute_resource.rb index 0aa89ce..e77e265 100644 --- a/test/factories/compute_resource.rb +++ b/test/factories/compute_resource.rb @@ -4,7 +4,7 @@ factory :compute_resource do trait :with_vmware_clusters do transient do - vmware_clusters_count 1 + vmware_clusters_count { 1 } end after(:create) do |compute_resource, evaluator| diff --git a/test/factories/foreman_wreckingball_factories.rb b/test/factories/foreman_wreckingball_factories.rb index bf026f0..6f5cd01 100644 --- a/test/factories/foreman_wreckingball_factories.rb +++ b/test/factories/foreman_wreckingball_factories.rb @@ -3,129 +3,133 @@ FactoryBot.define do factory :vmware_facet, class: 'ForemanWreckingball::VmwareFacet' do vmware_cluster - tools_state 2 #:toolsOk - power_state 1 #:poweredOn - cpus 2 - corespersocket 1 - memory_mb 8192 - guest_id 'rhel6_64Guest' - cpu_hot_add false - hardware_version 'vmx-10' - cpu_features [ - 'cpuid.SSE3', - 'cpuid.PCLMULQDQ', - 'cpuid.SSSE3', - 'cpuid.CMPXCHG16B', - 'cpuid.PCID', - 'cpuid.SSE41', - 'cpuid.SSE42', - 'cpuid.POPCNT', - 'cpuid.AES', - 'cpuid.XSAVE', - 'cpuid.AVX', - 'cpuid.DS', - 'cpuid.SS', - 'cpuid.XCR0_MASTER_SSE', - 'cpuid.XCR0_MASTER_YMM_H', - 'cpuid.LAHF64', - 'cpuid.NX', - 'cpuid.RDTSCP', - 'cpuid.LM', - 'cpuid.Intel' - ] + tools_state { 2 } #:toolsOk + power_state { 1 } #:poweredOn + cpus { 2 } + corespersocket { 1 } + memory_mb { 8192 } + guest_id { 'rhel6_64Guest' } + cpu_hot_add { false } + hardware_version { 'vmx-10' } + cpu_features do + [ + 'cpuid.SSE3', + 'cpuid.PCLMULQDQ', + 'cpuid.SSSE3', + 'cpuid.CMPXCHG16B', + 'cpuid.PCID', + 'cpuid.SSE41', + 'cpuid.SSE42', + 'cpuid.POPCNT', + 'cpuid.AES', + 'cpuid.XSAVE', + 'cpuid.AVX', + 'cpuid.DS', + 'cpuid.SS', + 'cpuid.XCR0_MASTER_SSE', + 'cpuid.XCR0_MASTER_YMM_H', + 'cpuid.LAHF64', + 'cpuid.NX', + 'cpuid.RDTSCP', + 'cpuid.LM', + 'cpuid.Intel' + ] + end host end factory :vmware_hypervisor_facet, class: 'ForemanWreckingball::VmwareHypervisorFacet' do host vmware_cluster - cpu_cores 18 - cpu_sockets 1 - cpu_threads 36 - memory 412_046_372_864 + cpu_cores { 18 } + cpu_sockets { 1 } + cpu_threads { 36 } + memory { 412_046_372_864 } uuid { SecureRandom.uuid } - feature_capabilities [ - 'cpuid.3DNOW', - 'cpuid.3DNOWPLUS', - 'cpuid.3DNPREFETCH', - 'cpuid.ABM', - 'cpuid.ADX', - 'cpuid.AES', - 'cpuid.AMD', - 'cpuid.AVX', - 'cpuid.AVX2', - 'cpuid.BMI1', - 'cpuid.BMI2', - 'cpuid.CMPXCHG16B', - 'cpuid.CR8AVAIL', - 'cpuid.Cyrix', - 'cpuid.DS', - 'cpuid.ENFSTRG', - 'cpuid.EXTAPICSPC', - 'cpuid.F16C', - 'cpuid.FFXSR', - 'cpuid.FMA', - 'cpuid.FMA4', - 'cpuid.FSGSBASE', - 'cpuid.HLE', - 'cpuid.IBPB', - 'cpuid.IBRS', - 'cpuid.INVPCID', - 'cpuid.Intel', - 'cpuid.LAHF64', - 'cpuid.LM', - 'cpuid.MISALIGNED_SSE', - 'cpuid.MMXEXT', - 'cpuid.MOVBE', - 'cpuid.MWAIT', - 'cpuid.NX', - 'cpuid.PCID', - 'cpuid.PCLMULQDQ', - 'cpuid.PDPE1GB', - 'cpuid.POPCNT', - 'cpuid.PSN', - 'cpuid.RDRAND', - 'cpuid.RDSEED', - 'cpuid.RDTSCP', - 'cpuid.RTM', - 'cpuid.SMAP', - 'cpuid.SMEP', - 'cpuid.SS', - 'cpuid.SSE3', - 'cpuid.SSE41', - 'cpuid.SSE42', - 'cpuid.SSE4A', - 'cpuid.SSSE3', - 'cpuid.STIBP', - 'cpuid.SVM', - 'cpuid.SVM_DECODE_ASSISTS', - 'cpuid.SVM_FLUSH_BY_ASID', - 'cpuid.SVM_NPT', - 'cpuid.SVM_NRIP', - 'cpuid.SVM_VMCB_CLEAN', - 'cpuid.TBM', - 'cpuid.VIA', - 'cpuid.VMX', - 'cpuid.XCR0_MASTER_SSE', - 'cpuid.XCR0_MASTER_YMM_H', - 'cpuid.XOP', - 'cpuid.XSAVE', - 'cpuid.XSAVEOPT', - 'hv.capable', - 'misc.cpuidFaulting', - 'vpmc.fixctr.0', - 'vpmc.fixedWidth', - 'vpmc.genWidth', - 'vpmc.genctr.0', - 'vpmc.genctr.1', - 'vpmc.genctr.2', - 'vpmc.genctr.3', - 'vpmc.microarchitecture.ivybridge', - 'vpmc.numFixedCtrs', - 'vpmc.numGenCtrs', - 'vpmc.version', - 'vt.realmode' - ] + feature_capabilities do + [ + 'cpuid.3DNOW', + 'cpuid.3DNOWPLUS', + 'cpuid.3DNPREFETCH', + 'cpuid.ABM', + 'cpuid.ADX', + 'cpuid.AES', + 'cpuid.AMD', + 'cpuid.AVX', + 'cpuid.AVX2', + 'cpuid.BMI1', + 'cpuid.BMI2', + 'cpuid.CMPXCHG16B', + 'cpuid.CR8AVAIL', + 'cpuid.Cyrix', + 'cpuid.DS', + 'cpuid.ENFSTRG', + 'cpuid.EXTAPICSPC', + 'cpuid.F16C', + 'cpuid.FFXSR', + 'cpuid.FMA', + 'cpuid.FMA4', + 'cpuid.FSGSBASE', + 'cpuid.HLE', + 'cpuid.IBPB', + 'cpuid.IBRS', + 'cpuid.INVPCID', + 'cpuid.Intel', + 'cpuid.LAHF64', + 'cpuid.LM', + 'cpuid.MISALIGNED_SSE', + 'cpuid.MMXEXT', + 'cpuid.MOVBE', + 'cpuid.MWAIT', + 'cpuid.NX', + 'cpuid.PCID', + 'cpuid.PCLMULQDQ', + 'cpuid.PDPE1GB', + 'cpuid.POPCNT', + 'cpuid.PSN', + 'cpuid.RDRAND', + 'cpuid.RDSEED', + 'cpuid.RDTSCP', + 'cpuid.RTM', + 'cpuid.SMAP', + 'cpuid.SMEP', + 'cpuid.SS', + 'cpuid.SSE3', + 'cpuid.SSE41', + 'cpuid.SSE42', + 'cpuid.SSE4A', + 'cpuid.SSSE3', + 'cpuid.STIBP', + 'cpuid.SVM', + 'cpuid.SVM_DECODE_ASSISTS', + 'cpuid.SVM_FLUSH_BY_ASID', + 'cpuid.SVM_NPT', + 'cpuid.SVM_NRIP', + 'cpuid.SVM_VMCB_CLEAN', + 'cpuid.TBM', + 'cpuid.VIA', + 'cpuid.VMX', + 'cpuid.XCR0_MASTER_SSE', + 'cpuid.XCR0_MASTER_YMM_H', + 'cpuid.XOP', + 'cpuid.XSAVE', + 'cpuid.XSAVEOPT', + 'hv.capable', + 'misc.cpuidFaulting', + 'vpmc.fixctr.0', + 'vpmc.fixedWidth', + 'vpmc.genWidth', + 'vpmc.genctr.0', + 'vpmc.genctr.1', + 'vpmc.genctr.2', + 'vpmc.genctr.3', + 'vpmc.microarchitecture.ivybridge', + 'vpmc.numFixedCtrs', + 'vpmc.numGenCtrs', + 'vpmc.version', + 'vt.realmode' + ] + end end factory :vmware_cluster, class: 'ForemanWreckingball::VmwareCluster' do diff --git a/test/factories/task.rb b/test/factories/task.rb index f00249c..600481b 100644 --- a/test/factories/task.rb +++ b/test/factories/task.rb @@ -3,15 +3,15 @@ FactoryBot.modify do factory :some_task do trait :running do - state 'running' + state { 'running' } end trait :stopped do - state 'stopped' + state { 'stopped' } end trait :vmware_sync do - label ::Actions::ForemanWreckingball::Vmware::ScheduleVmwareSync.to_s + label { ::Actions::ForemanWreckingball::Vmware::ScheduleVmwareSync.to_s } end end end diff --git a/test/models/foreman_wreckingball/operatingsystem_status_test.rb b/test/models/foreman_wreckingball/operatingsystem_status_test.rb index 61358e2..1a9f324 100644 --- a/test/models/foreman_wreckingball/operatingsystem_status_test.rb +++ b/test/models/foreman_wreckingball/operatingsystem_status_test.rb @@ -92,6 +92,11 @@ class OperatingsystemStatusTest < ActiveSupport::TestCase test 'when architecture, osfamily, name and major match' do assert status.os_matches_identifier? end + + test 'when os identifier is other os but a proper os is set' do + host.vmware_facet.update(guest_id: 'otherGuest64') + refute status.os_matches_identifier? + end end end end diff --git a/test/models/host_test.rb b/test/models/host_test.rb index 516fd66..20fbcb0 100644 --- a/test/models/host_test.rb +++ b/test/models/host_test.rb @@ -70,4 +70,32 @@ class Host::ManagedTest < ActiveSupport::TestCase assert_includes hosts, @host end end + + describe '#deduced_vsphere_guest' do + context 'host with RHEL 6.1' do + let(:operatingsystem) do + FactoryBot.create( + :operatingsystem, + architectures: [architectures(:x86_64)], + major: 6, + minor: 1, + type: 'Redhat', + name: 'RedHat' + ) + end + let(:host) do + FactoryBot.create( + :host, + :managed, + :with_vmware_facet, + architecture: architectures(:x86_64), + operatingsystem: operatingsystem + ) + end + + it 'deduced the vsphere guest from the host architecture and os' do + assert_equal 'rhel6_64Guest', host.deduced_vsphere_guest&.id + end + end + end end