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
23
# 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
  @rescue = 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



93
94
95
96
97
98
99
# File 'lib/foreman_maintain/runner.rb', line 93

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



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

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

#assumeyes?Boolean

Returns:

  • (Boolean)


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

def assumeyes?
  @assumeyes
end

#confirm_scenario(scenario) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/foreman_maintain/runner.rb', line 72

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)


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

def quit?
  @quit
end

#rescue?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/foreman_maintain/runner.rb', line 33

def rescue?
  @rescue
end

#runObject



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/foreman_maintain/runner.rb', line 37

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

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

#run_scenario(scenario) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/foreman_maintain/runner.rb', line 51

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

  confirm_scenario(scenario)
  return if quit? && !rescue?

  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



101
102
103
# File 'lib/foreman_maintain/runner.rb', line 101

def storage
  ForemanMaintain.storage(:default)
end

#whitelisted_step?(step) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/foreman_maintain/runner.rb', line 68

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