Class: ForemanRemoteExecutionCore::PollingScriptRunner

Inherits:
ScriptRunner
  • Object
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

Instance Attribute Summary

Attributes inherited from ScriptRunner

#execution_timeout_interval

Instance Method Summary collapse

Methods inherited from ScriptRunner

#kill, #publish_data, #start, #timeout, #timeout_interval, #with_disconnect_handling, #with_retries

Constructor Details

#initialize(options = {}) ⇒ PollingScriptRunner

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 = {})
  super(options)
  @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_scriptObject



100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/foreman_remote_execution_core/polling_script_runner.rb', line 100

def callback_script
  "  | #!/bin/sh\n  | exit_code=$(cat \"\#{@exit_code_path}\")\n  | url=\"\#{@callback_host}/dynflow/tasks/\#{@task_id}/done\"\n  | json=\"{ \\\\\\\"step_id\\\\\\\": \#{@step_id} }\"\n  | if which curl >/dev/null; then\n  |   curl -X POST -d \"$json\" -u \"\#{@task_id}:\#{@otp}\" \"$url\"\n  | else\n  |   echo 'curl is required' >&2\n  |   exit 1\n  | fi\n  SCRIPT\nend\n".gsub(/^ +\| /, '')

#callback_scriptlet(callback_script_path = nil) ⇒ Object



91
92
93
94
95
96
97
98
# File 'lib/foreman_remote_execution_core/polling_script_runner.rb', line 91

def callback_scriptlet(callback_script_path = nil)
  if @otp
    callback_script_path = cp_script_to_remote(callback_script, 'callback') if callback_script_path.nil?
    "#{su_prefix}#{callback_script_path}"
  else
    ':' # Shell synonym for "do nothing"
  end
end

#closeObject



52
53
54
55
# File 'lib/foreman_remote_execution_core/polling_script_runner.rb', line 52

def close
  super
  ForemanTasksCore::OtpManager.drop_otp(@task_id, @otp) if @otp
end

#control_scriptObject



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'
  # pipe the output to tee while capturing the exit code in a file, don't wait for it to finish, output PID of the main command
  "  | sh -c '(\#{su_prefix}\#{@remote_script} \#{close_stdin}; echo $?>\#{@exit_code_path}) | /usr/bin/tee \#{@output_path} >/dev/null; \#{callback_scriptlet}' \#{close_fds} &\n  | echo $! > '\#{@pid_path}'\n  SCRIPT\nend\n".gsub(/^\s+\| /, '')

#prepare_retrievalObject



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
85
86
87
88
89
# File 'lib/foreman_remote_execution_core/polling_script_runner.rb', line 57

def prepare_retrieval
  # The script always outputs at least one line
  # First line of the output either has to begin with
  # "RUNNING" or "DONE $EXITCODE"
  # The following lines are treated as regular output
  base = File.dirname(@output_path)
  posfile = File.join(base, 'position')
  tmpfile = File.join(base, 'tmp')
  script = "  | #!/bin/sh\n  | pid=$(cat \"\#{@pid_path}\")\n  | if ! pgrep --help 2>/dev/null >/dev/null; then\n  |   echo DONE 1\n  |   echo \"pgrep is required\" >&2\n  |   exit 1\n  | fi\n  | if pgrep -P \"$pid\" >/dev/null 2>/dev/null; then\n  |   echo RUNNING\n  | else\n  |   echo \"DONE $(cat \"\#{@exit_code_path}\" 2>/dev/null)\"\n  | fi\n  | [ -f \"\#{@output_path}\" ] || exit 0\n  | [ -f \"\#{posfile}\" ] || echo 1 > \"\#{posfile}\"\n  | position=$(cat \"\#{posfile}\")\n  | tail --bytes \"+${position}\" \"\#{@output_path}\" > \"\#{tmpfile}\"\n  | bytes=$(cat \"\#{tmpfile}\" | wc --bytes)\n  | expr \"${position}\" + \"${bytes}\" > \"\#{posfile}\"\n  | cat \"\#{tmpfile}\"\n  SCRIPT\n  @logger.debug(\"copying script:\\n\#{script.lines.map { |line| \"    | \#{line}\" }.join}\")\n  cp_script_to_remote(script, 'retrieve')\n  @prepared = true\nend\n".gsub(/^ +\| /, '')

#prepare_startObject



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

#refreshObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/foreman_remote_execution_core/polling_script_runner.rb', line 36

def refresh
  err = output = nil
  with_retries do
    _, output, err = run_sync("#{su_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)
  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