Class: ForemanMaintain::Runner

Inherits:
Object
  • Object
show all
Includes:
Concerns::Logger
Defined in:
lib/foreman_maintain/runner.rb,
lib/foreman_maintain/runner/execution.rb,
lib/foreman_maintain/runner/stored_execution.rb

Overview

Class responsible for running the scenario

Direct Known Subclasses

UpgradeRunner

Defined Under Namespace

Classes: Execution, StoredExecution

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Concerns::Logger

#logger

Constructor Details

#initialize(reporter, scenarios, options = {}) ⇒ Runner

Returns a new instance of Runner.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/foreman_maintain/runner.rb', line 9

def initialize(reporter, scenarios, options = {})
  options.validate_options!(:assumeyes, :whitelist, :force, :rescue_scenario)
  @assumeyes = options.fetch(:assumeyes, false)
  @whitelist = options.fetch(:whitelist, [])
  @force = options.fetch(:force, false)
  @rescue_scenario = options.fetch(:rescue_scenario, nil)
  @reporter = reporter
  @scenarios = Array(scenarios)
  @quit = false
  @last_scenario = nil
  @last_scenario_continuation_confirmed = false
  @exit_code = 0
  @procedure_step_counter = 0
end

Instance Attribute Details

#exit_codeObject (readonly)

Returns the value of attribute exit_code.



5
6
7
# File 'lib/foreman_maintain/runner.rb', line 5

def exit_code
  @exit_code
end

#reporterObject (readonly)

Returns the value of attribute reporter.



5
6
7
# File 'lib/foreman_maintain/runner.rb', line 5

def reporter
  @reporter
end

Instance Method Details

#add_steps(*steps) ⇒ Object



87
88
89
90
91
92
93
# File 'lib/foreman_maintain/runner.rb', line 87

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) ⇒ Object



82
83
84
85
# File 'lib/foreman_maintain/runner.rb', line 82

def ask_to_quit(exit_code = 1)
  @quit = true
  @exit_code = exit_code
end

#assumeyes?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/foreman_maintain/runner.rb', line 28

def assumeyes?
  @assumeyes
end

#confirm_scenario(scenario) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/foreman_maintain/runner.rb', line 66

def confirm_scenario(scenario)
  return if @last_scenario.nil? || @last_scenario_continuation_confirmed

  decision = if @last_scenario.steps_with_error(:whitelisted => false).any? ||
                @last_scenario.steps_with_abort(:whitelisted => false).any?
               :quit
             elsif @last_scenario.steps_with_warning(:whitelisted => false).any?
               @last_scenario_continuation_confirmed = true
               reporter.ask_decision("Continue with [#{scenario.description}]",
                                     run_strategy: scenario.run_strategy)
             end

  ask_to_quit if [:quit, :no].include?(decision)
  decision
end

#quit?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/foreman_maintain/runner.rb', line 24

def quit?
  @quit
end

#runObject



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/foreman_maintain/runner.rb', line 32

def run
  @scenarios.each do |scenario|
    run_scenario(scenario)
    next unless @quit

    if @rescue_scenario
      logger.debug('=== Rescue scenario found. Executing ===')
      execute_scenario_steps(@rescue_scenario, true)
    end
    break
  end
end

#run_scenario(scenario) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/foreman_maintain/runner.rb', line 45

def run_scenario(scenario)
  return if scenario.steps.empty?
  raise 'The runner is already in quit state' if quit?

  confirm_scenario(scenario)
  return if quit?

  execute_scenario_steps(scenario)
ensure
  unless scenario.steps.empty?
    @last_scenario = scenario
    @last_scenario_continuation_confirmed = false
  end
  @exit_code = 78 if scenario.warning?
  @exit_code = 1 if scenario.failed?
end

#storageObject



95
96
97
# File 'lib/foreman_maintain/runner.rb', line 95

def storage
  ForemanMaintain.storage(:default)
end

#whitelisted_step?(step) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/foreman_maintain/runner.rb', line 62

def whitelisted_step?(step)
  @whitelist.include?(step.label_dashed.to_s)
end