Class: Rufio::ScriptExecutor
- Inherits:
-
Object
- Object
- Rufio::ScriptExecutor
- Defined in:
- lib/rufio/script_executor.rb
Overview
スクリプトを安全に実行するクラス
Class Method Summary collapse
-
.execute(interpreter, script_path, args = [], timeout: nil, chdir: nil, env: nil) ⇒ Hash
スクリプトを実行する.
-
.execute_command(dsl_command, args = [], timeout: nil, chdir: nil, env: nil) ⇒ Hash
DslCommandを実行する(タイプ別に分岐).
-
.execute_ruby(dsl_command) ⇒ Hash
inline Rubyコマンドを実行する.
-
.execute_shell(dsl_command, timeout: nil, chdir: nil, env: nil) ⇒ Hash
inline シェルコマンドを実行する.
Class Method Details
.execute(interpreter, script_path, args = [], timeout: nil, chdir: nil, env: nil) ⇒ Hash
スクリプトを実行する
18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/rufio/script_executor.rb', line 18 def execute(interpreter, script_path, args = [], timeout: nil, chdir: nil, env: nil) # 配列ベースのコマンドを構築(シェルインジェクション防止) command = [interpreter, script_path, *args] # オプションを構築 = {} [:chdir] = chdir if chdir # 環境変数をマージ(既存の環境変数を保持) spawn_env = env || {} (command, spawn_env, , timeout) rescue StandardError => e build_error_result(e) end |
.execute_command(dsl_command, args = [], timeout: nil, chdir: nil, env: nil) ⇒ Hash
DslCommandを実行する(タイプ別に分岐)
40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/rufio/script_executor.rb', line 40 def execute_command(dsl_command, args = [], timeout: nil, chdir: nil, env: nil) case dsl_command.command_type when :ruby execute_ruby(dsl_command) when :shell execute_shell(dsl_command, timeout: timeout, chdir: chdir, env: env) else exec_args = dsl_command.to_execution_args execute(exec_args[0], exec_args[1], args, timeout: timeout, chdir: chdir, env: env) end end |
.execute_ruby(dsl_command) ⇒ Hash
inline Rubyコマンドを実行する
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/rufio/script_executor.rb', line 55 def execute_ruby(dsl_command) result = dsl_command.ruby_block.call { success: true, exit_code: 0, stdout: result.to_s, stderr: "", timeout: false } rescue StandardError => e { success: false, exit_code: 1, stdout: "", stderr: "", error: e., timeout: false } end |
.execute_shell(dsl_command, timeout: nil, chdir: nil, env: nil) ⇒ Hash
inline シェルコマンドを実行する
81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/rufio/script_executor.rb', line 81 def execute_shell(dsl_command, timeout: nil, chdir: nil, env: nil) = {} [:chdir] = chdir if chdir spawn_env = env || {} if timeout execute_shell_with_timeout(dsl_command.shell_command, spawn_env, , timeout) else execute_shell_without_timeout(dsl_command.shell_command, spawn_env, ) end rescue StandardError => e build_error_result(e) end |