Class: ForemanMaintain::Executable

Inherits:
Object
  • Object
show all
Extended by:
Concerns::Finders, Forwardable
Defined in:
lib/foreman_maintain/executable.rb

Direct Known Subclasses

Check, Procedure

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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 = {})
  @options = options.inject({}) { |h, (k, v)| h.update(k.to_s => v) }
  @param_values = {}
  setup_params
  after_initialize
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

Class Method Details

.ensure_instanceObject



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.message)
rescue Error::Abort => e
  set_abort(e.message)
end

#abort!(message = '') ⇒ Object

Raises:



79
80
81
# File 'lib/foreman_maintain/executable.rb', line 79

def abort!(message = '')
  raise Error::Abort, message
end

#after_initializeObject

public method to be overriden to perform after-initialization steps



29
# File 'lib/foreman_maintain/executable.rb', line 29

def after_initialize; end

#associated_featureObject



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_instanceObject

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

Returns:

  • (Boolean)


20
21
22
# File 'lib/foreman_maintain/executable.rb', line 20

def eql?(other)
  self.class.eql?(other.class) && options.eql?(other.options)
end

#executed?Boolean

Returns:

  • (Boolean)


111
112
113
# File 'lib/foreman_maintain/executable.rb', line 111

def executed?
  @_execution ? true : false
end

#executionObject



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.

Raises:



65
66
67
# File 'lib/foreman_maintain/executable.rb', line 65

def fail!(message)
  raise Error::Fail, message
end

#hashObject



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

def hash
  [self.class, options].hash
end

#inspectObject



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

Returns:

  • (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.

Returns:

  • (Boolean)


126
127
128
# File 'lib/foreman_maintain/executable.rb', line 126

def necessary?
  true
end

#next_stepsObject

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

#runObject

public method to be overriden

Raises:

  • (NotImplementedError)


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(message)
  execution.update(message)
end

#set_abort(message) ⇒ Object



96
97
98
# File 'lib/foreman_maintain/executable.rb', line 96

def set_abort(message)
  set_status(:abort, message)
end

#set_fail(message) ⇒ Object

rubocop:disable Naming/AccessorMethodName



84
85
86
# File 'lib/foreman_maintain/executable.rb', line 84

def set_fail(message)
  set_status(:fail, message)
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(message)
  set_status(:skipped, message)
end

#set_status(status, message) ⇒ Object



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

def set_status(status, message)
  execution.status = status
  execution.output << message if message && !message.empty?
end

#set_warn(message) ⇒ Object



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

def set_warn(message)
  set_status(:warning, message)
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_paramsObject

processes the params from provided options



32
33
34
35
36
37
# File 'lib/foreman_maintain/executable.rb', line 32

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

Raises:



75
76
77
# File 'lib/foreman_maintain/executable.rb', line 75

def skip(message = '')
  raise Error::Skip, message
end

#to_hashObject

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

#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

Raises:



71
72
73
# File 'lib/foreman_maintain/executable.rb', line 71

def warn!(message)
  raise Error::Warn, message
end