forked from theforeman/foreman_remote_execution
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_host_job.rb
84 lines (67 loc) · 2.8 KB
/
run_host_job.rb
1
2
3
4
5
6
7
8
9
10
11
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
module Actions
module RemoteExecution
class RunHostJob < Actions::EntryAction
middleware.do_not_use Dynflow::Middleware::Common::Transaction
include Actions::RemoteExecution::Helpers::LiveOutput
def resource_locks
:link
end
def plan(job_invocation, host, template_invocation, proxy, connection_options = {})
action_subject(host, :job_name => job_invocation.job_name)
link!(job_invocation)
link!(template_invocation)
hostname = find_ip_or_hostname(host)
raise _("Could not use any template used in the job invocation") if template_invocation.blank?
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 if proxy.blank?
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_type.to_s
plan_action(RunProxyCommand, proxy, hostname, script, { :connection_options => connection_options }.merge(provider_settings(provider, host)))
plan_self
end
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
def humanized_output
live_output.map { |line| line['output'].chomp }.join("\n")
end
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
def humanized_name
_('Run %{job_name} on %{host}') % { :job_name => input[:job_name], :host => input[:host][:name] }
end
def find_ip_or_hostname(host)
%w(execution primary provision).each do |flag|
if host.send("#{flag}_interface") && host.send("#{flag}_interface").ip.present?
return host.execution_interface.ip
end
end
host.interfaces.each do |interface|
return interface.ip unless interface.ip.blank?
end
return host.fqdn
end
def provider_settings(provider, host)
case provider
when 'Ssh'
{ :ssh_user => host.params['remote_execution_ssh_user'] || Setting[:remote_execution_ssh_user] }
else
{}
end
end
end
end
end