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.



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

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,



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

def additional_state_configs
  @additional_state_configs
end

#chef_run_listObject

attr_accessor :tasks, :attributes,



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

def chef_run_list
  @chef_run_list
end

#versionObject

attr_accessor :tasks, :attributes,



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

def version
  @version
end

Class Method Details

.find(criteria = {}) ⇒ Object



171
172
173
# File 'lib/toaster/model/automation.rb', line 171

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

.find_by_cookbook_and_runlist(automation_name, run_list) ⇒ Object



160
161
162
163
164
165
166
167
168
169
# File 'lib/toaster/model/automation.rb', line 160

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?



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

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



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/toaster/model/automation.rb', line 175

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



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

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_attribute(attr_name) ⇒ Object



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

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



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

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

#get_flat_attributesObject



70
71
72
# File 'lib/toaster/model/automation.rb', line 70

def get_flat_attributes()
  KeyValuePair.get_as_hash(automation_attributes)
end

#get_globally_executed_tasksObject



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

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



133
134
135
# File 'lib/toaster/model/automation.rb', line 133

def get_num_runs()
  return automation_runs.size
end

#get_run(automation_run_id) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/toaster/model/automation.rb', line 120

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



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/toaster/model/automation.rb', line 88

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



66
67
68
# File 'lib/toaster/model/automation.rb', line 66

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

#get_task(task_id, check_automation_runs = false) ⇒ Object



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

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.uuid.to_s == task_id || exe.task.uuid.to_s == 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)



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

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



154
155
156
157
158
# File 'lib/toaster/model/automation.rb', line 154

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

#is_chef?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/toaster/model/automation.rb', line 59

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

#short_nameObject



63
64
65
# File 'lib/toaster/model/automation.rb', line 63

def short_name()
  get_short_name()
end