Class: SmartBox::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/smart_box/runner.rb

Constant Summary collapse

DANGEROUS_PATTERNS =

Simple dangerous command patterns (string matching per design spec §12.1)

[
  /\brm\s+-rf\s+\//,
  /\bmkfs\b/,
  /\bdd\s+if=/,
  /\bshutdown\b/,
  /\breboot\b/,
  /\bsudo\b/,
  /\bchmod\s+-R\s+777\s+\//,
  /\bchown\s+-R\b/
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(box_id:, workspace_path:, logs_dir:) ⇒ Runner

Returns a new instance of Runner.



26
27
28
29
30
31
# File 'lib/smart_box/runner.rb', line 26

def initialize(box_id:, workspace_path:, logs_dir:)
  @box_id         = box_id
  @workspace_path = File.expand_path(workspace_path)
  @logs_dir       = File.expand_path(logs_dir)
  @command_index  = 0
end

Instance Attribute Details

#box_idObject (readonly)

Returns the value of attribute box_id.



24
25
26
# File 'lib/smart_box/runner.rb', line 24

def box_id
  @box_id
end

#logs_dirObject (readonly)

Returns the value of attribute logs_dir.



24
25
26
# File 'lib/smart_box/runner.rb', line 24

def logs_dir
  @logs_dir
end

#workspace_pathObject (readonly)

Returns the value of attribute workspace_path.



24
25
26
# File 'lib/smart_box/runner.rb', line 24

def workspace_path
  @workspace_path
end

Instance Method Details

#run(command, env: {}, timeout: nil, allow_dangerous: false) ⇒ Object

— main entry point —



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/smart_box/runner.rb', line 35

def run(command, env: {}, timeout: nil, allow_dangerous: false)
  unless allow_dangerous
    check_dangerous!(command)
  end

  started_at = Time.now
  @command_index += 1
  idx = @command_index

  result = execute_command(command, env, timeout, started_at, idx)
  write_logs(command, result, idx)

  result
end