Class: Proxy::RemoteExecution::Ssh::Runners::ScriptRunner
- Inherits:
-
Dynflow::Runner::Base
- Object
- Dynflow::Runner::Base
- Proxy::RemoteExecution::Ssh::Runners::ScriptRunner
- Includes:
- Dynflow::Runner::Command
- Defined in:
- lib/smart_proxy_remote_execution_ssh/runners/script_runner.rb
Overview
rubocop:disable Metrics/ClassLength
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.
Class Method Summary collapse
Instance Method Summary collapse
- #close ⇒ Object
- #close_session ⇒ Object
-
#initialization_script ⇒ Object
the script that initiates the execution.
-
#initialize(options, user_method, suspended_action: nil) ⇒ 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
Constructor Details
#initialize(options, user_method, suspended_action: nil) ⇒ ScriptRunner
Returns a new instance of ScriptRunner.
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/smart_proxy_remote_execution_ssh/runners/script_runner.rb', line 99 def initialize(, user_method, suspended_action: nil) super suspended_action: suspended_action @host = .fetch(:hostname) @script = .fetch(:script) @ssh_user = .fetch(:ssh_user, 'root') @ssh_port = .fetch(:ssh_port, 22) @ssh_password = .fetch(:secrets, {}).fetch(:ssh_password, nil) @key_passphrase = .fetch(:secrets, {}).fetch(:key_passphrase, nil) @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.ssh_identity_key_file @local_working_dir = .fetch(:local_working_dir, settings.local_working_dir) @remote_working_dir = .fetch(:remote_working_dir, settings.remote_working_dir) @cleanup_working_dirs = .fetch(:cleanup_working_dirs, settings.cleanup_working_dirs) @first_execution = .fetch(:first_execution, false) @user_method = user_method end |
Instance Attribute Details
#execution_timeout_interval ⇒ Object (readonly)
Returns the value of attribute execution_timeout_interval.
94 95 96 |
# File 'lib/smart_proxy_remote_execution_ssh/runners/script_runner.rb', line 94 def execution_timeout_interval @execution_timeout_interval end |
Class Method Details
.build(options, suspended_action:) ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/smart_proxy_remote_execution_ssh/runners/script_runner.rb', line 119 def self.build(, suspended_action:) effective_user = .fetch(:effective_user, nil) ssh_user = .fetch(:ssh_user, 'root') effective_user_method = .fetch(:effective_user_method, 'sudo') user_method = if effective_user.nil? || effective_user == ssh_user NoopUserMethod.new elsif effective_user_method == 'sudo' SudoUserMethod.new(effective_user, ssh_user, .fetch(:secrets, {}).fetch(:effective_user_password, nil)) elsif effective_user_method == 'dzdo' DzdoUserMethod.new(effective_user, ssh_user, .fetch(:secrets, {}).fetch(:effective_user_password, nil)) elsif effective_user_method == 'su' SuUserMethod.new(effective_user, ssh_user, .fetch(:secrets, {}).fetch(:effective_user_password, nil)) else raise "effective_user_method '#{effective_user_method}' not supported" end new(, user_method, suspended_action: suspended_action) end |
Instance Method Details
#close ⇒ Object
210 211 212 213 214 215 216 217 |
# File 'lib/smart_proxy_remote_execution_ssh/runners/script_runner.rb', line 210 def close run_sync("rm -rf \"#{remote_command_dir}\"") if should_cleanup? rescue StandardError => e publish_exception('Error when removing remote working dir', e, false) ensure close_session if @session FileUtils.rm_rf(local_command_dir) if Dir.exist?(local_command_dir) && @cleanup_working_dirs end |
#close_session ⇒ Object
199 200 201 202 203 204 205 206 207 208 |
# File 'lib/smart_proxy_remote_execution_ssh/runners/script_runner.rb', line 199 def close_session @session = nil raise 'Control socket file does not exist' unless File.exist?(local_command_file("socket")) @logger.debug("Sending exit request for session #{@ssh_user}@#{@host}") args = ['/usr/bin/ssh', @host, "-o", "User=#{@ssh_user}", "-o", "ControlPath=#{local_command_file("socket")}", "-O", "exit"].flatten pid, *, err = session(args, in_stream: false, out_stream: false) result = read_output_debug(err) Process.wait(pid) result end |
#initialization_script ⇒ Object
the script that initiates the execution
164 165 166 167 168 169 170 171 |
# File 'lib/smart_proxy_remote_execution_ssh/runners/script_runner.rb', line 164 def initialization_script su_method = @user_method.instance_of?(SuUserMethod) # pipe the output to tee while capturing the exit code in a file " | sh -c \"(\#{@user_method.cli_command_prefix}\#{su_method ? \"'\#{@remote_script} < /dev/null '\" : \"\#{@remote_script} < /dev/null\"}; echo \\\\$?>\#{@exit_code_path}) | /usr/bin/tee \#{@output_path}\n | exit \\\\$(cat \#{@exit_code_path})\"\n SCRIPT\nend\n".gsub(/^\s+\| /, '') |
#kill ⇒ Object
180 181 182 183 184 185 186 187 188 |
# File 'lib/smart_proxy_remote_execution_ssh/runners/script_runner.rb', line 180 def kill if @session run_sync("pkill -f #{remote_command_file('script')}") else logger.debug('connection closed') end rescue StandardError => e publish_exception('Unexpected error', e, false) end |
#prepare_start ⇒ Object
157 158 159 160 161 |
# File 'lib/smart_proxy_remote_execution_ssh/runners/script_runner.rb', line 157 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
219 220 221 222 |
# File 'lib/smart_proxy_remote_execution_ssh/runners/script_runner.rb', line 219 def publish_data(data, type) super(data.force_encoding('UTF-8'), type) unless @user_method.filter_password?(data) @user_method.on_data(data, @command_in) end |
#refresh ⇒ Object
173 174 175 176 177 178 |
# File 'lib/smart_proxy_remote_execution_ssh/runners/script_runner.rb', line 173 def refresh return if @session.nil? super ensure check_expecting_disconnect end |
#start ⇒ Object
142 143 144 145 146 147 148 149 150 151 |
# File 'lib/smart_proxy_remote_execution_ssh/runners/script_runner.rb', line 142 def start Proxy::RemoteExecution::Utils.prune_known_hosts!(@host, @ssh_port, logger) if @first_execution prepare_start script = initialization_script logger.debug("executing script:\n#{indent_multiline(script)}") trigger(script) rescue StandardError, NotImplementedError => e logger.error("error while initializing command #{e.class} #{e.message}:\n #{e.backtrace.join("\n")}") publish_exception('Error initializing command', e) end |
#timeout ⇒ Object
190 191 192 193 |
# File 'lib/smart_proxy_remote_execution_ssh/runners/script_runner.rb', line 190 def timeout @logger.debug('job timed out') super end |
#timeout_interval ⇒ Object
195 196 197 |
# File 'lib/smart_proxy_remote_execution_ssh/runners/script_runner.rb', line 195 def timeout_interval execution_timeout_interval end |
#trigger(*args) ⇒ Object
153 154 155 |
# File 'lib/smart_proxy_remote_execution_ssh/runners/script_runner.rb', line 153 def trigger(*args) run_async(*args) end |