class ForemanMaintain::Runner

Class responsible for running the scenario

Attributes

exit_code[R]
reporter[R]

Public Class Methods

new(reporter, scenarios, options = {}) click to toggle source
# File lib/foreman_maintain/runner.rb, line 8
def initialize(reporter, scenarios, options = {})
  options.validate_options!(:assumeyes, :whitelist, :force)
  @assumeyes = options.fetch(:assumeyes, false)
  @whitelist = options.fetch(:whitelist, [])
  @force = options.fetch(:force, false)
  @reporter = reporter
  @scenarios = Array(scenarios)
  @quit = false
  @last_scenario = nil
  @exit_code = 0
end

Public Instance Methods

add_steps(*steps) click to toggle source
# File lib/foreman_maintain/runner.rb, line 73
def add_steps(*steps)
  # we we add the steps at the beginning, but still keeping the
  # order of steps passed in the arguments
  steps.reverse.each do |step|
    @steps_to_run.unshift(step)
  end
end
ask_to_quit(exit_code = 1) click to toggle source
# File lib/foreman_maintain/runner.rb, line 68
def ask_to_quit(exit_code = 1)
  @quit = true
  @exit_code = exit_code
end
assumeyes?() click to toggle source
# File lib/foreman_maintain/runner.rb, line 24
def assumeyes?
  @assumeyes
end
confirm_scenario(scenario) click to toggle source
# File lib/foreman_maintain/runner.rb, line 56
def confirm_scenario(scenario)
  return unless @last_scenario
  decision = if @last_scenario.steps_with_error(:whitelisted => false).any?
               :quit
             elsif @last_scenario.steps_with_warning(:whitelisted => false).any?
               reporter.ask_decision("Continue with [#{scenario.description}]")
             end

  ask_to_quit if [:quit, :no].include?(decision)
  decision
end
quit?() click to toggle source
# File lib/foreman_maintain/runner.rb, line 20
def quit?
  @quit
end
run() click to toggle source
# File lib/foreman_maintain/runner.rb, line 28
def run
  @scenarios.each do |scenario|
    run_scenario(scenario)
    break if @quit
  end
end
run_scenario(scenario, confirm = true) click to toggle source

rubocop:disable Metrics/CyclomaticComplexity

# File lib/foreman_maintain/runner.rb, line 36
def run_scenario(scenario, confirm = true)
  return if scenario.steps.empty?
  raise 'The runner is already in quit state' if quit?

  if confirm
    confirm_scenario(scenario)
    return if quit?
  end

  execute_scenario_steps(scenario)
ensure
  @last_scenario = scenario unless scenario.steps.empty?
  @exit_code = 1 if scenario.failed?
end
storage() click to toggle source
# File lib/foreman_maintain/runner.rb, line 81
def storage
  ForemanMaintain.storage(:default)
end
whitelisted_step?(step) click to toggle source

rubocop:enable Metrics/CyclomaticComplexity

# File lib/foreman_maintain/runner.rb, line 52
def whitelisted_step?(step)
  @whitelist.include?(step.label_dashed.to_s)
end

Private Instance Methods

ask_about_offered_steps(step) click to toggle source

rubocop:disable Metrics/MethodLength, Metrics/AbcSize, Metrics/CyclomaticComplexity

# File lib/foreman_maintain/runner.rb, line 126
def ask_about_offered_steps(step)
  if assumeyes? && rerun_check?(step)
    @reporter.puts 'Check still failing after attempt to fix. Skipping'
    return :no
  end
  if step.next_steps && !step.next_steps.empty?
    @last_decision_step = step
    steps = step.next_steps.map(&:ensure_instance)
    decision = @reporter.on_next_steps(steps)
    case decision
    when :quit
      ask_to_quit
    when Executable
      chosen_steps = [decision]
      chosen_steps << step if step.is_a?(Check)
      add_steps(*chosen_steps)
      :yes
    end
  end
end
execute_scenario_steps(scenario) click to toggle source
# File lib/foreman_maintain/runner.rb, line 87
def execute_scenario_steps(scenario)
  scenario.before_scenarios.flatten.each { |before_scenario| run_scenario(before_scenario) }
  return if quit? # the before scenarios caused the stop of the execution
  @reporter.before_scenario_starts(scenario)
  run_steps(scenario, scenario.steps)
  @reporter.after_scenario_finishes(scenario)
end
post_step_decisions(scenario, execution) click to toggle source
# File lib/foreman_maintain/runner.rb, line 115
def post_step_decisions(scenario, execution)
  step = execution.step
  next_steps_decision = ask_about_offered_steps(step)
  if next_steps_decision != :yes &&
     execution.fail? && !execution.whitelisted? &&
     scenario.run_strategy == :fail_fast
    ask_to_quit
  end
end
rerun_check?(step) click to toggle source

rubocop:enable Metrics/MethodLength, Metrics/AbcSize, Metrics/CyclomaticComplexity

# File lib/foreman_maintain/runner.rb, line 148
def rerun_check?(step)
  @last_decision_step == step
end
run_step(step) click to toggle source
# File lib/foreman_maintain/runner.rb, line 103
def run_step(step)
  @reporter.puts('Rerunning the check after fix procedure') if rerun_check?(step)
  execution = Execution.new(step, @reporter,
                            :whitelisted => whitelisted_step?(step),
                            :storage => storage,
                            :force => @force)
  execution.run
  execution
ensure
  storage.save
end
run_steps(scenario, steps) click to toggle source
# File lib/foreman_maintain/runner.rb, line 95
def run_steps(scenario, steps)
  @steps_to_run = ForemanMaintain::DependencyGraph.sort(steps)
  while !@quit && !@steps_to_run.empty?
    execution = run_step(@steps_to_run.shift)
    post_step_decisions(scenario, execution)
  end
end