Class: Rufio::ScriptExecutor

Inherits:
Object
  • Object
show all
Defined in:
lib/rufio/script_executor.rb

Overview

スクリプトを安全に実行するクラス

Class Method Summary collapse

Class Method Details

.execute(interpreter, script_path, args = [], timeout: nil, chdir: nil, env: nil) ⇒ Hash

スクリプトを実行する

Parameters:

  • interpreter (String)

    インタープリタ(ruby, python3, bashなど)

  • script_path (String)

    スクリプトのパス

  • args (Array<String>) (defaults to: [])

    スクリプトへの引数

  • timeout (Numeric, nil) (defaults to: nil)

    タイムアウト秒数(nilの場合は無制限)

  • chdir (String, nil) (defaults to: nil)

    作業ディレクトリ

  • env (Hash, nil) (defaults to: nil)

    環境変数

Returns:

  • (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]

  # オプションを構築
  options = {}
  options[:chdir] = chdir if chdir
  # 環境変数をマージ(既存の環境変数を保持)
  spawn_env = env || {}

  execute_with_options(command, spawn_env, options, timeout)
rescue StandardError => e
  build_error_result(e)
end

.execute_command(dsl_command, args = [], timeout: nil, chdir: nil, env: nil) ⇒ Hash

DslCommandを実行する(タイプ別に分岐)

Parameters:

  • dsl_command (DslCommand)

    実行するDSLコマンド

  • args (Array<String>) (defaults to: [])

    追加の引数

  • timeout (Numeric, nil) (defaults to: nil)

    タイムアウト秒数

  • chdir (String, nil) (defaults to: nil)

    作業ディレクトリ

  • env (Hash, nil) (defaults to: nil)

    環境変数

Returns:

  • (Hash)

    実行結果



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コマンドを実行する

Parameters:

  • dsl_command (DslCommand)

    実行するDSLコマンド

Returns:

  • (Hash)

    実行結果



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.message,
    timeout: false
  }
end

.execute_shell(dsl_command, timeout: nil, chdir: nil, env: nil) ⇒ Hash

inline シェルコマンドを実行する

Parameters:

  • dsl_command (DslCommand)

    実行するDSLコマンド

  • timeout (Numeric, nil) (defaults to: nil)

    タイムアウト秒数

  • chdir (String, nil) (defaults to: nil)

    作業ディレクトリ

  • env (Hash, nil) (defaults to: nil)

    環境変数

Returns:

  • (Hash)

    実行結果



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)
  options = {}
  options[:chdir] = chdir if chdir
  spawn_env = env || {}

  if timeout
    execute_shell_with_timeout(dsl_command.shell_command, spawn_env, options, timeout)
  else
    execute_shell_without_timeout(dsl_command.shell_command, spawn_env, options)
  end
rescue StandardError => e
  build_error_result(e)
end