Class: Actions::RemoteExecution::RunHostJob

Inherits:
EntryAction
  • Object
show all
Includes:
Helpers::LiveOutput
Defined in:
app/lib/actions/remote_execution/run_host_job.rb

Instance Method Summary collapse

Methods included from Helpers::LiveOutput

#exception_to_output, #format_output

Instance Method Details

#finalize(*args) ⇒ Object



50
51
52
53
54
55
# File 'app/lib/actions/remote_execution/run_host_job.rb', line 50

def finalize(*args)
  host = Host.find(input[:host][:id])
  host.refresh_statuses
rescue => e
  ::Foreman::Logging.exception "Could not update execution status for #{input[:host][:name]}", e
end

#find_ip_or_hostname(host) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
# File 'app/lib/actions/remote_execution/run_host_job.rb', line 79

def find_ip_or_hostname(host)
  %w(execution primary provision).each do |flag|
    interface = host.send(flag + '_interface')
    return interface.ip if interface && interface.ip.present?
  end

  host.interfaces.each do |interface|
    return interface.ip unless interface.ip.blank?
  end

  return host.fqdn
end

#humanized_inputObject



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

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

#humanized_nameObject



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

def humanized_name
  N_('Remote action:')
end

#humanized_outputObject



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

def humanized_output
  live_output.map { |line| line['output'].chomp }.join("\n")
end

#live_outputObject



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

def live_output
  proxy_command_action = planned_actions(RunProxyCommand).first
  if proxy_command_action
    proxy_command_action.live_output
  else
    execution_plan.errors.map { |e| exception_to_output(_('Failed to initialize command'), e) }
  end
end

#plan(job_invocation, host, template_invocation, proxy, options = {}) ⇒ Object



12
13
14
15
16
17
18
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
# File 'app/lib/actions/remote_execution/run_host_job.rb', line 12

def plan(job_invocation, host, template_invocation, proxy, options = {})
  action_subject(host, :job_category => job_invocation.job_category, :description => job_invocation.description)

  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)
  hostname = find_ip_or_hostname(host)

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

  if proxy.blank?
    offline_proxies = options.fetch(:offline_proxies, [])
    settings = { :count => offline_proxies.count, :proxy_names => offline_proxies.map(&:name).join(', ') }
    raise n_('The only applicable proxy %{proxy_names} is down',
             'All %{count} applicable proxies are down. Tried %{proxy_names}',
             offline_proxies.count) % settings unless offline_proxies.empty?

    settings = { :global_proxy   => 'remote_execution_global_proxy',
                 :fallback_proxy => 'remote_execution_fallback_proxy' }

    raise _('Could not use any proxy. Consider configuring %{global_proxy} ' +
            'or %{fallback_proxy} in settings') % settings
  end

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

  provider = template_invocation.template.provider
  plan_action(RunProxyCommand, proxy, hostname, script, provider.proxy_command_options(template_invocation, host))
  plan_self
end

#resource_locksObject



8
9
10
# File 'app/lib/actions/remote_execution/run_host_job.rb', line 8

def resource_locks
  :link
end