2018-10-08 10:50:39 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-02-12 18:33:31 -05:00
|
|
|
module SystemCheck
|
2017-02-13 03:45:39 -05:00
|
|
|
# Base class for Checks. You must inherit from here
|
|
|
|
# and implement the methods below when necessary
|
2017-02-12 18:33:31 -05:00
|
|
|
class BaseCheck
|
2017-05-25 19:46:54 -04:00
|
|
|
include ::SystemCheck::Helpers
|
2017-05-25 10:16:25 -04:00
|
|
|
|
|
|
|
# Define a custom term for when check passed
|
|
|
|
#
|
|
|
|
# @param [String] term used when check passed (default: 'yes')
|
|
|
|
def self.set_check_pass(term)
|
|
|
|
@check_pass = term
|
|
|
|
end
|
|
|
|
|
|
|
|
# Define a custom term for when check failed
|
|
|
|
#
|
|
|
|
# @param [String] term used when check failed (default: 'no')
|
|
|
|
def self.set_check_fail(term)
|
|
|
|
@check_fail = term
|
|
|
|
end
|
|
|
|
|
|
|
|
# Define the name of the SystemCheck that will be displayed during execution
|
|
|
|
#
|
|
|
|
# @param [String] name of the check
|
|
|
|
def self.set_name(name)
|
|
|
|
@name = name
|
|
|
|
end
|
|
|
|
|
|
|
|
# Define the reason why we skipped the SystemCheck
|
|
|
|
#
|
|
|
|
# This is only used if subclass implements `#skip?`
|
|
|
|
#
|
|
|
|
# @param [String] reason to be displayed
|
|
|
|
def self.set_skip_reason(reason)
|
|
|
|
@skip_reason = reason
|
|
|
|
end
|
|
|
|
|
|
|
|
# Term to be displayed when check passed
|
|
|
|
#
|
|
|
|
# @return [String] term when check passed ('yes' if not re-defined in a subclass)
|
|
|
|
def self.check_pass
|
|
|
|
call_or_return(@check_pass) || 'yes'
|
|
|
|
end
|
|
|
|
|
|
|
|
## Term to be displayed when check failed
|
|
|
|
#
|
|
|
|
# @return [String] term when check failed ('no' if not re-defined in a subclass)
|
|
|
|
def self.check_fail
|
|
|
|
call_or_return(@check_fail) || 'no'
|
|
|
|
end
|
|
|
|
|
|
|
|
# Name of the SystemCheck defined by the subclass
|
|
|
|
#
|
|
|
|
# @return [String] the name
|
|
|
|
def self.display_name
|
|
|
|
call_or_return(@name) || self.name
|
|
|
|
end
|
|
|
|
|
|
|
|
# Skip reason defined by the subclass
|
|
|
|
#
|
|
|
|
# @return [String] the reason
|
|
|
|
def self.skip_reason
|
|
|
|
call_or_return(@skip_reason) || 'skipped'
|
|
|
|
end
|
|
|
|
|
2022-04-06 14:08:19 -04:00
|
|
|
# Define or get a reason why we skipped the SystemCheck (during runtime)
|
2017-09-03 21:58:54 -04:00
|
|
|
#
|
|
|
|
# This is used when you need dynamic evaluation like when you have
|
|
|
|
# multiple reasons why a check can fail
|
|
|
|
#
|
|
|
|
# @param [String] reason to be displayed
|
2022-04-06 14:08:19 -04:00
|
|
|
# @return [String] reason to be displayed
|
|
|
|
attr_accessor :skip_reason
|
2017-09-03 21:58:54 -04:00
|
|
|
|
2017-05-25 10:16:25 -04:00
|
|
|
# Does the check support automatically repair routine?
|
|
|
|
#
|
|
|
|
# @return [Boolean] whether check implemented `#repair!` method or not
|
|
|
|
def can_repair?
|
|
|
|
self.class.instance_methods(false).include?(:repair!)
|
|
|
|
end
|
|
|
|
|
|
|
|
def can_skip?
|
|
|
|
self.class.instance_methods(false).include?(:skip?)
|
|
|
|
end
|
|
|
|
|
2017-08-24 13:01:22 -04:00
|
|
|
def multi_check?
|
2017-05-25 10:16:25 -04:00
|
|
|
self.class.instance_methods(false).include?(:multi_check)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Execute the check routine
|
|
|
|
#
|
2017-02-13 03:45:39 -05:00
|
|
|
# This is where you should implement the main logic that will return
|
|
|
|
# a boolean at the end
|
|
|
|
#
|
|
|
|
# You should not print any output to STDOUT here, use the specific methods instead
|
|
|
|
#
|
2017-05-25 10:16:25 -04:00
|
|
|
# @return [Boolean] whether check passed or failed
|
2017-02-12 18:33:31 -05:00
|
|
|
def check?
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
2017-05-25 10:16:25 -04:00
|
|
|
# Execute a custom check that cover multiple unities
|
|
|
|
#
|
|
|
|
# When using multi_check you have to provide the output yourself
|
|
|
|
def multi_check
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
|
|
|
# Prints troubleshooting instructions
|
|
|
|
#
|
2017-02-13 03:45:39 -05:00
|
|
|
# This is where you should print detailed information for any error found during #check?
|
|
|
|
#
|
|
|
|
# You may use helper methods to help format the output:
|
|
|
|
#
|
|
|
|
# @see #try_fixing_it
|
|
|
|
# @see #fix_and_rerun
|
2019-05-20 10:11:44 -04:00
|
|
|
# @see #for_more_information
|
2017-02-12 18:33:31 -05:00
|
|
|
def show_error
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
2017-05-25 10:16:25 -04:00
|
|
|
# When implemented by a subclass, will attempt to fix the issue automatically
|
|
|
|
def repair!
|
|
|
|
raise NotImplementedError
|
2017-02-12 18:33:31 -05:00
|
|
|
end
|
|
|
|
|
2017-05-25 10:16:25 -04:00
|
|
|
# When implemented by a subclass, will evaluate whether check should be skipped or not
|
2017-02-13 03:45:39 -05:00
|
|
|
#
|
2017-05-25 10:16:25 -04:00
|
|
|
# @return [Boolean] whether or not this check should be skipped
|
|
|
|
def skip?
|
|
|
|
raise NotImplementedError
|
2017-02-12 18:33:31 -05:00
|
|
|
end
|
|
|
|
|
2017-05-25 10:16:25 -04:00
|
|
|
def self.call_or_return(input)
|
|
|
|
input.respond_to?(:call) ? input.call : input
|
2017-02-12 18:33:31 -05:00
|
|
|
end
|
2017-05-25 10:16:25 -04:00
|
|
|
private_class_method :call_or_return
|
2017-02-12 18:33:31 -05:00
|
|
|
end
|
|
|
|
end
|