Skip to content

Commit

Permalink
Add statuses to the host statuses page
Browse files Browse the repository at this point in the history
  • Loading branch information
kamils-iRonin committed Jan 25, 2021
1 parent 914f609 commit 6f53502
Show file tree
Hide file tree
Showing 9 changed files with 199 additions and 43 deletions.
34 changes: 28 additions & 6 deletions app/models/foreman_wreckingball/cpu_hot_add_status.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ class CpuHotAddStatus < ::HostStatus::Status
OK = 0
PERFORMANCE_DEGRATION = 1

LABELS = {
OK => N_('No Impact'),
PERFORMANCE_DEGRATION => N_('Possible performance degration')
}.freeze

def self.status_name
N_('CPU Hot Plug')
end
Expand All @@ -21,6 +26,28 @@ def self.supports_remediate?
false
end

def self.stats
data = group(:status).count
owned_data = where(host_id: Host::Managed.search_for('owner = current_user').select(:id)).group(:status).count

[
{
label: LABELS[OK],
hosts_count: data.fetch(OK, 0),
owned_hosts_count: owned_data.fetch(OK, 0),
search: 'host_status = ForemanWreckingball::CpuHotAddStatus::OK',
global_status: HostStatus::Global::OK
},
{
label: LABELS[PERFORMANCE_DEGRATION],
hosts_count: data.fetch(PERFORMANCE_DEGRATION, 0),
owned_hosts_count: owned_data.fetch(PERFORMANCE_DEGRATION, 0),
search: 'host_status = ForemanWreckingball::CpuHotAddStatus::PERFORMANCE_DEGRATION',
global_status: HostStatus::Global::ERROR
}
]
end

def to_status(_options = {})
performance_degration? ? PERFORMANCE_DEGRATION : OK
end
Expand All @@ -43,12 +70,7 @@ def self.global_ok_list
end

def to_label(_options = {})
case status
when PERFORMANCE_DEGRATION
N_('Possible performance degration')
else
N_('No Impact')
end
LABELS.fetch(status, LABELS[OK])
end

def relevant?(_options = {})
Expand Down
34 changes: 28 additions & 6 deletions app/models/foreman_wreckingball/hardware_version_status.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ class HardwareVersionStatus < ::HostStatus::Status
OK = 0
OUTOFDATE = 1

LABELS = {
OK => N_('OK'),
OUTOFDATE => N_('Out of date')
}.freeze

def self.status_name
N_('vSphere Hardware Version')
end
Expand All @@ -29,6 +34,28 @@ def self.remediate_action
::Actions::ForemanWreckingball::Host::RemediateHardwareVersion
end

def self.stats
data = group(:status).count
owned_data = where(host_id: Host::Managed.search_for('owner = current_user').select(:id)).group(:status).count

[
{
label: LABELS[OK],
hosts_count: data.fetch(OK, 0),
owned_hosts_count: owned_data.fetch(OK, 0),
search: 'host_status = ForemanWreckingball::HardwareVersionStatus::OK',
global_status: HostStatus::Global::OK
},
{
label: LABELS[OUTOFDATE],
hosts_count: data.fetch(OUTOFDATE, 0),
owned_hosts_count: owned_data.fetch(OUTOFDATE, 0),
search: 'host_status = ForemanWreckingball::HardwareVersionStatus::OUTOFDATE',
global_status: HostStatus::Global::WARN
}
]
end

def to_status(_options = {})
recent_hw_version? ? OK : OUTOFDATE
end
Expand All @@ -51,12 +78,7 @@ def self.global_ok_list
end

def to_label(_options = {})
case status
when OUTOFDATE
N_('Out of date')
else
N_('OK')
end
LABELS.fetch(status, LABELS[OK])
end

def relevant?(_options = {})
Expand Down
34 changes: 28 additions & 6 deletions app/models/foreman_wreckingball/operatingsystem_status.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ class OperatingsystemStatus < ::HostStatus::Status
OK = 0
MISMATCH = 1

LABELS = {
OK => N_('OK'),
MISMATCH => N_('VM OS is incorrect')
}.freeze

def self.status_name
N_('VM Operatingsystem')
end
Expand All @@ -29,6 +34,28 @@ def self.remediate_action
::Actions::ForemanWreckingball::Host::RemediateVmwareOperatingsystem
end

def self.stats
data = group(:status).count
owned_data = where(host_id: Host::Managed.search_for('owner = current_user').select(:id)).group(:status).count

[
{
label: LABELS[OK],
hosts_count: data.fetch(OK, 0),
owned_hosts_count: owned_data.fetch(OK, 0),
search: 'host_status = ForemanWreckingball::OperatingsystemStatus::OK',
global_status: HostStatus::Global::OK
},
{
label: LABELS[MISMATCH],
hosts_count: data.fetch(MISMATCH, 0),
owned_hosts_count: owned_data.fetch(MISMATCH, 0),
search: 'host_status = ForemanWreckingball::OperatingsystemStatus::MISMATCH',
global_status: HostStatus::Global::WARN
}
]
end

def to_status(_options = {})
os_matches_identifier? ? OK : MISMATCH
end
Expand All @@ -51,12 +78,7 @@ def self.global_ok_list
end

def to_label(_options = {})
case status
when MISMATCH
N_('VM OS is incorrect')
else
N_('OK')
end
LABELS.fetch(status, LABELS[OK])
end

def relevant?(_options = {})
Expand Down
34 changes: 28 additions & 6 deletions app/models/foreman_wreckingball/spectre_v2_status.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ class SpectreV2Status < ::HostStatus::Status
ENABLED = 0
MISSING = 1

LABELS = {
ENABLED => N_('Guest Mitigation Enabled'),
MISSING => N_('Guest Mitigation Missing')
}.freeze

def self.status_name
N_('Spectre v2 Guest Mitigation Enabled')
end
Expand All @@ -29,6 +34,28 @@ def self.remediate_action
::Actions::ForemanWreckingball::Host::RemediateSpectreV2
end

def self.stats
data = group(:status).count
owned_data = where(host_id: Host::Managed.search_for('owner = current_user').select(:id)).group(:status).count

[
{
label: LABELS[ENABLED],
hosts_count: data.fetch(ENABLED, 0),
owned_hosts_count: owned_data.fetch(ENABLED, 0),
search: 'host_status = ForemanWreckingball::SpectreV2Status::ENABLED',
global_status: HostStatus::Global::OK
},
{
label: LABELS[MISSING],
hosts_count: data.fetch(MISSING, 0),
owned_hosts_count: owned_data.fetch(MISSING, 0),
search: 'host_status = ForemanWreckingball::SpectreV2Status::MISSING',
global_status: HostStatus::Global::ERROR
}
]
end

def to_status(_options = {})
guest_mitigation_enabled? ? ENABLED : MISSING
end
Expand All @@ -51,12 +78,7 @@ def self.global_ok_list
end

def to_label(_options = {})
case status
when MISSING
N_('Guest Mitigation Missing')
else
N_('Guest Mitigation Enabled')
end
LABELS.fetch(status, LABELS[ENABLED])
end

def relevant?(_options = {})
Expand Down
65 changes: 60 additions & 5 deletions app/models/foreman_wreckingball/tools_status.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,20 @@

module ForemanWreckingball
class ToolsStatus < ::HostStatus::Status
TOOLS_NOT_INSTALLED = VmwareFacet.tools_states[:toolsNotInstalled]
TOOLS_NOT_RUNNING = VmwareFacet.tools_states[:toolsNotRunning]
TOOLS_OK = VmwareFacet.tools_states[:toolsOk]
TOOLS_OLD = VmwareFacet.tools_states[:toolsOld]
POWERDOWN = 10

LABELS = {
TOOLS_NOT_INSTALLED => N_('Not installed'),
TOOLS_NOT_RUNNING => N_('Not running'),
TOOLS_OK => N_('OK'),
TOOLS_OLD => N_('Out of date'),
POWERDOWN => N_('Powered down')
}.freeze

def self.status_name
N_('VMware Tools')
end
Expand All @@ -20,8 +32,52 @@ def self.supports_remediate?
false
end

def self.stats
data = group(:status).count
owned_data = where(host_id: Host::Managed.search_for('owner = current_user').select(:id)).group(:status).count

[
{
label: LABELS[TOOLS_NOT_INSTALLED],
hosts_count: data.fetch(TOOLS_NOT_INSTALLED, 0),
owned_hosts_count: owned_data.fetch(TOOLS_NOT_INSTALLED, 0),
search: 'host_status = ForemanWreckingball::ToolsStatus::TOOLS_NOT_INSTALLED',
global_status: HostStatus::Global::ERROR
},
{
label: LABELS[TOOLS_NOT_RUNNING],
hosts_count: data.fetch(TOOLS_NOT_RUNNING, 0),
owned_hosts_count: owned_data.fetch(TOOLS_NOT_RUNNING, 0),
search: 'host_status = ForemanWreckingball::ToolsStatus::TOOLS_NOT_RUNNING',
global_status: HostStatus::Global::ERROR
},
{
label: LABELS[TOOLS_OK],
hosts_count: data.fetch(TOOLS_OK, 0),
owned_hosts_count: owned_data.fetch(TOOLS_OK, 0),
search: 'host_status = ForemanWreckingball::ToolsStatus::TOOLS_OK',
global_status: HostStatus::Global::OK
},
{
label: LABELS[TOOLS_OLD],
hosts_count: data.fetch(TOOLS_OLD, 0),
owned_hosts_count: owned_data.fetch(TOOLS_OLD, 0),
search: 'host_status = ForemanWreckingball::ToolsStatus::TOOLS_OLD',
global_status: HostStatus::Global::WARN
},
{
label: LABELS[POWERDOWN],
hosts_count: data.fetch(POWERDOWN, 0),
owned_hosts_count: owned_data.fetch(POWERDOWN, 0),
search: 'host_status = ForemanWreckingball::ToolsStatus::POWERDOWN',
global_status: HostStatus::Global::OK
}
]
end

def to_status(_options = {})
return POWERDOWN unless host.supports_power? && host.vmware_facet.vm_ready?

VmwareFacet.tools_states[host.vmware_facet.tools_state]
end

Expand All @@ -31,22 +87,21 @@ def to_global(_options = {})

def self.to_global(status)
case status
when VmwareFacet.tools_states[:toolsOk], POWERDOWN
when TOOLS_OK, POWERDOWN
HostStatus::Global::OK
when VmwareFacet.tools_states[:toolsOld]
when TOOLS_OLD
HostStatus::Global::WARN
else
HostStatus::Global::ERROR
end
end

def self.global_ok_list
[VmwareFacet.tools_states[:toolsOk], POWERDOWN]
[TOOLS_OK, POWERDOWN]
end

def to_label(_options = {})
return N_('Powered down') if status == POWERDOWN
host.vmware_facet.tools_state_label
LABELS.fetch(status, N_('Unknown status'))
end

def relevant?(_options = {})
Expand Down
13 changes: 0 additions & 13 deletions app/models/foreman_wreckingball/vmware_facet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,6 @@ class VmwareFacet < ApplicationRecord

serialize :cpu_features, JSON

def tools_state_label
case tools_state.to_sym
when :toolsNotInstalled
N_('Not installed')
when :toolsNotRunning
N_('Not running')
when :toolsOk
N_('OK')
when :toolsOld
N_('Out of date')
end
end

def refresh!
vm = host.compute_object
return unless vm
Expand Down
10 changes: 10 additions & 0 deletions lib/foreman_wreckingball/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ class Engine < ::Rails::Engine
Foreman::Plugin.register :foreman_wreckingball do
requires_foreman '>= 1.21'

automatic_assets(false)
precompile_assets([
'foreman_wreckingball/modal.js',
'foreman_wreckingball/status_hosts_table.js',
'foreman_wreckingball/status_managed_hosts_dashboard.js',
'foreman_wreckingball/status_row.js',
'foreman_wreckingball/status_hosts_table.css',
'foreman_wreckingball/status_managed_hosts_dashboard.css',
])

security_block :foreman_wreckingball do
permission :refresh_vmware_status_hosts, {
:'foreman_wreckingball/hosts' => [:refresh_status_dashboard]
Expand Down
2 changes: 1 addition & 1 deletion test/helpers/foreman_wreckingball/status_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module ForemanWreckingball
module StatusHelper
def assert_statuses(expected)
actual = request.env['action_controller.instance'].instance_variable_get('@statuses')
assert_equal expected, actual
assert_same_elements expected, actual
end
end
end
16 changes: 16 additions & 0 deletions test/models/foreman_wreckingball/tools_status_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,24 @@ class ToolsStatusTest < ActiveSupport::TestCase
end

test 'shows ok message' do
status.status = ForemanWreckingball::ToolsStatus::TOOLS_OK
assert_equal 'OK', status.to_label
end

test 'shows not installed message' do
status.status = ForemanWreckingball::ToolsStatus::TOOLS_NOT_INSTALLED
assert_equal 'Not installed', status.to_label
end

test 'shows not running message' do
status.status = ForemanWreckingball::ToolsStatus::TOOLS_NOT_RUNNING
assert_equal 'Not running', status.to_label
end

test 'shows out of date message' do
status.status = ForemanWreckingball::ToolsStatus::TOOLS_OLD
assert_equal 'Out of date', status.to_label
end
end
end
end

0 comments on commit 6f53502

Please sign in to comment.