Class: Toaster::Automation

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attr_hash) ⇒ Automation

Returns a new instance of Automation.



31
32
33
34
35
36
# File 'lib/toaster/model/automation.rb', line 31

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

Instance Attribute Details

#additional_state_configsObject

attr_accessor :tasks, :attributes,



29
30
31
# File 'lib/toaster/model/automation.rb', line 29

def additional_state_configs
  @additional_state_configs
end

#chef_run_listObject

attr_accessor :tasks, :attributes,



29
30
31
# File 'lib/toaster/model/automation.rb', line 29

def chef_run_list
  @chef_run_list
end

#versionObject

attr_accessor :tasks, :attributes,



29
30
31
# File 'lib/toaster/model/automation.rb', line 29

def version
  @version
end

Class Method Details

.find(criteria = {}) ⇒ Object



190
191
192
# File 'lib/toaster/model/automation.rb', line 190

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

.find_by_cookbook_and_runlist(automation_name, run_list) ⇒ Object



179
180
181
182
183
184
185
186
187
188
# File 'lib/toaster/model/automation.rb', line 179

def self.find_by_cookbook_and_runlist(automation_name, run_list)
  criteria = {
    :cookbook => automation_name, 
    :recipes => run_list.to_s,
    :user => User.get_current_user
  }
  auto = find(criteria)
  puts "Automation for user=#{User.get_current_user} and name='#{automation_name}' : #{auto}"
  return auto
end

.get_attribute_array_names(current, array_name = "node", name_so_far = array_name, list_so_far = []) ⇒ Object

TODO move/remove?



222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/toaster/model/automation.rb', line 222

def self.get_attribute_array_names(current, array_name="node", name_so_far=array_name, list_so_far=[])
  return list_so_far if current.nil?
  if !current.kind_of?(Hash)
    list_so_far << name_so_far
    return list_so_far
  end
  current.each do |name,value|
    name = "#{name_so_far}['#{name}']"
    get_attribute_array_names(value, array_name, name, list_so_far)
  end
  return list_so_far
end

.load_for_chef(name, task_list = nil, attributes = nil, chef_run_list = nil) ⇒ Object



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/toaster/model/automation.rb', line 194

def self.load_for_chef(name, task_list = nil, attributes = nil, chef_run_list = nil)
  params = {
    :user => User.get_current_user(),
    :name => name,
    :cookbook => name,
    :recipes => chef_run_list.to_s,
    :language => "Chef"
  }
  auto = find_by(params)
  if !auto
    auto = Automation.new(params)
  else
    auto = auto[0]
  end
  if task_list && auto.tasks.empty?
    auto.tasks.concat(task_list)
  end
  if attributes && auto.automation_attributes.empty?
    attr_array = []
    attributes.each do |k,v|
      attr_array << AutomationAttribute.new(:key => k, :value => v)
    end
    auto.automation_attributes.concat(attr_array)
  end
  return auto
end

Instance Method Details

#all_affected_property_namesObject



126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/toaster/model/automation.rb', line 126

def all_affected_property_names()
  result = Set.new
  tasks.each do |t|
    t.global_state_transitions.each do |st|
      st.pre_state.each do |key,val|
        result << key
      end
    end
  end
  result = result.to_a.sort
  return result
end

#get_all_test_casesObject



73
74
75
76
# File 'lib/toaster/model/automation.rb', line 73

def get_all_test_cases()
  TestCase.joins(:automation_run => :automation).where(
    "automations.id = #{self.id}")
end

#get_attribute(attr_name) ⇒ Object



99
100
101
102
103
104
# File 'lib/toaster/model/automation.rb', line 99

def get_attribute(attr_name)
  attribs = get_flat_attributes()
  val = attribs[attr_name]
  return [attr_name,val] if val
  return nil
end

#get_default_value(parameter_name) ⇒ Object



93
94
95
96
97
# File 'lib/toaster/model/automation.rb', line 93

def get_default_value(parameter_name)
  a = get_attribute(parameter_name)
  return nil if !a
  return a[1]
end

#get_flat_attributesObject



89
90
91
# File 'lib/toaster/model/automation.rb', line 89

def get_flat_attributes()
  KeyValuePair.get_as_hash(automation_attributes)
end

#get_globally_executed_tasksObject



38
39
40
41
42
43
44
45
# File 'lib/toaster/model/automation.rb', line 38

def get_globally_executed_tasks()
  exec_tasks = Task.
      joins(:task_executions => {:automation_run => :automation}).
      where("automation_runs.automation_id = #{self.id}").
      distinct()
  return exec_tasks if !exec_tasks.empty?
  return tasks
end

#get_num_runsObject



152
153
154
# File 'lib/toaster/model/automation.rb', line 152

def get_num_runs()
  return automation_runs.size
end

#get_run(automation_run_id) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/toaster/model/automation.rb', line 139

def get_run(automation_run_id)
  run_ids = []
  automation_runs().each do |r|
    run_ids << r.id
    if r.id.to_s == automation_run_id.to_s || r.uuid.to_s == automation_run_id.to_s
      return r
    end
  end
  puts "WARN: Did not find automation run '#{automation_run_id}' in automation '#{uuid}'. Existing runs: #{run_ids.inspect}"
  puts caller
  return nil
end

#get_seen_attribute_valuesObject

TODO: fix/revise



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/toaster/model/automation.rb', line 107

def get_seen_attribute_values()
  map = {}
  attribute_names = self.class.get_attribute_array_names(automation_attributes,"")
  runs = automation_runs()
  runs.each do |run|
    attribute_names.each do |param_array_path|
      name = MarkupUtil.convert_array_to_dot_notation(param_array_path)
      map[name] = [] if !map[name]
      val = nil
      eval("val = run.run_attributes#{param_array_path}")
      map[name] << val
    end
  end
  map.each do |key,array|
    array.uniq!
  end
  return map
end

#get_short_nameObject



85
86
87
# File 'lib/toaster/model/automation.rb', line 85

def get_short_name() 
  return ChefUtil.extract_node_name(name)
end

#get_task(task_id, check_automation_runs = false) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/toaster/model/automation.rb', line 156

def get_task(task_id, check_automation_runs=false)
  task_id = task_id.to_s
  tasks.each do |t| 
    return t if (t.id.to_s == task_id || t.uuid.to_s == task_id)
  end
  if check_automation_runs
    automation_runs().each do |r|
      r.task_executions().each do |exe|
        if exe.task && (exe.task.id.to_s == task_id || exe.task.uuid == task_id)
          return exe.task
        end
      end
    end
  end
  raise "WARN: Did not find task '#{task_id}' in automation '#{uuid}'"
end

#get_task_execs_by_runObject

return a map AutomationRun -> (list of TaskExecution)



63
64
65
66
67
68
69
70
71
# File 'lib/toaster/model/automation.rb', line 63

def get_task_execs_by_run()
  result = {}
  task_execs = TaskExecution.load_all_for_automation(self)
  task_execs.each do |exe|
    result[exe.automation_run] = [] if !result[exe.automation_run]
    result[exe.automation_run] << exe
  end
  return result
end

#get_task_idsObject



173
174
175
176
177
# File 'lib/toaster/model/automation.rb', line 173

def get_task_ids()
  task_ids = []
  tasks.each do |t| task_ids.push(t.uuid) end
  return task_ids
end

#global_state_transitionsObject

collect the executed state transitions of all tasks contained in this automation



49
50
51
52
53
54
55
# File 'lib/toaster/model/automation.rb', line 49

def global_state_transitions()
  result = Set.new
  get_globally_executed_tasks().each do |task|
    result += task.global_state_transitions
  end
  return result
end

#is_chef?Boolean

Returns:

  • (Boolean)


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

def is_chef?
  "#{language}".casecmp("chef") == 0
end

#num_global_state_transitionsObject



56
57
58
# File 'lib/toaster/model/automation.rb', line 56

def num_global_state_transitions()
  global_state_transitions().size
end

#short_nameObject



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

def short_name()
  get_short_name()
end