Class: Actions::RemoteExecution::RunHostJob

Inherits:
EntryAction
  • Object
show all
Extended by:
ApipieDSL::Class
Includes:
Helpers::WithContinuousOutput, Helpers::WithDelegatedAction, ObservableAction
Defined in:
app/lib/actions/remote_execution/run_host_job.rb

Defined Under Namespace

Classes: Jail

Instance Method Summary collapse

Instance Method Details

#check_exit_statusObject



69
70
71
72
# File 'app/lib/actions/remote_execution/run_host_job.rb', line 69

def check_exit_status
  error! ForemanTasks::Task::TaskCancelledException.new(_('Task cancelled')) if delegated_action && delegated_action.output[:cancel_sent]
  error! _('Job execution failed') if exit_status.to_s != '0'
end

#continuous_output_providersObject



96
97
98
# File 'app/lib/actions/remote_execution/run_host_job.rb', line 96

def continuous_output_providers
  super << self
end

#exit_statusObject



121
122
123
# File 'app/lib/actions/remote_execution/run_host_job.rb', line 121

def exit_status
  delegated_output[:exit_status]
end

#fill_continuous_output(continuous_output) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'app/lib/actions/remote_execution/run_host_job.rb', line 100

def fill_continuous_output(continuous_output)
  delegated_output.fetch('result', []).each do |raw_output|
    continuous_output.add_raw_output(raw_output)
  end

  final_timestamp = (continuous_output.last_timestamp || task.ended_at).to_f + 1

  if task.state == 'stopped' && task.result == 'cancelled'
    continuous_output.add_output(_('Job cancelled by user'), 'debug', final_timestamp)
  else
    fill_planning_errors_to_continuous_output(continuous_output) unless exit_status
  end
  if exit_status
    continuous_output.add_output(_('Exit status: %s') % exit_status, 'stdout', final_timestamp)
  elsif run_step&.error
    continuous_output.add_output(_('Job finished with error') + ": #{run_step.error.exception_class} - #{run_step.error.message}", 'debug', final_timestamp)
  end
rescue => e
  continuous_output.add_exception(_('Error loading data from proxy'), e)
end

#finalize(*args) ⇒ Object



56
57
58
59
# File 'app/lib/actions/remote_execution/run_host_job.rb', line 56

def finalize(*args)
  update_host_status
  check_exit_status
end

#hostObject



141
142
143
# File 'app/lib/actions/remote_execution/run_host_job.rb', line 141

def host
  @host ||= ::Host.authorized.find(host_id)
end

#host_idObject



125
126
127
# File 'app/lib/actions/remote_execution/run_host_job.rb', line 125

def host_id
  input['host']['id']
end

#host_nameObject



129
130
131
# File 'app/lib/actions/remote_execution/run_host_job.rb', line 129

def host_name
  input['host']['name']
end

#humanized_inputObject



79
80
81
82
# File 'app/lib/actions/remote_execution/run_host_job.rb', line 79

def humanized_input
  N_('%{description} on %{host}') % { :host => input[:host].try(:[], :name),
                                      :description => input[:description].try(:capitalize) || input[:job_category] }
end

#humanized_nameObject



84
85
86
# File 'app/lib/actions/remote_execution/run_host_job.rb', line 84

def humanized_name
  N_('Remote action:')
end

#humanized_outputObject



92
93
94
# File 'app/lib/actions/remote_execution/run_host_job.rb', line 92

def humanized_output
  continuous_output.humanize
end

#job_invocationObject



137
138
139
# File 'app/lib/actions/remote_execution/run_host_job.rb', line 137

def job_invocation
  @job_invocation ||= ::JobInvocation.authorized.find(job_invocation_id)
end

#job_invocation_idObject



133
134
135
# File 'app/lib/actions/remote_execution/run_host_job.rb', line 133

def job_invocation_id
  input['job_invocation_id']
end

#live_outputObject



74
75
76
77
# File 'app/lib/actions/remote_execution/run_host_job.rb', line 74

def live_output
  continuous_output.sort!
  continuous_output.raw_outputs
end

#plan(job_invocation, host, template_invocation, proxy_selector = ::RemoteExecutionProxySelector.new, options = {}) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/lib/actions/remote_execution/run_host_job.rb', line 19

def plan(job_invocation, host, template_invocation, proxy_selector = ::RemoteExecutionProxySelector.new, options = {})
  action_subject(host, :job_category => job_invocation.job_category, :description => job_invocation.description, :job_invocation_id => job_invocation.id)

  template_invocation.host_id = host.id
  template_invocation.run_host_job_task_id = task.id
  template_invocation.save!

  link!(job_invocation)
  link!(template_invocation)

  verify_permissions(host, template_invocation)

  raise _('Could not use any template used in the job invocation') if template_invocation.blank?

  provider = template_invocation.template.provider
  proxy_selector = provider.required_proxy_selector_for(template_invocation.template) || proxy_selector

  provider_type = template_invocation.template.provider_type.to_s
  proxy = determine_proxy!(proxy_selector, provider_type, host)

  renderer = InputTemplateRenderer.new(template_invocation.template, host, template_invocation)
  script = renderer.render
  raise _('Failed rendering template: %s') % renderer.error_message unless script

  additional_options = { :hostname => provider.find_ip_or_hostname(host),
                         :script => script,
                         :execution_timeout_interval => job_invocation.execution_timeout_interval,
                         :secrets => secrets(host, job_invocation, provider),
                         :use_batch_triggering => true,
                         :use_concurrency_control => options[:use_concurrency_control]}
  action_options = provider.proxy_command_options(template_invocation, host)
                           .merge(additional_options)

  plan_delegated_action(proxy, provider.proxy_action_class, action_options)
  plan_self
end

#queueObject



11
12
13
# File 'app/lib/actions/remote_execution/run_host_job.rb', line 11

def queue
  ForemanRemoteExecution::DYNFLOW_QUEUE
end

#rescue_strategyObject



88
89
90
# File 'app/lib/actions/remote_execution/run_host_job.rb', line 88

def rescue_strategy
  ::Dynflow::Action::Rescue::Fail
end

#resource_locksObject



15
16
17
# File 'app/lib/actions/remote_execution/run_host_job.rb', line 15

def resource_locks
  :link
end

#secrets(host, job_invocation, provider) ⇒ Object



61
62
63
64
65
66
67
# File 'app/lib/actions/remote_execution/run_host_job.rb', line 61

def secrets(host, job_invocation, provider)
  job_secrets = { :ssh_password => job_invocation.password,
                  :key_passphrase => job_invocation.key_passphrase,
                  :effective_user_password => job_invocation.effective_user_password }

  job_secrets.merge(provider.secrets(host)) { |_key, job_secret, provider_secret| job_secret || provider_secret }
end