Class: Toaster::Task

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/toaster/model/task.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attr_hash) ⇒ Task

Returns a new instance of Task.



23
24
25
26
27
28
# File 'lib/toaster/model/task.rb', line 23

def initialize(attr_hash)
  if !attr_hash[:uuid]
    attr_hash[:uuid] = Util.generate_short_uid()
  end
  super(attr_hash)
end

Instance Attribute Details

#resource_objObject

Returns the value of attribute resource_obj.



21
22
23
# File 'lib/toaster/model/task.rb', line 21

def resource_obj
  @resource_obj
end

Class Method Details

.find(criteria = {}) ⇒ Object



265
266
267
# File 'lib/toaster/model/task.rb', line 265

def self.find(criteria={})
  DB.find_activerecord(Task, criteria)
end

.load_from_chef_source(resource, action, sourcecode, sourcefile, sourceline) ⇒ Object



269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/toaster/model/task.rb', line 269

def self.load_from_chef_source(resource, action, sourcecode, sourcefile, sourceline)
  sourcecode.strip! if sourcecode.kind_of?(String)
  params = {
    :resource => resource.to_s,
    :action => action,
    :sourcefile => sourcefile,
    :sourceline => sourceline
  }
  task = find_by(params)
  if !task
    params[:sourcecode] = sourcecode
    task = Task.new(params)
  end
  task.resource_obj = resource
  return task
end

.properties_flat(states_array) ⇒ Object



224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/toaster/model/task.rb', line 224

def self.properties_flat(states_array)
  result = []
  states_array.each do |s|
    MarkupUtil.eliminate_inserted_map_entries!(s)
    s = SystemState.get_flat_attributes(s)
    s.each do |k,v|
      entry = [k,v]
      if !result.include?(entry)
        result << entry
      end
    end
  end
  return result
end

Instance Method Details

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


286
287
288
# File 'lib/toaster/model/task.rb', line 286

def eql?(other)
  return other.kind_of?(Task) && !uuid.nil? && uuid == other.uuid
end

#get_config_for_potential_state_changesObject

Returns a hash which maps identifier=>configurations, indicating which types of state changes this task, upon execution, is potentially going to perform. For instance, if the task starts/stops a system service, the identifier “ports” will be in the hash keys. If the task modifies some files, the key will contain the identifier “files”, and possibly a list of potential files that may be edited. This helps us to develop tailor-made state capturing tools (e.g., implemented as ohai plugins) for different types of tasks.



253
254
255
# File 'lib/toaster/model/task.rb', line 253

def get_config_for_potential_state_changes()
  return ResourceInspector.get_config_for_potential_state_changes(self)
end

#global_executions(criteria = {}) ⇒ Object

Return an array of TaskExecution instances for this task.



47
48
49
50
# File 'lib/toaster/model/task.rb', line 47

def global_executions(criteria={})
  criteria[:task] = self
  return TaskExecution.find(criteria)
end

#global_num_executionsObject



52
53
54
# File 'lib/toaster/model/task.rb', line 52

def global_num_executions()
  return global_executions().size
end

#global_num_state_prop_changes(global_executions = nil) ⇒ Object



82
83
84
# File 'lib/toaster/model/task.rb', line 82

def global_num_state_prop_changes(global_executions = nil)
  return global_state_prop_changes(global_executions).size
end

#global_num_successful_executionsObject



58
59
60
# File 'lib/toaster/model/task.rb', line 58

def global_num_successful_executions()
  return global_successful_executions().size
end

#global_post_states(global_execs = nil) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/toaster/model/task.rb', line 200

def global_post_states(global_execs = nil)
  result = []
  global_execs = global_executions() if !global_execs
  # set list of ignored properties
  ignore_props = automation ? automation.ignore_properties.to_a.dup : []
  ignore_props.concat(SystemState.read_ignore_properties())
  global_execs.each do |exe|
    state = exe.state_after
    # elimination of map entries is required for removing ignore props!
    MarkupUtil.eliminate_inserted_map_entries!(state)
    SystemState.remove_ignore_props!(state, ignore_props)
    result << state
  end
  return result
end

#global_post_states_flat(global_execs = nil) ⇒ Object



220
221
222
# File 'lib/toaster/model/task.rb', line 220

def global_post_states_flat(global_execs = nil)
  return Task.properties_flat(global_post_states(global_execs))
end

#global_post_states_reduced(state_transitions = nil, include_empty_states = true) ⇒ Object



180
181
182
# File 'lib/toaster/model/task.rb', line 180

def global_post_states_reduced(state_transitions = nil, include_empty_states = true)
  return global_states_reduced(state_transitions, include_empty_states, false, true)
end

#global_pre_states(global_execs = nil) ⇒ Object



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/toaster/model/task.rb', line 184

def global_pre_states(global_execs = nil)
  result = []
  global_execs = global_executions() if !global_execs
  # set list of ignored properties
  ignore_props = automation ? automation.ignore_properties.to_a.dup : []
  ignore_props.concat(SystemState.read_ignore_properties())
  global_execs.each do |exe|
    state = exe.state_before
    # elimination of map entries is required for removing ignore props!
    MarkupUtil.eliminate_inserted_map_entries!(state)
    SystemState.remove_ignore_props!(state, ignore_props) 
    result << state
  end
  return result
end

#global_pre_states_flat(global_execs = nil) ⇒ Object



216
217
218
# File 'lib/toaster/model/task.rb', line 216

def global_pre_states_flat(global_execs = nil)
  return Task.properties_flat(global_pre_states(global_execs))
end

#global_pre_states_reduced(state_transitions = nil, include_empty_states = true) ⇒ Object



177
178
179
# File 'lib/toaster/model/task.rb', line 177

def global_pre_states_reduced(state_transitions = nil, include_empty_states = true)
  return global_states_reduced(state_transitions, include_empty_states, true, false)
end

#global_state_prop_changes(global_execs = nil) ⇒ Object

Return an array of lists of StateChange. Length of the array is the number of global executions of this task. For each task execution, the array contains a list with all state changes caused by this execution.



78
79
80
81
# File 'lib/toaster/model/task.rb', line 78

def global_state_prop_changes(global_execs = nil)
  return StateChange.joins(:task_execution => :task).where(
    "task_executions.task_id" => self.id)
end

#global_state_prop_changes_map(global_execs = nil) ⇒ Object

Return a map StateChange -> Integer . This method maps each state property change to the number of occurrences, summed up for all executions of this task.



91
92
93
94
95
96
97
98
# File 'lib/toaster/model/task.rb', line 91

def global_state_prop_changes_map(global_execs = nil)
  result = {}
  global_state_prop_changes(global_execs).each do |c|
    result[c] = 0 if !result[c]
    result[c] += 1
  end
  return result
end

#global_state_transitions(global_execs = nil, insert_nil_prestate_prop_for_insertions = false) ⇒ Object

Return a set of StateTransition objects for this task. A StateTransition basically represents a triple (pre-state, parameters, post-state).

  • pre-state and post-state are maps of state property values prop_key=>value.

  • parameters is a map of input parameter values param_name=>value.

The result set contains all state transitions that were recorded in any of the executions of this task.

This method is the basis for construction of the state transition graph.



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/toaster/model/task.rb', line 111

def global_state_transitions(global_execs=nil, insert_nil_prestate_prop_for_insertions=false)
  result = Set.new
  global_execs = global_executions() if !global_execs
  global_execs.each do |exe|
    changes = exe.state_changes
    prestate = {}
    changes.each { |c|
      if c.is_delete?
        prestate[c.property] = c.value
      elsif c.is_modify?
        prestate[c.property] = c.old_value
      elsif c.is_insert?
        if insert_nil_prestate_prop_for_insertions
          prestate[c.property] = nil
        end
      end
    }
    poststate = {}
    changes.each { |c| 
      if c.is_delete?
        poststate[c.property] = nil
      elsif c.is_modify? || c.is_insert?
        poststate[c.property] = c.value
      end
    }
    params = exe.get_used_parameters()
    trans = StateTransition.new(prestate, params, poststate)
    result << trans
  end
  return result
end

#global_states_reduced(state_transitions = nil, include_empty_states = true, add_prestates = true, add_poststates = true) ⇒ Object

Return a set of state property mappings => value .

The result set contains all states which were observed either as pre-state or as post-state of any of the executions of this task. The states are reduced to their “relevant” parts, i.e., only those state properties are included which are inserted/deleted/modified by the task, AND only those state properties which are not in the list of ignored properties of this task’s automation.



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/toaster/model/task.rb', line 153

def global_states_reduced(state_transitions = nil, include_empty_states = true, 
      add_prestates = true, add_poststates = true)
  ignore_props = automation ? automation.ignore_properties.to_a.dup : []
  # add global ignore properties
  ignore_props.concat(SystemState.read_ignore_properties())
  puts "WARN: No automation found for task UUID #{@uuid}!" if !automation
  result = Set.new
  state_transitions = global_state_transitions() if !state_transitions
  state_transitions.each do |t|
    if add_prestates
      SystemState.remove_ignore_props!(t.pre_state, ignore_props)
      if (include_empty_states || !t.pre_state.empty?)
        result << t.pre_state
      end
    end
    if add_poststates
      SystemState.remove_ignore_props!(t.post_state, ignore_props)
      if (include_empty_states || !t.post_state.empty?)
        result << t.post_state
      end
    end
  end
  return result
end

#global_success_percentageObject



64
65
66
# File 'lib/toaster/model/task.rb', line 64

def global_success_percentage()
  return global_num_successful_executions().to_f / global_num_executions().to_f * 100.0
end

#global_success_rateObject



61
62
63
# File 'lib/toaster/model/task.rb', line 61

def global_success_rate()
  return global_num_successful_executions().to_f / global_num_executions().to_f
end

#global_successful_executionsObject



55
56
57
# File 'lib/toaster/model/task.rb', line 55

def global_successful_executions()
  return global_executions(:success => true)
end

#guess_potential_state_changesObject



257
258
259
# File 'lib/toaster/model/task.rb', line 257

def guess_potential_state_changes()
  return ResourceInspector.guess_potential_state_changes(self)
end

#initialize1(resource, action, sourcecode, uuid = nil) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/toaster/model/task.rb', line 30

def initialize1(resource, action, sourcecode, uuid = nil)
  @db_type = "automation_task"
  @resource = resource ? resource.to_s : nil
  @resource_obj = resource
  @action = action ? action.to_s : nil
  @uuid = uuid ? uuid : Util.generate_short_uid()
  @sourcecode = sourcecode
  @sourcehash = sourcecode ? Util.md5(sourcecode) : nil
  @sourcefile = nil
  @sourceline = 0
  @parameters = []
  @executions_cache = []
end

#nameObject



261
262
263
# File 'lib/toaster/model/task.rb', line 261

def name
  return "#{resource}::#{action}"
end

#set_executions(execs) ⇒ Object



68
69
70
# File 'lib/toaster/model/task.rb', line 68

def set_executions(execs)
  @executions_cache = execs
end

#toaster_testing_task?Boolean

Returns:

  • (Boolean)


239
240
241
# File 'lib/toaster/model/task.rb', line 239

def toaster_testing_task?()
  return sourcefile == "toaster/recipes/testing.rb"
end