Class: ForemanRemoteExecutionCore::PollingScriptRunner
- Inherits:
-
ScriptRunner
- Object
- ForemanTasksCore::Runner::Base
- ScriptRunner
- ForemanRemoteExecutionCore::PollingScriptRunner
show all
- Defined in:
- lib/foreman_remote_execution_core/polling_script_runner.rb
Constant Summary
collapse
- DEFAULT_REFRESH_INTERVAL =
60
Constants inherited
from ScriptRunner
ScriptRunner::EXPECTED_POWER_ACTION_MESSAGES, ScriptRunner::MAX_PROCESS_RETRIES
Instance Attribute Summary
Attributes inherited from ScriptRunner
#execution_timeout_interval
Instance Method Summary
collapse
build, #kill, #publish_data, #start, #timeout, #timeout_interval, #with_disconnect_handling, #with_retries
Constructor Details
Returns a new instance of PollingScriptRunner.
6
7
8
9
10
11
12
|
# File 'lib/foreman_remote_execution_core/polling_script_runner.rb', line 6
def initialize(options, user_method)
super(options, user_method)
@callback_host = options[:callback_host]
@task_id = options[:uuid]
@step_id = options[:step_id]
@otp = ForemanTasksCore::OtpManager.generate_otp(@task_id)
end
|
Instance Method Details
#callback_script ⇒ Object
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
# File 'lib/foreman_remote_execution_core/polling_script_runner.rb', line 101
def callback_script
<<-SCRIPT.gsub(/^ +\| /, '')
| #!/bin/sh
| exit_code=$(cat "#{@exit_code_path}")
| url="#{@callback_host}/dynflow/tasks/#{@task_id}/done"
| json="{ \\\"step_id\\\": #{@step_id} }"
| if which curl >/dev/null; then
| curl -X POST -d "$json" -u "#{@task_id}:#{@otp}" "$url"
| else
| echo 'curl is required' >&2
| exit 1
| fi
SCRIPT
end
|
#callback_scriptlet(callback_script_path = nil) ⇒ Object
92
93
94
95
96
97
98
99
|
# File 'lib/foreman_remote_execution_core/polling_script_runner.rb', line 92
def callback_scriptlet(callback_script_path = nil)
if @otp
callback_script_path = cp_script_to_remote(callback_script, 'callback') if callback_script_path.nil?
"#{@user_method.cli_command_prefix}#{callback_script_path}"
else
':' end
end
|
#close ⇒ Object
53
54
55
56
|
# File 'lib/foreman_remote_execution_core/polling_script_runner.rb', line 53
def close
super
ForemanTasksCore::OtpManager.drop_otp(@task_id, @otp) if @otp
end
|
#control_script ⇒ Object
22
23
24
25
26
27
28
29
30
|
# File 'lib/foreman_remote_execution_core/polling_script_runner.rb', line 22
def control_script
close_stdin = '</dev/null'
close_fds = close_stdin + ' >/dev/null 2>/dev/null'
<<-SCRIPT.gsub(/^\s+\| /, '')
| sh -c '(#{@user_method.cli_command_prefix}#{@remote_script} #{close_stdin}; echo $?>#{@exit_code_path}) | /usr/bin/tee #{@output_path} >/dev/null; #{callback_scriptlet}' #{close_fds} &
| echo $! > '#{@pid_path}'
SCRIPT
end
|
#prepare_retrieval ⇒ Object
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
85
86
87
88
89
90
|
# File 'lib/foreman_remote_execution_core/polling_script_runner.rb', line 58
def prepare_retrieval
base = File.dirname(@output_path)
posfile = File.join(base, 'position')
tmpfile = File.join(base, 'tmp')
script = <<-SCRIPT.gsub(/^ +\| /, '')
| #!/bin/sh
| pid=$(cat "#{@pid_path}")
| if ! pgrep --help 2>/dev/null >/dev/null; then
| echo DONE 1
| echo "pgrep is required" >&2
| exit 1
| fi
| if pgrep -P "$pid" >/dev/null 2>/dev/null; then
| echo RUNNING
| else
| echo "DONE $(cat "#{@exit_code_path}" 2>/dev/null)"
| fi
| [ -f "#{@output_path}" ] || exit 0
| [ -f "#{posfile}" ] || echo 1 > "#{posfile}"
| position=$(cat "#{posfile}")
| tail --bytes "+${position}" "#{@output_path}" > "#{tmpfile}"
| bytes=$(cat "#{tmpfile}" | wc --bytes)
| expr "${position}" + "${bytes}" > "#{posfile}"
| cat "#{tmpfile}"
SCRIPT
@logger.debug("copying script:\n#{script.lines.map { |line| " | #{line}" }.join}")
cp_script_to_remote(script, 'retrieve')
@prepared = true
end
|
#prepare_start ⇒ Object
14
15
16
17
18
19
20
|
# File 'lib/foreman_remote_execution_core/polling_script_runner.rb', line 14
def prepare_start
super
basedir = File.dirname @remote_script
@pid_path = File.join(basedir, 'pid')
@retrieval_script ||= File.join(basedir, 'retrieve')
prepare_retrieval unless @prepared
end
|
#refresh ⇒ Object
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
# File 'lib/foreman_remote_execution_core/polling_script_runner.rb', line 36
def refresh
err = output = nil
with_retries do
_, output, err = run_sync("#{@user_method.cli_command_prefix} #{@retrieval_script}")
end
lines = output.lines
result = lines.shift.match(/^DONE (\d+)?/)
publish_data(lines.join, 'stdout') unless lines.empty?
publish_data(err, 'stderr') unless err.empty?
if result
exitcode = result[1] || 0
publish_exit_status(exitcode.to_i)
run_sync("rm -rf \"#{remote_command_dir}\"") if @cleanup_working_dirs
end
destroy_session
end
|
#trigger(*args) ⇒ Object
32
33
34
|
# File 'lib/foreman_remote_execution_core/polling_script_runner.rb', line 32
def trigger(*args)
run_sync(*args)
end
|