Class: ForemanMaintain::Executable
- Inherits:
-
Object
- Object
- ForemanMaintain::Executable
- Extended by:
- Concerns::Finders, Forwardable
- Defined in:
- lib/foreman_maintain/executable.rb
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Class Method Summary collapse
Instance Method Summary collapse
-
#__run__(execution) ⇒ Object
internal method called by executor.
- #abort!(message = '') ⇒ Object
-
#after_initialize ⇒ Object
public method to be overriden to perform after-initialization steps.
- #associated_feature ⇒ Object
-
#ensure_instance ⇒ Object
method defined both on object and class to ensure we work always with object even when the definitions provide us only class.
-
#eql?(other) ⇒ Boolean
To be able to call uniq on a set of steps to deduplicate the same steps inside the scenario.
- #executed? ⇒ Boolean
- #execution ⇒ Object
-
#fail!(message) ⇒ Object
make the step to fail: the failure is considered significant and the next steps should not continue.
- #hash ⇒ Object
-
#initialize(options = {}) ⇒ Executable
constructor
A new instance of Executable.
- #inspect ⇒ Object
- #matches_hash?(hash) ⇒ Boolean
-
#necessary? ⇒ Boolean
public method to be overriden: it can perform additional checks to say, if the step is actually necessary to run.
-
#next_steps ⇒ Object
next steps to be offered to the user after the step is run It can be added for example from the assert method.
-
#run ⇒ Object
public method to be overriden.
-
#say(message) ⇒ Object
update reporter about the current message.
- #set_abort(message) ⇒ Object
-
#set_fail(message) ⇒ Object
rubocop:disable Naming/AccessorMethodName.
- #set_param_variable(param_name, value) ⇒ Object
- #set_skip(message) ⇒ Object
- #set_status(status, message) ⇒ Object
- #set_warn(message) ⇒ Object
-
#setup_execution_state(execution) ⇒ Object
clean the execution-specific state to prepare for the next execution attempts.
-
#setup_params ⇒ Object
processes the params from provided options.
- #skip(message = '') ⇒ Object
-
#to_hash ⇒ Object
serialization methods.
- #update_from_hash(hash) ⇒ Object
-
#warn!(message) ⇒ Object
make the step a warning: this doesn’t indicate the whole scenario should not continue, but the user will be warned before proceeding.
Methods included from Concerns::Finders
check, detector, feature, find_all_scenarios, find_checks, find_procedures, find_scenarios, procedure
Constructor Details
#initialize(options = {}) ⇒ Executable
Returns a new instance of Executable.
11 12 13 14 15 16 |
# File 'lib/foreman_maintain/executable.rb', line 11 def initialize( = {}) @options = .inject({}) { |h, (k, v)| h.update(k.to_s => v) } @param_values = {} setup_params after_initialize end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
5 6 7 |
# File 'lib/foreman_maintain/executable.rb', line 5 def @options end |
Class Method Details
.ensure_instance ⇒ Object
189 190 191 |
# File 'lib/foreman_maintain/executable.rb', line 189 def ensure_instance new end |
Instance Method Details
#__run__(execution) ⇒ Object
internal method called by executor
136 137 138 139 140 141 142 143 144 145 |
# File 'lib/foreman_maintain/executable.rb', line 136 def __run__(execution) setup_execution_state(execution) unless skipped? run end rescue Error::Skip => e set_skip(e.) rescue Error::Abort => e set_abort(e.) end |
#abort!(message = '') ⇒ Object
79 80 81 |
# File 'lib/foreman_maintain/executable.rb', line 79 def abort!( = '') raise Error::Abort, end |
#after_initialize ⇒ Object
public method to be overriden to perform after-initialization steps
29 |
# File 'lib/foreman_maintain/executable.rb', line 29 def after_initialize; end |
#associated_feature ⇒ Object
47 48 49 50 51 52 |
# File 'lib/foreman_maintain/executable.rb', line 47 def associated_feature return @associated_feature if defined? @associated_feature if [:for_feature] @associated_feature = feature([:for_feature]) end end |
#ensure_instance ⇒ Object
method defined both on object and class to ensure we work always with object even when the definitions provide us only class
149 150 151 |
# File 'lib/foreman_maintain/executable.rb', line 149 def ensure_instance self end |
#eql?(other) ⇒ Boolean
To be able to call uniq on a set of steps to deduplicate the same steps inside the scenario
20 21 22 |
# File 'lib/foreman_maintain/executable.rb', line 20 def eql?(other) self.class.eql?(other.class) && .eql?(other.) end |
#executed? ⇒ Boolean
111 112 113 |
# File 'lib/foreman_maintain/executable.rb', line 111 def executed? @_execution ? true : false end |
#execution ⇒ Object
115 116 117 118 119 120 121 |
# File 'lib/foreman_maintain/executable.rb', line 115 def execution if @_execution @_execution else raise 'Trying to get execution information before the run started happened' end end |
#fail!(message) ⇒ Object
make the step to fail: the failure is considered significant and the next steps should not continue. The specific behaviour depends on the scenario it’s being used on. In check-sets scenario, the next steps of the same scenario might continue, while the following scenarios would be aborted.
65 66 67 |
# File 'lib/foreman_maintain/executable.rb', line 65 def fail!() raise Error::Fail, end |
#hash ⇒ Object
24 25 26 |
# File 'lib/foreman_maintain/executable.rb', line 24 def hash [self.class, ].hash end |
#inspect ⇒ Object
181 182 183 184 185 186 |
# File 'lib/foreman_maintain/executable.rb', line 181 def inspect ret = "#{self.class.name} label:#{label}" ret << " params: #{@param_values.inspect}" unless @param_values.empty? ret << " status: #{execution.status}" if executed? ret end |
#matches_hash?(hash) ⇒ Boolean
170 171 172 |
# File 'lib/foreman_maintain/executable.rb', line 170 def matches_hash?(hash) label == hash[:label] && @param_values == hash[:param_values] end |
#necessary? ⇒ Boolean
public method to be overriden: it can perform additional checks to say, if the step is actually necessary to run. For example an ‘Packages::Install` procedure would not be necessary when the package is already installed.
126 127 128 |
# File 'lib/foreman_maintain/executable.rb', line 126 def necessary? true end |
#next_steps ⇒ Object
next steps to be offered to the user after the step is run It can be added for example from the assert method
56 57 58 |
# File 'lib/foreman_maintain/executable.rb', line 56 def next_steps @next_steps ||= [] end |
#run ⇒ Object
public method to be overriden
107 108 109 |
# File 'lib/foreman_maintain/executable.rb', line 107 def run raise NotImplementedError end |
#say(message) ⇒ Object
update reporter about the current message
131 132 133 |
# File 'lib/foreman_maintain/executable.rb', line 131 def say() execution.update() end |
#set_abort(message) ⇒ Object
96 97 98 |
# File 'lib/foreman_maintain/executable.rb', line 96 def set_abort() set_status(:abort, ) end |
#set_fail(message) ⇒ Object
rubocop:disable Naming/AccessorMethodName
84 85 86 |
# File 'lib/foreman_maintain/executable.rb', line 84 def set_fail() set_status(:fail, ) end |
#set_param_variable(param_name, value) ⇒ Object
39 40 41 42 43 44 45 |
# File 'lib/foreman_maintain/executable.rb', line 39 def set_param_variable(param_name, value) @param_values[param_name] = value if instance_variable_defined?("@#{param_name}") raise "Instance variable @#{param_name} already set" end instance_variable_set("@#{param_name}", value) end |
#set_skip(message) ⇒ Object
92 93 94 |
# File 'lib/foreman_maintain/executable.rb', line 92 def set_skip() set_status(:skipped, ) end |
#set_status(status, message) ⇒ Object
100 101 102 103 |
# File 'lib/foreman_maintain/executable.rb', line 100 def set_status(status, ) execution.status = status execution.output << if && !.empty? end |
#set_warn(message) ⇒ Object
88 89 90 |
# File 'lib/foreman_maintain/executable.rb', line 88 def set_warn() set_status(:warning, ) end |
#setup_execution_state(execution) ⇒ Object
clean the execution-specific state to prepare for the next execution attempts
155 156 157 158 |
# File 'lib/foreman_maintain/executable.rb', line 155 def setup_execution_state(execution) @_execution = execution @next_steps = [] end |
#setup_params ⇒ Object
processes the params from provided options
32 33 34 35 36 37 |
# File 'lib/foreman_maintain/executable.rb', line 32 def setup_params @options.(params.values.map(&:name).map(&:to_s)) params.each_value do |param| set_param_variable(param.name, param.process(@options[param.name.to_s])) end end |
#skip(message = '') ⇒ Object
75 76 77 |
# File 'lib/foreman_maintain/executable.rb', line 75 def skip( = '') raise Error::Skip, end |
#to_hash ⇒ Object
serialization methods
161 162 163 164 165 166 167 168 |
# File 'lib/foreman_maintain/executable.rb', line 161 def to_hash ret = { :label => label, :param_values => @param_values } if @_execution ret[:status] = @_execution.status ret[:output] = @_execution.output end ret end |
#update_from_hash(hash) ⇒ Object
174 175 176 177 178 179 |
# File 'lib/foreman_maintain/executable.rb', line 174 def update_from_hash(hash) raise "The step is not matching the hash #{hash.inspect}" unless matches_hash?(hash) raise "Can't update step that was already executed" if @_execution @_execution = Runner::StoredExecution.new(self, :status => hash[:status], :output => hash[:output]) end |