Class: JobInvocation

Inherits:
ApplicationRecord
  • Object
show all
Includes:
Authorizable, Encryptable, ForemanRemoteExecution::ErrorsFlattener, ForemanTasks::Concerns::ActionSubject
Defined in:
app/models/job_invocation.rb

Constant Summary collapse

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

Methods included from ForemanRemoteExecution::ErrorsFlattener

#flattened_errors, #flattened_validation_exception

Instance Attribute Details

#description_formatObject

Returns the value of attribute description_format.



66
67
68
# File 'app/models/job_invocation.rb', line 66

def description_format
  @description_format
end

#start_at=(value) ⇒ Object (writeonly)

Sets the attribute start_at

Parameters:

  • value

    the value to set the attribute start_at to.



67
68
69
# File 'app/models/job_invocation.rb', line 67

def start_at=(value)
  @start_at = value
end

#start_beforeObject

Returns the value of attribute start_before.



66
67
68
# File 'app/models/job_invocation.rb', line 66

def start_before
  @start_before
end

Class Method Details

.search_by_recurring_logic(key, operator, value) ⇒ Object



79
80
81
82
83
84
85
# File 'app/models/job_invocation.rb', line 79

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



73
74
75
76
77
# File 'app/models/job_invocation.rb', line 73

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



184
185
186
# File 'app/models/job_invocation.rb', line 184

def available_providers(host)
  return RemoteExecutionProvider.provider_names
end

#build_notificationObject



91
92
93
94
95
96
97
98
99
100
101
102
# File 'app/models/job_invocation.rb', line 91

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



229
230
231
232
# File 'app/models/job_invocation.rb', line 229

def cancel(force = false)
  method = force ? :abort : :cancel
  task.send(method)
end

#deep_cloneObject



131
132
133
134
135
136
137
138
139
140
141
142
# File 'app/models/job_invocation.rb', line 131

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.sudo_password = self.sudo_password
  end
end

#deep_clone!Object



144
145
146
# File 'app/models/job_invocation.rb', line 144

def deep_clone!
  deep_clone.tap(&:save!)
end

#failed_host_idsObject



156
157
158
# File 'app/models/job_invocation.rb', line 156

def failed_host_ids
  failed_template_invocations.map(&:host_id)
end

#failed_hostsObject



160
161
162
# File 'app/models/job_invocation.rb', line 160

def failed_hosts
  failed_template_invocations.includes(:host).map(&:host)
end

#failed_template_invocation_tasksObject



152
153
154
# File 'app/models/job_invocation.rb', line 152

def failed_template_invocation_tasks
  template_invocation_tasks.where(:result => TemplateInvocation::TaskResultMap.status_to_task_result(:failed))
end

#finished?Boolean

Returns:

  • (Boolean)


234
235
236
# File 'app/models/job_invocation.rb', line 234

def finished?
  !task.pending?
end

#generate_descriptionObject



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'app/models/job_invocation.rb', line 197

def generate_description
  key_re = /%\{([^\}]+)\}/
  template_invocation = pattern_template_invocations.first
  hash_base = Hash.new { |hash, key| hash[key] = "%{#{key}}" }
  input_hash = template_invocation.input_values.reduce(hash_base) do |h, v|
    h.update(v.template_input.name => v.value)
  end
  input_hash.update(:job_category => job_category)
  input_hash.update(:template_name => template_invocation.template.name)
  description_format.scan(key_re) { |key| input_hash[key.first] }
  self.description = description_format
  input_hash.each do |k, v|
    self.description.gsub!(Regexp.new("%\{#{k}\}"), v || '')
  end
  self.description = self.description[0..(JobInvocation.columns_hash['description'].limit - 1)]
end

#notification_recipients_idsObject



87
88
89
# File 'app/models/job_invocation.rb', line 87

def notification_recipients_ids
  [ self.targeting.user_id ]
end

#output(host) ⇒ Object



192
193
194
195
# File 'app/models/job_invocation.rb', line 192

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



172
173
174
175
176
177
178
179
180
181
# File 'app/models/job_invocation.rb', line 172

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



117
118
119
120
121
122
123
124
125
# File 'app/models/job_invocation.rb', line 117

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_reportObject



214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'app/models/job_invocation.rb', line 214

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

Returns:

  • (Boolean)


127
128
129
# File 'app/models/job_invocation.rb', line 127

def queued?
  status == HostStatus::ExecutionStatus::QUEUED
end

#statusObject



104
105
106
# File 'app/models/job_invocation.rb', line 104

def status
  HostStatus::ExecutionStatus::ExecutionTaskStatusMapper.new(task).status
end

#status_labelObject



108
109
110
# File 'app/models/job_invocation.rb', line 108

def status_label
  HostStatus::ExecutionStatus::ExecutionTaskStatusMapper.new(task).status_label
end

#sub_task_for_host(host) ⇒ Object



188
189
190
# File 'app/models/job_invocation.rb', line 188

def sub_task_for_host(host)
  template_invocations.find_by(:host => host.id).try(:run_host_job_task)
end

#to_action_inputObject



148
149
150
# File 'app/models/job_invocation.rb', line 148

def to_action_input
  { :id => id, :name => job_category, :description => description }
end

#to_labelObject



112
113
114
# File 'app/models/job_invocation.rb', line 112

def to_label
  description
end

#total_hosts_countObject



164
165
166
167
168
169
170
# File 'app/models/job_invocation.rb', line 164

def total_hosts_count
  if targeting.resolved?
    targeting.hosts.count
  else
    _('N/A')
  end
end