Class: JobInvocation
Constant Summary
collapse
- CACHE_PREFIX =
"job_invocation".freeze
- FLATTENED_ERRORS_MAPPING =
{
:pattern_template_invocations => lambda do |template_invocation|
_('template') + " #{template_invocation.template.name}"
end,
}.freeze
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
#flattened_errors, #flattened_validation_exception
Instance Attribute Details
Returns the value of attribute description_format.
71
72
73
|
# File 'app/models/job_invocation.rb', line 71
def description_format
@description_format
end
|
#start_at=(value) ⇒ Object
Sets the attribute start_at
72
73
74
|
# File 'app/models/job_invocation.rb', line 72
def start_at=(value)
@start_at = value
end
|
#start_before ⇒ Object
Returns the value of attribute start_before.
71
72
73
|
# File 'app/models/job_invocation.rb', line 71
def start_before
@start_before
end
|
Class Method Details
.search_by_recurring_logic(key, operator, value) ⇒ Object
84
85
86
87
88
89
90
|
# File 'app/models/job_invocation.rb', line 84
def self.search_by_recurring_logic(key, operator, value)
reucurring = value == 'true'
reucurring = !reucurring if operator == '<>'
not_operator = reucurring ? 'NOT' : ''
{ :conditions => sanitize_sql_for_conditions(["foreman_tasks_recurring_logics.id IS #{not_operator} NULL"]), :joins => :recurring_logic }
end
|
.search_by_status(key, operator, value) ⇒ Object
78
79
80
81
82
|
# File 'app/models/job_invocation.rb', line 78
def self.search_by_status(key, operator, value)
conditions = HostStatus::ExecutionStatus::ExecutionTaskStatusMapper.sql_conditions_for(value)
conditions[0] = "NOT (#{conditions[0]})" if operator == '<>'
{ :conditions => sanitize_sql_for_conditions(conditions), :include => :task }
end
|
Instance Method Details
#available_providers(host) ⇒ Object
TODO: determine from the host and job_invocation details
#build_notification ⇒ Object
96
97
98
99
100
101
102
103
104
105
106
107
|
# File 'app/models/job_invocation.rb', line 96
def build_notification
klass = nil
if self.remote_execution_feature && self.remote_execution_feature.notification_builder.present?
begin
klass = remote_execution_feature.notification_builder.constantize
rescue NameError => e
logger.exception "REX feature defines unknown notification builder class", e
end
end
klass ||= UINotifications::RemoteExecutionJobs::BaseJobFinish
klass.new(self)
end
|
#cancel(force = false) ⇒ Object
237
238
239
240
|
# File 'app/models/job_invocation.rb', line 237
def cancel(force = false)
method = force ? :abort : :cancel
task.send(method)
end
|
#deep_clone ⇒ Object
136
137
138
139
140
141
142
143
144
145
146
147
|
# File 'app/models/job_invocation.rb', line 136
def deep_clone
JobInvocationComposer.from_job_invocation(self).job_invocation.tap do |invocation|
invocation.task_group = JobInvocationTaskGroup.new
invocation.triggering = self.triggering
invocation.description_format = self.description_format
invocation.description = self.description
invocation.pattern_template_invocations = self.pattern_template_invocations.map(&:deep_clone)
invocation.password = self.password
invocation.key_passphrase = self.key_passphrase
invocation.effective_user_password = self.effective_user_password
end
end
|
#deep_clone! ⇒ Object
149
150
151
|
# File 'app/models/job_invocation.rb', line 149
def deep_clone!
deep_clone.tap(&:save!)
end
|
#failed_host_ids ⇒ Object
161
162
163
|
# File 'app/models/job_invocation.rb', line 161
def failed_host_ids
failed_hosts.pluck(:id)
end
|
#failed_hosts ⇒ Object
165
166
167
168
169
170
171
172
|
# File 'app/models/job_invocation.rb', line 165
def failed_hosts
base = targeting.hosts
if finished?
base.where.not(:id => not_failed_template_invocations.select(:host_id))
else
base.where(:id => failed_template_invocations.select(:host_id))
end
end
|
#failed_template_invocation_tasks ⇒ Object
#finished? ⇒ Boolean
242
243
244
|
# File 'app/models/job_invocation.rb', line 242
def finished?
!task.pending?
end
|
#generate_description ⇒ Object
208
209
210
211
212
213
214
215
216
217
218
219
220
|
# File 'app/models/job_invocation.rb', line 208
def generate_description
template_invocation = pattern_template_invocations.first
input_hash = template_invocation.input_values.reduce({}) do |h, v|
value = v.value
value = '*' * 3 if v.template_input.respond_to?(:hidden_value) && v.template_input.hidden_value?
h.update("%{#{v.template_input.name}}" => value)
end
input_hash.update("%{job_category}" => job_category)
input_hash.update("%{template_name}" => template_invocation.template.name)
input_hash.default = "''"
self.description = description_format.gsub(/%{[^}]+}/) { |key| input_hash[key] }
self.description = self.description[0..(JobInvocation.columns_hash['description'].limit - 1)]
end
|
#notification_recipients_ids ⇒ Object
92
93
94
|
# File 'app/models/job_invocation.rb', line 92
def notification_recipients_ids
[ self.targeting.user_id ]
end
|
#output(host) ⇒ Object
202
203
204
205
206
|
# File 'app/models/job_invocation.rb', line 202
def output(host)
return unless (task = sub_task_for_host(host)) && task.main_action && task.main_action.live_output.any?
task.main_action.live_output.first['output']
end
|
#pattern_template_invocation_for_host(host) ⇒ Object
182
183
184
185
186
187
188
189
190
191
|
# File 'app/models/job_invocation.rb', line 182
def pattern_template_invocation_for_host(host)
providers = available_providers(host)
providers.each do |provider|
pattern_template_invocations.each do |template_invocation|
if template_invocation.template.provider_type == provider
return template_invocation
end
end
end
end
|
#progress(total = nil, done = nil) ⇒ Object
returns progress in percents
122
123
124
125
126
127
128
129
130
|
# File 'app/models/job_invocation.rb', line 122
def progress(total = nil, done = nil)
if queued? || !targeting.resolved? || done == 0
0
else
total ||= targeting.hosts.count
done ||= sub_tasks.where(:result => %w(success warning error)).count
((done.to_f / total) * 100).round
end
end
|
#progress_report ⇒ Object
222
223
224
225
226
227
228
229
230
231
232
233
234
235
|
# File 'app/models/job_invocation.rb', line 222
def progress_report
map = TemplateInvocation::TaskResultMap
all_keys = (map.results | map.statuses | [:progress, :total])
if queued? || (task && task.started_at.nil?) || !targeting.resolved?
all_keys.reduce({}) do |acc, key|
acc.merge(key => 0)
end
else
counts = task.sub_tasks_counts
done = counts.values_at(*map.results).reduce(:+)
percent = progress(counts[:total], done)
counts.merge(:progress => percent, :failed => counts.values_at(*map.status_to_task_result(:failed)).reduce(:+))
end
end
|
#queued? ⇒ Boolean
132
133
134
|
# File 'app/models/job_invocation.rb', line 132
def queued?
status == HostStatus::ExecutionStatus::QUEUED
end
|
#status ⇒ Object
109
110
111
|
# File 'app/models/job_invocation.rb', line 109
def status
HostStatus::ExecutionStatus::ExecutionTaskStatusMapper.new(task).status
end
|
#status_label ⇒ Object
113
114
115
|
# File 'app/models/job_invocation.rb', line 113
def status_label
HostStatus::ExecutionStatus::ExecutionTaskStatusMapper.new(task).status_label
end
|
#sub_task_for_host(host) ⇒ Object
198
199
200
|
# File 'app/models/job_invocation.rb', line 198
def sub_task_for_host(host)
template_invocations.find_by(:host => host.id).try(:run_host_job_task)
end
|
153
154
155
|
# File 'app/models/job_invocation.rb', line 153
def to_action_input
{ :id => id, :name => job_category, :description => description }
end
|
#to_label ⇒ Object
117
118
119
|
# File 'app/models/job_invocation.rb', line 117
def to_label
description
end
|
#total_hosts_count ⇒ Object
174
175
176
177
178
179
180
|
# File 'app/models/job_invocation.rb', line 174
def total_hosts_count
if targeting.resolved?
targeting.hosts.count
else
_('N/A')
end
end
|