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.



12
13
14
15
16
17
# 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

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



191
192
193
# File 'lib/foreman_maintain/executable.rb', line 191

def ensure_instance
  new
end

Instance Method Details

#__run__(execution) ⇒ Object

internal method called by executor



138
139
140
141
142
143
144
145
146
147
# File 'lib/foreman_maintain/executable.rb', line 138

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:



81
82
83
# File 'lib/foreman_maintain/executable.rb', line 81

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

#after_initializeObject

public method to be overriden to perform after-initialization steps



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

def after_initialize
end

#associated_featureObject



49
50
51
52
53
54
# File 'lib/foreman_maintain/executable.rb', line 49

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



151
152
153
# File 'lib/foreman_maintain/executable.rb', line 151

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)


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

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

#executed?Boolean

Returns:

  • (Boolean)


117
118
119
# File 'lib/foreman_maintain/executable.rb', line 117

def executed?
  @_execution ? true : false
end

#executionObject



121
122
123
# 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) ⇒ 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:



67
68
69
# File 'lib/foreman_maintain/executable.rb', line 67

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

#hashObject



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

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

#inspectObject



183
184
185
186
187
188
# File 'lib/foreman_maintain/executable.rb', line 183

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)


172
173
174
# File 'lib/foreman_maintain/executable.rb', line 172

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)


128
129
130
# File 'lib/foreman_maintain/executable.rb', line 128

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



58
59
60
# File 'lib/foreman_maintain/executable.rb', line 58

def next_steps
  @next_steps ||= []
end

#runObject

public method to be overriden

Raises:

  • (NotImplementedError)


113
114
115
# File 'lib/foreman_maintain/executable.rb', line 113

def run
  raise NotImplementedError
end

#say(message) ⇒ Object

update reporter about the current message



133
134
135
# File 'lib/foreman_maintain/executable.rb', line 133

def say(message)
  execution.update(message)
end

#set_abort(message) ⇒ Object



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

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

#set_fail(message) ⇒ Object

rubocop:disable Naming/AccessorMethodName



86
87
88
# File 'lib/foreman_maintain/executable.rb', line 86

def set_fail(message)
  set_status(:fail, message)
end

#set_info_warn(message) ⇒ Object



94
95
96
# 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) ⇒ Object



41
42
43
44
45
46
47
# 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) ⇒ Object



98
99
100
# File 'lib/foreman_maintain/executable.rb', line 98

def set_skip(message)
  set_status(:skipped, message)
end

#set_status(status, message) ⇒ Object



106
107
108
109
# 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) ⇒ Object



90
91
92
# File 'lib/foreman_maintain/executable.rb', line 90

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



157
158
159
160
# File 'lib/foreman_maintain/executable.rb', line 157

def setup_execution_state(execution)
  @_execution = execution
  @next_steps = []
end

#setup_paramsObject

processes the params from provided options



34
35
36
37
38
39
# 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 = '') ⇒ Object

Raises:



77
78
79
# File 'lib/foreman_maintain/executable.rb', line 77

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

#to_hashObject

serialization methods



163
164
165
166
167
168
169
170
# File 'lib/foreman_maintain/executable.rb', line 163

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



176
177
178
179
180
181
# File 'lib/foreman_maintain/executable.rb', line 176

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:



73
74
75
# File 'lib/foreman_maintain/executable.rb', line 73

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