class ForemanMaintain::Executable

Attributes

options[R]

Public Class Methods

ensure_instance() click to toggle source
# File lib/foreman_maintain/executable.rb, line 195
def ensure_instance
  new
end
new(options = {}) click to toggle source
# File lib/foreman_maintain/executable.rb, line 12
def initialize(options = {})
  @options = options.inject({}) { |h, (k, v)| h.update(k.to_s => v) }
  @param_values = {}
  setup_params
  after_initialize
end

Public Instance Methods

__run__(execution) click to toggle source

internal method called by executor

# File lib/foreman_maintain/executable.rb, line 138
def __run__(execution)
  setup_execution_state(execution)
  unless skipped?
    if defined?(self.class.present?)
      run if self.class.present?
    else
      run
    end
  end
rescue Error::Skip => e
  set_skip(e.message)
rescue Error::Abort => e
  set_abort(e.message)
end
abort!(message = '') click to toggle source
# File lib/foreman_maintain/executable.rb, line 81
def abort!(message = '')
  raise Error::Abort, message
end
after_initialize() click to toggle source

public method to be overriden to perform after-initialization steps

# File lib/foreman_maintain/executable.rb, line 30
def after_initialize
end
associated_feature() click to toggle source
# File lib/foreman_maintain/executable.rb, line 49
def associated_feature
  return @associated_feature if defined? @associated_feature
  if metadata[:for_feature]
    @associated_feature = feature(metadata[:for_feature])
  end
end
ensure_instance() click to toggle source

method defined both on object and class to ensure we work always with object even when the definitions provide us only class

# File lib/foreman_maintain/executable.rb, line 155
def ensure_instance
  self
end
eql?(other) click to toggle source

To be able to call uniq on a set of steps to deduplicate the same steps inside the scenario

# File lib/foreman_maintain/executable.rb, line 21
def eql?(other)
  self.class.eql?(other.class) && options.eql?(other.options)
end
executed?() click to toggle source
# File lib/foreman_maintain/executable.rb, line 117
def executed?
  @_execution ? true : false
end
execution() click to toggle source
# File lib/foreman_maintain/executable.rb, line 121
def execution
  @_execution || raise('Trying to get execution information before the run started happened')
end
fail!(message) click to toggle source

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.

# File lib/foreman_maintain/executable.rb, line 67
def fail!(message)
  raise Error::Fail, message
end
hash() click to toggle source
# File lib/foreman_maintain/executable.rb, line 25
def hash
  [self.class, options].hash
end
inspect() click to toggle source
# File lib/foreman_maintain/executable.rb, line 187
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) click to toggle source
# File lib/foreman_maintain/executable.rb, line 176
def matches_hash?(hash)
  label == hash[:label] && @param_values == hash[:param_values]
end
necessary?() click to toggle source

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.

# File lib/foreman_maintain/executable.rb, line 128
def necessary?
  true
end
next_steps() click to toggle source

next steps to be offered to the user after the step is run It can be added for example from the assert method

# File lib/foreman_maintain/executable.rb, line 58
def next_steps
  @next_steps ||= []
end
run() click to toggle source

public method to be overriden

# File lib/foreman_maintain/executable.rb, line 113
def run
  raise NotImplementedError
end
say(message) click to toggle source

update reporter about the current message

# File lib/foreman_maintain/executable.rb, line 133
def say(message)
  execution.update(message)
end
set_abort(message) click to toggle source
# File lib/foreman_maintain/executable.rb, line 102
def set_abort(message)
  set_status(:abort, message)
end
set_fail(message) click to toggle source

rubocop:disable Naming/AccessorMethodName

# File lib/foreman_maintain/executable.rb, line 86
def set_fail(message)
  set_status(:fail, message)
end
set_info_warn(message) click to toggle source
# File lib/foreman_maintain/executable.rb, line 94
def set_info_warn(message)
  set_status(:info_warning, message)
end
set_param_variable(param_name, value) click to toggle source
# File lib/foreman_maintain/executable.rb, line 41
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) click to toggle source
# File lib/foreman_maintain/executable.rb, line 98
def set_skip(message)
  set_status(:skipped, message)
end
set_status(status, message) click to toggle source
# File lib/foreman_maintain/executable.rb, line 106
def set_status(status, message)
  execution.status = status
  execution.output << message if message && !message.empty?
end
set_warn(message) click to toggle source
# File lib/foreman_maintain/executable.rb, line 90
def set_warn(message)
  set_status(:warning, message)
end
setup_execution_state(execution) click to toggle source

clean the execution-specific state to prepare for the next execution attempts

# File lib/foreman_maintain/executable.rb, line 161
def setup_execution_state(execution)
  @_execution = execution
  @next_steps = []
end
setup_params() click to toggle source

processes the params from provided options

# File lib/foreman_maintain/executable.rb, line 34
def setup_params
  @options.validate_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 = '') click to toggle source
# File lib/foreman_maintain/executable.rb, line 77
def skip(message = '')
  raise Error::Skip, message
end
to_hash() click to toggle source

serialization methods

# File lib/foreman_maintain/executable.rb, line 167
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) click to toggle source
# File lib/foreman_maintain/executable.rb, line 180
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
warn!(message) click to toggle source

make the step a warning: this doesn't indicate the whole scenario should not continue, but the user will be warned before proceeding

# File lib/foreman_maintain/executable.rb, line 73
def warn!(message)
  raise Error::Warn, message
end