Class: ForemanTasks::RemoteTask

Inherits:
ApplicationRecord
  • Object
show all
Defined in:
app/models/foreman_tasks/remote_task.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#resultObject

Returns the value of attribute result.



3
4
5
# File 'app/models/foreman_tasks/remote_task.rb', line 3

def result
  @result
end

Class Method Details

.batch_trigger(operation, remote_tasks) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'app/models/foreman_tasks/remote_task.rb', line 28

def self.batch_trigger(operation, remote_tasks)
  remote_tasks.group_by(&:proxy_url).values.map do |group|
    input_hash = group.reduce({}) do |acc, remote_task|
      acc.merge(remote_task.execution_plan_id => { :action_input => remote_task.proxy_input,
                                                   :action_class => remote_task.proxy_action_name })
    end
    safe_batch_trigger(operation, group, input_hash)
  end
  remote_tasks
end

.fallback_batch_trigger(remote_tasks, input_hash) ⇒ Object

Trigger the tasks one-by-one using the old API



49
50
51
52
53
54
# File 'app/models/foreman_tasks/remote_task.rb', line 49

def self.fallback_batch_trigger(remote_tasks, input_hash)
  remote_tasks.each do |remote_task|
    task_data = input_hash[remote_task.execution_plan_id]
    remote_task.trigger(task_data[:action_class], task_data[:action_input])
  end
end

.safe_batch_trigger(operation, remote_tasks, input_hash) ⇒ Object

Attempt to trigger the tasks using the new API and fall back to the old one if it fails



41
42
43
44
45
46
# File 'app/models/foreman_tasks/remote_task.rb', line 41

def self.safe_batch_trigger(operation, remote_tasks, input_hash)
  results = remote_tasks.first.proxy.launch_tasks(operation, input_hash)
  remote_tasks.each { |remote_task| remote_task.update_from_batch_trigger results[remote_task.execution_plan_id] }
rescue RestClient::NotFound
  fallback_batch_trigger remote_tasks, input_hash
end

Instance Method Details

#proxyObject



73
74
75
# File 'app/models/foreman_tasks/remote_task.rb', line 73

def proxy
  @proxy ||= ::ProxyAPI::ForemanDynflow::DynflowProxy.new(:url => proxy_url)
end

#proxy_inputObject



69
70
71
# File 'app/models/foreman_tasks/remote_task.rb', line 69

def proxy_input
  action.proxy_input(task.id)
end

#trigger(proxy_action_name, input) ⇒ Object

Triggers a task on the proxy “the old way”



17
18
19
20
21
22
23
24
25
26
# File 'app/models/foreman_tasks/remote_task.rb', line 17

def trigger(proxy_action_name, input)
  response = begin
               proxy.trigger_task(proxy_action_name, input).merge('result' => 'success')
             rescue RestClient::Exception => e
               logger.warn "Could not trigger task on the smart proxy: #{e.message}"
               {}
             end
  update_from_batch_trigger(response)
  save!
end

#update_from_batch_trigger(data) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/models/foreman_tasks/remote_task.rb', line 56

def update_from_batch_trigger(data)
  if data['result'] == 'success'
    self.remote_task_id = data['task_id']
    self.state = 'triggered'
  else
    # Tell the action the task on the smart proxy stopped
    ForemanTasks.dynflow.world.event execution_plan_id,
                                     step_id,
                                     ::Actions::ProxyAction::ProxyActionStopped.new
  end
  save!
end