Class: ForemanRemoteExecutionCore::ScriptRunner
- Inherits:
-
ForemanTasksCore::Runner::Base
- Object
- ForemanTasksCore::Runner::Base
- ForemanRemoteExecutionCore::ScriptRunner
- Defined in:
- lib/foreman_remote_execution_core/script_runner.rb
Direct Known Subclasses
Constant Summary collapse
- EXPECTED_POWER_ACTION_MESSAGES =
['restart host', 'shutdown host'].freeze
- DEFAULT_REFRESH_INTERVAL =
1
Instance Attribute Summary collapse
-
#execution_timeout_interval ⇒ Object
readonly
Returns the value of attribute execution_timeout_interval.
Instance Method Summary collapse
- #close ⇒ Object
- #control_script ⇒ Object
-
#initialize(options) ⇒ ScriptRunner
constructor
A new instance of ScriptRunner.
- #kill ⇒ Object
- #prepare_start ⇒ Object
- #publish_data(data, type) ⇒ Object
- #refresh ⇒ Object
- #start ⇒ Object
- #timeout ⇒ Object
- #timeout_interval ⇒ Object
- #trigger(*args) ⇒ Object
- #with_disconnect_handling ⇒ Object
- #with_retries ⇒ Object
Constructor Details
#initialize(options) ⇒ ScriptRunner
Returns a new instance of ScriptRunner.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/foreman_remote_execution_core/script_runner.rb', line 16 def initialize() super() @host = .fetch(:hostname) @script = .fetch(:script) @ssh_user = .fetch(:ssh_user, 'root') @ssh_port = .fetch(:ssh_port, 22) @effective_user = .fetch(:effective_user, nil) @effective_user_method = .fetch(:effective_user_method, 'sudo') @host_public_key = .fetch(:host_public_key, nil) @verify_host = .fetch(:verify_host, nil) @execution_timeout_interval = .fetch(:execution_timeout_interval, nil) @client_private_key_file = settings.fetch(:ssh_identity_key_file) @local_working_dir = .fetch(:local_working_dir, settings.fetch(:local_working_dir)) @remote_working_dir = .fetch(:remote_working_dir, settings.fetch(:remote_working_dir)) end |
Instance Attribute Details
#execution_timeout_interval ⇒ Object (readonly)
Returns the value of attribute execution_timeout_interval.
11 12 13 |
# File 'lib/foreman_remote_execution_core/script_runner.rb', line 11 def execution_timeout_interval @execution_timeout_interval end |
Instance Method Details
#close ⇒ Object
121 122 123 |
# File 'lib/foreman_remote_execution_core/script_runner.rb', line 121 def close @session.close if @session && !@session.closed? end |
#control_script ⇒ Object
53 54 55 56 57 58 59 60 61 |
# File 'lib/foreman_remote_execution_core/script_runner.rb', line 53 def control_script # pipe the output to tee while capturing the exit code in a file " | sh <<WRAPPER\n | (\#{su_prefix}\#{@remote_script} < /dev/null; echo \\\\$?>\#{@exit_code_path}) | /usr/bin/tee \#{@output_path}\n | exit \\\\$(cat \#{@exit_code_path})\n | WRAPPER\n SCRIPT\nend\n".gsub(/^\s+\| /, '') |
#kill ⇒ Object
74 75 76 77 78 79 80 81 82 |
# File 'lib/foreman_remote_execution_core/script_runner.rb', line 74 def kill if @session run_sync("pkill -f #{remote_command_file('script')}") else logger.debug('connection closed') end rescue => e publish_exception('Unexpected error', e, false) end |
#prepare_start ⇒ Object
47 48 49 50 51 |
# File 'lib/foreman_remote_execution_core/script_runner.rb', line 47 def prepare_start @remote_script = cp_script_to_remote @output_path = File.join(File.dirname(@remote_script), 'output') @exit_code_path = File.join(File.dirname(@remote_script), 'exit_code') end |
#publish_data(data, type) ⇒ Object
125 126 127 |
# File 'lib/foreman_remote_execution_core/script_runner.rb', line 125 def publish_data(data, type) super(data.force_encoding('UTF-8'), type) end |
#refresh ⇒ Object
63 64 65 66 67 68 69 70 71 72 |
# File 'lib/foreman_remote_execution_core/script_runner.rb', line 63 def refresh return if @session.nil? with_retries do with_disconnect_handling do @session.process(0) end end ensure check_expecting_disconnect end |
#start ⇒ Object
33 34 35 36 37 38 39 40 41 |
# File 'lib/foreman_remote_execution_core/script_runner.rb', line 33 def start prepare_start script = control_script logger.debug("executing script:\n#{script.lines.map { |line| " | #{line}" }.join}") trigger(script) rescue => e logger.error("error while initalizing command #{e.class} #{e.message}:\n #{e.backtrace.join("\n")}") publish_exception('Error initializing command', e) end |
#timeout ⇒ Object
84 85 86 87 |
# File 'lib/foreman_remote_execution_core/script_runner.rb', line 84 def timeout @logger.debug('job timed out') super end |
#timeout_interval ⇒ Object
89 90 91 |
# File 'lib/foreman_remote_execution_core/script_runner.rb', line 89 def timeout_interval execution_timeout_interval end |
#trigger(*args) ⇒ Object
43 44 45 |
# File 'lib/foreman_remote_execution_core/script_runner.rb', line 43 def trigger(*args) run_async(*args) end |
#with_disconnect_handling ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/foreman_remote_execution_core/script_runner.rb', line 109 def with_disconnect_handling yield rescue Net::SSH::Disconnect => e @session.shutdown! check_expecting_disconnect if @expecting_disconnect publish_exit_status(0) else publish_exception('Unexpected disconnect', e) end end |
#with_retries ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/foreman_remote_execution_core/script_runner.rb', line 93 def with_retries tries = 0 begin yield rescue => e logger.error("Unexpected error: #{e.class} #{e.message}\n #{e.backtrace.join("\n")}") tries += 1 if tries <= MAX_PROCESS_RETRIES logger.error('Retrying') retry else publish_exception('Unexpected error', e) end end end |